1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
8 * Kazunori MIYAZAWA @USAGI
14 #include <linux/compat.h>
15 #include <linux/crypto.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/socket.h>
21 #include <linux/string.h>
22 #include <linux/net.h>
23 #include <linux/skbuff.h>
24 #include <linux/pfkeyv2.h>
25 #include <linux/ipsec.h>
26 #include <linux/init.h>
27 #include <linux/security.h>
30 #include <net/netlink.h>
32 #include <linux/uaccess.h>
33 #if IS_ENABLED(CONFIG_IPV6)
34 #include <linux/in6.h>
36 #include <asm/unaligned.h>
38 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type,
39 struct netlink_ext_ack *extack)
41 struct nlattr *rt = attrs[type];
42 struct xfrm_algo *algp;
48 if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
49 NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
60 NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
64 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
68 static int verify_auth_trunc(struct nlattr **attrs,
69 struct netlink_ext_ack *extack)
71 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
72 struct xfrm_algo_auth *algp;
78 if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
79 NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
83 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
87 static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
89 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
90 struct xfrm_algo_aead *algp;
96 if (nla_len(rt) < (int)aead_len(algp)) {
97 NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
101 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
105 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
106 xfrm_address_t **addrp)
108 struct nlattr *rt = attrs[type];
111 *addrp = nla_data(rt);
114 static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
116 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
117 struct xfrm_user_sec_ctx *uctx;
123 if (uctx->len > nla_len(rt) ||
124 uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
125 NL_SET_ERR_MSG(extack, "Invalid security context length");
132 static inline int verify_replay(struct xfrm_usersa_info *p,
133 struct nlattr **attrs, u8 sa_dir,
134 struct netlink_ext_ack *extack)
136 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
137 struct xfrm_replay_state_esn *rs;
140 if (p->flags & XFRM_STATE_ESN) {
141 NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
149 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
150 NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
154 if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
155 nla_len(rt) != sizeof(*rs)) {
156 NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
160 /* As only ESP and AH support ESN feature. */
161 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
162 NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
166 if (p->replay_window != 0) {
167 NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
171 if (sa_dir == XFRM_SA_DIR_OUT) {
172 if (rs->replay_window) {
173 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
176 if (rs->seq || rs->seq_hi) {
177 NL_SET_ERR_MSG(extack,
178 "Replay seq and seq_hi should be 0 for output SA");
182 NL_SET_ERR_MSG(extack, "Replay bmp_len should 0 for output SA");
187 if (sa_dir == XFRM_SA_DIR_IN) {
188 if (rs->oseq || rs->oseq_hi) {
189 NL_SET_ERR_MSG(extack,
190 "Replay oseq and oseq_hi should be 0 for input SA");
198 static int verify_newsa_info(struct xfrm_usersa_info *p,
199 struct nlattr **attrs,
200 struct netlink_ext_ack *extack)
203 u8 sa_dir = attrs[XFRMA_SA_DIR] ? nla_get_u8(attrs[XFRMA_SA_DIR]) : 0;
211 #if IS_ENABLED(CONFIG_IPV6)
215 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
220 NL_SET_ERR_MSG(extack, "Invalid address family");
224 switch (p->sel.family) {
229 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
230 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
237 #if IS_ENABLED(CONFIG_IPV6)
238 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
239 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
245 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
251 NL_SET_ERR_MSG(extack, "Invalid address family in selector");
256 switch (p->id.proto) {
258 if (!attrs[XFRMA_ALG_AUTH] &&
259 !attrs[XFRMA_ALG_AUTH_TRUNC]) {
260 NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
264 if (attrs[XFRMA_ALG_AEAD] ||
265 attrs[XFRMA_ALG_CRYPT] ||
266 attrs[XFRMA_ALG_COMP] ||
267 attrs[XFRMA_TFCPAD]) {
268 NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
274 if (attrs[XFRMA_ALG_COMP]) {
275 NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
279 if (!attrs[XFRMA_ALG_AUTH] &&
280 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
281 !attrs[XFRMA_ALG_CRYPT] &&
282 !attrs[XFRMA_ALG_AEAD]) {
283 NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
287 if ((attrs[XFRMA_ALG_AUTH] ||
288 attrs[XFRMA_ALG_AUTH_TRUNC] ||
289 attrs[XFRMA_ALG_CRYPT]) &&
290 attrs[XFRMA_ALG_AEAD]) {
291 NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
295 if (attrs[XFRMA_TFCPAD] &&
296 p->mode != XFRM_MODE_TUNNEL) {
297 NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
303 if (!attrs[XFRMA_ALG_COMP]) {
304 NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
308 if (attrs[XFRMA_ALG_AEAD] ||
309 attrs[XFRMA_ALG_AUTH] ||
310 attrs[XFRMA_ALG_AUTH_TRUNC] ||
311 attrs[XFRMA_ALG_CRYPT] ||
312 attrs[XFRMA_TFCPAD]) {
313 NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
317 if (ntohl(p->id.spi) >= 0x10000) {
318 NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
323 #if IS_ENABLED(CONFIG_IPV6)
324 case IPPROTO_DSTOPTS:
325 case IPPROTO_ROUTING:
326 if (attrs[XFRMA_ALG_COMP] ||
327 attrs[XFRMA_ALG_AUTH] ||
328 attrs[XFRMA_ALG_AUTH_TRUNC] ||
329 attrs[XFRMA_ALG_AEAD] ||
330 attrs[XFRMA_ALG_CRYPT] ||
331 attrs[XFRMA_ENCAP] ||
332 attrs[XFRMA_SEC_CTX] ||
333 attrs[XFRMA_TFCPAD]) {
334 NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
338 if (!attrs[XFRMA_COADDR]) {
339 NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
346 NL_SET_ERR_MSG(extack, "Unsupported protocol");
350 if ((err = verify_aead(attrs, extack)))
352 if ((err = verify_auth_trunc(attrs, extack)))
354 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
356 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
358 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
360 if ((err = verify_sec_ctx_len(attrs, extack)))
362 if ((err = verify_replay(p, attrs, sa_dir, extack)))
367 case XFRM_MODE_TRANSPORT:
368 case XFRM_MODE_TUNNEL:
369 case XFRM_MODE_ROUTEOPTIMIZATION:
374 NL_SET_ERR_MSG(extack, "Unsupported mode");
380 if (attrs[XFRMA_MTIMER_THRESH]) {
381 if (!attrs[XFRMA_ENCAP]) {
382 NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
387 if (sa_dir == XFRM_SA_DIR_OUT) {
388 NL_SET_ERR_MSG(extack,
389 "MTIMER_THRESH attribute should not be set on output SA");
395 if (sa_dir == XFRM_SA_DIR_OUT) {
396 if (p->flags & XFRM_STATE_DECAP_DSCP) {
397 NL_SET_ERR_MSG(extack, "Flag DECAP_DSCP should not be set for output SA");
402 if (p->flags & XFRM_STATE_ICMP) {
403 NL_SET_ERR_MSG(extack, "Flag ICMP should not be set for output SA");
408 if (p->flags & XFRM_STATE_WILDRECV) {
409 NL_SET_ERR_MSG(extack, "Flag WILDRECV should not be set for output SA");
414 if (p->replay_window) {
415 NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
420 if (attrs[XFRMA_REPLAY_VAL]) {
421 struct xfrm_replay_state *replay;
423 replay = nla_data(attrs[XFRMA_REPLAY_VAL]);
425 if (replay->seq || replay->bitmap) {
426 NL_SET_ERR_MSG(extack,
427 "Replay seq and bitmap should be 0 for output SA");
434 if (sa_dir == XFRM_SA_DIR_IN) {
435 if (p->flags & XFRM_STATE_NOPMTUDISC) {
436 NL_SET_ERR_MSG(extack, "Flag NOPMTUDISC should not be set for input SA");
441 if (attrs[XFRMA_SA_EXTRA_FLAGS]) {
442 u32 xflags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
444 if (xflags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) {
445 NL_SET_ERR_MSG(extack, "Flag DONT_ENCAP_DSCP should not be set for input SA");
450 if (xflags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP) {
451 NL_SET_ERR_MSG(extack, "Flag OSEQ_MAY_WRAP should not be set for input SA");
463 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
464 struct xfrm_algo_desc *(*get_byname)(const char *, int),
465 struct nlattr *rta, struct netlink_ext_ack *extack)
467 struct xfrm_algo *p, *ualg;
468 struct xfrm_algo_desc *algo;
473 ualg = nla_data(rta);
475 algo = get_byname(ualg->alg_name, 1);
477 NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
480 *props = algo->desc.sadb_alg_id;
482 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
486 strcpy(p->alg_name, algo->name);
491 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
492 struct netlink_ext_ack *extack)
494 struct xfrm_algo *p, *ualg;
495 struct xfrm_algo_desc *algo;
500 ualg = nla_data(rta);
502 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
504 NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
507 x->props.ealgo = algo->desc.sadb_alg_id;
509 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
513 strcpy(p->alg_name, algo->name);
515 x->geniv = algo->uinfo.encr.geniv;
519 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
520 struct nlattr *rta, struct netlink_ext_ack *extack)
522 struct xfrm_algo *ualg;
523 struct xfrm_algo_auth *p;
524 struct xfrm_algo_desc *algo;
529 ualg = nla_data(rta);
531 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
533 NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
536 *props = algo->desc.sadb_alg_id;
538 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
542 strcpy(p->alg_name, algo->name);
543 p->alg_key_len = ualg->alg_key_len;
544 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
545 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
551 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
552 struct nlattr *rta, struct netlink_ext_ack *extack)
554 struct xfrm_algo_auth *p, *ualg;
555 struct xfrm_algo_desc *algo;
560 ualg = nla_data(rta);
562 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
564 NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
567 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
568 NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
571 *props = algo->desc.sadb_alg_id;
573 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
577 strcpy(p->alg_name, algo->name);
578 if (!p->alg_trunc_len)
579 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
585 static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
586 struct netlink_ext_ack *extack)
588 struct xfrm_algo_aead *p, *ualg;
589 struct xfrm_algo_desc *algo;
594 ualg = nla_data(rta);
596 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
598 NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
601 x->props.ealgo = algo->desc.sadb_alg_id;
603 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
607 strcpy(p->alg_name, algo->name);
609 x->geniv = algo->uinfo.aead.geniv;
613 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
615 struct netlink_ext_ack *extack)
617 struct xfrm_replay_state_esn *up;
620 if (!replay_esn || !rp)
624 ulen = xfrm_replay_state_esn_len(up);
626 /* Check the overall length and the internal bitmap length to avoid
627 * potential overflow. */
628 if (nla_len(rp) < (int)ulen) {
629 NL_SET_ERR_MSG(extack, "ESN attribute is too short");
633 if (xfrm_replay_state_esn_len(replay_esn) != ulen) {
634 NL_SET_ERR_MSG(extack, "New ESN size doesn't match the existing SA's ESN size");
638 if (replay_esn->bmp_len != up->bmp_len) {
639 NL_SET_ERR_MSG(extack, "New ESN bitmap size doesn't match the existing SA's ESN bitmap");
643 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) {
644 NL_SET_ERR_MSG(extack, "ESN replay window is longer than the bitmap");
651 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
652 struct xfrm_replay_state_esn **preplay_esn,
655 struct xfrm_replay_state_esn *p, *pp, *up;
656 unsigned int klen, ulen;
662 klen = xfrm_replay_state_esn_len(up);
663 ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
665 p = kzalloc(klen, GFP_KERNEL);
669 pp = kzalloc(klen, GFP_KERNEL);
676 memcpy(pp, up, ulen);
684 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
686 unsigned int len = 0;
689 len += sizeof(struct xfrm_user_sec_ctx);
690 len += xfrm_ctx->ctx_len;
695 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
697 memcpy(&x->id, &p->id, sizeof(x->id));
698 memcpy(&x->sel, &p->sel, sizeof(x->sel));
699 memcpy(&x->lft, &p->lft, sizeof(x->lft));
700 x->props.mode = p->mode;
701 x->props.replay_window = min_t(unsigned int, p->replay_window,
702 sizeof(x->replay.bitmap) * 8);
703 x->props.reqid = p->reqid;
704 x->props.family = p->family;
705 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
706 x->props.flags = p->flags;
708 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
709 x->sel.family = p->family;
713 * someday when pfkey also has support, we could have the code
714 * somehow made shareable and move it to xfrm_state.c - JHS
717 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
720 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
721 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
722 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
723 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
724 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
725 struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
727 if (re && x->replay_esn && x->preplay_esn) {
728 struct xfrm_replay_state_esn *replay_esn;
729 replay_esn = nla_data(re);
730 memcpy(x->replay_esn, replay_esn,
731 xfrm_replay_state_esn_len(replay_esn));
732 memcpy(x->preplay_esn, replay_esn,
733 xfrm_replay_state_esn_len(replay_esn));
737 struct xfrm_replay_state *replay;
738 replay = nla_data(rp);
739 memcpy(&x->replay, replay, sizeof(*replay));
740 memcpy(&x->preplay, replay, sizeof(*replay));
744 struct xfrm_lifetime_cur *ltime;
745 ltime = nla_data(lt);
746 x->curlft.bytes = ltime->bytes;
747 x->curlft.packets = ltime->packets;
748 x->curlft.add_time = ltime->add_time;
749 x->curlft.use_time = ltime->use_time;
753 x->replay_maxage = nla_get_u32(et);
756 x->replay_maxdiff = nla_get_u32(rt);
759 x->mapping_maxage = nla_get_u32(mt);
762 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
764 if (attrs[XFRMA_SET_MARK]) {
765 m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
766 if (attrs[XFRMA_SET_MARK_MASK])
767 m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
775 static struct xfrm_state *xfrm_state_construct(struct net *net,
776 struct xfrm_usersa_info *p,
777 struct nlattr **attrs,
779 struct netlink_ext_ack *extack)
781 struct xfrm_state *x = xfrm_state_alloc(net);
787 copy_from_user_state(x, p);
789 if (attrs[XFRMA_ENCAP]) {
790 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
791 sizeof(*x->encap), GFP_KERNEL);
792 if (x->encap == NULL)
796 if (attrs[XFRMA_COADDR]) {
797 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
798 sizeof(*x->coaddr), GFP_KERNEL);
799 if (x->coaddr == NULL)
803 if (attrs[XFRMA_SA_EXTRA_FLAGS])
804 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
806 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
808 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
809 attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
811 if (!x->props.aalgo) {
812 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
813 attrs[XFRMA_ALG_AUTH], extack)))
816 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
818 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
819 xfrm_calg_get_byname,
820 attrs[XFRMA_ALG_COMP], extack)))
823 if (attrs[XFRMA_TFCPAD])
824 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
826 xfrm_mark_get(attrs, &x->mark);
828 xfrm_smark_init(attrs, &x->props.smark);
830 if (attrs[XFRMA_IF_ID])
831 x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
833 if (attrs[XFRMA_SA_DIR])
834 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
836 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
840 if (attrs[XFRMA_SEC_CTX]) {
841 err = security_xfrm_state_alloc(x,
842 nla_data(attrs[XFRMA_SEC_CTX]));
847 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
848 attrs[XFRMA_REPLAY_ESN_VAL])))
852 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
853 /* sysctl_xfrm_aevent_etime is in 100ms units */
854 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
856 if ((err = xfrm_init_replay(x, extack)))
859 /* override default values from above */
860 xfrm_update_ae_params(x, attrs, 0);
862 /* configure the hardware if offload is requested */
863 if (attrs[XFRMA_OFFLOAD_DEV]) {
864 err = xfrm_dev_state_add(net, x,
865 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
874 x->km.state = XFRM_STATE_DEAD;
881 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
882 struct nlattr **attrs, struct netlink_ext_ack *extack)
884 struct net *net = sock_net(skb->sk);
885 struct xfrm_usersa_info *p = nlmsg_data(nlh);
886 struct xfrm_state *x;
890 err = verify_newsa_info(p, attrs, extack);
894 x = xfrm_state_construct(net, p, attrs, &err, extack);
899 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
900 err = xfrm_state_add(x);
902 err = xfrm_state_update(x);
904 xfrm_audit_state_add(x, err ? 0 : 1, true);
907 x->km.state = XFRM_STATE_DEAD;
908 xfrm_dev_state_delete(x);
913 if (x->km.state == XFRM_STATE_VOID)
914 x->km.state = XFRM_STATE_VALID;
916 c.seq = nlh->nlmsg_seq;
917 c.portid = nlh->nlmsg_pid;
918 c.event = nlh->nlmsg_type;
920 km_state_notify(x, &c);
926 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
927 struct xfrm_usersa_id *p,
928 struct nlattr **attrs,
931 struct xfrm_state *x = NULL;
934 u32 mark = xfrm_mark_get(attrs, &m);
936 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
938 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
940 xfrm_address_t *saddr = NULL;
942 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
949 x = xfrm_state_lookup_byaddr(net, mark,
951 p->proto, p->family);
960 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
961 struct nlattr **attrs, struct netlink_ext_ack *extack)
963 struct net *net = sock_net(skb->sk);
964 struct xfrm_state *x;
967 struct xfrm_usersa_id *p = nlmsg_data(nlh);
969 x = xfrm_user_state_lookup(net, p, attrs, &err);
973 if ((err = security_xfrm_state_delete(x)) != 0)
976 if (xfrm_state_kern(x)) {
977 NL_SET_ERR_MSG(extack, "SA is in use by tunnels");
982 err = xfrm_state_delete(x);
986 c.seq = nlh->nlmsg_seq;
987 c.portid = nlh->nlmsg_pid;
988 c.event = nlh->nlmsg_type;
989 km_state_notify(x, &c);
992 xfrm_audit_state_delete(x, err ? 0 : 1, true);
997 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
999 memset(p, 0, sizeof(*p));
1000 memcpy(&p->id, &x->id, sizeof(p->id));
1001 memcpy(&p->sel, &x->sel, sizeof(p->sel));
1002 memcpy(&p->lft, &x->lft, sizeof(p->lft));
1004 xfrm_dev_state_update_stats(x);
1005 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
1006 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
1007 put_unaligned(x->stats.replay, &p->stats.replay);
1008 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
1009 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
1010 p->mode = x->props.mode;
1011 p->replay_window = x->props.replay_window;
1012 p->reqid = x->props.reqid;
1013 p->family = x->props.family;
1014 p->flags = x->props.flags;
1018 struct xfrm_dump_info {
1019 struct sk_buff *in_skb;
1020 struct sk_buff *out_skb;
1025 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
1027 struct xfrm_user_sec_ctx *uctx;
1028 struct nlattr *attr;
1029 int ctx_size = sizeof(*uctx) + s->ctx_len;
1031 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
1035 uctx = nla_data(attr);
1036 uctx->exttype = XFRMA_SEC_CTX;
1037 uctx->len = ctx_size;
1038 uctx->ctx_doi = s->ctx_doi;
1039 uctx->ctx_alg = s->ctx_alg;
1040 uctx->ctx_len = s->ctx_len;
1041 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
1046 static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
1048 struct xfrm_user_offload *xuo;
1049 struct nlattr *attr;
1051 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
1055 xuo = nla_data(attr);
1056 memset(xuo, 0, sizeof(*xuo));
1057 xuo->ifindex = xso->dev->ifindex;
1058 if (xso->dir == XFRM_DEV_OFFLOAD_IN)
1059 xuo->flags = XFRM_OFFLOAD_INBOUND;
1060 if (xso->type == XFRM_DEV_OFFLOAD_PACKET)
1061 xuo->flags |= XFRM_OFFLOAD_PACKET;
1066 static bool xfrm_redact(void)
1068 return IS_ENABLED(CONFIG_SECURITY) &&
1069 security_locked_down(LOCKDOWN_XFRM_SECRET);
1072 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
1074 struct xfrm_algo *algo;
1075 struct xfrm_algo_auth *ap;
1077 bool redact_secret = xfrm_redact();
1079 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
1080 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
1083 algo = nla_data(nla);
1084 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
1086 if (redact_secret && auth->alg_key_len)
1087 memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
1089 memcpy(algo->alg_key, auth->alg_key,
1090 (auth->alg_key_len + 7) / 8);
1091 algo->alg_key_len = auth->alg_key_len;
1093 nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
1097 memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
1098 if (redact_secret && auth->alg_key_len)
1099 memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
1101 memcpy(ap->alg_key, auth->alg_key,
1102 (auth->alg_key_len + 7) / 8);
1106 static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
1108 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
1109 struct xfrm_algo_aead *ap;
1110 bool redact_secret = xfrm_redact();
1116 strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
1117 ap->alg_key_len = aead->alg_key_len;
1118 ap->alg_icv_len = aead->alg_icv_len;
1120 if (redact_secret && aead->alg_key_len)
1121 memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
1123 memcpy(ap->alg_key, aead->alg_key,
1124 (aead->alg_key_len + 7) / 8);
1128 static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
1130 struct xfrm_algo *ap;
1131 bool redact_secret = xfrm_redact();
1132 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
1133 xfrm_alg_len(ealg));
1138 strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
1139 ap->alg_key_len = ealg->alg_key_len;
1141 if (redact_secret && ealg->alg_key_len)
1142 memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
1144 memcpy(ap->alg_key, ealg->alg_key,
1145 (ealg->alg_key_len + 7) / 8);
1150 static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
1152 struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
1153 struct xfrm_algo *ap;
1159 strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
1160 ap->alg_key_len = 0;
1165 static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
1167 struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
1168 struct xfrm_encap_tmpl *uep;
1173 uep = nla_data(nla);
1174 memset(uep, 0, sizeof(*uep));
1176 uep->encap_type = ep->encap_type;
1177 uep->encap_sport = ep->encap_sport;
1178 uep->encap_dport = ep->encap_dport;
1179 uep->encap_oa = ep->encap_oa;
1184 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
1189 ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
1191 ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
1196 /* Don't change this without updating xfrm_sa_len! */
1197 static int copy_to_user_state_extra(struct xfrm_state *x,
1198 struct xfrm_usersa_info *p,
1199 struct sk_buff *skb)
1203 copy_to_user_state(x, p);
1205 if (x->props.extra_flags) {
1206 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
1207 x->props.extra_flags);
1213 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
1218 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
1224 ret = copy_to_user_aead(x->aead, skb);
1229 ret = copy_to_user_auth(x->aalg, skb);
1234 ret = copy_to_user_ealg(x->ealg, skb);
1239 ret = copy_to_user_calg(x->calg, skb);
1244 ret = copy_to_user_encap(x->encap, skb);
1249 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
1253 ret = xfrm_mark_put(skb, &x->mark);
1257 ret = xfrm_smark_put(skb, &x->props.smark);
1262 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1263 xfrm_replay_state_esn_len(x->replay_esn),
1266 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1271 ret = copy_user_offload(&x->xso, skb);
1275 ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
1280 ret = copy_sec_ctx(x->security, skb);
1284 if (x->mapping_maxage) {
1285 ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
1290 ret = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
1295 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
1297 struct xfrm_dump_info *sp = ptr;
1298 struct sk_buff *in_skb = sp->in_skb;
1299 struct sk_buff *skb = sp->out_skb;
1300 struct xfrm_translator *xtr;
1301 struct xfrm_usersa_info *p;
1302 struct nlmsghdr *nlh;
1305 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1306 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
1310 p = nlmsg_data(nlh);
1312 err = copy_to_user_state_extra(x, p, skb);
1314 nlmsg_cancel(skb, nlh);
1317 nlmsg_end(skb, nlh);
1319 xtr = xfrm_get_translator();
1321 err = xtr->alloc_compat(skb, nlh);
1323 xfrm_put_translator(xtr);
1325 nlmsg_cancel(skb, nlh);
1333 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1335 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1336 struct sock *sk = cb->skb->sk;
1337 struct net *net = sock_net(sk);
1340 xfrm_state_walk_done(walk, net);
1344 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1346 struct net *net = sock_net(skb->sk);
1347 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1348 struct xfrm_dump_info info;
1350 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1351 sizeof(cb->args) - sizeof(cb->args[0]));
1353 info.in_skb = cb->skb;
1355 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1356 info.nlmsg_flags = NLM_F_MULTI;
1359 struct nlattr *attrs[XFRMA_MAX+1];
1360 struct xfrm_address_filter *filter = NULL;
1364 err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1365 xfrma_policy, cb->extack);
1369 if (attrs[XFRMA_ADDRESS_FILTER]) {
1370 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1371 sizeof(*filter), GFP_KERNEL);
1375 /* see addr_match(), (prefix length >> 5) << 2
1376 * will be used to compare xfrm_address_t
1378 if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
1379 filter->dplen > (sizeof(xfrm_address_t) << 3)) {
1385 if (attrs[XFRMA_PROTO])
1386 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1388 xfrm_state_walk_init(walk, proto, filter);
1392 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1397 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1398 struct xfrm_state *x, u32 seq)
1400 struct xfrm_dump_info info;
1401 struct sk_buff *skb;
1404 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1406 return ERR_PTR(-ENOMEM);
1408 info.in_skb = in_skb;
1410 info.nlmsg_seq = seq;
1411 info.nlmsg_flags = 0;
1413 err = dump_one_state(x, 0, &info);
1416 return ERR_PTR(err);
1422 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1423 * Must be called with RCU read lock.
1425 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1426 u32 pid, unsigned int group)
1428 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1429 struct xfrm_translator *xtr;
1436 xtr = xfrm_get_translator();
1438 int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1440 xfrm_put_translator(xtr);
1447 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1450 static inline unsigned int xfrm_spdinfo_msgsize(void)
1452 return NLMSG_ALIGN(4)
1453 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1454 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1455 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1456 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1459 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1460 u32 portid, u32 seq, u32 flags)
1462 struct xfrmk_spdinfo si;
1463 struct xfrmu_spdinfo spc;
1464 struct xfrmu_spdhinfo sph;
1465 struct xfrmu_spdhthresh spt4, spt6;
1466 struct nlmsghdr *nlh;
1471 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1472 if (nlh == NULL) /* shouldn't really happen ... */
1475 f = nlmsg_data(nlh);
1477 xfrm_spd_getinfo(net, &si);
1478 spc.incnt = si.incnt;
1479 spc.outcnt = si.outcnt;
1480 spc.fwdcnt = si.fwdcnt;
1481 spc.inscnt = si.inscnt;
1482 spc.outscnt = si.outscnt;
1483 spc.fwdscnt = si.fwdscnt;
1484 sph.spdhcnt = si.spdhcnt;
1485 sph.spdhmcnt = si.spdhmcnt;
1488 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1490 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1491 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1492 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1493 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1494 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1496 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1498 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1500 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1502 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1504 nlmsg_cancel(skb, nlh);
1508 nlmsg_end(skb, nlh);
1512 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1513 struct nlattr **attrs,
1514 struct netlink_ext_ack *extack)
1516 struct net *net = sock_net(skb->sk);
1517 struct xfrmu_spdhthresh *thresh4 = NULL;
1518 struct xfrmu_spdhthresh *thresh6 = NULL;
1520 /* selector prefixlen thresholds to hash policies */
1521 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1522 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1524 if (nla_len(rta) < sizeof(*thresh4)) {
1525 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length");
1528 thresh4 = nla_data(rta);
1529 if (thresh4->lbits > 32 || thresh4->rbits > 32) {
1530 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)");
1534 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1535 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1537 if (nla_len(rta) < sizeof(*thresh6)) {
1538 NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length");
1541 thresh6 = nla_data(rta);
1542 if (thresh6->lbits > 128 || thresh6->rbits > 128) {
1543 NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)");
1548 if (thresh4 || thresh6) {
1549 write_seqlock(&net->xfrm.policy_hthresh.lock);
1551 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1552 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1555 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1556 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1558 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1560 xfrm_policy_hash_rebuild(net);
1566 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1567 struct nlattr **attrs,
1568 struct netlink_ext_ack *extack)
1570 struct net *net = sock_net(skb->sk);
1571 struct sk_buff *r_skb;
1572 u32 *flags = nlmsg_data(nlh);
1573 u32 sportid = NETLINK_CB(skb).portid;
1574 u32 seq = nlh->nlmsg_seq;
1577 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1581 err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1584 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1587 static inline unsigned int xfrm_sadinfo_msgsize(void)
1589 return NLMSG_ALIGN(4)
1590 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1591 + nla_total_size(4); /* XFRMA_SAD_CNT */
1594 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1595 u32 portid, u32 seq, u32 flags)
1597 struct xfrmk_sadinfo si;
1598 struct xfrmu_sadhinfo sh;
1599 struct nlmsghdr *nlh;
1603 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1604 if (nlh == NULL) /* shouldn't really happen ... */
1607 f = nlmsg_data(nlh);
1609 xfrm_sad_getinfo(net, &si);
1611 sh.sadhmcnt = si.sadhmcnt;
1612 sh.sadhcnt = si.sadhcnt;
1614 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1616 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1618 nlmsg_cancel(skb, nlh);
1622 nlmsg_end(skb, nlh);
1626 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1627 struct nlattr **attrs,
1628 struct netlink_ext_ack *extack)
1630 struct net *net = sock_net(skb->sk);
1631 struct sk_buff *r_skb;
1632 u32 *flags = nlmsg_data(nlh);
1633 u32 sportid = NETLINK_CB(skb).portid;
1634 u32 seq = nlh->nlmsg_seq;
1637 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1641 err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1644 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1647 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1648 struct nlattr **attrs, struct netlink_ext_ack *extack)
1650 struct net *net = sock_net(skb->sk);
1651 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1652 struct xfrm_state *x;
1653 struct sk_buff *resp_skb;
1656 x = xfrm_user_state_lookup(net, p, attrs, &err);
1660 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1661 if (IS_ERR(resp_skb)) {
1662 err = PTR_ERR(resp_skb);
1664 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1671 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1672 struct nlattr **attrs,
1673 struct netlink_ext_ack *extack)
1675 struct net *net = sock_net(skb->sk);
1676 struct xfrm_state *x;
1677 struct xfrm_userspi_info *p;
1678 struct xfrm_translator *xtr;
1679 struct sk_buff *resp_skb;
1680 xfrm_address_t *daddr;
1687 p = nlmsg_data(nlh);
1688 err = verify_spi_info(p->info.id.proto, p->min, p->max, extack);
1692 family = p->info.family;
1693 daddr = &p->info.id.daddr;
1697 mark = xfrm_mark_get(attrs, &m);
1699 if (attrs[XFRMA_IF_ID])
1700 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1703 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1704 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1711 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1712 if_id, p->info.id.proto, daddr,
1717 NL_SET_ERR_MSG(extack, "Target ACQUIRE not found");
1721 err = xfrm_alloc_spi(x, p->min, p->max, extack);
1725 if (attrs[XFRMA_SA_DIR])
1726 x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
1728 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1729 if (IS_ERR(resp_skb)) {
1730 err = PTR_ERR(resp_skb);
1734 xtr = xfrm_get_translator();
1736 err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1738 xfrm_put_translator(xtr);
1740 kfree_skb(resp_skb);
1745 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1753 static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
1756 case XFRM_POLICY_IN:
1757 case XFRM_POLICY_OUT:
1758 case XFRM_POLICY_FWD:
1762 NL_SET_ERR_MSG(extack, "Invalid policy direction");
1769 static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
1772 case XFRM_POLICY_TYPE_MAIN:
1773 #ifdef CONFIG_XFRM_SUB_POLICY
1774 case XFRM_POLICY_TYPE_SUB:
1779 NL_SET_ERR_MSG(extack, "Invalid policy type");
1786 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
1787 struct netlink_ext_ack *extack)
1792 case XFRM_SHARE_ANY:
1793 case XFRM_SHARE_SESSION:
1794 case XFRM_SHARE_USER:
1795 case XFRM_SHARE_UNIQUE:
1799 NL_SET_ERR_MSG(extack, "Invalid policy share");
1803 switch (p->action) {
1804 case XFRM_POLICY_ALLOW:
1805 case XFRM_POLICY_BLOCK:
1809 NL_SET_ERR_MSG(extack, "Invalid policy action");
1813 switch (p->sel.family) {
1815 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
1816 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
1823 #if IS_ENABLED(CONFIG_IPV6)
1824 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
1825 NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
1831 NL_SET_ERR_MSG(extack, "IPv6 support disabled");
1832 return -EAFNOSUPPORT;
1836 NL_SET_ERR_MSG(extack, "Invalid selector family");
1840 ret = verify_policy_dir(p->dir, extack);
1843 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
1844 NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
1851 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1853 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1854 struct xfrm_user_sec_ctx *uctx;
1859 uctx = nla_data(rt);
1860 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1863 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1869 for (i = 0; i < nr; i++, ut++) {
1870 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1872 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1873 memcpy(&t->saddr, &ut->saddr,
1874 sizeof(xfrm_address_t));
1875 t->reqid = ut->reqid;
1877 t->share = ut->share;
1878 t->optional = ut->optional;
1879 t->aalgos = ut->aalgos;
1880 t->ealgos = ut->ealgos;
1881 t->calgos = ut->calgos;
1882 /* If all masks are ~0, then we allow all algorithms. */
1883 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1884 t->encap_family = ut->family;
1888 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
1889 int dir, struct netlink_ext_ack *extack)
1894 if (nr > XFRM_MAX_DEPTH) {
1895 NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
1899 prev_family = family;
1901 for (i = 0; i < nr; i++) {
1902 /* We never validated the ut->family value, so many
1903 * applications simply leave it at zero. The check was
1904 * never made and ut->family was ignored because all
1905 * templates could be assumed to have the same family as
1906 * the policy itself. Now that we will have ipv4-in-ipv6
1907 * and ipv6-in-ipv4 tunnels, this is no longer true.
1910 ut[i].family = family;
1912 switch (ut[i].mode) {
1913 case XFRM_MODE_TUNNEL:
1914 case XFRM_MODE_BEET:
1915 if (ut[i].optional && dir == XFRM_POLICY_OUT) {
1916 NL_SET_ERR_MSG(extack, "Mode in optional template not allowed in outbound policy");
1921 if (ut[i].family != prev_family) {
1922 NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
1927 if (ut[i].mode >= XFRM_MODE_MAX) {
1928 NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
1932 prev_family = ut[i].family;
1934 switch (ut[i].family) {
1937 #if IS_ENABLED(CONFIG_IPV6)
1942 NL_SET_ERR_MSG(extack, "Invalid family in template");
1946 if (!xfrm_id_proto_valid(ut[i].id.proto)) {
1947 NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
1955 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
1956 int dir, struct netlink_ext_ack *extack)
1958 struct nlattr *rt = attrs[XFRMA_TMPL];
1963 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1964 int nr = nla_len(rt) / sizeof(*utmpl);
1967 err = validate_tmpl(nr, utmpl, pol->family, dir, extack);
1971 copy_templates(pol, utmpl, nr);
1976 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
1977 struct netlink_ext_ack *extack)
1979 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1980 struct xfrm_userpolicy_type *upt;
1981 u8 type = XFRM_POLICY_TYPE_MAIN;
1989 err = verify_policy_type(type, extack);
1997 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1999 xp->priority = p->priority;
2000 xp->index = p->index;
2001 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
2002 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
2003 xp->action = p->action;
2004 xp->flags = p->flags;
2005 xp->family = p->sel.family;
2006 /* XXX xp->share = p->share; */
2009 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
2011 memset(p, 0, sizeof(*p));
2012 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
2013 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
2014 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
2015 p->priority = xp->priority;
2016 p->index = xp->index;
2017 p->sel.family = xp->family;
2019 p->action = xp->action;
2020 p->flags = xp->flags;
2021 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
2024 static struct xfrm_policy *xfrm_policy_construct(struct net *net,
2025 struct xfrm_userpolicy_info *p,
2026 struct nlattr **attrs,
2028 struct netlink_ext_ack *extack)
2030 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
2038 copy_from_user_policy(xp, p);
2040 err = copy_from_user_policy_type(&xp->type, attrs, extack);
2044 if (!(err = copy_from_user_tmpl(xp, attrs, p->dir, extack)))
2045 err = copy_from_user_sec_ctx(xp, attrs);
2049 xfrm_mark_get(attrs, &xp->mark);
2051 if (attrs[XFRMA_IF_ID])
2052 xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2054 /* configure the hardware if offload is requested */
2055 if (attrs[XFRMA_OFFLOAD_DEV]) {
2056 err = xfrm_dev_policy_add(net, xp,
2057 nla_data(attrs[XFRMA_OFFLOAD_DEV]),
2067 xfrm_policy_destroy(xp);
2071 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2072 struct nlattr **attrs,
2073 struct netlink_ext_ack *extack)
2075 struct net *net = sock_net(skb->sk);
2076 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
2077 struct xfrm_policy *xp;
2082 err = verify_newpolicy_info(p, extack);
2085 err = verify_sec_ctx_len(attrs, extack);
2089 xp = xfrm_policy_construct(net, p, attrs, &err, extack);
2093 /* shouldn't excl be based on nlh flags??
2094 * Aha! this is anti-netlink really i.e more pfkey derived
2095 * in netlink excl is a flag and you wouldn't need
2096 * a type XFRM_MSG_UPDPOLICY - JHS */
2097 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
2098 err = xfrm_policy_insert(p->dir, xp, excl);
2099 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2102 xfrm_dev_policy_delete(xp);
2103 xfrm_dev_policy_free(xp);
2104 security_xfrm_policy_free(xp->security);
2109 c.event = nlh->nlmsg_type;
2110 c.seq = nlh->nlmsg_seq;
2111 c.portid = nlh->nlmsg_pid;
2112 km_policy_notify(xp, p->dir, &c);
2119 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
2121 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
2124 if (xp->xfrm_nr == 0)
2127 if (xp->xfrm_nr > XFRM_MAX_DEPTH)
2130 for (i = 0; i < xp->xfrm_nr; i++) {
2131 struct xfrm_user_tmpl *up = &vec[i];
2132 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
2134 memset(up, 0, sizeof(*up));
2135 memcpy(&up->id, &kp->id, sizeof(up->id));
2136 up->family = kp->encap_family;
2137 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
2138 up->reqid = kp->reqid;
2139 up->mode = kp->mode;
2140 up->share = kp->share;
2141 up->optional = kp->optional;
2142 up->aalgos = kp->aalgos;
2143 up->ealgos = kp->ealgos;
2144 up->calgos = kp->calgos;
2147 return nla_put(skb, XFRMA_TMPL,
2148 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
2151 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
2154 return copy_sec_ctx(x->security, skb);
2159 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
2162 return copy_sec_ctx(xp->security, skb);
2165 static inline unsigned int userpolicy_type_attrsize(void)
2167 #ifdef CONFIG_XFRM_SUB_POLICY
2168 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
2174 #ifdef CONFIG_XFRM_SUB_POLICY
2175 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
2177 struct xfrm_userpolicy_type upt;
2179 /* Sadly there are two holes in struct xfrm_userpolicy_type */
2180 memset(&upt, 0, sizeof(upt));
2183 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
2187 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
2193 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
2195 struct xfrm_dump_info *sp = ptr;
2196 struct xfrm_userpolicy_info *p;
2197 struct sk_buff *in_skb = sp->in_skb;
2198 struct sk_buff *skb = sp->out_skb;
2199 struct xfrm_translator *xtr;
2200 struct nlmsghdr *nlh;
2203 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
2204 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
2208 p = nlmsg_data(nlh);
2209 copy_to_user_policy(xp, p, dir);
2210 err = copy_to_user_tmpl(xp, skb);
2212 err = copy_to_user_sec_ctx(xp, skb);
2214 err = copy_to_user_policy_type(xp->type, skb);
2216 err = xfrm_mark_put(skb, &xp->mark);
2218 err = xfrm_if_id_put(skb, xp->if_id);
2219 if (!err && xp->xdo.dev)
2220 err = copy_user_offload(&xp->xdo, skb);
2222 nlmsg_cancel(skb, nlh);
2225 nlmsg_end(skb, nlh);
2227 xtr = xfrm_get_translator();
2229 err = xtr->alloc_compat(skb, nlh);
2231 xfrm_put_translator(xtr);
2233 nlmsg_cancel(skb, nlh);
2241 static int xfrm_dump_policy_done(struct netlink_callback *cb)
2243 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2244 struct net *net = sock_net(cb->skb->sk);
2246 xfrm_policy_walk_done(walk, net);
2250 static int xfrm_dump_policy_start(struct netlink_callback *cb)
2252 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2254 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
2256 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
2260 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
2262 struct net *net = sock_net(skb->sk);
2263 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
2264 struct xfrm_dump_info info;
2266 info.in_skb = cb->skb;
2268 info.nlmsg_seq = cb->nlh->nlmsg_seq;
2269 info.nlmsg_flags = NLM_F_MULTI;
2271 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
2276 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
2277 struct xfrm_policy *xp,
2280 struct xfrm_dump_info info;
2281 struct sk_buff *skb;
2284 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2286 return ERR_PTR(-ENOMEM);
2288 info.in_skb = in_skb;
2290 info.nlmsg_seq = seq;
2291 info.nlmsg_flags = 0;
2293 err = dump_one_policy(xp, dir, 0, &info);
2296 return ERR_PTR(err);
2302 static int xfrm_notify_userpolicy(struct net *net)
2304 struct xfrm_userpolicy_default *up;
2305 int len = NLMSG_ALIGN(sizeof(*up));
2306 struct nlmsghdr *nlh;
2307 struct sk_buff *skb;
2310 skb = nlmsg_new(len, GFP_ATOMIC);
2314 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
2320 up = nlmsg_data(nlh);
2321 up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2322 up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2323 up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2325 nlmsg_end(skb, nlh);
2328 err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2334 static bool xfrm_userpolicy_is_valid(__u8 policy)
2336 return policy == XFRM_USERPOLICY_BLOCK ||
2337 policy == XFRM_USERPOLICY_ACCEPT;
2340 static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2341 struct nlattr **attrs, struct netlink_ext_ack *extack)
2343 struct net *net = sock_net(skb->sk);
2344 struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
2346 if (xfrm_userpolicy_is_valid(up->in))
2347 net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
2349 if (xfrm_userpolicy_is_valid(up->fwd))
2350 net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
2352 if (xfrm_userpolicy_is_valid(up->out))
2353 net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
2355 rt_genid_bump_all(net);
2357 xfrm_notify_userpolicy(net);
2361 static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
2362 struct nlattr **attrs, struct netlink_ext_ack *extack)
2364 struct sk_buff *r_skb;
2365 struct nlmsghdr *r_nlh;
2366 struct net *net = sock_net(skb->sk);
2367 struct xfrm_userpolicy_default *r_up;
2368 int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
2369 u32 portid = NETLINK_CB(skb).portid;
2370 u32 seq = nlh->nlmsg_seq;
2372 r_skb = nlmsg_new(len, GFP_ATOMIC);
2376 r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
2382 r_up = nlmsg_data(r_nlh);
2383 r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
2384 r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
2385 r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
2386 nlmsg_end(r_skb, r_nlh);
2388 return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
2391 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2392 struct nlattr **attrs,
2393 struct netlink_ext_ack *extack)
2395 struct net *net = sock_net(skb->sk);
2396 struct xfrm_policy *xp;
2397 struct xfrm_userpolicy_id *p;
2398 u8 type = XFRM_POLICY_TYPE_MAIN;
2405 p = nlmsg_data(nlh);
2406 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
2408 err = copy_from_user_policy_type(&type, attrs, extack);
2412 err = verify_policy_dir(p->dir, extack);
2416 if (attrs[XFRMA_IF_ID])
2417 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2419 xfrm_mark_get(attrs, &m);
2422 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
2423 p->index, delete, &err);
2425 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2426 struct xfrm_sec_ctx *ctx;
2428 err = verify_sec_ctx_len(attrs, extack);
2434 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2436 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2440 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2441 &p->sel, ctx, delete, &err);
2442 security_xfrm_policy_free(ctx);
2448 struct sk_buff *resp_skb;
2450 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
2451 if (IS_ERR(resp_skb)) {
2452 err = PTR_ERR(resp_skb);
2454 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
2455 NETLINK_CB(skb).portid);
2458 xfrm_dev_policy_delete(xp);
2459 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2464 c.data.byid = p->index;
2465 c.event = nlh->nlmsg_type;
2466 c.seq = nlh->nlmsg_seq;
2467 c.portid = nlh->nlmsg_pid;
2468 km_policy_notify(xp, p->dir, &c);
2476 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
2477 struct nlattr **attrs,
2478 struct netlink_ext_ack *extack)
2480 struct net *net = sock_net(skb->sk);
2482 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
2485 err = xfrm_state_flush(net, p->proto, true, false);
2487 if (err == -ESRCH) /* empty table */
2491 c.data.proto = p->proto;
2492 c.event = nlh->nlmsg_type;
2493 c.seq = nlh->nlmsg_seq;
2494 c.portid = nlh->nlmsg_pid;
2496 km_state_notify(NULL, &c);
2501 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2503 unsigned int replay_size = x->replay_esn ?
2504 xfrm_replay_state_esn_len(x->replay_esn) :
2505 sizeof(struct xfrm_replay_state);
2507 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2508 + nla_total_size(replay_size)
2509 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2510 + nla_total_size(sizeof(struct xfrm_mark))
2511 + nla_total_size(4) /* XFRM_AE_RTHR */
2512 + nla_total_size(4) /* XFRM_AE_ETHR */
2513 + nla_total_size(sizeof(x->dir)); /* XFRMA_SA_DIR */
2516 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2518 struct xfrm_aevent_id *id;
2519 struct nlmsghdr *nlh;
2522 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2526 id = nlmsg_data(nlh);
2527 memset(&id->sa_id, 0, sizeof(id->sa_id));
2528 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2529 id->sa_id.spi = x->id.spi;
2530 id->sa_id.family = x->props.family;
2531 id->sa_id.proto = x->id.proto;
2532 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2533 id->reqid = x->props.reqid;
2534 id->flags = c->data.aevent;
2536 if (x->replay_esn) {
2537 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2538 xfrm_replay_state_esn_len(x->replay_esn),
2541 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2546 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2551 if (id->flags & XFRM_AE_RTHR) {
2552 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2556 if (id->flags & XFRM_AE_ETHR) {
2557 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2558 x->replay_maxage * 10 / HZ);
2562 err = xfrm_mark_put(skb, &x->mark);
2566 err = xfrm_if_id_put(skb, x->if_id);
2571 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
2576 nlmsg_end(skb, nlh);
2580 nlmsg_cancel(skb, nlh);
2584 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2585 struct nlattr **attrs, struct netlink_ext_ack *extack)
2587 struct net *net = sock_net(skb->sk);
2588 struct xfrm_state *x;
2589 struct sk_buff *r_skb;
2594 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2595 struct xfrm_usersa_id *id = &p->sa_id;
2597 mark = xfrm_mark_get(attrs, &m);
2599 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2603 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2604 if (r_skb == NULL) {
2610 * XXX: is this lock really needed - none of the other
2611 * gets lock (the concern is things getting updated
2612 * while we are still reading) - jhs
2614 spin_lock_bh(&x->lock);
2615 c.data.aevent = p->flags;
2616 c.seq = nlh->nlmsg_seq;
2617 c.portid = nlh->nlmsg_pid;
2619 err = build_aevent(r_skb, x, &c);
2622 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2623 spin_unlock_bh(&x->lock);
2628 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2629 struct nlattr **attrs, struct netlink_ext_ack *extack)
2631 struct net *net = sock_net(skb->sk);
2632 struct xfrm_state *x;
2637 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2638 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2639 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2640 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2641 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2642 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2644 if (!lt && !rp && !re && !et && !rt) {
2645 NL_SET_ERR_MSG(extack, "Missing required attribute for AE");
2649 /* pedantic mode - thou shalt sayeth replaceth */
2650 if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) {
2651 NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required");
2655 mark = xfrm_mark_get(attrs, &m);
2657 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2661 if (x->km.state != XFRM_STATE_VALID) {
2662 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
2666 err = xfrm_replay_verify_len(x->replay_esn, re, extack);
2670 spin_lock_bh(&x->lock);
2671 xfrm_update_ae_params(x, attrs, 1);
2672 spin_unlock_bh(&x->lock);
2674 c.event = nlh->nlmsg_type;
2675 c.seq = nlh->nlmsg_seq;
2676 c.portid = nlh->nlmsg_pid;
2677 c.data.aevent = XFRM_AE_CU;
2678 km_state_notify(x, &c);
2685 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2686 struct nlattr **attrs,
2687 struct netlink_ext_ack *extack)
2689 struct net *net = sock_net(skb->sk);
2691 u8 type = XFRM_POLICY_TYPE_MAIN;
2694 err = copy_from_user_policy_type(&type, attrs, extack);
2698 err = xfrm_policy_flush(net, type, true);
2700 if (err == -ESRCH) /* empty table */
2706 c.event = nlh->nlmsg_type;
2707 c.seq = nlh->nlmsg_seq;
2708 c.portid = nlh->nlmsg_pid;
2710 km_policy_notify(NULL, 0, &c);
2714 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2715 struct nlattr **attrs,
2716 struct netlink_ext_ack *extack)
2718 struct net *net = sock_net(skb->sk);
2719 struct xfrm_policy *xp;
2720 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2721 struct xfrm_userpolicy_info *p = &up->pol;
2722 u8 type = XFRM_POLICY_TYPE_MAIN;
2727 err = copy_from_user_policy_type(&type, attrs, extack);
2731 err = verify_policy_dir(p->dir, extack);
2735 if (attrs[XFRMA_IF_ID])
2736 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2738 xfrm_mark_get(attrs, &m);
2741 xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2744 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2745 struct xfrm_sec_ctx *ctx;
2747 err = verify_sec_ctx_len(attrs, extack);
2753 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2755 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2759 xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2760 &p->sel, ctx, 0, &err);
2761 security_xfrm_policy_free(ctx);
2766 if (unlikely(xp->walk.dead))
2771 xfrm_policy_delete(xp, p->dir);
2772 xfrm_audit_policy_delete(xp, 1, true);
2774 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2781 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2782 struct nlattr **attrs,
2783 struct netlink_ext_ack *extack)
2785 struct net *net = sock_net(skb->sk);
2786 struct xfrm_state *x;
2788 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2789 struct xfrm_usersa_info *p = &ue->state;
2791 u32 mark = xfrm_mark_get(attrs, &m);
2793 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2799 spin_lock_bh(&x->lock);
2801 if (x->km.state != XFRM_STATE_VALID) {
2802 NL_SET_ERR_MSG(extack, "SA must be in VALID state");
2806 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2809 __xfrm_state_delete(x);
2810 xfrm_audit_state_delete(x, 1, true);
2814 spin_unlock_bh(&x->lock);
2819 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2820 struct nlattr **attrs,
2821 struct netlink_ext_ack *extack)
2823 struct net *net = sock_net(skb->sk);
2824 struct xfrm_policy *xp;
2825 struct xfrm_user_tmpl *ut;
2827 struct nlattr *rt = attrs[XFRMA_TMPL];
2828 struct xfrm_mark mark;
2830 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2831 struct xfrm_state *x = xfrm_state_alloc(net);
2837 xfrm_mark_get(attrs, &mark);
2839 err = verify_newpolicy_info(&ua->policy, extack);
2842 err = verify_sec_ctx_len(attrs, extack);
2847 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
2851 memcpy(&x->id, &ua->id, sizeof(ua->id));
2852 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2853 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2854 xp->mark.m = x->mark.m = mark.m;
2855 xp->mark.v = x->mark.v = mark.v;
2857 /* extract the templates and for each call km_key */
2858 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2859 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2860 memcpy(&x->id, &t->id, sizeof(x->id));
2861 x->props.mode = t->mode;
2862 x->props.reqid = t->reqid;
2863 x->props.family = ut->family;
2864 t->aalgos = ua->aalgos;
2865 t->ealgos = ua->ealgos;
2866 t->calgos = ua->calgos;
2867 err = km_query(x, t, xp);
2882 #ifdef CONFIG_XFRM_MIGRATE
2883 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2884 struct xfrm_kmaddress *k,
2885 struct nlattr **attrs, int *num,
2886 struct netlink_ext_ack *extack)
2888 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2889 struct xfrm_user_migrate *um;
2893 struct xfrm_user_kmaddress *uk;
2895 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2896 memcpy(&k->local, &uk->local, sizeof(k->local));
2897 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2898 k->family = uk->family;
2899 k->reserved = uk->reserved;
2903 num_migrate = nla_len(rt) / sizeof(*um);
2905 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) {
2906 NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)");
2910 for (i = 0; i < num_migrate; i++, um++, ma++) {
2911 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2912 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2913 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2914 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2916 ma->proto = um->proto;
2917 ma->mode = um->mode;
2918 ma->reqid = um->reqid;
2920 ma->old_family = um->old_family;
2921 ma->new_family = um->new_family;
2928 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2929 struct nlattr **attrs, struct netlink_ext_ack *extack)
2931 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2932 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2933 struct xfrm_kmaddress km, *kmp;
2937 struct net *net = sock_net(skb->sk);
2938 struct xfrm_encap_tmpl *encap = NULL;
2941 if (!attrs[XFRMA_MIGRATE]) {
2942 NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute");
2946 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2948 err = copy_from_user_policy_type(&type, attrs, extack);
2952 err = copy_from_user_migrate(m, kmp, attrs, &n, extack);
2959 if (attrs[XFRMA_ENCAP]) {
2960 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2961 sizeof(*encap), GFP_KERNEL);
2966 if (attrs[XFRMA_IF_ID])
2967 if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2969 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap,
2977 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2978 struct nlattr **attrs, struct netlink_ext_ack *extack)
2980 return -ENOPROTOOPT;
2984 #ifdef CONFIG_XFRM_MIGRATE
2985 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2987 struct xfrm_user_migrate um;
2989 memset(&um, 0, sizeof(um));
2990 um.proto = m->proto;
2992 um.reqid = m->reqid;
2993 um.old_family = m->old_family;
2994 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2995 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2996 um.new_family = m->new_family;
2997 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2998 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
3000 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
3003 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
3005 struct xfrm_user_kmaddress uk;
3007 memset(&uk, 0, sizeof(uk));
3008 uk.family = k->family;
3009 uk.reserved = k->reserved;
3010 memcpy(&uk.local, &k->local, sizeof(uk.local));
3011 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
3013 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
3016 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
3019 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
3020 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
3021 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
3022 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
3023 + userpolicy_type_attrsize();
3026 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
3027 int num_migrate, const struct xfrm_kmaddress *k,
3028 const struct xfrm_selector *sel,
3029 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
3031 const struct xfrm_migrate *mp;
3032 struct xfrm_userpolicy_id *pol_id;
3033 struct nlmsghdr *nlh;
3036 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
3040 pol_id = nlmsg_data(nlh);
3041 /* copy data from selector, dir, and type to the pol_id */
3042 memset(pol_id, 0, sizeof(*pol_id));
3043 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
3047 err = copy_to_user_kmaddress(k, skb);
3052 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
3056 err = copy_to_user_policy_type(type, skb);
3059 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
3060 err = copy_to_user_migrate(mp, skb);
3065 nlmsg_end(skb, nlh);
3069 nlmsg_cancel(skb, nlh);
3073 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3074 const struct xfrm_migrate *m, int num_migrate,
3075 const struct xfrm_kmaddress *k,
3076 const struct xfrm_encap_tmpl *encap)
3078 struct net *net = &init_net;
3079 struct sk_buff *skb;
3082 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
3088 err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
3091 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
3094 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3095 const struct xfrm_migrate *m, int num_migrate,
3096 const struct xfrm_kmaddress *k,
3097 const struct xfrm_encap_tmpl *encap)
3099 return -ENOPROTOOPT;
3103 #define XMSGSIZE(type) sizeof(struct type)
3105 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
3106 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
3107 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3108 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
3109 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
3110 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3111 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3112 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
3113 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
3114 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
3115 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
3116 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
3117 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
3118 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
3119 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
3120 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
3121 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
3122 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
3123 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
3124 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
3125 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
3126 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
3127 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
3128 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
3130 EXPORT_SYMBOL_GPL(xfrm_msg_min);
3134 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
3135 [XFRMA_UNSPEC] = { .strict_start_type = XFRMA_SA_DIR },
3136 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
3137 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
3138 [XFRMA_LASTUSED] = { .type = NLA_U64},
3139 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
3140 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
3141 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
3142 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
3143 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
3144 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
3145 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
3146 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
3147 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
3148 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
3149 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
3150 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
3151 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
3152 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
3153 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
3154 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
3155 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
3156 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
3157 [XFRMA_TFCPAD] = { .type = NLA_U32 },
3158 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
3159 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
3160 [XFRMA_PROTO] = { .type = NLA_U8 },
3161 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
3162 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
3163 [XFRMA_SET_MARK] = { .type = NLA_U32 },
3164 [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
3165 [XFRMA_IF_ID] = { .type = NLA_U32 },
3166 [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
3167 [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT),
3169 EXPORT_SYMBOL_GPL(xfrma_policy);
3171 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
3172 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3173 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
3176 static const struct xfrm_link {
3177 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
3178 struct netlink_ext_ack *);
3179 int (*start)(struct netlink_callback *);
3180 int (*dump)(struct sk_buff *, struct netlink_callback *);
3181 int (*done)(struct netlink_callback *);
3182 const struct nla_policy *nla_pol;
3184 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
3185 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
3186 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
3187 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
3188 .dump = xfrm_dump_sa,
3189 .done = xfrm_dump_sa_done },
3190 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3191 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
3192 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
3193 .start = xfrm_dump_policy_start,
3194 .dump = xfrm_dump_policy,
3195 .done = xfrm_dump_policy_done },
3196 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
3197 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
3198 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
3199 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
3200 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
3201 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
3202 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
3203 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
3204 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
3205 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
3206 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
3207 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
3208 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
3209 .nla_pol = xfrma_spd_policy,
3210 .nla_max = XFRMA_SPD_MAX },
3211 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
3212 [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
3213 [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
3216 static int xfrm_reject_unused_attr(int type, struct nlattr **attrs,
3217 struct netlink_ext_ack *extack)
3219 if (attrs[XFRMA_SA_DIR]) {
3221 case XFRM_MSG_NEWSA:
3222 case XFRM_MSG_UPDSA:
3223 case XFRM_MSG_ALLOCSPI:
3226 NL_SET_ERR_MSG(extack, "Invalid attribute SA_DIR");
3234 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
3235 struct netlink_ext_ack *extack)
3237 struct net *net = sock_net(skb->sk);
3238 struct nlattr *attrs[XFRMA_MAX+1];
3239 const struct xfrm_link *link;
3240 struct nlmsghdr *nlh64 = NULL;
3243 type = nlh->nlmsg_type;
3244 if (type > XFRM_MSG_MAX)
3247 type -= XFRM_MSG_BASE;
3248 link = &xfrm_dispatch[type];
3250 /* All operations require privileges, even GET */
3251 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
3254 if (in_compat_syscall()) {
3255 struct xfrm_translator *xtr = xfrm_get_translator();
3260 nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
3261 link->nla_pol, extack);
3262 xfrm_put_translator(xtr);
3264 return PTR_ERR(nlh64);
3269 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
3270 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
3271 (nlh->nlmsg_flags & NLM_F_DUMP)) {
3272 struct netlink_dump_control c = {
3273 .start = link->start,
3278 if (link->dump == NULL) {
3283 err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
3287 err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
3288 link->nla_max ? : XFRMA_MAX,
3289 link->nla_pol ? : xfrma_policy, extack);
3293 if (!link->nla_pol || link->nla_pol == xfrma_policy) {
3294 err = xfrm_reject_unused_attr((type + XFRM_MSG_BASE), attrs, extack);
3299 if (link->doit == NULL) {
3304 err = link->doit(skb, nlh, attrs, extack);
3306 /* We need to free skb allocated in xfrm_alloc_compat() before
3307 * returning from this function, because consume_skb() won't take
3308 * care of frag_list since netlink destructor sets
3309 * sbk->head to NULL. (see netlink_skb_destructor())
3311 if (skb_has_frag_list(skb)) {
3312 kfree_skb(skb_shinfo(skb)->frag_list);
3313 skb_shinfo(skb)->frag_list = NULL;
3321 static void xfrm_netlink_rcv(struct sk_buff *skb)
3323 struct net *net = sock_net(skb->sk);
3325 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3326 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
3327 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3330 static inline unsigned int xfrm_expire_msgsize(void)
3332 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) +
3333 nla_total_size(sizeof(struct xfrm_mark)) +
3334 nla_total_size(sizeof_field(struct xfrm_state, dir));
3337 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
3339 struct xfrm_user_expire *ue;
3340 struct nlmsghdr *nlh;
3343 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
3347 ue = nlmsg_data(nlh);
3348 copy_to_user_state(x, &ue->state);
3349 ue->hard = (c->data.hard != 0) ? 1 : 0;
3350 /* clear the padding bytes */
3351 memset_after(ue, 0, hard);
3353 err = xfrm_mark_put(skb, &x->mark);
3357 err = xfrm_if_id_put(skb, x->if_id);
3362 err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
3367 nlmsg_end(skb, nlh);
3371 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
3373 struct net *net = xs_net(x);
3374 struct sk_buff *skb;
3376 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
3380 if (build_expire(skb, x, c) < 0) {
3385 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3388 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
3390 struct net *net = xs_net(x);
3391 struct sk_buff *skb;
3394 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
3398 err = build_aevent(skb, x, c);
3401 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
3404 static int xfrm_notify_sa_flush(const struct km_event *c)
3406 struct net *net = c->net;
3407 struct xfrm_usersa_flush *p;
3408 struct nlmsghdr *nlh;
3409 struct sk_buff *skb;
3410 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
3412 skb = nlmsg_new(len, GFP_ATOMIC);
3416 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
3422 p = nlmsg_data(nlh);
3423 p->proto = c->data.proto;
3425 nlmsg_end(skb, nlh);
3427 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3430 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
3434 l += nla_total_size(aead_len(x->aead));
3436 l += nla_total_size(sizeof(struct xfrm_algo) +
3437 (x->aalg->alg_key_len + 7) / 8);
3438 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
3441 l += nla_total_size(xfrm_alg_len(x->ealg));
3443 l += nla_total_size(sizeof(*x->calg));
3445 l += nla_total_size(sizeof(*x->encap));
3447 l += nla_total_size(sizeof(x->tfcpad));
3449 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
3451 l += nla_total_size(sizeof(struct xfrm_replay_state));
3453 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
3454 x->security->ctx_len);
3456 l += nla_total_size(sizeof(*x->coaddr));
3457 if (x->props.extra_flags)
3458 l += nla_total_size(sizeof(x->props.extra_flags));
3460 l += nla_total_size(sizeof(struct xfrm_user_offload));
3461 if (x->props.smark.v | x->props.smark.m) {
3462 l += nla_total_size(sizeof(x->props.smark.v));
3463 l += nla_total_size(sizeof(x->props.smark.m));
3466 l += nla_total_size(sizeof(x->if_id));
3468 /* Must count x->lastused as it may become non-zero behind our back. */
3469 l += nla_total_size_64bit(sizeof(u64));
3471 if (x->mapping_maxage)
3472 l += nla_total_size(sizeof(x->mapping_maxage));
3475 l += nla_total_size(sizeof(x->dir));
3480 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
3482 struct net *net = xs_net(x);
3483 struct xfrm_usersa_info *p;
3484 struct xfrm_usersa_id *id;
3485 struct nlmsghdr *nlh;
3486 struct sk_buff *skb;
3487 unsigned int len = xfrm_sa_len(x);
3488 unsigned int headlen;
3491 headlen = sizeof(*p);
3492 if (c->event == XFRM_MSG_DELSA) {
3493 len += nla_total_size(headlen);
3494 headlen = sizeof(*id);
3495 len += nla_total_size(sizeof(struct xfrm_mark));
3497 len += NLMSG_ALIGN(headlen);
3499 skb = nlmsg_new(len, GFP_ATOMIC);
3503 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3508 p = nlmsg_data(nlh);
3509 if (c->event == XFRM_MSG_DELSA) {
3510 struct nlattr *attr;
3512 id = nlmsg_data(nlh);
3513 memset(id, 0, sizeof(*id));
3514 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
3515 id->spi = x->id.spi;
3516 id->family = x->props.family;
3517 id->proto = x->id.proto;
3519 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
3526 err = copy_to_user_state_extra(x, p, skb);
3530 nlmsg_end(skb, nlh);
3532 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
3539 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
3543 case XFRM_MSG_EXPIRE:
3544 return xfrm_exp_state_notify(x, c);
3545 case XFRM_MSG_NEWAE:
3546 return xfrm_aevent_state_notify(x, c);
3547 case XFRM_MSG_DELSA:
3548 case XFRM_MSG_UPDSA:
3549 case XFRM_MSG_NEWSA:
3550 return xfrm_notify_sa(x, c);
3551 case XFRM_MSG_FLUSHSA:
3552 return xfrm_notify_sa_flush(c);
3554 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
3563 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
3564 struct xfrm_policy *xp)
3566 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
3567 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3568 + nla_total_size(sizeof(struct xfrm_mark))
3569 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
3570 + userpolicy_type_attrsize();
3573 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
3574 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3576 __u32 seq = xfrm_get_acqseq();
3577 struct xfrm_user_acquire *ua;
3578 struct nlmsghdr *nlh;
3581 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3585 ua = nlmsg_data(nlh);
3586 memcpy(&ua->id, &x->id, sizeof(ua->id));
3587 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3588 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3589 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3590 ua->aalgos = xt->aalgos;
3591 ua->ealgos = xt->ealgos;
3592 ua->calgos = xt->calgos;
3593 ua->seq = x->km.seq = seq;
3595 err = copy_to_user_tmpl(xp, skb);
3597 err = copy_to_user_state_sec_ctx(x, skb);
3599 err = copy_to_user_policy_type(xp->type, skb);
3601 err = xfrm_mark_put(skb, &xp->mark);
3603 err = xfrm_if_id_put(skb, xp->if_id);
3604 if (!err && xp->xdo.dev)
3605 err = copy_user_offload(&xp->xdo, skb);
3607 nlmsg_cancel(skb, nlh);
3611 nlmsg_end(skb, nlh);
3615 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3616 struct xfrm_policy *xp)
3618 struct net *net = xs_net(x);
3619 struct sk_buff *skb;
3622 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3626 err = build_acquire(skb, x, xt, xp);
3629 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3632 /* User gives us xfrm_user_policy_info followed by an array of 0
3633 * or more templates.
3635 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3636 u8 *data, int len, int *dir)
3638 struct net *net = sock_net(sk);
3639 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3640 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3641 struct xfrm_policy *xp;
3644 switch (sk->sk_family) {
3646 if (opt != IP_XFRM_POLICY) {
3651 #if IS_ENABLED(CONFIG_IPV6)
3653 if (opt != IPV6_XFRM_POLICY) {
3666 if (len < sizeof(*p) ||
3667 verify_newpolicy_info(p, NULL))
3670 nr = ((len - sizeof(*p)) / sizeof(*ut));
3671 if (validate_tmpl(nr, ut, p->sel.family, p->dir, NULL))
3674 if (p->dir > XFRM_POLICY_OUT)
3677 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3683 copy_from_user_policy(xp, p);
3684 xp->type = XFRM_POLICY_TYPE_MAIN;
3685 copy_templates(xp, ut, nr);
3692 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3694 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3695 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3696 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3697 + nla_total_size(sizeof(struct xfrm_mark))
3698 + userpolicy_type_attrsize();
3701 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3702 int dir, const struct km_event *c)
3704 struct xfrm_user_polexpire *upe;
3705 int hard = c->data.hard;
3706 struct nlmsghdr *nlh;
3709 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3713 upe = nlmsg_data(nlh);
3714 copy_to_user_policy(xp, &upe->pol, dir);
3715 err = copy_to_user_tmpl(xp, skb);
3717 err = copy_to_user_sec_ctx(xp, skb);
3719 err = copy_to_user_policy_type(xp->type, skb);
3721 err = xfrm_mark_put(skb, &xp->mark);
3723 err = xfrm_if_id_put(skb, xp->if_id);
3724 if (!err && xp->xdo.dev)
3725 err = copy_user_offload(&xp->xdo, skb);
3727 nlmsg_cancel(skb, nlh);
3732 nlmsg_end(skb, nlh);
3736 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3738 struct net *net = xp_net(xp);
3739 struct sk_buff *skb;
3742 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3746 err = build_polexpire(skb, xp, dir, c);
3749 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3752 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3754 unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3755 struct net *net = xp_net(xp);
3756 struct xfrm_userpolicy_info *p;
3757 struct xfrm_userpolicy_id *id;
3758 struct nlmsghdr *nlh;
3759 struct sk_buff *skb;
3760 unsigned int headlen;
3763 headlen = sizeof(*p);
3764 if (c->event == XFRM_MSG_DELPOLICY) {
3765 len += nla_total_size(headlen);
3766 headlen = sizeof(*id);
3768 len += userpolicy_type_attrsize();
3769 len += nla_total_size(sizeof(struct xfrm_mark));
3770 len += NLMSG_ALIGN(headlen);
3772 skb = nlmsg_new(len, GFP_ATOMIC);
3776 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3781 p = nlmsg_data(nlh);
3782 if (c->event == XFRM_MSG_DELPOLICY) {
3783 struct nlattr *attr;
3785 id = nlmsg_data(nlh);
3786 memset(id, 0, sizeof(*id));
3789 id->index = xp->index;
3791 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3793 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3801 copy_to_user_policy(xp, p, dir);
3802 err = copy_to_user_tmpl(xp, skb);
3804 err = copy_to_user_policy_type(xp->type, skb);
3806 err = xfrm_mark_put(skb, &xp->mark);
3808 err = xfrm_if_id_put(skb, xp->if_id);
3809 if (!err && xp->xdo.dev)
3810 err = copy_user_offload(&xp->xdo, skb);
3814 nlmsg_end(skb, nlh);
3816 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3823 static int xfrm_notify_policy_flush(const struct km_event *c)
3825 struct net *net = c->net;
3826 struct nlmsghdr *nlh;
3827 struct sk_buff *skb;
3830 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3834 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3838 err = copy_to_user_policy_type(c->data.type, skb);
3842 nlmsg_end(skb, nlh);
3844 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3851 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3855 case XFRM_MSG_NEWPOLICY:
3856 case XFRM_MSG_UPDPOLICY:
3857 case XFRM_MSG_DELPOLICY:
3858 return xfrm_notify_policy(xp, dir, c);
3859 case XFRM_MSG_FLUSHPOLICY:
3860 return xfrm_notify_policy_flush(c);
3861 case XFRM_MSG_POLEXPIRE:
3862 return xfrm_exp_policy_notify(xp, dir, c);
3864 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3872 static inline unsigned int xfrm_report_msgsize(void)
3874 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3877 static int build_report(struct sk_buff *skb, u8 proto,
3878 struct xfrm_selector *sel, xfrm_address_t *addr)
3880 struct xfrm_user_report *ur;
3881 struct nlmsghdr *nlh;
3883 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3887 ur = nlmsg_data(nlh);
3889 memcpy(&ur->sel, sel, sizeof(ur->sel));
3892 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3894 nlmsg_cancel(skb, nlh);
3898 nlmsg_end(skb, nlh);
3902 static int xfrm_send_report(struct net *net, u8 proto,
3903 struct xfrm_selector *sel, xfrm_address_t *addr)
3905 struct sk_buff *skb;
3908 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3912 err = build_report(skb, proto, sel, addr);
3915 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3918 static inline unsigned int xfrm_mapping_msgsize(void)
3920 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3923 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3924 xfrm_address_t *new_saddr, __be16 new_sport)
3926 struct xfrm_user_mapping *um;
3927 struct nlmsghdr *nlh;
3929 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3933 um = nlmsg_data(nlh);
3935 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3936 um->id.spi = x->id.spi;
3937 um->id.family = x->props.family;
3938 um->id.proto = x->id.proto;
3939 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3940 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3941 um->new_sport = new_sport;
3942 um->old_sport = x->encap->encap_sport;
3943 um->reqid = x->props.reqid;
3945 nlmsg_end(skb, nlh);
3949 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3952 struct net *net = xs_net(x);
3953 struct sk_buff *skb;
3956 if (x->id.proto != IPPROTO_ESP)
3962 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3966 err = build_mapping(skb, x, ipaddr, sport);
3969 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3972 static bool xfrm_is_alive(const struct km_event *c)
3974 return (bool)xfrm_acquire_is_on(c->net);
3977 static struct xfrm_mgr netlink_mgr = {
3978 .notify = xfrm_send_state_notify,
3979 .acquire = xfrm_send_acquire,
3980 .compile_policy = xfrm_compile_policy,
3981 .notify_policy = xfrm_send_policy_notify,
3982 .report = xfrm_send_report,
3983 .migrate = xfrm_send_migrate,
3984 .new_mapping = xfrm_send_mapping,
3985 .is_alive = xfrm_is_alive,
3988 static int __net_init xfrm_user_net_init(struct net *net)
3991 struct netlink_kernel_cfg cfg = {
3992 .groups = XFRMNLGRP_MAX,
3993 .input = xfrm_netlink_rcv,
3996 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3999 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
4000 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
4004 static void __net_exit xfrm_user_net_pre_exit(struct net *net)
4006 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
4009 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
4013 list_for_each_entry(net, net_exit_list, exit_list)
4014 netlink_kernel_release(net->xfrm.nlsk_stash);
4017 static struct pernet_operations xfrm_user_net_ops = {
4018 .init = xfrm_user_net_init,
4019 .pre_exit = xfrm_user_net_pre_exit,
4020 .exit_batch = xfrm_user_net_exit,
4023 static int __init xfrm_user_init(void)
4027 printk(KERN_INFO "Initializing XFRM netlink socket\n");
4029 rv = register_pernet_subsys(&xfrm_user_net_ops);
4032 xfrm_register_km(&netlink_mgr);
4036 static void __exit xfrm_user_exit(void)
4038 xfrm_unregister_km(&netlink_mgr);
4039 unregister_pernet_subsys(&xfrm_user_net_ops);
4042 module_init(xfrm_user_init);
4043 module_exit(xfrm_user_exit);
4044 MODULE_DESCRIPTION("XFRM User interface");
4045 MODULE_LICENSE("GPL");
4046 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);