1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
35 static inline int aead_len(struct xfrm_algo_aead
*alg
)
37 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
40 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
42 struct nlattr
*rt
= attrs
[type
];
43 struct xfrm_algo
*algp
;
49 if (nla_len(rt
) < xfrm_alg_len(algp
))
62 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
66 static int verify_auth_trunc(struct nlattr
**attrs
)
68 struct nlattr
*rt
= attrs
[XFRMA_ALG_AUTH_TRUNC
];
69 struct xfrm_algo_auth
*algp
;
75 if (nla_len(rt
) < xfrm_alg_auth_len(algp
))
78 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
82 static int verify_aead(struct nlattr
**attrs
)
84 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
85 struct xfrm_algo_aead
*algp
;
91 if (nla_len(rt
) < aead_len(algp
))
94 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
98 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
99 xfrm_address_t
**addrp
)
101 struct nlattr
*rt
= attrs
[type
];
104 *addrp
= nla_data(rt
);
107 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
109 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
110 struct xfrm_user_sec_ctx
*uctx
;
116 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
122 static inline int verify_replay(struct xfrm_usersa_info
*p
,
123 struct nlattr
**attrs
)
125 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_ESN_VAL
];
126 struct xfrm_replay_state_esn
*rs
;
128 if (p
->flags
& XFRM_STATE_ESN
) {
134 if (rs
->bmp_len
> XFRMA_REPLAY_ESN_MAX
/ sizeof(rs
->bmp
[0]) / 8)
137 if (nla_len(rt
) < xfrm_replay_state_esn_len(rs
) &&
138 nla_len(rt
) != sizeof(*rs
))
145 if (p
->id
.proto
!= IPPROTO_ESP
)
148 if (p
->replay_window
!= 0)
154 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
155 struct nlattr
**attrs
)
165 #if IS_ENABLED(CONFIG_IPV6)
177 switch (p
->id
.proto
) {
179 if ((!attrs
[XFRMA_ALG_AUTH
] &&
180 !attrs
[XFRMA_ALG_AUTH_TRUNC
]) ||
181 attrs
[XFRMA_ALG_AEAD
] ||
182 attrs
[XFRMA_ALG_CRYPT
] ||
183 attrs
[XFRMA_ALG_COMP
] ||
189 if (attrs
[XFRMA_ALG_COMP
])
191 if (!attrs
[XFRMA_ALG_AUTH
] &&
192 !attrs
[XFRMA_ALG_AUTH_TRUNC
] &&
193 !attrs
[XFRMA_ALG_CRYPT
] &&
194 !attrs
[XFRMA_ALG_AEAD
])
196 if ((attrs
[XFRMA_ALG_AUTH
] ||
197 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
198 attrs
[XFRMA_ALG_CRYPT
]) &&
199 attrs
[XFRMA_ALG_AEAD
])
201 if (attrs
[XFRMA_TFCPAD
] &&
202 p
->mode
!= XFRM_MODE_TUNNEL
)
207 if (!attrs
[XFRMA_ALG_COMP
] ||
208 attrs
[XFRMA_ALG_AEAD
] ||
209 attrs
[XFRMA_ALG_AUTH
] ||
210 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
211 attrs
[XFRMA_ALG_CRYPT
] ||
216 #if IS_ENABLED(CONFIG_IPV6)
217 case IPPROTO_DSTOPTS
:
218 case IPPROTO_ROUTING
:
219 if (attrs
[XFRMA_ALG_COMP
] ||
220 attrs
[XFRMA_ALG_AUTH
] ||
221 attrs
[XFRMA_ALG_AUTH_TRUNC
] ||
222 attrs
[XFRMA_ALG_AEAD
] ||
223 attrs
[XFRMA_ALG_CRYPT
] ||
224 attrs
[XFRMA_ENCAP
] ||
225 attrs
[XFRMA_SEC_CTX
] ||
226 attrs
[XFRMA_TFCPAD
] ||
227 !attrs
[XFRMA_COADDR
])
236 if ((err
= verify_aead(attrs
)))
238 if ((err
= verify_auth_trunc(attrs
)))
240 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
242 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
244 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
246 if ((err
= verify_sec_ctx_len(attrs
)))
248 if ((err
= verify_replay(p
, attrs
)))
253 case XFRM_MODE_TRANSPORT
:
254 case XFRM_MODE_TUNNEL
:
255 case XFRM_MODE_ROUTEOPTIMIZATION
:
269 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
270 struct xfrm_algo_desc
*(*get_byname
)(const char *, int),
273 struct xfrm_algo
*p
, *ualg
;
274 struct xfrm_algo_desc
*algo
;
279 ualg
= nla_data(rta
);
281 algo
= get_byname(ualg
->alg_name
, 1);
284 *props
= algo
->desc
.sadb_alg_id
;
286 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
290 strcpy(p
->alg_name
, algo
->name
);
295 static int attach_auth(struct xfrm_algo_auth
**algpp
, u8
*props
,
298 struct xfrm_algo
*ualg
;
299 struct xfrm_algo_auth
*p
;
300 struct xfrm_algo_desc
*algo
;
305 ualg
= nla_data(rta
);
307 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
310 *props
= algo
->desc
.sadb_alg_id
;
312 p
= kmalloc(sizeof(*p
) + (ualg
->alg_key_len
+ 7) / 8, GFP_KERNEL
);
316 strcpy(p
->alg_name
, algo
->name
);
317 p
->alg_key_len
= ualg
->alg_key_len
;
318 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
319 memcpy(p
->alg_key
, ualg
->alg_key
, (ualg
->alg_key_len
+ 7) / 8);
325 static int attach_auth_trunc(struct xfrm_algo_auth
**algpp
, u8
*props
,
328 struct xfrm_algo_auth
*p
, *ualg
;
329 struct xfrm_algo_desc
*algo
;
334 ualg
= nla_data(rta
);
336 algo
= xfrm_aalg_get_byname(ualg
->alg_name
, 1);
339 if ((ualg
->alg_trunc_len
/ 8) > MAX_AH_AUTH_LEN
||
340 ualg
->alg_trunc_len
> algo
->uinfo
.auth
.icv_fullbits
)
342 *props
= algo
->desc
.sadb_alg_id
;
344 p
= kmemdup(ualg
, xfrm_alg_auth_len(ualg
), GFP_KERNEL
);
348 strcpy(p
->alg_name
, algo
->name
);
349 if (!p
->alg_trunc_len
)
350 p
->alg_trunc_len
= algo
->uinfo
.auth
.icv_truncbits
;
356 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
359 struct xfrm_algo_aead
*p
, *ualg
;
360 struct xfrm_algo_desc
*algo
;
365 ualg
= nla_data(rta
);
367 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
370 *props
= algo
->desc
.sadb_alg_id
;
372 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
376 strcpy(p
->alg_name
, algo
->name
);
381 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn
*replay_esn
,
384 struct xfrm_replay_state_esn
*up
;
387 if (!replay_esn
|| !rp
)
391 ulen
= xfrm_replay_state_esn_len(up
);
393 /* Check the overall length and the internal bitmap length to avoid
394 * potential overflow. */
395 if (nla_len(rp
) < ulen
||
396 xfrm_replay_state_esn_len(replay_esn
) != ulen
||
397 replay_esn
->bmp_len
!= up
->bmp_len
)
400 if (up
->replay_window
> up
->bmp_len
* sizeof(__u32
) * 8)
406 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn
**replay_esn
,
407 struct xfrm_replay_state_esn
**preplay_esn
,
410 struct xfrm_replay_state_esn
*p
, *pp
, *up
;
417 klen
= xfrm_replay_state_esn_len(up
);
418 ulen
= nla_len(rta
) >= klen
? klen
: sizeof(*up
);
420 p
= kzalloc(klen
, GFP_KERNEL
);
424 pp
= kzalloc(klen
, GFP_KERNEL
);
431 memcpy(pp
, up
, ulen
);
439 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
444 len
+= sizeof(struct xfrm_user_sec_ctx
);
445 len
+= xfrm_ctx
->ctx_len
;
450 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
452 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
453 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
454 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
455 x
->props
.mode
= p
->mode
;
456 x
->props
.replay_window
= p
->replay_window
;
457 x
->props
.reqid
= p
->reqid
;
458 x
->props
.family
= p
->family
;
459 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
460 x
->props
.flags
= p
->flags
;
462 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
463 x
->sel
.family
= p
->family
;
467 * someday when pfkey also has support, we could have the code
468 * somehow made shareable and move it to xfrm_state.c - JHS
471 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
,
474 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
475 struct nlattr
*re
= update_esn
? attrs
[XFRMA_REPLAY_ESN_VAL
] : NULL
;
476 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
477 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
478 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
481 struct xfrm_replay_state_esn
*replay_esn
;
482 replay_esn
= nla_data(re
);
483 memcpy(x
->replay_esn
, replay_esn
,
484 xfrm_replay_state_esn_len(replay_esn
));
485 memcpy(x
->preplay_esn
, replay_esn
,
486 xfrm_replay_state_esn_len(replay_esn
));
490 struct xfrm_replay_state
*replay
;
491 replay
= nla_data(rp
);
492 memcpy(&x
->replay
, replay
, sizeof(*replay
));
493 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
497 struct xfrm_lifetime_cur
*ltime
;
498 ltime
= nla_data(lt
);
499 x
->curlft
.bytes
= ltime
->bytes
;
500 x
->curlft
.packets
= ltime
->packets
;
501 x
->curlft
.add_time
= ltime
->add_time
;
502 x
->curlft
.use_time
= ltime
->use_time
;
506 x
->replay_maxage
= nla_get_u32(et
);
509 x
->replay_maxdiff
= nla_get_u32(rt
);
512 static struct xfrm_state
*xfrm_state_construct(struct net
*net
,
513 struct xfrm_usersa_info
*p
,
514 struct nlattr
**attrs
,
517 struct xfrm_state
*x
= xfrm_state_alloc(net
);
523 copy_from_user_state(x
, p
);
525 if (attrs
[XFRMA_SA_EXTRA_FLAGS
])
526 x
->props
.extra_flags
= nla_get_u32(attrs
[XFRMA_SA_EXTRA_FLAGS
]);
528 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
529 attrs
[XFRMA_ALG_AEAD
])))
531 if ((err
= attach_auth_trunc(&x
->aalg
, &x
->props
.aalgo
,
532 attrs
[XFRMA_ALG_AUTH_TRUNC
])))
534 if (!x
->props
.aalgo
) {
535 if ((err
= attach_auth(&x
->aalg
, &x
->props
.aalgo
,
536 attrs
[XFRMA_ALG_AUTH
])))
539 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
540 xfrm_ealg_get_byname
,
541 attrs
[XFRMA_ALG_CRYPT
])))
543 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
544 xfrm_calg_get_byname
,
545 attrs
[XFRMA_ALG_COMP
])))
548 if (attrs
[XFRMA_ENCAP
]) {
549 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
550 sizeof(*x
->encap
), GFP_KERNEL
);
551 if (x
->encap
== NULL
)
555 if (attrs
[XFRMA_TFCPAD
])
556 x
->tfcpad
= nla_get_u32(attrs
[XFRMA_TFCPAD
]);
558 if (attrs
[XFRMA_COADDR
]) {
559 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
560 sizeof(*x
->coaddr
), GFP_KERNEL
);
561 if (x
->coaddr
== NULL
)
565 xfrm_mark_get(attrs
, &x
->mark
);
567 err
= __xfrm_init_state(x
, false);
571 if (attrs
[XFRMA_SEC_CTX
] &&
572 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
575 if ((err
= xfrm_alloc_replay_state_esn(&x
->replay_esn
, &x
->preplay_esn
,
576 attrs
[XFRMA_REPLAY_ESN_VAL
])))
580 x
->replay_maxdiff
= net
->xfrm
.sysctl_aevent_rseqth
;
581 /* sysctl_xfrm_aevent_etime is in 100ms units */
582 x
->replay_maxage
= (net
->xfrm
.sysctl_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
584 if ((err
= xfrm_init_replay(x
)))
587 /* override default values from above */
588 xfrm_update_ae_params(x
, attrs
, 0);
593 x
->km
.state
= XFRM_STATE_DEAD
;
600 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
601 struct nlattr
**attrs
)
603 struct net
*net
= sock_net(skb
->sk
);
604 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
605 struct xfrm_state
*x
;
608 kuid_t loginuid
= audit_get_loginuid(current
);
609 u32 sessionid
= audit_get_sessionid(current
);
612 err
= verify_newsa_info(p
, attrs
);
616 x
= xfrm_state_construct(net
, p
, attrs
, &err
);
621 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
622 err
= xfrm_state_add(x
);
624 err
= xfrm_state_update(x
);
626 security_task_getsecid(current
, &sid
);
627 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
630 x
->km
.state
= XFRM_STATE_DEAD
;
635 c
.seq
= nlh
->nlmsg_seq
;
636 c
.portid
= nlh
->nlmsg_pid
;
637 c
.event
= nlh
->nlmsg_type
;
639 km_state_notify(x
, &c
);
645 static struct xfrm_state
*xfrm_user_state_lookup(struct net
*net
,
646 struct xfrm_usersa_id
*p
,
647 struct nlattr
**attrs
,
650 struct xfrm_state
*x
= NULL
;
653 u32 mark
= xfrm_mark_get(attrs
, &m
);
655 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
657 x
= xfrm_state_lookup(net
, mark
, &p
->daddr
, p
->spi
, p
->proto
, p
->family
);
659 xfrm_address_t
*saddr
= NULL
;
661 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
668 x
= xfrm_state_lookup_byaddr(net
, mark
,
670 p
->proto
, p
->family
);
679 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
680 struct nlattr
**attrs
)
682 struct net
*net
= sock_net(skb
->sk
);
683 struct xfrm_state
*x
;
686 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
687 kuid_t loginuid
= audit_get_loginuid(current
);
688 u32 sessionid
= audit_get_sessionid(current
);
691 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
695 if ((err
= security_xfrm_state_delete(x
)) != 0)
698 if (xfrm_state_kern(x
)) {
703 err
= xfrm_state_delete(x
);
708 c
.seq
= nlh
->nlmsg_seq
;
709 c
.portid
= nlh
->nlmsg_pid
;
710 c
.event
= nlh
->nlmsg_type
;
711 km_state_notify(x
, &c
);
714 security_task_getsecid(current
, &sid
);
715 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
720 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
722 memset(p
, 0, sizeof(*p
));
723 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
724 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
725 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
726 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
727 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
728 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
729 p
->mode
= x
->props
.mode
;
730 p
->replay_window
= x
->props
.replay_window
;
731 p
->reqid
= x
->props
.reqid
;
732 p
->family
= x
->props
.family
;
733 p
->flags
= x
->props
.flags
;
737 struct xfrm_dump_info
{
738 struct sk_buff
*in_skb
;
739 struct sk_buff
*out_skb
;
744 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
746 struct xfrm_user_sec_ctx
*uctx
;
748 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
750 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
754 uctx
= nla_data(attr
);
755 uctx
->exttype
= XFRMA_SEC_CTX
;
756 uctx
->len
= ctx_size
;
757 uctx
->ctx_doi
= s
->ctx_doi
;
758 uctx
->ctx_alg
= s
->ctx_alg
;
759 uctx
->ctx_len
= s
->ctx_len
;
760 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
765 static int copy_to_user_auth(struct xfrm_algo_auth
*auth
, struct sk_buff
*skb
)
767 struct xfrm_algo
*algo
;
770 nla
= nla_reserve(skb
, XFRMA_ALG_AUTH
,
771 sizeof(*algo
) + (auth
->alg_key_len
+ 7) / 8);
775 algo
= nla_data(nla
);
776 strncpy(algo
->alg_name
, auth
->alg_name
, sizeof(algo
->alg_name
));
777 memcpy(algo
->alg_key
, auth
->alg_key
, (auth
->alg_key_len
+ 7) / 8);
778 algo
->alg_key_len
= auth
->alg_key_len
;
783 /* Don't change this without updating xfrm_sa_len! */
784 static int copy_to_user_state_extra(struct xfrm_state
*x
,
785 struct xfrm_usersa_info
*p
,
790 copy_to_user_state(x
, p
);
792 if (x
->props
.extra_flags
) {
793 ret
= nla_put_u32(skb
, XFRMA_SA_EXTRA_FLAGS
,
794 x
->props
.extra_flags
);
800 ret
= nla_put(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
805 ret
= nla_put_u64(skb
, XFRMA_LASTUSED
, x
->lastused
);
810 ret
= nla_put(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
815 ret
= copy_to_user_auth(x
->aalg
, skb
);
817 ret
= nla_put(skb
, XFRMA_ALG_AUTH_TRUNC
,
818 xfrm_alg_auth_len(x
->aalg
), x
->aalg
);
823 ret
= nla_put(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
828 ret
= nla_put(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
833 ret
= nla_put(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
838 ret
= nla_put_u32(skb
, XFRMA_TFCPAD
, x
->tfcpad
);
842 ret
= xfrm_mark_put(skb
, &x
->mark
);
846 ret
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
847 xfrm_replay_state_esn_len(x
->replay_esn
),
853 ret
= copy_sec_ctx(x
->security
, skb
);
858 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
860 struct xfrm_dump_info
*sp
= ptr
;
861 struct sk_buff
*in_skb
= sp
->in_skb
;
862 struct sk_buff
*skb
= sp
->out_skb
;
863 struct xfrm_usersa_info
*p
;
864 struct nlmsghdr
*nlh
;
867 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
868 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
874 err
= copy_to_user_state_extra(x
, p
, skb
);
876 nlmsg_cancel(skb
, nlh
);
883 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
885 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
886 xfrm_state_walk_done(walk
);
890 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
892 struct net
*net
= sock_net(skb
->sk
);
893 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
894 struct xfrm_dump_info info
;
896 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
897 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
899 info
.in_skb
= cb
->skb
;
901 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
902 info
.nlmsg_flags
= NLM_F_MULTI
;
906 xfrm_state_walk_init(walk
, 0);
909 (void) xfrm_state_walk(net
, walk
, dump_one_state
, &info
);
914 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
915 struct xfrm_state
*x
, u32 seq
)
917 struct xfrm_dump_info info
;
921 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
923 return ERR_PTR(-ENOMEM
);
925 info
.in_skb
= in_skb
;
927 info
.nlmsg_seq
= seq
;
928 info
.nlmsg_flags
= 0;
930 err
= dump_one_state(x
, 0, &info
);
939 static inline size_t xfrm_spdinfo_msgsize(void)
941 return NLMSG_ALIGN(4)
942 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
943 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
946 static int build_spdinfo(struct sk_buff
*skb
, struct net
*net
,
947 u32 portid
, u32 seq
, u32 flags
)
949 struct xfrmk_spdinfo si
;
950 struct xfrmu_spdinfo spc
;
951 struct xfrmu_spdhinfo sph
;
952 struct nlmsghdr
*nlh
;
956 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
957 if (nlh
== NULL
) /* shouldn't really happen ... */
962 xfrm_spd_getinfo(net
, &si
);
963 spc
.incnt
= si
.incnt
;
964 spc
.outcnt
= si
.outcnt
;
965 spc
.fwdcnt
= si
.fwdcnt
;
966 spc
.inscnt
= si
.inscnt
;
967 spc
.outscnt
= si
.outscnt
;
968 spc
.fwdscnt
= si
.fwdscnt
;
969 sph
.spdhcnt
= si
.spdhcnt
;
970 sph
.spdhmcnt
= si
.spdhmcnt
;
972 err
= nla_put(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
974 err
= nla_put(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
976 nlmsg_cancel(skb
, nlh
);
980 return nlmsg_end(skb
, nlh
);
983 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
984 struct nlattr
**attrs
)
986 struct net
*net
= sock_net(skb
->sk
);
987 struct sk_buff
*r_skb
;
988 u32
*flags
= nlmsg_data(nlh
);
989 u32 sportid
= NETLINK_CB(skb
).portid
;
990 u32 seq
= nlh
->nlmsg_seq
;
992 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
996 if (build_spdinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
999 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1002 static inline size_t xfrm_sadinfo_msgsize(void)
1004 return NLMSG_ALIGN(4)
1005 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
1006 + nla_total_size(4); /* XFRMA_SAD_CNT */
1009 static int build_sadinfo(struct sk_buff
*skb
, struct net
*net
,
1010 u32 portid
, u32 seq
, u32 flags
)
1012 struct xfrmk_sadinfo si
;
1013 struct xfrmu_sadhinfo sh
;
1014 struct nlmsghdr
*nlh
;
1018 nlh
= nlmsg_put(skb
, portid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
1019 if (nlh
== NULL
) /* shouldn't really happen ... */
1022 f
= nlmsg_data(nlh
);
1024 xfrm_sad_getinfo(net
, &si
);
1026 sh
.sadhmcnt
= si
.sadhmcnt
;
1027 sh
.sadhcnt
= si
.sadhcnt
;
1029 err
= nla_put_u32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
1031 err
= nla_put(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
1033 nlmsg_cancel(skb
, nlh
);
1037 return nlmsg_end(skb
, nlh
);
1040 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1041 struct nlattr
**attrs
)
1043 struct net
*net
= sock_net(skb
->sk
);
1044 struct sk_buff
*r_skb
;
1045 u32
*flags
= nlmsg_data(nlh
);
1046 u32 sportid
= NETLINK_CB(skb
).portid
;
1047 u32 seq
= nlh
->nlmsg_seq
;
1049 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
1053 if (build_sadinfo(r_skb
, net
, sportid
, seq
, *flags
) < 0)
1056 return nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, sportid
);
1059 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1060 struct nlattr
**attrs
)
1062 struct net
*net
= sock_net(skb
->sk
);
1063 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
1064 struct xfrm_state
*x
;
1065 struct sk_buff
*resp_skb
;
1068 x
= xfrm_user_state_lookup(net
, p
, attrs
, &err
);
1072 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1073 if (IS_ERR(resp_skb
)) {
1074 err
= PTR_ERR(resp_skb
);
1076 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1083 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
1085 switch (p
->info
.id
.proto
) {
1091 /* IPCOMP spi is 16-bits. */
1092 if (p
->max
>= 0x10000)
1100 if (p
->min
> p
->max
)
1106 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1107 struct nlattr
**attrs
)
1109 struct net
*net
= sock_net(skb
->sk
);
1110 struct xfrm_state
*x
;
1111 struct xfrm_userspi_info
*p
;
1112 struct sk_buff
*resp_skb
;
1113 xfrm_address_t
*daddr
;
1119 p
= nlmsg_data(nlh
);
1120 err
= verify_userspi_info(p
);
1124 family
= p
->info
.family
;
1125 daddr
= &p
->info
.id
.daddr
;
1129 mark
= xfrm_mark_get(attrs
, &m
);
1131 x
= xfrm_find_acq_byseq(net
, mark
, p
->info
.seq
);
1132 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, daddr
, family
)) {
1139 x
= xfrm_find_acq(net
, &m
, p
->info
.mode
, p
->info
.reqid
,
1140 p
->info
.id
.proto
, daddr
,
1147 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
1151 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
1152 if (IS_ERR(resp_skb
)) {
1153 err
= PTR_ERR(resp_skb
);
1157 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
, NETLINK_CB(skb
).portid
);
1165 static int verify_policy_dir(u8 dir
)
1168 case XFRM_POLICY_IN
:
1169 case XFRM_POLICY_OUT
:
1170 case XFRM_POLICY_FWD
:
1180 static int verify_policy_type(u8 type
)
1183 case XFRM_POLICY_TYPE_MAIN
:
1184 #ifdef CONFIG_XFRM_SUB_POLICY
1185 case XFRM_POLICY_TYPE_SUB
:
1196 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
1199 case XFRM_SHARE_ANY
:
1200 case XFRM_SHARE_SESSION
:
1201 case XFRM_SHARE_USER
:
1202 case XFRM_SHARE_UNIQUE
:
1209 switch (p
->action
) {
1210 case XFRM_POLICY_ALLOW
:
1211 case XFRM_POLICY_BLOCK
:
1218 switch (p
->sel
.family
) {
1223 #if IS_ENABLED(CONFIG_IPV6)
1226 return -EAFNOSUPPORT
;
1233 return verify_policy_dir(p
->dir
);
1236 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1238 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1239 struct xfrm_user_sec_ctx
*uctx
;
1244 uctx
= nla_data(rt
);
1245 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
1248 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
1254 for (i
= 0; i
< nr
; i
++, ut
++) {
1255 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1257 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1258 memcpy(&t
->saddr
, &ut
->saddr
,
1259 sizeof(xfrm_address_t
));
1260 t
->reqid
= ut
->reqid
;
1262 t
->share
= ut
->share
;
1263 t
->optional
= ut
->optional
;
1264 t
->aalgos
= ut
->aalgos
;
1265 t
->ealgos
= ut
->ealgos
;
1266 t
->calgos
= ut
->calgos
;
1267 /* If all masks are ~0, then we allow all algorithms. */
1268 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
1269 t
->encap_family
= ut
->family
;
1273 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1277 if (nr
> XFRM_MAX_DEPTH
)
1280 for (i
= 0; i
< nr
; i
++) {
1281 /* We never validated the ut->family value, so many
1282 * applications simply leave it at zero. The check was
1283 * never made and ut->family was ignored because all
1284 * templates could be assumed to have the same family as
1285 * the policy itself. Now that we will have ipv4-in-ipv6
1286 * and ipv6-in-ipv4 tunnels, this is no longer true.
1289 ut
[i
].family
= family
;
1291 switch (ut
[i
].family
) {
1294 #if IS_ENABLED(CONFIG_IPV6)
1306 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1308 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1313 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1314 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1317 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1321 copy_templates(pol
, utmpl
, nr
);
1326 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1328 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1329 struct xfrm_userpolicy_type
*upt
;
1330 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1338 err
= verify_policy_type(type
);
1346 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1348 xp
->priority
= p
->priority
;
1349 xp
->index
= p
->index
;
1350 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1351 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1352 xp
->action
= p
->action
;
1353 xp
->flags
= p
->flags
;
1354 xp
->family
= p
->sel
.family
;
1355 /* XXX xp->share = p->share; */
1358 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1360 memset(p
, 0, sizeof(*p
));
1361 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1362 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1363 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1364 p
->priority
= xp
->priority
;
1365 p
->index
= xp
->index
;
1366 p
->sel
.family
= xp
->family
;
1368 p
->action
= xp
->action
;
1369 p
->flags
= xp
->flags
;
1370 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1373 static struct xfrm_policy
*xfrm_policy_construct(struct net
*net
, struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1375 struct xfrm_policy
*xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
1383 copy_from_user_policy(xp
, p
);
1385 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1389 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1390 err
= copy_from_user_sec_ctx(xp
, attrs
);
1394 xfrm_mark_get(attrs
, &xp
->mark
);
1400 xfrm_policy_destroy(xp
);
1404 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1405 struct nlattr
**attrs
)
1407 struct net
*net
= sock_net(skb
->sk
);
1408 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1409 struct xfrm_policy
*xp
;
1413 kuid_t loginuid
= audit_get_loginuid(current
);
1414 u32 sessionid
= audit_get_sessionid(current
);
1417 err
= verify_newpolicy_info(p
);
1420 err
= verify_sec_ctx_len(attrs
);
1424 xp
= xfrm_policy_construct(net
, p
, attrs
, &err
);
1428 /* shouldn't excl be based on nlh flags??
1429 * Aha! this is anti-netlink really i.e more pfkey derived
1430 * in netlink excl is a flag and you wouldnt need
1431 * a type XFRM_MSG_UPDPOLICY - JHS */
1432 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1433 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1434 security_task_getsecid(current
, &sid
);
1435 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1438 security_xfrm_policy_free(xp
->security
);
1443 c
.event
= nlh
->nlmsg_type
;
1444 c
.seq
= nlh
->nlmsg_seq
;
1445 c
.portid
= nlh
->nlmsg_pid
;
1446 km_policy_notify(xp
, p
->dir
, &c
);
1453 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1455 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1458 if (xp
->xfrm_nr
== 0)
1461 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1462 struct xfrm_user_tmpl
*up
= &vec
[i
];
1463 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1465 memset(up
, 0, sizeof(*up
));
1466 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1467 up
->family
= kp
->encap_family
;
1468 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1469 up
->reqid
= kp
->reqid
;
1470 up
->mode
= kp
->mode
;
1471 up
->share
= kp
->share
;
1472 up
->optional
= kp
->optional
;
1473 up
->aalgos
= kp
->aalgos
;
1474 up
->ealgos
= kp
->ealgos
;
1475 up
->calgos
= kp
->calgos
;
1478 return nla_put(skb
, XFRMA_TMPL
,
1479 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1482 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1485 return copy_sec_ctx(x
->security
, skb
);
1490 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1493 return copy_sec_ctx(xp
->security
, skb
);
1496 static inline size_t userpolicy_type_attrsize(void)
1498 #ifdef CONFIG_XFRM_SUB_POLICY
1499 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1505 #ifdef CONFIG_XFRM_SUB_POLICY
1506 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1508 struct xfrm_userpolicy_type upt
= {
1512 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1516 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1522 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1524 struct xfrm_dump_info
*sp
= ptr
;
1525 struct xfrm_userpolicy_info
*p
;
1526 struct sk_buff
*in_skb
= sp
->in_skb
;
1527 struct sk_buff
*skb
= sp
->out_skb
;
1528 struct nlmsghdr
*nlh
;
1531 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, sp
->nlmsg_seq
,
1532 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1536 p
= nlmsg_data(nlh
);
1537 copy_to_user_policy(xp
, p
, dir
);
1538 err
= copy_to_user_tmpl(xp
, skb
);
1540 err
= copy_to_user_sec_ctx(xp
, skb
);
1542 err
= copy_to_user_policy_type(xp
->type
, skb
);
1544 err
= xfrm_mark_put(skb
, &xp
->mark
);
1546 nlmsg_cancel(skb
, nlh
);
1549 nlmsg_end(skb
, nlh
);
1553 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1555 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1557 xfrm_policy_walk_done(walk
);
1561 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1563 struct net
*net
= sock_net(skb
->sk
);
1564 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1565 struct xfrm_dump_info info
;
1567 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1568 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1570 info
.in_skb
= cb
->skb
;
1572 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1573 info
.nlmsg_flags
= NLM_F_MULTI
;
1577 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1580 (void) xfrm_policy_walk(net
, walk
, dump_one_policy
, &info
);
1585 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1586 struct xfrm_policy
*xp
,
1589 struct xfrm_dump_info info
;
1590 struct sk_buff
*skb
;
1593 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1595 return ERR_PTR(-ENOMEM
);
1597 info
.in_skb
= in_skb
;
1599 info
.nlmsg_seq
= seq
;
1600 info
.nlmsg_flags
= 0;
1602 err
= dump_one_policy(xp
, dir
, 0, &info
);
1605 return ERR_PTR(err
);
1611 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1612 struct nlattr
**attrs
)
1614 struct net
*net
= sock_net(skb
->sk
);
1615 struct xfrm_policy
*xp
;
1616 struct xfrm_userpolicy_id
*p
;
1617 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1622 u32 mark
= xfrm_mark_get(attrs
, &m
);
1624 p
= nlmsg_data(nlh
);
1625 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1627 err
= copy_from_user_policy_type(&type
, attrs
);
1631 err
= verify_policy_dir(p
->dir
);
1636 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, delete, &err
);
1638 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1639 struct xfrm_sec_ctx
*ctx
;
1641 err
= verify_sec_ctx_len(attrs
);
1647 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1649 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1653 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
, &p
->sel
,
1655 security_xfrm_policy_free(ctx
);
1661 struct sk_buff
*resp_skb
;
1663 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1664 if (IS_ERR(resp_skb
)) {
1665 err
= PTR_ERR(resp_skb
);
1667 err
= nlmsg_unicast(net
->xfrm
.nlsk
, resp_skb
,
1668 NETLINK_CB(skb
).portid
);
1671 kuid_t loginuid
= audit_get_loginuid(current
);
1672 u32 sessionid
= audit_get_sessionid(current
);
1675 security_task_getsecid(current
, &sid
);
1676 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1682 c
.data
.byid
= p
->index
;
1683 c
.event
= nlh
->nlmsg_type
;
1684 c
.seq
= nlh
->nlmsg_seq
;
1685 c
.portid
= nlh
->nlmsg_pid
;
1686 km_policy_notify(xp
, p
->dir
, &c
);
1691 if (delete && err
== 0)
1692 xfrm_garbage_collect(net
);
1696 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1697 struct nlattr
**attrs
)
1699 struct net
*net
= sock_net(skb
->sk
);
1701 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1702 struct xfrm_audit audit_info
;
1705 audit_info
.loginuid
= audit_get_loginuid(current
);
1706 audit_info
.sessionid
= audit_get_sessionid(current
);
1707 security_task_getsecid(current
, &audit_info
.secid
);
1708 err
= xfrm_state_flush(net
, p
->proto
, &audit_info
);
1710 if (err
== -ESRCH
) /* empty table */
1714 c
.data
.proto
= p
->proto
;
1715 c
.event
= nlh
->nlmsg_type
;
1716 c
.seq
= nlh
->nlmsg_seq
;
1717 c
.portid
= nlh
->nlmsg_pid
;
1719 km_state_notify(NULL
, &c
);
1724 static inline size_t xfrm_aevent_msgsize(struct xfrm_state
*x
)
1726 size_t replay_size
= x
->replay_esn
?
1727 xfrm_replay_state_esn_len(x
->replay_esn
) :
1728 sizeof(struct xfrm_replay_state
);
1730 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1731 + nla_total_size(replay_size
)
1732 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1733 + nla_total_size(sizeof(struct xfrm_mark
))
1734 + nla_total_size(4) /* XFRM_AE_RTHR */
1735 + nla_total_size(4); /* XFRM_AE_ETHR */
1738 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
1740 struct xfrm_aevent_id
*id
;
1741 struct nlmsghdr
*nlh
;
1744 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1748 id
= nlmsg_data(nlh
);
1749 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1750 id
->sa_id
.spi
= x
->id
.spi
;
1751 id
->sa_id
.family
= x
->props
.family
;
1752 id
->sa_id
.proto
= x
->id
.proto
;
1753 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1754 id
->reqid
= x
->props
.reqid
;
1755 id
->flags
= c
->data
.aevent
;
1757 if (x
->replay_esn
) {
1758 err
= nla_put(skb
, XFRMA_REPLAY_ESN_VAL
,
1759 xfrm_replay_state_esn_len(x
->replay_esn
),
1762 err
= nla_put(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
),
1767 err
= nla_put(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1771 if (id
->flags
& XFRM_AE_RTHR
) {
1772 err
= nla_put_u32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1776 if (id
->flags
& XFRM_AE_ETHR
) {
1777 err
= nla_put_u32(skb
, XFRMA_ETIMER_THRESH
,
1778 x
->replay_maxage
* 10 / HZ
);
1782 err
= xfrm_mark_put(skb
, &x
->mark
);
1786 return nlmsg_end(skb
, nlh
);
1789 nlmsg_cancel(skb
, nlh
);
1793 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1794 struct nlattr
**attrs
)
1796 struct net
*net
= sock_net(skb
->sk
);
1797 struct xfrm_state
*x
;
1798 struct sk_buff
*r_skb
;
1803 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1804 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1806 mark
= xfrm_mark_get(attrs
, &m
);
1808 x
= xfrm_state_lookup(net
, mark
, &id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1812 r_skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
1813 if (r_skb
== NULL
) {
1819 * XXX: is this lock really needed - none of the other
1820 * gets lock (the concern is things getting updated
1821 * while we are still reading) - jhs
1823 spin_lock_bh(&x
->lock
);
1824 c
.data
.aevent
= p
->flags
;
1825 c
.seq
= nlh
->nlmsg_seq
;
1826 c
.portid
= nlh
->nlmsg_pid
;
1828 if (build_aevent(r_skb
, x
, &c
) < 0)
1830 err
= nlmsg_unicast(net
->xfrm
.nlsk
, r_skb
, NETLINK_CB(skb
).portid
);
1831 spin_unlock_bh(&x
->lock
);
1836 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1837 struct nlattr
**attrs
)
1839 struct net
*net
= sock_net(skb
->sk
);
1840 struct xfrm_state
*x
;
1845 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1846 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1847 struct nlattr
*re
= attrs
[XFRMA_REPLAY_ESN_VAL
];
1848 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1850 if (!lt
&& !rp
&& !re
)
1853 /* pedantic mode - thou shalt sayeth replaceth */
1854 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1857 mark
= xfrm_mark_get(attrs
, &m
);
1859 x
= xfrm_state_lookup(net
, mark
, &p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1863 if (x
->km
.state
!= XFRM_STATE_VALID
)
1866 err
= xfrm_replay_verify_len(x
->replay_esn
, rp
);
1870 spin_lock_bh(&x
->lock
);
1871 xfrm_update_ae_params(x
, attrs
, 1);
1872 spin_unlock_bh(&x
->lock
);
1874 c
.event
= nlh
->nlmsg_type
;
1875 c
.seq
= nlh
->nlmsg_seq
;
1876 c
.portid
= nlh
->nlmsg_pid
;
1877 c
.data
.aevent
= XFRM_AE_CU
;
1878 km_state_notify(x
, &c
);
1885 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1886 struct nlattr
**attrs
)
1888 struct net
*net
= sock_net(skb
->sk
);
1890 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1892 struct xfrm_audit audit_info
;
1894 err
= copy_from_user_policy_type(&type
, attrs
);
1898 audit_info
.loginuid
= audit_get_loginuid(current
);
1899 audit_info
.sessionid
= audit_get_sessionid(current
);
1900 security_task_getsecid(current
, &audit_info
.secid
);
1901 err
= xfrm_policy_flush(net
, type
, &audit_info
);
1903 if (err
== -ESRCH
) /* empty table */
1909 c
.event
= nlh
->nlmsg_type
;
1910 c
.seq
= nlh
->nlmsg_seq
;
1911 c
.portid
= nlh
->nlmsg_pid
;
1913 km_policy_notify(NULL
, 0, &c
);
1917 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1918 struct nlattr
**attrs
)
1920 struct net
*net
= sock_net(skb
->sk
);
1921 struct xfrm_policy
*xp
;
1922 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1923 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1924 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1927 u32 mark
= xfrm_mark_get(attrs
, &m
);
1929 err
= copy_from_user_policy_type(&type
, attrs
);
1933 err
= verify_policy_dir(p
->dir
);
1938 xp
= xfrm_policy_byid(net
, mark
, type
, p
->dir
, p
->index
, 0, &err
);
1940 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1941 struct xfrm_sec_ctx
*ctx
;
1943 err
= verify_sec_ctx_len(attrs
);
1949 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1951 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1955 xp
= xfrm_policy_bysel_ctx(net
, mark
, type
, p
->dir
,
1956 &p
->sel
, ctx
, 0, &err
);
1957 security_xfrm_policy_free(ctx
);
1962 if (unlikely(xp
->walk
.dead
))
1967 kuid_t loginuid
= audit_get_loginuid(current
);
1968 u32 sessionid
= audit_get_sessionid(current
);
1971 security_task_getsecid(current
, &sid
);
1972 xfrm_policy_delete(xp
, p
->dir
);
1973 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1976 // reset the timers here?
1977 WARN(1, "Dont know what to do with soft policy expire\n");
1979 km_policy_expired(xp
, p
->dir
, up
->hard
, nlh
->nlmsg_pid
);
1986 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1987 struct nlattr
**attrs
)
1989 struct net
*net
= sock_net(skb
->sk
);
1990 struct xfrm_state
*x
;
1992 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1993 struct xfrm_usersa_info
*p
= &ue
->state
;
1995 u32 mark
= xfrm_mark_get(attrs
, &m
);
1997 x
= xfrm_state_lookup(net
, mark
, &p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
2003 spin_lock_bh(&x
->lock
);
2005 if (x
->km
.state
!= XFRM_STATE_VALID
)
2007 km_state_expired(x
, ue
->hard
, nlh
->nlmsg_pid
);
2010 kuid_t loginuid
= audit_get_loginuid(current
);
2011 u32 sessionid
= audit_get_sessionid(current
);
2014 security_task_getsecid(current
, &sid
);
2015 __xfrm_state_delete(x
);
2016 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
2020 spin_unlock_bh(&x
->lock
);
2025 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2026 struct nlattr
**attrs
)
2028 struct net
*net
= sock_net(skb
->sk
);
2029 struct xfrm_policy
*xp
;
2030 struct xfrm_user_tmpl
*ut
;
2032 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
2033 struct xfrm_mark mark
;
2035 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
2036 struct xfrm_state
*x
= xfrm_state_alloc(net
);
2042 xfrm_mark_get(attrs
, &mark
);
2044 err
= verify_newpolicy_info(&ua
->policy
);
2049 xp
= xfrm_policy_construct(net
, &ua
->policy
, attrs
, &err
);
2053 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
2054 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
2055 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
2056 xp
->mark
.m
= x
->mark
.m
= mark
.m
;
2057 xp
->mark
.v
= x
->mark
.v
= mark
.v
;
2059 /* extract the templates and for each call km_key */
2060 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
2061 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
2062 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
2063 x
->props
.mode
= t
->mode
;
2064 x
->props
.reqid
= t
->reqid
;
2065 x
->props
.family
= ut
->family
;
2066 t
->aalgos
= ua
->aalgos
;
2067 t
->ealgos
= ua
->ealgos
;
2068 t
->calgos
= ua
->calgos
;
2069 err
= km_query(x
, t
, xp
);
2079 WARN(1, "BAD policy passed\n");
2086 #ifdef CONFIG_XFRM_MIGRATE
2087 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
2088 struct xfrm_kmaddress
*k
,
2089 struct nlattr
**attrs
, int *num
)
2091 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
2092 struct xfrm_user_migrate
*um
;
2096 struct xfrm_user_kmaddress
*uk
;
2098 uk
= nla_data(attrs
[XFRMA_KMADDRESS
]);
2099 memcpy(&k
->local
, &uk
->local
, sizeof(k
->local
));
2100 memcpy(&k
->remote
, &uk
->remote
, sizeof(k
->remote
));
2101 k
->family
= uk
->family
;
2102 k
->reserved
= uk
->reserved
;
2106 num_migrate
= nla_len(rt
) / sizeof(*um
);
2108 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
2111 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
2112 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
2113 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
2114 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
2115 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
2117 ma
->proto
= um
->proto
;
2118 ma
->mode
= um
->mode
;
2119 ma
->reqid
= um
->reqid
;
2121 ma
->old_family
= um
->old_family
;
2122 ma
->new_family
= um
->new_family
;
2129 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2130 struct nlattr
**attrs
)
2132 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
2133 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2134 struct xfrm_kmaddress km
, *kmp
;
2139 if (attrs
[XFRMA_MIGRATE
] == NULL
)
2142 kmp
= attrs
[XFRMA_KMADDRESS
] ? &km
: NULL
;
2144 err
= copy_from_user_policy_type(&type
, attrs
);
2148 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
, kmp
, attrs
, &n
);
2155 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
, kmp
);
2160 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2161 struct nlattr
**attrs
)
2163 return -ENOPROTOOPT
;
2167 #ifdef CONFIG_XFRM_MIGRATE
2168 static int copy_to_user_migrate(const struct xfrm_migrate
*m
, struct sk_buff
*skb
)
2170 struct xfrm_user_migrate um
;
2172 memset(&um
, 0, sizeof(um
));
2173 um
.proto
= m
->proto
;
2175 um
.reqid
= m
->reqid
;
2176 um
.old_family
= m
->old_family
;
2177 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
2178 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
2179 um
.new_family
= m
->new_family
;
2180 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
2181 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
2183 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
2186 static int copy_to_user_kmaddress(const struct xfrm_kmaddress
*k
, struct sk_buff
*skb
)
2188 struct xfrm_user_kmaddress uk
;
2190 memset(&uk
, 0, sizeof(uk
));
2191 uk
.family
= k
->family
;
2192 uk
.reserved
= k
->reserved
;
2193 memcpy(&uk
.local
, &k
->local
, sizeof(uk
.local
));
2194 memcpy(&uk
.remote
, &k
->remote
, sizeof(uk
.remote
));
2196 return nla_put(skb
, XFRMA_KMADDRESS
, sizeof(uk
), &uk
);
2199 static inline size_t xfrm_migrate_msgsize(int num_migrate
, int with_kma
)
2201 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
2202 + (with_kma
? nla_total_size(sizeof(struct xfrm_kmaddress
)) : 0)
2203 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
2204 + userpolicy_type_attrsize();
2207 static int build_migrate(struct sk_buff
*skb
, const struct xfrm_migrate
*m
,
2208 int num_migrate
, const struct xfrm_kmaddress
*k
,
2209 const struct xfrm_selector
*sel
, u8 dir
, u8 type
)
2211 const struct xfrm_migrate
*mp
;
2212 struct xfrm_userpolicy_id
*pol_id
;
2213 struct nlmsghdr
*nlh
;
2216 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
2220 pol_id
= nlmsg_data(nlh
);
2221 /* copy data from selector, dir, and type to the pol_id */
2222 memset(pol_id
, 0, sizeof(*pol_id
));
2223 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
2227 err
= copy_to_user_kmaddress(k
, skb
);
2231 err
= copy_to_user_policy_type(type
, skb
);
2234 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
2235 err
= copy_to_user_migrate(mp
, skb
);
2240 return nlmsg_end(skb
, nlh
);
2243 nlmsg_cancel(skb
, nlh
);
2247 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2248 const struct xfrm_migrate
*m
, int num_migrate
,
2249 const struct xfrm_kmaddress
*k
)
2251 struct net
*net
= &init_net
;
2252 struct sk_buff
*skb
;
2254 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
, !!k
), GFP_ATOMIC
);
2259 if (build_migrate(skb
, m
, num_migrate
, k
, sel
, dir
, type
) < 0)
2262 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
2265 static int xfrm_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
2266 const struct xfrm_migrate
*m
, int num_migrate
,
2267 const struct xfrm_kmaddress
*k
)
2269 return -ENOPROTOOPT
;
2273 #define XMSGSIZE(type) sizeof(struct type)
2275 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
2276 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2277 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2278 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
2279 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2280 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2281 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2282 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
2283 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
2284 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
2285 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
2286 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
2287 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
2288 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
2289 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
2290 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2291 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
2292 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
2293 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
2294 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2295 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
2300 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
2301 [XFRMA_SA
] = { .len
= sizeof(struct xfrm_usersa_info
)},
2302 [XFRMA_POLICY
] = { .len
= sizeof(struct xfrm_userpolicy_info
)},
2303 [XFRMA_LASTUSED
] = { .type
= NLA_U64
},
2304 [XFRMA_ALG_AUTH_TRUNC
] = { .len
= sizeof(struct xfrm_algo_auth
)},
2305 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
2306 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
2307 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
2308 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
2309 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
2310 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
2311 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
2312 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
2313 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
2314 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
2315 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
2316 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
2317 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
2318 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
2319 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
2320 [XFRMA_KMADDRESS
] = { .len
= sizeof(struct xfrm_user_kmaddress
) },
2321 [XFRMA_MARK
] = { .len
= sizeof(struct xfrm_mark
) },
2322 [XFRMA_TFCPAD
] = { .type
= NLA_U32
},
2323 [XFRMA_REPLAY_ESN_VAL
] = { .len
= sizeof(struct xfrm_replay_state_esn
) },
2324 [XFRMA_SA_EXTRA_FLAGS
] = { .type
= NLA_U32
},
2327 static const struct xfrm_link
{
2328 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
2329 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
2330 int (*done
)(struct netlink_callback
*);
2331 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
2332 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2333 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
2334 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
2335 .dump
= xfrm_dump_sa
,
2336 .done
= xfrm_dump_sa_done
},
2337 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2338 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
2339 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
2340 .dump
= xfrm_dump_policy
,
2341 .done
= xfrm_dump_policy_done
},
2342 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
2343 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
2344 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
2345 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
2346 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
2347 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
2348 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
2349 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
2350 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
2351 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
2352 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
2353 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
2354 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
2357 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2359 struct net
*net
= sock_net(skb
->sk
);
2360 struct nlattr
*attrs
[XFRMA_MAX
+1];
2361 const struct xfrm_link
*link
;
2364 type
= nlh
->nlmsg_type
;
2365 if (type
> XFRM_MSG_MAX
)
2368 type
-= XFRM_MSG_BASE
;
2369 link
= &xfrm_dispatch
[type
];
2371 /* All operations require privileges, even GET */
2372 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
))
2375 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
2376 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
2377 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
2378 if (link
->dump
== NULL
)
2382 struct netlink_dump_control c
= {
2386 return netlink_dump_start(net
->xfrm
.nlsk
, skb
, nlh
, &c
);
2390 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
2395 if (link
->doit
== NULL
)
2398 return link
->doit(skb
, nlh
, attrs
);
2401 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
2403 mutex_lock(&xfrm_cfg_mutex
);
2404 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
2405 mutex_unlock(&xfrm_cfg_mutex
);
2408 static inline size_t xfrm_expire_msgsize(void)
2410 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
))
2411 + nla_total_size(sizeof(struct xfrm_mark
));
2414 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, const struct km_event
*c
)
2416 struct xfrm_user_expire
*ue
;
2417 struct nlmsghdr
*nlh
;
2420 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2424 ue
= nlmsg_data(nlh
);
2425 copy_to_user_state(x
, &ue
->state
);
2426 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2428 err
= xfrm_mark_put(skb
, &x
->mark
);
2432 return nlmsg_end(skb
, nlh
);
2435 static int xfrm_exp_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2437 struct net
*net
= xs_net(x
);
2438 struct sk_buff
*skb
;
2440 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2444 if (build_expire(skb
, x
, c
) < 0) {
2449 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2452 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2454 struct net
*net
= xs_net(x
);
2455 struct sk_buff
*skb
;
2457 skb
= nlmsg_new(xfrm_aevent_msgsize(x
), GFP_ATOMIC
);
2461 if (build_aevent(skb
, x
, c
) < 0)
2464 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2467 static int xfrm_notify_sa_flush(const struct km_event
*c
)
2469 struct net
*net
= c
->net
;
2470 struct xfrm_usersa_flush
*p
;
2471 struct nlmsghdr
*nlh
;
2472 struct sk_buff
*skb
;
2473 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2475 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2479 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2485 p
= nlmsg_data(nlh
);
2486 p
->proto
= c
->data
.proto
;
2488 nlmsg_end(skb
, nlh
);
2490 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2493 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2497 l
+= nla_total_size(aead_len(x
->aead
));
2499 l
+= nla_total_size(sizeof(struct xfrm_algo
) +
2500 (x
->aalg
->alg_key_len
+ 7) / 8);
2501 l
+= nla_total_size(xfrm_alg_auth_len(x
->aalg
));
2504 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2506 l
+= nla_total_size(sizeof(*x
->calg
));
2508 l
+= nla_total_size(sizeof(*x
->encap
));
2510 l
+= nla_total_size(sizeof(x
->tfcpad
));
2512 l
+= nla_total_size(xfrm_replay_state_esn_len(x
->replay_esn
));
2514 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2515 x
->security
->ctx_len
);
2517 l
+= nla_total_size(sizeof(*x
->coaddr
));
2518 if (x
->props
.extra_flags
)
2519 l
+= nla_total_size(sizeof(x
->props
.extra_flags
));
2521 /* Must count x->lastused as it may become non-zero behind our back. */
2522 l
+= nla_total_size(sizeof(u64
));
2527 static int xfrm_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
2529 struct net
*net
= xs_net(x
);
2530 struct xfrm_usersa_info
*p
;
2531 struct xfrm_usersa_id
*id
;
2532 struct nlmsghdr
*nlh
;
2533 struct sk_buff
*skb
;
2534 int len
= xfrm_sa_len(x
);
2537 headlen
= sizeof(*p
);
2538 if (c
->event
== XFRM_MSG_DELSA
) {
2539 len
+= nla_total_size(headlen
);
2540 headlen
= sizeof(*id
);
2541 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2543 len
+= NLMSG_ALIGN(headlen
);
2545 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2549 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2554 p
= nlmsg_data(nlh
);
2555 if (c
->event
== XFRM_MSG_DELSA
) {
2556 struct nlattr
*attr
;
2558 id
= nlmsg_data(nlh
);
2559 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2560 id
->spi
= x
->id
.spi
;
2561 id
->family
= x
->props
.family
;
2562 id
->proto
= x
->id
.proto
;
2564 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2571 err
= copy_to_user_state_extra(x
, p
, skb
);
2575 nlmsg_end(skb
, nlh
);
2577 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2584 static int xfrm_send_state_notify(struct xfrm_state
*x
, const struct km_event
*c
)
2588 case XFRM_MSG_EXPIRE
:
2589 return xfrm_exp_state_notify(x
, c
);
2590 case XFRM_MSG_NEWAE
:
2591 return xfrm_aevent_state_notify(x
, c
);
2592 case XFRM_MSG_DELSA
:
2593 case XFRM_MSG_UPDSA
:
2594 case XFRM_MSG_NEWSA
:
2595 return xfrm_notify_sa(x
, c
);
2596 case XFRM_MSG_FLUSHSA
:
2597 return xfrm_notify_sa_flush(c
);
2599 printk(KERN_NOTICE
"xfrm_user: Unknown SA event %d\n",
2608 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2609 struct xfrm_policy
*xp
)
2611 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2612 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2613 + nla_total_size(sizeof(struct xfrm_mark
))
2614 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2615 + userpolicy_type_attrsize();
2618 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2619 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
)
2621 __u32 seq
= xfrm_get_acqseq();
2622 struct xfrm_user_acquire
*ua
;
2623 struct nlmsghdr
*nlh
;
2626 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2630 ua
= nlmsg_data(nlh
);
2631 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2632 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2633 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2634 copy_to_user_policy(xp
, &ua
->policy
, XFRM_POLICY_OUT
);
2635 ua
->aalgos
= xt
->aalgos
;
2636 ua
->ealgos
= xt
->ealgos
;
2637 ua
->calgos
= xt
->calgos
;
2638 ua
->seq
= x
->km
.seq
= seq
;
2640 err
= copy_to_user_tmpl(xp
, skb
);
2642 err
= copy_to_user_state_sec_ctx(x
, skb
);
2644 err
= copy_to_user_policy_type(xp
->type
, skb
);
2646 err
= xfrm_mark_put(skb
, &xp
->mark
);
2648 nlmsg_cancel(skb
, nlh
);
2652 return nlmsg_end(skb
, nlh
);
2655 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2656 struct xfrm_policy
*xp
)
2658 struct net
*net
= xs_net(x
);
2659 struct sk_buff
*skb
;
2661 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2665 if (build_acquire(skb
, x
, xt
, xp
) < 0)
2668 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2671 /* User gives us xfrm_user_policy_info followed by an array of 0
2672 * or more templates.
2674 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2675 u8
*data
, int len
, int *dir
)
2677 struct net
*net
= sock_net(sk
);
2678 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2679 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2680 struct xfrm_policy
*xp
;
2683 switch (sk
->sk_family
) {
2685 if (opt
!= IP_XFRM_POLICY
) {
2690 #if IS_ENABLED(CONFIG_IPV6)
2692 if (opt
!= IPV6_XFRM_POLICY
) {
2705 if (len
< sizeof(*p
) ||
2706 verify_newpolicy_info(p
))
2709 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2710 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2713 if (p
->dir
> XFRM_POLICY_OUT
)
2716 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
2722 copy_from_user_policy(xp
, p
);
2723 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2724 copy_templates(xp
, ut
, nr
);
2731 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2733 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2734 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2735 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2736 + nla_total_size(sizeof(struct xfrm_mark
))
2737 + userpolicy_type_attrsize();
2740 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2741 int dir
, const struct km_event
*c
)
2743 struct xfrm_user_polexpire
*upe
;
2744 int hard
= c
->data
.hard
;
2745 struct nlmsghdr
*nlh
;
2748 nlh
= nlmsg_put(skb
, c
->portid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2752 upe
= nlmsg_data(nlh
);
2753 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2754 err
= copy_to_user_tmpl(xp
, skb
);
2756 err
= copy_to_user_sec_ctx(xp
, skb
);
2758 err
= copy_to_user_policy_type(xp
->type
, skb
);
2760 err
= xfrm_mark_put(skb
, &xp
->mark
);
2762 nlmsg_cancel(skb
, nlh
);
2767 return nlmsg_end(skb
, nlh
);
2770 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2772 struct net
*net
= xp_net(xp
);
2773 struct sk_buff
*skb
;
2775 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2779 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2782 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2785 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2787 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2788 struct net
*net
= xp_net(xp
);
2789 struct xfrm_userpolicy_info
*p
;
2790 struct xfrm_userpolicy_id
*id
;
2791 struct nlmsghdr
*nlh
;
2792 struct sk_buff
*skb
;
2795 headlen
= sizeof(*p
);
2796 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2797 len
+= nla_total_size(headlen
);
2798 headlen
= sizeof(*id
);
2800 len
+= userpolicy_type_attrsize();
2801 len
+= nla_total_size(sizeof(struct xfrm_mark
));
2802 len
+= NLMSG_ALIGN(headlen
);
2804 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2808 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, c
->event
, headlen
, 0);
2813 p
= nlmsg_data(nlh
);
2814 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2815 struct nlattr
*attr
;
2817 id
= nlmsg_data(nlh
);
2818 memset(id
, 0, sizeof(*id
));
2821 id
->index
= xp
->index
;
2823 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2825 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2833 copy_to_user_policy(xp
, p
, dir
);
2834 err
= copy_to_user_tmpl(xp
, skb
);
2836 err
= copy_to_user_policy_type(xp
->type
, skb
);
2838 err
= xfrm_mark_put(skb
, &xp
->mark
);
2842 nlmsg_end(skb
, nlh
);
2844 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2851 static int xfrm_notify_policy_flush(const struct km_event
*c
)
2853 struct net
*net
= c
->net
;
2854 struct nlmsghdr
*nlh
;
2855 struct sk_buff
*skb
;
2858 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2862 nlh
= nlmsg_put(skb
, c
->portid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2866 err
= copy_to_user_policy_type(c
->data
.type
, skb
);
2870 nlmsg_end(skb
, nlh
);
2872 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2879 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2883 case XFRM_MSG_NEWPOLICY
:
2884 case XFRM_MSG_UPDPOLICY
:
2885 case XFRM_MSG_DELPOLICY
:
2886 return xfrm_notify_policy(xp
, dir
, c
);
2887 case XFRM_MSG_FLUSHPOLICY
:
2888 return xfrm_notify_policy_flush(c
);
2889 case XFRM_MSG_POLEXPIRE
:
2890 return xfrm_exp_policy_notify(xp
, dir
, c
);
2892 printk(KERN_NOTICE
"xfrm_user: Unknown Policy event %d\n",
2900 static inline size_t xfrm_report_msgsize(void)
2902 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2905 static int build_report(struct sk_buff
*skb
, u8 proto
,
2906 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2908 struct xfrm_user_report
*ur
;
2909 struct nlmsghdr
*nlh
;
2911 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2915 ur
= nlmsg_data(nlh
);
2917 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2920 int err
= nla_put(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2922 nlmsg_cancel(skb
, nlh
);
2926 return nlmsg_end(skb
, nlh
);
2929 static int xfrm_send_report(struct net
*net
, u8 proto
,
2930 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2932 struct sk_buff
*skb
;
2934 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2938 if (build_report(skb
, proto
, sel
, addr
) < 0)
2941 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2944 static inline size_t xfrm_mapping_msgsize(void)
2946 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping
));
2949 static int build_mapping(struct sk_buff
*skb
, struct xfrm_state
*x
,
2950 xfrm_address_t
*new_saddr
, __be16 new_sport
)
2952 struct xfrm_user_mapping
*um
;
2953 struct nlmsghdr
*nlh
;
2955 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MAPPING
, sizeof(*um
), 0);
2959 um
= nlmsg_data(nlh
);
2961 memcpy(&um
->id
.daddr
, &x
->id
.daddr
, sizeof(um
->id
.daddr
));
2962 um
->id
.spi
= x
->id
.spi
;
2963 um
->id
.family
= x
->props
.family
;
2964 um
->id
.proto
= x
->id
.proto
;
2965 memcpy(&um
->new_saddr
, new_saddr
, sizeof(um
->new_saddr
));
2966 memcpy(&um
->old_saddr
, &x
->props
.saddr
, sizeof(um
->old_saddr
));
2967 um
->new_sport
= new_sport
;
2968 um
->old_sport
= x
->encap
->encap_sport
;
2969 um
->reqid
= x
->props
.reqid
;
2971 return nlmsg_end(skb
, nlh
);
2974 static int xfrm_send_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
,
2977 struct net
*net
= xs_net(x
);
2978 struct sk_buff
*skb
;
2980 if (x
->id
.proto
!= IPPROTO_ESP
)
2986 skb
= nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC
);
2990 if (build_mapping(skb
, x
, ipaddr
, sport
) < 0)
2993 return nlmsg_multicast(net
->xfrm
.nlsk
, skb
, 0, XFRMNLGRP_MAPPING
, GFP_ATOMIC
);
2996 static struct xfrm_mgr netlink_mgr
= {
2998 .notify
= xfrm_send_state_notify
,
2999 .acquire
= xfrm_send_acquire
,
3000 .compile_policy
= xfrm_compile_policy
,
3001 .notify_policy
= xfrm_send_policy_notify
,
3002 .report
= xfrm_send_report
,
3003 .migrate
= xfrm_send_migrate
,
3004 .new_mapping
= xfrm_send_mapping
,
3007 static int __net_init
xfrm_user_net_init(struct net
*net
)
3010 struct netlink_kernel_cfg cfg
= {
3011 .groups
= XFRMNLGRP_MAX
,
3012 .input
= xfrm_netlink_rcv
,
3015 nlsk
= netlink_kernel_create(net
, NETLINK_XFRM
, &cfg
);
3018 net
->xfrm
.nlsk_stash
= nlsk
; /* Don't set to NULL */
3019 rcu_assign_pointer(net
->xfrm
.nlsk
, nlsk
);
3023 static void __net_exit
xfrm_user_net_exit(struct list_head
*net_exit_list
)
3026 list_for_each_entry(net
, net_exit_list
, exit_list
)
3027 RCU_INIT_POINTER(net
->xfrm
.nlsk
, NULL
);
3029 list_for_each_entry(net
, net_exit_list
, exit_list
)
3030 netlink_kernel_release(net
->xfrm
.nlsk_stash
);
3033 static struct pernet_operations xfrm_user_net_ops
= {
3034 .init
= xfrm_user_net_init
,
3035 .exit_batch
= xfrm_user_net_exit
,
3038 static int __init
xfrm_user_init(void)
3042 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
3044 rv
= register_pernet_subsys(&xfrm_user_net_ops
);
3047 rv
= xfrm_register_km(&netlink_mgr
);
3049 unregister_pernet_subsys(&xfrm_user_net_ops
);
3053 static void __exit
xfrm_user_exit(void)
3055 xfrm_unregister_km(&netlink_mgr
);
3056 unregister_pernet_subsys(&xfrm_user_net_ops
);
3059 module_init(xfrm_user_init
);
3060 module_exit(xfrm_user_exit
);
3061 MODULE_LICENSE("GPL");
3062 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);