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
))
91 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
93 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
94 struct xfrm_user_sec_ctx
*uctx
;
100 if (rt
->rta_len
< sizeof(*uctx
))
105 len
+= sizeof(struct xfrm_user_sec_ctx
);
106 len
+= uctx
->ctx_len
;
108 if (uctx
->len
!= len
)
115 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
116 struct rtattr
**xfrma
)
126 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
138 switch (p
->id
.proto
) {
140 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
141 xfrma
[XFRMA_ALG_CRYPT
-1] ||
142 xfrma
[XFRMA_ALG_COMP
-1])
147 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
148 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
149 xfrma
[XFRMA_ALG_COMP
-1])
154 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
155 xfrma
[XFRMA_ALG_AUTH
-1] ||
156 xfrma
[XFRMA_ALG_CRYPT
-1])
164 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
166 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
168 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
170 if ((err
= verify_encap_tmpl(xfrma
)))
172 if ((err
= verify_sec_ctx_len(xfrma
)))
191 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
192 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
193 struct rtattr
*u_arg
)
195 struct rtattr
*rta
= u_arg
;
196 struct xfrm_algo
*p
, *ualg
;
197 struct xfrm_algo_desc
*algo
;
203 ualg
= RTA_DATA(rta
);
205 algo
= get_byname(ualg
->alg_name
, 1);
208 *props
= algo
->desc
.sadb_alg_id
;
210 len
= sizeof(*ualg
) + (ualg
->alg_key_len
+ 7U) / 8;
211 p
= kmalloc(len
, GFP_KERNEL
);
215 memcpy(p
, ualg
, len
);
216 strcpy(p
->alg_name
, algo
->name
);
221 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
223 struct rtattr
*rta
= u_arg
;
224 struct xfrm_encap_tmpl
*p
, *uencap
;
229 uencap
= RTA_DATA(rta
);
230 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
234 memcpy(p
, uencap
, sizeof(*p
));
240 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy
*xp
)
242 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
246 len
+= sizeof(struct xfrm_user_sec_ctx
);
247 len
+= xfrm_ctx
->ctx_len
;
252 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
254 struct xfrm_user_sec_ctx
*uctx
;
259 uctx
= RTA_DATA(u_arg
);
260 return security_xfrm_state_alloc(x
, uctx
);
263 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
265 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
266 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
267 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
268 x
->props
.mode
= p
->mode
;
269 x
->props
.replay_window
= p
->replay_window
;
270 x
->props
.reqid
= p
->reqid
;
271 x
->props
.family
= p
->family
;
272 x
->props
.saddr
= p
->saddr
;
273 x
->props
.flags
= p
->flags
;
277 * someday when pfkey also has support, we could have the code
278 * somehow made shareable and move it to xfrm_state.c - JHS
281 static int xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
284 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
285 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
286 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
287 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
290 struct xfrm_replay_state
*replay
;
291 if (RTA_PAYLOAD(rp
) < sizeof(*replay
))
293 replay
= RTA_DATA(rp
);
294 memcpy(&x
->replay
, replay
, sizeof(*replay
));
295 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
299 struct xfrm_lifetime_cur
*ltime
;
300 if (RTA_PAYLOAD(lt
) < sizeof(*ltime
))
302 ltime
= RTA_DATA(lt
);
303 x
->curlft
.bytes
= ltime
->bytes
;
304 x
->curlft
.packets
= ltime
->packets
;
305 x
->curlft
.add_time
= ltime
->add_time
;
306 x
->curlft
.use_time
= ltime
->use_time
;
310 if (RTA_PAYLOAD(et
) < sizeof(u32
))
312 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
316 if (RTA_PAYLOAD(rt
) < sizeof(u32
))
318 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
326 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
327 struct rtattr
**xfrma
,
330 struct xfrm_state
*x
= xfrm_state_alloc();
336 copy_from_user_state(x
, p
);
338 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
339 xfrm_aalg_get_byname
,
340 xfrma
[XFRMA_ALG_AUTH
-1])))
342 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
343 xfrm_ealg_get_byname
,
344 xfrma
[XFRMA_ALG_CRYPT
-1])))
346 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
347 xfrm_calg_get_byname
,
348 xfrma
[XFRMA_ALG_COMP
-1])))
350 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
353 err
= xfrm_init_state(x
);
357 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
361 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
362 /* sysctl_xfrm_aevent_etime is in 100ms units */
363 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
364 x
->preplay
.bitmap
= 0;
365 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
366 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
368 /* override default values from above */
370 err
= xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
377 x
->km
.state
= XFRM_STATE_DEAD
;
384 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
386 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
387 struct xfrm_state
*x
;
391 err
= verify_newsa_info(p
, (struct rtattr
**)xfrma
);
395 x
= xfrm_state_construct(p
, (struct rtattr
**)xfrma
, &err
);
400 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
401 err
= xfrm_state_add(x
);
403 err
= xfrm_state_update(x
);
406 x
->km
.state
= XFRM_STATE_DEAD
;
411 c
.seq
= nlh
->nlmsg_seq
;
412 c
.pid
= nlh
->nlmsg_pid
;
413 c
.event
= nlh
->nlmsg_type
;
415 km_state_notify(x
, &c
);
421 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
423 struct xfrm_state
*x
;
426 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
428 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
432 if ((err
= security_xfrm_state_delete(x
)) != 0)
435 if (xfrm_state_kern(x
)) {
440 err
= xfrm_state_delete(x
);
444 c
.seq
= nlh
->nlmsg_seq
;
445 c
.pid
= nlh
->nlmsg_pid
;
446 c
.event
= nlh
->nlmsg_type
;
447 km_state_notify(x
, &c
);
454 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
456 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
457 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
458 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
459 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
460 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
461 p
->saddr
= x
->props
.saddr
;
462 p
->mode
= x
->props
.mode
;
463 p
->replay_window
= x
->props
.replay_window
;
464 p
->reqid
= x
->props
.reqid
;
465 p
->family
= x
->props
.family
;
466 p
->flags
= x
->props
.flags
;
470 struct xfrm_dump_info
{
471 struct sk_buff
*in_skb
;
472 struct sk_buff
*out_skb
;
479 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
481 struct xfrm_dump_info
*sp
= ptr
;
482 struct sk_buff
*in_skb
= sp
->in_skb
;
483 struct sk_buff
*skb
= sp
->out_skb
;
484 struct xfrm_usersa_info
*p
;
485 struct nlmsghdr
*nlh
;
486 unsigned char *b
= skb
->tail
;
488 if (sp
->this_idx
< sp
->start_idx
)
491 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
493 XFRM_MSG_NEWSA
, sizeof(*p
));
494 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
497 copy_to_user_state(x
, p
);
500 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
501 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
503 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
504 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
506 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
509 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
512 int ctx_size
= sizeof(struct xfrm_sec_ctx
) +
513 x
->security
->ctx_len
;
514 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
515 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
517 uctx
->exttype
= XFRMA_SEC_CTX
;
518 uctx
->len
= ctx_size
;
519 uctx
->ctx_doi
= x
->security
->ctx_doi
;
520 uctx
->ctx_alg
= x
->security
->ctx_alg
;
521 uctx
->ctx_len
= x
->security
->ctx_len
;
522 memcpy(uctx
+ 1, x
->security
->ctx_str
, x
->security
->ctx_len
);
524 nlh
->nlmsg_len
= skb
->tail
- b
;
531 skb_trim(skb
, b
- skb
->data
);
535 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
537 struct xfrm_dump_info info
;
539 info
.in_skb
= cb
->skb
;
541 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
542 info
.nlmsg_flags
= NLM_F_MULTI
;
544 info
.start_idx
= cb
->args
[0];
545 (void) xfrm_state_walk(IPSEC_PROTO_ANY
, dump_one_state
, &info
);
546 cb
->args
[0] = info
.this_idx
;
551 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
552 struct xfrm_state
*x
, u32 seq
)
554 struct xfrm_dump_info info
;
557 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
559 return ERR_PTR(-ENOMEM
);
561 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
562 info
.in_skb
= in_skb
;
564 info
.nlmsg_seq
= seq
;
565 info
.nlmsg_flags
= 0;
566 info
.this_idx
= info
.start_idx
= 0;
568 if (dump_one_state(x
, 0, &info
)) {
576 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
578 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
579 struct xfrm_state
*x
;
580 struct sk_buff
*resp_skb
;
583 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
588 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
589 if (IS_ERR(resp_skb
)) {
590 err
= PTR_ERR(resp_skb
);
592 err
= netlink_unicast(xfrm_nl
, resp_skb
,
593 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
600 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
602 switch (p
->info
.id
.proto
) {
608 /* IPCOMP spi is 16-bits. */
609 if (p
->max
>= 0x10000)
623 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
625 struct xfrm_state
*x
;
626 struct xfrm_userspi_info
*p
;
627 struct sk_buff
*resp_skb
;
628 xfrm_address_t
*daddr
;
633 err
= verify_userspi_info(p
);
637 family
= p
->info
.family
;
638 daddr
= &p
->info
.id
.daddr
;
642 x
= xfrm_find_acq_byseq(p
->info
.seq
);
643 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
650 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
651 p
->info
.id
.proto
, daddr
,
658 resp_skb
= ERR_PTR(-ENOENT
);
660 spin_lock_bh(&x
->lock
);
661 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
662 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
664 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
666 spin_unlock_bh(&x
->lock
);
668 if (IS_ERR(resp_skb
)) {
669 err
= PTR_ERR(resp_skb
);
673 err
= netlink_unicast(xfrm_nl
, resp_skb
,
674 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
682 static int verify_policy_dir(__u8 dir
)
686 case XFRM_POLICY_OUT
:
687 case XFRM_POLICY_FWD
:
697 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
701 case XFRM_SHARE_SESSION
:
702 case XFRM_SHARE_USER
:
703 case XFRM_SHARE_UNIQUE
:
711 case XFRM_POLICY_ALLOW
:
712 case XFRM_POLICY_BLOCK
:
719 switch (p
->sel
.family
) {
724 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
727 return -EAFNOSUPPORT
;
734 return verify_policy_dir(p
->dir
);
737 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
739 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
740 struct xfrm_user_sec_ctx
*uctx
;
746 return security_xfrm_policy_alloc(pol
, uctx
);
749 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
755 for (i
= 0; i
< nr
; i
++, ut
++) {
756 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
758 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
759 memcpy(&t
->saddr
, &ut
->saddr
,
760 sizeof(xfrm_address_t
));
761 t
->reqid
= ut
->reqid
;
763 t
->share
= ut
->share
;
764 t
->optional
= ut
->optional
;
765 t
->aalgos
= ut
->aalgos
;
766 t
->ealgos
= ut
->ealgos
;
767 t
->calgos
= ut
->calgos
;
771 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
773 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
774 struct xfrm_user_tmpl
*utmpl
;
780 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
782 if (nr
> XFRM_MAX_DEPTH
)
785 copy_templates(pol
, RTA_DATA(rt
), nr
);
790 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
792 xp
->priority
= p
->priority
;
793 xp
->index
= p
->index
;
794 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
795 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
796 xp
->action
= p
->action
;
797 xp
->flags
= p
->flags
;
798 xp
->family
= p
->sel
.family
;
799 /* XXX xp->share = p->share; */
802 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
804 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
805 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
806 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
807 p
->priority
= xp
->priority
;
808 p
->index
= xp
->index
;
809 p
->sel
.family
= xp
->family
;
811 p
->action
= xp
->action
;
812 p
->flags
= xp
->flags
;
813 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
816 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
818 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
826 copy_from_user_policy(xp
, p
);
828 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
829 err
= copy_from_user_sec_ctx(xp
, xfrma
);
840 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
842 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
843 struct xfrm_policy
*xp
;
848 err
= verify_newpolicy_info(p
);
851 err
= verify_sec_ctx_len((struct rtattr
**)xfrma
);
855 xp
= xfrm_policy_construct(p
, (struct rtattr
**)xfrma
, &err
);
859 /* shouldnt excl be based on nlh flags??
860 * Aha! this is anti-netlink really i.e more pfkey derived
861 * in netlink excl is a flag and you wouldnt need
862 * a type XFRM_MSG_UPDPOLICY - JHS */
863 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
864 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
866 security_xfrm_policy_free(xp
);
871 c
.event
= nlh
->nlmsg_type
;
872 c
.seq
= nlh
->nlmsg_seq
;
873 c
.pid
= nlh
->nlmsg_pid
;
874 km_policy_notify(xp
, p
->dir
, &c
);
881 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
883 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
886 if (xp
->xfrm_nr
== 0)
889 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
890 struct xfrm_user_tmpl
*up
= &vec
[i
];
891 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
893 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
894 up
->family
= xp
->family
;
895 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
896 up
->reqid
= kp
->reqid
;
898 up
->share
= kp
->share
;
899 up
->optional
= kp
->optional
;
900 up
->aalgos
= kp
->aalgos
;
901 up
->ealgos
= kp
->ealgos
;
902 up
->calgos
= kp
->calgos
;
904 RTA_PUT(skb
, XFRMA_TMPL
,
905 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
914 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
916 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
917 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
918 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
920 uctx
->exttype
= XFRMA_SEC_CTX
;
921 uctx
->len
= ctx_size
;
922 uctx
->ctx_doi
= s
->ctx_doi
;
923 uctx
->ctx_alg
= s
->ctx_alg
;
924 uctx
->ctx_len
= s
->ctx_len
;
925 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
932 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
935 return copy_sec_ctx(x
->security
, skb
);
940 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
943 return copy_sec_ctx(xp
->security
, skb
);
948 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
950 struct xfrm_dump_info
*sp
= ptr
;
951 struct xfrm_userpolicy_info
*p
;
952 struct sk_buff
*in_skb
= sp
->in_skb
;
953 struct sk_buff
*skb
= sp
->out_skb
;
954 struct nlmsghdr
*nlh
;
955 unsigned char *b
= skb
->tail
;
957 if (sp
->this_idx
< sp
->start_idx
)
960 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
962 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
964 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
966 copy_to_user_policy(xp
, p
, dir
);
967 if (copy_to_user_tmpl(xp
, skb
) < 0)
969 if (copy_to_user_sec_ctx(xp
, skb
))
972 nlh
->nlmsg_len
= skb
->tail
- b
;
978 skb_trim(skb
, b
- skb
->data
);
982 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
984 struct xfrm_dump_info info
;
986 info
.in_skb
= cb
->skb
;
988 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
989 info
.nlmsg_flags
= NLM_F_MULTI
;
991 info
.start_idx
= cb
->args
[0];
992 (void) xfrm_policy_walk(dump_one_policy
, &info
);
993 cb
->args
[0] = info
.this_idx
;
998 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
999 struct xfrm_policy
*xp
,
1002 struct xfrm_dump_info info
;
1003 struct sk_buff
*skb
;
1005 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1007 return ERR_PTR(-ENOMEM
);
1009 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
1010 info
.in_skb
= in_skb
;
1012 info
.nlmsg_seq
= seq
;
1013 info
.nlmsg_flags
= 0;
1014 info
.this_idx
= info
.start_idx
= 0;
1016 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1024 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1026 struct xfrm_policy
*xp
;
1027 struct xfrm_userpolicy_id
*p
;
1032 p
= NLMSG_DATA(nlh
);
1033 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1035 err
= verify_policy_dir(p
->dir
);
1040 xp
= xfrm_policy_byid(p
->dir
, p
->index
, delete);
1042 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1043 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1044 struct xfrm_policy tmp
;
1046 err
= verify_sec_ctx_len(rtattrs
);
1050 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1052 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1054 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1057 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, delete);
1058 security_xfrm_policy_free(&tmp
);
1064 struct sk_buff
*resp_skb
;
1066 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1067 if (IS_ERR(resp_skb
)) {
1068 err
= PTR_ERR(resp_skb
);
1070 err
= netlink_unicast(xfrm_nl
, resp_skb
,
1071 NETLINK_CB(skb
).pid
,
1075 if ((err
= security_xfrm_policy_delete(xp
)) != 0)
1077 c
.data
.byid
= p
->index
;
1078 c
.event
= nlh
->nlmsg_type
;
1079 c
.seq
= nlh
->nlmsg_seq
;
1080 c
.pid
= nlh
->nlmsg_pid
;
1081 km_policy_notify(xp
, p
->dir
, &c
);
1090 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1093 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
1095 xfrm_state_flush(p
->proto
);
1096 c
.data
.proto
= p
->proto
;
1097 c
.event
= nlh
->nlmsg_type
;
1098 c
.seq
= nlh
->nlmsg_seq
;
1099 c
.pid
= nlh
->nlmsg_pid
;
1100 km_state_notify(NULL
, &c
);
1106 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1108 struct xfrm_aevent_id
*id
;
1109 struct nlmsghdr
*nlh
;
1110 struct xfrm_lifetime_cur ltime
;
1111 unsigned char *b
= skb
->tail
;
1113 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
));
1114 id
= NLMSG_DATA(nlh
);
1115 nlh
->nlmsg_flags
= 0;
1117 id
->sa_id
.daddr
= x
->id
.daddr
;
1118 id
->sa_id
.spi
= x
->id
.spi
;
1119 id
->sa_id
.family
= x
->props
.family
;
1120 id
->sa_id
.proto
= x
->id
.proto
;
1121 id
->flags
= c
->data
.aevent
;
1123 RTA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1125 ltime
.bytes
= x
->curlft
.bytes
;
1126 ltime
.packets
= x
->curlft
.packets
;
1127 ltime
.add_time
= x
->curlft
.add_time
;
1128 ltime
.use_time
= x
->curlft
.use_time
;
1130 RTA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(struct xfrm_lifetime_cur
), <ime
);
1132 if (id
->flags
&XFRM_AE_RTHR
) {
1133 RTA_PUT(skb
,XFRMA_REPLAY_THRESH
,sizeof(u32
),&x
->replay_maxdiff
);
1136 if (id
->flags
&XFRM_AE_ETHR
) {
1137 u32 etimer
= x
->replay_maxage
*10/HZ
;
1138 RTA_PUT(skb
,XFRMA_ETIMER_THRESH
,sizeof(u32
),&etimer
);
1141 nlh
->nlmsg_len
= skb
->tail
- b
;
1146 skb_trim(skb
, b
- skb
->data
);
1150 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1152 struct xfrm_state
*x
;
1153 struct sk_buff
*r_skb
;
1156 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1157 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1158 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1160 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1161 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1163 if (p
->flags
&XFRM_AE_RTHR
)
1164 len
+=RTA_SPACE(sizeof(u32
));
1166 if (p
->flags
&XFRM_AE_ETHR
)
1167 len
+=RTA_SPACE(sizeof(u32
));
1169 r_skb
= alloc_skb(len
, GFP_ATOMIC
);
1173 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1180 * XXX: is this lock really needed - none of the other
1181 * gets lock (the concern is things getting updated
1182 * while we are still reading) - jhs
1184 spin_lock_bh(&x
->lock
);
1185 c
.data
.aevent
= p
->flags
;
1186 c
.seq
= nlh
->nlmsg_seq
;
1187 c
.pid
= nlh
->nlmsg_pid
;
1189 if (build_aevent(r_skb
, x
, &c
) < 0)
1191 err
= netlink_unicast(xfrm_nl
, r_skb
,
1192 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1193 spin_unlock_bh(&x
->lock
);
1198 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1200 struct xfrm_state
*x
;
1203 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1204 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1205 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1210 /* pedantic mode - thou shalt sayeth replaceth */
1211 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1214 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1218 if (x
->km
.state
!= XFRM_STATE_VALID
)
1221 spin_lock_bh(&x
->lock
);
1222 err
= xfrm_update_ae_params(x
,(struct rtattr
**)xfrma
);
1223 spin_unlock_bh(&x
->lock
);
1227 c
.event
= nlh
->nlmsg_type
;
1228 c
.seq
= nlh
->nlmsg_seq
;
1229 c
.pid
= nlh
->nlmsg_pid
;
1230 c
.data
.aevent
= XFRM_AE_CU
;
1231 km_state_notify(x
, &c
);
1238 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1242 xfrm_policy_flush();
1243 c
.event
= nlh
->nlmsg_type
;
1244 c
.seq
= nlh
->nlmsg_seq
;
1245 c
.pid
= nlh
->nlmsg_pid
;
1246 km_policy_notify(NULL
, 0, &c
);
1250 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1252 struct xfrm_policy
*xp
;
1253 struct xfrm_user_polexpire
*up
= NLMSG_DATA(nlh
);
1254 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1258 xp
= xfrm_policy_byid(p
->dir
, p
->index
, 0);
1260 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1261 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1262 struct xfrm_policy tmp
;
1264 err
= verify_sec_ctx_len(rtattrs
);
1268 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1270 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1272 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1275 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, 0);
1276 security_xfrm_policy_free(&tmp
);
1281 read_lock(&xp
->lock
);
1283 read_unlock(&xp
->lock
);
1287 read_unlock(&xp
->lock
);
1290 xfrm_policy_delete(xp
, p
->dir
);
1292 // reset the timers here?
1293 printk("Dont know what to do with soft policy expire\n");
1295 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1302 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1304 struct xfrm_state
*x
;
1306 struct xfrm_user_expire
*ue
= NLMSG_DATA(nlh
);
1307 struct xfrm_usersa_info
*p
= &ue
->state
;
1309 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1317 spin_lock_bh(&x
->lock
);
1318 if (x
->km
.state
!= XFRM_STATE_VALID
)
1320 km_state_expired(x
, ue
->hard
, current
->pid
);
1323 __xfrm_state_delete(x
);
1325 spin_unlock_bh(&x
->lock
);
1330 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1332 struct xfrm_policy
*xp
;
1333 struct xfrm_user_tmpl
*ut
;
1335 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1337 struct xfrm_user_acquire
*ua
= NLMSG_DATA(nlh
);
1338 struct xfrm_state
*x
= xfrm_state_alloc();
1344 err
= verify_newpolicy_info(&ua
->policy
);
1346 printk("BAD policy passed\n");
1352 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
); if (!xp
) {
1357 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1358 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1359 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1362 /* extract the templates and for each call km_key */
1363 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1364 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1365 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1366 x
->props
.mode
= t
->mode
;
1367 x
->props
.reqid
= t
->reqid
;
1368 x
->props
.family
= ut
->family
;
1369 t
->aalgos
= ua
->aalgos
;
1370 t
->ealgos
= ua
->ealgos
;
1371 t
->calgos
= ua
->calgos
;
1372 err
= km_query(x
, t
, xp
);
1383 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1385 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1386 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1387 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1388 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1389 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1390 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1391 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1392 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1393 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1394 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1395 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1396 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1397 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1398 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1399 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
1400 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1401 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1406 static struct xfrm_link
{
1407 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
1408 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1409 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1410 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1411 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1412 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1413 .dump
= xfrm_dump_sa
},
1414 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1415 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1416 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1417 .dump
= xfrm_dump_policy
},
1418 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1419 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1420 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1421 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1422 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1423 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1424 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1425 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1426 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1427 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1430 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
1432 struct rtattr
*xfrma
[XFRMA_MAX
];
1433 struct xfrm_link
*link
;
1436 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
1439 type
= nlh
->nlmsg_type
;
1441 /* A control message: ignore them */
1442 if (type
< XFRM_MSG_BASE
)
1445 /* Unknown message: reply with EINVAL */
1446 if (type
> XFRM_MSG_MAX
)
1449 type
-= XFRM_MSG_BASE
;
1450 link
= &xfrm_dispatch
[type
];
1452 /* All operations require privileges, even GET */
1453 if (security_netlink_recv(skb
, CAP_NET_ADMIN
)) {
1458 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1459 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1460 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1461 if (link
->dump
== NULL
)
1464 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
1465 link
->dump
, NULL
)) != 0) {
1469 netlink_queue_skip(nlh
, skb
);
1473 memset(xfrma
, 0, sizeof(xfrma
));
1475 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1478 if (nlh
->nlmsg_len
> min_len
) {
1479 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1480 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1482 while (RTA_OK(attr
, attrlen
)) {
1483 unsigned short flavor
= attr
->rta_type
;
1485 if (flavor
> XFRMA_MAX
)
1487 xfrma
[flavor
- 1] = attr
;
1489 attr
= RTA_NEXT(attr
, attrlen
);
1493 if (link
->doit
== NULL
)
1495 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
1504 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1506 unsigned int qlen
= 0;
1509 mutex_lock(&xfrm_cfg_mutex
);
1510 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1511 mutex_unlock(&xfrm_cfg_mutex
);
1516 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1518 struct xfrm_user_expire
*ue
;
1519 struct nlmsghdr
*nlh
;
1520 unsigned char *b
= skb
->tail
;
1522 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
,
1524 ue
= NLMSG_DATA(nlh
);
1525 nlh
->nlmsg_flags
= 0;
1527 copy_to_user_state(x
, &ue
->state
);
1528 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1530 nlh
->nlmsg_len
= skb
->tail
- b
;
1534 skb_trim(skb
, b
- skb
->data
);
1538 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1540 struct sk_buff
*skb
;
1541 int len
= NLMSG_LENGTH(sizeof(struct xfrm_user_expire
));
1543 skb
= alloc_skb(len
, GFP_ATOMIC
);
1547 if (build_expire(skb
, x
, c
) < 0)
1550 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1551 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1554 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1556 struct sk_buff
*skb
;
1557 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1559 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1560 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1561 skb
= alloc_skb(len
, GFP_ATOMIC
);
1565 if (build_aevent(skb
, x
, c
) < 0)
1568 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_AEVENTS
;
1569 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1572 static int xfrm_notify_sa_flush(struct km_event
*c
)
1574 struct xfrm_usersa_flush
*p
;
1575 struct nlmsghdr
*nlh
;
1576 struct sk_buff
*skb
;
1578 int len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1580 skb
= alloc_skb(len
, GFP_ATOMIC
);
1585 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
,
1586 XFRM_MSG_FLUSHSA
, sizeof(*p
));
1587 nlh
->nlmsg_flags
= 0;
1589 p
= NLMSG_DATA(nlh
);
1590 p
->proto
= c
->data
.proto
;
1592 nlh
->nlmsg_len
= skb
->tail
- b
;
1594 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1595 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1602 static int inline xfrm_sa_len(struct xfrm_state
*x
)
1606 l
+= RTA_SPACE(sizeof(*x
->aalg
) + (x
->aalg
->alg_key_len
+7)/8);
1608 l
+= RTA_SPACE(sizeof(*x
->ealg
) + (x
->ealg
->alg_key_len
+7)/8);
1610 l
+= RTA_SPACE(sizeof(*x
->calg
));
1612 l
+= RTA_SPACE(sizeof(*x
->encap
));
1617 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1619 struct xfrm_usersa_info
*p
;
1620 struct xfrm_usersa_id
*id
;
1621 struct nlmsghdr
*nlh
;
1622 struct sk_buff
*skb
;
1624 int len
= xfrm_sa_len(x
);
1627 headlen
= sizeof(*p
);
1628 if (c
->event
== XFRM_MSG_DELSA
) {
1629 len
+= RTA_SPACE(headlen
);
1630 headlen
= sizeof(*id
);
1632 len
+= NLMSG_SPACE(headlen
);
1634 skb
= alloc_skb(len
, GFP_ATOMIC
);
1639 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1640 nlh
->nlmsg_flags
= 0;
1642 p
= NLMSG_DATA(nlh
);
1643 if (c
->event
== XFRM_MSG_DELSA
) {
1644 id
= NLMSG_DATA(nlh
);
1645 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
1646 id
->spi
= x
->id
.spi
;
1647 id
->family
= x
->props
.family
;
1648 id
->proto
= x
->id
.proto
;
1650 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_SA
, sizeof(*p
)));
1653 copy_to_user_state(x
, p
);
1656 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
1657 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
1659 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
1660 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
1662 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
1665 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
1667 nlh
->nlmsg_len
= skb
->tail
- b
;
1669 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1670 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1678 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1682 case XFRM_MSG_EXPIRE
:
1683 return xfrm_exp_state_notify(x
, c
);
1684 case XFRM_MSG_NEWAE
:
1685 return xfrm_aevent_state_notify(x
, c
);
1686 case XFRM_MSG_DELSA
:
1687 case XFRM_MSG_UPDSA
:
1688 case XFRM_MSG_NEWSA
:
1689 return xfrm_notify_sa(x
, c
);
1690 case XFRM_MSG_FLUSHSA
:
1691 return xfrm_notify_sa_flush(c
);
1693 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
1701 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1702 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1705 struct xfrm_user_acquire
*ua
;
1706 struct nlmsghdr
*nlh
;
1707 unsigned char *b
= skb
->tail
;
1708 __u32 seq
= xfrm_get_acqseq();
1710 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1712 ua
= NLMSG_DATA(nlh
);
1713 nlh
->nlmsg_flags
= 0;
1715 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1716 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1717 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1718 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1719 ua
->aalgos
= xt
->aalgos
;
1720 ua
->ealgos
= xt
->ealgos
;
1721 ua
->calgos
= xt
->calgos
;
1722 ua
->seq
= x
->km
.seq
= seq
;
1724 if (copy_to_user_tmpl(xp
, skb
) < 0)
1726 if (copy_to_user_state_sec_ctx(x
, skb
))
1729 nlh
->nlmsg_len
= skb
->tail
- b
;
1733 skb_trim(skb
, b
- skb
->data
);
1737 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1738 struct xfrm_policy
*xp
, int dir
)
1740 struct sk_buff
*skb
;
1743 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1744 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1745 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1746 skb
= alloc_skb(len
, GFP_ATOMIC
);
1750 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1753 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_ACQUIRE
;
1754 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
1757 /* User gives us xfrm_user_policy_info followed by an array of 0
1758 * or more templates.
1760 static struct xfrm_policy
*xfrm_compile_policy(u16 family
, int opt
,
1761 u8
*data
, int len
, int *dir
)
1763 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1764 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1765 struct xfrm_policy
*xp
;
1770 if (opt
!= IP_XFRM_POLICY
) {
1775 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1777 if (opt
!= IPV6_XFRM_POLICY
) {
1790 if (len
< sizeof(*p
) ||
1791 verify_newpolicy_info(p
))
1794 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1795 if (nr
> XFRM_MAX_DEPTH
)
1798 if (p
->dir
> XFRM_POLICY_OUT
)
1801 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1807 copy_from_user_policy(xp
, p
);
1808 copy_templates(xp
, ut
, nr
);
1815 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
1816 int dir
, struct km_event
*c
)
1818 struct xfrm_user_polexpire
*upe
;
1819 struct nlmsghdr
*nlh
;
1820 int hard
= c
->data
.hard
;
1821 unsigned char *b
= skb
->tail
;
1823 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
1824 upe
= NLMSG_DATA(nlh
);
1825 nlh
->nlmsg_flags
= 0;
1827 copy_to_user_policy(xp
, &upe
->pol
, dir
);
1828 if (copy_to_user_tmpl(xp
, skb
) < 0)
1830 if (copy_to_user_sec_ctx(xp
, skb
))
1834 nlh
->nlmsg_len
= skb
->tail
- b
;
1838 skb_trim(skb
, b
- skb
->data
);
1842 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1844 struct sk_buff
*skb
;
1847 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1848 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
1849 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1850 skb
= alloc_skb(len
, GFP_ATOMIC
);
1854 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
1857 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1858 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1861 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1863 struct xfrm_userpolicy_info
*p
;
1864 struct xfrm_userpolicy_id
*id
;
1865 struct nlmsghdr
*nlh
;
1866 struct sk_buff
*skb
;
1868 int len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1871 headlen
= sizeof(*p
);
1872 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1873 len
+= RTA_SPACE(headlen
);
1874 headlen
= sizeof(*id
);
1876 len
+= NLMSG_SPACE(headlen
);
1878 skb
= alloc_skb(len
, GFP_ATOMIC
);
1883 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1885 p
= NLMSG_DATA(nlh
);
1886 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1887 id
= NLMSG_DATA(nlh
);
1888 memset(id
, 0, sizeof(*id
));
1891 id
->index
= xp
->index
;
1893 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
1895 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_POLICY
, sizeof(*p
)));
1898 nlh
->nlmsg_flags
= 0;
1900 copy_to_user_policy(xp
, p
, dir
);
1901 if (copy_to_user_tmpl(xp
, skb
) < 0)
1904 nlh
->nlmsg_len
= skb
->tail
- b
;
1906 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1907 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1915 static int xfrm_notify_policy_flush(struct km_event
*c
)
1917 struct nlmsghdr
*nlh
;
1918 struct sk_buff
*skb
;
1920 int len
= NLMSG_LENGTH(0);
1922 skb
= alloc_skb(len
, GFP_ATOMIC
);
1928 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0);
1930 nlh
->nlmsg_len
= skb
->tail
- b
;
1932 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1933 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1940 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1944 case XFRM_MSG_NEWPOLICY
:
1945 case XFRM_MSG_UPDPOLICY
:
1946 case XFRM_MSG_DELPOLICY
:
1947 return xfrm_notify_policy(xp
, dir
, c
);
1948 case XFRM_MSG_FLUSHPOLICY
:
1949 return xfrm_notify_policy_flush(c
);
1950 case XFRM_MSG_POLEXPIRE
:
1951 return xfrm_exp_policy_notify(xp
, dir
, c
);
1953 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
1960 static struct xfrm_mgr netlink_mgr
= {
1962 .notify
= xfrm_send_state_notify
,
1963 .acquire
= xfrm_send_acquire
,
1964 .compile_policy
= xfrm_compile_policy
,
1965 .notify_policy
= xfrm_send_policy_notify
,
1968 static int __init
xfrm_user_init(void)
1972 printk(KERN_INFO
"Initializing IPsec netlink socket\n");
1974 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
1975 xfrm_netlink_rcv
, THIS_MODULE
);
1978 rcu_assign_pointer(xfrm_nl
, nlsk
);
1980 xfrm_register_km(&netlink_mgr
);
1985 static void __exit
xfrm_user_exit(void)
1987 struct sock
*nlsk
= xfrm_nl
;
1989 xfrm_unregister_km(&netlink_mgr
);
1990 rcu_assign_pointer(xfrm_nl
, NULL
);
1992 sock_release(nlsk
->sk_socket
);
1995 module_init(xfrm_user_init
);
1996 module_exit(xfrm_user_exit
);
1997 MODULE_LICENSE("GPL");
1998 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);