1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
8 * Kazunori MIYAZAWA @USAGI
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
14 #include <linux/crypto.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/socket.h>
20 #include <linux/string.h>
21 #include <linux/net.h>
22 #include <linux/skbuff.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/ipsec.h>
25 #include <linux/init.h>
26 #include <linux/security.h>
29 #include <net/netlink.h>
31 #include <linux/uaccess.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <linux/in6.h>
35 #include <asm/unaligned.h>
37 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
39 struct nlattr *rt = attrs[type];
40 struct xfrm_algo *algp;
46 if (nla_len(rt) < (int)xfrm_alg_len(algp))
59 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
63 static int verify_auth_trunc(struct nlattr **attrs)
65 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
66 struct xfrm_algo_auth *algp;
72 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
75 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
79 static int verify_aead(struct nlattr **attrs)
81 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
82 struct xfrm_algo_aead *algp;
88 if (nla_len(rt) < (int)aead_len(algp))
91 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
95 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
96 xfrm_address_t **addrp)
98 struct nlattr *rt = attrs[type];
101 *addrp = nla_data(rt);
104 static inline int verify_sec_ctx_len(struct nlattr **attrs)
106 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
107 struct xfrm_user_sec_ctx *uctx;
113 if (uctx->len > nla_len(rt) ||
114 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
120 static inline int verify_replay(struct xfrm_usersa_info *p,
121 struct nlattr **attrs)
123 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
124 struct xfrm_replay_state_esn *rs;
127 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
131 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
134 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
135 nla_len(rt) != sizeof(*rs))
138 /* As only ESP and AH support ESN feature. */
139 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
142 if (p->replay_window != 0)
148 static int verify_newsa_info(struct xfrm_usersa_info *p,
149 struct nlattr **attrs)
159 #if IS_ENABLED(CONFIG_IPV6)
170 switch (p->sel.family) {
175 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
181 #if IS_ENABLED(CONFIG_IPV6)
182 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
196 switch (p->id.proto) {
198 if ((!attrs[XFRMA_ALG_AUTH] &&
199 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
200 attrs[XFRMA_ALG_AEAD] ||
201 attrs[XFRMA_ALG_CRYPT] ||
202 attrs[XFRMA_ALG_COMP] ||
208 if (attrs[XFRMA_ALG_COMP])
210 if (!attrs[XFRMA_ALG_AUTH] &&
211 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
212 !attrs[XFRMA_ALG_CRYPT] &&
213 !attrs[XFRMA_ALG_AEAD])
215 if ((attrs[XFRMA_ALG_AUTH] ||
216 attrs[XFRMA_ALG_AUTH_TRUNC] ||
217 attrs[XFRMA_ALG_CRYPT]) &&
218 attrs[XFRMA_ALG_AEAD])
220 if (attrs[XFRMA_TFCPAD] &&
221 p->mode != XFRM_MODE_TUNNEL)
226 if (!attrs[XFRMA_ALG_COMP] ||
227 attrs[XFRMA_ALG_AEAD] ||
228 attrs[XFRMA_ALG_AUTH] ||
229 attrs[XFRMA_ALG_AUTH_TRUNC] ||
230 attrs[XFRMA_ALG_CRYPT] ||
231 attrs[XFRMA_TFCPAD] ||
232 (ntohl(p->id.spi) >= 0x10000))
236 #if IS_ENABLED(CONFIG_IPV6)
237 case IPPROTO_DSTOPTS:
238 case IPPROTO_ROUTING:
239 if (attrs[XFRMA_ALG_COMP] ||
240 attrs[XFRMA_ALG_AUTH] ||
241 attrs[XFRMA_ALG_AUTH_TRUNC] ||
242 attrs[XFRMA_ALG_AEAD] ||
243 attrs[XFRMA_ALG_CRYPT] ||
244 attrs[XFRMA_ENCAP] ||
245 attrs[XFRMA_SEC_CTX] ||
246 attrs[XFRMA_TFCPAD] ||
247 !attrs[XFRMA_COADDR])
256 if ((err = verify_aead(attrs)))
258 if ((err = verify_auth_trunc(attrs)))
260 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
262 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
264 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
266 if ((err = verify_sec_ctx_len(attrs)))
268 if ((err = verify_replay(p, attrs)))
273 case XFRM_MODE_TRANSPORT:
274 case XFRM_MODE_TUNNEL:
275 case XFRM_MODE_ROUTEOPTIMIZATION:
289 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
290 struct xfrm_algo_desc *(*get_byname)(const char *, int),
293 struct xfrm_algo *p, *ualg;
294 struct xfrm_algo_desc *algo;
299 ualg = nla_data(rta);
301 algo = get_byname(ualg->alg_name, 1);
304 *props = algo->desc.sadb_alg_id;
306 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
310 strcpy(p->alg_name, algo->name);
315 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
317 struct xfrm_algo *p, *ualg;
318 struct xfrm_algo_desc *algo;
323 ualg = nla_data(rta);
325 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
328 x->props.ealgo = algo->desc.sadb_alg_id;
330 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
334 strcpy(p->alg_name, algo->name);
336 x->geniv = algo->uinfo.encr.geniv;
340 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
343 struct xfrm_algo *ualg;
344 struct xfrm_algo_auth *p;
345 struct xfrm_algo_desc *algo;
350 ualg = nla_data(rta);
352 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
355 *props = algo->desc.sadb_alg_id;
357 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
361 strcpy(p->alg_name, algo->name);
362 p->alg_key_len = ualg->alg_key_len;
363 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
364 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
370 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
373 struct xfrm_algo_auth *p, *ualg;
374 struct xfrm_algo_desc *algo;
379 ualg = nla_data(rta);
381 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
384 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
386 *props = algo->desc.sadb_alg_id;
388 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
392 strcpy(p->alg_name, algo->name);
393 if (!p->alg_trunc_len)
394 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
400 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
402 struct xfrm_algo_aead *p, *ualg;
403 struct xfrm_algo_desc *algo;
408 ualg = nla_data(rta);
410 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
413 x->props.ealgo = algo->desc.sadb_alg_id;
415 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
419 strcpy(p->alg_name, algo->name);
421 x->geniv = algo->uinfo.aead.geniv;
425 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
428 struct xfrm_replay_state_esn *up;
431 if (!replay_esn || !rp)
435 ulen = xfrm_replay_state_esn_len(up);
437 /* Check the overall length and the internal bitmap length to avoid
438 * potential overflow. */
439 if (nla_len(rp) < (int)ulen ||
440 xfrm_replay_state_esn_len(replay_esn) != ulen ||
441 replay_esn->bmp_len != up->bmp_len)
444 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
450 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
451 struct xfrm_replay_state_esn **preplay_esn,
454 struct xfrm_replay_state_esn *p, *pp, *up;
455 unsigned int klen, ulen;
461 klen = xfrm_replay_state_esn_len(up);
462 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
464 p = kzalloc(klen, GFP_KERNEL);
468 pp = kzalloc(klen, GFP_KERNEL);
475 memcpy(pp, up, ulen);
483 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
485 unsigned int len = 0;
488 len += sizeof(struct xfrm_user_sec_ctx);
489 len += xfrm_ctx->ctx_len;
494 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
496 memcpy(&x->id, &p->id, sizeof(x->id));
497 memcpy(&x->sel, &p->sel, sizeof(x->sel));
498 memcpy(&x->lft, &p->lft, sizeof(x->lft));
499 x->props.mode = p->mode;
500 x->props.replay_window = min_t(unsigned int, p->replay_window,
501 sizeof(x->replay.bitmap) * 8);
502 x->props.reqid = p->reqid;
503 x->props.family = p->family;
504 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
505 x->props.flags = p->flags;
507 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
508 x->sel.family = p->family;
512 * someday when pfkey also has support, we could have the code
513 * somehow made shareable and move it to xfrm_state.c - JHS
516 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
519 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
520 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
521 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
522 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
523 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
526 struct xfrm_replay_state_esn *replay_esn;
527 replay_esn = nla_data(re);
528 memcpy(x->replay_esn, replay_esn,
529 xfrm_replay_state_esn_len(replay_esn));
530 memcpy(x->preplay_esn, replay_esn,
531 xfrm_replay_state_esn_len(replay_esn));
535 struct xfrm_replay_state *replay;
536 replay = nla_data(rp);
537 memcpy(&x->replay, replay, sizeof(*replay));
538 memcpy(&x->preplay, replay, sizeof(*replay));
542 struct xfrm_lifetime_cur *ltime;
543 ltime = nla_data(lt);
544 x->curlft.bytes = ltime->bytes;
545 x->curlft.packets = ltime->packets;
546 x->curlft.add_time = ltime->add_time;
547 x->curlft.use_time = ltime->use_time;
551 x->replay_maxage = nla_get_u32(et);
554 x->replay_maxdiff = nla_get_u32(rt);
557 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
559 if (attrs[XFRMA_SET_MARK]) {
560 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
561 if (attrs[XFRMA_SET_MARK_MASK])
562 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
570 static struct xfrm_state *xfrm_state_construct(struct net *net,
571 struct xfrm_usersa_info *p,
572 struct nlattr **attrs,
575 struct xfrm_state *x = xfrm_state_alloc(net);
581 copy_from_user_state(x, p);
583 if (attrs[XFRMA_SA_EXTRA_FLAGS])
584 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
586 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
588 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
589 attrs[XFRMA_ALG_AUTH_TRUNC])))
591 if (!x->props.aalgo) {
592 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
593 attrs[XFRMA_ALG_AUTH])))
596 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
598 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
599 xfrm_calg_get_byname,
600 attrs[XFRMA_ALG_COMP])))
603 if (attrs[XFRMA_ENCAP]) {
604 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
605 sizeof(*x->encap), GFP_KERNEL);
606 if (x->encap == NULL)
610 if (attrs[XFRMA_TFCPAD])
611 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
613 if (attrs[XFRMA_COADDR]) {
614 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
615 sizeof(*x->coaddr), GFP_KERNEL);
616 if (x->coaddr == NULL)
620 xfrm_mark_get(attrs, &x->mark);
622 xfrm_smark_init(attrs, &x->props.smark);
624 if (attrs[XFRMA_IF_ID])
625 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
627 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
631 if (attrs[XFRMA_SEC_CTX]) {
632 err = security_xfrm_state_alloc(x,
633 nla_data(attrs[XFRMA_SEC_CTX]));
638 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
639 attrs[XFRMA_REPLAY_ESN_VAL])))
643 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
644 /* sysctl_xfrm_aevent_etime is in 100ms units */
645 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
647 if ((err = xfrm_init_replay(x)))
650 /* override default values from above */
651 xfrm_update_ae_params(x, attrs, 0);
653 /* configure the hardware if offload is requested */
654 if (attrs[XFRMA_OFFLOAD_DEV]) {
655 err = xfrm_dev_state_add(net, x,
656 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
664 x->km.state = XFRM_STATE_DEAD;
671 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
672 struct nlattr **attrs)
674 struct net *net = sock_net(skb->sk);
675 struct xfrm_usersa_info *p = nlmsg_data(nlh);
676 struct xfrm_state *x;
680 err = verify_newsa_info(p, attrs);
684 x = xfrm_state_construct(net, p, attrs, &err);
689 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
690 err = xfrm_state_add(x);
692 err = xfrm_state_update(x);
694 xfrm_audit_state_add(x, err ? 0 : 1, true);
697 x->km.state = XFRM_STATE_DEAD;
698 xfrm_dev_state_delete(x);
703 if (x->km.state == XFRM_STATE_VOID)
704 x->km.state = XFRM_STATE_VALID;
706 c.seq = nlh->nlmsg_seq;
707 c.portid = nlh->nlmsg_pid;
708 c.event = nlh->nlmsg_type;
710 km_state_notify(x, &c);
716 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
717 struct xfrm_usersa_id *p,
718 struct nlattr **attrs,
721 struct xfrm_state *x = NULL;
724 u32 mark = xfrm_mark_get(attrs, &m);
726 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
728 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
730 xfrm_address_t *saddr = NULL;
732 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
739 x = xfrm_state_lookup_byaddr(net, mark,
741 p->proto, p->family);
750 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
751 struct nlattr **attrs)
753 struct net *net = sock_net(skb->sk);
754 struct xfrm_state *x;
757 struct xfrm_usersa_id *p = nlmsg_data(nlh);
759 x = xfrm_user_state_lookup(net, p, attrs, &err);
763 if ((err = security_xfrm_state_delete(x)) != 0)
766 if (xfrm_state_kern(x)) {
771 err = xfrm_state_delete(x);
776 c.seq = nlh->nlmsg_seq;
777 c.portid = nlh->nlmsg_pid;
778 c.event = nlh->nlmsg_type;
779 km_state_notify(x, &c);
782 xfrm_audit_state_delete(x, err ? 0 : 1, true);
787 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
789 memset(p, 0, sizeof(*p));
790 memcpy(&p->id, &x->id, sizeof(p->id));
791 memcpy(&p->sel, &x->sel, sizeof(p->sel));
792 memcpy(&p->lft, &x->lft, sizeof(p->lft));
793 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
794 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
795 put_unaligned(x->stats.replay, &p->stats.replay);
796 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
797 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
798 p->mode = x->props.mode;
799 p->replay_window = x->props.replay_window;
800 p->reqid = x->props.reqid;
801 p->family = x->props.family;
802 p->flags = x->props.flags;
806 struct xfrm_dump_info {
807 struct sk_buff *in_skb;
808 struct sk_buff *out_skb;
813 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
815 struct xfrm_user_sec_ctx *uctx;
817 int ctx_size = sizeof(*uctx) + s->ctx_len;
819 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
823 uctx = nla_data(attr);
824 uctx->exttype = XFRMA_SEC_CTX;
825 uctx->len = ctx_size;
826 uctx->ctx_doi = s->ctx_doi;
827 uctx->ctx_alg = s->ctx_alg;
828 uctx->ctx_len = s->ctx_len;
829 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
834 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
836 struct xfrm_user_offload *xuo;
839 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
843 xuo = nla_data(attr);
844 memset(xuo, 0, sizeof(*xuo));
845 xuo->ifindex = xso->dev->ifindex;
846 xuo->flags = xso->flags;
851 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
853 struct xfrm_algo *algo;
856 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
857 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
861 algo = nla_data(nla);
862 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
863 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
864 algo->alg_key_len = auth->alg_key_len;
869 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
874 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
876 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
881 /* Don't change this without updating xfrm_sa_len! */
882 static int copy_to_user_state_extra(struct xfrm_state *x,
883 struct xfrm_usersa_info *p,
888 copy_to_user_state(x, p);
890 if (x->props.extra_flags) {
891 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
892 x->props.extra_flags);
898 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
903 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
909 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
914 ret = copy_to_user_auth(x->aalg, skb);
916 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
917 xfrm_alg_auth_len(x->aalg), x->aalg);
922 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
927 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
932 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
937 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
941 ret = xfrm_mark_put(skb, &x->mark);
945 ret = xfrm_smark_put(skb, &x->props.smark);
950 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
951 xfrm_replay_state_esn_len(x->replay_esn),
954 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
959 ret = copy_user_offload(&x->xso, skb);
963 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
968 ret = copy_sec_ctx(x->security, skb);
973 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
975 struct xfrm_dump_info *sp = ptr;
976 struct sk_buff *in_skb = sp->in_skb;
977 struct sk_buff *skb = sp->out_skb;
978 struct xfrm_translator *xtr;
979 struct xfrm_usersa_info *p;
980 struct nlmsghdr *nlh;
983 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
984 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
990 err = copy_to_user_state_extra(x, p, skb);
992 nlmsg_cancel(skb, nlh);
997 xtr = xfrm_get_translator();
999 err = xtr->alloc_compat(skb, nlh);
1001 xfrm_put_translator(xtr);
1003 nlmsg_cancel(skb, nlh);
1011 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1013 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1014 struct sock *sk = cb->skb->sk;
1015 struct net *net = sock_net(sk);
1018 xfrm_state_walk_done(walk, net);
1022 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1024 struct net *net = sock_net(skb->sk);
1025 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1026 struct xfrm_dump_info info;
1028 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1029 sizeof(cb->args) - sizeof(cb->args[0]));
1031 info.in_skb = cb->skb;
1033 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1034 info.nlmsg_flags = NLM_F_MULTI;
1037 struct nlattr *attrs[XFRMA_MAX+1];
1038 struct xfrm_address_filter *filter = NULL;
1042 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1043 xfrma_policy, cb->extack);
1047 if (attrs[XFRMA_ADDRESS_FILTER]) {
1048 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1049 sizeof(*filter), GFP_KERNEL);
1054 if (attrs[XFRMA_PROTO])
1055 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1057 xfrm_state_walk_init(walk, proto, filter);
1061 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1066 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1067 struct xfrm_state *x, u32 seq)
1069 struct xfrm_dump_info info;
1070 struct sk_buff *skb;
1073 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1075 return ERR_PTR(-ENOMEM);
1077 info.in_skb = in_skb;
1079 info.nlmsg_seq = seq;
1080 info.nlmsg_flags = 0;
1082 err = dump_one_state(x, 0, &info);
1085 return ERR_PTR(err);
1091 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1092 * Must be called with RCU read lock.
1094 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1095 u32 pid, unsigned int group)
1097 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1098 struct xfrm_translator *xtr;
1105 xtr = xfrm_get_translator();
1107 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1109 xfrm_put_translator(xtr);
1116 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1119 static inline unsigned int xfrm_spdinfo_msgsize(void)
1121 return NLMSG_ALIGN(4)
1122 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1123 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1124 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1125 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1128 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1129 u32 portid, u32 seq, u32 flags)
1131 struct xfrmk_spdinfo si;
1132 struct xfrmu_spdinfo spc;
1133 struct xfrmu_spdhinfo sph;
1134 struct xfrmu_spdhthresh spt4, spt6;
1135 struct nlmsghdr *nlh;
1140 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1141 if (nlh == NULL) /* shouldn't really happen ... */
1144 f = nlmsg_data(nlh);
1146 xfrm_spd_getinfo(net, &si);
1147 spc.incnt = si.incnt;
1148 spc.outcnt = si.outcnt;
1149 spc.fwdcnt = si.fwdcnt;
1150 spc.inscnt = si.inscnt;
1151 spc.outscnt = si.outscnt;
1152 spc.fwdscnt = si.fwdscnt;
1153 sph.spdhcnt = si.spdhcnt;
1154 sph.spdhmcnt = si.spdhmcnt;
1157 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1159 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1160 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1161 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1162 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1163 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1165 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1167 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1169 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1171 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1173 nlmsg_cancel(skb, nlh);
1177 nlmsg_end(skb, nlh);
1181 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1182 struct nlattr **attrs)
1184 struct net *net = sock_net(skb->sk);
1185 struct xfrmu_spdhthresh *thresh4 = NULL;
1186 struct xfrmu_spdhthresh *thresh6 = NULL;
1188 /* selector prefixlen thresholds to hash policies */
1189 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1190 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1192 if (nla_len(rta) < sizeof(*thresh4))
1194 thresh4 = nla_data(rta);
1195 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1198 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1199 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1201 if (nla_len(rta) < sizeof(*thresh6))
1203 thresh6 = nla_data(rta);
1204 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1208 if (thresh4 || thresh6) {
1209 write_seqlock(&net->xfrm.policy_hthresh.lock);
1211 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1212 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1215 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1216 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1218 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1220 xfrm_policy_hash_rebuild(net);
1226 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1227 struct nlattr **attrs)
1229 struct net *net = sock_net(skb->sk);
1230 struct sk_buff *r_skb;
1231 u32 *flags = nlmsg_data(nlh);
1232 u32 sportid = NETLINK_CB(skb).portid;
1233 u32 seq = nlh->nlmsg_seq;
1236 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1240 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1243 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1246 static inline unsigned int xfrm_sadinfo_msgsize(void)
1248 return NLMSG_ALIGN(4)
1249 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1250 + nla_total_size(4); /* XFRMA_SAD_CNT */
1253 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1254 u32 portid, u32 seq, u32 flags)
1256 struct xfrmk_sadinfo si;
1257 struct xfrmu_sadhinfo sh;
1258 struct nlmsghdr *nlh;
1262 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1263 if (nlh == NULL) /* shouldn't really happen ... */
1266 f = nlmsg_data(nlh);
1268 xfrm_sad_getinfo(net, &si);
1270 sh.sadhmcnt = si.sadhmcnt;
1271 sh.sadhcnt = si.sadhcnt;
1273 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1275 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1277 nlmsg_cancel(skb, nlh);
1281 nlmsg_end(skb, nlh);
1285 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1286 struct nlattr **attrs)
1288 struct net *net = sock_net(skb->sk);
1289 struct sk_buff *r_skb;
1290 u32 *flags = nlmsg_data(nlh);
1291 u32 sportid = NETLINK_CB(skb).portid;
1292 u32 seq = nlh->nlmsg_seq;
1295 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1299 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1302 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1305 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1306 struct nlattr **attrs)
1308 struct net *net = sock_net(skb->sk);
1309 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1310 struct xfrm_state *x;
1311 struct sk_buff *resp_skb;
1314 x = xfrm_user_state_lookup(net, p, attrs, &err);
1318 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1319 if (IS_ERR(resp_skb)) {
1320 err = PTR_ERR(resp_skb);
1322 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1329 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1330 struct nlattr **attrs)
1332 struct net *net = sock_net(skb->sk);
1333 struct xfrm_state *x;
1334 struct xfrm_userspi_info *p;
1335 struct xfrm_translator *xtr;
1336 struct sk_buff *resp_skb;
1337 xfrm_address_t *daddr;
1344 p = nlmsg_data(nlh);
1345 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1349 family = p->info.family;
1350 daddr = &p->info.id.daddr;
1354 mark = xfrm_mark_get(attrs, &m);
1356 if (attrs[XFRMA_IF_ID])
1357 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1360 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1361 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1368 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1369 if_id, p->info.id.proto, daddr,
1376 err = xfrm_alloc_spi(x, p->min, p->max);
1380 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1381 if (IS_ERR(resp_skb)) {
1382 err = PTR_ERR(resp_skb);
1386 xtr = xfrm_get_translator();
1388 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1390 xfrm_put_translator(xtr);
1392 kfree_skb(resp_skb);
1397 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1405 static int verify_policy_dir(u8 dir)
1408 case XFRM_POLICY_IN:
1409 case XFRM_POLICY_OUT:
1410 case XFRM_POLICY_FWD:
1420 static int verify_policy_type(u8 type)
1423 case XFRM_POLICY_TYPE_MAIN:
1424 #ifdef CONFIG_XFRM_SUB_POLICY
1425 case XFRM_POLICY_TYPE_SUB:
1436 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1441 case XFRM_SHARE_ANY:
1442 case XFRM_SHARE_SESSION:
1443 case XFRM_SHARE_USER:
1444 case XFRM_SHARE_UNIQUE:
1451 switch (p->action) {
1452 case XFRM_POLICY_ALLOW:
1453 case XFRM_POLICY_BLOCK:
1460 switch (p->sel.family) {
1462 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1468 #if IS_ENABLED(CONFIG_IPV6)
1469 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1474 return -EAFNOSUPPORT;
1481 ret = verify_policy_dir(p->dir);
1484 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1490 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1492 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1493 struct xfrm_user_sec_ctx *uctx;
1498 uctx = nla_data(rt);
1499 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1502 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1508 for (i = 0; i < nr; i++, ut++) {
1509 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1511 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1512 memcpy(&t->saddr, &ut->saddr,
1513 sizeof(xfrm_address_t));
1514 t->reqid = ut->reqid;
1516 t->share = ut->share;
1517 t->optional = ut->optional;
1518 t->aalgos = ut->aalgos;
1519 t->ealgos = ut->ealgos;
1520 t->calgos = ut->calgos;
1521 /* If all masks are ~0, then we allow all algorithms. */
1522 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1523 t->encap_family = ut->family;
1527 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1532 if (nr > XFRM_MAX_DEPTH)
1535 prev_family = family;
1537 for (i = 0; i < nr; i++) {
1538 /* We never validated the ut->family value, so many
1539 * applications simply leave it at zero. The check was
1540 * never made and ut->family was ignored because all
1541 * templates could be assumed to have the same family as
1542 * the policy itself. Now that we will have ipv4-in-ipv6
1543 * and ipv6-in-ipv4 tunnels, this is no longer true.
1546 ut[i].family = family;
1548 switch (ut[i].mode) {
1549 case XFRM_MODE_TUNNEL:
1550 case XFRM_MODE_BEET:
1553 if (ut[i].family != prev_family)
1557 if (ut[i].mode >= XFRM_MODE_MAX)
1560 prev_family = ut[i].family;
1562 switch (ut[i].family) {
1565 #if IS_ENABLED(CONFIG_IPV6)
1573 if (!xfrm_id_proto_valid(ut[i].id.proto))
1580 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1582 struct nlattr *rt = attrs[XFRMA_TMPL];
1587 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1588 int nr = nla_len(rt) / sizeof(*utmpl);
1591 err = validate_tmpl(nr, utmpl, pol->family);
1595 copy_templates(pol, utmpl, nr);
1600 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1602 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1603 struct xfrm_userpolicy_type *upt;
1604 u8 type = XFRM_POLICY_TYPE_MAIN;
1612 err = verify_policy_type(type);
1620 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1622 xp->priority = p->priority;
1623 xp->index = p->index;
1624 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1625 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1626 xp->action = p->action;
1627 xp->flags = p->flags;
1628 xp->family = p->sel.family;
1629 /* XXX xp->share = p->share; */
1632 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1634 memset(p, 0, sizeof(*p));
1635 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1636 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1637 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1638 p->priority = xp->priority;
1639 p->index = xp->index;
1640 p->sel.family = xp->family;
1642 p->action = xp->action;
1643 p->flags = xp->flags;
1644 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1647 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1649 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1657 copy_from_user_policy(xp, p);
1659 err = copy_from_user_policy_type(&xp->type, attrs);
1663 if (!(err = copy_from_user_tmpl(xp, attrs)))
1664 err = copy_from_user_sec_ctx(xp, attrs);
1668 xfrm_mark_get(attrs, &xp->mark);
1670 if (attrs[XFRMA_IF_ID])
1671 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1677 xfrm_policy_destroy(xp);
1681 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1682 struct nlattr **attrs)
1684 struct net *net = sock_net(skb->sk);
1685 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1686 struct xfrm_policy *xp;
1691 err = verify_newpolicy_info(p);
1694 err = verify_sec_ctx_len(attrs);
1698 xp = xfrm_policy_construct(net, p, attrs, &err);
1702 /* shouldn't excl be based on nlh flags??
1703 * Aha! this is anti-netlink really i.e more pfkey derived
1704 * in netlink excl is a flag and you wouldnt need
1705 * a type XFRM_MSG_UPDPOLICY - JHS */
1706 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1707 err = xfrm_policy_insert(p->dir, xp, excl);
1708 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1711 security_xfrm_policy_free(xp->security);
1716 c.event = nlh->nlmsg_type;
1717 c.seq = nlh->nlmsg_seq;
1718 c.portid = nlh->nlmsg_pid;
1719 km_policy_notify(xp, p->dir, &c);
1726 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1728 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1731 if (xp->xfrm_nr == 0)
1734 for (i = 0; i < xp->xfrm_nr; i++) {
1735 struct xfrm_user_tmpl *up = &vec[i];
1736 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1738 memset(up, 0, sizeof(*up));
1739 memcpy(&up->id, &kp->id, sizeof(up->id));
1740 up->family = kp->encap_family;
1741 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1742 up->reqid = kp->reqid;
1743 up->mode = kp->mode;
1744 up->share = kp->share;
1745 up->optional = kp->optional;
1746 up->aalgos = kp->aalgos;
1747 up->ealgos = kp->ealgos;
1748 up->calgos = kp->calgos;
1751 return nla_put(skb, XFRMA_TMPL,
1752 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1755 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1758 return copy_sec_ctx(x->security, skb);
1763 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1766 return copy_sec_ctx(xp->security, skb);
1769 static inline unsigned int userpolicy_type_attrsize(void)
1771 #ifdef CONFIG_XFRM_SUB_POLICY
1772 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1778 #ifdef CONFIG_XFRM_SUB_POLICY
1779 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1781 struct xfrm_userpolicy_type upt;
1783 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1784 memset(&upt, 0, sizeof(upt));
1787 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1791 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1797 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1799 struct xfrm_dump_info *sp = ptr;
1800 struct xfrm_userpolicy_info *p;
1801 struct sk_buff *in_skb = sp->in_skb;
1802 struct sk_buff *skb = sp->out_skb;
1803 struct xfrm_translator *xtr;
1804 struct nlmsghdr *nlh;
1807 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1808 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1812 p = nlmsg_data(nlh);
1813 copy_to_user_policy(xp, p, dir);
1814 err = copy_to_user_tmpl(xp, skb);
1816 err = copy_to_user_sec_ctx(xp, skb);
1818 err = copy_to_user_policy_type(xp->type, skb);
1820 err = xfrm_mark_put(skb, &xp->mark);
1822 err = xfrm_if_id_put(skb, xp->if_id);
1824 nlmsg_cancel(skb, nlh);
1827 nlmsg_end(skb, nlh);
1829 xtr = xfrm_get_translator();
1831 err = xtr->alloc_compat(skb, nlh);
1833 xfrm_put_translator(xtr);
1835 nlmsg_cancel(skb, nlh);
1843 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1845 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1846 struct net *net = sock_net(cb->skb->sk);
1848 xfrm_policy_walk_done(walk, net);
1852 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1854 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1856 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1858 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1862 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1864 struct net *net = sock_net(skb->sk);
1865 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1866 struct xfrm_dump_info info;
1868 info.in_skb = cb->skb;
1870 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1871 info.nlmsg_flags = NLM_F_MULTI;
1873 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1878 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1879 struct xfrm_policy *xp,
1882 struct xfrm_dump_info info;
1883 struct sk_buff *skb;
1886 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1888 return ERR_PTR(-ENOMEM);
1890 info.in_skb = in_skb;
1892 info.nlmsg_seq = seq;
1893 info.nlmsg_flags = 0;
1895 err = dump_one_policy(xp, dir, 0, &info);
1898 return ERR_PTR(err);
1904 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1905 struct nlattr **attrs)
1907 struct net *net = sock_net(skb->sk);
1908 struct xfrm_policy *xp;
1909 struct xfrm_userpolicy_id *p;
1910 u8 type = XFRM_POLICY_TYPE_MAIN;
1917 p = nlmsg_data(nlh);
1918 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1920 err = copy_from_user_policy_type(&type, attrs);
1924 err = verify_policy_dir(p->dir);
1928 if (attrs[XFRMA_IF_ID])
1929 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1931 xfrm_mark_get(attrs, &m);
1934 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
1935 p->index, delete, &err);
1937 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1938 struct xfrm_sec_ctx *ctx;
1940 err = verify_sec_ctx_len(attrs);
1946 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1948 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1952 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
1953 &p->sel, ctx, delete, &err);
1954 security_xfrm_policy_free(ctx);
1960 struct sk_buff *resp_skb;
1962 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1963 if (IS_ERR(resp_skb)) {
1964 err = PTR_ERR(resp_skb);
1966 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1967 NETLINK_CB(skb).portid);
1970 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1975 c.data.byid = p->index;
1976 c.event = nlh->nlmsg_type;
1977 c.seq = nlh->nlmsg_seq;
1978 c.portid = nlh->nlmsg_pid;
1979 km_policy_notify(xp, p->dir, &c);
1987 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1988 struct nlattr **attrs)
1990 struct net *net = sock_net(skb->sk);
1992 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1995 err = xfrm_state_flush(net, p->proto, true, false);
1997 if (err == -ESRCH) /* empty table */
2001 c.data.proto = p->proto;
2002 c.event = nlh->nlmsg_type;
2003 c.seq = nlh->nlmsg_seq;
2004 c.portid = nlh->nlmsg_pid;
2006 km_state_notify(NULL, &c);
2011 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2013 unsigned int replay_size = x->replay_esn ?
2014 xfrm_replay_state_esn_len(x->replay_esn) :
2015 sizeof(struct xfrm_replay_state);
2017 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2018 + nla_total_size(replay_size)
2019 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2020 + nla_total_size(sizeof(struct xfrm_mark))
2021 + nla_total_size(4) /* XFRM_AE_RTHR */
2022 + nla_total_size(4); /* XFRM_AE_ETHR */
2025 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2027 struct xfrm_aevent_id *id;
2028 struct nlmsghdr *nlh;
2031 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2035 id = nlmsg_data(nlh);
2036 memset(&id->sa_id, 0, sizeof(id->sa_id));
2037 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2038 id->sa_id.spi = x->id.spi;
2039 id->sa_id.family = x->props.family;
2040 id->sa_id.proto = x->id.proto;
2041 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2042 id->reqid = x->props.reqid;
2043 id->flags = c->data.aevent;
2045 if (x->replay_esn) {
2046 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2047 xfrm_replay_state_esn_len(x->replay_esn),
2050 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2055 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2060 if (id->flags & XFRM_AE_RTHR) {
2061 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2065 if (id->flags & XFRM_AE_ETHR) {
2066 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2067 x->replay_maxage * 10 / HZ);
2071 err = xfrm_mark_put(skb, &x->mark);
2075 err = xfrm_if_id_put(skb, x->if_id);
2079 nlmsg_end(skb, nlh);
2083 nlmsg_cancel(skb, nlh);
2087 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2088 struct nlattr **attrs)
2090 struct net *net = sock_net(skb->sk);
2091 struct xfrm_state *x;
2092 struct sk_buff *r_skb;
2097 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2098 struct xfrm_usersa_id *id = &p->sa_id;
2100 mark = xfrm_mark_get(attrs, &m);
2102 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2106 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2107 if (r_skb == NULL) {
2113 * XXX: is this lock really needed - none of the other
2114 * gets lock (the concern is things getting updated
2115 * while we are still reading) - jhs
2117 spin_lock_bh(&x->lock);
2118 c.data.aevent = p->flags;
2119 c.seq = nlh->nlmsg_seq;
2120 c.portid = nlh->nlmsg_pid;
2122 err = build_aevent(r_skb, x, &c);
2125 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2126 spin_unlock_bh(&x->lock);
2131 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2132 struct nlattr **attrs)
2134 struct net *net = sock_net(skb->sk);
2135 struct xfrm_state *x;
2140 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2141 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2142 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2143 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2144 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2145 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2147 if (!lt && !rp && !re && !et && !rt)
2150 /* pedantic mode - thou shalt sayeth replaceth */
2151 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2154 mark = xfrm_mark_get(attrs, &m);
2156 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2160 if (x->km.state != XFRM_STATE_VALID)
2163 err = xfrm_replay_verify_len(x->replay_esn, re);
2167 spin_lock_bh(&x->lock);
2168 xfrm_update_ae_params(x, attrs, 1);
2169 spin_unlock_bh(&x->lock);
2171 c.event = nlh->nlmsg_type;
2172 c.seq = nlh->nlmsg_seq;
2173 c.portid = nlh->nlmsg_pid;
2174 c.data.aevent = XFRM_AE_CU;
2175 km_state_notify(x, &c);
2182 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2183 struct nlattr **attrs)
2185 struct net *net = sock_net(skb->sk);
2187 u8 type = XFRM_POLICY_TYPE_MAIN;
2190 err = copy_from_user_policy_type(&type, attrs);
2194 err = xfrm_policy_flush(net, type, true);
2196 if (err == -ESRCH) /* empty table */
2202 c.event = nlh->nlmsg_type;
2203 c.seq = nlh->nlmsg_seq;
2204 c.portid = nlh->nlmsg_pid;
2206 km_policy_notify(NULL, 0, &c);
2210 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2211 struct nlattr **attrs)
2213 struct net *net = sock_net(skb->sk);
2214 struct xfrm_policy *xp;
2215 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2216 struct xfrm_userpolicy_info *p = &up->pol;
2217 u8 type = XFRM_POLICY_TYPE_MAIN;
2222 err = copy_from_user_policy_type(&type, attrs);
2226 err = verify_policy_dir(p->dir);
2230 if (attrs[XFRMA_IF_ID])
2231 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2233 xfrm_mark_get(attrs, &m);
2236 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2239 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2240 struct xfrm_sec_ctx *ctx;
2242 err = verify_sec_ctx_len(attrs);
2248 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2250 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2254 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2255 &p->sel, ctx, 0, &err);
2256 security_xfrm_policy_free(ctx);
2261 if (unlikely(xp->walk.dead))
2266 xfrm_policy_delete(xp, p->dir);
2267 xfrm_audit_policy_delete(xp, 1, true);
2269 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2276 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2277 struct nlattr **attrs)
2279 struct net *net = sock_net(skb->sk);
2280 struct xfrm_state *x;
2282 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2283 struct xfrm_usersa_info *p = &ue->state;
2285 u32 mark = xfrm_mark_get(attrs, &m);
2287 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2293 spin_lock_bh(&x->lock);
2295 if (x->km.state != XFRM_STATE_VALID)
2297 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2300 __xfrm_state_delete(x);
2301 xfrm_audit_state_delete(x, 1, true);
2305 spin_unlock_bh(&x->lock);
2310 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2311 struct nlattr **attrs)
2313 struct net *net = sock_net(skb->sk);
2314 struct xfrm_policy *xp;
2315 struct xfrm_user_tmpl *ut;
2317 struct nlattr *rt = attrs[XFRMA_TMPL];
2318 struct xfrm_mark mark;
2320 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2321 struct xfrm_state *x = xfrm_state_alloc(net);
2327 xfrm_mark_get(attrs, &mark);
2329 err = verify_newpolicy_info(&ua->policy);
2332 err = verify_sec_ctx_len(attrs);
2337 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2341 memcpy(&x->id, &ua->id, sizeof(ua->id));
2342 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2343 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2344 xp->mark.m = x->mark.m = mark.m;
2345 xp->mark.v = x->mark.v = mark.v;
2347 /* extract the templates and for each call km_key */
2348 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2349 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2350 memcpy(&x->id, &t->id, sizeof(x->id));
2351 x->props.mode = t->mode;
2352 x->props.reqid = t->reqid;
2353 x->props.family = ut->family;
2354 t->aalgos = ua->aalgos;
2355 t->ealgos = ua->ealgos;
2356 t->calgos = ua->calgos;
2357 err = km_query(x, t, xp);
2372 #ifdef CONFIG_XFRM_MIGRATE
2373 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2374 struct xfrm_kmaddress *k,
2375 struct nlattr **attrs, int *num)
2377 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2378 struct xfrm_user_migrate *um;
2382 struct xfrm_user_kmaddress *uk;
2384 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2385 memcpy(&k->local, &uk->local, sizeof(k->local));
2386 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2387 k->family = uk->family;
2388 k->reserved = uk->reserved;
2392 num_migrate = nla_len(rt) / sizeof(*um);
2394 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2397 for (i = 0; i < num_migrate; i++, um++, ma++) {
2398 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2399 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2400 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2401 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2403 ma->proto = um->proto;
2404 ma->mode = um->mode;
2405 ma->reqid = um->reqid;
2407 ma->old_family = um->old_family;
2408 ma->new_family = um->new_family;
2415 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2416 struct nlattr **attrs)
2418 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2419 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2420 struct xfrm_kmaddress km, *kmp;
2424 struct net *net = sock_net(skb->sk);
2425 struct xfrm_encap_tmpl *encap = NULL;
2427 if (attrs[XFRMA_MIGRATE] == NULL)
2430 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2432 err = copy_from_user_policy_type(&type, attrs);
2436 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2443 if (attrs[XFRMA_ENCAP]) {
2444 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2445 sizeof(*encap), GFP_KERNEL);
2450 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2457 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2458 struct nlattr **attrs)
2460 return -ENOPROTOOPT;
2464 #ifdef CONFIG_XFRM_MIGRATE
2465 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2467 struct xfrm_user_migrate um;
2469 memset(&um, 0, sizeof(um));
2470 um.proto = m->proto;
2472 um.reqid = m->reqid;
2473 um.old_family = m->old_family;
2474 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2475 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2476 um.new_family = m->new_family;
2477 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2478 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2480 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2483 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2485 struct xfrm_user_kmaddress uk;
2487 memset(&uk, 0, sizeof(uk));
2488 uk.family = k->family;
2489 uk.reserved = k->reserved;
2490 memcpy(&uk.local, &k->local, sizeof(uk.local));
2491 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2493 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2496 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2499 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2500 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2501 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2502 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2503 + userpolicy_type_attrsize();
2506 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2507 int num_migrate, const struct xfrm_kmaddress *k,
2508 const struct xfrm_selector *sel,
2509 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2511 const struct xfrm_migrate *mp;
2512 struct xfrm_userpolicy_id *pol_id;
2513 struct nlmsghdr *nlh;
2516 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2520 pol_id = nlmsg_data(nlh);
2521 /* copy data from selector, dir, and type to the pol_id */
2522 memset(pol_id, 0, sizeof(*pol_id));
2523 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2527 err = copy_to_user_kmaddress(k, skb);
2532 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2536 err = copy_to_user_policy_type(type, skb);
2539 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2540 err = copy_to_user_migrate(mp, skb);
2545 nlmsg_end(skb, nlh);
2549 nlmsg_cancel(skb, nlh);
2553 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2554 const struct xfrm_migrate *m, int num_migrate,
2555 const struct xfrm_kmaddress *k,
2556 const struct xfrm_encap_tmpl *encap)
2558 struct net *net = &init_net;
2559 struct sk_buff *skb;
2562 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2568 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2571 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2574 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2575 const struct xfrm_migrate *m, int num_migrate,
2576 const struct xfrm_kmaddress *k,
2577 const struct xfrm_encap_tmpl *encap)
2579 return -ENOPROTOOPT;
2583 #define XMSGSIZE(type) sizeof(struct type)
2585 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2586 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2587 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2588 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2589 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2590 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2591 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2592 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2593 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2594 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2595 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2596 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2597 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2598 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2599 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2600 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2601 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2602 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2603 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2604 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2605 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2606 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2608 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2612 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2613 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2614 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2615 [XFRMA_LASTUSED] = { .type = NLA_U64},
2616 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2617 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2618 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2619 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2620 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2621 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2622 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2623 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2624 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2625 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2626 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2627 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2628 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2629 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2630 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2631 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2632 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2633 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2634 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2635 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2636 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2637 [XFRMA_PROTO] = { .type = NLA_U8 },
2638 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2639 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2640 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2641 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2642 [XFRMA_IF_ID] = { .type = NLA_U32 },
2644 EXPORT_SYMBOL_GPL(xfrma_policy);
2646 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2647 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2648 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2651 static const struct xfrm_link {
2652 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2653 int (*start)(struct netlink_callback *);
2654 int (*dump)(struct sk_buff *, struct netlink_callback *);
2655 int (*done)(struct netlink_callback *);
2656 const struct nla_policy *nla_pol;
2658 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2659 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2660 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2661 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2662 .dump = xfrm_dump_sa,
2663 .done = xfrm_dump_sa_done },
2664 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2665 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2666 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2667 .start = xfrm_dump_policy_start,
2668 .dump = xfrm_dump_policy,
2669 .done = xfrm_dump_policy_done },
2670 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2671 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2672 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2673 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2674 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2675 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2676 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2677 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2678 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2679 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2680 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2681 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2682 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2683 .nla_pol = xfrma_spd_policy,
2684 .nla_max = XFRMA_SPD_MAX },
2685 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2688 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2689 struct netlink_ext_ack *extack)
2691 struct net *net = sock_net(skb->sk);
2692 struct nlattr *attrs[XFRMA_MAX+1];
2693 const struct xfrm_link *link;
2694 struct nlmsghdr *nlh64 = NULL;
2697 type = nlh->nlmsg_type;
2698 if (type > XFRM_MSG_MAX)
2701 type -= XFRM_MSG_BASE;
2702 link = &xfrm_dispatch[type];
2704 /* All operations require privileges, even GET */
2705 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2708 if (in_compat_syscall()) {
2709 struct xfrm_translator *xtr = xfrm_get_translator();
2714 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2715 link->nla_pol, extack);
2716 xfrm_put_translator(xtr);
2718 return PTR_ERR(nlh64);
2723 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2724 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2725 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2726 struct netlink_dump_control c = {
2727 .start = link->start,
2732 if (link->dump == NULL) {
2737 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2741 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2742 link->nla_max ? : XFRMA_MAX,
2743 link->nla_pol ? : xfrma_policy, extack);
2747 if (link->doit == NULL) {
2752 err = link->doit(skb, nlh, attrs);
2759 static void xfrm_netlink_rcv(struct sk_buff *skb)
2761 struct net *net = sock_net(skb->sk);
2763 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2764 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2765 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2768 static inline unsigned int xfrm_expire_msgsize(void)
2770 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2771 + nla_total_size(sizeof(struct xfrm_mark));
2774 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2776 struct xfrm_user_expire *ue;
2777 struct nlmsghdr *nlh;
2780 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2784 ue = nlmsg_data(nlh);
2785 copy_to_user_state(x, &ue->state);
2786 ue->hard = (c->data.hard != 0) ? 1 : 0;
2787 /* clear the padding bytes */
2788 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2790 err = xfrm_mark_put(skb, &x->mark);
2794 err = xfrm_if_id_put(skb, x->if_id);
2798 nlmsg_end(skb, nlh);
2802 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2804 struct net *net = xs_net(x);
2805 struct sk_buff *skb;
2807 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2811 if (build_expire(skb, x, c) < 0) {
2816 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2819 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2821 struct net *net = xs_net(x);
2822 struct sk_buff *skb;
2825 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2829 err = build_aevent(skb, x, c);
2832 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2835 static int xfrm_notify_sa_flush(const struct km_event *c)
2837 struct net *net = c->net;
2838 struct xfrm_usersa_flush *p;
2839 struct nlmsghdr *nlh;
2840 struct sk_buff *skb;
2841 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2843 skb = nlmsg_new(len, GFP_ATOMIC);
2847 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2853 p = nlmsg_data(nlh);
2854 p->proto = c->data.proto;
2856 nlmsg_end(skb, nlh);
2858 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2861 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
2865 l += nla_total_size(aead_len(x->aead));
2867 l += nla_total_size(sizeof(struct xfrm_algo) +
2868 (x->aalg->alg_key_len + 7) / 8);
2869 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2872 l += nla_total_size(xfrm_alg_len(x->ealg));
2874 l += nla_total_size(sizeof(*x->calg));
2876 l += nla_total_size(sizeof(*x->encap));
2878 l += nla_total_size(sizeof(x->tfcpad));
2880 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2882 l += nla_total_size(sizeof(struct xfrm_replay_state));
2884 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2885 x->security->ctx_len);
2887 l += nla_total_size(sizeof(*x->coaddr));
2888 if (x->props.extra_flags)
2889 l += nla_total_size(sizeof(x->props.extra_flags));
2891 l += nla_total_size(sizeof(x->xso));
2892 if (x->props.smark.v | x->props.smark.m) {
2893 l += nla_total_size(sizeof(x->props.smark.v));
2894 l += nla_total_size(sizeof(x->props.smark.m));
2897 l += nla_total_size(sizeof(x->if_id));
2899 /* Must count x->lastused as it may become non-zero behind our back. */
2900 l += nla_total_size_64bit(sizeof(u64));
2905 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2907 struct net *net = xs_net(x);
2908 struct xfrm_usersa_info *p;
2909 struct xfrm_usersa_id *id;
2910 struct nlmsghdr *nlh;
2911 struct sk_buff *skb;
2912 unsigned int len = xfrm_sa_len(x);
2913 unsigned int headlen;
2916 headlen = sizeof(*p);
2917 if (c->event == XFRM_MSG_DELSA) {
2918 len += nla_total_size(headlen);
2919 headlen = sizeof(*id);
2920 len += nla_total_size(sizeof(struct xfrm_mark));
2922 len += NLMSG_ALIGN(headlen);
2924 skb = nlmsg_new(len, GFP_ATOMIC);
2928 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2933 p = nlmsg_data(nlh);
2934 if (c->event == XFRM_MSG_DELSA) {
2935 struct nlattr *attr;
2937 id = nlmsg_data(nlh);
2938 memset(id, 0, sizeof(*id));
2939 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2940 id->spi = x->id.spi;
2941 id->family = x->props.family;
2942 id->proto = x->id.proto;
2944 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2951 err = copy_to_user_state_extra(x, p, skb);
2955 nlmsg_end(skb, nlh);
2957 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2964 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2968 case XFRM_MSG_EXPIRE:
2969 return xfrm_exp_state_notify(x, c);
2970 case XFRM_MSG_NEWAE:
2971 return xfrm_aevent_state_notify(x, c);
2972 case XFRM_MSG_DELSA:
2973 case XFRM_MSG_UPDSA:
2974 case XFRM_MSG_NEWSA:
2975 return xfrm_notify_sa(x, c);
2976 case XFRM_MSG_FLUSHSA:
2977 return xfrm_notify_sa_flush(c);
2979 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2988 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
2989 struct xfrm_policy *xp)
2991 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2992 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2993 + nla_total_size(sizeof(struct xfrm_mark))
2994 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2995 + userpolicy_type_attrsize();
2998 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2999 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3001 __u32 seq = xfrm_get_acqseq();
3002 struct xfrm_user_acquire *ua;
3003 struct nlmsghdr *nlh;
3006 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3010 ua = nlmsg_data(nlh);
3011 memcpy(&ua->id, &x->id, sizeof(ua->id));
3012 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3013 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3014 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3015 ua->aalgos = xt->aalgos;
3016 ua->ealgos = xt->ealgos;
3017 ua->calgos = xt->calgos;
3018 ua->seq = x->km.seq = seq;
3020 err = copy_to_user_tmpl(xp, skb);
3022 err = copy_to_user_state_sec_ctx(x, skb);
3024 err = copy_to_user_policy_type(xp->type, skb);
3026 err = xfrm_mark_put(skb, &xp->mark);
3028 err = xfrm_if_id_put(skb, xp->if_id);
3030 nlmsg_cancel(skb, nlh);
3034 nlmsg_end(skb, nlh);
3038 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3039 struct xfrm_policy *xp)
3041 struct net *net = xs_net(x);
3042 struct sk_buff *skb;
3045 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3049 err = build_acquire(skb, x, xt, xp);
3052 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3055 /* User gives us xfrm_user_policy_info followed by an array of 0
3056 * or more templates.
3058 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3059 u8 *data, int len, int *dir)
3061 struct net *net = sock_net(sk);
3062 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3063 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3064 struct xfrm_policy *xp;
3067 switch (sk->sk_family) {
3069 if (opt != IP_XFRM_POLICY) {
3074 #if IS_ENABLED(CONFIG_IPV6)
3076 if (opt != IPV6_XFRM_POLICY) {
3089 if (len < sizeof(*p) ||
3090 verify_newpolicy_info(p))
3093 nr = ((len - sizeof(*p)) / sizeof(*ut));
3094 if (validate_tmpl(nr, ut, p->sel.family))
3097 if (p->dir > XFRM_POLICY_OUT)
3100 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3106 copy_from_user_policy(xp, p);
3107 xp->type = XFRM_POLICY_TYPE_MAIN;
3108 copy_templates(xp, ut, nr);
3115 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3117 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3118 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3119 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3120 + nla_total_size(sizeof(struct xfrm_mark))
3121 + userpolicy_type_attrsize();
3124 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3125 int dir, const struct km_event *c)
3127 struct xfrm_user_polexpire *upe;
3128 int hard = c->data.hard;
3129 struct nlmsghdr *nlh;
3132 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3136 upe = nlmsg_data(nlh);
3137 copy_to_user_policy(xp, &upe->pol, dir);
3138 err = copy_to_user_tmpl(xp, skb);
3140 err = copy_to_user_sec_ctx(xp, skb);
3142 err = copy_to_user_policy_type(xp->type, skb);
3144 err = xfrm_mark_put(skb, &xp->mark);
3146 err = xfrm_if_id_put(skb, xp->if_id);
3148 nlmsg_cancel(skb, nlh);
3153 nlmsg_end(skb, nlh);
3157 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3159 struct net *net = xp_net(xp);
3160 struct sk_buff *skb;
3163 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3167 err = build_polexpire(skb, xp, dir, c);
3170 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3173 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3175 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3176 struct net *net = xp_net(xp);
3177 struct xfrm_userpolicy_info *p;
3178 struct xfrm_userpolicy_id *id;
3179 struct nlmsghdr *nlh;
3180 struct sk_buff *skb;
3181 unsigned int headlen;
3184 headlen = sizeof(*p);
3185 if (c->event == XFRM_MSG_DELPOLICY) {
3186 len += nla_total_size(headlen);
3187 headlen = sizeof(*id);
3189 len += userpolicy_type_attrsize();
3190 len += nla_total_size(sizeof(struct xfrm_mark));
3191 len += NLMSG_ALIGN(headlen);
3193 skb = nlmsg_new(len, GFP_ATOMIC);
3197 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3202 p = nlmsg_data(nlh);
3203 if (c->event == XFRM_MSG_DELPOLICY) {
3204 struct nlattr *attr;
3206 id = nlmsg_data(nlh);
3207 memset(id, 0, sizeof(*id));
3210 id->index = xp->index;
3212 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3214 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3222 copy_to_user_policy(xp, p, dir);
3223 err = copy_to_user_tmpl(xp, skb);
3225 err = copy_to_user_policy_type(xp->type, skb);
3227 err = xfrm_mark_put(skb, &xp->mark);
3229 err = xfrm_if_id_put(skb, xp->if_id);
3233 nlmsg_end(skb, nlh);
3235 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3242 static int xfrm_notify_policy_flush(const struct km_event *c)
3244 struct net *net = c->net;
3245 struct nlmsghdr *nlh;
3246 struct sk_buff *skb;
3249 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3253 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3257 err = copy_to_user_policy_type(c->data.type, skb);
3261 nlmsg_end(skb, nlh);
3263 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3270 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3274 case XFRM_MSG_NEWPOLICY:
3275 case XFRM_MSG_UPDPOLICY:
3276 case XFRM_MSG_DELPOLICY:
3277 return xfrm_notify_policy(xp, dir, c);
3278 case XFRM_MSG_FLUSHPOLICY:
3279 return xfrm_notify_policy_flush(c);
3280 case XFRM_MSG_POLEXPIRE:
3281 return xfrm_exp_policy_notify(xp, dir, c);
3283 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3291 static inline unsigned int xfrm_report_msgsize(void)
3293 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3296 static int build_report(struct sk_buff *skb, u8 proto,
3297 struct xfrm_selector *sel, xfrm_address_t *addr)
3299 struct xfrm_user_report *ur;
3300 struct nlmsghdr *nlh;
3302 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3306 ur = nlmsg_data(nlh);
3308 memcpy(&ur->sel, sel, sizeof(ur->sel));
3311 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3313 nlmsg_cancel(skb, nlh);
3317 nlmsg_end(skb, nlh);
3321 static int xfrm_send_report(struct net *net, u8 proto,
3322 struct xfrm_selector *sel, xfrm_address_t *addr)
3324 struct sk_buff *skb;
3327 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3331 err = build_report(skb, proto, sel, addr);
3334 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3337 static inline unsigned int xfrm_mapping_msgsize(void)
3339 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3342 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3343 xfrm_address_t *new_saddr, __be16 new_sport)
3345 struct xfrm_user_mapping *um;
3346 struct nlmsghdr *nlh;
3348 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3352 um = nlmsg_data(nlh);
3354 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3355 um->id.spi = x->id.spi;
3356 um->id.family = x->props.family;
3357 um->id.proto = x->id.proto;
3358 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3359 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3360 um->new_sport = new_sport;
3361 um->old_sport = x->encap->encap_sport;
3362 um->reqid = x->props.reqid;
3364 nlmsg_end(skb, nlh);
3368 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3371 struct net *net = xs_net(x);
3372 struct sk_buff *skb;
3375 if (x->id.proto != IPPROTO_ESP)
3381 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3385 err = build_mapping(skb, x, ipaddr, sport);
3388 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3391 static bool xfrm_is_alive(const struct km_event *c)
3393 return (bool)xfrm_acquire_is_on(c->net);
3396 static struct xfrm_mgr netlink_mgr = {
3397 .notify = xfrm_send_state_notify,
3398 .acquire = xfrm_send_acquire,
3399 .compile_policy = xfrm_compile_policy,
3400 .notify_policy = xfrm_send_policy_notify,
3401 .report = xfrm_send_report,
3402 .migrate = xfrm_send_migrate,
3403 .new_mapping = xfrm_send_mapping,
3404 .is_alive = xfrm_is_alive,
3407 static int __net_init xfrm_user_net_init(struct net *net)
3410 struct netlink_kernel_cfg cfg = {
3411 .groups = XFRMNLGRP_MAX,
3412 .input = xfrm_netlink_rcv,
3415 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3418 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3419 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3423 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3426 list_for_each_entry(net, net_exit_list, exit_list)
3427 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3429 list_for_each_entry(net, net_exit_list, exit_list)
3430 netlink_kernel_release(net->xfrm.nlsk_stash);
3433 static struct pernet_operations xfrm_user_net_ops = {
3434 .init = xfrm_user_net_init,
3435 .exit_batch = xfrm_user_net_exit,
3438 static int __init xfrm_user_init(void)
3442 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3444 rv = register_pernet_subsys(&xfrm_user_net_ops);
3447 rv = xfrm_register_km(&netlink_mgr);
3449 unregister_pernet_subsys(&xfrm_user_net_ops);
3453 static void __exit xfrm_user_exit(void)
3455 xfrm_unregister_km(&netlink_mgr);
3456 unregister_pernet_subsys(&xfrm_user_net_ops);
3459 module_init(xfrm_user_init);
3460 module_exit(xfrm_user_exit);
3461 MODULE_LICENSE("GPL");
3462 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);