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:
285 if (attrs[XFRMA_MTIMER_THRESH])
286 if (!attrs[XFRMA_ENCAP])
293 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
294 struct xfrm_algo_desc *(*get_byname)(const char *, int),
297 struct xfrm_algo *p, *ualg;
298 struct xfrm_algo_desc *algo;
303 ualg = nla_data(rta);
305 algo = get_byname(ualg->alg_name, 1);
308 *props = algo->desc.sadb_alg_id;
310 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
314 strcpy(p->alg_name, algo->name);
319 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
321 struct xfrm_algo *p, *ualg;
322 struct xfrm_algo_desc *algo;
327 ualg = nla_data(rta);
329 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
332 x->props.ealgo = algo->desc.sadb_alg_id;
334 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
338 strcpy(p->alg_name, algo->name);
340 x->geniv = algo->uinfo.encr.geniv;
344 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
347 struct xfrm_algo *ualg;
348 struct xfrm_algo_auth *p;
349 struct xfrm_algo_desc *algo;
354 ualg = nla_data(rta);
356 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
359 *props = algo->desc.sadb_alg_id;
361 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
365 strcpy(p->alg_name, algo->name);
366 p->alg_key_len = ualg->alg_key_len;
367 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
368 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
374 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
377 struct xfrm_algo_auth *p, *ualg;
378 struct xfrm_algo_desc *algo;
383 ualg = nla_data(rta);
385 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
388 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
390 *props = algo->desc.sadb_alg_id;
392 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
396 strcpy(p->alg_name, algo->name);
397 if (!p->alg_trunc_len)
398 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
404 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
406 struct xfrm_algo_aead *p, *ualg;
407 struct xfrm_algo_desc *algo;
412 ualg = nla_data(rta);
414 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
417 x->props.ealgo = algo->desc.sadb_alg_id;
419 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
423 strcpy(p->alg_name, algo->name);
425 x->geniv = algo->uinfo.aead.geniv;
429 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
432 struct xfrm_replay_state_esn *up;
435 if (!replay_esn || !rp)
439 ulen = xfrm_replay_state_esn_len(up);
441 /* Check the overall length and the internal bitmap length to avoid
442 * potential overflow. */
443 if (nla_len(rp) < (int)ulen ||
444 xfrm_replay_state_esn_len(replay_esn) != ulen ||
445 replay_esn->bmp_len != up->bmp_len)
448 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
454 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
455 struct xfrm_replay_state_esn **preplay_esn,
458 struct xfrm_replay_state_esn *p, *pp, *up;
459 unsigned int klen, ulen;
465 klen = xfrm_replay_state_esn_len(up);
466 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
468 p = kzalloc(klen, GFP_KERNEL);
472 pp = kzalloc(klen, GFP_KERNEL);
479 memcpy(pp, up, ulen);
487 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
489 unsigned int len = 0;
492 len += sizeof(struct xfrm_user_sec_ctx);
493 len += xfrm_ctx->ctx_len;
498 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
500 memcpy(&x->id, &p->id, sizeof(x->id));
501 memcpy(&x->sel, &p->sel, sizeof(x->sel));
502 memcpy(&x->lft, &p->lft, sizeof(x->lft));
503 x->props.mode = p->mode;
504 x->props.replay_window = min_t(unsigned int, p->replay_window,
505 sizeof(x->replay.bitmap) * 8);
506 x->props.reqid = p->reqid;
507 x->props.family = p->family;
508 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
509 x->props.flags = p->flags;
511 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
512 x->sel.family = p->family;
516 * someday when pfkey also has support, we could have the code
517 * somehow made shareable and move it to xfrm_state.c - JHS
520 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
523 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
524 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
525 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
526 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
527 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
528 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
531 struct xfrm_replay_state_esn *replay_esn;
532 replay_esn = nla_data(re);
533 memcpy(x->replay_esn, replay_esn,
534 xfrm_replay_state_esn_len(replay_esn));
535 memcpy(x->preplay_esn, replay_esn,
536 xfrm_replay_state_esn_len(replay_esn));
540 struct xfrm_replay_state *replay;
541 replay = nla_data(rp);
542 memcpy(&x->replay, replay, sizeof(*replay));
543 memcpy(&x->preplay, replay, sizeof(*replay));
547 struct xfrm_lifetime_cur *ltime;
548 ltime = nla_data(lt);
549 x->curlft.bytes = ltime->bytes;
550 x->curlft.packets = ltime->packets;
551 x->curlft.add_time = ltime->add_time;
552 x->curlft.use_time = ltime->use_time;
556 x->replay_maxage = nla_get_u32(et);
559 x->replay_maxdiff = nla_get_u32(rt);
562 x->mapping_maxage = nla_get_u32(mt);
565 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
567 if (attrs[XFRMA_SET_MARK]) {
568 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
569 if (attrs[XFRMA_SET_MARK_MASK])
570 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
578 static struct xfrm_state *xfrm_state_construct(struct net *net,
579 struct xfrm_usersa_info *p,
580 struct nlattr **attrs,
583 struct xfrm_state *x = xfrm_state_alloc(net);
589 copy_from_user_state(x, p);
591 if (attrs[XFRMA_ENCAP]) {
592 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
593 sizeof(*x->encap), GFP_KERNEL);
594 if (x->encap == NULL)
598 if (attrs[XFRMA_COADDR]) {
599 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
600 sizeof(*x->coaddr), GFP_KERNEL);
601 if (x->coaddr == NULL)
605 if (attrs[XFRMA_SA_EXTRA_FLAGS])
606 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
608 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
610 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
611 attrs[XFRMA_ALG_AUTH_TRUNC])))
613 if (!x->props.aalgo) {
614 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
615 attrs[XFRMA_ALG_AUTH])))
618 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
620 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
621 xfrm_calg_get_byname,
622 attrs[XFRMA_ALG_COMP])))
625 if (attrs[XFRMA_TFCPAD])
626 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
628 xfrm_mark_get(attrs, &x->mark);
630 xfrm_smark_init(attrs, &x->props.smark);
632 if (attrs[XFRMA_IF_ID]) {
633 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
640 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
644 if (attrs[XFRMA_SEC_CTX]) {
645 err = security_xfrm_state_alloc(x,
646 nla_data(attrs[XFRMA_SEC_CTX]));
651 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
652 attrs[XFRMA_REPLAY_ESN_VAL])))
656 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
657 /* sysctl_xfrm_aevent_etime is in 100ms units */
658 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
660 if ((err = xfrm_init_replay(x)))
663 /* override default values from above */
664 xfrm_update_ae_params(x, attrs, 0);
666 /* configure the hardware if offload is requested */
667 if (attrs[XFRMA_OFFLOAD_DEV]) {
668 err = xfrm_dev_state_add(net, x,
669 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
677 x->km.state = XFRM_STATE_DEAD;
684 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
685 struct nlattr **attrs)
687 struct net *net = sock_net(skb->sk);
688 struct xfrm_usersa_info *p = nlmsg_data(nlh);
689 struct xfrm_state *x;
693 err = verify_newsa_info(p, attrs);
697 x = xfrm_state_construct(net, p, attrs, &err);
702 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
703 err = xfrm_state_add(x);
705 err = xfrm_state_update(x);
707 xfrm_audit_state_add(x, err ? 0 : 1, true);
710 x->km.state = XFRM_STATE_DEAD;
711 xfrm_dev_state_delete(x);
716 if (x->km.state == XFRM_STATE_VOID)
717 x->km.state = XFRM_STATE_VALID;
719 c.seq = nlh->nlmsg_seq;
720 c.portid = nlh->nlmsg_pid;
721 c.event = nlh->nlmsg_type;
723 km_state_notify(x, &c);
729 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
730 struct xfrm_usersa_id *p,
731 struct nlattr **attrs,
734 struct xfrm_state *x = NULL;
737 u32 mark = xfrm_mark_get(attrs, &m);
739 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
741 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
743 xfrm_address_t *saddr = NULL;
745 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
752 x = xfrm_state_lookup_byaddr(net, mark,
754 p->proto, p->family);
763 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
764 struct nlattr **attrs)
766 struct net *net = sock_net(skb->sk);
767 struct xfrm_state *x;
770 struct xfrm_usersa_id *p = nlmsg_data(nlh);
772 x = xfrm_user_state_lookup(net, p, attrs, &err);
776 if ((err = security_xfrm_state_delete(x)) != 0)
779 if (xfrm_state_kern(x)) {
784 err = xfrm_state_delete(x);
789 c.seq = nlh->nlmsg_seq;
790 c.portid = nlh->nlmsg_pid;
791 c.event = nlh->nlmsg_type;
792 km_state_notify(x, &c);
795 xfrm_audit_state_delete(x, err ? 0 : 1, true);
800 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
802 memset(p, 0, sizeof(*p));
803 memcpy(&p->id, &x->id, sizeof(p->id));
804 memcpy(&p->sel, &x->sel, sizeof(p->sel));
805 memcpy(&p->lft, &x->lft, sizeof(p->lft));
806 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
807 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
808 put_unaligned(x->stats.replay, &p->stats.replay);
809 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
810 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
811 p->mode = x->props.mode;
812 p->replay_window = x->props.replay_window;
813 p->reqid = x->props.reqid;
814 p->family = x->props.family;
815 p->flags = x->props.flags;
819 struct xfrm_dump_info {
820 struct sk_buff *in_skb;
821 struct sk_buff *out_skb;
826 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
828 struct xfrm_user_sec_ctx *uctx;
830 int ctx_size = sizeof(*uctx) + s->ctx_len;
832 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
836 uctx = nla_data(attr);
837 uctx->exttype = XFRMA_SEC_CTX;
838 uctx->len = ctx_size;
839 uctx->ctx_doi = s->ctx_doi;
840 uctx->ctx_alg = s->ctx_alg;
841 uctx->ctx_len = s->ctx_len;
842 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
847 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
849 struct xfrm_user_offload *xuo;
852 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
856 xuo = nla_data(attr);
857 memset(xuo, 0, sizeof(*xuo));
858 xuo->ifindex = xso->dev->ifindex;
859 xuo->flags = xso->flags;
864 static bool xfrm_redact(void)
866 return IS_ENABLED(CONFIG_SECURITY) &&
867 security_locked_down(LOCKDOWN_XFRM_SECRET);
870 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
872 struct xfrm_algo *algo;
873 struct xfrm_algo_auth *ap;
875 bool redact_secret = xfrm_redact();
877 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
878 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
881 algo = nla_data(nla);
882 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
884 if (redact_secret && auth->alg_key_len)
885 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
887 memcpy(algo->alg_key, auth->alg_key,
888 (auth->alg_key_len + 7) / 8);
889 algo->alg_key_len = auth->alg_key_len;
891 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
895 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
896 if (redact_secret && auth->alg_key_len)
897 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
899 memcpy(ap->alg_key, auth->alg_key,
900 (auth->alg_key_len + 7) / 8);
904 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
906 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
907 struct xfrm_algo_aead *ap;
908 bool redact_secret = xfrm_redact();
914 memcpy(ap, aead, sizeof(*aead));
916 if (redact_secret && aead->alg_key_len)
917 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
919 memcpy(ap->alg_key, aead->alg_key,
920 (aead->alg_key_len + 7) / 8);
924 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
926 struct xfrm_algo *ap;
927 bool redact_secret = xfrm_redact();
928 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
934 memcpy(ap, ealg, sizeof(*ealg));
936 if (redact_secret && ealg->alg_key_len)
937 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
939 memcpy(ap->alg_key, ealg->alg_key,
940 (ealg->alg_key_len + 7) / 8);
945 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
950 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
952 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
957 /* Don't change this without updating xfrm_sa_len! */
958 static int copy_to_user_state_extra(struct xfrm_state *x,
959 struct xfrm_usersa_info *p,
964 copy_to_user_state(x, p);
966 if (x->props.extra_flags) {
967 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
968 x->props.extra_flags);
974 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
979 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
985 ret = copy_to_user_aead(x->aead, skb);
990 ret = copy_to_user_auth(x->aalg, skb);
995 ret = copy_to_user_ealg(x->ealg, skb);
1000 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1005 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1010 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1014 ret = xfrm_mark_put(skb, &x->mark);
1018 ret = xfrm_smark_put(skb, &x->props.smark);
1023 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1024 xfrm_replay_state_esn_len(x->replay_esn),
1027 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1032 ret = copy_user_offload(&x->xso, skb);
1036 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1041 ret = copy_sec_ctx(x->security, skb);
1045 if (x->mapping_maxage)
1046 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1051 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1053 struct xfrm_dump_info *sp = ptr;
1054 struct sk_buff *in_skb = sp->in_skb;
1055 struct sk_buff *skb = sp->out_skb;
1056 struct xfrm_translator *xtr;
1057 struct xfrm_usersa_info *p;
1058 struct nlmsghdr *nlh;
1061 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1062 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1066 p = nlmsg_data(nlh);
1068 err = copy_to_user_state_extra(x, p, skb);
1070 nlmsg_cancel(skb, nlh);
1073 nlmsg_end(skb, nlh);
1075 xtr = xfrm_get_translator();
1077 err = xtr->alloc_compat(skb, nlh);
1079 xfrm_put_translator(xtr);
1081 nlmsg_cancel(skb, nlh);
1089 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1091 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1092 struct sock *sk = cb->skb->sk;
1093 struct net *net = sock_net(sk);
1096 xfrm_state_walk_done(walk, net);
1100 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1102 struct net *net = sock_net(skb->sk);
1103 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1104 struct xfrm_dump_info info;
1106 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1107 sizeof(cb->args) - sizeof(cb->args[0]));
1109 info.in_skb = cb->skb;
1111 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1112 info.nlmsg_flags = NLM_F_MULTI;
1115 struct nlattr *attrs[XFRMA_MAX+1];
1116 struct xfrm_address_filter *filter = NULL;
1120 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1121 xfrma_policy, cb->extack);
1125 if (attrs[XFRMA_ADDRESS_FILTER]) {
1126 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1127 sizeof(*filter), GFP_KERNEL);
1132 if (attrs[XFRMA_PROTO])
1133 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1135 xfrm_state_walk_init(walk, proto, filter);
1139 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1144 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1145 struct xfrm_state *x, u32 seq)
1147 struct xfrm_dump_info info;
1148 struct sk_buff *skb;
1151 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1153 return ERR_PTR(-ENOMEM);
1155 info.in_skb = in_skb;
1157 info.nlmsg_seq = seq;
1158 info.nlmsg_flags = 0;
1160 err = dump_one_state(x, 0, &info);
1163 return ERR_PTR(err);
1169 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1170 * Must be called with RCU read lock.
1172 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1173 u32 pid, unsigned int group)
1175 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1176 struct xfrm_translator *xtr;
1183 xtr = xfrm_get_translator();
1185 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1187 xfrm_put_translator(xtr);
1194 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1197 static inline unsigned int xfrm_spdinfo_msgsize(void)
1199 return NLMSG_ALIGN(4)
1200 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1201 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1202 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1203 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1206 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1207 u32 portid, u32 seq, u32 flags)
1209 struct xfrmk_spdinfo si;
1210 struct xfrmu_spdinfo spc;
1211 struct xfrmu_spdhinfo sph;
1212 struct xfrmu_spdhthresh spt4, spt6;
1213 struct nlmsghdr *nlh;
1218 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1219 if (nlh == NULL) /* shouldn't really happen ... */
1222 f = nlmsg_data(nlh);
1224 xfrm_spd_getinfo(net, &si);
1225 spc.incnt = si.incnt;
1226 spc.outcnt = si.outcnt;
1227 spc.fwdcnt = si.fwdcnt;
1228 spc.inscnt = si.inscnt;
1229 spc.outscnt = si.outscnt;
1230 spc.fwdscnt = si.fwdscnt;
1231 sph.spdhcnt = si.spdhcnt;
1232 sph.spdhmcnt = si.spdhmcnt;
1235 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1237 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1238 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1239 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1240 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1241 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1243 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1245 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1247 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1249 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1251 nlmsg_cancel(skb, nlh);
1255 nlmsg_end(skb, nlh);
1259 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1260 struct nlattr **attrs)
1262 struct net *net = sock_net(skb->sk);
1263 struct xfrmu_spdhthresh *thresh4 = NULL;
1264 struct xfrmu_spdhthresh *thresh6 = NULL;
1266 /* selector prefixlen thresholds to hash policies */
1267 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1268 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1270 if (nla_len(rta) < sizeof(*thresh4))
1272 thresh4 = nla_data(rta);
1273 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1276 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1277 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1279 if (nla_len(rta) < sizeof(*thresh6))
1281 thresh6 = nla_data(rta);
1282 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1286 if (thresh4 || thresh6) {
1287 write_seqlock(&net->xfrm.policy_hthresh.lock);
1289 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1290 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1293 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1294 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1296 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1298 xfrm_policy_hash_rebuild(net);
1304 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1305 struct nlattr **attrs)
1307 struct net *net = sock_net(skb->sk);
1308 struct sk_buff *r_skb;
1309 u32 *flags = nlmsg_data(nlh);
1310 u32 sportid = NETLINK_CB(skb).portid;
1311 u32 seq = nlh->nlmsg_seq;
1314 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1318 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1321 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1324 static inline unsigned int xfrm_sadinfo_msgsize(void)
1326 return NLMSG_ALIGN(4)
1327 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1328 + nla_total_size(4); /* XFRMA_SAD_CNT */
1331 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1332 u32 portid, u32 seq, u32 flags)
1334 struct xfrmk_sadinfo si;
1335 struct xfrmu_sadhinfo sh;
1336 struct nlmsghdr *nlh;
1340 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1341 if (nlh == NULL) /* shouldn't really happen ... */
1344 f = nlmsg_data(nlh);
1346 xfrm_sad_getinfo(net, &si);
1348 sh.sadhmcnt = si.sadhmcnt;
1349 sh.sadhcnt = si.sadhcnt;
1351 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1353 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1355 nlmsg_cancel(skb, nlh);
1359 nlmsg_end(skb, nlh);
1363 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1364 struct nlattr **attrs)
1366 struct net *net = sock_net(skb->sk);
1367 struct sk_buff *r_skb;
1368 u32 *flags = nlmsg_data(nlh);
1369 u32 sportid = NETLINK_CB(skb).portid;
1370 u32 seq = nlh->nlmsg_seq;
1373 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1377 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1380 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1383 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1384 struct nlattr **attrs)
1386 struct net *net = sock_net(skb->sk);
1387 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1388 struct xfrm_state *x;
1389 struct sk_buff *resp_skb;
1392 x = xfrm_user_state_lookup(net, p, attrs, &err);
1396 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1397 if (IS_ERR(resp_skb)) {
1398 err = PTR_ERR(resp_skb);
1400 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1407 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1408 struct nlattr **attrs)
1410 struct net *net = sock_net(skb->sk);
1411 struct xfrm_state *x;
1412 struct xfrm_userspi_info *p;
1413 struct xfrm_translator *xtr;
1414 struct sk_buff *resp_skb;
1415 xfrm_address_t *daddr;
1422 p = nlmsg_data(nlh);
1423 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1427 family = p->info.family;
1428 daddr = &p->info.id.daddr;
1432 mark = xfrm_mark_get(attrs, &m);
1434 if (attrs[XFRMA_IF_ID]) {
1435 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1443 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1444 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1451 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1452 if_id, p->info.id.proto, daddr,
1459 err = xfrm_alloc_spi(x, p->min, p->max);
1463 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1464 if (IS_ERR(resp_skb)) {
1465 err = PTR_ERR(resp_skb);
1469 xtr = xfrm_get_translator();
1471 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1473 xfrm_put_translator(xtr);
1475 kfree_skb(resp_skb);
1480 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1488 static int verify_policy_dir(u8 dir)
1491 case XFRM_POLICY_IN:
1492 case XFRM_POLICY_OUT:
1493 case XFRM_POLICY_FWD:
1503 static int verify_policy_type(u8 type)
1506 case XFRM_POLICY_TYPE_MAIN:
1507 #ifdef CONFIG_XFRM_SUB_POLICY
1508 case XFRM_POLICY_TYPE_SUB:
1519 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1524 case XFRM_SHARE_ANY:
1525 case XFRM_SHARE_SESSION:
1526 case XFRM_SHARE_USER:
1527 case XFRM_SHARE_UNIQUE:
1534 switch (p->action) {
1535 case XFRM_POLICY_ALLOW:
1536 case XFRM_POLICY_BLOCK:
1543 switch (p->sel.family) {
1545 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1551 #if IS_ENABLED(CONFIG_IPV6)
1552 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1557 return -EAFNOSUPPORT;
1564 ret = verify_policy_dir(p->dir);
1567 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1573 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1575 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1576 struct xfrm_user_sec_ctx *uctx;
1581 uctx = nla_data(rt);
1582 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1585 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1591 for (i = 0; i < nr; i++, ut++) {
1592 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1594 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1595 memcpy(&t->saddr, &ut->saddr,
1596 sizeof(xfrm_address_t));
1597 t->reqid = ut->reqid;
1599 t->share = ut->share;
1600 t->optional = ut->optional;
1601 t->aalgos = ut->aalgos;
1602 t->ealgos = ut->ealgos;
1603 t->calgos = ut->calgos;
1604 /* If all masks are ~0, then we allow all algorithms. */
1605 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1606 t->encap_family = ut->family;
1610 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1615 if (nr > XFRM_MAX_DEPTH)
1618 prev_family = family;
1620 for (i = 0; i < nr; i++) {
1621 /* We never validated the ut->family value, so many
1622 * applications simply leave it at zero. The check was
1623 * never made and ut->family was ignored because all
1624 * templates could be assumed to have the same family as
1625 * the policy itself. Now that we will have ipv4-in-ipv6
1626 * and ipv6-in-ipv4 tunnels, this is no longer true.
1629 ut[i].family = family;
1631 switch (ut[i].mode) {
1632 case XFRM_MODE_TUNNEL:
1633 case XFRM_MODE_BEET:
1636 if (ut[i].family != prev_family)
1640 if (ut[i].mode >= XFRM_MODE_MAX)
1643 prev_family = ut[i].family;
1645 switch (ut[i].family) {
1648 #if IS_ENABLED(CONFIG_IPV6)
1656 if (!xfrm_id_proto_valid(ut[i].id.proto))
1663 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1665 struct nlattr *rt = attrs[XFRMA_TMPL];
1670 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1671 int nr = nla_len(rt) / sizeof(*utmpl);
1674 err = validate_tmpl(nr, utmpl, pol->family);
1678 copy_templates(pol, utmpl, nr);
1683 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1685 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1686 struct xfrm_userpolicy_type *upt;
1687 u8 type = XFRM_POLICY_TYPE_MAIN;
1695 err = verify_policy_type(type);
1703 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1705 xp->priority = p->priority;
1706 xp->index = p->index;
1707 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1708 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1709 xp->action = p->action;
1710 xp->flags = p->flags;
1711 xp->family = p->sel.family;
1712 /* XXX xp->share = p->share; */
1715 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1717 memset(p, 0, sizeof(*p));
1718 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1719 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1720 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1721 p->priority = xp->priority;
1722 p->index = xp->index;
1723 p->sel.family = xp->family;
1725 p->action = xp->action;
1726 p->flags = xp->flags;
1727 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1730 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1732 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1740 copy_from_user_policy(xp, p);
1742 err = copy_from_user_policy_type(&xp->type, attrs);
1746 if (!(err = copy_from_user_tmpl(xp, attrs)))
1747 err = copy_from_user_sec_ctx(xp, attrs);
1751 xfrm_mark_get(attrs, &xp->mark);
1753 if (attrs[XFRMA_IF_ID]) {
1754 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1765 xfrm_policy_destroy(xp);
1769 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1770 struct nlattr **attrs)
1772 struct net *net = sock_net(skb->sk);
1773 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1774 struct xfrm_policy *xp;
1779 err = verify_newpolicy_info(p);
1782 err = verify_sec_ctx_len(attrs);
1786 xp = xfrm_policy_construct(net, p, attrs, &err);
1790 /* shouldn't excl be based on nlh flags??
1791 * Aha! this is anti-netlink really i.e more pfkey derived
1792 * in netlink excl is a flag and you wouldn't need
1793 * a type XFRM_MSG_UPDPOLICY - JHS */
1794 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1795 err = xfrm_policy_insert(p->dir, xp, excl);
1796 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1799 security_xfrm_policy_free(xp->security);
1804 c.event = nlh->nlmsg_type;
1805 c.seq = nlh->nlmsg_seq;
1806 c.portid = nlh->nlmsg_pid;
1807 km_policy_notify(xp, p->dir, &c);
1814 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1816 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1819 if (xp->xfrm_nr == 0)
1822 for (i = 0; i < xp->xfrm_nr; i++) {
1823 struct xfrm_user_tmpl *up = &vec[i];
1824 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1826 memset(up, 0, sizeof(*up));
1827 memcpy(&up->id, &kp->id, sizeof(up->id));
1828 up->family = kp->encap_family;
1829 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1830 up->reqid = kp->reqid;
1831 up->mode = kp->mode;
1832 up->share = kp->share;
1833 up->optional = kp->optional;
1834 up->aalgos = kp->aalgos;
1835 up->ealgos = kp->ealgos;
1836 up->calgos = kp->calgos;
1839 return nla_put(skb, XFRMA_TMPL,
1840 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1843 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1846 return copy_sec_ctx(x->security, skb);
1851 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1854 return copy_sec_ctx(xp->security, skb);
1857 static inline unsigned int userpolicy_type_attrsize(void)
1859 #ifdef CONFIG_XFRM_SUB_POLICY
1860 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1866 #ifdef CONFIG_XFRM_SUB_POLICY
1867 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1869 struct xfrm_userpolicy_type upt;
1871 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1872 memset(&upt, 0, sizeof(upt));
1875 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1879 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1885 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1887 struct xfrm_dump_info *sp = ptr;
1888 struct xfrm_userpolicy_info *p;
1889 struct sk_buff *in_skb = sp->in_skb;
1890 struct sk_buff *skb = sp->out_skb;
1891 struct xfrm_translator *xtr;
1892 struct nlmsghdr *nlh;
1895 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1896 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1900 p = nlmsg_data(nlh);
1901 copy_to_user_policy(xp, p, dir);
1902 err = copy_to_user_tmpl(xp, skb);
1904 err = copy_to_user_sec_ctx(xp, skb);
1906 err = copy_to_user_policy_type(xp->type, skb);
1908 err = xfrm_mark_put(skb, &xp->mark);
1910 err = xfrm_if_id_put(skb, xp->if_id);
1912 nlmsg_cancel(skb, nlh);
1915 nlmsg_end(skb, nlh);
1917 xtr = xfrm_get_translator();
1919 err = xtr->alloc_compat(skb, nlh);
1921 xfrm_put_translator(xtr);
1923 nlmsg_cancel(skb, nlh);
1931 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1933 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1934 struct net *net = sock_net(cb->skb->sk);
1936 xfrm_policy_walk_done(walk, net);
1940 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1942 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1944 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1946 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1950 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1952 struct net *net = sock_net(skb->sk);
1953 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1954 struct xfrm_dump_info info;
1956 info.in_skb = cb->skb;
1958 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1959 info.nlmsg_flags = NLM_F_MULTI;
1961 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1966 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1967 struct xfrm_policy *xp,
1970 struct xfrm_dump_info info;
1971 struct sk_buff *skb;
1974 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1976 return ERR_PTR(-ENOMEM);
1978 info.in_skb = in_skb;
1980 info.nlmsg_seq = seq;
1981 info.nlmsg_flags = 0;
1983 err = dump_one_policy(xp, dir, 0, &info);
1986 return ERR_PTR(err);
1992 static int xfrm_notify_userpolicy(struct net *net)
1994 struct xfrm_userpolicy_default *up;
1995 int len = NLMSG_ALIGN(sizeof(*up));
1996 struct nlmsghdr *nlh;
1997 struct sk_buff *skb;
2000 skb = nlmsg_new(len, GFP_ATOMIC);
2004 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2010 up = nlmsg_data(nlh);
2011 up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2012 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2013 up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2014 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2015 up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2016 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2018 nlmsg_end(skb, nlh);
2021 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2027 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2028 struct nlattr **attrs)
2030 struct net *net = sock_net(skb->sk);
2031 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2033 if (up->in == XFRM_USERPOLICY_BLOCK)
2034 net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
2035 else if (up->in == XFRM_USERPOLICY_ACCEPT)
2036 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
2038 if (up->fwd == XFRM_USERPOLICY_BLOCK)
2039 net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
2040 else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
2041 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
2043 if (up->out == XFRM_USERPOLICY_BLOCK)
2044 net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
2045 else if (up->out == XFRM_USERPOLICY_ACCEPT)
2046 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
2048 rt_genid_bump_all(net);
2050 xfrm_notify_userpolicy(net);
2054 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2055 struct nlattr **attrs)
2057 struct sk_buff *r_skb;
2058 struct nlmsghdr *r_nlh;
2059 struct net *net = sock_net(skb->sk);
2060 struct xfrm_userpolicy_default *r_up;
2061 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2062 u32 portid = NETLINK_CB(skb).portid;
2063 u32 seq = nlh->nlmsg_seq;
2065 r_skb = nlmsg_new(len, GFP_ATOMIC);
2069 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2075 r_up = nlmsg_data(r_nlh);
2077 r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2078 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2079 r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2080 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2081 r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2082 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2083 nlmsg_end(r_skb, r_nlh);
2085 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2088 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2089 struct nlattr **attrs)
2091 struct net *net = sock_net(skb->sk);
2092 struct xfrm_policy *xp;
2093 struct xfrm_userpolicy_id *p;
2094 u8 type = XFRM_POLICY_TYPE_MAIN;
2101 p = nlmsg_data(nlh);
2102 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2104 err = copy_from_user_policy_type(&type, attrs);
2108 err = verify_policy_dir(p->dir);
2112 if (attrs[XFRMA_IF_ID])
2113 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2115 xfrm_mark_get(attrs, &m);
2118 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2119 p->index, delete, &err);
2121 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2122 struct xfrm_sec_ctx *ctx;
2124 err = verify_sec_ctx_len(attrs);
2130 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2132 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2136 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2137 &p->sel, ctx, delete, &err);
2138 security_xfrm_policy_free(ctx);
2144 struct sk_buff *resp_skb;
2146 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2147 if (IS_ERR(resp_skb)) {
2148 err = PTR_ERR(resp_skb);
2150 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2151 NETLINK_CB(skb).portid);
2154 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2159 c.data.byid = p->index;
2160 c.event = nlh->nlmsg_type;
2161 c.seq = nlh->nlmsg_seq;
2162 c.portid = nlh->nlmsg_pid;
2163 km_policy_notify(xp, p->dir, &c);
2171 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2172 struct nlattr **attrs)
2174 struct net *net = sock_net(skb->sk);
2176 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2179 err = xfrm_state_flush(net, p->proto, true, false);
2181 if (err == -ESRCH) /* empty table */
2185 c.data.proto = p->proto;
2186 c.event = nlh->nlmsg_type;
2187 c.seq = nlh->nlmsg_seq;
2188 c.portid = nlh->nlmsg_pid;
2190 km_state_notify(NULL, &c);
2195 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2197 unsigned int replay_size = x->replay_esn ?
2198 xfrm_replay_state_esn_len(x->replay_esn) :
2199 sizeof(struct xfrm_replay_state);
2201 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2202 + nla_total_size(replay_size)
2203 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2204 + nla_total_size(sizeof(struct xfrm_mark))
2205 + nla_total_size(4) /* XFRM_AE_RTHR */
2206 + nla_total_size(4); /* XFRM_AE_ETHR */
2209 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2211 struct xfrm_aevent_id *id;
2212 struct nlmsghdr *nlh;
2215 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2219 id = nlmsg_data(nlh);
2220 memset(&id->sa_id, 0, sizeof(id->sa_id));
2221 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2222 id->sa_id.spi = x->id.spi;
2223 id->sa_id.family = x->props.family;
2224 id->sa_id.proto = x->id.proto;
2225 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2226 id->reqid = x->props.reqid;
2227 id->flags = c->data.aevent;
2229 if (x->replay_esn) {
2230 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2231 xfrm_replay_state_esn_len(x->replay_esn),
2234 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2239 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2244 if (id->flags & XFRM_AE_RTHR) {
2245 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2249 if (id->flags & XFRM_AE_ETHR) {
2250 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2251 x->replay_maxage * 10 / HZ);
2255 err = xfrm_mark_put(skb, &x->mark);
2259 err = xfrm_if_id_put(skb, x->if_id);
2263 nlmsg_end(skb, nlh);
2267 nlmsg_cancel(skb, nlh);
2271 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2272 struct nlattr **attrs)
2274 struct net *net = sock_net(skb->sk);
2275 struct xfrm_state *x;
2276 struct sk_buff *r_skb;
2281 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2282 struct xfrm_usersa_id *id = &p->sa_id;
2284 mark = xfrm_mark_get(attrs, &m);
2286 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2290 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2291 if (r_skb == NULL) {
2297 * XXX: is this lock really needed - none of the other
2298 * gets lock (the concern is things getting updated
2299 * while we are still reading) - jhs
2301 spin_lock_bh(&x->lock);
2302 c.data.aevent = p->flags;
2303 c.seq = nlh->nlmsg_seq;
2304 c.portid = nlh->nlmsg_pid;
2306 err = build_aevent(r_skb, x, &c);
2309 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2310 spin_unlock_bh(&x->lock);
2315 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2316 struct nlattr **attrs)
2318 struct net *net = sock_net(skb->sk);
2319 struct xfrm_state *x;
2324 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2325 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2326 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2327 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2328 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2329 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2331 if (!lt && !rp && !re && !et && !rt)
2334 /* pedantic mode - thou shalt sayeth replaceth */
2335 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2338 mark = xfrm_mark_get(attrs, &m);
2340 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2344 if (x->km.state != XFRM_STATE_VALID)
2347 err = xfrm_replay_verify_len(x->replay_esn, re);
2351 spin_lock_bh(&x->lock);
2352 xfrm_update_ae_params(x, attrs, 1);
2353 spin_unlock_bh(&x->lock);
2355 c.event = nlh->nlmsg_type;
2356 c.seq = nlh->nlmsg_seq;
2357 c.portid = nlh->nlmsg_pid;
2358 c.data.aevent = XFRM_AE_CU;
2359 km_state_notify(x, &c);
2366 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2367 struct nlattr **attrs)
2369 struct net *net = sock_net(skb->sk);
2371 u8 type = XFRM_POLICY_TYPE_MAIN;
2374 err = copy_from_user_policy_type(&type, attrs);
2378 err = xfrm_policy_flush(net, type, true);
2380 if (err == -ESRCH) /* empty table */
2386 c.event = nlh->nlmsg_type;
2387 c.seq = nlh->nlmsg_seq;
2388 c.portid = nlh->nlmsg_pid;
2390 km_policy_notify(NULL, 0, &c);
2394 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2395 struct nlattr **attrs)
2397 struct net *net = sock_net(skb->sk);
2398 struct xfrm_policy *xp;
2399 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2400 struct xfrm_userpolicy_info *p = &up->pol;
2401 u8 type = XFRM_POLICY_TYPE_MAIN;
2406 err = copy_from_user_policy_type(&type, attrs);
2410 err = verify_policy_dir(p->dir);
2414 if (attrs[XFRMA_IF_ID])
2415 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2417 xfrm_mark_get(attrs, &m);
2420 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2423 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2424 struct xfrm_sec_ctx *ctx;
2426 err = verify_sec_ctx_len(attrs);
2432 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2434 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2438 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2439 &p->sel, ctx, 0, &err);
2440 security_xfrm_policy_free(ctx);
2445 if (unlikely(xp->walk.dead))
2450 xfrm_policy_delete(xp, p->dir);
2451 xfrm_audit_policy_delete(xp, 1, true);
2453 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2460 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2461 struct nlattr **attrs)
2463 struct net *net = sock_net(skb->sk);
2464 struct xfrm_state *x;
2466 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2467 struct xfrm_usersa_info *p = &ue->state;
2469 u32 mark = xfrm_mark_get(attrs, &m);
2471 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2477 spin_lock_bh(&x->lock);
2479 if (x->km.state != XFRM_STATE_VALID)
2481 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2484 __xfrm_state_delete(x);
2485 xfrm_audit_state_delete(x, 1, true);
2489 spin_unlock_bh(&x->lock);
2494 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2495 struct nlattr **attrs)
2497 struct net *net = sock_net(skb->sk);
2498 struct xfrm_policy *xp;
2499 struct xfrm_user_tmpl *ut;
2501 struct nlattr *rt = attrs[XFRMA_TMPL];
2502 struct xfrm_mark mark;
2504 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2505 struct xfrm_state *x = xfrm_state_alloc(net);
2511 xfrm_mark_get(attrs, &mark);
2513 err = verify_newpolicy_info(&ua->policy);
2516 err = verify_sec_ctx_len(attrs);
2521 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2525 memcpy(&x->id, &ua->id, sizeof(ua->id));
2526 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2527 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2528 xp->mark.m = x->mark.m = mark.m;
2529 xp->mark.v = x->mark.v = mark.v;
2531 /* extract the templates and for each call km_key */
2532 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2533 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2534 memcpy(&x->id, &t->id, sizeof(x->id));
2535 x->props.mode = t->mode;
2536 x->props.reqid = t->reqid;
2537 x->props.family = ut->family;
2538 t->aalgos = ua->aalgos;
2539 t->ealgos = ua->ealgos;
2540 t->calgos = ua->calgos;
2541 err = km_query(x, t, xp);
2556 #ifdef CONFIG_XFRM_MIGRATE
2557 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2558 struct xfrm_kmaddress *k,
2559 struct nlattr **attrs, int *num)
2561 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2562 struct xfrm_user_migrate *um;
2566 struct xfrm_user_kmaddress *uk;
2568 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2569 memcpy(&k->local, &uk->local, sizeof(k->local));
2570 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2571 k->family = uk->family;
2572 k->reserved = uk->reserved;
2576 num_migrate = nla_len(rt) / sizeof(*um);
2578 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2581 for (i = 0; i < num_migrate; i++, um++, ma++) {
2582 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2583 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2584 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2585 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2587 ma->proto = um->proto;
2588 ma->mode = um->mode;
2589 ma->reqid = um->reqid;
2591 ma->old_family = um->old_family;
2592 ma->new_family = um->new_family;
2599 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2600 struct nlattr **attrs)
2602 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2603 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2604 struct xfrm_kmaddress km, *kmp;
2608 struct net *net = sock_net(skb->sk);
2609 struct xfrm_encap_tmpl *encap = NULL;
2611 if (attrs[XFRMA_MIGRATE] == NULL)
2614 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2616 err = copy_from_user_policy_type(&type, attrs);
2620 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2627 if (attrs[XFRMA_ENCAP]) {
2628 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2629 sizeof(*encap), GFP_KERNEL);
2634 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2641 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2642 struct nlattr **attrs)
2644 return -ENOPROTOOPT;
2648 #ifdef CONFIG_XFRM_MIGRATE
2649 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2651 struct xfrm_user_migrate um;
2653 memset(&um, 0, sizeof(um));
2654 um.proto = m->proto;
2656 um.reqid = m->reqid;
2657 um.old_family = m->old_family;
2658 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2659 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2660 um.new_family = m->new_family;
2661 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2662 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2664 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2667 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2669 struct xfrm_user_kmaddress uk;
2671 memset(&uk, 0, sizeof(uk));
2672 uk.family = k->family;
2673 uk.reserved = k->reserved;
2674 memcpy(&uk.local, &k->local, sizeof(uk.local));
2675 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2677 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2680 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2683 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2684 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2685 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2686 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2687 + userpolicy_type_attrsize();
2690 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2691 int num_migrate, const struct xfrm_kmaddress *k,
2692 const struct xfrm_selector *sel,
2693 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2695 const struct xfrm_migrate *mp;
2696 struct xfrm_userpolicy_id *pol_id;
2697 struct nlmsghdr *nlh;
2700 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2704 pol_id = nlmsg_data(nlh);
2705 /* copy data from selector, dir, and type to the pol_id */
2706 memset(pol_id, 0, sizeof(*pol_id));
2707 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2711 err = copy_to_user_kmaddress(k, skb);
2716 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2720 err = copy_to_user_policy_type(type, skb);
2723 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2724 err = copy_to_user_migrate(mp, skb);
2729 nlmsg_end(skb, nlh);
2733 nlmsg_cancel(skb, nlh);
2737 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2738 const struct xfrm_migrate *m, int num_migrate,
2739 const struct xfrm_kmaddress *k,
2740 const struct xfrm_encap_tmpl *encap)
2742 struct net *net = &init_net;
2743 struct sk_buff *skb;
2746 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2752 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2755 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2758 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2759 const struct xfrm_migrate *m, int num_migrate,
2760 const struct xfrm_kmaddress *k,
2761 const struct xfrm_encap_tmpl *encap)
2763 return -ENOPROTOOPT;
2767 #define XMSGSIZE(type) sizeof(struct type)
2769 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2770 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2771 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2772 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2773 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2774 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2775 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2776 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2777 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2778 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2779 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2780 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2781 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2782 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2783 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2784 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2785 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2786 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2787 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2788 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2789 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2790 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2791 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2792 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2794 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2798 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2799 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2800 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2801 [XFRMA_LASTUSED] = { .type = NLA_U64},
2802 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2803 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2804 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2805 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2806 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2807 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2808 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2809 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2810 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2811 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2812 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2813 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2814 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2815 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2816 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2817 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2818 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2819 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2820 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2821 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2822 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2823 [XFRMA_PROTO] = { .type = NLA_U8 },
2824 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2825 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2826 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2827 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2828 [XFRMA_IF_ID] = { .type = NLA_U32 },
2830 EXPORT_SYMBOL_GPL(xfrma_policy);
2832 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2833 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2834 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2837 static const struct xfrm_link {
2838 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2839 int (*start)(struct netlink_callback *);
2840 int (*dump)(struct sk_buff *, struct netlink_callback *);
2841 int (*done)(struct netlink_callback *);
2842 const struct nla_policy *nla_pol;
2844 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2845 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2846 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2847 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2848 .dump = xfrm_dump_sa,
2849 .done = xfrm_dump_sa_done },
2850 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2851 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2852 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2853 .start = xfrm_dump_policy_start,
2854 .dump = xfrm_dump_policy,
2855 .done = xfrm_dump_policy_done },
2856 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2857 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2858 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2859 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2860 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2861 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2862 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2863 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2864 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2865 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2866 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2867 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2868 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2869 .nla_pol = xfrma_spd_policy,
2870 .nla_max = XFRMA_SPD_MAX },
2871 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2872 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2873 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2876 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2877 struct netlink_ext_ack *extack)
2879 struct net *net = sock_net(skb->sk);
2880 struct nlattr *attrs[XFRMA_MAX+1];
2881 const struct xfrm_link *link;
2882 struct nlmsghdr *nlh64 = NULL;
2885 type = nlh->nlmsg_type;
2886 if (type > XFRM_MSG_MAX)
2889 type -= XFRM_MSG_BASE;
2890 link = &xfrm_dispatch[type];
2892 /* All operations require privileges, even GET */
2893 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2896 if (in_compat_syscall()) {
2897 struct xfrm_translator *xtr = xfrm_get_translator();
2902 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2903 link->nla_pol, extack);
2904 xfrm_put_translator(xtr);
2906 return PTR_ERR(nlh64);
2911 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2912 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2913 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2914 struct netlink_dump_control c = {
2915 .start = link->start,
2920 if (link->dump == NULL) {
2925 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2929 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2930 link->nla_max ? : XFRMA_MAX,
2931 link->nla_pol ? : xfrma_policy, extack);
2935 if (link->doit == NULL) {
2940 err = link->doit(skb, nlh, attrs);
2942 /* We need to free skb allocated in xfrm_alloc_compat() before
2943 * returning from this function, because consume_skb() won't take
2944 * care of frag_list since netlink destructor sets
2945 * sbk->head to NULL. (see netlink_skb_destructor())
2947 if (skb_has_frag_list(skb)) {
2948 kfree_skb(skb_shinfo(skb)->frag_list);
2949 skb_shinfo(skb)->frag_list = NULL;
2957 static void xfrm_netlink_rcv(struct sk_buff *skb)
2959 struct net *net = sock_net(skb->sk);
2961 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2962 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2963 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2966 static inline unsigned int xfrm_expire_msgsize(void)
2968 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2969 + nla_total_size(sizeof(struct xfrm_mark));
2972 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2974 struct xfrm_user_expire *ue;
2975 struct nlmsghdr *nlh;
2978 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2982 ue = nlmsg_data(nlh);
2983 copy_to_user_state(x, &ue->state);
2984 ue->hard = (c->data.hard != 0) ? 1 : 0;
2985 /* clear the padding bytes */
2986 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2988 err = xfrm_mark_put(skb, &x->mark);
2992 err = xfrm_if_id_put(skb, x->if_id);
2996 nlmsg_end(skb, nlh);
3000 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3002 struct net *net = xs_net(x);
3003 struct sk_buff *skb;
3005 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3009 if (build_expire(skb, x, c) < 0) {
3014 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3017 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3019 struct net *net = xs_net(x);
3020 struct sk_buff *skb;
3023 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3027 err = build_aevent(skb, x, c);
3030 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3033 static int xfrm_notify_sa_flush(const struct km_event *c)
3035 struct net *net = c->net;
3036 struct xfrm_usersa_flush *p;
3037 struct nlmsghdr *nlh;
3038 struct sk_buff *skb;
3039 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3041 skb = nlmsg_new(len, GFP_ATOMIC);
3045 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3051 p = nlmsg_data(nlh);
3052 p->proto = c->data.proto;
3054 nlmsg_end(skb, nlh);
3056 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3059 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3063 l += nla_total_size(aead_len(x->aead));
3065 l += nla_total_size(sizeof(struct xfrm_algo) +
3066 (x->aalg->alg_key_len + 7) / 8);
3067 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3070 l += nla_total_size(xfrm_alg_len(x->ealg));
3072 l += nla_total_size(sizeof(*x->calg));
3074 l += nla_total_size(sizeof(*x->encap));
3076 l += nla_total_size(sizeof(x->tfcpad));
3078 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3080 l += nla_total_size(sizeof(struct xfrm_replay_state));
3082 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3083 x->security->ctx_len);
3085 l += nla_total_size(sizeof(*x->coaddr));
3086 if (x->props.extra_flags)
3087 l += nla_total_size(sizeof(x->props.extra_flags));
3089 l += nla_total_size(sizeof(struct xfrm_user_offload));
3090 if (x->props.smark.v | x->props.smark.m) {
3091 l += nla_total_size(sizeof(x->props.smark.v));
3092 l += nla_total_size(sizeof(x->props.smark.m));
3095 l += nla_total_size(sizeof(x->if_id));
3097 /* Must count x->lastused as it may become non-zero behind our back. */
3098 l += nla_total_size_64bit(sizeof(u64));
3100 if (x->mapping_maxage)
3101 l += nla_total_size(sizeof(x->mapping_maxage));
3106 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3108 struct net *net = xs_net(x);
3109 struct xfrm_usersa_info *p;
3110 struct xfrm_usersa_id *id;
3111 struct nlmsghdr *nlh;
3112 struct sk_buff *skb;
3113 unsigned int len = xfrm_sa_len(x);
3114 unsigned int headlen;
3117 headlen = sizeof(*p);
3118 if (c->event == XFRM_MSG_DELSA) {
3119 len += nla_total_size(headlen);
3120 headlen = sizeof(*id);
3121 len += nla_total_size(sizeof(struct xfrm_mark));
3123 len += NLMSG_ALIGN(headlen);
3125 skb = nlmsg_new(len, GFP_ATOMIC);
3129 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3134 p = nlmsg_data(nlh);
3135 if (c->event == XFRM_MSG_DELSA) {
3136 struct nlattr *attr;
3138 id = nlmsg_data(nlh);
3139 memset(id, 0, sizeof(*id));
3140 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3141 id->spi = x->id.spi;
3142 id->family = x->props.family;
3143 id->proto = x->id.proto;
3145 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3152 err = copy_to_user_state_extra(x, p, skb);
3156 nlmsg_end(skb, nlh);
3158 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3165 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3169 case XFRM_MSG_EXPIRE:
3170 return xfrm_exp_state_notify(x, c);
3171 case XFRM_MSG_NEWAE:
3172 return xfrm_aevent_state_notify(x, c);
3173 case XFRM_MSG_DELSA:
3174 case XFRM_MSG_UPDSA:
3175 case XFRM_MSG_NEWSA:
3176 return xfrm_notify_sa(x, c);
3177 case XFRM_MSG_FLUSHSA:
3178 return xfrm_notify_sa_flush(c);
3180 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3189 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3190 struct xfrm_policy *xp)
3192 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3193 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3194 + nla_total_size(sizeof(struct xfrm_mark))
3195 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3196 + userpolicy_type_attrsize();
3199 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3200 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3202 __u32 seq = xfrm_get_acqseq();
3203 struct xfrm_user_acquire *ua;
3204 struct nlmsghdr *nlh;
3207 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3211 ua = nlmsg_data(nlh);
3212 memcpy(&ua->id, &x->id, sizeof(ua->id));
3213 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3214 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3215 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3216 ua->aalgos = xt->aalgos;
3217 ua->ealgos = xt->ealgos;
3218 ua->calgos = xt->calgos;
3219 ua->seq = x->km.seq = seq;
3221 err = copy_to_user_tmpl(xp, skb);
3223 err = copy_to_user_state_sec_ctx(x, 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);
3231 nlmsg_cancel(skb, nlh);
3235 nlmsg_end(skb, nlh);
3239 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3240 struct xfrm_policy *xp)
3242 struct net *net = xs_net(x);
3243 struct sk_buff *skb;
3246 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3250 err = build_acquire(skb, x, xt, xp);
3253 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3256 /* User gives us xfrm_user_policy_info followed by an array of 0
3257 * or more templates.
3259 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3260 u8 *data, int len, int *dir)
3262 struct net *net = sock_net(sk);
3263 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3264 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3265 struct xfrm_policy *xp;
3268 switch (sk->sk_family) {
3270 if (opt != IP_XFRM_POLICY) {
3275 #if IS_ENABLED(CONFIG_IPV6)
3277 if (opt != IPV6_XFRM_POLICY) {
3290 if (len < sizeof(*p) ||
3291 verify_newpolicy_info(p))
3294 nr = ((len - sizeof(*p)) / sizeof(*ut));
3295 if (validate_tmpl(nr, ut, p->sel.family))
3298 if (p->dir > XFRM_POLICY_OUT)
3301 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3307 copy_from_user_policy(xp, p);
3308 xp->type = XFRM_POLICY_TYPE_MAIN;
3309 copy_templates(xp, ut, nr);
3316 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3318 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3319 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3320 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3321 + nla_total_size(sizeof(struct xfrm_mark))
3322 + userpolicy_type_attrsize();
3325 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3326 int dir, const struct km_event *c)
3328 struct xfrm_user_polexpire *upe;
3329 int hard = c->data.hard;
3330 struct nlmsghdr *nlh;
3333 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3337 upe = nlmsg_data(nlh);
3338 copy_to_user_policy(xp, &upe->pol, dir);
3339 err = copy_to_user_tmpl(xp, skb);
3341 err = copy_to_user_sec_ctx(xp, skb);
3343 err = copy_to_user_policy_type(xp->type, skb);
3345 err = xfrm_mark_put(skb, &xp->mark);
3347 err = xfrm_if_id_put(skb, xp->if_id);
3349 nlmsg_cancel(skb, nlh);
3354 nlmsg_end(skb, nlh);
3358 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3360 struct net *net = xp_net(xp);
3361 struct sk_buff *skb;
3364 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3368 err = build_polexpire(skb, xp, dir, c);
3371 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3374 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3376 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3377 struct net *net = xp_net(xp);
3378 struct xfrm_userpolicy_info *p;
3379 struct xfrm_userpolicy_id *id;
3380 struct nlmsghdr *nlh;
3381 struct sk_buff *skb;
3382 unsigned int headlen;
3385 headlen = sizeof(*p);
3386 if (c->event == XFRM_MSG_DELPOLICY) {
3387 len += nla_total_size(headlen);
3388 headlen = sizeof(*id);
3390 len += userpolicy_type_attrsize();
3391 len += nla_total_size(sizeof(struct xfrm_mark));
3392 len += NLMSG_ALIGN(headlen);
3394 skb = nlmsg_new(len, GFP_ATOMIC);
3398 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3403 p = nlmsg_data(nlh);
3404 if (c->event == XFRM_MSG_DELPOLICY) {
3405 struct nlattr *attr;
3407 id = nlmsg_data(nlh);
3408 memset(id, 0, sizeof(*id));
3411 id->index = xp->index;
3413 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3415 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3423 copy_to_user_policy(xp, p, dir);
3424 err = copy_to_user_tmpl(xp, skb);
3426 err = copy_to_user_policy_type(xp->type, skb);
3428 err = xfrm_mark_put(skb, &xp->mark);
3430 err = xfrm_if_id_put(skb, xp->if_id);
3434 nlmsg_end(skb, nlh);
3436 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3443 static int xfrm_notify_policy_flush(const struct km_event *c)
3445 struct net *net = c->net;
3446 struct nlmsghdr *nlh;
3447 struct sk_buff *skb;
3450 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3454 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3458 err = copy_to_user_policy_type(c->data.type, skb);
3462 nlmsg_end(skb, nlh);
3464 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3471 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3475 case XFRM_MSG_NEWPOLICY:
3476 case XFRM_MSG_UPDPOLICY:
3477 case XFRM_MSG_DELPOLICY:
3478 return xfrm_notify_policy(xp, dir, c);
3479 case XFRM_MSG_FLUSHPOLICY:
3480 return xfrm_notify_policy_flush(c);
3481 case XFRM_MSG_POLEXPIRE:
3482 return xfrm_exp_policy_notify(xp, dir, c);
3484 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3492 static inline unsigned int xfrm_report_msgsize(void)
3494 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3497 static int build_report(struct sk_buff *skb, u8 proto,
3498 struct xfrm_selector *sel, xfrm_address_t *addr)
3500 struct xfrm_user_report *ur;
3501 struct nlmsghdr *nlh;
3503 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3507 ur = nlmsg_data(nlh);
3509 memcpy(&ur->sel, sel, sizeof(ur->sel));
3512 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3514 nlmsg_cancel(skb, nlh);
3518 nlmsg_end(skb, nlh);
3522 static int xfrm_send_report(struct net *net, u8 proto,
3523 struct xfrm_selector *sel, xfrm_address_t *addr)
3525 struct sk_buff *skb;
3528 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3532 err = build_report(skb, proto, sel, addr);
3535 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3538 static inline unsigned int xfrm_mapping_msgsize(void)
3540 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3543 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3544 xfrm_address_t *new_saddr, __be16 new_sport)
3546 struct xfrm_user_mapping *um;
3547 struct nlmsghdr *nlh;
3549 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3553 um = nlmsg_data(nlh);
3555 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3556 um->id.spi = x->id.spi;
3557 um->id.family = x->props.family;
3558 um->id.proto = x->id.proto;
3559 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3560 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3561 um->new_sport = new_sport;
3562 um->old_sport = x->encap->encap_sport;
3563 um->reqid = x->props.reqid;
3565 nlmsg_end(skb, nlh);
3569 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3572 struct net *net = xs_net(x);
3573 struct sk_buff *skb;
3576 if (x->id.proto != IPPROTO_ESP)
3582 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3586 err = build_mapping(skb, x, ipaddr, sport);
3589 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3592 static bool xfrm_is_alive(const struct km_event *c)
3594 return (bool)xfrm_acquire_is_on(c->net);
3597 static struct xfrm_mgr netlink_mgr = {
3598 .notify = xfrm_send_state_notify,
3599 .acquire = xfrm_send_acquire,
3600 .compile_policy = xfrm_compile_policy,
3601 .notify_policy = xfrm_send_policy_notify,
3602 .report = xfrm_send_report,
3603 .migrate = xfrm_send_migrate,
3604 .new_mapping = xfrm_send_mapping,
3605 .is_alive = xfrm_is_alive,
3608 static int __net_init xfrm_user_net_init(struct net *net)
3611 struct netlink_kernel_cfg cfg = {
3612 .groups = XFRMNLGRP_MAX,
3613 .input = xfrm_netlink_rcv,
3616 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3619 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3620 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3624 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3626 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3629 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3633 list_for_each_entry(net, net_exit_list, exit_list)
3634 netlink_kernel_release(net->xfrm.nlsk_stash);
3637 static struct pernet_operations xfrm_user_net_ops = {
3638 .init = xfrm_user_net_init,
3639 .pre_exit = xfrm_user_net_pre_exit,
3640 .exit_batch = xfrm_user_net_exit,
3643 static int __init xfrm_user_init(void)
3647 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3649 rv = register_pernet_subsys(&xfrm_user_net_ops);
3652 rv = xfrm_register_km(&netlink_mgr);
3654 unregister_pernet_subsys(&xfrm_user_net_ops);
3658 static void __exit xfrm_user_exit(void)
3660 xfrm_unregister_km(&netlink_mgr);
3661 unregister_pernet_subsys(&xfrm_user_net_ops);
3664 module_init(xfrm_user_init);
3665 module_exit(xfrm_user_exit);
3666 MODULE_LICENSE("GPL");
3667 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);