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/rtnetlink.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/ipsec.h>
25 #include <linux/init.h>
26 #include <linux/security.h>
29 #include <net/netlink.h>
30 #include <asm/uaccess.h>
32 static int verify_one_alg(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
)
34 struct rtattr
*rt
= xfrma
[type
- 1];
35 struct xfrm_algo
*algp
;
41 len
= (rt
->rta_len
- sizeof(*rt
)) - sizeof(*algp
);
47 len
-= (algp
->alg_key_len
+ 7U) / 8;
53 if (!algp
->alg_key_len
&&
54 strcmp(algp
->alg_name
, "digest_null") != 0)
59 if (!algp
->alg_key_len
&&
60 strcmp(algp
->alg_name
, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
76 static int verify_encap_tmpl(struct rtattr
**xfrma
)
78 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
79 struct xfrm_encap_tmpl
*encap
;
84 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
90 static int verify_one_addr(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
,
91 xfrm_address_t
**addrp
)
93 struct rtattr
*rt
= xfrma
[type
- 1];
98 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(**addrp
))
102 *addrp
= RTA_DATA(rt
);
107 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
109 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
110 struct xfrm_user_sec_ctx
*uctx
;
116 if (rt
->rta_len
< sizeof(*uctx
))
121 len
+= sizeof(struct xfrm_user_sec_ctx
);
122 len
+= uctx
->ctx_len
;
124 if (uctx
->len
!= len
)
131 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
132 struct rtattr
**xfrma
)
142 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
154 switch (p
->id
.proto
) {
156 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
157 xfrma
[XFRMA_ALG_CRYPT
-1] ||
158 xfrma
[XFRMA_ALG_COMP
-1])
163 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
164 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
165 xfrma
[XFRMA_ALG_COMP
-1])
170 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
171 xfrma
[XFRMA_ALG_AUTH
-1] ||
172 xfrma
[XFRMA_ALG_CRYPT
-1])
180 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
182 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
184 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
186 if ((err
= verify_encap_tmpl(xfrma
)))
188 if ((err
= verify_sec_ctx_len(xfrma
)))
193 case XFRM_MODE_TRANSPORT
:
194 case XFRM_MODE_TUNNEL
:
207 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
208 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
209 struct rtattr
*u_arg
)
211 struct rtattr
*rta
= u_arg
;
212 struct xfrm_algo
*p
, *ualg
;
213 struct xfrm_algo_desc
*algo
;
219 ualg
= RTA_DATA(rta
);
221 algo
= get_byname(ualg
->alg_name
, 1);
224 *props
= algo
->desc
.sadb_alg_id
;
226 len
= sizeof(*ualg
) + (ualg
->alg_key_len
+ 7U) / 8;
227 p
= kmalloc(len
, GFP_KERNEL
);
231 memcpy(p
, ualg
, len
);
232 strcpy(p
->alg_name
, algo
->name
);
237 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
239 struct rtattr
*rta
= u_arg
;
240 struct xfrm_encap_tmpl
*p
, *uencap
;
245 uencap
= RTA_DATA(rta
);
246 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
250 memcpy(p
, uencap
, sizeof(*p
));
256 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy
*xp
)
258 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
262 len
+= sizeof(struct xfrm_user_sec_ctx
);
263 len
+= xfrm_ctx
->ctx_len
;
268 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
270 struct xfrm_user_sec_ctx
*uctx
;
275 uctx
= RTA_DATA(u_arg
);
276 return security_xfrm_state_alloc(x
, uctx
);
279 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
281 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
282 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
283 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
284 x
->props
.mode
= p
->mode
;
285 x
->props
.replay_window
= p
->replay_window
;
286 x
->props
.reqid
= p
->reqid
;
287 x
->props
.family
= p
->family
;
288 x
->props
.saddr
= p
->saddr
;
289 x
->props
.flags
= p
->flags
;
293 * someday when pfkey also has support, we could have the code
294 * somehow made shareable and move it to xfrm_state.c - JHS
297 static int xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
300 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
301 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
302 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
303 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
306 struct xfrm_replay_state
*replay
;
307 if (RTA_PAYLOAD(rp
) < sizeof(*replay
))
309 replay
= RTA_DATA(rp
);
310 memcpy(&x
->replay
, replay
, sizeof(*replay
));
311 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
315 struct xfrm_lifetime_cur
*ltime
;
316 if (RTA_PAYLOAD(lt
) < sizeof(*ltime
))
318 ltime
= RTA_DATA(lt
);
319 x
->curlft
.bytes
= ltime
->bytes
;
320 x
->curlft
.packets
= ltime
->packets
;
321 x
->curlft
.add_time
= ltime
->add_time
;
322 x
->curlft
.use_time
= ltime
->use_time
;
326 if (RTA_PAYLOAD(et
) < sizeof(u32
))
328 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
332 if (RTA_PAYLOAD(rt
) < sizeof(u32
))
334 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
342 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
343 struct rtattr
**xfrma
,
346 struct xfrm_state
*x
= xfrm_state_alloc();
352 copy_from_user_state(x
, p
);
354 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
355 xfrm_aalg_get_byname
,
356 xfrma
[XFRMA_ALG_AUTH
-1])))
358 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
359 xfrm_ealg_get_byname
,
360 xfrma
[XFRMA_ALG_CRYPT
-1])))
362 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
363 xfrm_calg_get_byname
,
364 xfrma
[XFRMA_ALG_COMP
-1])))
366 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
369 err
= xfrm_init_state(x
);
373 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
377 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
378 /* sysctl_xfrm_aevent_etime is in 100ms units */
379 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
380 x
->preplay
.bitmap
= 0;
381 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
382 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
384 /* override default values from above */
386 err
= xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
393 x
->km
.state
= XFRM_STATE_DEAD
;
400 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
402 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
403 struct xfrm_state
*x
;
407 err
= verify_newsa_info(p
, (struct rtattr
**)xfrma
);
411 x
= xfrm_state_construct(p
, (struct rtattr
**)xfrma
, &err
);
416 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
417 err
= xfrm_state_add(x
);
419 err
= xfrm_state_update(x
);
422 x
->km
.state
= XFRM_STATE_DEAD
;
427 c
.seq
= nlh
->nlmsg_seq
;
428 c
.pid
= nlh
->nlmsg_pid
;
429 c
.event
= nlh
->nlmsg_type
;
431 km_state_notify(x
, &c
);
437 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
438 struct rtattr
**xfrma
,
441 struct xfrm_state
*x
= NULL
;
444 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
446 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
448 xfrm_address_t
*saddr
= NULL
;
450 err
= verify_one_addr(xfrma
, XFRMA_SRCADDR
, &saddr
);
459 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
469 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
471 struct xfrm_state
*x
;
474 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
476 x
= xfrm_user_state_lookup(p
, (struct rtattr
**)xfrma
, &err
);
480 if ((err
= security_xfrm_state_delete(x
)) != 0)
483 if (xfrm_state_kern(x
)) {
488 err
= xfrm_state_delete(x
);
492 c
.seq
= nlh
->nlmsg_seq
;
493 c
.pid
= nlh
->nlmsg_pid
;
494 c
.event
= nlh
->nlmsg_type
;
495 km_state_notify(x
, &c
);
502 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
504 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
505 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
506 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
507 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
508 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
509 p
->saddr
= x
->props
.saddr
;
510 p
->mode
= x
->props
.mode
;
511 p
->replay_window
= x
->props
.replay_window
;
512 p
->reqid
= x
->props
.reqid
;
513 p
->family
= x
->props
.family
;
514 p
->flags
= x
->props
.flags
;
518 struct xfrm_dump_info
{
519 struct sk_buff
*in_skb
;
520 struct sk_buff
*out_skb
;
527 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
529 struct xfrm_dump_info
*sp
= ptr
;
530 struct sk_buff
*in_skb
= sp
->in_skb
;
531 struct sk_buff
*skb
= sp
->out_skb
;
532 struct xfrm_usersa_info
*p
;
533 struct nlmsghdr
*nlh
;
534 unsigned char *b
= skb
->tail
;
536 if (sp
->this_idx
< sp
->start_idx
)
539 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
541 XFRM_MSG_NEWSA
, sizeof(*p
));
542 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
545 copy_to_user_state(x
, p
);
548 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
549 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
551 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
552 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
554 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
557 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
560 int ctx_size
= sizeof(struct xfrm_sec_ctx
) +
561 x
->security
->ctx_len
;
562 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
563 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
565 uctx
->exttype
= XFRMA_SEC_CTX
;
566 uctx
->len
= ctx_size
;
567 uctx
->ctx_doi
= x
->security
->ctx_doi
;
568 uctx
->ctx_alg
= x
->security
->ctx_alg
;
569 uctx
->ctx_len
= x
->security
->ctx_len
;
570 memcpy(uctx
+ 1, x
->security
->ctx_str
, x
->security
->ctx_len
);
572 nlh
->nlmsg_len
= skb
->tail
- b
;
579 skb_trim(skb
, b
- skb
->data
);
583 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
585 struct xfrm_dump_info info
;
587 info
.in_skb
= cb
->skb
;
589 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
590 info
.nlmsg_flags
= NLM_F_MULTI
;
592 info
.start_idx
= cb
->args
[0];
593 (void) xfrm_state_walk(0, dump_one_state
, &info
);
594 cb
->args
[0] = info
.this_idx
;
599 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
600 struct xfrm_state
*x
, u32 seq
)
602 struct xfrm_dump_info info
;
605 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
607 return ERR_PTR(-ENOMEM
);
609 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
610 info
.in_skb
= in_skb
;
612 info
.nlmsg_seq
= seq
;
613 info
.nlmsg_flags
= 0;
614 info
.this_idx
= info
.start_idx
= 0;
616 if (dump_one_state(x
, 0, &info
)) {
624 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
626 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
627 struct xfrm_state
*x
;
628 struct sk_buff
*resp_skb
;
631 x
= xfrm_user_state_lookup(p
, (struct rtattr
**)xfrma
, &err
);
635 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
636 if (IS_ERR(resp_skb
)) {
637 err
= PTR_ERR(resp_skb
);
639 err
= netlink_unicast(xfrm_nl
, resp_skb
,
640 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
647 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
649 switch (p
->info
.id
.proto
) {
655 /* IPCOMP spi is 16-bits. */
656 if (p
->max
>= 0x10000)
670 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
672 struct xfrm_state
*x
;
673 struct xfrm_userspi_info
*p
;
674 struct sk_buff
*resp_skb
;
675 xfrm_address_t
*daddr
;
680 err
= verify_userspi_info(p
);
684 family
= p
->info
.family
;
685 daddr
= &p
->info
.id
.daddr
;
689 x
= xfrm_find_acq_byseq(p
->info
.seq
);
690 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
697 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
698 p
->info
.id
.proto
, daddr
,
705 resp_skb
= ERR_PTR(-ENOENT
);
707 spin_lock_bh(&x
->lock
);
708 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
709 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
711 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
713 spin_unlock_bh(&x
->lock
);
715 if (IS_ERR(resp_skb
)) {
716 err
= PTR_ERR(resp_skb
);
720 err
= netlink_unicast(xfrm_nl
, resp_skb
,
721 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
729 static int verify_policy_dir(__u8 dir
)
733 case XFRM_POLICY_OUT
:
734 case XFRM_POLICY_FWD
:
744 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
748 case XFRM_SHARE_SESSION
:
749 case XFRM_SHARE_USER
:
750 case XFRM_SHARE_UNIQUE
:
758 case XFRM_POLICY_ALLOW
:
759 case XFRM_POLICY_BLOCK
:
766 switch (p
->sel
.family
) {
771 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
774 return -EAFNOSUPPORT
;
781 return verify_policy_dir(p
->dir
);
784 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
786 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
787 struct xfrm_user_sec_ctx
*uctx
;
793 return security_xfrm_policy_alloc(pol
, uctx
);
796 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
802 for (i
= 0; i
< nr
; i
++, ut
++) {
803 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
805 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
806 memcpy(&t
->saddr
, &ut
->saddr
,
807 sizeof(xfrm_address_t
));
808 t
->reqid
= ut
->reqid
;
810 t
->share
= ut
->share
;
811 t
->optional
= ut
->optional
;
812 t
->aalgos
= ut
->aalgos
;
813 t
->ealgos
= ut
->ealgos
;
814 t
->calgos
= ut
->calgos
;
818 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
820 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
821 struct xfrm_user_tmpl
*utmpl
;
827 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
829 if (nr
> XFRM_MAX_DEPTH
)
832 copy_templates(pol
, RTA_DATA(rt
), nr
);
837 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
839 xp
->priority
= p
->priority
;
840 xp
->index
= p
->index
;
841 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
842 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
843 xp
->action
= p
->action
;
844 xp
->flags
= p
->flags
;
845 xp
->family
= p
->sel
.family
;
846 /* XXX xp->share = p->share; */
849 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
851 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
852 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
853 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
854 p
->priority
= xp
->priority
;
855 p
->index
= xp
->index
;
856 p
->sel
.family
= xp
->family
;
858 p
->action
= xp
->action
;
859 p
->flags
= xp
->flags
;
860 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
863 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
865 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
873 copy_from_user_policy(xp
, p
);
875 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
876 err
= copy_from_user_sec_ctx(xp
, xfrma
);
887 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
889 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
890 struct xfrm_policy
*xp
;
895 err
= verify_newpolicy_info(p
);
898 err
= verify_sec_ctx_len((struct rtattr
**)xfrma
);
902 xp
= xfrm_policy_construct(p
, (struct rtattr
**)xfrma
, &err
);
906 /* shouldnt excl be based on nlh flags??
907 * Aha! this is anti-netlink really i.e more pfkey derived
908 * in netlink excl is a flag and you wouldnt need
909 * a type XFRM_MSG_UPDPOLICY - JHS */
910 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
911 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
913 security_xfrm_policy_free(xp
);
918 c
.event
= nlh
->nlmsg_type
;
919 c
.seq
= nlh
->nlmsg_seq
;
920 c
.pid
= nlh
->nlmsg_pid
;
921 km_policy_notify(xp
, p
->dir
, &c
);
928 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
930 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
933 if (xp
->xfrm_nr
== 0)
936 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
937 struct xfrm_user_tmpl
*up
= &vec
[i
];
938 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
940 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
941 up
->family
= xp
->family
;
942 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
943 up
->reqid
= kp
->reqid
;
945 up
->share
= kp
->share
;
946 up
->optional
= kp
->optional
;
947 up
->aalgos
= kp
->aalgos
;
948 up
->ealgos
= kp
->ealgos
;
949 up
->calgos
= kp
->calgos
;
951 RTA_PUT(skb
, XFRMA_TMPL
,
952 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
961 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
963 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
964 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
965 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
967 uctx
->exttype
= XFRMA_SEC_CTX
;
968 uctx
->len
= ctx_size
;
969 uctx
->ctx_doi
= s
->ctx_doi
;
970 uctx
->ctx_alg
= s
->ctx_alg
;
971 uctx
->ctx_len
= s
->ctx_len
;
972 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
979 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
982 return copy_sec_ctx(x
->security
, skb
);
987 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
990 return copy_sec_ctx(xp
->security
, skb
);
995 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
997 struct xfrm_dump_info
*sp
= ptr
;
998 struct xfrm_userpolicy_info
*p
;
999 struct sk_buff
*in_skb
= sp
->in_skb
;
1000 struct sk_buff
*skb
= sp
->out_skb
;
1001 struct nlmsghdr
*nlh
;
1002 unsigned char *b
= skb
->tail
;
1004 if (sp
->this_idx
< sp
->start_idx
)
1007 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
1009 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
1010 p
= NLMSG_DATA(nlh
);
1011 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
1013 copy_to_user_policy(xp
, p
, dir
);
1014 if (copy_to_user_tmpl(xp
, skb
) < 0)
1016 if (copy_to_user_sec_ctx(xp
, skb
))
1019 nlh
->nlmsg_len
= skb
->tail
- b
;
1025 skb_trim(skb
, b
- skb
->data
);
1029 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1031 struct xfrm_dump_info info
;
1033 info
.in_skb
= cb
->skb
;
1035 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1036 info
.nlmsg_flags
= NLM_F_MULTI
;
1038 info
.start_idx
= cb
->args
[0];
1039 (void) xfrm_policy_walk(dump_one_policy
, &info
);
1040 cb
->args
[0] = info
.this_idx
;
1045 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1046 struct xfrm_policy
*xp
,
1049 struct xfrm_dump_info info
;
1050 struct sk_buff
*skb
;
1052 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1054 return ERR_PTR(-ENOMEM
);
1056 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
1057 info
.in_skb
= in_skb
;
1059 info
.nlmsg_seq
= seq
;
1060 info
.nlmsg_flags
= 0;
1061 info
.this_idx
= info
.start_idx
= 0;
1063 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1071 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1073 struct xfrm_policy
*xp
;
1074 struct xfrm_userpolicy_id
*p
;
1079 p
= NLMSG_DATA(nlh
);
1080 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1082 err
= verify_policy_dir(p
->dir
);
1087 xp
= xfrm_policy_byid(p
->dir
, p
->index
, delete);
1089 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1090 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1091 struct xfrm_policy tmp
;
1093 err
= verify_sec_ctx_len(rtattrs
);
1097 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1099 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1101 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1104 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, delete);
1105 security_xfrm_policy_free(&tmp
);
1111 struct sk_buff
*resp_skb
;
1113 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1114 if (IS_ERR(resp_skb
)) {
1115 err
= PTR_ERR(resp_skb
);
1117 err
= netlink_unicast(xfrm_nl
, resp_skb
,
1118 NETLINK_CB(skb
).pid
,
1122 if ((err
= security_xfrm_policy_delete(xp
)) != 0)
1124 c
.data
.byid
= p
->index
;
1125 c
.event
= nlh
->nlmsg_type
;
1126 c
.seq
= nlh
->nlmsg_seq
;
1127 c
.pid
= nlh
->nlmsg_pid
;
1128 km_policy_notify(xp
, p
->dir
, &c
);
1137 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1140 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
1142 xfrm_state_flush(p
->proto
);
1143 c
.data
.proto
= p
->proto
;
1144 c
.event
= nlh
->nlmsg_type
;
1145 c
.seq
= nlh
->nlmsg_seq
;
1146 c
.pid
= nlh
->nlmsg_pid
;
1147 km_state_notify(NULL
, &c
);
1153 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1155 struct xfrm_aevent_id
*id
;
1156 struct nlmsghdr
*nlh
;
1157 struct xfrm_lifetime_cur ltime
;
1158 unsigned char *b
= skb
->tail
;
1160 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
));
1161 id
= NLMSG_DATA(nlh
);
1162 nlh
->nlmsg_flags
= 0;
1164 id
->sa_id
.daddr
= x
->id
.daddr
;
1165 id
->sa_id
.spi
= x
->id
.spi
;
1166 id
->sa_id
.family
= x
->props
.family
;
1167 id
->sa_id
.proto
= x
->id
.proto
;
1168 id
->flags
= c
->data
.aevent
;
1170 RTA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1172 ltime
.bytes
= x
->curlft
.bytes
;
1173 ltime
.packets
= x
->curlft
.packets
;
1174 ltime
.add_time
= x
->curlft
.add_time
;
1175 ltime
.use_time
= x
->curlft
.use_time
;
1177 RTA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(struct xfrm_lifetime_cur
), <ime
);
1179 if (id
->flags
&XFRM_AE_RTHR
) {
1180 RTA_PUT(skb
,XFRMA_REPLAY_THRESH
,sizeof(u32
),&x
->replay_maxdiff
);
1183 if (id
->flags
&XFRM_AE_ETHR
) {
1184 u32 etimer
= x
->replay_maxage
*10/HZ
;
1185 RTA_PUT(skb
,XFRMA_ETIMER_THRESH
,sizeof(u32
),&etimer
);
1188 nlh
->nlmsg_len
= skb
->tail
- b
;
1193 skb_trim(skb
, b
- skb
->data
);
1197 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1199 struct xfrm_state
*x
;
1200 struct sk_buff
*r_skb
;
1203 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1204 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1205 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1207 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1208 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1210 if (p
->flags
&XFRM_AE_RTHR
)
1211 len
+=RTA_SPACE(sizeof(u32
));
1213 if (p
->flags
&XFRM_AE_ETHR
)
1214 len
+=RTA_SPACE(sizeof(u32
));
1216 r_skb
= alloc_skb(len
, GFP_ATOMIC
);
1220 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1227 * XXX: is this lock really needed - none of the other
1228 * gets lock (the concern is things getting updated
1229 * while we are still reading) - jhs
1231 spin_lock_bh(&x
->lock
);
1232 c
.data
.aevent
= p
->flags
;
1233 c
.seq
= nlh
->nlmsg_seq
;
1234 c
.pid
= nlh
->nlmsg_pid
;
1236 if (build_aevent(r_skb
, x
, &c
) < 0)
1238 err
= netlink_unicast(xfrm_nl
, r_skb
,
1239 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1240 spin_unlock_bh(&x
->lock
);
1245 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1247 struct xfrm_state
*x
;
1250 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1251 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1252 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1257 /* pedantic mode - thou shalt sayeth replaceth */
1258 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1261 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1265 if (x
->km
.state
!= XFRM_STATE_VALID
)
1268 spin_lock_bh(&x
->lock
);
1269 err
= xfrm_update_ae_params(x
,(struct rtattr
**)xfrma
);
1270 spin_unlock_bh(&x
->lock
);
1274 c
.event
= nlh
->nlmsg_type
;
1275 c
.seq
= nlh
->nlmsg_seq
;
1276 c
.pid
= nlh
->nlmsg_pid
;
1277 c
.data
.aevent
= XFRM_AE_CU
;
1278 km_state_notify(x
, &c
);
1285 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1289 xfrm_policy_flush();
1290 c
.event
= nlh
->nlmsg_type
;
1291 c
.seq
= nlh
->nlmsg_seq
;
1292 c
.pid
= nlh
->nlmsg_pid
;
1293 km_policy_notify(NULL
, 0, &c
);
1297 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1299 struct xfrm_policy
*xp
;
1300 struct xfrm_user_polexpire
*up
= NLMSG_DATA(nlh
);
1301 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1305 xp
= xfrm_policy_byid(p
->dir
, p
->index
, 0);
1307 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1308 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1309 struct xfrm_policy tmp
;
1311 err
= verify_sec_ctx_len(rtattrs
);
1315 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1317 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1319 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1322 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, 0);
1323 security_xfrm_policy_free(&tmp
);
1328 read_lock(&xp
->lock
);
1330 read_unlock(&xp
->lock
);
1334 read_unlock(&xp
->lock
);
1337 xfrm_policy_delete(xp
, p
->dir
);
1339 // reset the timers here?
1340 printk("Dont know what to do with soft policy expire\n");
1342 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1349 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1351 struct xfrm_state
*x
;
1353 struct xfrm_user_expire
*ue
= NLMSG_DATA(nlh
);
1354 struct xfrm_usersa_info
*p
= &ue
->state
;
1356 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1364 spin_lock_bh(&x
->lock
);
1365 if (x
->km
.state
!= XFRM_STATE_VALID
)
1367 km_state_expired(x
, ue
->hard
, current
->pid
);
1370 __xfrm_state_delete(x
);
1372 spin_unlock_bh(&x
->lock
);
1377 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1379 struct xfrm_policy
*xp
;
1380 struct xfrm_user_tmpl
*ut
;
1382 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1384 struct xfrm_user_acquire
*ua
= NLMSG_DATA(nlh
);
1385 struct xfrm_state
*x
= xfrm_state_alloc();
1391 err
= verify_newpolicy_info(&ua
->policy
);
1393 printk("BAD policy passed\n");
1399 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
); if (!xp
) {
1404 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1405 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1406 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1409 /* extract the templates and for each call km_key */
1410 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1411 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1412 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1413 x
->props
.mode
= t
->mode
;
1414 x
->props
.reqid
= t
->reqid
;
1415 x
->props
.family
= ut
->family
;
1416 t
->aalgos
= ua
->aalgos
;
1417 t
->ealgos
= ua
->ealgos
;
1418 t
->calgos
= ua
->calgos
;
1419 err
= km_query(x
, t
, xp
);
1430 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1432 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1433 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1434 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1435 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1436 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1437 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1438 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1439 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1440 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1441 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1442 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1443 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1444 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1445 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1446 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
1447 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1448 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1453 static struct xfrm_link
{
1454 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
1455 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1456 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1457 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1458 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1459 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1460 .dump
= xfrm_dump_sa
},
1461 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1462 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1463 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1464 .dump
= xfrm_dump_policy
},
1465 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1466 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1467 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1468 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1469 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1470 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1471 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1472 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1473 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1474 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1477 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
1479 struct rtattr
*xfrma
[XFRMA_MAX
];
1480 struct xfrm_link
*link
;
1483 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
1486 type
= nlh
->nlmsg_type
;
1488 /* A control message: ignore them */
1489 if (type
< XFRM_MSG_BASE
)
1492 /* Unknown message: reply with EINVAL */
1493 if (type
> XFRM_MSG_MAX
)
1496 type
-= XFRM_MSG_BASE
;
1497 link
= &xfrm_dispatch
[type
];
1499 /* All operations require privileges, even GET */
1500 if (security_netlink_recv(skb
, CAP_NET_ADMIN
)) {
1505 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1506 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1507 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1508 if (link
->dump
== NULL
)
1511 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
1512 link
->dump
, NULL
)) != 0) {
1516 netlink_queue_skip(nlh
, skb
);
1520 memset(xfrma
, 0, sizeof(xfrma
));
1522 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1525 if (nlh
->nlmsg_len
> min_len
) {
1526 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1527 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1529 while (RTA_OK(attr
, attrlen
)) {
1530 unsigned short flavor
= attr
->rta_type
;
1532 if (flavor
> XFRMA_MAX
)
1534 xfrma
[flavor
- 1] = attr
;
1536 attr
= RTA_NEXT(attr
, attrlen
);
1540 if (link
->doit
== NULL
)
1542 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
1551 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1553 unsigned int qlen
= 0;
1556 mutex_lock(&xfrm_cfg_mutex
);
1557 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1558 mutex_unlock(&xfrm_cfg_mutex
);
1563 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1565 struct xfrm_user_expire
*ue
;
1566 struct nlmsghdr
*nlh
;
1567 unsigned char *b
= skb
->tail
;
1569 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
,
1571 ue
= NLMSG_DATA(nlh
);
1572 nlh
->nlmsg_flags
= 0;
1574 copy_to_user_state(x
, &ue
->state
);
1575 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1577 nlh
->nlmsg_len
= skb
->tail
- b
;
1581 skb_trim(skb
, b
- skb
->data
);
1585 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1587 struct sk_buff
*skb
;
1588 int len
= NLMSG_LENGTH(sizeof(struct xfrm_user_expire
));
1590 skb
= alloc_skb(len
, GFP_ATOMIC
);
1594 if (build_expire(skb
, x
, c
) < 0)
1597 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1598 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1601 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1603 struct sk_buff
*skb
;
1604 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1606 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1607 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1608 skb
= alloc_skb(len
, GFP_ATOMIC
);
1612 if (build_aevent(skb
, x
, c
) < 0)
1615 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_AEVENTS
;
1616 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1619 static int xfrm_notify_sa_flush(struct km_event
*c
)
1621 struct xfrm_usersa_flush
*p
;
1622 struct nlmsghdr
*nlh
;
1623 struct sk_buff
*skb
;
1625 int len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1627 skb
= alloc_skb(len
, GFP_ATOMIC
);
1632 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
,
1633 XFRM_MSG_FLUSHSA
, sizeof(*p
));
1634 nlh
->nlmsg_flags
= 0;
1636 p
= NLMSG_DATA(nlh
);
1637 p
->proto
= c
->data
.proto
;
1639 nlh
->nlmsg_len
= skb
->tail
- b
;
1641 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1642 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1649 static int inline xfrm_sa_len(struct xfrm_state
*x
)
1653 l
+= RTA_SPACE(sizeof(*x
->aalg
) + (x
->aalg
->alg_key_len
+7)/8);
1655 l
+= RTA_SPACE(sizeof(*x
->ealg
) + (x
->ealg
->alg_key_len
+7)/8);
1657 l
+= RTA_SPACE(sizeof(*x
->calg
));
1659 l
+= RTA_SPACE(sizeof(*x
->encap
));
1664 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1666 struct xfrm_usersa_info
*p
;
1667 struct xfrm_usersa_id
*id
;
1668 struct nlmsghdr
*nlh
;
1669 struct sk_buff
*skb
;
1671 int len
= xfrm_sa_len(x
);
1674 headlen
= sizeof(*p
);
1675 if (c
->event
== XFRM_MSG_DELSA
) {
1676 len
+= RTA_SPACE(headlen
);
1677 headlen
= sizeof(*id
);
1679 len
+= NLMSG_SPACE(headlen
);
1681 skb
= alloc_skb(len
, GFP_ATOMIC
);
1686 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1687 nlh
->nlmsg_flags
= 0;
1689 p
= NLMSG_DATA(nlh
);
1690 if (c
->event
== XFRM_MSG_DELSA
) {
1691 id
= NLMSG_DATA(nlh
);
1692 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
1693 id
->spi
= x
->id
.spi
;
1694 id
->family
= x
->props
.family
;
1695 id
->proto
= x
->id
.proto
;
1697 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_SA
, sizeof(*p
)));
1700 copy_to_user_state(x
, p
);
1703 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
1704 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
1706 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
1707 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
1709 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
1712 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
1714 nlh
->nlmsg_len
= skb
->tail
- b
;
1716 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1717 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1725 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1729 case XFRM_MSG_EXPIRE
:
1730 return xfrm_exp_state_notify(x
, c
);
1731 case XFRM_MSG_NEWAE
:
1732 return xfrm_aevent_state_notify(x
, c
);
1733 case XFRM_MSG_DELSA
:
1734 case XFRM_MSG_UPDSA
:
1735 case XFRM_MSG_NEWSA
:
1736 return xfrm_notify_sa(x
, c
);
1737 case XFRM_MSG_FLUSHSA
:
1738 return xfrm_notify_sa_flush(c
);
1740 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
1748 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1749 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1752 struct xfrm_user_acquire
*ua
;
1753 struct nlmsghdr
*nlh
;
1754 unsigned char *b
= skb
->tail
;
1755 __u32 seq
= xfrm_get_acqseq();
1757 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1759 ua
= NLMSG_DATA(nlh
);
1760 nlh
->nlmsg_flags
= 0;
1762 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1763 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1764 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1765 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1766 ua
->aalgos
= xt
->aalgos
;
1767 ua
->ealgos
= xt
->ealgos
;
1768 ua
->calgos
= xt
->calgos
;
1769 ua
->seq
= x
->km
.seq
= seq
;
1771 if (copy_to_user_tmpl(xp
, skb
) < 0)
1773 if (copy_to_user_state_sec_ctx(x
, skb
))
1776 nlh
->nlmsg_len
= skb
->tail
- b
;
1780 skb_trim(skb
, b
- skb
->data
);
1784 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1785 struct xfrm_policy
*xp
, int dir
)
1787 struct sk_buff
*skb
;
1790 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1791 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1792 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1793 skb
= alloc_skb(len
, GFP_ATOMIC
);
1797 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1800 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_ACQUIRE
;
1801 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
1804 /* User gives us xfrm_user_policy_info followed by an array of 0
1805 * or more templates.
1807 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
1808 u8
*data
, int len
, int *dir
)
1810 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1811 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1812 struct xfrm_policy
*xp
;
1815 switch (sk
->sk_family
) {
1817 if (opt
!= IP_XFRM_POLICY
) {
1822 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1824 if (opt
!= IPV6_XFRM_POLICY
) {
1837 if (len
< sizeof(*p
) ||
1838 verify_newpolicy_info(p
))
1841 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1842 if (nr
> XFRM_MAX_DEPTH
)
1845 if (p
->dir
> XFRM_POLICY_OUT
)
1848 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1854 copy_from_user_policy(xp
, p
);
1855 copy_templates(xp
, ut
, nr
);
1857 if (!xp
->security
) {
1858 int err
= security_xfrm_sock_policy_alloc(xp
, sk
);
1871 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
1872 int dir
, struct km_event
*c
)
1874 struct xfrm_user_polexpire
*upe
;
1875 struct nlmsghdr
*nlh
;
1876 int hard
= c
->data
.hard
;
1877 unsigned char *b
= skb
->tail
;
1879 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
1880 upe
= NLMSG_DATA(nlh
);
1881 nlh
->nlmsg_flags
= 0;
1883 copy_to_user_policy(xp
, &upe
->pol
, dir
);
1884 if (copy_to_user_tmpl(xp
, skb
) < 0)
1886 if (copy_to_user_sec_ctx(xp
, skb
))
1890 nlh
->nlmsg_len
= skb
->tail
- b
;
1894 skb_trim(skb
, b
- skb
->data
);
1898 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1900 struct sk_buff
*skb
;
1903 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1904 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
1905 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1906 skb
= alloc_skb(len
, GFP_ATOMIC
);
1910 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
1913 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1914 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1917 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1919 struct xfrm_userpolicy_info
*p
;
1920 struct xfrm_userpolicy_id
*id
;
1921 struct nlmsghdr
*nlh
;
1922 struct sk_buff
*skb
;
1924 int len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1927 headlen
= sizeof(*p
);
1928 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1929 len
+= RTA_SPACE(headlen
);
1930 headlen
= sizeof(*id
);
1932 len
+= NLMSG_SPACE(headlen
);
1934 skb
= alloc_skb(len
, GFP_ATOMIC
);
1939 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1941 p
= NLMSG_DATA(nlh
);
1942 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1943 id
= NLMSG_DATA(nlh
);
1944 memset(id
, 0, sizeof(*id
));
1947 id
->index
= xp
->index
;
1949 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
1951 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_POLICY
, sizeof(*p
)));
1954 nlh
->nlmsg_flags
= 0;
1956 copy_to_user_policy(xp
, p
, dir
);
1957 if (copy_to_user_tmpl(xp
, skb
) < 0)
1960 nlh
->nlmsg_len
= skb
->tail
- b
;
1962 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1963 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1971 static int xfrm_notify_policy_flush(struct km_event
*c
)
1973 struct nlmsghdr
*nlh
;
1974 struct sk_buff
*skb
;
1976 int len
= NLMSG_LENGTH(0);
1978 skb
= alloc_skb(len
, GFP_ATOMIC
);
1984 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0);
1986 nlh
->nlmsg_len
= skb
->tail
- b
;
1988 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1989 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1996 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2000 case XFRM_MSG_NEWPOLICY
:
2001 case XFRM_MSG_UPDPOLICY
:
2002 case XFRM_MSG_DELPOLICY
:
2003 return xfrm_notify_policy(xp
, dir
, c
);
2004 case XFRM_MSG_FLUSHPOLICY
:
2005 return xfrm_notify_policy_flush(c
);
2006 case XFRM_MSG_POLEXPIRE
:
2007 return xfrm_exp_policy_notify(xp
, dir
, c
);
2009 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2016 static struct xfrm_mgr netlink_mgr
= {
2018 .notify
= xfrm_send_state_notify
,
2019 .acquire
= xfrm_send_acquire
,
2020 .compile_policy
= xfrm_compile_policy
,
2021 .notify_policy
= xfrm_send_policy_notify
,
2024 static int __init
xfrm_user_init(void)
2028 printk(KERN_INFO
"Initializing IPsec netlink socket\n");
2030 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2031 xfrm_netlink_rcv
, THIS_MODULE
);
2034 rcu_assign_pointer(xfrm_nl
, nlsk
);
2036 xfrm_register_km(&netlink_mgr
);
2041 static void __exit
xfrm_user_exit(void)
2043 struct sock
*nlsk
= xfrm_nl
;
2045 xfrm_unregister_km(&netlink_mgr
);
2046 rcu_assign_pointer(xfrm_nl
, NULL
);
2048 sock_release(nlsk
->sk_socket
);
2051 module_init(xfrm_user_init
);
2052 module_exit(xfrm_user_exit
);
2053 MODULE_LICENSE("GPL");
2054 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);