mx31ads: Configure SSI5 pins in IOMUX for PMIC module audio support
[firewire-audio.git] / net / xfrm / xfrm_user.c
blob1ada6186933c24256c92bfee4db98eafde653a03
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, u32 pid, u32 seq, u32 flags)
786 struct xfrmk_spdinfo si;
787 struct xfrmu_spdinfo spc;
788 struct xfrmu_spdhinfo sph;
789 struct nlmsghdr *nlh;
790 u32 *f;
792 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
793 if (nlh == NULL) /* shouldnt really happen ... */
794 return -EMSGSIZE;
796 f = nlmsg_data(nlh);
797 *f = flags;
798 xfrm_spd_getinfo(&si);
799 spc.incnt = si.incnt;
800 spc.outcnt = si.outcnt;
801 spc.fwdcnt = si.fwdcnt;
802 spc.inscnt = si.inscnt;
803 spc.outscnt = si.outscnt;
804 spc.fwdscnt = si.fwdscnt;
805 sph.spdhcnt = si.spdhcnt;
806 sph.spdhmcnt = si.spdhmcnt;
808 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
809 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
811 return nlmsg_end(skb, nlh);
813 nla_put_failure:
814 nlmsg_cancel(skb, nlh);
815 return -EMSGSIZE;
818 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
819 struct nlattr **attrs)
821 struct net *net = sock_net(skb->sk);
822 struct sk_buff *r_skb;
823 u32 *flags = nlmsg_data(nlh);
824 u32 spid = NETLINK_CB(skb).pid;
825 u32 seq = nlh->nlmsg_seq;
827 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
828 if (r_skb == NULL)
829 return -ENOMEM;
831 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
832 BUG();
834 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
837 static inline size_t xfrm_sadinfo_msgsize(void)
839 return NLMSG_ALIGN(4)
840 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
841 + nla_total_size(4); /* XFRMA_SAD_CNT */
844 static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
846 struct xfrmk_sadinfo si;
847 struct xfrmu_sadhinfo sh;
848 struct nlmsghdr *nlh;
849 u32 *f;
851 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
852 if (nlh == NULL) /* shouldnt really happen ... */
853 return -EMSGSIZE;
855 f = nlmsg_data(nlh);
856 *f = flags;
857 xfrm_sad_getinfo(&si);
859 sh.sadhmcnt = si.sadhmcnt;
860 sh.sadhcnt = si.sadhcnt;
862 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
863 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
865 return nlmsg_end(skb, nlh);
867 nla_put_failure:
868 nlmsg_cancel(skb, nlh);
869 return -EMSGSIZE;
872 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
873 struct nlattr **attrs)
875 struct net *net = sock_net(skb->sk);
876 struct sk_buff *r_skb;
877 u32 *flags = nlmsg_data(nlh);
878 u32 spid = NETLINK_CB(skb).pid;
879 u32 seq = nlh->nlmsg_seq;
881 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
882 if (r_skb == NULL)
883 return -ENOMEM;
885 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
886 BUG();
888 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
891 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
892 struct nlattr **attrs)
894 struct net *net = sock_net(skb->sk);
895 struct xfrm_usersa_id *p = nlmsg_data(nlh);
896 struct xfrm_state *x;
897 struct sk_buff *resp_skb;
898 int err = -ESRCH;
900 x = xfrm_user_state_lookup(net, p, attrs, &err);
901 if (x == NULL)
902 goto out_noput;
904 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
905 if (IS_ERR(resp_skb)) {
906 err = PTR_ERR(resp_skb);
907 } else {
908 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
910 xfrm_state_put(x);
911 out_noput:
912 return err;
915 static int verify_userspi_info(struct xfrm_userspi_info *p)
917 switch (p->info.id.proto) {
918 case IPPROTO_AH:
919 case IPPROTO_ESP:
920 break;
922 case IPPROTO_COMP:
923 /* IPCOMP spi is 16-bits. */
924 if (p->max >= 0x10000)
925 return -EINVAL;
926 break;
928 default:
929 return -EINVAL;
932 if (p->min > p->max)
933 return -EINVAL;
935 return 0;
938 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
939 struct nlattr **attrs)
941 struct net *net = sock_net(skb->sk);
942 struct xfrm_state *x;
943 struct xfrm_userspi_info *p;
944 struct sk_buff *resp_skb;
945 xfrm_address_t *daddr;
946 int family;
947 int err;
949 p = nlmsg_data(nlh);
950 err = verify_userspi_info(p);
951 if (err)
952 goto out_noput;
954 family = p->info.family;
955 daddr = &p->info.id.daddr;
957 x = NULL;
958 if (p->info.seq) {
959 x = xfrm_find_acq_byseq(net, p->info.seq);
960 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
961 xfrm_state_put(x);
962 x = NULL;
966 if (!x)
967 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
968 p->info.id.proto, daddr,
969 &p->info.saddr, 1,
970 family);
971 err = -ENOENT;
972 if (x == NULL)
973 goto out_noput;
975 err = xfrm_alloc_spi(x, p->min, p->max);
976 if (err)
977 goto out;
979 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
980 if (IS_ERR(resp_skb)) {
981 err = PTR_ERR(resp_skb);
982 goto out;
985 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
987 out:
988 xfrm_state_put(x);
989 out_noput:
990 return err;
993 static int verify_policy_dir(u8 dir)
995 switch (dir) {
996 case XFRM_POLICY_IN:
997 case XFRM_POLICY_OUT:
998 case XFRM_POLICY_FWD:
999 break;
1001 default:
1002 return -EINVAL;
1005 return 0;
1008 static int verify_policy_type(u8 type)
1010 switch (type) {
1011 case XFRM_POLICY_TYPE_MAIN:
1012 #ifdef CONFIG_XFRM_SUB_POLICY
1013 case XFRM_POLICY_TYPE_SUB:
1014 #endif
1015 break;
1017 default:
1018 return -EINVAL;
1021 return 0;
1024 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1026 switch (p->share) {
1027 case XFRM_SHARE_ANY:
1028 case XFRM_SHARE_SESSION:
1029 case XFRM_SHARE_USER:
1030 case XFRM_SHARE_UNIQUE:
1031 break;
1033 default:
1034 return -EINVAL;
1037 switch (p->action) {
1038 case XFRM_POLICY_ALLOW:
1039 case XFRM_POLICY_BLOCK:
1040 break;
1042 default:
1043 return -EINVAL;
1046 switch (p->sel.family) {
1047 case AF_INET:
1048 break;
1050 case AF_INET6:
1051 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1052 break;
1053 #else
1054 return -EAFNOSUPPORT;
1055 #endif
1057 default:
1058 return -EINVAL;
1061 return verify_policy_dir(p->dir);
1064 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1066 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1067 struct xfrm_user_sec_ctx *uctx;
1069 if (!rt)
1070 return 0;
1072 uctx = nla_data(rt);
1073 return security_xfrm_policy_alloc(&pol->security, uctx);
1076 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1077 int nr)
1079 int i;
1081 xp->xfrm_nr = nr;
1082 for (i = 0; i < nr; i++, ut++) {
1083 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1085 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1086 memcpy(&t->saddr, &ut->saddr,
1087 sizeof(xfrm_address_t));
1088 t->reqid = ut->reqid;
1089 t->mode = ut->mode;
1090 t->share = ut->share;
1091 t->optional = ut->optional;
1092 t->aalgos = ut->aalgos;
1093 t->ealgos = ut->ealgos;
1094 t->calgos = ut->calgos;
1095 /* If all masks are ~0, then we allow all algorithms. */
1096 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1097 t->encap_family = ut->family;
1101 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1103 int i;
1105 if (nr > XFRM_MAX_DEPTH)
1106 return -EINVAL;
1108 for (i = 0; i < nr; i++) {
1109 /* We never validated the ut->family value, so many
1110 * applications simply leave it at zero. The check was
1111 * never made and ut->family was ignored because all
1112 * templates could be assumed to have the same family as
1113 * the policy itself. Now that we will have ipv4-in-ipv6
1114 * and ipv6-in-ipv4 tunnels, this is no longer true.
1116 if (!ut[i].family)
1117 ut[i].family = family;
1119 switch (ut[i].family) {
1120 case AF_INET:
1121 break;
1122 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1123 case AF_INET6:
1124 break;
1125 #endif
1126 default:
1127 return -EINVAL;
1131 return 0;
1134 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1136 struct nlattr *rt = attrs[XFRMA_TMPL];
1138 if (!rt) {
1139 pol->xfrm_nr = 0;
1140 } else {
1141 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1142 int nr = nla_len(rt) / sizeof(*utmpl);
1143 int err;
1145 err = validate_tmpl(nr, utmpl, pol->family);
1146 if (err)
1147 return err;
1149 copy_templates(pol, utmpl, nr);
1151 return 0;
1154 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1156 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1157 struct xfrm_userpolicy_type *upt;
1158 u8 type = XFRM_POLICY_TYPE_MAIN;
1159 int err;
1161 if (rt) {
1162 upt = nla_data(rt);
1163 type = upt->type;
1166 err = verify_policy_type(type);
1167 if (err)
1168 return err;
1170 *tp = type;
1171 return 0;
1174 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1176 xp->priority = p->priority;
1177 xp->index = p->index;
1178 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1179 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1180 xp->action = p->action;
1181 xp->flags = p->flags;
1182 xp->family = p->sel.family;
1183 /* XXX xp->share = p->share; */
1186 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1188 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1189 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1190 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1191 p->priority = xp->priority;
1192 p->index = xp->index;
1193 p->sel.family = xp->family;
1194 p->dir = dir;
1195 p->action = xp->action;
1196 p->flags = xp->flags;
1197 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1200 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1202 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1203 int err;
1205 if (!xp) {
1206 *errp = -ENOMEM;
1207 return NULL;
1210 copy_from_user_policy(xp, p);
1212 err = copy_from_user_policy_type(&xp->type, attrs);
1213 if (err)
1214 goto error;
1216 if (!(err = copy_from_user_tmpl(xp, attrs)))
1217 err = copy_from_user_sec_ctx(xp, attrs);
1218 if (err)
1219 goto error;
1221 return xp;
1222 error:
1223 *errp = err;
1224 xp->walk.dead = 1;
1225 xfrm_policy_destroy(xp);
1226 return NULL;
1229 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1230 struct nlattr **attrs)
1232 struct net *net = sock_net(skb->sk);
1233 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1234 struct xfrm_policy *xp;
1235 struct km_event c;
1236 int err;
1237 int excl;
1238 uid_t loginuid = NETLINK_CB(skb).loginuid;
1239 u32 sessionid = NETLINK_CB(skb).sessionid;
1240 u32 sid = NETLINK_CB(skb).sid;
1242 err = verify_newpolicy_info(p);
1243 if (err)
1244 return err;
1245 err = verify_sec_ctx_len(attrs);
1246 if (err)
1247 return err;
1249 xp = xfrm_policy_construct(net, p, attrs, &err);
1250 if (!xp)
1251 return err;
1253 /* shouldnt excl be based on nlh flags??
1254 * Aha! this is anti-netlink really i.e more pfkey derived
1255 * in netlink excl is a flag and you wouldnt need
1256 * a type XFRM_MSG_UPDPOLICY - JHS */
1257 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1258 err = xfrm_policy_insert(p->dir, xp, excl);
1259 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1261 if (err) {
1262 security_xfrm_policy_free(xp->security);
1263 kfree(xp);
1264 return err;
1267 c.event = nlh->nlmsg_type;
1268 c.seq = nlh->nlmsg_seq;
1269 c.pid = nlh->nlmsg_pid;
1270 km_policy_notify(xp, p->dir, &c);
1272 xfrm_pol_put(xp);
1274 return 0;
1277 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1279 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1280 int i;
1282 if (xp->xfrm_nr == 0)
1283 return 0;
1285 for (i = 0; i < xp->xfrm_nr; i++) {
1286 struct xfrm_user_tmpl *up = &vec[i];
1287 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1289 memcpy(&up->id, &kp->id, sizeof(up->id));
1290 up->family = kp->encap_family;
1291 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1292 up->reqid = kp->reqid;
1293 up->mode = kp->mode;
1294 up->share = kp->share;
1295 up->optional = kp->optional;
1296 up->aalgos = kp->aalgos;
1297 up->ealgos = kp->ealgos;
1298 up->calgos = kp->calgos;
1301 return nla_put(skb, XFRMA_TMPL,
1302 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1305 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1307 if (x->security) {
1308 return copy_sec_ctx(x->security, skb);
1310 return 0;
1313 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1315 if (xp->security) {
1316 return copy_sec_ctx(xp->security, skb);
1318 return 0;
1320 static inline size_t userpolicy_type_attrsize(void)
1322 #ifdef CONFIG_XFRM_SUB_POLICY
1323 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1324 #else
1325 return 0;
1326 #endif
1329 #ifdef CONFIG_XFRM_SUB_POLICY
1330 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1332 struct xfrm_userpolicy_type upt = {
1333 .type = type,
1336 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1339 #else
1340 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1342 return 0;
1344 #endif
1346 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1348 struct xfrm_dump_info *sp = ptr;
1349 struct xfrm_userpolicy_info *p;
1350 struct sk_buff *in_skb = sp->in_skb;
1351 struct sk_buff *skb = sp->out_skb;
1352 struct nlmsghdr *nlh;
1354 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1355 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1356 if (nlh == NULL)
1357 return -EMSGSIZE;
1359 p = nlmsg_data(nlh);
1360 copy_to_user_policy(xp, p, dir);
1361 if (copy_to_user_tmpl(xp, skb) < 0)
1362 goto nlmsg_failure;
1363 if (copy_to_user_sec_ctx(xp, skb))
1364 goto nlmsg_failure;
1365 if (copy_to_user_policy_type(xp->type, skb) < 0)
1366 goto nlmsg_failure;
1368 nlmsg_end(skb, nlh);
1369 return 0;
1371 nlmsg_failure:
1372 nlmsg_cancel(skb, nlh);
1373 return -EMSGSIZE;
1376 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1378 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1380 xfrm_policy_walk_done(walk);
1381 return 0;
1384 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1386 struct net *net = sock_net(skb->sk);
1387 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1388 struct xfrm_dump_info info;
1390 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1391 sizeof(cb->args) - sizeof(cb->args[0]));
1393 info.in_skb = cb->skb;
1394 info.out_skb = skb;
1395 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1396 info.nlmsg_flags = NLM_F_MULTI;
1398 if (!cb->args[0]) {
1399 cb->args[0] = 1;
1400 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1403 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1405 return skb->len;
1408 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1409 struct xfrm_policy *xp,
1410 int dir, u32 seq)
1412 struct xfrm_dump_info info;
1413 struct sk_buff *skb;
1415 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1416 if (!skb)
1417 return ERR_PTR(-ENOMEM);
1419 info.in_skb = in_skb;
1420 info.out_skb = skb;
1421 info.nlmsg_seq = seq;
1422 info.nlmsg_flags = 0;
1424 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1425 kfree_skb(skb);
1426 return NULL;
1429 return skb;
1432 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1433 struct nlattr **attrs)
1435 struct net *net = sock_net(skb->sk);
1436 struct xfrm_policy *xp;
1437 struct xfrm_userpolicy_id *p;
1438 u8 type = XFRM_POLICY_TYPE_MAIN;
1439 int err;
1440 struct km_event c;
1441 int delete;
1443 p = nlmsg_data(nlh);
1444 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1446 err = copy_from_user_policy_type(&type, attrs);
1447 if (err)
1448 return err;
1450 err = verify_policy_dir(p->dir);
1451 if (err)
1452 return err;
1454 if (p->index)
1455 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
1456 else {
1457 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1458 struct xfrm_sec_ctx *ctx;
1460 err = verify_sec_ctx_len(attrs);
1461 if (err)
1462 return err;
1464 ctx = NULL;
1465 if (rt) {
1466 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1468 err = security_xfrm_policy_alloc(&ctx, uctx);
1469 if (err)
1470 return err;
1472 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
1473 delete, &err);
1474 security_xfrm_policy_free(ctx);
1476 if (xp == NULL)
1477 return -ENOENT;
1479 if (!delete) {
1480 struct sk_buff *resp_skb;
1482 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1483 if (IS_ERR(resp_skb)) {
1484 err = PTR_ERR(resp_skb);
1485 } else {
1486 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1487 NETLINK_CB(skb).pid);
1489 } else {
1490 uid_t loginuid = NETLINK_CB(skb).loginuid;
1491 u32 sessionid = NETLINK_CB(skb).sessionid;
1492 u32 sid = NETLINK_CB(skb).sid;
1494 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1495 sid);
1497 if (err != 0)
1498 goto out;
1500 c.data.byid = p->index;
1501 c.event = nlh->nlmsg_type;
1502 c.seq = nlh->nlmsg_seq;
1503 c.pid = nlh->nlmsg_pid;
1504 km_policy_notify(xp, p->dir, &c);
1507 out:
1508 xfrm_pol_put(xp);
1509 return err;
1512 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1513 struct nlattr **attrs)
1515 struct net *net = sock_net(skb->sk);
1516 struct km_event c;
1517 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1518 struct xfrm_audit audit_info;
1519 int err;
1521 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1522 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1523 audit_info.secid = NETLINK_CB(skb).sid;
1524 err = xfrm_state_flush(net, p->proto, &audit_info);
1525 if (err)
1526 return err;
1527 c.data.proto = p->proto;
1528 c.event = nlh->nlmsg_type;
1529 c.seq = nlh->nlmsg_seq;
1530 c.pid = nlh->nlmsg_pid;
1531 c.net = net;
1532 km_state_notify(NULL, &c);
1534 return 0;
1537 static inline size_t xfrm_aevent_msgsize(void)
1539 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1540 + nla_total_size(sizeof(struct xfrm_replay_state))
1541 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1542 + nla_total_size(4) /* XFRM_AE_RTHR */
1543 + nla_total_size(4); /* XFRM_AE_ETHR */
1546 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1548 struct xfrm_aevent_id *id;
1549 struct nlmsghdr *nlh;
1551 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1552 if (nlh == NULL)
1553 return -EMSGSIZE;
1555 id = nlmsg_data(nlh);
1556 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1557 id->sa_id.spi = x->id.spi;
1558 id->sa_id.family = x->props.family;
1559 id->sa_id.proto = x->id.proto;
1560 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1561 id->reqid = x->props.reqid;
1562 id->flags = c->data.aevent;
1564 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1565 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1567 if (id->flags & XFRM_AE_RTHR)
1568 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1570 if (id->flags & XFRM_AE_ETHR)
1571 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1572 x->replay_maxage * 10 / HZ);
1574 return nlmsg_end(skb, nlh);
1576 nla_put_failure:
1577 nlmsg_cancel(skb, nlh);
1578 return -EMSGSIZE;
1581 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1582 struct nlattr **attrs)
1584 struct net *net = sock_net(skb->sk);
1585 struct xfrm_state *x;
1586 struct sk_buff *r_skb;
1587 int err;
1588 struct km_event c;
1589 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1590 struct xfrm_usersa_id *id = &p->sa_id;
1592 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
1593 if (r_skb == NULL)
1594 return -ENOMEM;
1596 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
1597 if (x == NULL) {
1598 kfree_skb(r_skb);
1599 return -ESRCH;
1603 * XXX: is this lock really needed - none of the other
1604 * gets lock (the concern is things getting updated
1605 * while we are still reading) - jhs
1607 spin_lock_bh(&x->lock);
1608 c.data.aevent = p->flags;
1609 c.seq = nlh->nlmsg_seq;
1610 c.pid = nlh->nlmsg_pid;
1612 if (build_aevent(r_skb, x, &c) < 0)
1613 BUG();
1614 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1615 spin_unlock_bh(&x->lock);
1616 xfrm_state_put(x);
1617 return err;
1620 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1621 struct nlattr **attrs)
1623 struct net *net = sock_net(skb->sk);
1624 struct xfrm_state *x;
1625 struct km_event c;
1626 int err = - EINVAL;
1627 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1628 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1629 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1631 if (!lt && !rp)
1632 return err;
1634 /* pedantic mode - thou shalt sayeth replaceth */
1635 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1636 return err;
1638 x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1639 if (x == NULL)
1640 return -ESRCH;
1642 if (x->km.state != XFRM_STATE_VALID)
1643 goto out;
1645 spin_lock_bh(&x->lock);
1646 xfrm_update_ae_params(x, attrs);
1647 spin_unlock_bh(&x->lock);
1649 c.event = nlh->nlmsg_type;
1650 c.seq = nlh->nlmsg_seq;
1651 c.pid = nlh->nlmsg_pid;
1652 c.data.aevent = XFRM_AE_CU;
1653 km_state_notify(x, &c);
1654 err = 0;
1655 out:
1656 xfrm_state_put(x);
1657 return err;
1660 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1661 struct nlattr **attrs)
1663 struct net *net = sock_net(skb->sk);
1664 struct km_event c;
1665 u8 type = XFRM_POLICY_TYPE_MAIN;
1666 int err;
1667 struct xfrm_audit audit_info;
1669 err = copy_from_user_policy_type(&type, attrs);
1670 if (err)
1671 return err;
1673 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1674 audit_info.sessionid = NETLINK_CB(skb).sessionid;
1675 audit_info.secid = NETLINK_CB(skb).sid;
1676 err = xfrm_policy_flush(net, type, &audit_info);
1677 if (err)
1678 return err;
1679 c.data.type = type;
1680 c.event = nlh->nlmsg_type;
1681 c.seq = nlh->nlmsg_seq;
1682 c.pid = nlh->nlmsg_pid;
1683 c.net = net;
1684 km_policy_notify(NULL, 0, &c);
1685 return 0;
1688 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1689 struct nlattr **attrs)
1691 struct net *net = sock_net(skb->sk);
1692 struct xfrm_policy *xp;
1693 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1694 struct xfrm_userpolicy_info *p = &up->pol;
1695 u8 type = XFRM_POLICY_TYPE_MAIN;
1696 int err = -ENOENT;
1698 err = copy_from_user_policy_type(&type, attrs);
1699 if (err)
1700 return err;
1702 if (p->index)
1703 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
1704 else {
1705 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1706 struct xfrm_sec_ctx *ctx;
1708 err = verify_sec_ctx_len(attrs);
1709 if (err)
1710 return err;
1712 ctx = NULL;
1713 if (rt) {
1714 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1716 err = security_xfrm_policy_alloc(&ctx, uctx);
1717 if (err)
1718 return err;
1720 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
1721 security_xfrm_policy_free(ctx);
1723 if (xp == NULL)
1724 return -ENOENT;
1726 read_lock(&xp->lock);
1727 if (xp->walk.dead) {
1728 read_unlock(&xp->lock);
1729 goto out;
1732 read_unlock(&xp->lock);
1733 err = 0;
1734 if (up->hard) {
1735 uid_t loginuid = NETLINK_CB(skb).loginuid;
1736 uid_t sessionid = NETLINK_CB(skb).sessionid;
1737 u32 sid = NETLINK_CB(skb).sid;
1738 xfrm_policy_delete(xp, p->dir);
1739 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1741 } else {
1742 // reset the timers here?
1743 printk("Dont know what to do with soft policy expire\n");
1745 km_policy_expired(xp, p->dir, up->hard, current->pid);
1747 out:
1748 xfrm_pol_put(xp);
1749 return err;
1752 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1753 struct nlattr **attrs)
1755 struct net *net = sock_net(skb->sk);
1756 struct xfrm_state *x;
1757 int err;
1758 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1759 struct xfrm_usersa_info *p = &ue->state;
1761 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1763 err = -ENOENT;
1764 if (x == NULL)
1765 return err;
1767 spin_lock_bh(&x->lock);
1768 err = -EINVAL;
1769 if (x->km.state != XFRM_STATE_VALID)
1770 goto out;
1771 km_state_expired(x, ue->hard, current->pid);
1773 if (ue->hard) {
1774 uid_t loginuid = NETLINK_CB(skb).loginuid;
1775 uid_t sessionid = NETLINK_CB(skb).sessionid;
1776 u32 sid = NETLINK_CB(skb).sid;
1777 __xfrm_state_delete(x);
1778 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1780 err = 0;
1781 out:
1782 spin_unlock_bh(&x->lock);
1783 xfrm_state_put(x);
1784 return err;
1787 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1788 struct nlattr **attrs)
1790 struct net *net = sock_net(skb->sk);
1791 struct xfrm_policy *xp;
1792 struct xfrm_user_tmpl *ut;
1793 int i;
1794 struct nlattr *rt = attrs[XFRMA_TMPL];
1796 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1797 struct xfrm_state *x = xfrm_state_alloc(net);
1798 int err = -ENOMEM;
1800 if (!x)
1801 goto nomem;
1803 err = verify_newpolicy_info(&ua->policy);
1804 if (err)
1805 goto bad_policy;
1807 /* build an XP */
1808 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1809 if (!xp)
1810 goto free_state;
1812 memcpy(&x->id, &ua->id, sizeof(ua->id));
1813 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1814 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1816 ut = nla_data(rt);
1817 /* extract the templates and for each call km_key */
1818 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1819 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1820 memcpy(&x->id, &t->id, sizeof(x->id));
1821 x->props.mode = t->mode;
1822 x->props.reqid = t->reqid;
1823 x->props.family = ut->family;
1824 t->aalgos = ua->aalgos;
1825 t->ealgos = ua->ealgos;
1826 t->calgos = ua->calgos;
1827 err = km_query(x, t, xp);
1831 kfree(x);
1832 kfree(xp);
1834 return 0;
1836 bad_policy:
1837 printk("BAD policy passed\n");
1838 free_state:
1839 kfree(x);
1840 nomem:
1841 return err;
1844 #ifdef CONFIG_XFRM_MIGRATE
1845 static int copy_from_user_migrate(struct xfrm_migrate *ma,
1846 struct xfrm_kmaddress *k,
1847 struct nlattr **attrs, int *num)
1849 struct nlattr *rt = attrs[XFRMA_MIGRATE];
1850 struct xfrm_user_migrate *um;
1851 int i, num_migrate;
1853 if (k != NULL) {
1854 struct xfrm_user_kmaddress *uk;
1856 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1857 memcpy(&k->local, &uk->local, sizeof(k->local));
1858 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1859 k->family = uk->family;
1860 k->reserved = uk->reserved;
1863 um = nla_data(rt);
1864 num_migrate = nla_len(rt) / sizeof(*um);
1866 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1867 return -EINVAL;
1869 for (i = 0; i < num_migrate; i++, um++, ma++) {
1870 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1871 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1872 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1873 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1875 ma->proto = um->proto;
1876 ma->mode = um->mode;
1877 ma->reqid = um->reqid;
1879 ma->old_family = um->old_family;
1880 ma->new_family = um->new_family;
1883 *num = i;
1884 return 0;
1887 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1888 struct nlattr **attrs)
1890 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
1891 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1892 struct xfrm_kmaddress km, *kmp;
1893 u8 type;
1894 int err;
1895 int n = 0;
1897 if (attrs[XFRMA_MIGRATE] == NULL)
1898 return -EINVAL;
1900 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1902 err = copy_from_user_policy_type(&type, attrs);
1903 if (err)
1904 return err;
1906 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
1907 if (err)
1908 return err;
1910 if (!n)
1911 return 0;
1913 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
1915 return 0;
1917 #else
1918 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
1919 struct nlattr **attrs)
1921 return -ENOPROTOOPT;
1923 #endif
1925 #ifdef CONFIG_XFRM_MIGRATE
1926 static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1928 struct xfrm_user_migrate um;
1930 memset(&um, 0, sizeof(um));
1931 um.proto = m->proto;
1932 um.mode = m->mode;
1933 um.reqid = m->reqid;
1934 um.old_family = m->old_family;
1935 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1936 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1937 um.new_family = m->new_family;
1938 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1939 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1941 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
1944 static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1946 struct xfrm_user_kmaddress uk;
1948 memset(&uk, 0, sizeof(uk));
1949 uk.family = k->family;
1950 uk.reserved = k->reserved;
1951 memcpy(&uk.local, &k->local, sizeof(uk.local));
1952 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
1954 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1957 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
1959 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1960 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1961 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1962 + userpolicy_type_attrsize();
1965 static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1966 int num_migrate, struct xfrm_kmaddress *k,
1967 struct xfrm_selector *sel, u8 dir, u8 type)
1969 struct xfrm_migrate *mp;
1970 struct xfrm_userpolicy_id *pol_id;
1971 struct nlmsghdr *nlh;
1972 int i;
1974 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1975 if (nlh == NULL)
1976 return -EMSGSIZE;
1978 pol_id = nlmsg_data(nlh);
1979 /* copy data from selector, dir, and type to the pol_id */
1980 memset(pol_id, 0, sizeof(*pol_id));
1981 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1982 pol_id->dir = dir;
1984 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1985 goto nlmsg_failure;
1987 if (copy_to_user_policy_type(type, skb) < 0)
1988 goto nlmsg_failure;
1990 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1991 if (copy_to_user_migrate(mp, skb) < 0)
1992 goto nlmsg_failure;
1995 return nlmsg_end(skb, nlh);
1996 nlmsg_failure:
1997 nlmsg_cancel(skb, nlh);
1998 return -EMSGSIZE;
2001 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2002 struct xfrm_migrate *m, int num_migrate,
2003 struct xfrm_kmaddress *k)
2005 struct net *net = &init_net;
2006 struct sk_buff *skb;
2008 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2009 if (skb == NULL)
2010 return -ENOMEM;
2012 /* build migrate */
2013 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2014 BUG();
2016 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2018 #else
2019 static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
2020 struct xfrm_migrate *m, int num_migrate,
2021 struct xfrm_kmaddress *k)
2023 return -ENOPROTOOPT;
2025 #endif
2027 #define XMSGSIZE(type) sizeof(struct type)
2029 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2030 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2031 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2032 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2033 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2034 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2035 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2036 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2037 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2038 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2039 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2040 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2041 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2042 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2043 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2044 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2045 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2046 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2047 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2048 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2049 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2052 #undef XMSGSIZE
2054 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2055 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2056 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2057 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2058 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2059 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2060 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2061 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2062 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2063 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2064 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2065 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2066 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2067 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2068 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2069 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2070 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2073 static struct xfrm_link {
2074 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2075 int (*dump)(struct sk_buff *, struct netlink_callback *);
2076 int (*done)(struct netlink_callback *);
2077 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2078 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2079 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2080 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2081 .dump = xfrm_dump_sa,
2082 .done = xfrm_dump_sa_done },
2083 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2084 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2085 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2086 .dump = xfrm_dump_policy,
2087 .done = xfrm_dump_policy_done },
2088 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2089 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2090 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2091 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2092 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2093 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2094 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2095 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2096 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2097 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2098 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2099 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2100 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2103 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2105 struct net *net = sock_net(skb->sk);
2106 struct nlattr *attrs[XFRMA_MAX+1];
2107 struct xfrm_link *link;
2108 int type, err;
2110 type = nlh->nlmsg_type;
2111 if (type > XFRM_MSG_MAX)
2112 return -EINVAL;
2114 type -= XFRM_MSG_BASE;
2115 link = &xfrm_dispatch[type];
2117 /* All operations require privileges, even GET */
2118 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2119 return -EPERM;
2121 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2122 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2123 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2124 if (link->dump == NULL)
2125 return -EINVAL;
2127 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2130 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2131 xfrma_policy);
2132 if (err < 0)
2133 return err;
2135 if (link->doit == NULL)
2136 return -EINVAL;
2138 return link->doit(skb, nlh, attrs);
2141 static void xfrm_netlink_rcv(struct sk_buff *skb)
2143 mutex_lock(&xfrm_cfg_mutex);
2144 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2145 mutex_unlock(&xfrm_cfg_mutex);
2148 static inline size_t xfrm_expire_msgsize(void)
2150 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2153 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
2155 struct xfrm_user_expire *ue;
2156 struct nlmsghdr *nlh;
2158 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2159 if (nlh == NULL)
2160 return -EMSGSIZE;
2162 ue = nlmsg_data(nlh);
2163 copy_to_user_state(x, &ue->state);
2164 ue->hard = (c->data.hard != 0) ? 1 : 0;
2166 return nlmsg_end(skb, nlh);
2169 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
2171 struct net *net = xs_net(x);
2172 struct sk_buff *skb;
2174 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2175 if (skb == NULL)
2176 return -ENOMEM;
2178 if (build_expire(skb, x, c) < 0)
2179 BUG();
2181 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2184 static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2186 struct net *net = xs_net(x);
2187 struct sk_buff *skb;
2189 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
2190 if (skb == NULL)
2191 return -ENOMEM;
2193 if (build_aevent(skb, x, c) < 0)
2194 BUG();
2196 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2199 static int xfrm_notify_sa_flush(struct km_event *c)
2201 struct net *net = c->net;
2202 struct xfrm_usersa_flush *p;
2203 struct nlmsghdr *nlh;
2204 struct sk_buff *skb;
2205 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2207 skb = nlmsg_new(len, GFP_ATOMIC);
2208 if (skb == NULL)
2209 return -ENOMEM;
2211 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2212 if (nlh == NULL) {
2213 kfree_skb(skb);
2214 return -EMSGSIZE;
2217 p = nlmsg_data(nlh);
2218 p->proto = c->data.proto;
2220 nlmsg_end(skb, nlh);
2222 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2225 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2227 size_t l = 0;
2228 if (x->aead)
2229 l += nla_total_size(aead_len(x->aead));
2230 if (x->aalg) {
2231 l += nla_total_size(sizeof(struct xfrm_algo) +
2232 (x->aalg->alg_key_len + 7) / 8);
2233 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2235 if (x->ealg)
2236 l += nla_total_size(xfrm_alg_len(x->ealg));
2237 if (x->calg)
2238 l += nla_total_size(sizeof(*x->calg));
2239 if (x->encap)
2240 l += nla_total_size(sizeof(*x->encap));
2241 if (x->security)
2242 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2243 x->security->ctx_len);
2244 if (x->coaddr)
2245 l += nla_total_size(sizeof(*x->coaddr));
2247 /* Must count x->lastused as it may become non-zero behind our back. */
2248 l += nla_total_size(sizeof(u64));
2250 return l;
2253 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2255 struct net *net = xs_net(x);
2256 struct xfrm_usersa_info *p;
2257 struct xfrm_usersa_id *id;
2258 struct nlmsghdr *nlh;
2259 struct sk_buff *skb;
2260 int len = xfrm_sa_len(x);
2261 int headlen;
2263 headlen = sizeof(*p);
2264 if (c->event == XFRM_MSG_DELSA) {
2265 len += nla_total_size(headlen);
2266 headlen = sizeof(*id);
2268 len += NLMSG_ALIGN(headlen);
2270 skb = nlmsg_new(len, GFP_ATOMIC);
2271 if (skb == NULL)
2272 return -ENOMEM;
2274 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2275 if (nlh == NULL)
2276 goto nla_put_failure;
2278 p = nlmsg_data(nlh);
2279 if (c->event == XFRM_MSG_DELSA) {
2280 struct nlattr *attr;
2282 id = nlmsg_data(nlh);
2283 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2284 id->spi = x->id.spi;
2285 id->family = x->props.family;
2286 id->proto = x->id.proto;
2288 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2289 if (attr == NULL)
2290 goto nla_put_failure;
2292 p = nla_data(attr);
2295 if (copy_to_user_state_extra(x, p, skb))
2296 goto nla_put_failure;
2298 nlmsg_end(skb, nlh);
2300 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2302 nla_put_failure:
2303 /* Somebody screwed up with xfrm_sa_len! */
2304 WARN_ON(1);
2305 kfree_skb(skb);
2306 return -1;
2309 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2312 switch (c->event) {
2313 case XFRM_MSG_EXPIRE:
2314 return xfrm_exp_state_notify(x, c);
2315 case XFRM_MSG_NEWAE:
2316 return xfrm_aevent_state_notify(x, c);
2317 case XFRM_MSG_DELSA:
2318 case XFRM_MSG_UPDSA:
2319 case XFRM_MSG_NEWSA:
2320 return xfrm_notify_sa(x, c);
2321 case XFRM_MSG_FLUSHSA:
2322 return xfrm_notify_sa_flush(c);
2323 default:
2324 printk("xfrm_user: Unknown SA event %d\n", c->event);
2325 break;
2328 return 0;
2332 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2333 struct xfrm_policy *xp)
2335 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2336 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2337 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2338 + userpolicy_type_attrsize();
2341 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2342 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2343 int dir)
2345 struct xfrm_user_acquire *ua;
2346 struct nlmsghdr *nlh;
2347 __u32 seq = xfrm_get_acqseq();
2349 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2350 if (nlh == NULL)
2351 return -EMSGSIZE;
2353 ua = nlmsg_data(nlh);
2354 memcpy(&ua->id, &x->id, sizeof(ua->id));
2355 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2356 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2357 copy_to_user_policy(xp, &ua->policy, dir);
2358 ua->aalgos = xt->aalgos;
2359 ua->ealgos = xt->ealgos;
2360 ua->calgos = xt->calgos;
2361 ua->seq = x->km.seq = seq;
2363 if (copy_to_user_tmpl(xp, skb) < 0)
2364 goto nlmsg_failure;
2365 if (copy_to_user_state_sec_ctx(x, skb))
2366 goto nlmsg_failure;
2367 if (copy_to_user_policy_type(xp->type, skb) < 0)
2368 goto nlmsg_failure;
2370 return nlmsg_end(skb, nlh);
2372 nlmsg_failure:
2373 nlmsg_cancel(skb, nlh);
2374 return -EMSGSIZE;
2377 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2378 struct xfrm_policy *xp, int dir)
2380 struct net *net = xs_net(x);
2381 struct sk_buff *skb;
2383 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2384 if (skb == NULL)
2385 return -ENOMEM;
2387 if (build_acquire(skb, x, xt, xp, dir) < 0)
2388 BUG();
2390 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2393 /* User gives us xfrm_user_policy_info followed by an array of 0
2394 * or more templates.
2396 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2397 u8 *data, int len, int *dir)
2399 struct net *net = sock_net(sk);
2400 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2401 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2402 struct xfrm_policy *xp;
2403 int nr;
2405 switch (sk->sk_family) {
2406 case AF_INET:
2407 if (opt != IP_XFRM_POLICY) {
2408 *dir = -EOPNOTSUPP;
2409 return NULL;
2411 break;
2412 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2413 case AF_INET6:
2414 if (opt != IPV6_XFRM_POLICY) {
2415 *dir = -EOPNOTSUPP;
2416 return NULL;
2418 break;
2419 #endif
2420 default:
2421 *dir = -EINVAL;
2422 return NULL;
2425 *dir = -EINVAL;
2427 if (len < sizeof(*p) ||
2428 verify_newpolicy_info(p))
2429 return NULL;
2431 nr = ((len - sizeof(*p)) / sizeof(*ut));
2432 if (validate_tmpl(nr, ut, p->sel.family))
2433 return NULL;
2435 if (p->dir > XFRM_POLICY_OUT)
2436 return NULL;
2438 xp = xfrm_policy_alloc(net, GFP_KERNEL);
2439 if (xp == NULL) {
2440 *dir = -ENOBUFS;
2441 return NULL;
2444 copy_from_user_policy(xp, p);
2445 xp->type = XFRM_POLICY_TYPE_MAIN;
2446 copy_templates(xp, ut, nr);
2448 *dir = p->dir;
2450 return xp;
2453 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2455 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2456 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2457 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2458 + userpolicy_type_attrsize();
2461 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2462 int dir, struct km_event *c)
2464 struct xfrm_user_polexpire *upe;
2465 struct nlmsghdr *nlh;
2466 int hard = c->data.hard;
2468 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2469 if (nlh == NULL)
2470 return -EMSGSIZE;
2472 upe = nlmsg_data(nlh);
2473 copy_to_user_policy(xp, &upe->pol, dir);
2474 if (copy_to_user_tmpl(xp, skb) < 0)
2475 goto nlmsg_failure;
2476 if (copy_to_user_sec_ctx(xp, skb))
2477 goto nlmsg_failure;
2478 if (copy_to_user_policy_type(xp->type, skb) < 0)
2479 goto nlmsg_failure;
2480 upe->hard = !!hard;
2482 return nlmsg_end(skb, nlh);
2484 nlmsg_failure:
2485 nlmsg_cancel(skb, nlh);
2486 return -EMSGSIZE;
2489 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2491 struct net *net = xp_net(xp);
2492 struct sk_buff *skb;
2494 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2495 if (skb == NULL)
2496 return -ENOMEM;
2498 if (build_polexpire(skb, xp, dir, c) < 0)
2499 BUG();
2501 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2504 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2506 struct net *net = xp_net(xp);
2507 struct xfrm_userpolicy_info *p;
2508 struct xfrm_userpolicy_id *id;
2509 struct nlmsghdr *nlh;
2510 struct sk_buff *skb;
2511 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2512 int headlen;
2514 headlen = sizeof(*p);
2515 if (c->event == XFRM_MSG_DELPOLICY) {
2516 len += nla_total_size(headlen);
2517 headlen = sizeof(*id);
2519 len += userpolicy_type_attrsize();
2520 len += NLMSG_ALIGN(headlen);
2522 skb = nlmsg_new(len, GFP_ATOMIC);
2523 if (skb == NULL)
2524 return -ENOMEM;
2526 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2527 if (nlh == NULL)
2528 goto nlmsg_failure;
2530 p = nlmsg_data(nlh);
2531 if (c->event == XFRM_MSG_DELPOLICY) {
2532 struct nlattr *attr;
2534 id = nlmsg_data(nlh);
2535 memset(id, 0, sizeof(*id));
2536 id->dir = dir;
2537 if (c->data.byid)
2538 id->index = xp->index;
2539 else
2540 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2542 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2543 if (attr == NULL)
2544 goto nlmsg_failure;
2546 p = nla_data(attr);
2549 copy_to_user_policy(xp, p, dir);
2550 if (copy_to_user_tmpl(xp, skb) < 0)
2551 goto nlmsg_failure;
2552 if (copy_to_user_policy_type(xp->type, skb) < 0)
2553 goto nlmsg_failure;
2555 nlmsg_end(skb, nlh);
2557 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2559 nlmsg_failure:
2560 kfree_skb(skb);
2561 return -1;
2564 static int xfrm_notify_policy_flush(struct km_event *c)
2566 struct net *net = c->net;
2567 struct nlmsghdr *nlh;
2568 struct sk_buff *skb;
2570 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2571 if (skb == NULL)
2572 return -ENOMEM;
2574 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2575 if (nlh == NULL)
2576 goto nlmsg_failure;
2577 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2578 goto nlmsg_failure;
2580 nlmsg_end(skb, nlh);
2582 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2584 nlmsg_failure:
2585 kfree_skb(skb);
2586 return -1;
2589 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2592 switch (c->event) {
2593 case XFRM_MSG_NEWPOLICY:
2594 case XFRM_MSG_UPDPOLICY:
2595 case XFRM_MSG_DELPOLICY:
2596 return xfrm_notify_policy(xp, dir, c);
2597 case XFRM_MSG_FLUSHPOLICY:
2598 return xfrm_notify_policy_flush(c);
2599 case XFRM_MSG_POLEXPIRE:
2600 return xfrm_exp_policy_notify(xp, dir, c);
2601 default:
2602 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2605 return 0;
2609 static inline size_t xfrm_report_msgsize(void)
2611 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2614 static int build_report(struct sk_buff *skb, u8 proto,
2615 struct xfrm_selector *sel, xfrm_address_t *addr)
2617 struct xfrm_user_report *ur;
2618 struct nlmsghdr *nlh;
2620 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2621 if (nlh == NULL)
2622 return -EMSGSIZE;
2624 ur = nlmsg_data(nlh);
2625 ur->proto = proto;
2626 memcpy(&ur->sel, sel, sizeof(ur->sel));
2628 if (addr)
2629 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2631 return nlmsg_end(skb, nlh);
2633 nla_put_failure:
2634 nlmsg_cancel(skb, nlh);
2635 return -EMSGSIZE;
2638 static int xfrm_send_report(struct net *net, u8 proto,
2639 struct xfrm_selector *sel, xfrm_address_t *addr)
2641 struct sk_buff *skb;
2643 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2644 if (skb == NULL)
2645 return -ENOMEM;
2647 if (build_report(skb, proto, sel, addr) < 0)
2648 BUG();
2650 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2653 static inline size_t xfrm_mapping_msgsize(void)
2655 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2658 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2659 xfrm_address_t *new_saddr, __be16 new_sport)
2661 struct xfrm_user_mapping *um;
2662 struct nlmsghdr *nlh;
2664 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2665 if (nlh == NULL)
2666 return -EMSGSIZE;
2668 um = nlmsg_data(nlh);
2670 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2671 um->id.spi = x->id.spi;
2672 um->id.family = x->props.family;
2673 um->id.proto = x->id.proto;
2674 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2675 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2676 um->new_sport = new_sport;
2677 um->old_sport = x->encap->encap_sport;
2678 um->reqid = x->props.reqid;
2680 return nlmsg_end(skb, nlh);
2683 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2684 __be16 sport)
2686 struct net *net = xs_net(x);
2687 struct sk_buff *skb;
2689 if (x->id.proto != IPPROTO_ESP)
2690 return -EINVAL;
2692 if (!x->encap)
2693 return -EINVAL;
2695 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2696 if (skb == NULL)
2697 return -ENOMEM;
2699 if (build_mapping(skb, x, ipaddr, sport) < 0)
2700 BUG();
2702 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2705 static struct xfrm_mgr netlink_mgr = {
2706 .id = "netlink",
2707 .notify = xfrm_send_state_notify,
2708 .acquire = xfrm_send_acquire,
2709 .compile_policy = xfrm_compile_policy,
2710 .notify_policy = xfrm_send_policy_notify,
2711 .report = xfrm_send_report,
2712 .migrate = xfrm_send_migrate,
2713 .new_mapping = xfrm_send_mapping,
2716 static int __net_init xfrm_user_net_init(struct net *net)
2718 struct sock *nlsk;
2720 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2721 xfrm_netlink_rcv, NULL, THIS_MODULE);
2722 if (nlsk == NULL)
2723 return -ENOMEM;
2724 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2725 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2726 return 0;
2729 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2731 struct net *net;
2732 list_for_each_entry(net, net_exit_list, exit_list)
2733 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2734 synchronize_net();
2735 list_for_each_entry(net, net_exit_list, exit_list)
2736 netlink_kernel_release(net->xfrm.nlsk_stash);
2739 static struct pernet_operations xfrm_user_net_ops = {
2740 .init = xfrm_user_net_init,
2741 .exit_batch = xfrm_user_net_exit,
2744 static int __init xfrm_user_init(void)
2746 int rv;
2748 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2750 rv = register_pernet_subsys(&xfrm_user_net_ops);
2751 if (rv < 0)
2752 return rv;
2753 rv = xfrm_register_km(&netlink_mgr);
2754 if (rv < 0)
2755 unregister_pernet_subsys(&xfrm_user_net_ops);
2756 return rv;
2759 static void __exit xfrm_user_exit(void)
2761 xfrm_unregister_km(&netlink_mgr);
2762 unregister_pernet_subsys(&xfrm_user_net_ops);
2765 module_init(xfrm_user_init);
2766 module_exit(xfrm_user_exit);
2767 MODULE_LICENSE("GPL");
2768 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);