1 /* xfrm_user.c: User interface to configure xfrm engine.
7 * Kazunori MIYAZAWA @USAGI
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>
28 #include <net/netlink.h>
30 #include <linux/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
34 #include <asm/unaligned.h>
36 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
38 struct nlattr *rt = attrs[type];
39 struct xfrm_algo *algp;
45 if (nla_len(rt) < (int)xfrm_alg_len(algp))
58 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
62 static int verify_auth_trunc(struct nlattr **attrs)
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
71 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
74 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
78 static int verify_aead(struct nlattr **attrs)
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
87 if (nla_len(rt) < (int)aead_len(algp))
90 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
94 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
97 struct nlattr *rt = attrs[type];
100 *addrp = nla_data(rt);
103 static inline int verify_sec_ctx_len(struct nlattr **attrs)
105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106 struct xfrm_user_sec_ctx *uctx;
112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
118 static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122 struct xfrm_replay_state_esn *rs;
125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
140 if (p->replay_window != 0)
146 static int verify_newsa_info(struct xfrm_usersa_info *p,
147 struct nlattr **attrs)
157 #if IS_ENABLED(CONFIG_IPV6)
169 switch (p->id.proto) {
171 if ((!attrs[XFRMA_ALG_AUTH] &&
172 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
173 attrs[XFRMA_ALG_AEAD] ||
174 attrs[XFRMA_ALG_CRYPT] ||
175 attrs[XFRMA_ALG_COMP] ||
181 if (attrs[XFRMA_ALG_COMP])
183 if (!attrs[XFRMA_ALG_AUTH] &&
184 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
185 !attrs[XFRMA_ALG_CRYPT] &&
186 !attrs[XFRMA_ALG_AEAD])
188 if ((attrs[XFRMA_ALG_AUTH] ||
189 attrs[XFRMA_ALG_AUTH_TRUNC] ||
190 attrs[XFRMA_ALG_CRYPT]) &&
191 attrs[XFRMA_ALG_AEAD])
193 if (attrs[XFRMA_TFCPAD] &&
194 p->mode != XFRM_MODE_TUNNEL)
199 if (!attrs[XFRMA_ALG_COMP] ||
200 attrs[XFRMA_ALG_AEAD] ||
201 attrs[XFRMA_ALG_AUTH] ||
202 attrs[XFRMA_ALG_AUTH_TRUNC] ||
203 attrs[XFRMA_ALG_CRYPT] ||
204 attrs[XFRMA_TFCPAD] ||
205 (ntohl(p->id.spi) >= 0x10000))
209 #if IS_ENABLED(CONFIG_IPV6)
210 case IPPROTO_DSTOPTS:
211 case IPPROTO_ROUTING:
212 if (attrs[XFRMA_ALG_COMP] ||
213 attrs[XFRMA_ALG_AUTH] ||
214 attrs[XFRMA_ALG_AUTH_TRUNC] ||
215 attrs[XFRMA_ALG_AEAD] ||
216 attrs[XFRMA_ALG_CRYPT] ||
217 attrs[XFRMA_ENCAP] ||
218 attrs[XFRMA_SEC_CTX] ||
219 attrs[XFRMA_TFCPAD] ||
220 !attrs[XFRMA_COADDR])
229 if ((err = verify_aead(attrs)))
231 if ((err = verify_auth_trunc(attrs)))
233 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
235 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
239 if ((err = verify_sec_ctx_len(attrs)))
241 if ((err = verify_replay(p, attrs)))
246 case XFRM_MODE_TRANSPORT:
247 case XFRM_MODE_TUNNEL:
248 case XFRM_MODE_ROUTEOPTIMIZATION:
262 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
263 struct xfrm_algo_desc *(*get_byname)(const char *, int),
266 struct xfrm_algo *p, *ualg;
267 struct xfrm_algo_desc *algo;
272 ualg = nla_data(rta);
274 algo = get_byname(ualg->alg_name, 1);
277 *props = algo->desc.sadb_alg_id;
279 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
283 strcpy(p->alg_name, algo->name);
288 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
290 struct xfrm_algo *p, *ualg;
291 struct xfrm_algo_desc *algo;
296 ualg = nla_data(rta);
298 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
301 x->props.ealgo = algo->desc.sadb_alg_id;
303 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
307 strcpy(p->alg_name, algo->name);
309 x->geniv = algo->uinfo.encr.geniv;
313 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
316 struct xfrm_algo *ualg;
317 struct xfrm_algo_auth *p;
318 struct xfrm_algo_desc *algo;
323 ualg = nla_data(rta);
325 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
328 *props = algo->desc.sadb_alg_id;
330 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
334 strcpy(p->alg_name, algo->name);
335 p->alg_key_len = ualg->alg_key_len;
336 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
337 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
343 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
346 struct xfrm_algo_auth *p, *ualg;
347 struct xfrm_algo_desc *algo;
352 ualg = nla_data(rta);
354 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
357 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
359 *props = algo->desc.sadb_alg_id;
361 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
365 strcpy(p->alg_name, algo->name);
366 if (!p->alg_trunc_len)
367 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
373 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
375 struct xfrm_algo_aead *p, *ualg;
376 struct xfrm_algo_desc *algo;
381 ualg = nla_data(rta);
383 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
386 x->props.ealgo = algo->desc.sadb_alg_id;
388 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
392 strcpy(p->alg_name, algo->name);
394 x->geniv = algo->uinfo.aead.geniv;
398 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
401 struct xfrm_replay_state_esn *up;
404 if (!replay_esn || !rp)
408 ulen = xfrm_replay_state_esn_len(up);
410 /* Check the overall length and the internal bitmap length to avoid
411 * potential overflow. */
412 if (nla_len(rp) < (int)ulen ||
413 xfrm_replay_state_esn_len(replay_esn) != ulen ||
414 replay_esn->bmp_len != up->bmp_len)
417 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
423 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
424 struct xfrm_replay_state_esn **preplay_esn,
427 struct xfrm_replay_state_esn *p, *pp, *up;
428 unsigned int klen, ulen;
434 klen = xfrm_replay_state_esn_len(up);
435 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
437 p = kzalloc(klen, GFP_KERNEL);
441 pp = kzalloc(klen, GFP_KERNEL);
448 memcpy(pp, up, ulen);
456 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
458 unsigned int len = 0;
461 len += sizeof(struct xfrm_user_sec_ctx);
462 len += xfrm_ctx->ctx_len;
467 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
469 memcpy(&x->id, &p->id, sizeof(x->id));
470 memcpy(&x->sel, &p->sel, sizeof(x->sel));
471 memcpy(&x->lft, &p->lft, sizeof(x->lft));
472 x->props.mode = p->mode;
473 x->props.replay_window = min_t(unsigned int, p->replay_window,
474 sizeof(x->replay.bitmap) * 8);
475 x->props.reqid = p->reqid;
476 x->props.family = p->family;
477 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
478 x->props.flags = p->flags;
480 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
481 x->sel.family = p->family;
485 * someday when pfkey also has support, we could have the code
486 * somehow made shareable and move it to xfrm_state.c - JHS
489 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
492 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
493 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
494 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
495 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
496 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
499 struct xfrm_replay_state_esn *replay_esn;
500 replay_esn = nla_data(re);
501 memcpy(x->replay_esn, replay_esn,
502 xfrm_replay_state_esn_len(replay_esn));
503 memcpy(x->preplay_esn, replay_esn,
504 xfrm_replay_state_esn_len(replay_esn));
508 struct xfrm_replay_state *replay;
509 replay = nla_data(rp);
510 memcpy(&x->replay, replay, sizeof(*replay));
511 memcpy(&x->preplay, replay, sizeof(*replay));
515 struct xfrm_lifetime_cur *ltime;
516 ltime = nla_data(lt);
517 x->curlft.bytes = ltime->bytes;
518 x->curlft.packets = ltime->packets;
519 x->curlft.add_time = ltime->add_time;
520 x->curlft.use_time = ltime->use_time;
524 x->replay_maxage = nla_get_u32(et);
527 x->replay_maxdiff = nla_get_u32(rt);
530 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
532 if (attrs[XFRMA_SET_MARK]) {
533 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
534 if (attrs[XFRMA_SET_MARK_MASK])
535 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
543 static struct xfrm_state *xfrm_state_construct(struct net *net,
544 struct xfrm_usersa_info *p,
545 struct nlattr **attrs,
548 struct xfrm_state *x = xfrm_state_alloc(net);
554 copy_from_user_state(x, p);
556 if (attrs[XFRMA_SA_EXTRA_FLAGS])
557 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
559 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
561 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
562 attrs[XFRMA_ALG_AUTH_TRUNC])))
564 if (!x->props.aalgo) {
565 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
566 attrs[XFRMA_ALG_AUTH])))
569 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
571 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
572 xfrm_calg_get_byname,
573 attrs[XFRMA_ALG_COMP])))
576 if (attrs[XFRMA_ENCAP]) {
577 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
578 sizeof(*x->encap), GFP_KERNEL);
579 if (x->encap == NULL)
583 if (attrs[XFRMA_TFCPAD])
584 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
586 if (attrs[XFRMA_COADDR]) {
587 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
588 sizeof(*x->coaddr), GFP_KERNEL);
589 if (x->coaddr == NULL)
593 xfrm_mark_get(attrs, &x->mark);
595 xfrm_smark_init(attrs, &x->props.smark);
597 if (attrs[XFRMA_IF_ID])
598 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
600 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
604 if (attrs[XFRMA_SEC_CTX]) {
605 err = security_xfrm_state_alloc(x,
606 nla_data(attrs[XFRMA_SEC_CTX]));
611 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
612 attrs[XFRMA_REPLAY_ESN_VAL])))
616 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
617 /* sysctl_xfrm_aevent_etime is in 100ms units */
618 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
620 if ((err = xfrm_init_replay(x)))
623 /* override default values from above */
624 xfrm_update_ae_params(x, attrs, 0);
626 /* configure the hardware if offload is requested */
627 if (attrs[XFRMA_OFFLOAD_DEV]) {
628 err = xfrm_dev_state_add(net, x,
629 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
637 x->km.state = XFRM_STATE_DEAD;
644 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
645 struct nlattr **attrs)
647 struct net *net = sock_net(skb->sk);
648 struct xfrm_usersa_info *p = nlmsg_data(nlh);
649 struct xfrm_state *x;
653 err = verify_newsa_info(p, attrs);
657 x = xfrm_state_construct(net, p, attrs, &err);
662 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
663 err = xfrm_state_add(x);
665 err = xfrm_state_update(x);
667 xfrm_audit_state_add(x, err ? 0 : 1, true);
670 x->km.state = XFRM_STATE_DEAD;
671 xfrm_dev_state_delete(x);
676 if (x->km.state == XFRM_STATE_VOID)
677 x->km.state = XFRM_STATE_VALID;
679 c.seq = nlh->nlmsg_seq;
680 c.portid = nlh->nlmsg_pid;
681 c.event = nlh->nlmsg_type;
683 km_state_notify(x, &c);
689 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
690 struct xfrm_usersa_id *p,
691 struct nlattr **attrs,
694 struct xfrm_state *x = NULL;
697 u32 mark = xfrm_mark_get(attrs, &m);
699 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
701 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
703 xfrm_address_t *saddr = NULL;
705 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
712 x = xfrm_state_lookup_byaddr(net, mark,
714 p->proto, p->family);
723 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
724 struct nlattr **attrs)
726 struct net *net = sock_net(skb->sk);
727 struct xfrm_state *x;
730 struct xfrm_usersa_id *p = nlmsg_data(nlh);
732 x = xfrm_user_state_lookup(net, p, attrs, &err);
736 if ((err = security_xfrm_state_delete(x)) != 0)
739 if (xfrm_state_kern(x)) {
744 err = xfrm_state_delete(x);
749 c.seq = nlh->nlmsg_seq;
750 c.portid = nlh->nlmsg_pid;
751 c.event = nlh->nlmsg_type;
752 km_state_notify(x, &c);
755 xfrm_audit_state_delete(x, err ? 0 : 1, true);
760 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
762 memset(p, 0, sizeof(*p));
763 memcpy(&p->id, &x->id, sizeof(p->id));
764 memcpy(&p->sel, &x->sel, sizeof(p->sel));
765 memcpy(&p->lft, &x->lft, sizeof(p->lft));
766 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
767 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
768 put_unaligned(x->stats.replay, &p->stats.replay);
769 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
770 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
771 p->mode = x->props.mode;
772 p->replay_window = x->props.replay_window;
773 p->reqid = x->props.reqid;
774 p->family = x->props.family;
775 p->flags = x->props.flags;
779 struct xfrm_dump_info {
780 struct sk_buff *in_skb;
781 struct sk_buff *out_skb;
786 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
788 struct xfrm_user_sec_ctx *uctx;
790 int ctx_size = sizeof(*uctx) + s->ctx_len;
792 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
796 uctx = nla_data(attr);
797 uctx->exttype = XFRMA_SEC_CTX;
798 uctx->len = ctx_size;
799 uctx->ctx_doi = s->ctx_doi;
800 uctx->ctx_alg = s->ctx_alg;
801 uctx->ctx_len = s->ctx_len;
802 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
807 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
809 struct xfrm_user_offload *xuo;
812 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
816 xuo = nla_data(attr);
817 memset(xuo, 0, sizeof(*xuo));
818 xuo->ifindex = xso->dev->ifindex;
819 xuo->flags = xso->flags;
824 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
826 struct xfrm_algo *algo;
829 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
830 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
834 algo = nla_data(nla);
835 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
836 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
837 algo->alg_key_len = auth->alg_key_len;
842 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
847 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
849 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
854 /* Don't change this without updating xfrm_sa_len! */
855 static int copy_to_user_state_extra(struct xfrm_state *x,
856 struct xfrm_usersa_info *p,
861 copy_to_user_state(x, p);
863 if (x->props.extra_flags) {
864 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
865 x->props.extra_flags);
871 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
876 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
882 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
887 ret = copy_to_user_auth(x->aalg, skb);
889 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
890 xfrm_alg_auth_len(x->aalg), x->aalg);
895 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
900 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
905 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
910 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
914 ret = xfrm_mark_put(skb, &x->mark);
918 ret = xfrm_smark_put(skb, &x->props.smark);
923 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
924 xfrm_replay_state_esn_len(x->replay_esn),
927 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
932 ret = copy_user_offload(&x->xso, skb);
936 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
941 ret = copy_sec_ctx(x->security, skb);
946 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
948 struct xfrm_dump_info *sp = ptr;
949 struct sk_buff *in_skb = sp->in_skb;
950 struct sk_buff *skb = sp->out_skb;
951 struct xfrm_usersa_info *p;
952 struct nlmsghdr *nlh;
955 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
956 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
962 err = copy_to_user_state_extra(x, p, skb);
964 nlmsg_cancel(skb, nlh);
971 static int xfrm_dump_sa_done(struct netlink_callback *cb)
973 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
974 struct sock *sk = cb->skb->sk;
975 struct net *net = sock_net(sk);
978 xfrm_state_walk_done(walk, net);
982 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
983 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
985 struct net *net = sock_net(skb->sk);
986 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
987 struct xfrm_dump_info info;
989 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
990 sizeof(cb->args) - sizeof(cb->args[0]));
992 info.in_skb = cb->skb;
994 info.nlmsg_seq = cb->nlh->nlmsg_seq;
995 info.nlmsg_flags = NLM_F_MULTI;
998 struct nlattr *attrs[XFRMA_MAX+1];
999 struct xfrm_address_filter *filter = NULL;
1003 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
1008 if (attrs[XFRMA_ADDRESS_FILTER]) {
1009 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1010 sizeof(*filter), GFP_KERNEL);
1015 if (attrs[XFRMA_PROTO])
1016 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1018 xfrm_state_walk_init(walk, proto, filter);
1022 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1027 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1028 struct xfrm_state *x, u32 seq)
1030 struct xfrm_dump_info info;
1031 struct sk_buff *skb;
1034 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1036 return ERR_PTR(-ENOMEM);
1038 info.in_skb = in_skb;
1040 info.nlmsg_seq = seq;
1041 info.nlmsg_flags = 0;
1043 err = dump_one_state(x, 0, &info);
1046 return ERR_PTR(err);
1052 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1053 * Must be called with RCU read lock.
1055 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1056 u32 pid, unsigned int group)
1058 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1065 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1068 static inline unsigned int xfrm_spdinfo_msgsize(void)
1070 return NLMSG_ALIGN(4)
1071 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1072 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1073 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1074 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1077 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1078 u32 portid, u32 seq, u32 flags)
1080 struct xfrmk_spdinfo si;
1081 struct xfrmu_spdinfo spc;
1082 struct xfrmu_spdhinfo sph;
1083 struct xfrmu_spdhthresh spt4, spt6;
1084 struct nlmsghdr *nlh;
1089 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1090 if (nlh == NULL) /* shouldn't really happen ... */
1093 f = nlmsg_data(nlh);
1095 xfrm_spd_getinfo(net, &si);
1096 spc.incnt = si.incnt;
1097 spc.outcnt = si.outcnt;
1098 spc.fwdcnt = si.fwdcnt;
1099 spc.inscnt = si.inscnt;
1100 spc.outscnt = si.outscnt;
1101 spc.fwdscnt = si.fwdscnt;
1102 sph.spdhcnt = si.spdhcnt;
1103 sph.spdhmcnt = si.spdhmcnt;
1106 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1108 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1109 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1110 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1111 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1112 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1114 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1116 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1118 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1120 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1122 nlmsg_cancel(skb, nlh);
1126 nlmsg_end(skb, nlh);
1130 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1131 struct nlattr **attrs)
1133 struct net *net = sock_net(skb->sk);
1134 struct xfrmu_spdhthresh *thresh4 = NULL;
1135 struct xfrmu_spdhthresh *thresh6 = NULL;
1137 /* selector prefixlen thresholds to hash policies */
1138 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1139 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1141 if (nla_len(rta) < sizeof(*thresh4))
1143 thresh4 = nla_data(rta);
1144 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1147 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1148 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1150 if (nla_len(rta) < sizeof(*thresh6))
1152 thresh6 = nla_data(rta);
1153 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1157 if (thresh4 || thresh6) {
1158 write_seqlock(&net->xfrm.policy_hthresh.lock);
1160 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1161 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1164 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1165 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1167 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1169 xfrm_policy_hash_rebuild(net);
1175 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1176 struct nlattr **attrs)
1178 struct net *net = sock_net(skb->sk);
1179 struct sk_buff *r_skb;
1180 u32 *flags = nlmsg_data(nlh);
1181 u32 sportid = NETLINK_CB(skb).portid;
1182 u32 seq = nlh->nlmsg_seq;
1185 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1189 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1192 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1195 static inline unsigned int xfrm_sadinfo_msgsize(void)
1197 return NLMSG_ALIGN(4)
1198 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1199 + nla_total_size(4); /* XFRMA_SAD_CNT */
1202 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1203 u32 portid, u32 seq, u32 flags)
1205 struct xfrmk_sadinfo si;
1206 struct xfrmu_sadhinfo sh;
1207 struct nlmsghdr *nlh;
1211 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1212 if (nlh == NULL) /* shouldn't really happen ... */
1215 f = nlmsg_data(nlh);
1217 xfrm_sad_getinfo(net, &si);
1219 sh.sadhmcnt = si.sadhmcnt;
1220 sh.sadhcnt = si.sadhcnt;
1222 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1224 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1226 nlmsg_cancel(skb, nlh);
1230 nlmsg_end(skb, nlh);
1234 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1235 struct nlattr **attrs)
1237 struct net *net = sock_net(skb->sk);
1238 struct sk_buff *r_skb;
1239 u32 *flags = nlmsg_data(nlh);
1240 u32 sportid = NETLINK_CB(skb).portid;
1241 u32 seq = nlh->nlmsg_seq;
1244 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1248 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1251 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1254 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1255 struct nlattr **attrs)
1257 struct net *net = sock_net(skb->sk);
1258 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1259 struct xfrm_state *x;
1260 struct sk_buff *resp_skb;
1263 x = xfrm_user_state_lookup(net, p, attrs, &err);
1267 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1268 if (IS_ERR(resp_skb)) {
1269 err = PTR_ERR(resp_skb);
1271 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1278 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1279 struct nlattr **attrs)
1281 struct net *net = sock_net(skb->sk);
1282 struct xfrm_state *x;
1283 struct xfrm_userspi_info *p;
1284 struct sk_buff *resp_skb;
1285 xfrm_address_t *daddr;
1292 p = nlmsg_data(nlh);
1293 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1297 family = p->info.family;
1298 daddr = &p->info.id.daddr;
1302 mark = xfrm_mark_get(attrs, &m);
1304 if (attrs[XFRMA_IF_ID])
1305 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1308 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1309 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1316 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1317 if_id, p->info.id.proto, daddr,
1324 err = xfrm_alloc_spi(x, p->min, p->max);
1328 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1329 if (IS_ERR(resp_skb)) {
1330 err = PTR_ERR(resp_skb);
1334 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1342 static int verify_policy_dir(u8 dir)
1345 case XFRM_POLICY_IN:
1346 case XFRM_POLICY_OUT:
1347 case XFRM_POLICY_FWD:
1357 static int verify_policy_type(u8 type)
1360 case XFRM_POLICY_TYPE_MAIN:
1361 #ifdef CONFIG_XFRM_SUB_POLICY
1362 case XFRM_POLICY_TYPE_SUB:
1373 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1378 case XFRM_SHARE_ANY:
1379 case XFRM_SHARE_SESSION:
1380 case XFRM_SHARE_USER:
1381 case XFRM_SHARE_UNIQUE:
1388 switch (p->action) {
1389 case XFRM_POLICY_ALLOW:
1390 case XFRM_POLICY_BLOCK:
1397 switch (p->sel.family) {
1402 #if IS_ENABLED(CONFIG_IPV6)
1405 return -EAFNOSUPPORT;
1412 ret = verify_policy_dir(p->dir);
1415 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1421 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1423 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1424 struct xfrm_user_sec_ctx *uctx;
1429 uctx = nla_data(rt);
1430 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1433 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1439 for (i = 0; i < nr; i++, ut++) {
1440 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1442 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1443 memcpy(&t->saddr, &ut->saddr,
1444 sizeof(xfrm_address_t));
1445 t->reqid = ut->reqid;
1447 t->share = ut->share;
1448 t->optional = ut->optional;
1449 t->aalgos = ut->aalgos;
1450 t->ealgos = ut->ealgos;
1451 t->calgos = ut->calgos;
1452 /* If all masks are ~0, then we allow all algorithms. */
1453 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1454 t->encap_family = ut->family;
1458 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1463 if (nr > XFRM_MAX_DEPTH)
1466 prev_family = family;
1468 for (i = 0; i < nr; i++) {
1469 /* We never validated the ut->family value, so many
1470 * applications simply leave it at zero. The check was
1471 * never made and ut->family was ignored because all
1472 * templates could be assumed to have the same family as
1473 * the policy itself. Now that we will have ipv4-in-ipv6
1474 * and ipv6-in-ipv4 tunnels, this is no longer true.
1477 ut[i].family = family;
1479 if ((ut[i].mode == XFRM_MODE_TRANSPORT) &&
1480 (ut[i].family != prev_family))
1483 prev_family = ut[i].family;
1485 switch (ut[i].family) {
1488 #if IS_ENABLED(CONFIG_IPV6)
1496 switch (ut[i].id.proto) {
1500 #if IS_ENABLED(CONFIG_IPV6)
1501 case IPPROTO_ROUTING:
1502 case IPPROTO_DSTOPTS:
1504 case IPSEC_PROTO_ANY:
1515 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1517 struct nlattr *rt = attrs[XFRMA_TMPL];
1522 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1523 int nr = nla_len(rt) / sizeof(*utmpl);
1526 err = validate_tmpl(nr, utmpl, pol->family);
1530 copy_templates(pol, utmpl, nr);
1535 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1537 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1538 struct xfrm_userpolicy_type *upt;
1539 u8 type = XFRM_POLICY_TYPE_MAIN;
1547 err = verify_policy_type(type);
1555 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1557 xp->priority = p->priority;
1558 xp->index = p->index;
1559 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1560 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1561 xp->action = p->action;
1562 xp->flags = p->flags;
1563 xp->family = p->sel.family;
1564 /* XXX xp->share = p->share; */
1567 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1569 memset(p, 0, sizeof(*p));
1570 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1571 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1572 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1573 p->priority = xp->priority;
1574 p->index = xp->index;
1575 p->sel.family = xp->family;
1577 p->action = xp->action;
1578 p->flags = xp->flags;
1579 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1582 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1584 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1592 copy_from_user_policy(xp, p);
1594 err = copy_from_user_policy_type(&xp->type, attrs);
1598 if (!(err = copy_from_user_tmpl(xp, attrs)))
1599 err = copy_from_user_sec_ctx(xp, attrs);
1603 xfrm_mark_get(attrs, &xp->mark);
1605 if (attrs[XFRMA_IF_ID])
1606 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1612 xfrm_policy_destroy(xp);
1616 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1617 struct nlattr **attrs)
1619 struct net *net = sock_net(skb->sk);
1620 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1621 struct xfrm_policy *xp;
1626 err = verify_newpolicy_info(p);
1629 err = verify_sec_ctx_len(attrs);
1633 xp = xfrm_policy_construct(net, p, attrs, &err);
1637 /* shouldn't excl be based on nlh flags??
1638 * Aha! this is anti-netlink really i.e more pfkey derived
1639 * in netlink excl is a flag and you wouldnt need
1640 * a type XFRM_MSG_UPDPOLICY - JHS */
1641 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1642 err = xfrm_policy_insert(p->dir, xp, excl);
1643 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1646 security_xfrm_policy_free(xp->security);
1651 c.event = nlh->nlmsg_type;
1652 c.seq = nlh->nlmsg_seq;
1653 c.portid = nlh->nlmsg_pid;
1654 km_policy_notify(xp, p->dir, &c);
1661 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1663 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1666 if (xp->xfrm_nr == 0)
1669 for (i = 0; i < xp->xfrm_nr; i++) {
1670 struct xfrm_user_tmpl *up = &vec[i];
1671 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1673 memset(up, 0, sizeof(*up));
1674 memcpy(&up->id, &kp->id, sizeof(up->id));
1675 up->family = kp->encap_family;
1676 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1677 up->reqid = kp->reqid;
1678 up->mode = kp->mode;
1679 up->share = kp->share;
1680 up->optional = kp->optional;
1681 up->aalgos = kp->aalgos;
1682 up->ealgos = kp->ealgos;
1683 up->calgos = kp->calgos;
1686 return nla_put(skb, XFRMA_TMPL,
1687 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1690 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1693 return copy_sec_ctx(x->security, skb);
1698 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1701 return copy_sec_ctx(xp->security, skb);
1704 static inline unsigned int userpolicy_type_attrsize(void)
1706 #ifdef CONFIG_XFRM_SUB_POLICY
1707 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1713 #ifdef CONFIG_XFRM_SUB_POLICY
1714 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1716 struct xfrm_userpolicy_type upt;
1718 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1719 memset(&upt, 0, sizeof(upt));
1722 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1726 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1732 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1734 struct xfrm_dump_info *sp = ptr;
1735 struct xfrm_userpolicy_info *p;
1736 struct sk_buff *in_skb = sp->in_skb;
1737 struct sk_buff *skb = sp->out_skb;
1738 struct nlmsghdr *nlh;
1741 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1742 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1746 p = nlmsg_data(nlh);
1747 copy_to_user_policy(xp, p, dir);
1748 err = copy_to_user_tmpl(xp, skb);
1750 err = copy_to_user_sec_ctx(xp, skb);
1752 err = copy_to_user_policy_type(xp->type, skb);
1754 err = xfrm_mark_put(skb, &xp->mark);
1756 err = xfrm_if_id_put(skb, xp->if_id);
1758 nlmsg_cancel(skb, nlh);
1761 nlmsg_end(skb, nlh);
1765 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1767 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1768 struct net *net = sock_net(cb->skb->sk);
1770 xfrm_policy_walk_done(walk, net);
1774 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1776 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1778 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1780 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1784 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1786 struct net *net = sock_net(skb->sk);
1787 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1788 struct xfrm_dump_info info;
1790 info.in_skb = cb->skb;
1792 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1793 info.nlmsg_flags = NLM_F_MULTI;
1795 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1800 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1801 struct xfrm_policy *xp,
1804 struct xfrm_dump_info info;
1805 struct sk_buff *skb;
1808 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1810 return ERR_PTR(-ENOMEM);
1812 info.in_skb = in_skb;
1814 info.nlmsg_seq = seq;
1815 info.nlmsg_flags = 0;
1817 err = dump_one_policy(xp, dir, 0, &info);
1820 return ERR_PTR(err);
1826 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1827 struct nlattr **attrs)
1829 struct net *net = sock_net(skb->sk);
1830 struct xfrm_policy *xp;
1831 struct xfrm_userpolicy_id *p;
1832 u8 type = XFRM_POLICY_TYPE_MAIN;
1837 u32 mark = xfrm_mark_get(attrs, &m);
1840 p = nlmsg_data(nlh);
1841 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1843 err = copy_from_user_policy_type(&type, attrs);
1847 err = verify_policy_dir(p->dir);
1851 if (attrs[XFRMA_IF_ID])
1852 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1855 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, delete, &err);
1857 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1858 struct xfrm_sec_ctx *ctx;
1860 err = verify_sec_ctx_len(attrs);
1866 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1868 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1872 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir, &p->sel,
1874 security_xfrm_policy_free(ctx);
1880 struct sk_buff *resp_skb;
1882 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1883 if (IS_ERR(resp_skb)) {
1884 err = PTR_ERR(resp_skb);
1886 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1887 NETLINK_CB(skb).portid);
1890 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1895 c.data.byid = p->index;
1896 c.event = nlh->nlmsg_type;
1897 c.seq = nlh->nlmsg_seq;
1898 c.portid = nlh->nlmsg_pid;
1899 km_policy_notify(xp, p->dir, &c);
1907 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1908 struct nlattr **attrs)
1910 struct net *net = sock_net(skb->sk);
1912 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1915 err = xfrm_state_flush(net, p->proto, true);
1917 if (err == -ESRCH) /* empty table */
1921 c.data.proto = p->proto;
1922 c.event = nlh->nlmsg_type;
1923 c.seq = nlh->nlmsg_seq;
1924 c.portid = nlh->nlmsg_pid;
1926 km_state_notify(NULL, &c);
1931 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
1933 unsigned int replay_size = x->replay_esn ?
1934 xfrm_replay_state_esn_len(x->replay_esn) :
1935 sizeof(struct xfrm_replay_state);
1937 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1938 + nla_total_size(replay_size)
1939 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1940 + nla_total_size(sizeof(struct xfrm_mark))
1941 + nla_total_size(4) /* XFRM_AE_RTHR */
1942 + nla_total_size(4); /* XFRM_AE_ETHR */
1945 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1947 struct xfrm_aevent_id *id;
1948 struct nlmsghdr *nlh;
1951 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1955 id = nlmsg_data(nlh);
1956 memset(&id->sa_id, 0, sizeof(id->sa_id));
1957 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1958 id->sa_id.spi = x->id.spi;
1959 id->sa_id.family = x->props.family;
1960 id->sa_id.proto = x->id.proto;
1961 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1962 id->reqid = x->props.reqid;
1963 id->flags = c->data.aevent;
1965 if (x->replay_esn) {
1966 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1967 xfrm_replay_state_esn_len(x->replay_esn),
1970 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1975 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1980 if (id->flags & XFRM_AE_RTHR) {
1981 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1985 if (id->flags & XFRM_AE_ETHR) {
1986 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1987 x->replay_maxage * 10 / HZ);
1991 err = xfrm_mark_put(skb, &x->mark);
1995 err = xfrm_if_id_put(skb, x->if_id);
1999 nlmsg_end(skb, nlh);
2003 nlmsg_cancel(skb, nlh);
2007 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2008 struct nlattr **attrs)
2010 struct net *net = sock_net(skb->sk);
2011 struct xfrm_state *x;
2012 struct sk_buff *r_skb;
2017 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2018 struct xfrm_usersa_id *id = &p->sa_id;
2020 mark = xfrm_mark_get(attrs, &m);
2022 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2026 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2027 if (r_skb == NULL) {
2033 * XXX: is this lock really needed - none of the other
2034 * gets lock (the concern is things getting updated
2035 * while we are still reading) - jhs
2037 spin_lock_bh(&x->lock);
2038 c.data.aevent = p->flags;
2039 c.seq = nlh->nlmsg_seq;
2040 c.portid = nlh->nlmsg_pid;
2042 err = build_aevent(r_skb, x, &c);
2045 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2046 spin_unlock_bh(&x->lock);
2051 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2052 struct nlattr **attrs)
2054 struct net *net = sock_net(skb->sk);
2055 struct xfrm_state *x;
2060 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2061 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2062 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2063 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2064 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2065 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2067 if (!lt && !rp && !re && !et && !rt)
2070 /* pedantic mode - thou shalt sayeth replaceth */
2071 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2074 mark = xfrm_mark_get(attrs, &m);
2076 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2080 if (x->km.state != XFRM_STATE_VALID)
2083 err = xfrm_replay_verify_len(x->replay_esn, re);
2087 spin_lock_bh(&x->lock);
2088 xfrm_update_ae_params(x, attrs, 1);
2089 spin_unlock_bh(&x->lock);
2091 c.event = nlh->nlmsg_type;
2092 c.seq = nlh->nlmsg_seq;
2093 c.portid = nlh->nlmsg_pid;
2094 c.data.aevent = XFRM_AE_CU;
2095 km_state_notify(x, &c);
2102 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2103 struct nlattr **attrs)
2105 struct net *net = sock_net(skb->sk);
2107 u8 type = XFRM_POLICY_TYPE_MAIN;
2110 err = copy_from_user_policy_type(&type, attrs);
2114 err = xfrm_policy_flush(net, type, true);
2116 if (err == -ESRCH) /* empty table */
2122 c.event = nlh->nlmsg_type;
2123 c.seq = nlh->nlmsg_seq;
2124 c.portid = nlh->nlmsg_pid;
2126 km_policy_notify(NULL, 0, &c);
2130 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2131 struct nlattr **attrs)
2133 struct net *net = sock_net(skb->sk);
2134 struct xfrm_policy *xp;
2135 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2136 struct xfrm_userpolicy_info *p = &up->pol;
2137 u8 type = XFRM_POLICY_TYPE_MAIN;
2140 u32 mark = xfrm_mark_get(attrs, &m);
2143 err = copy_from_user_policy_type(&type, attrs);
2147 err = verify_policy_dir(p->dir);
2151 if (attrs[XFRMA_IF_ID])
2152 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2155 xp = xfrm_policy_byid(net, mark, if_id, type, p->dir, p->index, 0, &err);
2157 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2158 struct xfrm_sec_ctx *ctx;
2160 err = verify_sec_ctx_len(attrs);
2166 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2168 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2172 xp = xfrm_policy_bysel_ctx(net, mark, if_id, type, p->dir,
2173 &p->sel, ctx, 0, &err);
2174 security_xfrm_policy_free(ctx);
2179 if (unlikely(xp->walk.dead))
2184 xfrm_policy_delete(xp, p->dir);
2185 xfrm_audit_policy_delete(xp, 1, true);
2187 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2194 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2195 struct nlattr **attrs)
2197 struct net *net = sock_net(skb->sk);
2198 struct xfrm_state *x;
2200 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2201 struct xfrm_usersa_info *p = &ue->state;
2203 u32 mark = xfrm_mark_get(attrs, &m);
2205 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2211 spin_lock_bh(&x->lock);
2213 if (x->km.state != XFRM_STATE_VALID)
2215 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2218 __xfrm_state_delete(x);
2219 xfrm_audit_state_delete(x, 1, true);
2223 spin_unlock_bh(&x->lock);
2228 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2229 struct nlattr **attrs)
2231 struct net *net = sock_net(skb->sk);
2232 struct xfrm_policy *xp;
2233 struct xfrm_user_tmpl *ut;
2235 struct nlattr *rt = attrs[XFRMA_TMPL];
2236 struct xfrm_mark mark;
2238 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2239 struct xfrm_state *x = xfrm_state_alloc(net);
2245 xfrm_mark_get(attrs, &mark);
2247 err = verify_newpolicy_info(&ua->policy);
2252 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2256 memcpy(&x->id, &ua->id, sizeof(ua->id));
2257 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2258 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2259 xp->mark.m = x->mark.m = mark.m;
2260 xp->mark.v = x->mark.v = mark.v;
2262 /* extract the templates and for each call km_key */
2263 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2264 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2265 memcpy(&x->id, &t->id, sizeof(x->id));
2266 x->props.mode = t->mode;
2267 x->props.reqid = t->reqid;
2268 x->props.family = ut->family;
2269 t->aalgos = ua->aalgos;
2270 t->ealgos = ua->ealgos;
2271 t->calgos = ua->calgos;
2272 err = km_query(x, t, xp);
2287 #ifdef CONFIG_XFRM_MIGRATE
2288 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2289 struct xfrm_kmaddress *k,
2290 struct nlattr **attrs, int *num)
2292 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2293 struct xfrm_user_migrate *um;
2297 struct xfrm_user_kmaddress *uk;
2299 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2300 memcpy(&k->local, &uk->local, sizeof(k->local));
2301 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2302 k->family = uk->family;
2303 k->reserved = uk->reserved;
2307 num_migrate = nla_len(rt) / sizeof(*um);
2309 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2312 for (i = 0; i < num_migrate; i++, um++, ma++) {
2313 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2314 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2315 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2316 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2318 ma->proto = um->proto;
2319 ma->mode = um->mode;
2320 ma->reqid = um->reqid;
2322 ma->old_family = um->old_family;
2323 ma->new_family = um->new_family;
2330 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2331 struct nlattr **attrs)
2333 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2334 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2335 struct xfrm_kmaddress km, *kmp;
2339 struct net *net = sock_net(skb->sk);
2340 struct xfrm_encap_tmpl *encap = NULL;
2342 if (attrs[XFRMA_MIGRATE] == NULL)
2345 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2347 err = copy_from_user_policy_type(&type, attrs);
2351 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2358 if (attrs[XFRMA_ENCAP]) {
2359 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2360 sizeof(*encap), GFP_KERNEL);
2365 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2372 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2373 struct nlattr **attrs)
2375 return -ENOPROTOOPT;
2379 #ifdef CONFIG_XFRM_MIGRATE
2380 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2382 struct xfrm_user_migrate um;
2384 memset(&um, 0, sizeof(um));
2385 um.proto = m->proto;
2387 um.reqid = m->reqid;
2388 um.old_family = m->old_family;
2389 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2390 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2391 um.new_family = m->new_family;
2392 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2393 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2395 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2398 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2400 struct xfrm_user_kmaddress uk;
2402 memset(&uk, 0, sizeof(uk));
2403 uk.family = k->family;
2404 uk.reserved = k->reserved;
2405 memcpy(&uk.local, &k->local, sizeof(uk.local));
2406 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2408 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2411 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2414 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2415 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2416 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2417 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2418 + userpolicy_type_attrsize();
2421 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2422 int num_migrate, const struct xfrm_kmaddress *k,
2423 const struct xfrm_selector *sel,
2424 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2426 const struct xfrm_migrate *mp;
2427 struct xfrm_userpolicy_id *pol_id;
2428 struct nlmsghdr *nlh;
2431 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2435 pol_id = nlmsg_data(nlh);
2436 /* copy data from selector, dir, and type to the pol_id */
2437 memset(pol_id, 0, sizeof(*pol_id));
2438 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2442 err = copy_to_user_kmaddress(k, skb);
2447 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2451 err = copy_to_user_policy_type(type, skb);
2454 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2455 err = copy_to_user_migrate(mp, skb);
2460 nlmsg_end(skb, nlh);
2464 nlmsg_cancel(skb, nlh);
2468 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2469 const struct xfrm_migrate *m, int num_migrate,
2470 const struct xfrm_kmaddress *k,
2471 const struct xfrm_encap_tmpl *encap)
2473 struct net *net = &init_net;
2474 struct sk_buff *skb;
2477 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2483 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2486 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2489 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2490 const struct xfrm_migrate *m, int num_migrate,
2491 const struct xfrm_kmaddress *k,
2492 const struct xfrm_encap_tmpl *encap)
2494 return -ENOPROTOOPT;
2498 #define XMSGSIZE(type) sizeof(struct type)
2500 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2501 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2502 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2503 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2504 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2505 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2506 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2507 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2508 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2509 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2510 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2511 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2512 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2513 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2514 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2515 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2516 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2517 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2518 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2519 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2520 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2521 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2526 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2527 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2528 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2529 [XFRMA_LASTUSED] = { .type = NLA_U64},
2530 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2531 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2532 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2533 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2534 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2535 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2536 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2537 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2538 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2539 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2540 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2541 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2542 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2543 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2544 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2545 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2546 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2547 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2548 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2549 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2550 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2551 [XFRMA_PROTO] = { .type = NLA_U8 },
2552 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2553 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2554 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2555 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2556 [XFRMA_IF_ID] = { .type = NLA_U32 },
2559 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2560 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2561 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2564 static const struct xfrm_link {
2565 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2566 int (*start)(struct netlink_callback *);
2567 int (*dump)(struct sk_buff *, struct netlink_callback *);
2568 int (*done)(struct netlink_callback *);
2569 const struct nla_policy *nla_pol;
2571 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2572 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2573 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2574 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2575 .dump = xfrm_dump_sa,
2576 .done = xfrm_dump_sa_done },
2577 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2578 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2579 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2580 .start = xfrm_dump_policy_start,
2581 .dump = xfrm_dump_policy,
2582 .done = xfrm_dump_policy_done },
2583 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2584 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2585 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2586 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2587 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2588 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2589 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2590 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2591 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2592 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2593 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2594 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2595 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2596 .nla_pol = xfrma_spd_policy,
2597 .nla_max = XFRMA_SPD_MAX },
2598 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2601 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2602 struct netlink_ext_ack *extack)
2604 struct net *net = sock_net(skb->sk);
2605 struct nlattr *attrs[XFRMA_MAX+1];
2606 const struct xfrm_link *link;
2609 #ifdef CONFIG_COMPAT
2610 if (in_compat_syscall())
2614 type = nlh->nlmsg_type;
2615 if (type > XFRM_MSG_MAX)
2618 type -= XFRM_MSG_BASE;
2619 link = &xfrm_dispatch[type];
2621 /* All operations require privileges, even GET */
2622 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2625 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2626 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2627 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2628 if (link->dump == NULL)
2632 struct netlink_dump_control c = {
2633 .start = link->start,
2637 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2641 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2642 link->nla_max ? : XFRMA_MAX,
2643 link->nla_pol ? : xfrma_policy, extack);
2647 if (link->doit == NULL)
2650 return link->doit(skb, nlh, attrs);
2653 static void xfrm_netlink_rcv(struct sk_buff *skb)
2655 struct net *net = sock_net(skb->sk);
2657 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2658 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2659 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2662 static inline unsigned int xfrm_expire_msgsize(void)
2664 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2665 + nla_total_size(sizeof(struct xfrm_mark));
2668 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2670 struct xfrm_user_expire *ue;
2671 struct nlmsghdr *nlh;
2674 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2678 ue = nlmsg_data(nlh);
2679 copy_to_user_state(x, &ue->state);
2680 ue->hard = (c->data.hard != 0) ? 1 : 0;
2681 /* clear the padding bytes */
2682 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2684 err = xfrm_mark_put(skb, &x->mark);
2688 err = xfrm_if_id_put(skb, x->if_id);
2692 nlmsg_end(skb, nlh);
2696 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2698 struct net *net = xs_net(x);
2699 struct sk_buff *skb;
2701 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2705 if (build_expire(skb, x, c) < 0) {
2710 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2713 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2715 struct net *net = xs_net(x);
2716 struct sk_buff *skb;
2719 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2723 err = build_aevent(skb, x, c);
2726 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2729 static int xfrm_notify_sa_flush(const struct km_event *c)
2731 struct net *net = c->net;
2732 struct xfrm_usersa_flush *p;
2733 struct nlmsghdr *nlh;
2734 struct sk_buff *skb;
2735 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2737 skb = nlmsg_new(len, GFP_ATOMIC);
2741 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2747 p = nlmsg_data(nlh);
2748 p->proto = c->data.proto;
2750 nlmsg_end(skb, nlh);
2752 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2755 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
2759 l += nla_total_size(aead_len(x->aead));
2761 l += nla_total_size(sizeof(struct xfrm_algo) +
2762 (x->aalg->alg_key_len + 7) / 8);
2763 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2766 l += nla_total_size(xfrm_alg_len(x->ealg));
2768 l += nla_total_size(sizeof(*x->calg));
2770 l += nla_total_size(sizeof(*x->encap));
2772 l += nla_total_size(sizeof(x->tfcpad));
2774 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2776 l += nla_total_size(sizeof(struct xfrm_replay_state));
2778 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2779 x->security->ctx_len);
2781 l += nla_total_size(sizeof(*x->coaddr));
2782 if (x->props.extra_flags)
2783 l += nla_total_size(sizeof(x->props.extra_flags));
2785 l += nla_total_size(sizeof(x->xso));
2786 if (x->props.smark.v | x->props.smark.m) {
2787 l += nla_total_size(sizeof(x->props.smark.v));
2788 l += nla_total_size(sizeof(x->props.smark.m));
2791 l += nla_total_size(sizeof(x->if_id));
2793 /* Must count x->lastused as it may become non-zero behind our back. */
2794 l += nla_total_size_64bit(sizeof(u64));
2799 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2801 struct net *net = xs_net(x);
2802 struct xfrm_usersa_info *p;
2803 struct xfrm_usersa_id *id;
2804 struct nlmsghdr *nlh;
2805 struct sk_buff *skb;
2806 unsigned int len = xfrm_sa_len(x);
2807 unsigned int headlen;
2810 headlen = sizeof(*p);
2811 if (c->event == XFRM_MSG_DELSA) {
2812 len += nla_total_size(headlen);
2813 headlen = sizeof(*id);
2814 len += nla_total_size(sizeof(struct xfrm_mark));
2816 len += NLMSG_ALIGN(headlen);
2818 skb = nlmsg_new(len, GFP_ATOMIC);
2822 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2827 p = nlmsg_data(nlh);
2828 if (c->event == XFRM_MSG_DELSA) {
2829 struct nlattr *attr;
2831 id = nlmsg_data(nlh);
2832 memset(id, 0, sizeof(*id));
2833 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2834 id->spi = x->id.spi;
2835 id->family = x->props.family;
2836 id->proto = x->id.proto;
2838 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2845 err = copy_to_user_state_extra(x, p, skb);
2849 nlmsg_end(skb, nlh);
2851 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2858 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2862 case XFRM_MSG_EXPIRE:
2863 return xfrm_exp_state_notify(x, c);
2864 case XFRM_MSG_NEWAE:
2865 return xfrm_aevent_state_notify(x, c);
2866 case XFRM_MSG_DELSA:
2867 case XFRM_MSG_UPDSA:
2868 case XFRM_MSG_NEWSA:
2869 return xfrm_notify_sa(x, c);
2870 case XFRM_MSG_FLUSHSA:
2871 return xfrm_notify_sa_flush(c);
2873 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2882 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
2883 struct xfrm_policy *xp)
2885 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2886 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2887 + nla_total_size(sizeof(struct xfrm_mark))
2888 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2889 + userpolicy_type_attrsize();
2892 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2893 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2895 __u32 seq = xfrm_get_acqseq();
2896 struct xfrm_user_acquire *ua;
2897 struct nlmsghdr *nlh;
2900 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2904 ua = nlmsg_data(nlh);
2905 memcpy(&ua->id, &x->id, sizeof(ua->id));
2906 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2907 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2908 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2909 ua->aalgos = xt->aalgos;
2910 ua->ealgos = xt->ealgos;
2911 ua->calgos = xt->calgos;
2912 ua->seq = x->km.seq = seq;
2914 err = copy_to_user_tmpl(xp, skb);
2916 err = copy_to_user_state_sec_ctx(x, skb);
2918 err = copy_to_user_policy_type(xp->type, skb);
2920 err = xfrm_mark_put(skb, &xp->mark);
2922 err = xfrm_if_id_put(skb, xp->if_id);
2924 nlmsg_cancel(skb, nlh);
2928 nlmsg_end(skb, nlh);
2932 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2933 struct xfrm_policy *xp)
2935 struct net *net = xs_net(x);
2936 struct sk_buff *skb;
2939 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2943 err = build_acquire(skb, x, xt, xp);
2946 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2949 /* User gives us xfrm_user_policy_info followed by an array of 0
2950 * or more templates.
2952 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2953 u8 *data, int len, int *dir)
2955 struct net *net = sock_net(sk);
2956 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2957 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2958 struct xfrm_policy *xp;
2961 switch (sk->sk_family) {
2963 if (opt != IP_XFRM_POLICY) {
2968 #if IS_ENABLED(CONFIG_IPV6)
2970 if (opt != IPV6_XFRM_POLICY) {
2983 if (len < sizeof(*p) ||
2984 verify_newpolicy_info(p))
2987 nr = ((len - sizeof(*p)) / sizeof(*ut));
2988 if (validate_tmpl(nr, ut, p->sel.family))
2991 if (p->dir > XFRM_POLICY_OUT)
2994 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3000 copy_from_user_policy(xp, p);
3001 xp->type = XFRM_POLICY_TYPE_MAIN;
3002 copy_templates(xp, ut, nr);
3009 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3011 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3012 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3013 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3014 + nla_total_size(sizeof(struct xfrm_mark))
3015 + userpolicy_type_attrsize();
3018 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3019 int dir, const struct km_event *c)
3021 struct xfrm_user_polexpire *upe;
3022 int hard = c->data.hard;
3023 struct nlmsghdr *nlh;
3026 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3030 upe = nlmsg_data(nlh);
3031 copy_to_user_policy(xp, &upe->pol, dir);
3032 err = copy_to_user_tmpl(xp, skb);
3034 err = copy_to_user_sec_ctx(xp, skb);
3036 err = copy_to_user_policy_type(xp->type, skb);
3038 err = xfrm_mark_put(skb, &xp->mark);
3040 err = xfrm_if_id_put(skb, xp->if_id);
3042 nlmsg_cancel(skb, nlh);
3047 nlmsg_end(skb, nlh);
3051 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3053 struct net *net = xp_net(xp);
3054 struct sk_buff *skb;
3057 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3061 err = build_polexpire(skb, xp, dir, c);
3064 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3067 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3069 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3070 struct net *net = xp_net(xp);
3071 struct xfrm_userpolicy_info *p;
3072 struct xfrm_userpolicy_id *id;
3073 struct nlmsghdr *nlh;
3074 struct sk_buff *skb;
3075 unsigned int headlen;
3078 headlen = sizeof(*p);
3079 if (c->event == XFRM_MSG_DELPOLICY) {
3080 len += nla_total_size(headlen);
3081 headlen = sizeof(*id);
3083 len += userpolicy_type_attrsize();
3084 len += nla_total_size(sizeof(struct xfrm_mark));
3085 len += NLMSG_ALIGN(headlen);
3087 skb = nlmsg_new(len, GFP_ATOMIC);
3091 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3096 p = nlmsg_data(nlh);
3097 if (c->event == XFRM_MSG_DELPOLICY) {
3098 struct nlattr *attr;
3100 id = nlmsg_data(nlh);
3101 memset(id, 0, sizeof(*id));
3104 id->index = xp->index;
3106 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3108 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3116 copy_to_user_policy(xp, p, dir);
3117 err = copy_to_user_tmpl(xp, skb);
3119 err = copy_to_user_policy_type(xp->type, skb);
3121 err = xfrm_mark_put(skb, &xp->mark);
3123 err = xfrm_if_id_put(skb, xp->if_id);
3127 nlmsg_end(skb, nlh);
3129 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3136 static int xfrm_notify_policy_flush(const struct km_event *c)
3138 struct net *net = c->net;
3139 struct nlmsghdr *nlh;
3140 struct sk_buff *skb;
3143 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3147 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3151 err = copy_to_user_policy_type(c->data.type, skb);
3155 nlmsg_end(skb, nlh);
3157 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3164 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3168 case XFRM_MSG_NEWPOLICY:
3169 case XFRM_MSG_UPDPOLICY:
3170 case XFRM_MSG_DELPOLICY:
3171 return xfrm_notify_policy(xp, dir, c);
3172 case XFRM_MSG_FLUSHPOLICY:
3173 return xfrm_notify_policy_flush(c);
3174 case XFRM_MSG_POLEXPIRE:
3175 return xfrm_exp_policy_notify(xp, dir, c);
3177 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3185 static inline unsigned int xfrm_report_msgsize(void)
3187 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3190 static int build_report(struct sk_buff *skb, u8 proto,
3191 struct xfrm_selector *sel, xfrm_address_t *addr)
3193 struct xfrm_user_report *ur;
3194 struct nlmsghdr *nlh;
3196 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3200 ur = nlmsg_data(nlh);
3202 memcpy(&ur->sel, sel, sizeof(ur->sel));
3205 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3207 nlmsg_cancel(skb, nlh);
3211 nlmsg_end(skb, nlh);
3215 static int xfrm_send_report(struct net *net, u8 proto,
3216 struct xfrm_selector *sel, xfrm_address_t *addr)
3218 struct sk_buff *skb;
3221 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3225 err = build_report(skb, proto, sel, addr);
3228 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3231 static inline unsigned int xfrm_mapping_msgsize(void)
3233 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3236 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3237 xfrm_address_t *new_saddr, __be16 new_sport)
3239 struct xfrm_user_mapping *um;
3240 struct nlmsghdr *nlh;
3242 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3246 um = nlmsg_data(nlh);
3248 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3249 um->id.spi = x->id.spi;
3250 um->id.family = x->props.family;
3251 um->id.proto = x->id.proto;
3252 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3253 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3254 um->new_sport = new_sport;
3255 um->old_sport = x->encap->encap_sport;
3256 um->reqid = x->props.reqid;
3258 nlmsg_end(skb, nlh);
3262 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3265 struct net *net = xs_net(x);
3266 struct sk_buff *skb;
3269 if (x->id.proto != IPPROTO_ESP)
3275 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3279 err = build_mapping(skb, x, ipaddr, sport);
3282 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3285 static bool xfrm_is_alive(const struct km_event *c)
3287 return (bool)xfrm_acquire_is_on(c->net);
3290 static struct xfrm_mgr netlink_mgr = {
3291 .notify = xfrm_send_state_notify,
3292 .acquire = xfrm_send_acquire,
3293 .compile_policy = xfrm_compile_policy,
3294 .notify_policy = xfrm_send_policy_notify,
3295 .report = xfrm_send_report,
3296 .migrate = xfrm_send_migrate,
3297 .new_mapping = xfrm_send_mapping,
3298 .is_alive = xfrm_is_alive,
3301 static int __net_init xfrm_user_net_init(struct net *net)
3304 struct netlink_kernel_cfg cfg = {
3305 .groups = XFRMNLGRP_MAX,
3306 .input = xfrm_netlink_rcv,
3309 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3312 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3313 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3317 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3320 list_for_each_entry(net, net_exit_list, exit_list)
3321 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3323 list_for_each_entry(net, net_exit_list, exit_list)
3324 netlink_kernel_release(net->xfrm.nlsk_stash);
3327 static struct pernet_operations xfrm_user_net_ops = {
3328 .init = xfrm_user_net_init,
3329 .exit_batch = xfrm_user_net_exit,
3332 static int __init xfrm_user_init(void)
3336 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3338 rv = register_pernet_subsys(&xfrm_user_net_ops);
3341 rv = xfrm_register_km(&netlink_mgr);
3343 unregister_pernet_subsys(&xfrm_user_net_ops);
3347 static void __exit xfrm_user_exit(void)
3349 xfrm_unregister_km(&netlink_mgr);
3350 unregister_pernet_subsys(&xfrm_user_net_ops);
3353 module_init(xfrm_user_init);
3354 module_exit(xfrm_user_exit);
3355 MODULE_LICENSE("GPL");
3356 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);