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_ENCAP]) {
584 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
585 sizeof(*x->encap), GFP_KERNEL);
586 if (x->encap == NULL)
590 if (attrs[XFRMA_COADDR]) {
591 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
592 sizeof(*x->coaddr), GFP_KERNEL);
593 if (x->coaddr == NULL)
597 if (attrs[XFRMA_SA_EXTRA_FLAGS])
598 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
600 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
602 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
603 attrs[XFRMA_ALG_AUTH_TRUNC])))
605 if (!x->props.aalgo) {
606 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
607 attrs[XFRMA_ALG_AUTH])))
610 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
612 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
613 xfrm_calg_get_byname,
614 attrs[XFRMA_ALG_COMP])))
617 if (attrs[XFRMA_TFCPAD])
618 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
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]);
632 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
636 if (attrs[XFRMA_SEC_CTX]) {
637 err = security_xfrm_state_alloc(x,
638 nla_data(attrs[XFRMA_SEC_CTX]));
643 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
644 attrs[XFRMA_REPLAY_ESN_VAL])))
648 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
649 /* sysctl_xfrm_aevent_etime is in 100ms units */
650 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
652 if ((err = xfrm_init_replay(x)))
655 /* override default values from above */
656 xfrm_update_ae_params(x, attrs, 0);
658 /* configure the hardware if offload is requested */
659 if (attrs[XFRMA_OFFLOAD_DEV]) {
660 err = xfrm_dev_state_add(net, x,
661 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
669 x->km.state = XFRM_STATE_DEAD;
676 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
677 struct nlattr **attrs)
679 struct net *net = sock_net(skb->sk);
680 struct xfrm_usersa_info *p = nlmsg_data(nlh);
681 struct xfrm_state *x;
685 err = verify_newsa_info(p, attrs);
689 x = xfrm_state_construct(net, p, attrs, &err);
694 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
695 err = xfrm_state_add(x);
697 err = xfrm_state_update(x);
699 xfrm_audit_state_add(x, err ? 0 : 1, true);
702 x->km.state = XFRM_STATE_DEAD;
703 xfrm_dev_state_delete(x);
708 if (x->km.state == XFRM_STATE_VOID)
709 x->km.state = XFRM_STATE_VALID;
711 c.seq = nlh->nlmsg_seq;
712 c.portid = nlh->nlmsg_pid;
713 c.event = nlh->nlmsg_type;
715 km_state_notify(x, &c);
721 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
722 struct xfrm_usersa_id *p,
723 struct nlattr **attrs,
726 struct xfrm_state *x = NULL;
729 u32 mark = xfrm_mark_get(attrs, &m);
731 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
733 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
735 xfrm_address_t *saddr = NULL;
737 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
744 x = xfrm_state_lookup_byaddr(net, mark,
746 p->proto, p->family);
755 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
756 struct nlattr **attrs)
758 struct net *net = sock_net(skb->sk);
759 struct xfrm_state *x;
762 struct xfrm_usersa_id *p = nlmsg_data(nlh);
764 x = xfrm_user_state_lookup(net, p, attrs, &err);
768 if ((err = security_xfrm_state_delete(x)) != 0)
771 if (xfrm_state_kern(x)) {
776 err = xfrm_state_delete(x);
781 c.seq = nlh->nlmsg_seq;
782 c.portid = nlh->nlmsg_pid;
783 c.event = nlh->nlmsg_type;
784 km_state_notify(x, &c);
787 xfrm_audit_state_delete(x, err ? 0 : 1, true);
792 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
794 memset(p, 0, sizeof(*p));
795 memcpy(&p->id, &x->id, sizeof(p->id));
796 memcpy(&p->sel, &x->sel, sizeof(p->sel));
797 memcpy(&p->lft, &x->lft, sizeof(p->lft));
798 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
799 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
800 put_unaligned(x->stats.replay, &p->stats.replay);
801 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
802 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
803 p->mode = x->props.mode;
804 p->replay_window = x->props.replay_window;
805 p->reqid = x->props.reqid;
806 p->family = x->props.family;
807 p->flags = x->props.flags;
811 struct xfrm_dump_info {
812 struct sk_buff *in_skb;
813 struct sk_buff *out_skb;
818 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
820 struct xfrm_user_sec_ctx *uctx;
822 int ctx_size = sizeof(*uctx) + s->ctx_len;
824 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
828 uctx = nla_data(attr);
829 uctx->exttype = XFRMA_SEC_CTX;
830 uctx->len = ctx_size;
831 uctx->ctx_doi = s->ctx_doi;
832 uctx->ctx_alg = s->ctx_alg;
833 uctx->ctx_len = s->ctx_len;
834 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
839 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
841 struct xfrm_user_offload *xuo;
844 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
848 xuo = nla_data(attr);
849 memset(xuo, 0, sizeof(*xuo));
850 xuo->ifindex = xso->dev->ifindex;
851 xuo->flags = xso->flags;
856 static bool xfrm_redact(void)
858 return IS_ENABLED(CONFIG_SECURITY) &&
859 security_locked_down(LOCKDOWN_XFRM_SECRET);
862 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
864 struct xfrm_algo *algo;
865 struct xfrm_algo_auth *ap;
867 bool redact_secret = xfrm_redact();
869 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
870 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
873 algo = nla_data(nla);
874 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
876 if (redact_secret && auth->alg_key_len)
877 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
879 memcpy(algo->alg_key, auth->alg_key,
880 (auth->alg_key_len + 7) / 8);
881 algo->alg_key_len = auth->alg_key_len;
883 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
887 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
888 if (redact_secret && auth->alg_key_len)
889 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
891 memcpy(ap->alg_key, auth->alg_key,
892 (auth->alg_key_len + 7) / 8);
896 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
898 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
899 struct xfrm_algo_aead *ap;
900 bool redact_secret = xfrm_redact();
906 memcpy(ap, aead, sizeof(*aead));
908 if (redact_secret && aead->alg_key_len)
909 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
911 memcpy(ap->alg_key, aead->alg_key,
912 (aead->alg_key_len + 7) / 8);
916 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
918 struct xfrm_algo *ap;
919 bool redact_secret = xfrm_redact();
920 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
926 memcpy(ap, ealg, sizeof(*ealg));
928 if (redact_secret && ealg->alg_key_len)
929 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
931 memcpy(ap->alg_key, ealg->alg_key,
932 (ealg->alg_key_len + 7) / 8);
937 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
942 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
944 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
949 /* Don't change this without updating xfrm_sa_len! */
950 static int copy_to_user_state_extra(struct xfrm_state *x,
951 struct xfrm_usersa_info *p,
956 copy_to_user_state(x, p);
958 if (x->props.extra_flags) {
959 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
960 x->props.extra_flags);
966 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
971 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
977 ret = copy_to_user_aead(x->aead, skb);
982 ret = copy_to_user_auth(x->aalg, skb);
987 ret = copy_to_user_ealg(x->ealg, skb);
992 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
997 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1002 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1006 ret = xfrm_mark_put(skb, &x->mark);
1010 ret = xfrm_smark_put(skb, &x->props.smark);
1015 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1016 xfrm_replay_state_esn_len(x->replay_esn),
1019 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1024 ret = copy_user_offload(&x->xso, skb);
1028 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1033 ret = copy_sec_ctx(x->security, skb);
1038 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1040 struct xfrm_dump_info *sp = ptr;
1041 struct sk_buff *in_skb = sp->in_skb;
1042 struct sk_buff *skb = sp->out_skb;
1043 struct xfrm_translator *xtr;
1044 struct xfrm_usersa_info *p;
1045 struct nlmsghdr *nlh;
1048 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1049 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1053 p = nlmsg_data(nlh);
1055 err = copy_to_user_state_extra(x, p, skb);
1057 nlmsg_cancel(skb, nlh);
1060 nlmsg_end(skb, nlh);
1062 xtr = xfrm_get_translator();
1064 err = xtr->alloc_compat(skb, nlh);
1066 xfrm_put_translator(xtr);
1068 nlmsg_cancel(skb, nlh);
1076 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1078 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1079 struct sock *sk = cb->skb->sk;
1080 struct net *net = sock_net(sk);
1083 xfrm_state_walk_done(walk, net);
1087 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1089 struct net *net = sock_net(skb->sk);
1090 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1091 struct xfrm_dump_info info;
1093 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1094 sizeof(cb->args) - sizeof(cb->args[0]));
1096 info.in_skb = cb->skb;
1098 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1099 info.nlmsg_flags = NLM_F_MULTI;
1102 struct nlattr *attrs[XFRMA_MAX+1];
1103 struct xfrm_address_filter *filter = NULL;
1107 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1108 xfrma_policy, cb->extack);
1112 if (attrs[XFRMA_ADDRESS_FILTER]) {
1113 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1114 sizeof(*filter), GFP_KERNEL);
1119 if (attrs[XFRMA_PROTO])
1120 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1122 xfrm_state_walk_init(walk, proto, filter);
1126 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1131 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1132 struct xfrm_state *x, u32 seq)
1134 struct xfrm_dump_info info;
1135 struct sk_buff *skb;
1138 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1140 return ERR_PTR(-ENOMEM);
1142 info.in_skb = in_skb;
1144 info.nlmsg_seq = seq;
1145 info.nlmsg_flags = 0;
1147 err = dump_one_state(x, 0, &info);
1150 return ERR_PTR(err);
1156 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1157 * Must be called with RCU read lock.
1159 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1160 u32 pid, unsigned int group)
1162 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1163 struct xfrm_translator *xtr;
1170 xtr = xfrm_get_translator();
1172 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1174 xfrm_put_translator(xtr);
1181 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1184 static inline unsigned int xfrm_spdinfo_msgsize(void)
1186 return NLMSG_ALIGN(4)
1187 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1188 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1189 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1190 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1193 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1194 u32 portid, u32 seq, u32 flags)
1196 struct xfrmk_spdinfo si;
1197 struct xfrmu_spdinfo spc;
1198 struct xfrmu_spdhinfo sph;
1199 struct xfrmu_spdhthresh spt4, spt6;
1200 struct nlmsghdr *nlh;
1205 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1206 if (nlh == NULL) /* shouldn't really happen ... */
1209 f = nlmsg_data(nlh);
1211 xfrm_spd_getinfo(net, &si);
1212 spc.incnt = si.incnt;
1213 spc.outcnt = si.outcnt;
1214 spc.fwdcnt = si.fwdcnt;
1215 spc.inscnt = si.inscnt;
1216 spc.outscnt = si.outscnt;
1217 spc.fwdscnt = si.fwdscnt;
1218 sph.spdhcnt = si.spdhcnt;
1219 sph.spdhmcnt = si.spdhmcnt;
1222 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1224 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1225 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1226 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1227 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1228 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1230 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1232 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1234 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1236 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1238 nlmsg_cancel(skb, nlh);
1242 nlmsg_end(skb, nlh);
1246 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1247 struct nlattr **attrs)
1249 struct net *net = sock_net(skb->sk);
1250 struct xfrmu_spdhthresh *thresh4 = NULL;
1251 struct xfrmu_spdhthresh *thresh6 = NULL;
1253 /* selector prefixlen thresholds to hash policies */
1254 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1255 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1257 if (nla_len(rta) < sizeof(*thresh4))
1259 thresh4 = nla_data(rta);
1260 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1263 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1264 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1266 if (nla_len(rta) < sizeof(*thresh6))
1268 thresh6 = nla_data(rta);
1269 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1273 if (thresh4 || thresh6) {
1274 write_seqlock(&net->xfrm.policy_hthresh.lock);
1276 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1277 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1280 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1281 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1283 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1285 xfrm_policy_hash_rebuild(net);
1291 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1292 struct nlattr **attrs)
1294 struct net *net = sock_net(skb->sk);
1295 struct sk_buff *r_skb;
1296 u32 *flags = nlmsg_data(nlh);
1297 u32 sportid = NETLINK_CB(skb).portid;
1298 u32 seq = nlh->nlmsg_seq;
1301 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1305 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1308 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1311 static inline unsigned int xfrm_sadinfo_msgsize(void)
1313 return NLMSG_ALIGN(4)
1314 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1315 + nla_total_size(4); /* XFRMA_SAD_CNT */
1318 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1319 u32 portid, u32 seq, u32 flags)
1321 struct xfrmk_sadinfo si;
1322 struct xfrmu_sadhinfo sh;
1323 struct nlmsghdr *nlh;
1327 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1328 if (nlh == NULL) /* shouldn't really happen ... */
1331 f = nlmsg_data(nlh);
1333 xfrm_sad_getinfo(net, &si);
1335 sh.sadhmcnt = si.sadhmcnt;
1336 sh.sadhcnt = si.sadhcnt;
1338 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1340 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1342 nlmsg_cancel(skb, nlh);
1346 nlmsg_end(skb, nlh);
1350 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1351 struct nlattr **attrs)
1353 struct net *net = sock_net(skb->sk);
1354 struct sk_buff *r_skb;
1355 u32 *flags = nlmsg_data(nlh);
1356 u32 sportid = NETLINK_CB(skb).portid;
1357 u32 seq = nlh->nlmsg_seq;
1360 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1364 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1367 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1370 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1371 struct nlattr **attrs)
1373 struct net *net = sock_net(skb->sk);
1374 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1375 struct xfrm_state *x;
1376 struct sk_buff *resp_skb;
1379 x = xfrm_user_state_lookup(net, p, attrs, &err);
1383 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1384 if (IS_ERR(resp_skb)) {
1385 err = PTR_ERR(resp_skb);
1387 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1394 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1395 struct nlattr **attrs)
1397 struct net *net = sock_net(skb->sk);
1398 struct xfrm_state *x;
1399 struct xfrm_userspi_info *p;
1400 struct xfrm_translator *xtr;
1401 struct sk_buff *resp_skb;
1402 xfrm_address_t *daddr;
1409 p = nlmsg_data(nlh);
1410 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1414 family = p->info.family;
1415 daddr = &p->info.id.daddr;
1419 mark = xfrm_mark_get(attrs, &m);
1421 if (attrs[XFRMA_IF_ID]) {
1422 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1430 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1431 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1438 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1439 if_id, p->info.id.proto, daddr,
1446 err = xfrm_alloc_spi(x, p->min, p->max);
1450 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1451 if (IS_ERR(resp_skb)) {
1452 err = PTR_ERR(resp_skb);
1456 xtr = xfrm_get_translator();
1458 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1460 xfrm_put_translator(xtr);
1462 kfree_skb(resp_skb);
1467 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1475 static int verify_policy_dir(u8 dir)
1478 case XFRM_POLICY_IN:
1479 case XFRM_POLICY_OUT:
1480 case XFRM_POLICY_FWD:
1490 static int verify_policy_type(u8 type)
1493 case XFRM_POLICY_TYPE_MAIN:
1494 #ifdef CONFIG_XFRM_SUB_POLICY
1495 case XFRM_POLICY_TYPE_SUB:
1506 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1511 case XFRM_SHARE_ANY:
1512 case XFRM_SHARE_SESSION:
1513 case XFRM_SHARE_USER:
1514 case XFRM_SHARE_UNIQUE:
1521 switch (p->action) {
1522 case XFRM_POLICY_ALLOW:
1523 case XFRM_POLICY_BLOCK:
1530 switch (p->sel.family) {
1532 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1538 #if IS_ENABLED(CONFIG_IPV6)
1539 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1544 return -EAFNOSUPPORT;
1551 ret = verify_policy_dir(p->dir);
1554 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1560 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1562 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1563 struct xfrm_user_sec_ctx *uctx;
1568 uctx = nla_data(rt);
1569 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1572 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1578 for (i = 0; i < nr; i++, ut++) {
1579 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1581 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1582 memcpy(&t->saddr, &ut->saddr,
1583 sizeof(xfrm_address_t));
1584 t->reqid = ut->reqid;
1586 t->share = ut->share;
1587 t->optional = ut->optional;
1588 t->aalgos = ut->aalgos;
1589 t->ealgos = ut->ealgos;
1590 t->calgos = ut->calgos;
1591 /* If all masks are ~0, then we allow all algorithms. */
1592 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1593 t->encap_family = ut->family;
1597 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1602 if (nr > XFRM_MAX_DEPTH)
1605 prev_family = family;
1607 for (i = 0; i < nr; i++) {
1608 /* We never validated the ut->family value, so many
1609 * applications simply leave it at zero. The check was
1610 * never made and ut->family was ignored because all
1611 * templates could be assumed to have the same family as
1612 * the policy itself. Now that we will have ipv4-in-ipv6
1613 * and ipv6-in-ipv4 tunnels, this is no longer true.
1616 ut[i].family = family;
1618 switch (ut[i].mode) {
1619 case XFRM_MODE_TUNNEL:
1620 case XFRM_MODE_BEET:
1623 if (ut[i].family != prev_family)
1627 if (ut[i].mode >= XFRM_MODE_MAX)
1630 prev_family = ut[i].family;
1632 switch (ut[i].family) {
1635 #if IS_ENABLED(CONFIG_IPV6)
1643 if (!xfrm_id_proto_valid(ut[i].id.proto))
1650 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1652 struct nlattr *rt = attrs[XFRMA_TMPL];
1657 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1658 int nr = nla_len(rt) / sizeof(*utmpl);
1661 err = validate_tmpl(nr, utmpl, pol->family);
1665 copy_templates(pol, utmpl, nr);
1670 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1672 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1673 struct xfrm_userpolicy_type *upt;
1674 u8 type = XFRM_POLICY_TYPE_MAIN;
1682 err = verify_policy_type(type);
1690 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1692 xp->priority = p->priority;
1693 xp->index = p->index;
1694 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1695 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1696 xp->action = p->action;
1697 xp->flags = p->flags;
1698 xp->family = p->sel.family;
1699 /* XXX xp->share = p->share; */
1702 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1704 memset(p, 0, sizeof(*p));
1705 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1706 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1707 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1708 p->priority = xp->priority;
1709 p->index = xp->index;
1710 p->sel.family = xp->family;
1712 p->action = xp->action;
1713 p->flags = xp->flags;
1714 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1717 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1719 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1727 copy_from_user_policy(xp, p);
1729 err = copy_from_user_policy_type(&xp->type, attrs);
1733 if (!(err = copy_from_user_tmpl(xp, attrs)))
1734 err = copy_from_user_sec_ctx(xp, attrs);
1738 xfrm_mark_get(attrs, &xp->mark);
1740 if (attrs[XFRMA_IF_ID]) {
1741 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1752 xfrm_policy_destroy(xp);
1756 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1757 struct nlattr **attrs)
1759 struct net *net = sock_net(skb->sk);
1760 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1761 struct xfrm_policy *xp;
1766 err = verify_newpolicy_info(p);
1769 err = verify_sec_ctx_len(attrs);
1773 xp = xfrm_policy_construct(net, p, attrs, &err);
1777 /* shouldn't excl be based on nlh flags??
1778 * Aha! this is anti-netlink really i.e more pfkey derived
1779 * in netlink excl is a flag and you wouldn't need
1780 * a type XFRM_MSG_UPDPOLICY - JHS */
1781 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1782 err = xfrm_policy_insert(p->dir, xp, excl);
1783 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1786 security_xfrm_policy_free(xp->security);
1791 c.event = nlh->nlmsg_type;
1792 c.seq = nlh->nlmsg_seq;
1793 c.portid = nlh->nlmsg_pid;
1794 km_policy_notify(xp, p->dir, &c);
1801 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1803 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1806 if (xp->xfrm_nr == 0)
1809 for (i = 0; i < xp->xfrm_nr; i++) {
1810 struct xfrm_user_tmpl *up = &vec[i];
1811 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1813 memset(up, 0, sizeof(*up));
1814 memcpy(&up->id, &kp->id, sizeof(up->id));
1815 up->family = kp->encap_family;
1816 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1817 up->reqid = kp->reqid;
1818 up->mode = kp->mode;
1819 up->share = kp->share;
1820 up->optional = kp->optional;
1821 up->aalgos = kp->aalgos;
1822 up->ealgos = kp->ealgos;
1823 up->calgos = kp->calgos;
1826 return nla_put(skb, XFRMA_TMPL,
1827 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1830 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1833 return copy_sec_ctx(x->security, skb);
1838 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1841 return copy_sec_ctx(xp->security, skb);
1844 static inline unsigned int userpolicy_type_attrsize(void)
1846 #ifdef CONFIG_XFRM_SUB_POLICY
1847 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1853 #ifdef CONFIG_XFRM_SUB_POLICY
1854 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1856 struct xfrm_userpolicy_type upt;
1858 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1859 memset(&upt, 0, sizeof(upt));
1862 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1866 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1872 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1874 struct xfrm_dump_info *sp = ptr;
1875 struct xfrm_userpolicy_info *p;
1876 struct sk_buff *in_skb = sp->in_skb;
1877 struct sk_buff *skb = sp->out_skb;
1878 struct xfrm_translator *xtr;
1879 struct nlmsghdr *nlh;
1882 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1883 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1887 p = nlmsg_data(nlh);
1888 copy_to_user_policy(xp, p, dir);
1889 err = copy_to_user_tmpl(xp, skb);
1891 err = copy_to_user_sec_ctx(xp, skb);
1893 err = copy_to_user_policy_type(xp->type, skb);
1895 err = xfrm_mark_put(skb, &xp->mark);
1897 err = xfrm_if_id_put(skb, xp->if_id);
1899 nlmsg_cancel(skb, nlh);
1902 nlmsg_end(skb, nlh);
1904 xtr = xfrm_get_translator();
1906 err = xtr->alloc_compat(skb, nlh);
1908 xfrm_put_translator(xtr);
1910 nlmsg_cancel(skb, nlh);
1918 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1920 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1921 struct net *net = sock_net(cb->skb->sk);
1923 xfrm_policy_walk_done(walk, net);
1927 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1929 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1931 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1933 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1937 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1939 struct net *net = sock_net(skb->sk);
1940 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1941 struct xfrm_dump_info info;
1943 info.in_skb = cb->skb;
1945 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1946 info.nlmsg_flags = NLM_F_MULTI;
1948 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1953 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1954 struct xfrm_policy *xp,
1957 struct xfrm_dump_info info;
1958 struct sk_buff *skb;
1961 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1963 return ERR_PTR(-ENOMEM);
1965 info.in_skb = in_skb;
1967 info.nlmsg_seq = seq;
1968 info.nlmsg_flags = 0;
1970 err = dump_one_policy(xp, dir, 0, &info);
1973 return ERR_PTR(err);
1979 static int xfrm_notify_userpolicy(struct net *net)
1981 struct xfrm_userpolicy_default *up;
1982 int len = NLMSG_ALIGN(sizeof(*up));
1983 struct nlmsghdr *nlh;
1984 struct sk_buff *skb;
1987 skb = nlmsg_new(len, GFP_ATOMIC);
1991 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
1997 up = nlmsg_data(nlh);
1998 up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
1999 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2000 up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2001 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2002 up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2003 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2005 nlmsg_end(skb, nlh);
2008 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2014 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2015 struct nlattr **attrs)
2017 struct net *net = sock_net(skb->sk);
2018 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2020 if (up->in == XFRM_USERPOLICY_BLOCK)
2021 net->xfrm.policy_default |= XFRM_POL_DEFAULT_IN;
2022 else if (up->in == XFRM_USERPOLICY_ACCEPT)
2023 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_IN;
2025 if (up->fwd == XFRM_USERPOLICY_BLOCK)
2026 net->xfrm.policy_default |= XFRM_POL_DEFAULT_FWD;
2027 else if (up->fwd == XFRM_USERPOLICY_ACCEPT)
2028 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_FWD;
2030 if (up->out == XFRM_USERPOLICY_BLOCK)
2031 net->xfrm.policy_default |= XFRM_POL_DEFAULT_OUT;
2032 else if (up->out == XFRM_USERPOLICY_ACCEPT)
2033 net->xfrm.policy_default &= ~XFRM_POL_DEFAULT_OUT;
2035 rt_genid_bump_all(net);
2037 xfrm_notify_userpolicy(net);
2041 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2042 struct nlattr **attrs)
2044 struct sk_buff *r_skb;
2045 struct nlmsghdr *r_nlh;
2046 struct net *net = sock_net(skb->sk);
2047 struct xfrm_userpolicy_default *r_up;
2048 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2049 u32 portid = NETLINK_CB(skb).portid;
2050 u32 seq = nlh->nlmsg_seq;
2052 r_skb = nlmsg_new(len, GFP_ATOMIC);
2056 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2062 r_up = nlmsg_data(r_nlh);
2064 r_up->in = net->xfrm.policy_default & XFRM_POL_DEFAULT_IN ?
2065 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2066 r_up->fwd = net->xfrm.policy_default & XFRM_POL_DEFAULT_FWD ?
2067 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2068 r_up->out = net->xfrm.policy_default & XFRM_POL_DEFAULT_OUT ?
2069 XFRM_USERPOLICY_BLOCK : XFRM_USERPOLICY_ACCEPT;
2070 nlmsg_end(r_skb, r_nlh);
2072 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2075 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2076 struct nlattr **attrs)
2078 struct net *net = sock_net(skb->sk);
2079 struct xfrm_policy *xp;
2080 struct xfrm_userpolicy_id *p;
2081 u8 type = XFRM_POLICY_TYPE_MAIN;
2088 p = nlmsg_data(nlh);
2089 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2091 err = copy_from_user_policy_type(&type, attrs);
2095 err = verify_policy_dir(p->dir);
2099 if (attrs[XFRMA_IF_ID])
2100 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2102 xfrm_mark_get(attrs, &m);
2105 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2106 p->index, delete, &err);
2108 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2109 struct xfrm_sec_ctx *ctx;
2111 err = verify_sec_ctx_len(attrs);
2117 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2119 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2123 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2124 &p->sel, ctx, delete, &err);
2125 security_xfrm_policy_free(ctx);
2131 struct sk_buff *resp_skb;
2133 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2134 if (IS_ERR(resp_skb)) {
2135 err = PTR_ERR(resp_skb);
2137 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2138 NETLINK_CB(skb).portid);
2141 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2146 c.data.byid = p->index;
2147 c.event = nlh->nlmsg_type;
2148 c.seq = nlh->nlmsg_seq;
2149 c.portid = nlh->nlmsg_pid;
2150 km_policy_notify(xp, p->dir, &c);
2158 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2159 struct nlattr **attrs)
2161 struct net *net = sock_net(skb->sk);
2163 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2166 err = xfrm_state_flush(net, p->proto, true, false);
2168 if (err == -ESRCH) /* empty table */
2172 c.data.proto = p->proto;
2173 c.event = nlh->nlmsg_type;
2174 c.seq = nlh->nlmsg_seq;
2175 c.portid = nlh->nlmsg_pid;
2177 km_state_notify(NULL, &c);
2182 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2184 unsigned int replay_size = x->replay_esn ?
2185 xfrm_replay_state_esn_len(x->replay_esn) :
2186 sizeof(struct xfrm_replay_state);
2188 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2189 + nla_total_size(replay_size)
2190 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2191 + nla_total_size(sizeof(struct xfrm_mark))
2192 + nla_total_size(4) /* XFRM_AE_RTHR */
2193 + nla_total_size(4); /* XFRM_AE_ETHR */
2196 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2198 struct xfrm_aevent_id *id;
2199 struct nlmsghdr *nlh;
2202 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2206 id = nlmsg_data(nlh);
2207 memset(&id->sa_id, 0, sizeof(id->sa_id));
2208 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2209 id->sa_id.spi = x->id.spi;
2210 id->sa_id.family = x->props.family;
2211 id->sa_id.proto = x->id.proto;
2212 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2213 id->reqid = x->props.reqid;
2214 id->flags = c->data.aevent;
2216 if (x->replay_esn) {
2217 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2218 xfrm_replay_state_esn_len(x->replay_esn),
2221 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2226 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2231 if (id->flags & XFRM_AE_RTHR) {
2232 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2236 if (id->flags & XFRM_AE_ETHR) {
2237 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2238 x->replay_maxage * 10 / HZ);
2242 err = xfrm_mark_put(skb, &x->mark);
2246 err = xfrm_if_id_put(skb, x->if_id);
2250 nlmsg_end(skb, nlh);
2254 nlmsg_cancel(skb, nlh);
2258 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2259 struct nlattr **attrs)
2261 struct net *net = sock_net(skb->sk);
2262 struct xfrm_state *x;
2263 struct sk_buff *r_skb;
2268 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2269 struct xfrm_usersa_id *id = &p->sa_id;
2271 mark = xfrm_mark_get(attrs, &m);
2273 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2277 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2278 if (r_skb == NULL) {
2284 * XXX: is this lock really needed - none of the other
2285 * gets lock (the concern is things getting updated
2286 * while we are still reading) - jhs
2288 spin_lock_bh(&x->lock);
2289 c.data.aevent = p->flags;
2290 c.seq = nlh->nlmsg_seq;
2291 c.portid = nlh->nlmsg_pid;
2293 err = build_aevent(r_skb, x, &c);
2296 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2297 spin_unlock_bh(&x->lock);
2302 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2303 struct nlattr **attrs)
2305 struct net *net = sock_net(skb->sk);
2306 struct xfrm_state *x;
2311 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2312 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2313 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2314 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2315 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2316 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2318 if (!lt && !rp && !re && !et && !rt)
2321 /* pedantic mode - thou shalt sayeth replaceth */
2322 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2325 mark = xfrm_mark_get(attrs, &m);
2327 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2331 if (x->km.state != XFRM_STATE_VALID)
2334 err = xfrm_replay_verify_len(x->replay_esn, re);
2338 spin_lock_bh(&x->lock);
2339 xfrm_update_ae_params(x, attrs, 1);
2340 spin_unlock_bh(&x->lock);
2342 c.event = nlh->nlmsg_type;
2343 c.seq = nlh->nlmsg_seq;
2344 c.portid = nlh->nlmsg_pid;
2345 c.data.aevent = XFRM_AE_CU;
2346 km_state_notify(x, &c);
2353 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2354 struct nlattr **attrs)
2356 struct net *net = sock_net(skb->sk);
2358 u8 type = XFRM_POLICY_TYPE_MAIN;
2361 err = copy_from_user_policy_type(&type, attrs);
2365 err = xfrm_policy_flush(net, type, true);
2367 if (err == -ESRCH) /* empty table */
2373 c.event = nlh->nlmsg_type;
2374 c.seq = nlh->nlmsg_seq;
2375 c.portid = nlh->nlmsg_pid;
2377 km_policy_notify(NULL, 0, &c);
2381 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2382 struct nlattr **attrs)
2384 struct net *net = sock_net(skb->sk);
2385 struct xfrm_policy *xp;
2386 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2387 struct xfrm_userpolicy_info *p = &up->pol;
2388 u8 type = XFRM_POLICY_TYPE_MAIN;
2393 err = copy_from_user_policy_type(&type, attrs);
2397 err = verify_policy_dir(p->dir);
2401 if (attrs[XFRMA_IF_ID])
2402 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2404 xfrm_mark_get(attrs, &m);
2407 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2410 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2411 struct xfrm_sec_ctx *ctx;
2413 err = verify_sec_ctx_len(attrs);
2419 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2421 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2425 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2426 &p->sel, ctx, 0, &err);
2427 security_xfrm_policy_free(ctx);
2432 if (unlikely(xp->walk.dead))
2437 xfrm_policy_delete(xp, p->dir);
2438 xfrm_audit_policy_delete(xp, 1, true);
2440 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2447 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2448 struct nlattr **attrs)
2450 struct net *net = sock_net(skb->sk);
2451 struct xfrm_state *x;
2453 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2454 struct xfrm_usersa_info *p = &ue->state;
2456 u32 mark = xfrm_mark_get(attrs, &m);
2458 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2464 spin_lock_bh(&x->lock);
2466 if (x->km.state != XFRM_STATE_VALID)
2468 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2471 __xfrm_state_delete(x);
2472 xfrm_audit_state_delete(x, 1, true);
2476 spin_unlock_bh(&x->lock);
2481 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2482 struct nlattr **attrs)
2484 struct net *net = sock_net(skb->sk);
2485 struct xfrm_policy *xp;
2486 struct xfrm_user_tmpl *ut;
2488 struct nlattr *rt = attrs[XFRMA_TMPL];
2489 struct xfrm_mark mark;
2491 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2492 struct xfrm_state *x = xfrm_state_alloc(net);
2498 xfrm_mark_get(attrs, &mark);
2500 err = verify_newpolicy_info(&ua->policy);
2503 err = verify_sec_ctx_len(attrs);
2508 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2512 memcpy(&x->id, &ua->id, sizeof(ua->id));
2513 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2514 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2515 xp->mark.m = x->mark.m = mark.m;
2516 xp->mark.v = x->mark.v = mark.v;
2518 /* extract the templates and for each call km_key */
2519 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2520 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2521 memcpy(&x->id, &t->id, sizeof(x->id));
2522 x->props.mode = t->mode;
2523 x->props.reqid = t->reqid;
2524 x->props.family = ut->family;
2525 t->aalgos = ua->aalgos;
2526 t->ealgos = ua->ealgos;
2527 t->calgos = ua->calgos;
2528 err = km_query(x, t, xp);
2543 #ifdef CONFIG_XFRM_MIGRATE
2544 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2545 struct xfrm_kmaddress *k,
2546 struct nlattr **attrs, int *num)
2548 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2549 struct xfrm_user_migrate *um;
2553 struct xfrm_user_kmaddress *uk;
2555 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2556 memcpy(&k->local, &uk->local, sizeof(k->local));
2557 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2558 k->family = uk->family;
2559 k->reserved = uk->reserved;
2563 num_migrate = nla_len(rt) / sizeof(*um);
2565 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2568 for (i = 0; i < num_migrate; i++, um++, ma++) {
2569 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2570 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2571 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2572 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2574 ma->proto = um->proto;
2575 ma->mode = um->mode;
2576 ma->reqid = um->reqid;
2578 ma->old_family = um->old_family;
2579 ma->new_family = um->new_family;
2586 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2587 struct nlattr **attrs)
2589 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2590 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2591 struct xfrm_kmaddress km, *kmp;
2595 struct net *net = sock_net(skb->sk);
2596 struct xfrm_encap_tmpl *encap = NULL;
2598 if (attrs[XFRMA_MIGRATE] == NULL)
2601 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2603 err = copy_from_user_policy_type(&type, attrs);
2607 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2614 if (attrs[XFRMA_ENCAP]) {
2615 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2616 sizeof(*encap), GFP_KERNEL);
2621 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2628 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2629 struct nlattr **attrs)
2631 return -ENOPROTOOPT;
2635 #ifdef CONFIG_XFRM_MIGRATE
2636 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2638 struct xfrm_user_migrate um;
2640 memset(&um, 0, sizeof(um));
2641 um.proto = m->proto;
2643 um.reqid = m->reqid;
2644 um.old_family = m->old_family;
2645 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2646 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2647 um.new_family = m->new_family;
2648 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2649 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2651 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2654 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2656 struct xfrm_user_kmaddress uk;
2658 memset(&uk, 0, sizeof(uk));
2659 uk.family = k->family;
2660 uk.reserved = k->reserved;
2661 memcpy(&uk.local, &k->local, sizeof(uk.local));
2662 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2664 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2667 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2670 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2671 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2672 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2673 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2674 + userpolicy_type_attrsize();
2677 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2678 int num_migrate, const struct xfrm_kmaddress *k,
2679 const struct xfrm_selector *sel,
2680 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2682 const struct xfrm_migrate *mp;
2683 struct xfrm_userpolicy_id *pol_id;
2684 struct nlmsghdr *nlh;
2687 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2691 pol_id = nlmsg_data(nlh);
2692 /* copy data from selector, dir, and type to the pol_id */
2693 memset(pol_id, 0, sizeof(*pol_id));
2694 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2698 err = copy_to_user_kmaddress(k, skb);
2703 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2707 err = copy_to_user_policy_type(type, skb);
2710 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2711 err = copy_to_user_migrate(mp, skb);
2716 nlmsg_end(skb, nlh);
2720 nlmsg_cancel(skb, nlh);
2724 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2725 const struct xfrm_migrate *m, int num_migrate,
2726 const struct xfrm_kmaddress *k,
2727 const struct xfrm_encap_tmpl *encap)
2729 struct net *net = &init_net;
2730 struct sk_buff *skb;
2733 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2739 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2742 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2745 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2746 const struct xfrm_migrate *m, int num_migrate,
2747 const struct xfrm_kmaddress *k,
2748 const struct xfrm_encap_tmpl *encap)
2750 return -ENOPROTOOPT;
2754 #define XMSGSIZE(type) sizeof(struct type)
2756 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2757 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2758 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2759 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2760 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2761 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2762 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2763 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2764 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2765 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2766 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2767 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2768 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2769 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2770 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2771 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2772 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2773 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2774 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2775 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2776 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2777 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2778 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2779 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
2781 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2785 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2786 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2787 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2788 [XFRMA_LASTUSED] = { .type = NLA_U64},
2789 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2790 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2791 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2792 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2793 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2794 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2795 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2796 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2797 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2798 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2799 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2800 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2801 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2802 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2803 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2804 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2805 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2806 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2807 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2808 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2809 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2810 [XFRMA_PROTO] = { .type = NLA_U8 },
2811 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2812 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2813 [XFRMA_SET_MARK] = { .type = NLA_U32 },
2814 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
2815 [XFRMA_IF_ID] = { .type = NLA_U32 },
2817 EXPORT_SYMBOL_GPL(xfrma_policy);
2819 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2820 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2821 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2824 static const struct xfrm_link {
2825 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2826 int (*start)(struct netlink_callback *);
2827 int (*dump)(struct sk_buff *, struct netlink_callback *);
2828 int (*done)(struct netlink_callback *);
2829 const struct nla_policy *nla_pol;
2831 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2832 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2833 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2834 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2835 .dump = xfrm_dump_sa,
2836 .done = xfrm_dump_sa_done },
2837 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2838 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2839 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2840 .start = xfrm_dump_policy_start,
2841 .dump = xfrm_dump_policy,
2842 .done = xfrm_dump_policy_done },
2843 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2844 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2845 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2846 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2847 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2848 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2849 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2850 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2851 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2852 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2853 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2854 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2855 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2856 .nla_pol = xfrma_spd_policy,
2857 .nla_max = XFRMA_SPD_MAX },
2858 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2859 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
2860 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
2863 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2864 struct netlink_ext_ack *extack)
2866 struct net *net = sock_net(skb->sk);
2867 struct nlattr *attrs[XFRMA_MAX+1];
2868 const struct xfrm_link *link;
2869 struct nlmsghdr *nlh64 = NULL;
2872 type = nlh->nlmsg_type;
2873 if (type > XFRM_MSG_MAX)
2876 type -= XFRM_MSG_BASE;
2877 link = &xfrm_dispatch[type];
2879 /* All operations require privileges, even GET */
2880 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2883 if (in_compat_syscall()) {
2884 struct xfrm_translator *xtr = xfrm_get_translator();
2889 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2890 link->nla_pol, extack);
2891 xfrm_put_translator(xtr);
2893 return PTR_ERR(nlh64);
2898 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2899 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2900 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2901 struct netlink_dump_control c = {
2902 .start = link->start,
2907 if (link->dump == NULL) {
2912 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2916 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2917 link->nla_max ? : XFRMA_MAX,
2918 link->nla_pol ? : xfrma_policy, extack);
2922 if (link->doit == NULL) {
2927 err = link->doit(skb, nlh, attrs);
2929 /* We need to free skb allocated in xfrm_alloc_compat() before
2930 * returning from this function, because consume_skb() won't take
2931 * care of frag_list since netlink destructor sets
2932 * sbk->head to NULL. (see netlink_skb_destructor())
2934 if (skb_has_frag_list(skb)) {
2935 kfree_skb(skb_shinfo(skb)->frag_list);
2936 skb_shinfo(skb)->frag_list = NULL;
2944 static void xfrm_netlink_rcv(struct sk_buff *skb)
2946 struct net *net = sock_net(skb->sk);
2948 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2949 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2950 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2953 static inline unsigned int xfrm_expire_msgsize(void)
2955 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2956 + nla_total_size(sizeof(struct xfrm_mark));
2959 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2961 struct xfrm_user_expire *ue;
2962 struct nlmsghdr *nlh;
2965 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2969 ue = nlmsg_data(nlh);
2970 copy_to_user_state(x, &ue->state);
2971 ue->hard = (c->data.hard != 0) ? 1 : 0;
2972 /* clear the padding bytes */
2973 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2975 err = xfrm_mark_put(skb, &x->mark);
2979 err = xfrm_if_id_put(skb, x->if_id);
2983 nlmsg_end(skb, nlh);
2987 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2989 struct net *net = xs_net(x);
2990 struct sk_buff *skb;
2992 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2996 if (build_expire(skb, x, c) < 0) {
3001 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3004 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3006 struct net *net = xs_net(x);
3007 struct sk_buff *skb;
3010 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3014 err = build_aevent(skb, x, c);
3017 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3020 static int xfrm_notify_sa_flush(const struct km_event *c)
3022 struct net *net = c->net;
3023 struct xfrm_usersa_flush *p;
3024 struct nlmsghdr *nlh;
3025 struct sk_buff *skb;
3026 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3028 skb = nlmsg_new(len, GFP_ATOMIC);
3032 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3038 p = nlmsg_data(nlh);
3039 p->proto = c->data.proto;
3041 nlmsg_end(skb, nlh);
3043 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3046 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3050 l += nla_total_size(aead_len(x->aead));
3052 l += nla_total_size(sizeof(struct xfrm_algo) +
3053 (x->aalg->alg_key_len + 7) / 8);
3054 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3057 l += nla_total_size(xfrm_alg_len(x->ealg));
3059 l += nla_total_size(sizeof(*x->calg));
3061 l += nla_total_size(sizeof(*x->encap));
3063 l += nla_total_size(sizeof(x->tfcpad));
3065 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3067 l += nla_total_size(sizeof(struct xfrm_replay_state));
3069 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3070 x->security->ctx_len);
3072 l += nla_total_size(sizeof(*x->coaddr));
3073 if (x->props.extra_flags)
3074 l += nla_total_size(sizeof(x->props.extra_flags));
3076 l += nla_total_size(sizeof(struct xfrm_user_offload));
3077 if (x->props.smark.v | x->props.smark.m) {
3078 l += nla_total_size(sizeof(x->props.smark.v));
3079 l += nla_total_size(sizeof(x->props.smark.m));
3082 l += nla_total_size(sizeof(x->if_id));
3084 /* Must count x->lastused as it may become non-zero behind our back. */
3085 l += nla_total_size_64bit(sizeof(u64));
3090 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3092 struct net *net = xs_net(x);
3093 struct xfrm_usersa_info *p;
3094 struct xfrm_usersa_id *id;
3095 struct nlmsghdr *nlh;
3096 struct sk_buff *skb;
3097 unsigned int len = xfrm_sa_len(x);
3098 unsigned int headlen;
3101 headlen = sizeof(*p);
3102 if (c->event == XFRM_MSG_DELSA) {
3103 len += nla_total_size(headlen);
3104 headlen = sizeof(*id);
3105 len += nla_total_size(sizeof(struct xfrm_mark));
3107 len += NLMSG_ALIGN(headlen);
3109 skb = nlmsg_new(len, GFP_ATOMIC);
3113 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3118 p = nlmsg_data(nlh);
3119 if (c->event == XFRM_MSG_DELSA) {
3120 struct nlattr *attr;
3122 id = nlmsg_data(nlh);
3123 memset(id, 0, sizeof(*id));
3124 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3125 id->spi = x->id.spi;
3126 id->family = x->props.family;
3127 id->proto = x->id.proto;
3129 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3136 err = copy_to_user_state_extra(x, p, skb);
3140 nlmsg_end(skb, nlh);
3142 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3149 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3153 case XFRM_MSG_EXPIRE:
3154 return xfrm_exp_state_notify(x, c);
3155 case XFRM_MSG_NEWAE:
3156 return xfrm_aevent_state_notify(x, c);
3157 case XFRM_MSG_DELSA:
3158 case XFRM_MSG_UPDSA:
3159 case XFRM_MSG_NEWSA:
3160 return xfrm_notify_sa(x, c);
3161 case XFRM_MSG_FLUSHSA:
3162 return xfrm_notify_sa_flush(c);
3164 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3173 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3174 struct xfrm_policy *xp)
3176 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3177 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3178 + nla_total_size(sizeof(struct xfrm_mark))
3179 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3180 + userpolicy_type_attrsize();
3183 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3184 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3186 __u32 seq = xfrm_get_acqseq();
3187 struct xfrm_user_acquire *ua;
3188 struct nlmsghdr *nlh;
3191 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3195 ua = nlmsg_data(nlh);
3196 memcpy(&ua->id, &x->id, sizeof(ua->id));
3197 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3198 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3199 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3200 ua->aalgos = xt->aalgos;
3201 ua->ealgos = xt->ealgos;
3202 ua->calgos = xt->calgos;
3203 ua->seq = x->km.seq = seq;
3205 err = copy_to_user_tmpl(xp, skb);
3207 err = copy_to_user_state_sec_ctx(x, skb);
3209 err = copy_to_user_policy_type(xp->type, skb);
3211 err = xfrm_mark_put(skb, &xp->mark);
3213 err = xfrm_if_id_put(skb, xp->if_id);
3215 nlmsg_cancel(skb, nlh);
3219 nlmsg_end(skb, nlh);
3223 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3224 struct xfrm_policy *xp)
3226 struct net *net = xs_net(x);
3227 struct sk_buff *skb;
3230 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3234 err = build_acquire(skb, x, xt, xp);
3237 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3240 /* User gives us xfrm_user_policy_info followed by an array of 0
3241 * or more templates.
3243 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3244 u8 *data, int len, int *dir)
3246 struct net *net = sock_net(sk);
3247 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3248 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3249 struct xfrm_policy *xp;
3252 switch (sk->sk_family) {
3254 if (opt != IP_XFRM_POLICY) {
3259 #if IS_ENABLED(CONFIG_IPV6)
3261 if (opt != IPV6_XFRM_POLICY) {
3274 if (len < sizeof(*p) ||
3275 verify_newpolicy_info(p))
3278 nr = ((len - sizeof(*p)) / sizeof(*ut));
3279 if (validate_tmpl(nr, ut, p->sel.family))
3282 if (p->dir > XFRM_POLICY_OUT)
3285 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3291 copy_from_user_policy(xp, p);
3292 xp->type = XFRM_POLICY_TYPE_MAIN;
3293 copy_templates(xp, ut, nr);
3300 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3302 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3303 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3304 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3305 + nla_total_size(sizeof(struct xfrm_mark))
3306 + userpolicy_type_attrsize();
3309 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3310 int dir, const struct km_event *c)
3312 struct xfrm_user_polexpire *upe;
3313 int hard = c->data.hard;
3314 struct nlmsghdr *nlh;
3317 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3321 upe = nlmsg_data(nlh);
3322 copy_to_user_policy(xp, &upe->pol, dir);
3323 err = copy_to_user_tmpl(xp, skb);
3325 err = copy_to_user_sec_ctx(xp, skb);
3327 err = copy_to_user_policy_type(xp->type, skb);
3329 err = xfrm_mark_put(skb, &xp->mark);
3331 err = xfrm_if_id_put(skb, xp->if_id);
3333 nlmsg_cancel(skb, nlh);
3338 nlmsg_end(skb, nlh);
3342 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3344 struct net *net = xp_net(xp);
3345 struct sk_buff *skb;
3348 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3352 err = build_polexpire(skb, xp, dir, c);
3355 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3358 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3360 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3361 struct net *net = xp_net(xp);
3362 struct xfrm_userpolicy_info *p;
3363 struct xfrm_userpolicy_id *id;
3364 struct nlmsghdr *nlh;
3365 struct sk_buff *skb;
3366 unsigned int headlen;
3369 headlen = sizeof(*p);
3370 if (c->event == XFRM_MSG_DELPOLICY) {
3371 len += nla_total_size(headlen);
3372 headlen = sizeof(*id);
3374 len += userpolicy_type_attrsize();
3375 len += nla_total_size(sizeof(struct xfrm_mark));
3376 len += NLMSG_ALIGN(headlen);
3378 skb = nlmsg_new(len, GFP_ATOMIC);
3382 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3387 p = nlmsg_data(nlh);
3388 if (c->event == XFRM_MSG_DELPOLICY) {
3389 struct nlattr *attr;
3391 id = nlmsg_data(nlh);
3392 memset(id, 0, sizeof(*id));
3395 id->index = xp->index;
3397 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3399 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3407 copy_to_user_policy(xp, p, dir);
3408 err = copy_to_user_tmpl(xp, skb);
3410 err = copy_to_user_policy_type(xp->type, skb);
3412 err = xfrm_mark_put(skb, &xp->mark);
3414 err = xfrm_if_id_put(skb, xp->if_id);
3418 nlmsg_end(skb, nlh);
3420 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3427 static int xfrm_notify_policy_flush(const struct km_event *c)
3429 struct net *net = c->net;
3430 struct nlmsghdr *nlh;
3431 struct sk_buff *skb;
3434 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3438 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3442 err = copy_to_user_policy_type(c->data.type, skb);
3446 nlmsg_end(skb, nlh);
3448 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3455 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3459 case XFRM_MSG_NEWPOLICY:
3460 case XFRM_MSG_UPDPOLICY:
3461 case XFRM_MSG_DELPOLICY:
3462 return xfrm_notify_policy(xp, dir, c);
3463 case XFRM_MSG_FLUSHPOLICY:
3464 return xfrm_notify_policy_flush(c);
3465 case XFRM_MSG_POLEXPIRE:
3466 return xfrm_exp_policy_notify(xp, dir, c);
3468 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3476 static inline unsigned int xfrm_report_msgsize(void)
3478 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3481 static int build_report(struct sk_buff *skb, u8 proto,
3482 struct xfrm_selector *sel, xfrm_address_t *addr)
3484 struct xfrm_user_report *ur;
3485 struct nlmsghdr *nlh;
3487 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3491 ur = nlmsg_data(nlh);
3493 memcpy(&ur->sel, sel, sizeof(ur->sel));
3496 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3498 nlmsg_cancel(skb, nlh);
3502 nlmsg_end(skb, nlh);
3506 static int xfrm_send_report(struct net *net, u8 proto,
3507 struct xfrm_selector *sel, xfrm_address_t *addr)
3509 struct sk_buff *skb;
3512 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3516 err = build_report(skb, proto, sel, addr);
3519 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3522 static inline unsigned int xfrm_mapping_msgsize(void)
3524 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3527 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3528 xfrm_address_t *new_saddr, __be16 new_sport)
3530 struct xfrm_user_mapping *um;
3531 struct nlmsghdr *nlh;
3533 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3537 um = nlmsg_data(nlh);
3539 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3540 um->id.spi = x->id.spi;
3541 um->id.family = x->props.family;
3542 um->id.proto = x->id.proto;
3543 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3544 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3545 um->new_sport = new_sport;
3546 um->old_sport = x->encap->encap_sport;
3547 um->reqid = x->props.reqid;
3549 nlmsg_end(skb, nlh);
3553 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3556 struct net *net = xs_net(x);
3557 struct sk_buff *skb;
3560 if (x->id.proto != IPPROTO_ESP)
3566 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3570 err = build_mapping(skb, x, ipaddr, sport);
3573 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3576 static bool xfrm_is_alive(const struct km_event *c)
3578 return (bool)xfrm_acquire_is_on(c->net);
3581 static struct xfrm_mgr netlink_mgr = {
3582 .notify = xfrm_send_state_notify,
3583 .acquire = xfrm_send_acquire,
3584 .compile_policy = xfrm_compile_policy,
3585 .notify_policy = xfrm_send_policy_notify,
3586 .report = xfrm_send_report,
3587 .migrate = xfrm_send_migrate,
3588 .new_mapping = xfrm_send_mapping,
3589 .is_alive = xfrm_is_alive,
3592 static int __net_init xfrm_user_net_init(struct net *net)
3595 struct netlink_kernel_cfg cfg = {
3596 .groups = XFRMNLGRP_MAX,
3597 .input = xfrm_netlink_rcv,
3600 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3603 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3604 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3608 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
3610 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3613 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3617 list_for_each_entry(net, net_exit_list, exit_list)
3618 netlink_kernel_release(net->xfrm.nlsk_stash);
3621 static struct pernet_operations xfrm_user_net_ops = {
3622 .init = xfrm_user_net_init,
3623 .pre_exit = xfrm_user_net_pre_exit,
3624 .exit_batch = xfrm_user_net_exit,
3627 static int __init xfrm_user_init(void)
3631 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3633 rv = register_pernet_subsys(&xfrm_user_net_ops);
3636 rv = xfrm_register_km(&netlink_mgr);
3638 unregister_pernet_subsys(&xfrm_user_net_ops);
3642 static void __exit xfrm_user_exit(void)
3644 xfrm_unregister_km(&netlink_mgr);
3645 unregister_pernet_subsys(&xfrm_user_net_ops);
3648 module_init(xfrm_user_init);
3649 module_exit(xfrm_user_exit);
3650 MODULE_LICENSE("GPL");
3651 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);