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/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/socket.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/rtnetlink.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>
29 #include <asm/uaccess.h>
31 static int verify_one_alg(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
)
33 struct rtattr
*rt
= xfrma
[type
- 1];
34 struct xfrm_algo
*algp
;
40 len
= (rt
->rta_len
- sizeof(*rt
)) - sizeof(*algp
);
46 len
-= (algp
->alg_key_len
+ 7U) / 8;
52 if (!algp
->alg_key_len
&&
53 strcmp(algp
->alg_name
, "digest_null") != 0)
58 if (!algp
->alg_key_len
&&
59 strcmp(algp
->alg_name
, "cipher_null") != 0)
64 /* Zero length keys are legal. */
71 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
75 static int verify_encap_tmpl(struct rtattr
**xfrma
)
77 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
78 struct xfrm_encap_tmpl
*encap
;
83 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
90 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
92 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
93 struct xfrm_user_sec_ctx
*uctx
;
99 if (rt
->rta_len
< sizeof(*uctx
))
104 len
+= sizeof(struct xfrm_user_sec_ctx
);
105 len
+= uctx
->ctx_len
;
107 if (uctx
->len
!= len
)
114 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
115 struct rtattr
**xfrma
)
125 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
137 switch (p
->id
.proto
) {
139 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
140 xfrma
[XFRMA_ALG_CRYPT
-1] ||
141 xfrma
[XFRMA_ALG_COMP
-1])
146 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
147 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
148 xfrma
[XFRMA_ALG_COMP
-1])
153 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
154 xfrma
[XFRMA_ALG_AUTH
-1] ||
155 xfrma
[XFRMA_ALG_CRYPT
-1])
163 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
165 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
167 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
169 if ((err
= verify_encap_tmpl(xfrma
)))
171 if ((err
= verify_sec_ctx_len(xfrma
)))
190 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
191 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
192 struct rtattr
*u_arg
)
194 struct rtattr
*rta
= u_arg
;
195 struct xfrm_algo
*p
, *ualg
;
196 struct xfrm_algo_desc
*algo
;
202 ualg
= RTA_DATA(rta
);
204 algo
= get_byname(ualg
->alg_name
, 1);
207 *props
= algo
->desc
.sadb_alg_id
;
209 len
= sizeof(*ualg
) + (ualg
->alg_key_len
+ 7U) / 8;
210 p
= kmalloc(len
, GFP_KERNEL
);
214 memcpy(p
, ualg
, len
);
219 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
221 struct rtattr
*rta
= u_arg
;
222 struct xfrm_encap_tmpl
*p
, *uencap
;
227 uencap
= RTA_DATA(rta
);
228 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
232 memcpy(p
, uencap
, sizeof(*p
));
238 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy
*xp
)
240 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
244 len
+= sizeof(struct xfrm_user_sec_ctx
);
245 len
+= xfrm_ctx
->ctx_len
;
250 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
252 struct xfrm_user_sec_ctx
*uctx
;
257 uctx
= RTA_DATA(u_arg
);
258 return security_xfrm_state_alloc(x
, uctx
);
261 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
263 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
264 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
265 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
266 x
->props
.mode
= p
->mode
;
267 x
->props
.replay_window
= p
->replay_window
;
268 x
->props
.reqid
= p
->reqid
;
269 x
->props
.family
= p
->family
;
270 x
->props
.saddr
= p
->saddr
;
271 x
->props
.flags
= p
->flags
;
275 * someday when pfkey also has support, we could have the code
276 * somehow made shareable and move it to xfrm_state.c - JHS
279 static int xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
282 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
283 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
284 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
285 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
288 struct xfrm_replay_state
*replay
;
289 if (RTA_PAYLOAD(rp
) < sizeof(*replay
))
291 replay
= RTA_DATA(rp
);
292 memcpy(&x
->replay
, replay
, sizeof(*replay
));
293 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
297 struct xfrm_lifetime_cur
*ltime
;
298 if (RTA_PAYLOAD(lt
) < sizeof(*ltime
))
300 ltime
= RTA_DATA(lt
);
301 x
->curlft
.bytes
= ltime
->bytes
;
302 x
->curlft
.packets
= ltime
->packets
;
303 x
->curlft
.add_time
= ltime
->add_time
;
304 x
->curlft
.use_time
= ltime
->use_time
;
308 if (RTA_PAYLOAD(et
) < sizeof(u32
))
310 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
314 if (RTA_PAYLOAD(rt
) < sizeof(u32
))
316 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
324 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
325 struct rtattr
**xfrma
,
328 struct xfrm_state
*x
= xfrm_state_alloc();
334 copy_from_user_state(x
, p
);
336 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
337 xfrm_aalg_get_byname
,
338 xfrma
[XFRMA_ALG_AUTH
-1])))
340 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
341 xfrm_ealg_get_byname
,
342 xfrma
[XFRMA_ALG_CRYPT
-1])))
344 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
345 xfrm_calg_get_byname
,
346 xfrma
[XFRMA_ALG_COMP
-1])))
348 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
351 err
= xfrm_init_state(x
);
355 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
359 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
360 /* sysctl_xfrm_aevent_etime is in 100ms units */
361 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
362 x
->preplay
.bitmap
= 0;
363 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
364 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
366 /* override default values from above */
368 err
= xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
375 x
->km
.state
= XFRM_STATE_DEAD
;
382 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
384 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
385 struct xfrm_state
*x
;
389 err
= verify_newsa_info(p
, (struct rtattr
**)xfrma
);
393 x
= xfrm_state_construct(p
, (struct rtattr
**)xfrma
, &err
);
398 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
399 err
= xfrm_state_add(x
);
401 err
= xfrm_state_update(x
);
404 x
->km
.state
= XFRM_STATE_DEAD
;
409 c
.seq
= nlh
->nlmsg_seq
;
410 c
.pid
= nlh
->nlmsg_pid
;
411 c
.event
= nlh
->nlmsg_type
;
413 km_state_notify(x
, &c
);
419 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
421 struct xfrm_state
*x
;
424 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
426 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
430 if ((err
= security_xfrm_state_delete(x
)) != 0)
433 if (xfrm_state_kern(x
)) {
438 err
= xfrm_state_delete(x
);
442 c
.seq
= nlh
->nlmsg_seq
;
443 c
.pid
= nlh
->nlmsg_pid
;
444 c
.event
= nlh
->nlmsg_type
;
445 km_state_notify(x
, &c
);
452 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
454 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
455 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
456 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
457 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
458 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
459 p
->saddr
= x
->props
.saddr
;
460 p
->mode
= x
->props
.mode
;
461 p
->replay_window
= x
->props
.replay_window
;
462 p
->reqid
= x
->props
.reqid
;
463 p
->family
= x
->props
.family
;
464 p
->flags
= x
->props
.flags
;
468 struct xfrm_dump_info
{
469 struct sk_buff
*in_skb
;
470 struct sk_buff
*out_skb
;
477 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
479 struct xfrm_dump_info
*sp
= ptr
;
480 struct sk_buff
*in_skb
= sp
->in_skb
;
481 struct sk_buff
*skb
= sp
->out_skb
;
482 struct xfrm_usersa_info
*p
;
483 struct nlmsghdr
*nlh
;
484 unsigned char *b
= skb
->tail
;
486 if (sp
->this_idx
< sp
->start_idx
)
489 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
491 XFRM_MSG_NEWSA
, sizeof(*p
));
492 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
495 copy_to_user_state(x
, p
);
498 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
499 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
501 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
502 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
504 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
507 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
510 int ctx_size
= sizeof(struct xfrm_sec_ctx
) +
511 x
->security
->ctx_len
;
512 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
513 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
515 uctx
->exttype
= XFRMA_SEC_CTX
;
516 uctx
->len
= ctx_size
;
517 uctx
->ctx_doi
= x
->security
->ctx_doi
;
518 uctx
->ctx_alg
= x
->security
->ctx_alg
;
519 uctx
->ctx_len
= x
->security
->ctx_len
;
520 memcpy(uctx
+ 1, x
->security
->ctx_str
, x
->security
->ctx_len
);
522 nlh
->nlmsg_len
= skb
->tail
- b
;
529 skb_trim(skb
, b
- skb
->data
);
533 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
535 struct xfrm_dump_info info
;
537 info
.in_skb
= cb
->skb
;
539 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
540 info
.nlmsg_flags
= NLM_F_MULTI
;
542 info
.start_idx
= cb
->args
[0];
543 (void) xfrm_state_walk(IPSEC_PROTO_ANY
, dump_one_state
, &info
);
544 cb
->args
[0] = info
.this_idx
;
549 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
550 struct xfrm_state
*x
, u32 seq
)
552 struct xfrm_dump_info info
;
555 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
557 return ERR_PTR(-ENOMEM
);
559 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
560 info
.in_skb
= in_skb
;
562 info
.nlmsg_seq
= seq
;
563 info
.nlmsg_flags
= 0;
564 info
.this_idx
= info
.start_idx
= 0;
566 if (dump_one_state(x
, 0, &info
)) {
574 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
576 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
577 struct xfrm_state
*x
;
578 struct sk_buff
*resp_skb
;
581 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
586 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
587 if (IS_ERR(resp_skb
)) {
588 err
= PTR_ERR(resp_skb
);
590 err
= netlink_unicast(xfrm_nl
, resp_skb
,
591 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
598 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
600 switch (p
->info
.id
.proto
) {
606 /* IPCOMP spi is 16-bits. */
607 if (p
->max
>= 0x10000)
621 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
623 struct xfrm_state
*x
;
624 struct xfrm_userspi_info
*p
;
625 struct sk_buff
*resp_skb
;
626 xfrm_address_t
*daddr
;
631 err
= verify_userspi_info(p
);
635 family
= p
->info
.family
;
636 daddr
= &p
->info
.id
.daddr
;
640 x
= xfrm_find_acq_byseq(p
->info
.seq
);
641 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
648 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
649 p
->info
.id
.proto
, daddr
,
656 resp_skb
= ERR_PTR(-ENOENT
);
658 spin_lock_bh(&x
->lock
);
659 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
660 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
662 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
664 spin_unlock_bh(&x
->lock
);
666 if (IS_ERR(resp_skb
)) {
667 err
= PTR_ERR(resp_skb
);
671 err
= netlink_unicast(xfrm_nl
, resp_skb
,
672 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
680 static int verify_policy_dir(__u8 dir
)
684 case XFRM_POLICY_OUT
:
685 case XFRM_POLICY_FWD
:
695 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
699 case XFRM_SHARE_SESSION
:
700 case XFRM_SHARE_USER
:
701 case XFRM_SHARE_UNIQUE
:
709 case XFRM_POLICY_ALLOW
:
710 case XFRM_POLICY_BLOCK
:
717 switch (p
->sel
.family
) {
722 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
725 return -EAFNOSUPPORT
;
732 return verify_policy_dir(p
->dir
);
735 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
737 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
738 struct xfrm_user_sec_ctx
*uctx
;
744 return security_xfrm_policy_alloc(pol
, uctx
);
747 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
753 for (i
= 0; i
< nr
; i
++, ut
++) {
754 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
756 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
757 memcpy(&t
->saddr
, &ut
->saddr
,
758 sizeof(xfrm_address_t
));
759 t
->reqid
= ut
->reqid
;
761 t
->share
= ut
->share
;
762 t
->optional
= ut
->optional
;
763 t
->aalgos
= ut
->aalgos
;
764 t
->ealgos
= ut
->ealgos
;
765 t
->calgos
= ut
->calgos
;
769 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
771 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
772 struct xfrm_user_tmpl
*utmpl
;
778 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
780 if (nr
> XFRM_MAX_DEPTH
)
783 copy_templates(pol
, RTA_DATA(rt
), nr
);
788 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
790 xp
->priority
= p
->priority
;
791 xp
->index
= p
->index
;
792 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
793 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
794 xp
->action
= p
->action
;
795 xp
->flags
= p
->flags
;
796 xp
->family
= p
->sel
.family
;
797 /* XXX xp->share = p->share; */
800 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
802 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
803 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
804 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
805 p
->priority
= xp
->priority
;
806 p
->index
= xp
->index
;
807 p
->sel
.family
= xp
->family
;
809 p
->action
= xp
->action
;
810 p
->flags
= xp
->flags
;
811 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
814 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
816 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
824 copy_from_user_policy(xp
, p
);
826 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
827 err
= copy_from_user_sec_ctx(xp
, xfrma
);
838 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
840 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
841 struct xfrm_policy
*xp
;
846 err
= verify_newpolicy_info(p
);
849 err
= verify_sec_ctx_len((struct rtattr
**)xfrma
);
853 xp
= xfrm_policy_construct(p
, (struct rtattr
**)xfrma
, &err
);
857 /* shouldnt excl be based on nlh flags??
858 * Aha! this is anti-netlink really i.e more pfkey derived
859 * in netlink excl is a flag and you wouldnt need
860 * a type XFRM_MSG_UPDPOLICY - JHS */
861 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
862 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
864 security_xfrm_policy_free(xp
);
869 c
.event
= nlh
->nlmsg_type
;
870 c
.seq
= nlh
->nlmsg_seq
;
871 c
.pid
= nlh
->nlmsg_pid
;
872 km_policy_notify(xp
, p
->dir
, &c
);
879 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
881 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
884 if (xp
->xfrm_nr
== 0)
887 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
888 struct xfrm_user_tmpl
*up
= &vec
[i
];
889 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
891 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
892 up
->family
= xp
->family
;
893 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
894 up
->reqid
= kp
->reqid
;
896 up
->share
= kp
->share
;
897 up
->optional
= kp
->optional
;
898 up
->aalgos
= kp
->aalgos
;
899 up
->ealgos
= kp
->ealgos
;
900 up
->calgos
= kp
->calgos
;
902 RTA_PUT(skb
, XFRMA_TMPL
,
903 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
912 static int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
915 int ctx_size
= sizeof(struct xfrm_sec_ctx
) +
916 xp
->security
->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
= xp
->security
->ctx_doi
;
923 uctx
->ctx_alg
= xp
->security
->ctx_alg
;
924 uctx
->ctx_len
= xp
->security
->ctx_len
;
925 memcpy(uctx
+ 1, xp
->security
->ctx_str
, xp
->security
->ctx_len
);
933 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
935 struct xfrm_dump_info
*sp
= ptr
;
936 struct xfrm_userpolicy_info
*p
;
937 struct sk_buff
*in_skb
= sp
->in_skb
;
938 struct sk_buff
*skb
= sp
->out_skb
;
939 struct nlmsghdr
*nlh
;
940 unsigned char *b
= skb
->tail
;
942 if (sp
->this_idx
< sp
->start_idx
)
945 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
947 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
949 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
951 copy_to_user_policy(xp
, p
, dir
);
952 if (copy_to_user_tmpl(xp
, skb
) < 0)
954 if (copy_to_user_sec_ctx(xp
, skb
))
957 nlh
->nlmsg_len
= skb
->tail
- b
;
963 skb_trim(skb
, b
- skb
->data
);
967 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
969 struct xfrm_dump_info info
;
971 info
.in_skb
= cb
->skb
;
973 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
974 info
.nlmsg_flags
= NLM_F_MULTI
;
976 info
.start_idx
= cb
->args
[0];
977 (void) xfrm_policy_walk(dump_one_policy
, &info
);
978 cb
->args
[0] = info
.this_idx
;
983 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
984 struct xfrm_policy
*xp
,
987 struct xfrm_dump_info info
;
990 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
992 return ERR_PTR(-ENOMEM
);
994 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
995 info
.in_skb
= in_skb
;
997 info
.nlmsg_seq
= seq
;
998 info
.nlmsg_flags
= 0;
999 info
.this_idx
= info
.start_idx
= 0;
1001 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1009 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1011 struct xfrm_policy
*xp
;
1012 struct xfrm_userpolicy_id
*p
;
1017 p
= NLMSG_DATA(nlh
);
1018 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1020 err
= verify_policy_dir(p
->dir
);
1025 xp
= xfrm_policy_byid(p
->dir
, p
->index
, delete);
1027 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1028 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1029 struct xfrm_policy tmp
;
1031 err
= verify_sec_ctx_len(rtattrs
);
1035 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1037 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1039 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1042 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, delete);
1043 security_xfrm_policy_free(&tmp
);
1049 struct sk_buff
*resp_skb
;
1051 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1052 if (IS_ERR(resp_skb
)) {
1053 err
= PTR_ERR(resp_skb
);
1055 err
= netlink_unicast(xfrm_nl
, resp_skb
,
1056 NETLINK_CB(skb
).pid
,
1060 if ((err
= security_xfrm_policy_delete(xp
)) != 0)
1062 c
.data
.byid
= p
->index
;
1063 c
.event
= nlh
->nlmsg_type
;
1064 c
.seq
= nlh
->nlmsg_seq
;
1065 c
.pid
= nlh
->nlmsg_pid
;
1066 km_policy_notify(xp
, p
->dir
, &c
);
1075 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1078 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
1080 xfrm_state_flush(p
->proto
);
1081 c
.data
.proto
= p
->proto
;
1082 c
.event
= nlh
->nlmsg_type
;
1083 c
.seq
= nlh
->nlmsg_seq
;
1084 c
.pid
= nlh
->nlmsg_pid
;
1085 km_state_notify(NULL
, &c
);
1091 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1093 struct xfrm_aevent_id
*id
;
1094 struct nlmsghdr
*nlh
;
1095 struct xfrm_lifetime_cur ltime
;
1096 unsigned char *b
= skb
->tail
;
1098 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
));
1099 id
= NLMSG_DATA(nlh
);
1100 nlh
->nlmsg_flags
= 0;
1102 id
->sa_id
.daddr
= x
->id
.daddr
;
1103 id
->sa_id
.spi
= x
->id
.spi
;
1104 id
->sa_id
.family
= x
->props
.family
;
1105 id
->sa_id
.proto
= x
->id
.proto
;
1106 id
->flags
= c
->data
.aevent
;
1108 RTA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1110 ltime
.bytes
= x
->curlft
.bytes
;
1111 ltime
.packets
= x
->curlft
.packets
;
1112 ltime
.add_time
= x
->curlft
.add_time
;
1113 ltime
.use_time
= x
->curlft
.use_time
;
1115 RTA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(struct xfrm_lifetime_cur
), <ime
);
1117 if (id
->flags
&XFRM_AE_RTHR
) {
1118 RTA_PUT(skb
,XFRMA_REPLAY_THRESH
,sizeof(u32
),&x
->replay_maxdiff
);
1121 if (id
->flags
&XFRM_AE_ETHR
) {
1122 u32 etimer
= x
->replay_maxage
*10/HZ
;
1123 RTA_PUT(skb
,XFRMA_ETIMER_THRESH
,sizeof(u32
),&etimer
);
1126 nlh
->nlmsg_len
= skb
->tail
- b
;
1131 skb_trim(skb
, b
- skb
->data
);
1135 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1137 struct xfrm_state
*x
;
1138 struct sk_buff
*r_skb
;
1141 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1142 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1143 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1145 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1146 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1148 if (p
->flags
&XFRM_AE_RTHR
)
1149 len
+=RTA_SPACE(sizeof(u32
));
1151 if (p
->flags
&XFRM_AE_ETHR
)
1152 len
+=RTA_SPACE(sizeof(u32
));
1154 r_skb
= alloc_skb(len
, GFP_ATOMIC
);
1158 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1165 * XXX: is this lock really needed - none of the other
1166 * gets lock (the concern is things getting updated
1167 * while we are still reading) - jhs
1169 spin_lock_bh(&x
->lock
);
1170 c
.data
.aevent
= p
->flags
;
1171 c
.seq
= nlh
->nlmsg_seq
;
1172 c
.pid
= nlh
->nlmsg_pid
;
1174 if (build_aevent(r_skb
, x
, &c
) < 0)
1176 err
= netlink_unicast(xfrm_nl
, r_skb
,
1177 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1178 spin_unlock_bh(&x
->lock
);
1183 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1185 struct xfrm_state
*x
;
1188 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1189 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1190 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1195 /* pedantic mode - thou shalt sayeth replaceth */
1196 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1199 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1203 if (x
->km
.state
!= XFRM_STATE_VALID
)
1206 spin_lock_bh(&x
->lock
);
1207 err
= xfrm_update_ae_params(x
,(struct rtattr
**)xfrma
);
1208 spin_unlock_bh(&x
->lock
);
1212 c
.event
= nlh
->nlmsg_type
;
1213 c
.seq
= nlh
->nlmsg_seq
;
1214 c
.pid
= nlh
->nlmsg_pid
;
1215 c
.data
.aevent
= XFRM_AE_CU
;
1216 km_state_notify(x
, &c
);
1223 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1227 xfrm_policy_flush();
1228 c
.event
= nlh
->nlmsg_type
;
1229 c
.seq
= nlh
->nlmsg_seq
;
1230 c
.pid
= nlh
->nlmsg_pid
;
1231 km_policy_notify(NULL
, 0, &c
);
1235 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1237 struct xfrm_policy
*xp
;
1238 struct xfrm_user_polexpire
*up
= NLMSG_DATA(nlh
);
1239 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1243 xp
= xfrm_policy_byid(p
->dir
, p
->index
, 0);
1245 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1246 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1247 struct xfrm_policy tmp
;
1249 err
= verify_sec_ctx_len(rtattrs
);
1253 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1255 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1257 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1260 xp
= xfrm_policy_bysel_ctx(p
->dir
, &p
->sel
, tmp
.security
, 0);
1261 security_xfrm_policy_free(&tmp
);
1266 read_lock(&xp
->lock
);
1268 read_unlock(&xp
->lock
);
1272 read_unlock(&xp
->lock
);
1275 xfrm_policy_delete(xp
, p
->dir
);
1277 // reset the timers here?
1278 printk("Dont know what to do with soft policy expire\n");
1280 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1287 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1289 struct xfrm_state
*x
;
1291 struct xfrm_user_expire
*ue
= NLMSG_DATA(nlh
);
1292 struct xfrm_usersa_info
*p
= &ue
->state
;
1294 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1302 spin_lock_bh(&x
->lock
);
1303 if (x
->km
.state
!= XFRM_STATE_VALID
)
1305 km_state_expired(x
, ue
->hard
, current
->pid
);
1308 __xfrm_state_delete(x
);
1310 spin_unlock_bh(&x
->lock
);
1315 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1317 struct xfrm_policy
*xp
;
1318 struct xfrm_user_tmpl
*ut
;
1320 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1322 struct xfrm_user_acquire
*ua
= NLMSG_DATA(nlh
);
1323 struct xfrm_state
*x
= xfrm_state_alloc();
1329 err
= verify_newpolicy_info(&ua
->policy
);
1331 printk("BAD policy passed\n");
1337 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
); if (!xp
) {
1342 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1343 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1344 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1347 /* extract the templates and for each call km_key */
1348 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1349 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1350 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1351 x
->props
.mode
= t
->mode
;
1352 x
->props
.reqid
= t
->reqid
;
1353 x
->props
.family
= ut
->family
;
1354 t
->aalgos
= ua
->aalgos
;
1355 t
->ealgos
= ua
->ealgos
;
1356 t
->calgos
= ua
->calgos
;
1357 err
= km_query(x
, t
, xp
);
1368 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1370 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1371 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1372 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1373 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1374 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1375 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1376 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1377 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1378 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1379 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1380 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1381 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1382 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1383 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1384 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
1385 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1386 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1391 static struct xfrm_link
{
1392 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
1393 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1394 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1395 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1396 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1397 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1398 .dump
= xfrm_dump_sa
},
1399 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1400 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1401 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1402 .dump
= xfrm_dump_policy
},
1403 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1404 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1405 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1406 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1407 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1408 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1409 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1410 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1411 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1412 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1415 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
1417 struct rtattr
*xfrma
[XFRMA_MAX
];
1418 struct xfrm_link
*link
;
1421 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
1424 type
= nlh
->nlmsg_type
;
1426 /* A control message: ignore them */
1427 if (type
< XFRM_MSG_BASE
)
1430 /* Unknown message: reply with EINVAL */
1431 if (type
> XFRM_MSG_MAX
)
1434 type
-= XFRM_MSG_BASE
;
1435 link
= &xfrm_dispatch
[type
];
1437 /* All operations require privileges, even GET */
1438 if (security_netlink_recv(skb
, CAP_NET_ADMIN
)) {
1443 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1444 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1445 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1446 if (link
->dump
== NULL
)
1449 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
1450 link
->dump
, NULL
)) != 0) {
1454 netlink_queue_skip(nlh
, skb
);
1458 memset(xfrma
, 0, sizeof(xfrma
));
1460 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1463 if (nlh
->nlmsg_len
> min_len
) {
1464 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1465 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1467 while (RTA_OK(attr
, attrlen
)) {
1468 unsigned short flavor
= attr
->rta_type
;
1470 if (flavor
> XFRMA_MAX
)
1472 xfrma
[flavor
- 1] = attr
;
1474 attr
= RTA_NEXT(attr
, attrlen
);
1478 if (link
->doit
== NULL
)
1480 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
1489 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1491 unsigned int qlen
= 0;
1494 mutex_lock(&xfrm_cfg_mutex
);
1495 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1496 mutex_unlock(&xfrm_cfg_mutex
);
1501 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1503 struct xfrm_user_expire
*ue
;
1504 struct nlmsghdr
*nlh
;
1505 unsigned char *b
= skb
->tail
;
1507 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
,
1509 ue
= NLMSG_DATA(nlh
);
1510 nlh
->nlmsg_flags
= 0;
1512 copy_to_user_state(x
, &ue
->state
);
1513 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1515 nlh
->nlmsg_len
= skb
->tail
- b
;
1519 skb_trim(skb
, b
- skb
->data
);
1523 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1525 struct sk_buff
*skb
;
1526 int len
= NLMSG_LENGTH(sizeof(struct xfrm_user_expire
));
1528 skb
= alloc_skb(len
, GFP_ATOMIC
);
1532 if (build_expire(skb
, x
, c
) < 0)
1535 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1536 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1539 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1541 struct sk_buff
*skb
;
1542 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1544 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1545 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1546 skb
= alloc_skb(len
, GFP_ATOMIC
);
1550 if (build_aevent(skb
, x
, c
) < 0)
1553 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_AEVENTS
;
1554 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1557 static int xfrm_notify_sa_flush(struct km_event
*c
)
1559 struct xfrm_usersa_flush
*p
;
1560 struct nlmsghdr
*nlh
;
1561 struct sk_buff
*skb
;
1563 int len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1565 skb
= alloc_skb(len
, GFP_ATOMIC
);
1570 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
,
1571 XFRM_MSG_FLUSHSA
, sizeof(*p
));
1572 nlh
->nlmsg_flags
= 0;
1574 p
= NLMSG_DATA(nlh
);
1575 p
->proto
= c
->data
.proto
;
1577 nlh
->nlmsg_len
= skb
->tail
- b
;
1579 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1580 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1587 static int inline xfrm_sa_len(struct xfrm_state
*x
)
1591 l
+= RTA_SPACE(sizeof(*x
->aalg
) + (x
->aalg
->alg_key_len
+7)/8);
1593 l
+= RTA_SPACE(sizeof(*x
->ealg
) + (x
->ealg
->alg_key_len
+7)/8);
1595 l
+= RTA_SPACE(sizeof(*x
->calg
));
1597 l
+= RTA_SPACE(sizeof(*x
->encap
));
1602 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1604 struct xfrm_usersa_info
*p
;
1605 struct xfrm_usersa_id
*id
;
1606 struct nlmsghdr
*nlh
;
1607 struct sk_buff
*skb
;
1609 int len
= xfrm_sa_len(x
);
1612 headlen
= sizeof(*p
);
1613 if (c
->event
== XFRM_MSG_DELSA
) {
1614 len
+= RTA_SPACE(headlen
);
1615 headlen
= sizeof(*id
);
1617 len
+= NLMSG_SPACE(headlen
);
1619 skb
= alloc_skb(len
, GFP_ATOMIC
);
1624 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1625 nlh
->nlmsg_flags
= 0;
1627 p
= NLMSG_DATA(nlh
);
1628 if (c
->event
== XFRM_MSG_DELSA
) {
1629 id
= NLMSG_DATA(nlh
);
1630 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
1631 id
->spi
= x
->id
.spi
;
1632 id
->family
= x
->props
.family
;
1633 id
->proto
= x
->id
.proto
;
1635 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_SA
, sizeof(*p
)));
1638 copy_to_user_state(x
, p
);
1641 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
1642 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
1644 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
1645 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
1647 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
1650 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
1652 nlh
->nlmsg_len
= skb
->tail
- b
;
1654 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1655 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1663 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1667 case XFRM_MSG_EXPIRE
:
1668 return xfrm_exp_state_notify(x
, c
);
1669 case XFRM_MSG_NEWAE
:
1670 return xfrm_aevent_state_notify(x
, c
);
1671 case XFRM_MSG_DELSA
:
1672 case XFRM_MSG_UPDSA
:
1673 case XFRM_MSG_NEWSA
:
1674 return xfrm_notify_sa(x
, c
);
1675 case XFRM_MSG_FLUSHSA
:
1676 return xfrm_notify_sa_flush(c
);
1678 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
1686 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1687 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1690 struct xfrm_user_acquire
*ua
;
1691 struct nlmsghdr
*nlh
;
1692 unsigned char *b
= skb
->tail
;
1693 __u32 seq
= xfrm_get_acqseq();
1695 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1697 ua
= NLMSG_DATA(nlh
);
1698 nlh
->nlmsg_flags
= 0;
1700 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1701 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1702 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1703 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1704 ua
->aalgos
= xt
->aalgos
;
1705 ua
->ealgos
= xt
->ealgos
;
1706 ua
->calgos
= xt
->calgos
;
1707 ua
->seq
= x
->km
.seq
= seq
;
1709 if (copy_to_user_tmpl(xp
, skb
) < 0)
1711 if (copy_to_user_sec_ctx(xp
, skb
))
1714 nlh
->nlmsg_len
= skb
->tail
- b
;
1718 skb_trim(skb
, b
- skb
->data
);
1722 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1723 struct xfrm_policy
*xp
, int dir
)
1725 struct sk_buff
*skb
;
1728 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1729 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1730 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1731 skb
= alloc_skb(len
, GFP_ATOMIC
);
1735 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1738 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_ACQUIRE
;
1739 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
1742 /* User gives us xfrm_user_policy_info followed by an array of 0
1743 * or more templates.
1745 static struct xfrm_policy
*xfrm_compile_policy(u16 family
, int opt
,
1746 u8
*data
, int len
, int *dir
)
1748 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1749 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1750 struct xfrm_policy
*xp
;
1755 if (opt
!= IP_XFRM_POLICY
) {
1760 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1762 if (opt
!= IPV6_XFRM_POLICY
) {
1775 if (len
< sizeof(*p
) ||
1776 verify_newpolicy_info(p
))
1779 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1780 if (nr
> XFRM_MAX_DEPTH
)
1783 if (p
->dir
> XFRM_POLICY_OUT
)
1786 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1792 copy_from_user_policy(xp
, p
);
1793 copy_templates(xp
, ut
, nr
);
1800 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
1801 int dir
, struct km_event
*c
)
1803 struct xfrm_user_polexpire
*upe
;
1804 struct nlmsghdr
*nlh
;
1805 int hard
= c
->data
.hard
;
1806 unsigned char *b
= skb
->tail
;
1808 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
1809 upe
= NLMSG_DATA(nlh
);
1810 nlh
->nlmsg_flags
= 0;
1812 copy_to_user_policy(xp
, &upe
->pol
, dir
);
1813 if (copy_to_user_tmpl(xp
, skb
) < 0)
1815 if (copy_to_user_sec_ctx(xp
, skb
))
1819 nlh
->nlmsg_len
= skb
->tail
- b
;
1823 skb_trim(skb
, b
- skb
->data
);
1827 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1829 struct sk_buff
*skb
;
1832 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1833 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
1834 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1835 skb
= alloc_skb(len
, GFP_ATOMIC
);
1839 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
1842 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1843 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1846 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1848 struct xfrm_userpolicy_info
*p
;
1849 struct xfrm_userpolicy_id
*id
;
1850 struct nlmsghdr
*nlh
;
1851 struct sk_buff
*skb
;
1853 int len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1856 headlen
= sizeof(*p
);
1857 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1858 len
+= RTA_SPACE(headlen
);
1859 headlen
= sizeof(*id
);
1861 len
+= NLMSG_SPACE(headlen
);
1863 skb
= alloc_skb(len
, GFP_ATOMIC
);
1868 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1870 p
= NLMSG_DATA(nlh
);
1871 if (c
->event
== XFRM_MSG_DELPOLICY
) {
1872 id
= NLMSG_DATA(nlh
);
1873 memset(id
, 0, sizeof(*id
));
1876 id
->index
= xp
->index
;
1878 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
1880 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_POLICY
, sizeof(*p
)));
1883 nlh
->nlmsg_flags
= 0;
1885 copy_to_user_policy(xp
, p
, dir
);
1886 if (copy_to_user_tmpl(xp
, skb
) < 0)
1889 nlh
->nlmsg_len
= skb
->tail
- b
;
1891 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1892 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1900 static int xfrm_notify_policy_flush(struct km_event
*c
)
1902 struct nlmsghdr
*nlh
;
1903 struct sk_buff
*skb
;
1905 int len
= NLMSG_LENGTH(0);
1907 skb
= alloc_skb(len
, GFP_ATOMIC
);
1913 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0);
1915 nlh
->nlmsg_len
= skb
->tail
- b
;
1917 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
1918 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
1925 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1929 case XFRM_MSG_NEWPOLICY
:
1930 case XFRM_MSG_UPDPOLICY
:
1931 case XFRM_MSG_DELPOLICY
:
1932 return xfrm_notify_policy(xp
, dir
, c
);
1933 case XFRM_MSG_FLUSHPOLICY
:
1934 return xfrm_notify_policy_flush(c
);
1935 case XFRM_MSG_POLEXPIRE
:
1936 return xfrm_exp_policy_notify(xp
, dir
, c
);
1938 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
1945 static struct xfrm_mgr netlink_mgr
= {
1947 .notify
= xfrm_send_state_notify
,
1948 .acquire
= xfrm_send_acquire
,
1949 .compile_policy
= xfrm_compile_policy
,
1950 .notify_policy
= xfrm_send_policy_notify
,
1953 static int __init
xfrm_user_init(void)
1957 printk(KERN_INFO
"Initializing IPsec netlink socket\n");
1959 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
1960 xfrm_netlink_rcv
, THIS_MODULE
);
1963 rcu_assign_pointer(xfrm_nl
, nlsk
);
1965 xfrm_register_km(&netlink_mgr
);
1970 static void __exit
xfrm_user_exit(void)
1972 struct sock
*nlsk
= xfrm_nl
;
1974 xfrm_unregister_km(&netlink_mgr
);
1975 rcu_assign_pointer(xfrm_nl
, NULL
);
1977 sock_release(nlsk
->sk_socket
);
1980 module_init(xfrm_user_init
);
1981 module_exit(xfrm_user_exit
);
1982 MODULE_LICENSE("GPL");
1983 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);