blob: 8bae6b22c8461c7a03fd15f86bd672a662b0b437 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Herbert Xu1a6509d2008-01-28 19:37:29 -080034static inline int aead_len(struct xfrm_algo_aead *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080048 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 break;
56
57 default:
58 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
62 return 0;
63}
64
Martin Willi4447bb32009-11-25 00:29:52 +000065static int verify_auth_trunc(struct nlattr **attrs)
66{
67 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
68 struct xfrm_algo_auth *algp;
69
70 if (!rt)
71 return 0;
72
73 algp = nla_data(rt);
74 if (nla_len(rt) < xfrm_alg_auth_len(algp))
75 return -EINVAL;
76
77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
78 return 0;
79}
80
Herbert Xu1a6509d2008-01-28 19:37:29 -080081static int verify_aead(struct nlattr **attrs)
82{
83 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
84 struct xfrm_algo_aead *algp;
85
86 if (!rt)
87 return 0;
88
89 algp = nla_data(rt);
90 if (nla_len(rt) < aead_len(algp))
91 return -EINVAL;
92
93 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
94 return 0;
95}
96
Thomas Graf5424f322007-08-22 14:01:33 -070097static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098 xfrm_address_t **addrp)
99{
Thomas Graf5424f322007-08-22 14:01:33 -0700100 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101
Thomas Grafcf5cb792007-08-22 13:59:04 -0700102 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700103 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700104}
Trent Jaegerdf718372005-12-13 23:12:27 -0800105
Thomas Graf5424f322007-08-22 14:01:33 -0700106static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800107{
Thomas Graf5424f322007-08-22 14:01:33 -0700108 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800109 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800110
111 if (!rt)
112 return 0;
113
Thomas Graf5424f322007-08-22 14:01:33 -0700114 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700115 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800116 return -EINVAL;
117
118 return 0;
119}
120
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700123 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int err;
126
127 err = -EINVAL;
128 switch (p->family) {
129 case AF_INET:
130 break;
131
132 case AF_INET6:
133#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
134 break;
135#else
136 err = -EAFNOSUPPORT;
137 goto out;
138#endif
139
140 default:
141 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 err = -EINVAL;
145 switch (p->id.proto) {
146 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000147 if ((!attrs[XFRMA_ALG_AUTH] &&
148 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800149 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700150 attrs[XFRMA_ALG_CRYPT] ||
151 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto out;
153 break;
154
155 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800156 if (attrs[XFRMA_ALG_COMP])
157 goto out;
158 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000159 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800160 !attrs[XFRMA_ALG_CRYPT] &&
161 !attrs[XFRMA_ALG_AEAD])
162 goto out;
163 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000164 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800165 attrs[XFRMA_ALG_CRYPT]) &&
166 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
168 break;
169
170 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700171 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800172 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700173 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000174 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700175 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto out;
177 break;
178
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700179#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 if (attrs[XFRMA_ALG_COMP] ||
183 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000184 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800185 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700186 attrs[XFRMA_ALG_CRYPT] ||
187 attrs[XFRMA_ENCAP] ||
188 attrs[XFRMA_SEC_CTX] ||
189 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700190 goto out;
191 break;
192#endif
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 default:
195 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 if ((err = verify_aead(attrs)))
199 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000200 if ((err = verify_auth_trunc(attrs)))
201 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700202 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700208 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err = -EINVAL;
212 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700213 case XFRM_MODE_TRANSPORT:
214 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700215 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700216 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
218
219 default:
220 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 err = 0;
224
225out:
226 return err;
227}
228
229static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
230 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700231 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
235
236 if (!rta)
237 return 0;
238
Thomas Graf5424f322007-08-22 14:01:33 -0700239 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 algo = get_byname(ualg->alg_name, 1);
242 if (!algo)
243 return -ENOSYS;
244 *props = algo->desc.sadb_alg_id;
245
Eric Dumazet0f99be02008-01-08 23:39:06 -0800246 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!p)
248 return -ENOMEM;
249
Herbert Xu04ff1262006-08-13 08:50:00 +1000250 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *algpp = p;
252 return 0;
253}
254
Martin Willi4447bb32009-11-25 00:29:52 +0000255static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
256 struct nlattr *rta)
257{
258 struct xfrm_algo *ualg;
259 struct xfrm_algo_auth *p;
260 struct xfrm_algo_desc *algo;
261
262 if (!rta)
263 return 0;
264
265 ualg = nla_data(rta);
266
267 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
268 if (!algo)
269 return -ENOSYS;
270 *props = algo->desc.sadb_alg_id;
271
272 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
273 if (!p)
274 return -ENOMEM;
275
276 strcpy(p->alg_name, algo->name);
277 p->alg_key_len = ualg->alg_key_len;
278 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
279 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
280
281 *algpp = p;
282 return 0;
283}
284
285static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
286 struct nlattr *rta)
287{
288 struct xfrm_algo_auth *p, *ualg;
289 struct xfrm_algo_desc *algo;
290
291 if (!rta)
292 return 0;
293
294 ualg = nla_data(rta);
295
296 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
297 if (!algo)
298 return -ENOSYS;
299 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
300 return -EINVAL;
301 *props = algo->desc.sadb_alg_id;
302
303 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
304 if (!p)
305 return -ENOMEM;
306
307 strcpy(p->alg_name, algo->name);
308 if (!p->alg_trunc_len)
309 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
310
311 *algpp = p;
312 return 0;
313}
314
Herbert Xu1a6509d2008-01-28 19:37:29 -0800315static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
316 struct nlattr *rta)
317{
318 struct xfrm_algo_aead *p, *ualg;
319 struct xfrm_algo_desc *algo;
320
321 if (!rta)
322 return 0;
323
324 ualg = nla_data(rta);
325
326 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
327 if (!algo)
328 return -ENOSYS;
329 *props = algo->desc.sadb_alg_id;
330
331 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
332 if (!p)
333 return -ENOMEM;
334
335 strcpy(p->alg_name, algo->name);
336 *algpp = p;
337 return 0;
338}
339
Joy Latten661697f2007-04-13 16:14:35 -0700340static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800341{
Trent Jaegerdf718372005-12-13 23:12:27 -0800342 int len = 0;
343
344 if (xfrm_ctx) {
345 len += sizeof(struct xfrm_user_sec_ctx);
346 len += xfrm_ctx->ctx_len;
347 }
348 return len;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
352{
353 memcpy(&x->id, &p->id, sizeof(x->id));
354 memcpy(&x->sel, &p->sel, sizeof(x->sel));
355 memcpy(&x->lft, &p->lft, sizeof(x->lft));
356 x->props.mode = p->mode;
357 x->props.replay_window = p->replay_window;
358 x->props.reqid = p->reqid;
359 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700360 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700362
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700363 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700364 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800367/*
368 * someday when pfkey also has support, we could have the code
369 * somehow made shareable and move it to xfrm_state.c - JHS
370 *
371*/
Thomas Graf5424f322007-08-22 14:01:33 -0700372static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800373{
Thomas Graf5424f322007-08-22 14:01:33 -0700374 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
375 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
376 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
377 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800378
379 if (rp) {
380 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700381 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800382 memcpy(&x->replay, replay, sizeof(*replay));
383 memcpy(&x->preplay, replay, sizeof(*replay));
384 }
385
386 if (lt) {
387 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700388 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800389 x->curlft.bytes = ltime->bytes;
390 x->curlft.packets = ltime->packets;
391 x->curlft.add_time = ltime->add_time;
392 x->curlft.use_time = ltime->use_time;
393 }
394
Thomas Grafcf5cb792007-08-22 13:59:04 -0700395 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700396 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800397
Thomas Grafcf5cb792007-08-22 13:59:04 -0700398 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700399 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800400}
401
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800402static struct xfrm_state *xfrm_state_construct(struct net *net,
403 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700404 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int *errp)
406{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800407 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int err = -ENOMEM;
409
410 if (!x)
411 goto error_no_put;
412
413 copy_from_user_state(x, p);
414
Herbert Xu1a6509d2008-01-28 19:37:29 -0800415 if ((err = attach_aead(&x->aead, &x->props.ealgo,
416 attrs[XFRMA_ALG_AEAD])))
417 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000418 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
419 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000421 if (!x->props.aalgo) {
422 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
423 attrs[XFRMA_ALG_AUTH])))
424 goto error;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
427 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700428 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto error;
430 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
431 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700432 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700434
435 if (attrs[XFRMA_ENCAP]) {
436 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
437 sizeof(*x->encap), GFP_KERNEL);
438 if (x->encap == NULL)
439 goto error;
440 }
441
442 if (attrs[XFRMA_COADDR]) {
443 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
444 sizeof(*x->coaddr), GFP_KERNEL);
445 if (x->coaddr == NULL)
446 goto error;
447 }
448
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000449 xfrm_mark_get(attrs, &x->mark);
450
Herbert Xu72cb6962005-06-20 13:18:08 -0700451 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (err)
453 goto error;
454
Thomas Graffd211502007-09-06 03:28:08 -0700455 if (attrs[XFRMA_SEC_CTX] &&
456 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800457 goto error;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800460 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800461 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800462 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463 x->preplay.bitmap = 0;
464 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
465 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
466
467 /* override default values from above */
468
Thomas Graf5424f322007-08-22 14:01:33 -0700469 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return x;
472
473error:
474 x->km.state = XFRM_STATE_DEAD;
475 xfrm_state_put(x);
476error_no_put:
477 *errp = err;
478 return NULL;
479}
480
Christoph Hellwig22e70052007-01-02 15:22:30 -0800481static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700482 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800484 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700485 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct xfrm_state *x;
487 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700488 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400489 uid_t loginuid = NETLINK_CB(skb).loginuid;
490 u32 sessionid = NETLINK_CB(skb).sessionid;
491 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Thomas Graf35a7aa02007-08-22 14:00:40 -0700493 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (err)
495 return err;
496
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800497 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!x)
499 return err;
500
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700501 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
503 err = xfrm_state_add(x);
504 else
505 err = xfrm_state_update(x);
506
Eric Paris25323862008-04-18 10:09:25 -0400507 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (err < 0) {
510 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800511 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700515 c.seq = nlh->nlmsg_seq;
516 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700517 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700518
519 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700520out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700521 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return err;
523}
524
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800525static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
526 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700527 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700528 int *errp)
529{
530 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000531 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700532 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000533 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700534
535 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
536 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000537 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700538 } else {
539 xfrm_address_t *saddr = NULL;
540
Thomas Graf35a7aa02007-08-22 14:00:40 -0700541 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700542 if (!saddr) {
543 err = -EINVAL;
544 goto out;
545 }
546
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800547 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000548 x = xfrm_state_lookup_byaddr(net, mark,
549 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800550 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700551 }
552
553 out:
554 if (!x && errp)
555 *errp = err;
556 return x;
557}
558
Christoph Hellwig22e70052007-01-02 15:22:30 -0800559static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700560 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800562 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700564 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700565 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700566 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400567 uid_t loginuid = NETLINK_CB(skb).loginuid;
568 u32 sessionid = NETLINK_CB(skb).sessionid;
569 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800571 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
David S. Miller6f68dc32006-06-08 23:58:52 -0700575 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700576 goto out;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700579 err = -EPERM;
580 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700583 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600584
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700585 if (err < 0)
586 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700587
588 c.seq = nlh->nlmsg_seq;
589 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700590 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700591 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700593out:
Eric Paris25323862008-04-18 10:09:25 -0400594 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700595 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700596 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
600{
601 memcpy(&p->id, &x->id, sizeof(p->id));
602 memcpy(&p->sel, &x->sel, sizeof(p->sel));
603 memcpy(&p->lft, &x->lft, sizeof(p->lft));
604 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
605 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700606 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 p->mode = x->props.mode;
608 p->replay_window = x->props.replay_window;
609 p->reqid = x->props.reqid;
610 p->family = x->props.family;
611 p->flags = x->props.flags;
612 p->seq = x->km.seq;
613}
614
615struct xfrm_dump_info {
616 struct sk_buff *in_skb;
617 struct sk_buff *out_skb;
618 u32 nlmsg_seq;
619 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620};
621
Thomas Grafc0144be2007-08-22 13:55:43 -0700622static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
623{
Thomas Grafc0144be2007-08-22 13:55:43 -0700624 struct xfrm_user_sec_ctx *uctx;
625 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700626 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700627
628 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
629 if (attr == NULL)
630 return -EMSGSIZE;
631
632 uctx = nla_data(attr);
633 uctx->exttype = XFRMA_SEC_CTX;
634 uctx->len = ctx_size;
635 uctx->ctx_doi = s->ctx_doi;
636 uctx->ctx_alg = s->ctx_alg;
637 uctx->ctx_len = s->ctx_len;
638 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
639
640 return 0;
641}
642
Martin Willi4447bb32009-11-25 00:29:52 +0000643static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
644{
645 struct xfrm_algo *algo;
646 struct nlattr *nla;
647
648 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
649 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
650 if (!nla)
651 return -EMSGSIZE;
652
653 algo = nla_data(nla);
654 strcpy(algo->alg_name, auth->alg_name);
655 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
656 algo->alg_key_len = auth->alg_key_len;
657
658 return 0;
659}
660
Herbert Xu68325d32007-10-09 13:30:57 -0700661/* Don't change this without updating xfrm_sa_len! */
662static int copy_to_user_state_extra(struct xfrm_state *x,
663 struct xfrm_usersa_info *p,
664 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 copy_to_user_state(x, p);
667
Herbert Xu050f0092007-10-09 13:31:47 -0700668 if (x->coaddr)
669 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
670
671 if (x->lastused)
672 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700673
Herbert Xu1a6509d2008-01-28 19:37:29 -0800674 if (x->aead)
675 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000676 if (x->aalg) {
677 if (copy_to_user_auth(x->aalg, skb))
678 goto nla_put_failure;
679
680 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
681 xfrm_alg_auth_len(x->aalg), x->aalg);
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800684 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700686 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700689 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000691 if (xfrm_mark_put(skb, &x->mark))
692 goto nla_put_failure;
693
Thomas Grafc0144be2007-08-22 13:55:43 -0700694 if (x->security && copy_sec_ctx(x->security, skb) < 0)
695 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700696
Herbert Xu68325d32007-10-09 13:30:57 -0700697 return 0;
698
699nla_put_failure:
700 return -EMSGSIZE;
701}
702
703static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
704{
705 struct xfrm_dump_info *sp = ptr;
706 struct sk_buff *in_skb = sp->in_skb;
707 struct sk_buff *skb = sp->out_skb;
708 struct xfrm_usersa_info *p;
709 struct nlmsghdr *nlh;
710 int err;
711
Herbert Xu68325d32007-10-09 13:30:57 -0700712 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
713 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
714 if (nlh == NULL)
715 return -EMSGSIZE;
716
717 p = nlmsg_data(nlh);
718
719 err = copy_to_user_state_extra(x, p, skb);
720 if (err)
721 goto nla_put_failure;
722
Thomas Graf98250692007-08-22 12:47:26 -0700723 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725
Thomas Grafc0144be2007-08-22 13:55:43 -0700726nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700727 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Timo Teras4c563f72008-02-28 21:31:08 -0800731static int xfrm_dump_sa_done(struct netlink_callback *cb)
732{
733 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
734 xfrm_state_walk_done(walk);
735 return 0;
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
739{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800740 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800741 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct xfrm_dump_info info;
743
Timo Teras4c563f72008-02-28 21:31:08 -0800744 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
745 sizeof(cb->args) - sizeof(cb->args[0]));
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 info.in_skb = cb->skb;
748 info.out_skb = skb;
749 info.nlmsg_seq = cb->nlh->nlmsg_seq;
750 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800751
752 if (!cb->args[0]) {
753 cb->args[0] = 1;
754 xfrm_state_walk_init(walk, 0);
755 }
756
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800757 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return skb->len;
760}
761
762static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
763 struct xfrm_state *x, u32 seq)
764{
765 struct xfrm_dump_info info;
766 struct sk_buff *skb;
767
Thomas Graf7deb2262007-08-22 13:57:39 -0700768 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (!skb)
770 return ERR_PTR(-ENOMEM);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 info.in_skb = in_skb;
773 info.out_skb = skb;
774 info.nlmsg_seq = seq;
775 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (dump_one_state(x, 0, &info)) {
778 kfree_skb(skb);
779 return NULL;
780 }
781
782 return skb;
783}
784
Thomas Graf7deb2262007-08-22 13:57:39 -0700785static inline size_t xfrm_spdinfo_msgsize(void)
786{
787 return NLMSG_ALIGN(4)
788 + nla_total_size(sizeof(struct xfrmu_spdinfo))
789 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
790}
791
Alexey Dobriyane0710412010-01-23 13:37:10 +0000792static int build_spdinfo(struct sk_buff *skb, struct net *net,
793 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700794{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700795 struct xfrmk_spdinfo si;
796 struct xfrmu_spdinfo spc;
797 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700798 struct nlmsghdr *nlh;
799 u32 *f;
800
801 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
802 if (nlh == NULL) /* shouldnt really happen ... */
803 return -EMSGSIZE;
804
805 f = nlmsg_data(nlh);
806 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000807 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700808 spc.incnt = si.incnt;
809 spc.outcnt = si.outcnt;
810 spc.fwdcnt = si.fwdcnt;
811 spc.inscnt = si.inscnt;
812 spc.outscnt = si.outscnt;
813 spc.fwdscnt = si.fwdscnt;
814 sph.spdhcnt = si.spdhcnt;
815 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700816
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700817 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
818 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700819
820 return nlmsg_end(skb, nlh);
821
822nla_put_failure:
823 nlmsg_cancel(skb, nlh);
824 return -EMSGSIZE;
825}
826
827static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700828 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700829{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800830 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700831 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700832 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700833 u32 spid = NETLINK_CB(skb).pid;
834 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700835
Thomas Graf7deb2262007-08-22 13:57:39 -0700836 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700837 if (r_skb == NULL)
838 return -ENOMEM;
839
Alexey Dobriyane0710412010-01-23 13:37:10 +0000840 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700841 BUG();
842
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800843 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700844}
845
Thomas Graf7deb2262007-08-22 13:57:39 -0700846static inline size_t xfrm_sadinfo_msgsize(void)
847{
848 return NLMSG_ALIGN(4)
849 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
850 + nla_total_size(4); /* XFRMA_SAD_CNT */
851}
852
Alexey Dobriyane0710412010-01-23 13:37:10 +0000853static int build_sadinfo(struct sk_buff *skb, struct net *net,
854 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700855{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700856 struct xfrmk_sadinfo si;
857 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700858 struct nlmsghdr *nlh;
859 u32 *f;
860
861 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
862 if (nlh == NULL) /* shouldnt really happen ... */
863 return -EMSGSIZE;
864
865 f = nlmsg_data(nlh);
866 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000867 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700868
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700869 sh.sadhmcnt = si.sadhmcnt;
870 sh.sadhcnt = si.sadhcnt;
871
872 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
873 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700874
875 return nlmsg_end(skb, nlh);
876
877nla_put_failure:
878 nlmsg_cancel(skb, nlh);
879 return -EMSGSIZE;
880}
881
882static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700883 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700884{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800885 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700886 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700887 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700888 u32 spid = NETLINK_CB(skb).pid;
889 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700890
Thomas Graf7deb2262007-08-22 13:57:39 -0700891 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700892 if (r_skb == NULL)
893 return -ENOMEM;
894
Alexey Dobriyane0710412010-01-23 13:37:10 +0000895 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700896 BUG();
897
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800898 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700899}
900
Christoph Hellwig22e70052007-01-02 15:22:30 -0800901static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700902 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800904 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700905 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct xfrm_state *x;
907 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700908 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800910 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (x == NULL)
912 goto out_noput;
913
914 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
915 if (IS_ERR(resp_skb)) {
916 err = PTR_ERR(resp_skb);
917 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800918 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 xfrm_state_put(x);
921out_noput:
922 return err;
923}
924
925static int verify_userspi_info(struct xfrm_userspi_info *p)
926{
927 switch (p->info.id.proto) {
928 case IPPROTO_AH:
929 case IPPROTO_ESP:
930 break;
931
932 case IPPROTO_COMP:
933 /* IPCOMP spi is 16-bits. */
934 if (p->max >= 0x10000)
935 return -EINVAL;
936 break;
937
938 default:
939 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (p->min > p->max)
943 return -EINVAL;
944
945 return 0;
946}
947
Christoph Hellwig22e70052007-01-02 15:22:30 -0800948static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700949 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800951 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct xfrm_state *x;
953 struct xfrm_userspi_info *p;
954 struct sk_buff *resp_skb;
955 xfrm_address_t *daddr;
956 int family;
957 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000958 u32 mark;
959 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Thomas Graf7b67c852007-08-22 13:53:52 -0700961 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 err = verify_userspi_info(p);
963 if (err)
964 goto out_noput;
965
966 family = p->info.family;
967 daddr = &p->info.id.daddr;
968
969 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000970
971 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000973 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
975 xfrm_state_put(x);
976 x = NULL;
977 }
978 }
979
980 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000981 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 p->info.id.proto, daddr,
983 &p->info.saddr, 1,
984 family);
985 err = -ENOENT;
986 if (x == NULL)
987 goto out_noput;
988
Herbert Xu658b2192007-10-09 13:29:52 -0700989 err = xfrm_alloc_spi(x, p->min, p->max);
990 if (err)
991 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Herbert Xu658b2192007-10-09 13:29:52 -0700993 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (IS_ERR(resp_skb)) {
995 err = PTR_ERR(resp_skb);
996 goto out;
997 }
998
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800999 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001out:
1002 xfrm_state_put(x);
1003out_noput:
1004 return err;
1005}
1006
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001007static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 switch (dir) {
1010 case XFRM_POLICY_IN:
1011 case XFRM_POLICY_OUT:
1012 case XFRM_POLICY_FWD:
1013 break;
1014
1015 default:
1016 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 return 0;
1020}
1021
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001022static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001023{
1024 switch (type) {
1025 case XFRM_POLICY_TYPE_MAIN:
1026#ifdef CONFIG_XFRM_SUB_POLICY
1027 case XFRM_POLICY_TYPE_SUB:
1028#endif
1029 break;
1030
1031 default:
1032 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001033 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001034
1035 return 0;
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1039{
1040 switch (p->share) {
1041 case XFRM_SHARE_ANY:
1042 case XFRM_SHARE_SESSION:
1043 case XFRM_SHARE_USER:
1044 case XFRM_SHARE_UNIQUE:
1045 break;
1046
1047 default:
1048 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 switch (p->action) {
1052 case XFRM_POLICY_ALLOW:
1053 case XFRM_POLICY_BLOCK:
1054 break;
1055
1056 default:
1057 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 switch (p->sel.family) {
1061 case AF_INET:
1062 break;
1063
1064 case AF_INET6:
1065#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1066 break;
1067#else
1068 return -EAFNOSUPPORT;
1069#endif
1070
1071 default:
1072 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 return verify_policy_dir(p->dir);
1076}
1077
Thomas Graf5424f322007-08-22 14:01:33 -07001078static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001079{
Thomas Graf5424f322007-08-22 14:01:33 -07001080 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001081 struct xfrm_user_sec_ctx *uctx;
1082
1083 if (!rt)
1084 return 0;
1085
Thomas Graf5424f322007-08-22 14:01:33 -07001086 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001087 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1091 int nr)
1092{
1093 int i;
1094
1095 xp->xfrm_nr = nr;
1096 for (i = 0; i < nr; i++, ut++) {
1097 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1098
1099 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1100 memcpy(&t->saddr, &ut->saddr,
1101 sizeof(xfrm_address_t));
1102 t->reqid = ut->reqid;
1103 t->mode = ut->mode;
1104 t->share = ut->share;
1105 t->optional = ut->optional;
1106 t->aalgos = ut->aalgos;
1107 t->ealgos = ut->ealgos;
1108 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001109 /* If all masks are ~0, then we allow all algorithms. */
1110 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001111 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113}
1114
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001115static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1116{
1117 int i;
1118
1119 if (nr > XFRM_MAX_DEPTH)
1120 return -EINVAL;
1121
1122 for (i = 0; i < nr; i++) {
1123 /* We never validated the ut->family value, so many
1124 * applications simply leave it at zero. The check was
1125 * never made and ut->family was ignored because all
1126 * templates could be assumed to have the same family as
1127 * the policy itself. Now that we will have ipv4-in-ipv6
1128 * and ipv6-in-ipv4 tunnels, this is no longer true.
1129 */
1130 if (!ut[i].family)
1131 ut[i].family = family;
1132
1133 switch (ut[i].family) {
1134 case AF_INET:
1135 break;
1136#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1137 case AF_INET6:
1138 break;
1139#endif
1140 default:
1141 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001142 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001143 }
1144
1145 return 0;
1146}
1147
Thomas Graf5424f322007-08-22 14:01:33 -07001148static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Thomas Graf5424f322007-08-22 14:01:33 -07001150 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (!rt) {
1153 pol->xfrm_nr = 0;
1154 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001155 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1156 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001159 err = validate_tmpl(nr, utmpl, pol->family);
1160 if (err)
1161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Thomas Graf5424f322007-08-22 14:01:33 -07001163 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return 0;
1166}
1167
Thomas Graf5424f322007-08-22 14:01:33 -07001168static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001169{
Thomas Graf5424f322007-08-22 14:01:33 -07001170 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001171 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001172 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001173 int err;
1174
1175 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001176 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001177 type = upt->type;
1178 }
1179
1180 err = verify_policy_type(type);
1181 if (err)
1182 return err;
1183
1184 *tp = type;
1185 return 0;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1189{
1190 xp->priority = p->priority;
1191 xp->index = p->index;
1192 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1193 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1194 xp->action = p->action;
1195 xp->flags = p->flags;
1196 xp->family = p->sel.family;
1197 /* XXX xp->share = p->share; */
1198}
1199
1200static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1201{
1202 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1203 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1204 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1205 p->priority = xp->priority;
1206 p->index = xp->index;
1207 p->sel.family = xp->family;
1208 p->dir = dir;
1209 p->action = xp->action;
1210 p->flags = xp->flags;
1211 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1212}
1213
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001214static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001216 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int err;
1218
1219 if (!xp) {
1220 *errp = -ENOMEM;
1221 return NULL;
1222 }
1223
1224 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001225
Thomas Graf35a7aa02007-08-22 14:00:40 -07001226 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001227 if (err)
1228 goto error;
1229
Thomas Graf35a7aa02007-08-22 14:00:40 -07001230 if (!(err = copy_from_user_tmpl(xp, attrs)))
1231 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001232 if (err)
1233 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001235 xfrm_mark_get(attrs, &xp->mark);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001238 error:
1239 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001240 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001241 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Christoph Hellwig22e70052007-01-02 15:22:30 -08001245static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001246 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001248 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001249 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001251 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int err;
1253 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001254 uid_t loginuid = NETLINK_CB(skb).loginuid;
1255 u32 sessionid = NETLINK_CB(skb).sessionid;
1256 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 err = verify_newpolicy_info(p);
1259 if (err)
1260 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001261 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001262 if (err)
1263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001265 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!xp)
1267 return err;
1268
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001269 /* shouldnt excl be based on nlh flags??
1270 * Aha! this is anti-netlink really i.e more pfkey derived
1271 * in netlink excl is a flag and you wouldnt need
1272 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1274 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001275 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001278 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 kfree(xp);
1280 return err;
1281 }
1282
Herbert Xuf60f6b82005-06-18 22:44:37 -07001283 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001284 c.seq = nlh->nlmsg_seq;
1285 c.pid = nlh->nlmsg_pid;
1286 km_policy_notify(xp, p->dir, &c);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 xfrm_pol_put(xp);
1289
1290 return 0;
1291}
1292
1293static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1294{
1295 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1296 int i;
1297
1298 if (xp->xfrm_nr == 0)
1299 return 0;
1300
1301 for (i = 0; i < xp->xfrm_nr; i++) {
1302 struct xfrm_user_tmpl *up = &vec[i];
1303 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1304
1305 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001306 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1308 up->reqid = kp->reqid;
1309 up->mode = kp->mode;
1310 up->share = kp->share;
1311 up->optional = kp->optional;
1312 up->aalgos = kp->aalgos;
1313 up->ealgos = kp->ealgos;
1314 up->calgos = kp->calgos;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Thomas Grafc0144be2007-08-22 13:55:43 -07001317 return nla_put(skb, XFRMA_TMPL,
1318 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001319}
1320
Serge Hallyn0d681622006-07-24 23:30:44 -07001321static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1322{
1323 if (x->security) {
1324 return copy_sec_ctx(x->security, skb);
1325 }
1326 return 0;
1327}
1328
1329static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1330{
1331 if (xp->security) {
1332 return copy_sec_ctx(xp->security, skb);
1333 }
1334 return 0;
1335}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001336static inline size_t userpolicy_type_attrsize(void)
1337{
1338#ifdef CONFIG_XFRM_SUB_POLICY
1339 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1340#else
1341 return 0;
1342#endif
1343}
Serge Hallyn0d681622006-07-24 23:30:44 -07001344
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001345#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001346static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001347{
Thomas Grafc0144be2007-08-22 13:55:43 -07001348 struct xfrm_userpolicy_type upt = {
1349 .type = type,
1350 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001351
Thomas Grafc0144be2007-08-22 13:55:43 -07001352 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001353}
1354
1355#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001356static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001357{
1358 return 0;
1359}
1360#endif
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1363{
1364 struct xfrm_dump_info *sp = ptr;
1365 struct xfrm_userpolicy_info *p;
1366 struct sk_buff *in_skb = sp->in_skb;
1367 struct sk_buff *skb = sp->out_skb;
1368 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001370 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1371 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1372 if (nlh == NULL)
1373 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Thomas Graf7b67c852007-08-22 13:53:52 -07001375 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 copy_to_user_policy(xp, p, dir);
1377 if (copy_to_user_tmpl(xp, skb) < 0)
1378 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001379 if (copy_to_user_sec_ctx(xp, skb))
1380 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001381 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001382 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001383 if (xfrm_mark_put(skb, &xp->mark))
1384 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Thomas Graf98250692007-08-22 12:47:26 -07001386 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return 0;
1388
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001389nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001391 nlmsg_cancel(skb, nlh);
1392 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Timo Teras4c563f72008-02-28 21:31:08 -08001395static int xfrm_dump_policy_done(struct netlink_callback *cb)
1396{
1397 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1398
1399 xfrm_policy_walk_done(walk);
1400 return 0;
1401}
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1404{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001405 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001406 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct xfrm_dump_info info;
1408
Timo Teras4c563f72008-02-28 21:31:08 -08001409 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1410 sizeof(cb->args) - sizeof(cb->args[0]));
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 info.in_skb = cb->skb;
1413 info.out_skb = skb;
1414 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1415 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001416
1417 if (!cb->args[0]) {
1418 cb->args[0] = 1;
1419 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1420 }
1421
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001422 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 return skb->len;
1425}
1426
1427static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1428 struct xfrm_policy *xp,
1429 int dir, u32 seq)
1430{
1431 struct xfrm_dump_info info;
1432 struct sk_buff *skb;
1433
Thomas Graf7deb2262007-08-22 13:57:39 -07001434 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!skb)
1436 return ERR_PTR(-ENOMEM);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 info.in_skb = in_skb;
1439 info.out_skb = skb;
1440 info.nlmsg_seq = seq;
1441 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1444 kfree_skb(skb);
1445 return NULL;
1446 }
1447
1448 return skb;
1449}
1450
Christoph Hellwig22e70052007-01-02 15:22:30 -08001451static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001452 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001454 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct xfrm_policy *xp;
1456 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001457 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001459 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001461 struct xfrm_mark m;
1462 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Thomas Graf7b67c852007-08-22 13:53:52 -07001464 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1466
Thomas Graf35a7aa02007-08-22 14:00:40 -07001467 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001468 if (err)
1469 return err;
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 err = verify_policy_dir(p->dir);
1472 if (err)
1473 return err;
1474
1475 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001476 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001477 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001478 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001479 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001480
Thomas Graf35a7aa02007-08-22 14:00:40 -07001481 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001482 if (err)
1483 return err;
1484
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001485 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001486 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001487 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001488
Paul Moore03e1ad72008-04-12 19:07:52 -07001489 err = security_xfrm_policy_alloc(&ctx, uctx);
1490 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001491 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001492 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001493 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001494 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001495 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (xp == NULL)
1498 return -ENOENT;
1499
1500 if (!delete) {
1501 struct sk_buff *resp_skb;
1502
1503 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1504 if (IS_ERR(resp_skb)) {
1505 err = PTR_ERR(resp_skb);
1506 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001507 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001508 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001510 } else {
Eric Paris25323862008-04-18 10:09:25 -04001511 uid_t loginuid = NETLINK_CB(skb).loginuid;
1512 u32 sessionid = NETLINK_CB(skb).sessionid;
1513 u32 sid = NETLINK_CB(skb).sid;
1514
1515 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1516 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001517
1518 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001519 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001520
Herbert Xue7443892005-06-18 22:44:18 -07001521 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001522 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001523 c.seq = nlh->nlmsg_seq;
1524 c.pid = nlh->nlmsg_pid;
1525 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001528out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001529 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return err;
1531}
1532
Christoph Hellwig22e70052007-01-02 15:22:30 -08001533static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001534 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001536 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001537 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001538 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001539 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Joy Latten161a09e2006-11-27 13:11:54 -06001542 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001543 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001544 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001545 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001546 if (err) {
1547 if (err == -ESRCH) /* empty table */
1548 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001549 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001550 }
Herbert Xubf088672005-06-18 22:44:00 -07001551 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001552 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553 c.seq = nlh->nlmsg_seq;
1554 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001555 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001556 km_state_notify(NULL, &c);
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559}
1560
Thomas Graf7deb2262007-08-22 13:57:39 -07001561static inline size_t xfrm_aevent_msgsize(void)
1562{
1563 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1564 + nla_total_size(sizeof(struct xfrm_replay_state))
1565 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001566 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001567 + nla_total_size(4) /* XFRM_AE_RTHR */
1568 + nla_total_size(4); /* XFRM_AE_ETHR */
1569}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001570
1571static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1572{
1573 struct xfrm_aevent_id *id;
1574 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001575
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001576 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1577 if (nlh == NULL)
1578 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001579
Thomas Graf7b67c852007-08-22 13:53:52 -07001580 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001581 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001582 id->sa_id.spi = x->id.spi;
1583 id->sa_id.family = x->props.family;
1584 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001585 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1586 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001587 id->flags = c->data.aevent;
1588
Thomas Grafc0144be2007-08-22 13:55:43 -07001589 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1590 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001591
Thomas Grafc0144be2007-08-22 13:55:43 -07001592 if (id->flags & XFRM_AE_RTHR)
1593 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001594
Thomas Grafc0144be2007-08-22 13:55:43 -07001595 if (id->flags & XFRM_AE_ETHR)
1596 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1597 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001598
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001599 if (xfrm_mark_put(skb, &x->mark))
1600 goto nla_put_failure;
1601
Thomas Graf98250692007-08-22 12:47:26 -07001602 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001603
Thomas Grafc0144be2007-08-22 13:55:43 -07001604nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001605 nlmsg_cancel(skb, nlh);
1606 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001607}
1608
Christoph Hellwig22e70052007-01-02 15:22:30 -08001609static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001610 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001611{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001612 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001613 struct xfrm_state *x;
1614 struct sk_buff *r_skb;
1615 int err;
1616 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001617 u32 mark;
1618 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001619 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001620 struct xfrm_usersa_id *id = &p->sa_id;
1621
Thomas Graf7deb2262007-08-22 13:57:39 -07001622 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001623 if (r_skb == NULL)
1624 return -ENOMEM;
1625
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001626 mark = xfrm_mark_get(attrs, &m);
1627
1628 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001629 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001630 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001631 return -ESRCH;
1632 }
1633
1634 /*
1635 * XXX: is this lock really needed - none of the other
1636 * gets lock (the concern is things getting updated
1637 * while we are still reading) - jhs
1638 */
1639 spin_lock_bh(&x->lock);
1640 c.data.aevent = p->flags;
1641 c.seq = nlh->nlmsg_seq;
1642 c.pid = nlh->nlmsg_pid;
1643
1644 if (build_aevent(r_skb, x, &c) < 0)
1645 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001646 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001647 spin_unlock_bh(&x->lock);
1648 xfrm_state_put(x);
1649 return err;
1650}
1651
Christoph Hellwig22e70052007-01-02 15:22:30 -08001652static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001653 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001654{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001655 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001656 struct xfrm_state *x;
1657 struct km_event c;
1658 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001659 u32 mark = 0;
1660 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001661 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001662 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1663 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001664
1665 if (!lt && !rp)
1666 return err;
1667
1668 /* pedantic mode - thou shalt sayeth replaceth */
1669 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1670 return err;
1671
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001672 mark = xfrm_mark_get(attrs, &m);
1673
1674 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001675 if (x == NULL)
1676 return -ESRCH;
1677
1678 if (x->km.state != XFRM_STATE_VALID)
1679 goto out;
1680
1681 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001682 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001683 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001684
1685 c.event = nlh->nlmsg_type;
1686 c.seq = nlh->nlmsg_seq;
1687 c.pid = nlh->nlmsg_pid;
1688 c.data.aevent = XFRM_AE_CU;
1689 km_state_notify(x, &c);
1690 err = 0;
1691out:
1692 xfrm_state_put(x);
1693 return err;
1694}
1695
Christoph Hellwig22e70052007-01-02 15:22:30 -08001696static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001697 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001699 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001700 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001701 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001702 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001703 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001704
Thomas Graf35a7aa02007-08-22 14:00:40 -07001705 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001706 if (err)
1707 return err;
1708
Joy Latten161a09e2006-11-27 13:11:54 -06001709 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001710 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001711 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001712 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001713 if (err) {
1714 if (err == -ESRCH) /* empty table */
1715 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001716 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001717 }
1718
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001719 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001720 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721 c.seq = nlh->nlmsg_seq;
1722 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001723 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return 0;
1726}
1727
Christoph Hellwig22e70052007-01-02 15:22:30 -08001728static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001729 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001730{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001731 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001732 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001733 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001734 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001735 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001736 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001737 struct xfrm_mark m;
1738 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001739
Thomas Graf35a7aa02007-08-22 14:00:40 -07001740 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001741 if (err)
1742 return err;
1743
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001744 err = verify_policy_dir(p->dir);
1745 if (err)
1746 return err;
1747
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001748 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001749 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001751 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001752 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001753
Thomas Graf35a7aa02007-08-22 14:00:40 -07001754 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001755 if (err)
1756 return err;
1757
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001758 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001759 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001760 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001761
Paul Moore03e1ad72008-04-12 19:07:52 -07001762 err = security_xfrm_policy_alloc(&ctx, uctx);
1763 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001764 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001765 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001766 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001767 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001768 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001769 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001770 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001771 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001772
Timo Teräsea2dea92010-03-31 00:17:05 +00001773 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001774 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001775
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001776 err = 0;
1777 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001778 uid_t loginuid = NETLINK_CB(skb).loginuid;
1779 uid_t sessionid = NETLINK_CB(skb).sessionid;
1780 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001781 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001782 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001783
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001784 } else {
1785 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001786 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001787 }
1788 km_policy_expired(xp, p->dir, up->hard, current->pid);
1789
1790out:
1791 xfrm_pol_put(xp);
1792 return err;
1793}
1794
Christoph Hellwig22e70052007-01-02 15:22:30 -08001795static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001796 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001797{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001798 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001799 struct xfrm_state *x;
1800 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001801 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001802 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001803 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001804 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001805
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001806 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001807
David S. Miller3a765aa2007-02-26 14:52:21 -08001808 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001809 if (x == NULL)
1810 return err;
1811
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001812 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001813 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001814 if (x->km.state != XFRM_STATE_VALID)
1815 goto out;
1816 km_state_expired(x, ue->hard, current->pid);
1817
Joy Latten161a09e2006-11-27 13:11:54 -06001818 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001819 uid_t loginuid = NETLINK_CB(skb).loginuid;
1820 uid_t sessionid = NETLINK_CB(skb).sessionid;
1821 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001822 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001823 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001824 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001825 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001826out:
1827 spin_unlock_bh(&x->lock);
1828 xfrm_state_put(x);
1829 return err;
1830}
1831
Christoph Hellwig22e70052007-01-02 15:22:30 -08001832static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001833 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001834{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001835 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001836 struct xfrm_policy *xp;
1837 struct xfrm_user_tmpl *ut;
1838 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001839 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001840 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001841
Thomas Graf7b67c852007-08-22 13:53:52 -07001842 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001843 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001844 int err = -ENOMEM;
1845
1846 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001847 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001848
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001849 xfrm_mark_get(attrs, &mark);
1850
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001851 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001852 if (err)
1853 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001854
1855 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001856 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001857 if (!xp)
1858 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001859
1860 memcpy(&x->id, &ua->id, sizeof(ua->id));
1861 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1862 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001863 xp->mark.m = x->mark.m = mark.m;
1864 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001865 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001866 /* extract the templates and for each call km_key */
1867 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1868 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1869 memcpy(&x->id, &t->id, sizeof(x->id));
1870 x->props.mode = t->mode;
1871 x->props.reqid = t->reqid;
1872 x->props.family = ut->family;
1873 t->aalgos = ua->aalgos;
1874 t->ealgos = ua->ealgos;
1875 t->calgos = ua->calgos;
1876 err = km_query(x, t, xp);
1877
1878 }
1879
1880 kfree(x);
1881 kfree(xp);
1882
1883 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001884
1885bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001886 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001887free_state:
1888 kfree(x);
1889nomem:
1890 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001891}
1892
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001893#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001894static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001895 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001896 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001897{
Thomas Graf5424f322007-08-22 14:01:33 -07001898 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001899 struct xfrm_user_migrate *um;
1900 int i, num_migrate;
1901
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001902 if (k != NULL) {
1903 struct xfrm_user_kmaddress *uk;
1904
1905 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1906 memcpy(&k->local, &uk->local, sizeof(k->local));
1907 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1908 k->family = uk->family;
1909 k->reserved = uk->reserved;
1910 }
1911
Thomas Graf5424f322007-08-22 14:01:33 -07001912 um = nla_data(rt);
1913 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001914
1915 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1916 return -EINVAL;
1917
1918 for (i = 0; i < num_migrate; i++, um++, ma++) {
1919 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1920 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1921 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1922 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1923
1924 ma->proto = um->proto;
1925 ma->mode = um->mode;
1926 ma->reqid = um->reqid;
1927
1928 ma->old_family = um->old_family;
1929 ma->new_family = um->new_family;
1930 }
1931
1932 *num = i;
1933 return 0;
1934}
1935
1936static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001937 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001938{
Thomas Graf7b67c852007-08-22 13:53:52 -07001939 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001940 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001941 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001942 u8 type;
1943 int err;
1944 int n = 0;
1945
Thomas Graf35a7aa02007-08-22 14:00:40 -07001946 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001947 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001948
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001949 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1950
Thomas Graf5424f322007-08-22 14:01:33 -07001951 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001952 if (err)
1953 return err;
1954
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001955 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001956 if (err)
1957 return err;
1958
1959 if (!n)
1960 return 0;
1961
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001962 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001963
1964 return 0;
1965}
1966#else
1967static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001968 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001969{
1970 return -ENOPROTOOPT;
1971}
1972#endif
1973
1974#ifdef CONFIG_XFRM_MIGRATE
1975static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1976{
1977 struct xfrm_user_migrate um;
1978
1979 memset(&um, 0, sizeof(um));
1980 um.proto = m->proto;
1981 um.mode = m->mode;
1982 um.reqid = m->reqid;
1983 um.old_family = m->old_family;
1984 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1985 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1986 um.new_family = m->new_family;
1987 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1988 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1989
Thomas Grafc0144be2007-08-22 13:55:43 -07001990 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001991}
1992
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001993static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1994{
1995 struct xfrm_user_kmaddress uk;
1996
1997 memset(&uk, 0, sizeof(uk));
1998 uk.family = k->family;
1999 uk.reserved = k->reserved;
2000 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002001 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002002
2003 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2004}
2005
2006static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002007{
2008 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002009 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2010 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2011 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002012}
2013
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002014static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002015 int num_migrate, struct xfrm_kmaddress *k,
2016 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002017{
2018 struct xfrm_migrate *mp;
2019 struct xfrm_userpolicy_id *pol_id;
2020 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002021 int i;
2022
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002023 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2024 if (nlh == NULL)
2025 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002026
Thomas Graf7b67c852007-08-22 13:53:52 -07002027 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002028 /* copy data from selector, dir, and type to the pol_id */
2029 memset(pol_id, 0, sizeof(*pol_id));
2030 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2031 pol_id->dir = dir;
2032
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002033 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2034 goto nlmsg_failure;
2035
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002036 if (copy_to_user_policy_type(type, skb) < 0)
2037 goto nlmsg_failure;
2038
2039 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2040 if (copy_to_user_migrate(mp, skb) < 0)
2041 goto nlmsg_failure;
2042 }
2043
Thomas Graf98250692007-08-22 12:47:26 -07002044 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002045nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002046 nlmsg_cancel(skb, nlh);
2047 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002048}
2049
2050static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002051 struct xfrm_migrate *m, int num_migrate,
2052 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002053{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002054 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002055 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002056
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002057 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002058 if (skb == NULL)
2059 return -ENOMEM;
2060
2061 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002062 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002063 BUG();
2064
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002065 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066}
2067#else
2068static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002069 struct xfrm_migrate *m, int num_migrate,
2070 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002071{
2072 return -ENOPROTOOPT;
2073}
2074#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002075
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002076#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002077
2078static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002079 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002080 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2081 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2082 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2083 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2084 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2085 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002086 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002087 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002088 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002089 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002090 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002091 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002092 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002093 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2094 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002095 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002096 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002097 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2098 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099};
2100
Thomas Graf492b5582005-05-03 14:26:40 -07002101#undef XMSGSIZE
2102
Thomas Grafcf5cb792007-08-22 13:59:04 -07002103static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002104 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2105 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2106 [XFRMA_LASTUSED] = { .type = NLA_U64},
2107 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002108 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002109 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2110 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2111 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2112 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2113 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2114 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2115 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2116 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2117 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2118 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2119 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2120 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2121 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2122 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002123 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002124 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002125};
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002128 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002130 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002131} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2132 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2133 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2134 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002135 .dump = xfrm_dump_sa,
2136 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002137 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2138 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2139 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002140 .dump = xfrm_dump_policy,
2141 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002142 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002143 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002144 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002145 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2146 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002147 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002148 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2149 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002150 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2151 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002153 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002154 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155};
2156
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002157static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002159 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002160 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002162 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 type -= XFRM_MSG_BASE;
2169 link = &xfrm_dispatch[type];
2170
2171 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002172 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2173 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Thomas Graf492b5582005-05-03 14:26:40 -07002175 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2176 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2177 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002181 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
Thomas Graf35a7aa02007-08-22 14:00:40 -07002184 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002185 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002186 if (err < 0)
2187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002190 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Thomas Graf5424f322007-08-22 14:01:33 -07002192 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002195static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002197 mutex_lock(&xfrm_cfg_mutex);
2198 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2199 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Thomas Graf7deb2262007-08-22 13:57:39 -07002202static inline size_t xfrm_expire_msgsize(void)
2203{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002204 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2205 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002206}
2207
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002208static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
2210 struct xfrm_user_expire *ue;
2211 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002213 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2214 if (nlh == NULL)
2215 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Thomas Graf7b67c852007-08-22 13:53:52 -07002217 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002219 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002221 if (xfrm_mark_put(skb, &x->mark))
2222 goto nla_put_failure;
2223
Thomas Graf98250692007-08-22 12:47:26 -07002224 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002225
2226nla_put_failure:
2227 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002230static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002232 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 struct sk_buff *skb;
2234
Thomas Graf7deb2262007-08-22 13:57:39 -07002235 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (skb == NULL)
2237 return -ENOMEM;
2238
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002239 if (build_expire(skb, x, c) < 0) {
2240 kfree_skb(skb);
2241 return -EMSGSIZE;
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002244 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002247static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2248{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002249 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002250 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002251
Thomas Graf7deb2262007-08-22 13:57:39 -07002252 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002253 if (skb == NULL)
2254 return -ENOMEM;
2255
2256 if (build_aevent(skb, x, c) < 0)
2257 BUG();
2258
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002259 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002260}
2261
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002262static int xfrm_notify_sa_flush(struct km_event *c)
2263{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002264 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002265 struct xfrm_usersa_flush *p;
2266 struct nlmsghdr *nlh;
2267 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002268 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002269
Thomas Graf7deb2262007-08-22 13:57:39 -07002270 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002271 if (skb == NULL)
2272 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002273
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002274 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2275 if (nlh == NULL) {
2276 kfree_skb(skb);
2277 return -EMSGSIZE;
2278 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002279
Thomas Graf7b67c852007-08-22 13:53:52 -07002280 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002281 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282
Thomas Graf98250692007-08-22 12:47:26 -07002283 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002284
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002285 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286}
2287
Thomas Graf7deb2262007-08-22 13:57:39 -07002288static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002289{
Thomas Graf7deb2262007-08-22 13:57:39 -07002290 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002291 if (x->aead)
2292 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002293 if (x->aalg) {
2294 l += nla_total_size(sizeof(struct xfrm_algo) +
2295 (x->aalg->alg_key_len + 7) / 8);
2296 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2297 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002299 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002300 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002301 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002302 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002303 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002304 if (x->security)
2305 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2306 x->security->ctx_len);
2307 if (x->coaddr)
2308 l += nla_total_size(sizeof(*x->coaddr));
2309
Herbert Xud26f3982007-11-13 21:47:08 -08002310 /* Must count x->lastused as it may become non-zero behind our back. */
2311 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312
2313 return l;
2314}
2315
2316static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2317{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002318 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002319 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002320 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321 struct nlmsghdr *nlh;
2322 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002324 int headlen;
2325
2326 headlen = sizeof(*p);
2327 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002328 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002329 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002330 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002331 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002332 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002333
Thomas Graf7deb2262007-08-22 13:57:39 -07002334 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002335 if (skb == NULL)
2336 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002337
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002338 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2339 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002340 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341
Thomas Graf7b67c852007-08-22 13:53:52 -07002342 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002343 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002344 struct nlattr *attr;
2345
Thomas Graf7b67c852007-08-22 13:53:52 -07002346 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002347 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2348 id->spi = x->id.spi;
2349 id->family = x->props.family;
2350 id->proto = x->id.proto;
2351
Thomas Grafc0144be2007-08-22 13:55:43 -07002352 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2353 if (attr == NULL)
2354 goto nla_put_failure;
2355
2356 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002357 }
2358
Herbert Xu68325d32007-10-09 13:30:57 -07002359 if (copy_to_user_state_extra(x, p, skb))
2360 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002361
Thomas Graf98250692007-08-22 12:47:26 -07002362 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002363
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002364 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365
Thomas Grafc0144be2007-08-22 13:55:43 -07002366nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002367 /* Somebody screwed up with xfrm_sa_len! */
2368 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002369 kfree_skb(skb);
2370 return -1;
2371}
2372
2373static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2374{
2375
2376 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002377 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002379 case XFRM_MSG_NEWAE:
2380 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002381 case XFRM_MSG_DELSA:
2382 case XFRM_MSG_UPDSA:
2383 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002385 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386 return xfrm_notify_sa_flush(c);
2387 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002388 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2389 c->event);
2390 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391 }
2392
2393 return 0;
2394
2395}
2396
Thomas Graf7deb2262007-08-22 13:57:39 -07002397static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2398 struct xfrm_policy *xp)
2399{
2400 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2401 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002402 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002403 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2404 + userpolicy_type_attrsize();
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2408 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2409 int dir)
2410{
2411 struct xfrm_user_acquire *ua;
2412 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 __u32 seq = xfrm_get_acqseq();
2414
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002415 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2416 if (nlh == NULL)
2417 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Thomas Graf7b67c852007-08-22 13:53:52 -07002419 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 memcpy(&ua->id, &x->id, sizeof(ua->id));
2421 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2422 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2423 copy_to_user_policy(xp, &ua->policy, dir);
2424 ua->aalgos = xt->aalgos;
2425 ua->ealgos = xt->ealgos;
2426 ua->calgos = xt->calgos;
2427 ua->seq = x->km.seq = seq;
2428
2429 if (copy_to_user_tmpl(xp, skb) < 0)
2430 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002431 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002432 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002433 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002434 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002435 if (xfrm_mark_put(skb, &xp->mark))
2436 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Thomas Graf98250692007-08-22 12:47:26 -07002438 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002440nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002442 nlmsg_cancel(skb, nlh);
2443 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2447 struct xfrm_policy *xp, int dir)
2448{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002449 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Thomas Graf7deb2262007-08-22 13:57:39 -07002452 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (skb == NULL)
2454 return -ENOMEM;
2455
2456 if (build_acquire(skb, x, xt, xp, dir) < 0)
2457 BUG();
2458
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002459 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
2462/* User gives us xfrm_user_policy_info followed by an array of 0
2463 * or more templates.
2464 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002465static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 u8 *data, int len, int *dir)
2467{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002468 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2470 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2471 struct xfrm_policy *xp;
2472 int nr;
2473
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002474 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 case AF_INET:
2476 if (opt != IP_XFRM_POLICY) {
2477 *dir = -EOPNOTSUPP;
2478 return NULL;
2479 }
2480 break;
2481#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2482 case AF_INET6:
2483 if (opt != IPV6_XFRM_POLICY) {
2484 *dir = -EOPNOTSUPP;
2485 return NULL;
2486 }
2487 break;
2488#endif
2489 default:
2490 *dir = -EINVAL;
2491 return NULL;
2492 }
2493
2494 *dir = -EINVAL;
2495
2496 if (len < sizeof(*p) ||
2497 verify_newpolicy_info(p))
2498 return NULL;
2499
2500 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002501 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return NULL;
2503
Herbert Xua4f1bac2005-07-26 15:43:17 -07002504 if (p->dir > XFRM_POLICY_OUT)
2505 return NULL;
2506
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002507 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (xp == NULL) {
2509 *dir = -ENOBUFS;
2510 return NULL;
2511 }
2512
2513 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002514 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 copy_templates(xp, ut, nr);
2516
2517 *dir = p->dir;
2518
2519 return xp;
2520}
2521
Thomas Graf7deb2262007-08-22 13:57:39 -07002522static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2523{
2524 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2525 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2526 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002527 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002528 + userpolicy_type_attrsize();
2529}
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002532 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 struct xfrm_user_polexpire *upe;
2535 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002536 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002538 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2539 if (nlh == NULL)
2540 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Thomas Graf7b67c852007-08-22 13:53:52 -07002542 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 copy_to_user_policy(xp, &upe->pol, dir);
2544 if (copy_to_user_tmpl(xp, skb) < 0)
2545 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002546 if (copy_to_user_sec_ctx(xp, skb))
2547 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002548 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002549 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002550 if (xfrm_mark_put(skb, &xp->mark))
2551 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 upe->hard = !!hard;
2553
Thomas Graf98250692007-08-22 12:47:26 -07002554 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002556nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002558 nlmsg_cancel(skb, nlh);
2559 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002564 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Thomas Graf7deb2262007-08-22 13:57:39 -07002567 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (skb == NULL)
2569 return -ENOMEM;
2570
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002571 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 BUG();
2573
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002574 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2578{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002579 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002580 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002581 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002582 struct nlmsghdr *nlh;
2583 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002584 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002585 int headlen;
2586
2587 headlen = sizeof(*p);
2588 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002589 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002590 headlen = sizeof(*id);
2591 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002592 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002593 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002594 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595
Thomas Graf7deb2262007-08-22 13:57:39 -07002596 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597 if (skb == NULL)
2598 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002600 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2601 if (nlh == NULL)
2602 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002603
Thomas Graf7b67c852007-08-22 13:53:52 -07002604 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002605 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002606 struct nlattr *attr;
2607
Thomas Graf7b67c852007-08-22 13:53:52 -07002608 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002609 memset(id, 0, sizeof(*id));
2610 id->dir = dir;
2611 if (c->data.byid)
2612 id->index = xp->index;
2613 else
2614 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2615
Thomas Grafc0144be2007-08-22 13:55:43 -07002616 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2617 if (attr == NULL)
2618 goto nlmsg_failure;
2619
2620 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002621 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002622
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002623 copy_to_user_policy(xp, p, dir);
2624 if (copy_to_user_tmpl(xp, skb) < 0)
2625 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002626 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002627 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002628
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002629 if (xfrm_mark_put(skb, &xp->mark))
2630 goto nla_put_failure;
2631
Thomas Graf98250692007-08-22 12:47:26 -07002632 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002633
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002634 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002635
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002636nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637nlmsg_failure:
2638 kfree_skb(skb);
2639 return -1;
2640}
2641
2642static int xfrm_notify_policy_flush(struct km_event *c)
2643{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002644 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645 struct nlmsghdr *nlh;
2646 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002647
Thomas Graf7deb2262007-08-22 13:57:39 -07002648 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002649 if (skb == NULL)
2650 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002651
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002652 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2653 if (nlh == NULL)
2654 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002655 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2656 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002657
Thomas Graf98250692007-08-22 12:47:26 -07002658 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002659
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002660 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002661
2662nlmsg_failure:
2663 kfree_skb(skb);
2664 return -1;
2665}
2666
2667static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2668{
2669
2670 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002671 case XFRM_MSG_NEWPOLICY:
2672 case XFRM_MSG_UPDPOLICY:
2673 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002675 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002677 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678 return xfrm_exp_policy_notify(xp, dir, c);
2679 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002680 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2681 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002682 }
2683
2684 return 0;
2685
2686}
2687
Thomas Graf7deb2262007-08-22 13:57:39 -07002688static inline size_t xfrm_report_msgsize(void)
2689{
2690 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2691}
2692
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002693static int build_report(struct sk_buff *skb, u8 proto,
2694 struct xfrm_selector *sel, xfrm_address_t *addr)
2695{
2696 struct xfrm_user_report *ur;
2697 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002698
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002699 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2700 if (nlh == NULL)
2701 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002702
Thomas Graf7b67c852007-08-22 13:53:52 -07002703 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002704 ur->proto = proto;
2705 memcpy(&ur->sel, sel, sizeof(ur->sel));
2706
2707 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002708 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002709
Thomas Graf98250692007-08-22 12:47:26 -07002710 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002711
Thomas Grafc0144be2007-08-22 13:55:43 -07002712nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002713 nlmsg_cancel(skb, nlh);
2714 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002715}
2716
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002717static int xfrm_send_report(struct net *net, u8 proto,
2718 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002719{
2720 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002721
Thomas Graf7deb2262007-08-22 13:57:39 -07002722 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002723 if (skb == NULL)
2724 return -ENOMEM;
2725
2726 if (build_report(skb, proto, sel, addr) < 0)
2727 BUG();
2728
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002729 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002730}
2731
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002732static inline size_t xfrm_mapping_msgsize(void)
2733{
2734 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2735}
2736
2737static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2738 xfrm_address_t *new_saddr, __be16 new_sport)
2739{
2740 struct xfrm_user_mapping *um;
2741 struct nlmsghdr *nlh;
2742
2743 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2744 if (nlh == NULL)
2745 return -EMSGSIZE;
2746
2747 um = nlmsg_data(nlh);
2748
2749 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2750 um->id.spi = x->id.spi;
2751 um->id.family = x->props.family;
2752 um->id.proto = x->id.proto;
2753 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2754 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2755 um->new_sport = new_sport;
2756 um->old_sport = x->encap->encap_sport;
2757 um->reqid = x->props.reqid;
2758
2759 return nlmsg_end(skb, nlh);
2760}
2761
2762static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2763 __be16 sport)
2764{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002765 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002766 struct sk_buff *skb;
2767
2768 if (x->id.proto != IPPROTO_ESP)
2769 return -EINVAL;
2770
2771 if (!x->encap)
2772 return -EINVAL;
2773
2774 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2775 if (skb == NULL)
2776 return -ENOMEM;
2777
2778 if (build_mapping(skb, x, ipaddr, sport) < 0)
2779 BUG();
2780
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002781 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002782}
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784static struct xfrm_mgr netlink_mgr = {
2785 .id = "netlink",
2786 .notify = xfrm_send_state_notify,
2787 .acquire = xfrm_send_acquire,
2788 .compile_policy = xfrm_compile_policy,
2789 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002790 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002791 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002792 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793};
2794
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002795static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Patrick McHardybe336902006-03-20 22:40:54 -08002797 struct sock *nlsk;
2798
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002799 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002800 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002801 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002803 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002804 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return 0;
2806}
2807
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002808static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002809{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002810 struct net *net;
2811 list_for_each_entry(net, net_exit_list, exit_list)
2812 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2813 synchronize_net();
2814 list_for_each_entry(net, net_exit_list, exit_list)
2815 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002816}
2817
2818static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002819 .init = xfrm_user_net_init,
2820 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002821};
2822
2823static int __init xfrm_user_init(void)
2824{
2825 int rv;
2826
2827 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2828
2829 rv = register_pernet_subsys(&xfrm_user_net_ops);
2830 if (rv < 0)
2831 return rv;
2832 rv = xfrm_register_km(&netlink_mgr);
2833 if (rv < 0)
2834 unregister_pernet_subsys(&xfrm_user_net_ops);
2835 return rv;
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838static void __exit xfrm_user_exit(void)
2839{
2840 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002841 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
2844module_init(xfrm_user_init);
2845module_exit(xfrm_user_exit);
2846MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002847MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002848