x86, apic: Fix spurious error interrupts triggering on all non-boot APs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_user.c
blobd5a712976004cdc4c8fbdfcd53baa501abd59b39
1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
13 #include <linux/crypto.h>
14 #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>
22 #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>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
32 #endif
34 static inline int aead_len(struct xfrm_algo_aead *alg)
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
44 if (!rt)
45 return 0;
47 algp = nla_data(rt);
48 if (nla_len(rt) < xfrm_alg_len(algp))
49 return -EINVAL;
51 switch (type) {
52 case XFRMA_ALG_AUTH:
53 case XFRMA_ALG_CRYPT:
54 case XFRMA_ALG_COMP:
55 break;
57 default:
58 return -EINVAL;
61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
62 return 0;
65 static int verify_auth_trunc(struct nlattr **attrs)
67 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
68 struct xfrm_algo_auth *algp;
70 if (!rt)
71 return 0;
73 algp = nla_data(rt);
74 if (nla_len(rt) < xfrm_alg_auth_len(algp))
75 return -EINVAL;
77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
78 return 0;
81 static int verify_aead(struct nlattr **attrs)
83 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
84 struct xfrm_algo_aead *algp;
86 if (!rt)
87 return 0;
89 algp = nla_data(rt);
90 if (nla_len(rt) < aead_len(algp))
91 return -EINVAL;
93 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
94 return 0;
97 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
98 xfrm_address_t **addrp)
100 struct nlattr *rt = attrs[type];
102 if (rt && addrp)
103 *addrp = nla_data(rt);
106 static inline int verify_sec_ctx_len(struct nlattr **attrs)
108 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
109 struct xfrm_user_sec_ctx *uctx;
111 if (!rt)
112 return 0;
114 uctx = nla_data(rt);
115 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
116 return -EINVAL;
118 return 0;
122 static int verify_newsa_info(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
125 int err;
127 err = -EINVAL;
128 switch (p->family) {
129 case AF_INET:
130 break;
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
140 default:
141 goto out;
144 err = -EINVAL;
145 switch (p->id.proto) {
146 case IPPROTO_AH:
147 if ((!attrs[XFRMA_ALG_AUTH] &&
148 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
149 attrs[XFRMA_ALG_AEAD] ||
150 attrs[XFRMA_ALG_CRYPT] ||
151 attrs[XFRMA_ALG_COMP])
152 goto out;
153 break;
155 case IPPROTO_ESP:
156 if (attrs[XFRMA_ALG_COMP])
157 goto out;
158 if (!attrs[XFRMA_ALG_AUTH] &&
159 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
160 !attrs[XFRMA_ALG_CRYPT] &&
161 !attrs[XFRMA_ALG_AEAD])
162 goto out;
163 if ((attrs[XFRMA_ALG_AUTH] ||
164 attrs[XFRMA_ALG_AUTH_TRUNC] ||
165 attrs[XFRMA_ALG_CRYPT]) &&
166 attrs[XFRMA_ALG_AEAD])
167 goto out;
168 break;
170 case IPPROTO_COMP:
171 if (!attrs[XFRMA_ALG_COMP] ||
172 attrs[XFRMA_ALG_AEAD] ||
173 attrs[XFRMA_ALG_AUTH] ||
174 attrs[XFRMA_ALG_AUTH_TRUNC] ||
175 attrs[XFRMA_ALG_CRYPT])
176 goto out;
177 break;
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
182 if (attrs[XFRMA_ALG_COMP] ||
183 attrs[XFRMA_ALG_AUTH] ||
184 attrs[XFRMA_ALG_AUTH_TRUNC] ||
185 attrs[XFRMA_ALG_AEAD] ||
186 attrs[XFRMA_ALG_CRYPT] ||
187 attrs[XFRMA_ENCAP] ||
188 attrs[XFRMA_SEC_CTX] ||
189 !attrs[XFRMA_COADDR])
190 goto out;
191 break;
192 #endif
194 default:
195 goto out;
198 if ((err = verify_aead(attrs)))
199 goto out;
200 if ((err = verify_auth_trunc(attrs)))
201 goto out;
202 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
203 goto out;
204 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
205 goto out;
206 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
207 goto out;
208 if ((err = verify_sec_ctx_len(attrs)))
209 goto out;
211 err = -EINVAL;
212 switch (p->mode) {
213 case XFRM_MODE_TRANSPORT:
214 case XFRM_MODE_TUNNEL:
215 case XFRM_MODE_ROUTEOPTIMIZATION:
216 case XFRM_MODE_BEET:
217 break;
219 default:
220 goto out;
223 err = 0;
225 out:
226 return err;
229 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
230 struct xfrm_algo_desc *(*get_byname)(char *, int),
231 struct nlattr *rta)
233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
236 if (!rta)
237 return 0;
239 ualg = nla_data(rta);
241 algo = get_byname(ualg->alg_name, 1);
242 if (!algo)
243 return -ENOSYS;
244 *props = algo->desc.sadb_alg_id;
246 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
247 if (!p)
248 return -ENOMEM;
250 strcpy(p->alg_name, algo->name);
251 *algpp = p;
252 return 0;
255 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
256 struct nlattr *rta)
258 struct xfrm_algo *ualg;
259 struct xfrm_algo_auth *p;
260 struct xfrm_algo_desc *algo;
262 if (!rta)
263 return 0;
265 ualg = nla_data(rta);
267 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
268 if (!algo)
269 return -ENOSYS;
270 *props = algo->desc.sadb_alg_id;
272 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
273 if (!p)
274 return -ENOMEM;
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);
281 *algpp = p;
282 return 0;
285 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
286 struct nlattr *rta)
288 struct xfrm_algo_auth *p, *ualg;
289 struct xfrm_algo_desc *algo;
291 if (!rta)
292 return 0;
294 ualg = nla_data(rta);
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;
303 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
304 if (!p)
305 return -ENOMEM;
307 strcpy(p->alg_name, algo->name);
308 if (!p->alg_trunc_len)
309 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
311 *algpp = p;
312 return 0;
315 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
316 struct nlattr *rta)
318 struct xfrm_algo_aead *p, *ualg;
319 struct xfrm_algo_desc *algo;
321 if (!rta)
322 return 0;
324 ualg = nla_data(rta);
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;
331 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
332 if (!p)
333 return -ENOMEM;
335 strcpy(p->alg_name, algo->name);
336 *algpp = p;
337 return 0;
340 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
342 int len = 0;
344 if (xfrm_ctx) {
345 len += sizeof(struct xfrm_user_sec_ctx);
346 len += xfrm_ctx->ctx_len;
348 return len;
351 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
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;
360 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
361 x->props.flags = p->flags;
363 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
364 x->sel.family = p->family;
368 * someday when pfkey also has support, we could have the code
369 * somehow made shareable and move it to xfrm_state.c - JHS
372 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
374 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];
379 if (rp) {
380 struct xfrm_replay_state *replay;
381 replay = nla_data(rp);
382 memcpy(&x->replay, replay, sizeof(*replay));
383 memcpy(&x->preplay, replay, sizeof(*replay));
386 if (lt) {
387 struct xfrm_lifetime_cur *ltime;
388 ltime = nla_data(lt);
389 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;
395 if (et)
396 x->replay_maxage = nla_get_u32(et);
398 if (rt)
399 x->replay_maxdiff = nla_get_u32(rt);
402 static struct xfrm_state *xfrm_state_construct(struct net *net,
403 struct xfrm_usersa_info *p,
404 struct nlattr **attrs,
405 int *errp)
407 struct xfrm_state *x = xfrm_state_alloc(net);
408 int err = -ENOMEM;
410 if (!x)
411 goto error_no_put;
413 copy_from_user_state(x, p);
415 if ((err = attach_aead(&x->aead, &x->props.ealgo,
416 attrs[XFRMA_ALG_AEAD])))
417 goto error;
418 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
419 attrs[XFRMA_ALG_AUTH_TRUNC])))
420 goto error;
421 if (!x->props.aalgo) {
422 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
423 attrs[XFRMA_ALG_AUTH])))
424 goto error;
426 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
427 xfrm_ealg_get_byname,
428 attrs[XFRMA_ALG_CRYPT])))
429 goto error;
430 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
431 xfrm_calg_get_byname,
432 attrs[XFRMA_ALG_COMP])))
433 goto error;
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;
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;
449 err = xfrm_init_state(x);
450 if (err)
451 goto error;
453 if (attrs[XFRMA_SEC_CTX] &&
454 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
455 goto error;
457 x->km.seq = p->seq;
458 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
459 /* sysctl_xfrm_aevent_etime is in 100ms units */
460 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
461 x->preplay.bitmap = 0;
462 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
463 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
465 /* override default values from above */
467 xfrm_update_ae_params(x, attrs);
469 return x;
471 error:
472 x->km.state = XFRM_STATE_DEAD;
473 xfrm_state_put(x);
474 error_no_put:
475 *errp = err;
476 return NULL;
479 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
480 struct nlattr **attrs)
482 struct net *net = sock_net(skb->sk);
483 struct xfrm_usersa_info *p = nlmsg_data(nlh);
484 struct xfrm_state *x;
485 int err;
486 struct km_event c;
487 uid_t loginuid = NETLINK_CB(skb).loginuid;
488 u32 sessionid = NETLINK_CB(skb).sessionid;
489 u32 sid = NETLINK_CB(skb).sid;
491 err = verify_newsa_info(p, attrs);
492 if (err)
493 return err;
495 x = xfrm_state_construct(net, p, attrs, &err);
496 if (!x)
497 return err;
499 xfrm_state_hold(x);
500 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
501 err = xfrm_state_add(x);
502 else
503 err = xfrm_state_update(x);
505 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
507 if (err < 0) {
508 x->km.state = XFRM_STATE_DEAD;
509 __xfrm_state_put(x);
510 goto out;
513 c.seq = nlh->nlmsg_seq;
514 c.pid = nlh->nlmsg_pid;
515 c.event = nlh->nlmsg_type;
517 km_state_notify(x, &c);
518 out:
519 xfrm_state_put(x);
520 return err;
523 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
524 struct xfrm_usersa_id *p,
525 struct nlattr **attrs,
526 int *errp)
528 struct xfrm_state *x = NULL;
529 int err;
531 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
532 err = -ESRCH;
533 x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family);
534 } else {
535 xfrm_address_t *saddr = NULL;
537 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
538 if (!saddr) {
539 err = -EINVAL;
540 goto out;
543 err = -ESRCH;
544 x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr,
545 p->proto, p->family);
548 out:
549 if (!x && errp)
550 *errp = err;
551 return x;
554 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
555 struct nlattr **attrs)
557 struct net *net = sock_net(skb->sk);
558 struct xfrm_state *x;
559 int err = -ESRCH;
560 struct km_event c;
561 struct xfrm_usersa_id *p = nlmsg_data(nlh);
562 uid_t loginuid = NETLINK_CB(skb).loginuid;
563 u32 sessionid = NETLINK_CB(skb).sessionid;
564 u32 sid = NETLINK_CB(skb).sid;
566 x = xfrm_user_state_lookup(net, p, attrs, &err);
567 if (x == NULL)
568 return err;
570 if ((err = security_xfrm_state_delete(x)) != 0)
571 goto out;
573 if (xfrm_state_kern(x)) {
574 err = -EPERM;
575 goto out;
578 err = xfrm_state_delete(x);
580 if (err < 0)
581 goto out;
583 c.seq = nlh->nlmsg_seq;
584 c.pid = nlh->nlmsg_pid;
585 c.event = nlh->nlmsg_type;
586 km_state_notify(x, &c);
588 out:
589 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
590 xfrm_state_put(x);
591 return err;
594 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
596 memcpy(&p->id, &x->id, sizeof(p->id));
597 memcpy(&p->sel, &x->sel, sizeof(p->sel));
598 memcpy(&p->lft, &x->lft, sizeof(p->lft));
599 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
600 memcpy(&p->stats, &x->stats, sizeof(p->stats));
601 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
602 p->mode = x->props.mode;
603 p->replay_window = x->props.replay_window;
604 p->reqid = x->props.reqid;
605 p->family = x->props.family;
606 p->flags = x->props.flags;
607 p->seq = x->km.seq;
610 struct xfrm_dump_info {
611 struct sk_buff *in_skb;
612 struct sk_buff *out_skb;
613 u32 nlmsg_seq;
614 u16 nlmsg_flags;
617 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
619 struct xfrm_user_sec_ctx *uctx;
620 struct nlattr *attr;
621 int ctx_size = sizeof(*uctx) + s->ctx_len;
623 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
624 if (attr == NULL)
625 return -EMSGSIZE;
627 uctx = nla_data(attr);
628 uctx->exttype = XFRMA_SEC_CTX;
629 uctx->len = ctx_size;
630 uctx->ctx_doi = s->ctx_doi;
631 uctx->ctx_alg = s->ctx_alg;
632 uctx->ctx_len = s->ctx_len;
633 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
635 return 0;
638 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
640 struct xfrm_algo *algo;
641 struct nlattr *nla;
643 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
644 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
645 if (!nla)
646 return -EMSGSIZE;
648 algo = nla_data(nla);
649 strcpy(algo->alg_name, auth->alg_name);
650 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
651 algo->alg_key_len = auth->alg_key_len;
653 return 0;
656 /* Don't change this without updating xfrm_sa_len! */
657 static int copy_to_user_state_extra(struct xfrm_state *x,
658 struct xfrm_usersa_info *p,
659 struct sk_buff *skb)
661 copy_to_user_state(x, p);
663 if (x->coaddr)
664 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
666 if (x->lastused)
667 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
669 if (x->aead)
670 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
671 if (x->aalg) {
672 if (copy_to_user_auth(x->aalg, skb))
673 goto nla_put_failure;
675 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
676 xfrm_alg_auth_len(x->aalg), x->aalg);
678 if (x->ealg)
679 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
680 if (x->calg)
681 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
683 if (x->encap)
684 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
686 if (x->security && copy_sec_ctx(x->security, skb) < 0)
687 goto nla_put_failure;
689 return 0;
691 nla_put_failure:
692 return -EMSGSIZE;
695 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
697 struct xfrm_dump_info *sp = ptr;
698 struct sk_buff *in_skb = sp->in_skb;
699 struct sk_buff *skb = sp->out_skb;
700 struct xfrm_usersa_info *p;
701 struct nlmsghdr *nlh;
702 int err;
704 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
705 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
706 if (nlh == NULL)
707 return -EMSGSIZE;
709 p = nlmsg_data(nlh);
711 err = copy_to_user_state_extra(x, p, skb);
712 if (err)
713 goto nla_put_failure;
715 nlmsg_end(skb, nlh);
716 return 0;
718 nla_put_failure:
719 nlmsg_cancel(skb, nlh);
720 return err;
723 static int xfrm_dump_sa_done(struct netlink_callback *cb)
725 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
726 xfrm_state_walk_done(walk);
727 return 0;
730 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
732 struct net *net = sock_net(skb->sk);
733 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
734 struct xfrm_dump_info info;
736 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
737 sizeof(cb->args) - sizeof(cb->args[0]));
739 info.in_skb = cb->skb;
740 info.out_skb = skb;
741 info.nlmsg_seq = cb->nlh->nlmsg_seq;
742 info.nlmsg_flags = NLM_F_MULTI;
744 if (!cb->args[0]) {
745 cb->args[0] = 1;
746 xfrm_state_walk_init(walk, 0);
749 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
751 return skb->len;
754 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
755 struct xfrm_state *x, u32 seq)
757 struct xfrm_dump_info info;
758 struct sk_buff *skb;
760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
761 if (!skb)
762 return ERR_PTR(-ENOMEM);
764 info.in_skb = in_skb;
765 info.out_skb = skb;
766 info.nlmsg_seq = seq;
767 info.nlmsg_flags = 0;
769 if (dump_one_state(x, 0, &info)) {
770 kfree_skb(skb);
771 return NULL;
774 return skb;
777 static inline size_t xfrm_spdinfo_msgsize(void)
779 return NLMSG_ALIGN(4)
780 + nla_total_size(sizeof(struct xfrmu_spdinfo))
781 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
784 static int build_spdinfo(struct sk_buff *skb, struct net *net,
785 u32 pid, u32 seq, u32 flags)
787 struct xfrmk_spdinfo si;
788 struct xfrmu_spdinfo spc;
789 struct xfrmu_spdhinfo sph;
790 struct nlmsghdr *nlh;
791 u32 *f;
793 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
794 if (nlh == NULL) /* shouldnt really happen ... */
795 return -EMSGSIZE;
797 f = nlmsg_data(nlh);
798 *f = flags;
799 xfrm_spd_getinfo(net, &si);
800 spc.incnt = si.incnt;
801 spc.outcnt = si.outcnt;
802 spc.fwdcnt = si.fwdcnt;
803 spc.inscnt = si.inscnt;
804 spc.outscnt = si.outscnt;
805 spc.fwdscnt = si.fwdscnt;
806 sph.spdhcnt = si.spdhcnt;
807 sph.spdhmcnt = si.spdhmcnt;
809 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
810 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
812 return nlmsg_end(skb, nlh);
814 nla_put_failure:
815 nlmsg_cancel(skb, nlh);
816 return -EMSGSIZE;
819 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
820 struct nlattr **attrs)
822 struct net *net = sock_net(skb->sk);
823 struct sk_buff *r_skb;
824 u32 *flags = nlmsg_data(nlh);
825 u32 spid = NETLINK_CB(skb).pid;
826 u32 seq = nlh->nlmsg_seq;
828 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
829 if (r_skb == NULL)
830 return -ENOMEM;
832 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
833 BUG();
835 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
838 static inline size_t xfrm_sadinfo_msgsize(void)
840 return NLMSG_ALIGN(4)
841 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
842 + nla_total_size(4); /* XFRMA_SAD_CNT */
845 static int build_sadinfo(struct sk_buff *skb, struct net *net,
846 u32 pid, u32 seq, u32 flags)
848 struct xfrmk_sadinfo si;
849 struct xfrmu_sadhinfo sh;
850 struct nlmsghdr *nlh;
851 u32 *f;
853 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
854 if (nlh == NULL) /* shouldnt really happen ... */
855 return -EMSGSIZE;
857 f = nlmsg_data(nlh);
858 *f = flags;
859 xfrm_sad_getinfo(net, &si);
861 sh.sadhmcnt = si.sadhmcnt;
862 sh.sadhcnt = si.sadhcnt;
864 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
865 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
867 return nlmsg_end(skb, nlh);
869 nla_put_failure:
870 nlmsg_cancel(skb, nlh);
871 return -EMSGSIZE;
874 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
875 struct nlattr **attrs)
877 struct net *net = sock_net(skb->sk);
878 struct sk_buff *r_skb;
879 u32 *flags = nlmsg_data(nlh);
880 u32 spid = NETLINK_CB(skb).pid;
881 u32 seq = nlh->nlmsg_seq;
883 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
884 if (r_skb == NULL)
885 return -ENOMEM;
887 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
888 BUG();
890 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
893 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
894 struct nlattr **attrs)
896 struct net *net = sock_net(skb->sk);
897 struct xfrm_usersa_id *p = nlmsg_data(nlh);
898 struct xfrm_state *x;
899 struct sk_buff *resp_skb;
900 int err = -ESRCH;
902 x = xfrm_user_state_lookup(net, p, attrs, &err);
903 if (x == NULL)
904 goto out_noput;
906 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
907 if (IS_ERR(resp_skb)) {
908 err = PTR_ERR(resp_skb);
909 } else {
910 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
912 xfrm_state_put(x);
913 out_noput:
914 return err;
917 static int verify_userspi_info(struct xfrm_userspi_info *p)
919 switch (p->info.id.proto) {
920 case IPPROTO_AH:
921 case IPPROTO_ESP:
922 break;
924 case IPPROTO_COMP:
925 /* IPCOMP spi is 16-bits. */
926 if (p->max >= 0x10000)
927 return -EINVAL;
928 break;
930 default:
931 return -EINVAL;
934 if (p->min > p->max)
935 return -EINVAL;
937 return 0;
940 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
941 struct nlattr **attrs)
943 struct net *net = sock_net(skb->sk);
944 struct xfrm_state *x;
945 struct xfrm_userspi_info *p;
946 struct sk_buff *resp_skb;
947 xfrm_address_t *daddr;
948 int family;
949 int err;
951 p = nlmsg_data(nlh);
952 err = verify_userspi_info(p);
953 if (err)
954 goto out_noput;
956 family = p->info.family;
957 daddr = &p->info.id.daddr;
959 x = NULL;
960 if (p->info.seq) {
961 x = xfrm_find_acq_byseq(net, p->info.seq);
962 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
963 xfrm_state_put(x);
964 x = NULL;
968 if (!x)
969 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
970 p->info.id.proto, daddr,
971 &p->info.saddr, 1,
972 family);
973 err = -ENOENT;
974 if (x == NULL)
975 goto out_noput;
977 err = xfrm_alloc_spi(x, p->min, p->max);
978 if (err)
979 goto out;
981 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
982 if (IS_ERR(resp_skb)) {
983 err = PTR_ERR(resp_skb);
984 goto out;
987 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
989 out:
990 xfrm_state_put(x);
991 out_noput:
992 return err;
995 static int verify_policy_dir(u8 dir)
997 switch (dir) {
998 case XFRM_POLICY_IN:
999 case XFRM_POLICY_OUT:
1000 case XFRM_POLICY_FWD:
1001 break;
1003 default:
1004 return -EINVAL;
1007 return 0;
1010 static int verify_policy_type(u8 type)
1012 switch (type) {
1013 case XFRM_POLICY_TYPE_MAIN:
1014 #ifdef CONFIG_XFRM_SUB_POLICY
1015 case XFRM_POLICY_TYPE_SUB:
1016 #endif
1017 break;
1019 default:
1020 return -EINVAL;
1023 return 0;
1026 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1028 switch (p->share) {
1029 case XFRM_SHARE_ANY:
1030 case XFRM_SHARE_SESSION:
1031 case XFRM_SHARE_USER:
1032 case XFRM_SHARE_UNIQUE:
1033 break;
1035 default:
1036 return -EINVAL;
1039 switch (p->action) {
1040 case XFRM_POLICY_ALLOW:
1041 case XFRM_POLICY_BLOCK:
1042 break;
1044 default:
1045 return -EINVAL;
1048 switch (p->sel.family) {
1049 case AF_INET:
1050 break;
1052 case AF_INET6:
1053 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1054 break;
1055 #else
1056 return -EAFNOSUPPORT;
1057 #endif
1059 default:
1060 return -EINVAL;
1063 return verify_policy_dir(p->dir);
1066 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1068 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1069 struct xfrm_user_sec_ctx *uctx;
1071 if (!rt)
1072 return 0;
1074 uctx = nla_data(rt);
1075 return security_xfrm_policy_alloc(&pol->security, uctx);
1078 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1079 int nr)
1081 int i;
1083 xp->xfrm_nr = nr;
1084 for (i = 0; i < nr; i++, ut++) {
1085 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1087 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1088 memcpy(&t->saddr, &ut->saddr,
1089 sizeof(xfrm_address_t));
1090 t->reqid = ut->reqid;
1091 t->mode = ut->mode;
1092 t->share = ut->share;
1093 t->optional = ut->optional;
1094 t->aalgos = ut->aalgos;
1095 t->ealgos = ut->ealgos;
1096 t->calgos = ut->calgos;
1097 /* If all masks are ~0, then we allow all algorithms. */
1098 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1099 t->encap_family = ut->family;
1103 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1105 int i;
1107 if (nr > XFRM_MAX_DEPTH)
1108 return -EINVAL;
1110 for (i = 0; i < nr; i++) {
1111 /* We never validated the ut->family value, so many
1112 * applications simply leave it at zero. The check was
1113 * never made and ut->family was ignored because all
1114 * templates could be assumed to have the same family as
1115 * the policy itself. Now that we will have ipv4-in-ipv6
1116 * and ipv6-in-ipv4 tunnels, this is no longer true.
1118 if (!ut[i].family)
1119 ut[i].family = family;
1121 switch (ut[i].family) {
1122 case AF_INET:
1123 break;
1124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1125 case AF_INET6:
1126 break;
1127 #endif
1128 default:
1129 return -EINVAL;
1133 return 0;
1136 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1138 struct nlattr *rt = attrs[XFRMA_TMPL];
1140 if (!rt) {
1141 pol->xfrm_nr = 0;
1142 } else {
1143 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1144 int nr = nla_len(rt) / sizeof(*utmpl);
1145 int err;
1147 err = validate_tmpl(nr, utmpl, pol->family);
1148 if (err)
1149 return err;
1151 copy_templates(pol, utmpl, nr);
1153 return 0;
1156 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1158 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1159 struct xfrm_userpolicy_type *upt;
1160 u8 type = XFRM_POLICY_TYPE_MAIN;
1161 int err;
1163 if (rt) {
1164 upt = nla_data(rt);
1165 type = upt->type;
1168 err = verify_policy_type(type);
1169 if (err)
1170 return err;
1172 *tp = type;
1173 return 0;
1176 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1178 xp->priority = p->priority;
1179 xp->index = p->index;
1180 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1181 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1182 xp->action = p->action;
1183 xp->flags = p->flags;
1184 xp->family = p->sel.family;
1185 /* XXX xp->share = p->share; */
1188 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1190 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1191 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1192 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1193 p->priority = xp->priority;
1194 p->index = xp->index;
1195 p->sel.family = xp->family;
1196 p->dir = dir;
1197 p->action = xp->action;
1198 p->flags = xp->flags;
1199 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1202 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1204 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1205 int err;
1207 if (!xp) {
1208 *errp = -ENOMEM;
1209 return NULL;
1212 copy_from_user_policy(xp, p);
1214 err = copy_from_user_policy_type(&xp->type, attrs);
1215 if (err)
1216 goto error;
1218 if (!(err = copy_from_user_tmpl(xp, attrs)))
1219 err = copy_from_user_sec_ctx(xp, attrs);
1220 if (err)
1221 goto error;
1223 return xp;
1224 error:
1225 *errp = err;
1226 xp->walk.dead = 1;
1227 xfrm_policy_destroy(xp);
1228 return NULL;
1231 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1232 struct nlattr **attrs)
1234 struct net *net = sock_net(skb->sk);
1235 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1236 struct xfrm_policy *xp;
1237 struct km_event c;
1238 int err;
1239 int excl;
1240 uid_t loginuid = NETLINK_CB(skb).loginuid;
1241 u32 sessionid = NETLINK_CB(skb).sessionid;
1242 u32 sid = NETLINK_CB(skb).sid;
1244 err = verify_newpolicy_info(p);
1245 if (err)
1246 return err;
1247 err = verify_sec_ctx_len(attrs);
1248 if (err)
1249 return err;
1251 xp = xfrm_policy_construct(net, p, attrs, &err);
1252 if (!xp)
1253 return err;
1255 /* shouldnt excl be based on nlh flags??
1256 * Aha! this is anti-netlink really i.e more pfkey derived
1257 * in netlink excl is a flag and you wouldnt need
1258 * a type XFRM_MSG_UPDPOLICY - JHS */
1259 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1260 err = xfrm_policy_insert(p->dir, xp, excl);
1261 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1263 if (err) {
1264 security_xfrm_policy_free(xp->security);
1265 kfree(xp);
1266 return err;
1269 c.event = nlh->nlmsg_type;
1270 c.seq = nlh->nlmsg_seq;
1271 c.pid = nlh->nlmsg_pid;
1272 km_policy_notify(xp, p->dir, &c);
1274 xfrm_pol_put(xp);
1276 return 0;
1279 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1281 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1282 int i;
1284 if (xp->xfrm_nr == 0)
1285 return 0;
1287 for (i = 0; i < xp->xfrm_nr; i++) {
1288 struct xfrm_user_tmpl *up = &vec[i];
1289 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1291 memcpy(&up->id, &kp->id, sizeof(up->id));
1292 up->family = kp->encap_family;
1293 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1294 up->reqid = kp->reqid;
1295 up->mode = kp->mode;
1296 up->share = kp->share;
1297 up->optional = kp->optional;
1298 up->aalgos = kp->aalgos;
1299 up->ealgos = kp->ealgos;
1300 up->calgos = kp->calgos;
1303 return nla_put(skb, XFRMA_TMPL,
1304 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1307 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1309 if (x->security) {
1310 return copy_sec_ctx(x->security, skb);
1312 return 0;
1315 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1317 if (xp->security) {
1318 return copy_sec_ctx(xp->security, skb);
1320 return 0;
1322 static inline size_t userpolicy_type_attrsize(void)
1324 #ifdef CONFIG_XFRM_SUB_POLICY
1325 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1326 #else
1327 return 0;
1328 #endif
1331 #ifdef CONFIG_XFRM_SUB_POLICY
1332 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1334 struct xfrm_userpolicy_type upt = {
1335 .type = type,
1338 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1341 #else
1342 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1344 return 0;
1346 #endif
1348 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1350 struct xfrm_dump_info *sp = ptr;
1351 struct xfrm_userpolicy_info *p;
1352 struct sk_buff *in_skb = sp->in_skb;
1353 struct sk_buff *skb = sp->out_skb;
1354 struct nlmsghdr *nlh;
1356 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1357 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1358 if (nlh == NULL)
1359 return -EMSGSIZE;
1361 p = nlmsg_data(nlh);
1362 copy_to_user_policy(xp, p, dir);
1363 if (copy_to_user_tmpl(xp, skb) < 0)
1364 goto nlmsg_failure;
1365 if (copy_to_user_sec_ctx(xp, skb))
1366 goto nlmsg_failure;
1367 if (copy_to_user_policy_type(xp->type, skb) < 0)
1368 goto nlmsg_failure;
1370 nlmsg_end(skb, nlh);
1371 return 0;
1373 nlmsg_failure:
1374 nlmsg_cancel(skb, nlh);
1375 return -EMSGSIZE;
1378 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1380 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1382 xfrm_policy_walk_done(walk);
1383 return 0;
1386 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1388 struct net *net = sock_net(skb->sk);
1389 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1390 struct xfrm_dump_info info;
1392 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1393 sizeof(cb->args) - sizeof(cb->args[0]));
1395 info.in_skb = cb->skb;
1396 info.out_skb = skb;
1397 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1398 info.nlmsg_flags = NLM_F_MULTI;
1400 if (!cb->args[0]) {
1401 cb->args[0] = 1;
1402 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1405 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1407 return skb->len;
1410 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1411 struct xfrm_policy *xp,
1412 int dir, u32 seq)
1414 struct xfrm_dump_info info;
1415 struct sk_buff *skb;
1417 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1418 if (!skb)
1419 return ERR_PTR(-ENOMEM);
1421 info.in_skb = in_skb;
1422 info.out_skb = skb;
1423 info.nlmsg_seq = seq;
1424 info.nlmsg_flags = 0;
1426 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1427 kfree_skb(skb);
1428 return NULL;
1431 return skb;
1434 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1435 struct nlattr **attrs)
1437 struct net *net = sock_net(skb->sk);
1438 struct xfrm_policy *xp;
1439 struct xfrm_userpolicy_id *p;
1440 u8 type = XFRM_POLICY_TYPE_MAIN;
1441 int err;
1442 struct km_event c;
1443 int delete;
1445 p = nlmsg_data(nlh);
1446 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1448 err = copy_from_user_policy_type(&type, attrs);
1449 if (err)
1450 return err;
1452 err = verify_policy_dir(p->dir);
1453 if (err)
1454 return err;
1456 if (p->index)
1457 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
1458 else {
1459 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1460 struct xfrm_sec_ctx *ctx;
1462 err = verify_sec_ctx_len(attrs);
1463 if (err)
1464 return err;
1466 ctx = NULL;
1467 if (rt) {
1468 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1470 err = security_xfrm_policy_alloc(&ctx, uctx);
1471 if (err)
1472 return err;
1474 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
1475 delete, &err);
1476 security_xfrm_policy_free(ctx);
1478 if (xp == NULL)
1479 return -ENOENT;
1481 if (!delete) {
1482 struct sk_buff *resp_skb;
1484 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1485 if (IS_ERR(resp_skb)) {
1486 err = PTR_ERR(resp_skb);
1487 } else {
1488 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1489 NETLINK_CB(skb).pid);
1491 } else {
1492 uid_t loginuid = NETLINK_CB(skb).loginuid;
1493 u32 sessionid = NETLINK_CB(skb).sessionid;
1494 u32 sid = NETLINK_CB(skb).sid;
1496 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1497 sid);
1499 if (err != 0)
1500 goto out;
1502 c.data.byid = p->index;
1503 c.event = nlh->nlmsg_type;
1504 c.seq = nlh->nlmsg_seq;
1505 c.pid = nlh->nlmsg_pid;
1506 km_policy_notify(xp, p->dir, &c);
1509 out:
1510 xfrm_pol_put(xp);
1511 return err;
1514 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1515 struct nlattr **attrs)
1517 struct net *net = sock_net(skb->sk);
1518 struct km_event c;
1519 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1520 struct xfrm_audit audit_info;
1521 int err;
1523 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1524 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1525 audit_info.secid = NETLINK_CB(skb).sid;
1526 err = xfrm_state_flush(net, p->proto, &audit_info);
1527 if (err)
1528 return err;
1529 c.data.proto = p->proto;
1530 c.event = nlh->nlmsg_type;
1531 c.seq = nlh->nlmsg_seq;
1532 c.pid = nlh->nlmsg_pid;
1533 c.net = net;
1534 km_state_notify(NULL, &c);
1536 return 0;
1539 static inline size_t xfrm_aevent_msgsize(void)
1541 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1542 + nla_total_size(sizeof(struct xfrm_replay_state))
1543 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1544 + nla_total_size(4) /* XFRM_AE_RTHR */
1545 + nla_total_size(4); /* XFRM_AE_ETHR */
1548 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1550 struct xfrm_aevent_id *id;
1551 struct nlmsghdr *nlh;
1553 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1554 if (nlh == NULL)
1555 return -EMSGSIZE;
1557 id = nlmsg_data(nlh);
1558 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1559 id->sa_id.spi = x->id.spi;
1560 id->sa_id.family = x->props.family;
1561 id->sa_id.proto = x->id.proto;
1562 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1563 id->reqid = x->props.reqid;
1564 id->flags = c->data.aevent;
1566 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1567 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1569 if (id->flags & XFRM_AE_RTHR)
1570 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1572 if (id->flags & XFRM_AE_ETHR)
1573 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1574 x->replay_maxage * 10 / HZ);
1576 return nlmsg_end(skb, nlh);
1578 nla_put_failure:
1579 nlmsg_cancel(skb, nlh);
1580 return -EMSGSIZE;
1583 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1584 struct nlattr **attrs)
1586 struct net *net = sock_net(skb->sk);
1587 struct xfrm_state *x;
1588 struct sk_buff *r_skb;
1589 int err;
1590 struct km_event c;
1591 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1592 struct xfrm_usersa_id *id = &p->sa_id;
1594 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1595 if (r_skb == NULL)
1596 return -ENOMEM;
1598 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
1599 if (x == NULL) {
1600 kfree_skb(r_skb);
1601 return -ESRCH;
1605 * XXX: is this lock really needed - none of the other
1606 * gets lock (the concern is things getting updated
1607 * while we are still reading) - jhs
1609 spin_lock_bh(&x->lock);
1610 c.data.aevent = p->flags;
1611 c.seq = nlh->nlmsg_seq;
1612 c.pid = nlh->nlmsg_pid;
1614 if (build_aevent(r_skb, x, &c) < 0)
1615 BUG();
1616 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1617 spin_unlock_bh(&x->lock);
1618 xfrm_state_put(x);
1619 return err;
1622 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1623 struct nlattr **attrs)
1625 struct net *net = sock_net(skb->sk);
1626 struct xfrm_state *x;
1627 struct km_event c;
1628 int err = - EINVAL;
1629 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1630 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1631 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1633 if (!lt && !rp)
1634 return err;
1636 /* pedantic mode - thou shalt sayeth replaceth */
1637 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1638 return err;
1640 x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1641 if (x == NULL)
1642 return -ESRCH;
1644 if (x->km.state != XFRM_STATE_VALID)
1645 goto out;
1647 spin_lock_bh(&x->lock);
1648 xfrm_update_ae_params(x, attrs);
1649 spin_unlock_bh(&x->lock);
1651 c.event = nlh->nlmsg_type;
1652 c.seq = nlh->nlmsg_seq;
1653 c.pid = nlh->nlmsg_pid;
1654 c.data.aevent = XFRM_AE_CU;
1655 km_state_notify(x, &c);
1656 err = 0;
1657 out:
1658 xfrm_state_put(x);
1659 return err;
1662 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1663 struct nlattr **attrs)
1665 struct net *net = sock_net(skb->sk);
1666 struct km_event c;
1667 u8 type = XFRM_POLICY_TYPE_MAIN;
1668 int err;
1669 struct xfrm_audit audit_info;
1671 err = copy_from_user_policy_type(&type, attrs);
1672 if (err)
1673 return err;
1675 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1676 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1677 audit_info.secid = NETLINK_CB(skb).sid;
1678 err = xfrm_policy_flush(net, type, &audit_info);
1679 if (err)
1680 return err;
1681 c.data.type = type;
1682 c.event = nlh->nlmsg_type;
1683 c.seq = nlh->nlmsg_seq;
1684 c.pid = nlh->nlmsg_pid;
1685 c.net = net;
1686 km_policy_notify(NULL, 0, &c);
1687 return 0;
1690 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1691 struct nlattr **attrs)
1693 struct net *net = sock_net(skb->sk);
1694 struct xfrm_policy *xp;
1695 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1696 struct xfrm_userpolicy_info *p = &up->pol;
1697 u8 type = XFRM_POLICY_TYPE_MAIN;
1698 int err = -ENOENT;
1700 err = copy_from_user_policy_type(&type, attrs);
1701 if (err)
1702 return err;
1704 if (p->index)
1705 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
1706 else {
1707 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1708 struct xfrm_sec_ctx *ctx;
1710 err = verify_sec_ctx_len(attrs);
1711 if (err)
1712 return err;
1714 ctx = NULL;
1715 if (rt) {
1716 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1718 err = security_xfrm_policy_alloc(&ctx, uctx);
1719 if (err)
1720 return err;
1722 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
1723 security_xfrm_policy_free(ctx);
1725 if (xp == NULL)
1726 return -ENOENT;
1728 read_lock(&xp->lock);
1729 if (xp->walk.dead) {
1730 read_unlock(&xp->lock);
1731 goto out;
1734 read_unlock(&xp->lock);
1735 err = 0;
1736 if (up->hard) {
1737 uid_t loginuid = NETLINK_CB(skb).loginuid;
1738 uid_t sessionid = NETLINK_CB(skb).sessionid;
1739 u32 sid = NETLINK_CB(skb).sid;
1740 xfrm_policy_delete(xp, p->dir);
1741 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1743 } else {
1744 // reset the timers here?
1745 printk("Dont know what to do with soft policy expire\n");
1747 km_policy_expired(xp, p->dir, up->hard, current->pid);
1749 out:
1750 xfrm_pol_put(xp);
1751 return err;
1754 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1755 struct nlattr **attrs)
1757 struct net *net = sock_net(skb->sk);
1758 struct xfrm_state *x;
1759 int err;
1760 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1761 struct xfrm_usersa_info *p = &ue->state;
1763 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1765 err = -ENOENT;
1766 if (x == NULL)
1767 return err;
1769 spin_lock_bh(&x->lock);
1770 err = -EINVAL;
1771 if (x->km.state != XFRM_STATE_VALID)
1772 goto out;
1773 km_state_expired(x, ue->hard, current->pid);
1775 if (ue->hard) {
1776 uid_t loginuid = NETLINK_CB(skb).loginuid;
1777 uid_t sessionid = NETLINK_CB(skb).sessionid;
1778 u32 sid = NETLINK_CB(skb).sid;
1779 __xfrm_state_delete(x);
1780 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1782 err = 0;
1783 out:
1784 spin_unlock_bh(&x->lock);
1785 xfrm_state_put(x);
1786 return err;
1789 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1790 struct nlattr **attrs)
1792 struct net *net = sock_net(skb->sk);
1793 struct xfrm_policy *xp;
1794 struct xfrm_user_tmpl *ut;
1795 int i;
1796 struct nlattr *rt = attrs[XFRMA_TMPL];
1798 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1799 struct xfrm_state *x = xfrm_state_alloc(net);
1800 int err = -ENOMEM;
1802 if (!x)
1803 goto nomem;
1805 err = verify_newpolicy_info(&ua->policy);
1806 if (err)
1807 goto bad_policy;
1809 /* build an XP */
1810 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1811 if (!xp)
1812 goto free_state;
1814 memcpy(&x->id, &ua->id, sizeof(ua->id));
1815 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1816 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1818 ut = nla_data(rt);
1819 /* extract the templates and for each call km_key */
1820 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1821 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1822 memcpy(&x->id, &t->id, sizeof(x->id));
1823 x->props.mode = t->mode;
1824 x->props.reqid = t->reqid;
1825 x->props.family = ut->family;
1826 t->aalgos = ua->aalgos;
1827 t->ealgos = ua->ealgos;
1828 t->calgos = ua->calgos;
1829 err = km_query(x, t, xp);
1833 kfree(x);
1834 kfree(xp);
1836 return 0;
1838 bad_policy:
1839 printk("BAD policy passed\n");
1840 free_state:
1841 kfree(x);
1842 nomem:
1843 return err;
1846 #ifdef CONFIG_XFRM_MIGRATE
1847 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1848 struct xfrm_kmaddress *k,
1849 struct nlattr **attrs, int *num)
1851 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1852 struct xfrm_user_migrate *um;
1853 int i, num_migrate;
1855 if (k != NULL) {
1856 struct xfrm_user_kmaddress *uk;
1858 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1859 memcpy(&k->local, &uk->local, sizeof(k->local));
1860 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1861 k->family = uk->family;
1862 k->reserved = uk->reserved;
1865 um = nla_data(rt);
1866 num_migrate = nla_len(rt) / sizeof(*um);
1868 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1869 return -EINVAL;
1871 for (i = 0; i < num_migrate; i++, um++, ma++) {
1872 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1873 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1874 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1875 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1877 ma->proto = um->proto;
1878 ma->mode = um->mode;
1879 ma->reqid = um->reqid;
1881 ma->old_family = um->old_family;
1882 ma->new_family = um->new_family;
1885 *num = i;
1886 return 0;
1889 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1890 struct nlattr **attrs)
1892 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1893 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1894 struct xfrm_kmaddress km, *kmp;
1895 u8 type;
1896 int err;
1897 int n = 0;
1899 if (attrs[XFRMA_MIGRATE] == NULL)
1900 return -EINVAL;
1902 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1904 err = copy_from_user_policy_type(&type, attrs);
1905 if (err)
1906 return err;
1908 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
1909 if (err)
1910 return err;
1912 if (!n)
1913 return 0;
1915 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
1917 return 0;
1919 #else
1920 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1921 struct nlattr **attrs)
1923 return -ENOPROTOOPT;
1925 #endif
1927 #ifdef CONFIG_XFRM_MIGRATE
1928 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1930 struct xfrm_user_migrate um;
1932 memset(&um, 0, sizeof(um));
1933 um.proto = m->proto;
1934 um.mode = m->mode;
1935 um.reqid = m->reqid;
1936 um.old_family = m->old_family;
1937 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1938 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1939 um.new_family = m->new_family;
1940 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1941 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1943 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1946 static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1948 struct xfrm_user_kmaddress uk;
1950 memset(&uk, 0, sizeof(uk));
1951 uk.family = k->family;
1952 uk.reserved = k->reserved;
1953 memcpy(&uk.local, &k->local, sizeof(uk.local));
1954 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
1956 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1959 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
1961 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1962 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1963 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1964 + userpolicy_type_attrsize();
1967 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1968 int num_migrate, struct xfrm_kmaddress *k,
1969 struct xfrm_selector *sel, u8 dir, u8 type)
1971 struct xfrm_migrate *mp;
1972 struct xfrm_userpolicy_id *pol_id;
1973 struct nlmsghdr *nlh;
1974 int i;
1976 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1977 if (nlh == NULL)
1978 return -EMSGSIZE;
1980 pol_id = nlmsg_data(nlh);
1981 /* copy data from selector, dir, and type to the pol_id */
1982 memset(pol_id, 0, sizeof(*pol_id));
1983 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1984 pol_id->dir = dir;
1986 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1987 goto nlmsg_failure;
1989 if (copy_to_user_policy_type(type, skb) < 0)
1990 goto nlmsg_failure;
1992 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1993 if (copy_to_user_migrate(mp, skb) < 0)
1994 goto nlmsg_failure;
1997 return nlmsg_end(skb, nlh);
1998 nlmsg_failure:
1999 nlmsg_cancel(skb, nlh);
2000 return -EMSGSIZE;
2003 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2004 struct xfrm_migrate *m, int num_migrate,
2005 struct xfrm_kmaddress *k)
2007 struct net *net = &init_net;
2008 struct sk_buff *skb;
2010 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2011 if (skb == NULL)
2012 return -ENOMEM;
2014 /* build migrate */
2015 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2016 BUG();
2018 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2020 #else
2021 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2022 struct xfrm_migrate *m, int num_migrate,
2023 struct xfrm_kmaddress *k)
2025 return -ENOPROTOOPT;
2027 #endif
2029 #define XMSGSIZE(type) sizeof(struct type)
2031 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2032 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2033 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2034 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2035 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2036 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2037 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2038 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2039 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2040 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2041 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2042 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2043 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2044 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2045 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2046 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2047 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2048 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2049 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2050 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2051 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2054 #undef XMSGSIZE
2056 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2057 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2058 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2059 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2060 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2061 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2062 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2063 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2064 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2065 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2066 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2067 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2068 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2069 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2070 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2071 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2072 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2075 static struct xfrm_link {
2076 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2077 int (*dump)(struct sk_buff *, struct netlink_callback *);
2078 int (*done)(struct netlink_callback *);
2079 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2080 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2081 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2082 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2083 .dump = xfrm_dump_sa,
2084 .done = xfrm_dump_sa_done },
2085 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2086 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2087 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2088 .dump = xfrm_dump_policy,
2089 .done = xfrm_dump_policy_done },
2090 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2091 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2092 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2093 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2094 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2095 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2096 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2097 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2098 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2099 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2100 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2101 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2102 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2105 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2107 struct net *net = sock_net(skb->sk);
2108 struct nlattr *attrs[XFRMA_MAX+1];
2109 struct xfrm_link *link;
2110 int type, err;
2112 type = nlh->nlmsg_type;
2113 if (type > XFRM_MSG_MAX)
2114 return -EINVAL;
2116 type -= XFRM_MSG_BASE;
2117 link = &xfrm_dispatch[type];
2119 /* All operations require privileges, even GET */
2120 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2121 return -EPERM;
2123 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2124 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2125 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2126 if (link->dump == NULL)
2127 return -EINVAL;
2129 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2132 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2133 xfrma_policy);
2134 if (err < 0)
2135 return err;
2137 if (link->doit == NULL)
2138 return -EINVAL;
2140 return link->doit(skb, nlh, attrs);
2143 static void xfrm_netlink_rcv(struct sk_buff *skb)
2145 mutex_lock(&xfrm_cfg_mutex);
2146 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2147 mutex_unlock(&xfrm_cfg_mutex);
2150 static inline size_t xfrm_expire_msgsize(void)
2152 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2155 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
2157 struct xfrm_user_expire *ue;
2158 struct nlmsghdr *nlh;
2160 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2161 if (nlh == NULL)
2162 return -EMSGSIZE;
2164 ue = nlmsg_data(nlh);
2165 copy_to_user_state(x, &ue->state);
2166 ue->hard = (c->data.hard != 0) ? 1 : 0;
2168 return nlmsg_end(skb, nlh);
2171 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2173 struct net *net = xs_net(x);
2174 struct sk_buff *skb;
2176 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2177 if (skb == NULL)
2178 return -ENOMEM;
2180 if (build_expire(skb, x, c) < 0)
2181 BUG();
2183 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2186 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2188 struct net *net = xs_net(x);
2189 struct sk_buff *skb;
2191 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2192 if (skb == NULL)
2193 return -ENOMEM;
2195 if (build_aevent(skb, x, c) < 0)
2196 BUG();
2198 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2201 static int xfrm_notify_sa_flush(struct km_event *c)
2203 struct net *net = c->net;
2204 struct xfrm_usersa_flush *p;
2205 struct nlmsghdr *nlh;
2206 struct sk_buff *skb;
2207 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2209 skb = nlmsg_new(len, GFP_ATOMIC);
2210 if (skb == NULL)
2211 return -ENOMEM;
2213 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2214 if (nlh == NULL) {
2215 kfree_skb(skb);
2216 return -EMSGSIZE;
2219 p = nlmsg_data(nlh);
2220 p->proto = c->data.proto;
2222 nlmsg_end(skb, nlh);
2224 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2227 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2229 size_t l = 0;
2230 if (x->aead)
2231 l += nla_total_size(aead_len(x->aead));
2232 if (x->aalg) {
2233 l += nla_total_size(sizeof(struct xfrm_algo) +
2234 (x->aalg->alg_key_len + 7) / 8);
2235 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2237 if (x->ealg)
2238 l += nla_total_size(xfrm_alg_len(x->ealg));
2239 if (x->calg)
2240 l += nla_total_size(sizeof(*x->calg));
2241 if (x->encap)
2242 l += nla_total_size(sizeof(*x->encap));
2243 if (x->security)
2244 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2245 x->security->ctx_len);
2246 if (x->coaddr)
2247 l += nla_total_size(sizeof(*x->coaddr));
2249 /* Must count x->lastused as it may become non-zero behind our back. */
2250 l += nla_total_size(sizeof(u64));
2252 return l;
2255 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2257 struct net *net = xs_net(x);
2258 struct xfrm_usersa_info *p;
2259 struct xfrm_usersa_id *id;
2260 struct nlmsghdr *nlh;
2261 struct sk_buff *skb;
2262 int len = xfrm_sa_len(x);
2263 int headlen;
2265 headlen = sizeof(*p);
2266 if (c->event == XFRM_MSG_DELSA) {
2267 len += nla_total_size(headlen);
2268 headlen = sizeof(*id);
2270 len += NLMSG_ALIGN(headlen);
2272 skb = nlmsg_new(len, GFP_ATOMIC);
2273 if (skb == NULL)
2274 return -ENOMEM;
2276 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2277 if (nlh == NULL)
2278 goto nla_put_failure;
2280 p = nlmsg_data(nlh);
2281 if (c->event == XFRM_MSG_DELSA) {
2282 struct nlattr *attr;
2284 id = nlmsg_data(nlh);
2285 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2286 id->spi = x->id.spi;
2287 id->family = x->props.family;
2288 id->proto = x->id.proto;
2290 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2291 if (attr == NULL)
2292 goto nla_put_failure;
2294 p = nla_data(attr);
2297 if (copy_to_user_state_extra(x, p, skb))
2298 goto nla_put_failure;
2300 nlmsg_end(skb, nlh);
2302 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2304 nla_put_failure:
2305 /* Somebody screwed up with xfrm_sa_len! */
2306 WARN_ON(1);
2307 kfree_skb(skb);
2308 return -1;
2311 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2314 switch (c->event) {
2315 case XFRM_MSG_EXPIRE:
2316 return xfrm_exp_state_notify(x, c);
2317 case XFRM_MSG_NEWAE:
2318 return xfrm_aevent_state_notify(x, c);
2319 case XFRM_MSG_DELSA:
2320 case XFRM_MSG_UPDSA:
2321 case XFRM_MSG_NEWSA:
2322 return xfrm_notify_sa(x, c);
2323 case XFRM_MSG_FLUSHSA:
2324 return xfrm_notify_sa_flush(c);
2325 default:
2326 printk("xfrm_user: Unknown SA event %d\n", c->event);
2327 break;
2330 return 0;
2334 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2335 struct xfrm_policy *xp)
2337 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2338 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2339 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2340 + userpolicy_type_attrsize();
2343 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2344 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2345 int dir)
2347 struct xfrm_user_acquire *ua;
2348 struct nlmsghdr *nlh;
2349 __u32 seq = xfrm_get_acqseq();
2351 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2352 if (nlh == NULL)
2353 return -EMSGSIZE;
2355 ua = nlmsg_data(nlh);
2356 memcpy(&ua->id, &x->id, sizeof(ua->id));
2357 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2358 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2359 copy_to_user_policy(xp, &ua->policy, dir);
2360 ua->aalgos = xt->aalgos;
2361 ua->ealgos = xt->ealgos;
2362 ua->calgos = xt->calgos;
2363 ua->seq = x->km.seq = seq;
2365 if (copy_to_user_tmpl(xp, skb) < 0)
2366 goto nlmsg_failure;
2367 if (copy_to_user_state_sec_ctx(x, skb))
2368 goto nlmsg_failure;
2369 if (copy_to_user_policy_type(xp->type, skb) < 0)
2370 goto nlmsg_failure;
2372 return nlmsg_end(skb, nlh);
2374 nlmsg_failure:
2375 nlmsg_cancel(skb, nlh);
2376 return -EMSGSIZE;
2379 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2380 struct xfrm_policy *xp, int dir)
2382 struct net *net = xs_net(x);
2383 struct sk_buff *skb;
2385 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2386 if (skb == NULL)
2387 return -ENOMEM;
2389 if (build_acquire(skb, x, xt, xp, dir) < 0)
2390 BUG();
2392 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2395 /* User gives us xfrm_user_policy_info followed by an array of 0
2396 * or more templates.
2398 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2399 u8 *data, int len, int *dir)
2401 struct net *net = sock_net(sk);
2402 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2403 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2404 struct xfrm_policy *xp;
2405 int nr;
2407 switch (sk->sk_family) {
2408 case AF_INET:
2409 if (opt != IP_XFRM_POLICY) {
2410 *dir = -EOPNOTSUPP;
2411 return NULL;
2413 break;
2414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2415 case AF_INET6:
2416 if (opt != IPV6_XFRM_POLICY) {
2417 *dir = -EOPNOTSUPP;
2418 return NULL;
2420 break;
2421 #endif
2422 default:
2423 *dir = -EINVAL;
2424 return NULL;
2427 *dir = -EINVAL;
2429 if (len < sizeof(*p) ||
2430 verify_newpolicy_info(p))
2431 return NULL;
2433 nr = ((len - sizeof(*p)) / sizeof(*ut));
2434 if (validate_tmpl(nr, ut, p->sel.family))
2435 return NULL;
2437 if (p->dir > XFRM_POLICY_OUT)
2438 return NULL;
2440 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2441 if (xp == NULL) {
2442 *dir = -ENOBUFS;
2443 return NULL;
2446 copy_from_user_policy(xp, p);
2447 xp->type = XFRM_POLICY_TYPE_MAIN;
2448 copy_templates(xp, ut, nr);
2450 *dir = p->dir;
2452 return xp;
2455 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2457 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2458 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2459 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2460 + userpolicy_type_attrsize();
2463 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2464 int dir, struct km_event *c)
2466 struct xfrm_user_polexpire *upe;
2467 struct nlmsghdr *nlh;
2468 int hard = c->data.hard;
2470 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2471 if (nlh == NULL)
2472 return -EMSGSIZE;
2474 upe = nlmsg_data(nlh);
2475 copy_to_user_policy(xp, &upe->pol, dir);
2476 if (copy_to_user_tmpl(xp, skb) < 0)
2477 goto nlmsg_failure;
2478 if (copy_to_user_sec_ctx(xp, skb))
2479 goto nlmsg_failure;
2480 if (copy_to_user_policy_type(xp->type, skb) < 0)
2481 goto nlmsg_failure;
2482 upe->hard = !!hard;
2484 return nlmsg_end(skb, nlh);
2486 nlmsg_failure:
2487 nlmsg_cancel(skb, nlh);
2488 return -EMSGSIZE;
2491 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2493 struct net *net = xp_net(xp);
2494 struct sk_buff *skb;
2496 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2497 if (skb == NULL)
2498 return -ENOMEM;
2500 if (build_polexpire(skb, xp, dir, c) < 0)
2501 BUG();
2503 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2506 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2508 struct net *net = xp_net(xp);
2509 struct xfrm_userpolicy_info *p;
2510 struct xfrm_userpolicy_id *id;
2511 struct nlmsghdr *nlh;
2512 struct sk_buff *skb;
2513 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2514 int headlen;
2516 headlen = sizeof(*p);
2517 if (c->event == XFRM_MSG_DELPOLICY) {
2518 len += nla_total_size(headlen);
2519 headlen = sizeof(*id);
2521 len += userpolicy_type_attrsize();
2522 len += NLMSG_ALIGN(headlen);
2524 skb = nlmsg_new(len, GFP_ATOMIC);
2525 if (skb == NULL)
2526 return -ENOMEM;
2528 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2529 if (nlh == NULL)
2530 goto nlmsg_failure;
2532 p = nlmsg_data(nlh);
2533 if (c->event == XFRM_MSG_DELPOLICY) {
2534 struct nlattr *attr;
2536 id = nlmsg_data(nlh);
2537 memset(id, 0, sizeof(*id));
2538 id->dir = dir;
2539 if (c->data.byid)
2540 id->index = xp->index;
2541 else
2542 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2544 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2545 if (attr == NULL)
2546 goto nlmsg_failure;
2548 p = nla_data(attr);
2551 copy_to_user_policy(xp, p, dir);
2552 if (copy_to_user_tmpl(xp, skb) < 0)
2553 goto nlmsg_failure;
2554 if (copy_to_user_policy_type(xp->type, skb) < 0)
2555 goto nlmsg_failure;
2557 nlmsg_end(skb, nlh);
2559 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2561 nlmsg_failure:
2562 kfree_skb(skb);
2563 return -1;
2566 static int xfrm_notify_policy_flush(struct km_event *c)
2568 struct net *net = c->net;
2569 struct nlmsghdr *nlh;
2570 struct sk_buff *skb;
2572 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2573 if (skb == NULL)
2574 return -ENOMEM;
2576 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2577 if (nlh == NULL)
2578 goto nlmsg_failure;
2579 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2580 goto nlmsg_failure;
2582 nlmsg_end(skb, nlh);
2584 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2586 nlmsg_failure:
2587 kfree_skb(skb);
2588 return -1;
2591 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2594 switch (c->event) {
2595 case XFRM_MSG_NEWPOLICY:
2596 case XFRM_MSG_UPDPOLICY:
2597 case XFRM_MSG_DELPOLICY:
2598 return xfrm_notify_policy(xp, dir, c);
2599 case XFRM_MSG_FLUSHPOLICY:
2600 return xfrm_notify_policy_flush(c);
2601 case XFRM_MSG_POLEXPIRE:
2602 return xfrm_exp_policy_notify(xp, dir, c);
2603 default:
2604 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2607 return 0;
2611 static inline size_t xfrm_report_msgsize(void)
2613 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2616 static int build_report(struct sk_buff *skb, u8 proto,
2617 struct xfrm_selector *sel, xfrm_address_t *addr)
2619 struct xfrm_user_report *ur;
2620 struct nlmsghdr *nlh;
2622 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2623 if (nlh == NULL)
2624 return -EMSGSIZE;
2626 ur = nlmsg_data(nlh);
2627 ur->proto = proto;
2628 memcpy(&ur->sel, sel, sizeof(ur->sel));
2630 if (addr)
2631 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2633 return nlmsg_end(skb, nlh);
2635 nla_put_failure:
2636 nlmsg_cancel(skb, nlh);
2637 return -EMSGSIZE;
2640 static int xfrm_send_report(struct net *net, u8 proto,
2641 struct xfrm_selector *sel, xfrm_address_t *addr)
2643 struct sk_buff *skb;
2645 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2646 if (skb == NULL)
2647 return -ENOMEM;
2649 if (build_report(skb, proto, sel, addr) < 0)
2650 BUG();
2652 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2655 static inline size_t xfrm_mapping_msgsize(void)
2657 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2660 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2661 xfrm_address_t *new_saddr, __be16 new_sport)
2663 struct xfrm_user_mapping *um;
2664 struct nlmsghdr *nlh;
2666 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2667 if (nlh == NULL)
2668 return -EMSGSIZE;
2670 um = nlmsg_data(nlh);
2672 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2673 um->id.spi = x->id.spi;
2674 um->id.family = x->props.family;
2675 um->id.proto = x->id.proto;
2676 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2677 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2678 um->new_sport = new_sport;
2679 um->old_sport = x->encap->encap_sport;
2680 um->reqid = x->props.reqid;
2682 return nlmsg_end(skb, nlh);
2685 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2686 __be16 sport)
2688 struct net *net = xs_net(x);
2689 struct sk_buff *skb;
2691 if (x->id.proto != IPPROTO_ESP)
2692 return -EINVAL;
2694 if (!x->encap)
2695 return -EINVAL;
2697 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2698 if (skb == NULL)
2699 return -ENOMEM;
2701 if (build_mapping(skb, x, ipaddr, sport) < 0)
2702 BUG();
2704 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2707 static struct xfrm_mgr netlink_mgr = {
2708 .id = "netlink",
2709 .notify = xfrm_send_state_notify,
2710 .acquire = xfrm_send_acquire,
2711 .compile_policy = xfrm_compile_policy,
2712 .notify_policy = xfrm_send_policy_notify,
2713 .report = xfrm_send_report,
2714 .migrate = xfrm_send_migrate,
2715 .new_mapping = xfrm_send_mapping,
2718 static int __net_init xfrm_user_net_init(struct net *net)
2720 struct sock *nlsk;
2722 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2723 xfrm_netlink_rcv, NULL, THIS_MODULE);
2724 if (nlsk == NULL)
2725 return -ENOMEM;
2726 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2727 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2728 return 0;
2731 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2733 struct net *net;
2734 list_for_each_entry(net, net_exit_list, exit_list)
2735 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2736 synchronize_net();
2737 list_for_each_entry(net, net_exit_list, exit_list)
2738 netlink_kernel_release(net->xfrm.nlsk_stash);
2741 static struct pernet_operations xfrm_user_net_ops = {
2742 .init = xfrm_user_net_init,
2743 .exit_batch = xfrm_user_net_exit,
2746 static int __init xfrm_user_init(void)
2748 int rv;
2750 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2752 rv = register_pernet_subsys(&xfrm_user_net_ops);
2753 if (rv < 0)
2754 return rv;
2755 rv = xfrm_register_km(&netlink_mgr);
2756 if (rv < 0)
2757 unregister_pernet_subsys(&xfrm_user_net_ops);
2758 return rv;
2761 static void __exit xfrm_user_exit(void)
2763 xfrm_unregister_km(&netlink_mgr);
2764 unregister_pernet_subsys(&xfrm_user_net_ops);
2767 module_init(xfrm_user_init);
2768 module_exit(xfrm_user_exit);
2769 MODULE_LICENSE("GPL");
2770 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);