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/netlink.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 <asm/uaccess.h>
31 static struct sock
*xfrm_nl
;
33 static int verify_one_alg(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
)
35 struct rtattr
*rt
= xfrma
[type
- 1];
36 struct xfrm_algo
*algp
;
42 len
= (rt
->rta_len
- sizeof(*rt
)) - sizeof(*algp
);
48 len
-= (algp
->alg_key_len
+ 7U) / 8;
54 if (!algp
->alg_key_len
&&
55 strcmp(algp
->alg_name
, "digest_null") != 0)
60 if (!algp
->alg_key_len
&&
61 strcmp(algp
->alg_name
, "cipher_null") != 0)
66 /* Zero length keys are legal. */
73 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
77 static int verify_encap_tmpl(struct rtattr
**xfrma
)
79 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
80 struct xfrm_encap_tmpl
*encap
;
85 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
91 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
92 struct rtattr
**xfrma
)
102 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
114 switch (p
->id
.proto
) {
116 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
117 xfrma
[XFRMA_ALG_CRYPT
-1] ||
118 xfrma
[XFRMA_ALG_COMP
-1])
123 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
124 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
125 xfrma
[XFRMA_ALG_COMP
-1])
130 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
131 xfrma
[XFRMA_ALG_AUTH
-1] ||
132 xfrma
[XFRMA_ALG_CRYPT
-1])
140 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
142 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
144 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
146 if ((err
= verify_encap_tmpl(xfrma
)))
165 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
166 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
167 struct rtattr
*u_arg
)
169 struct rtattr
*rta
= u_arg
;
170 struct xfrm_algo
*p
, *ualg
;
171 struct xfrm_algo_desc
*algo
;
177 ualg
= RTA_DATA(rta
);
179 algo
= get_byname(ualg
->alg_name
, 1);
182 *props
= algo
->desc
.sadb_alg_id
;
184 len
= sizeof(*ualg
) + (ualg
->alg_key_len
+ 7U) / 8;
185 p
= kmalloc(len
, GFP_KERNEL
);
189 memcpy(p
, ualg
, len
);
194 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
196 struct rtattr
*rta
= u_arg
;
197 struct xfrm_encap_tmpl
*p
, *uencap
;
202 uencap
= RTA_DATA(rta
);
203 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
207 memcpy(p
, uencap
, sizeof(*p
));
212 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
214 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
215 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
216 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
217 x
->props
.mode
= p
->mode
;
218 x
->props
.replay_window
= p
->replay_window
;
219 x
->props
.reqid
= p
->reqid
;
220 x
->props
.family
= p
->family
;
221 x
->props
.saddr
= p
->saddr
;
222 x
->props
.flags
= p
->flags
;
225 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
226 struct rtattr
**xfrma
,
229 struct xfrm_state
*x
= xfrm_state_alloc();
235 copy_from_user_state(x
, p
);
237 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
238 xfrm_aalg_get_byname
,
239 xfrma
[XFRMA_ALG_AUTH
-1])))
241 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
242 xfrm_ealg_get_byname
,
243 xfrma
[XFRMA_ALG_CRYPT
-1])))
245 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
246 xfrm_calg_get_byname
,
247 xfrma
[XFRMA_ALG_COMP
-1])))
249 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
253 x
->type
= xfrm_get_type(x
->id
.proto
, x
->props
.family
);
257 err
= x
->type
->init_state(x
, NULL
);
261 x
->curlft
.add_time
= (unsigned long) xtime
.tv_sec
;
262 x
->km
.state
= XFRM_STATE_VALID
;
268 x
->km
.state
= XFRM_STATE_DEAD
;
275 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
277 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
278 struct xfrm_state
*x
;
282 err
= verify_newsa_info(p
, (struct rtattr
**) xfrma
);
286 x
= xfrm_state_construct(p
, (struct rtattr
**) xfrma
, &err
);
291 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
292 err
= xfrm_state_add(x
);
294 err
= xfrm_state_update(x
);
297 x
->km
.state
= XFRM_STATE_DEAD
;
302 c
.seq
= nlh
->nlmsg_seq
;
303 c
.pid
= nlh
->nlmsg_pid
;
304 c
.event
= nlh
->nlmsg_type
;
306 km_state_notify(x
, &c
);
312 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
314 struct xfrm_state
*x
;
317 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
319 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
323 if (xfrm_state_kern(x
)) {
328 err
= xfrm_state_delete(x
);
334 c
.seq
= nlh
->nlmsg_seq
;
335 c
.pid
= nlh
->nlmsg_pid
;
336 c
.event
= nlh
->nlmsg_type
;
337 km_state_notify(x
, &c
);
343 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
345 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
346 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
347 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
348 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
349 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
350 p
->saddr
= x
->props
.saddr
;
351 p
->mode
= x
->props
.mode
;
352 p
->replay_window
= x
->props
.replay_window
;
353 p
->reqid
= x
->props
.reqid
;
354 p
->family
= x
->props
.family
;
355 p
->flags
= x
->props
.flags
;
359 struct xfrm_dump_info
{
360 struct sk_buff
*in_skb
;
361 struct sk_buff
*out_skb
;
368 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
370 struct xfrm_dump_info
*sp
= ptr
;
371 struct sk_buff
*in_skb
= sp
->in_skb
;
372 struct sk_buff
*skb
= sp
->out_skb
;
373 struct xfrm_usersa_info
*p
;
374 struct nlmsghdr
*nlh
;
375 unsigned char *b
= skb
->tail
;
377 if (sp
->this_idx
< sp
->start_idx
)
380 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
382 XFRM_MSG_NEWSA
, sizeof(*p
));
383 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
386 copy_to_user_state(x
, p
);
389 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
390 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
392 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
393 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
395 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
398 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
400 nlh
->nlmsg_len
= skb
->tail
- b
;
407 skb_trim(skb
, b
- skb
->data
);
411 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
413 struct xfrm_dump_info info
;
415 info
.in_skb
= cb
->skb
;
417 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
418 info
.nlmsg_flags
= NLM_F_MULTI
;
420 info
.start_idx
= cb
->args
[0];
421 (void) xfrm_state_walk(IPSEC_PROTO_ANY
, dump_one_state
, &info
);
422 cb
->args
[0] = info
.this_idx
;
427 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
428 struct xfrm_state
*x
, u32 seq
)
430 struct xfrm_dump_info info
;
433 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
435 return ERR_PTR(-ENOMEM
);
437 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
438 info
.in_skb
= in_skb
;
440 info
.nlmsg_seq
= seq
;
441 info
.nlmsg_flags
= 0;
442 info
.this_idx
= info
.start_idx
= 0;
444 if (dump_one_state(x
, 0, &info
)) {
452 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
454 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
455 struct xfrm_state
*x
;
456 struct sk_buff
*resp_skb
;
459 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
464 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
465 if (IS_ERR(resp_skb
)) {
466 err
= PTR_ERR(resp_skb
);
468 err
= netlink_unicast(xfrm_nl
, resp_skb
,
469 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
476 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
478 switch (p
->info
.id
.proto
) {
484 /* IPCOMP spi is 16-bits. */
485 if (p
->max
>= 0x10000)
499 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
501 struct xfrm_state
*x
;
502 struct xfrm_userspi_info
*p
;
503 struct sk_buff
*resp_skb
;
504 xfrm_address_t
*daddr
;
509 err
= verify_userspi_info(p
);
513 family
= p
->info
.family
;
514 daddr
= &p
->info
.id
.daddr
;
518 x
= xfrm_find_acq_byseq(p
->info
.seq
);
519 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
526 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
527 p
->info
.id
.proto
, daddr
,
534 resp_skb
= ERR_PTR(-ENOENT
);
536 spin_lock_bh(&x
->lock
);
537 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
538 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
540 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
542 spin_unlock_bh(&x
->lock
);
544 if (IS_ERR(resp_skb
)) {
545 err
= PTR_ERR(resp_skb
);
549 err
= netlink_unicast(xfrm_nl
, resp_skb
,
550 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
558 static int verify_policy_dir(__u8 dir
)
562 case XFRM_POLICY_OUT
:
563 case XFRM_POLICY_FWD
:
573 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
577 case XFRM_SHARE_SESSION
:
578 case XFRM_SHARE_USER
:
579 case XFRM_SHARE_UNIQUE
:
587 case XFRM_POLICY_ALLOW
:
588 case XFRM_POLICY_BLOCK
:
595 switch (p
->sel
.family
) {
600 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
603 return -EAFNOSUPPORT
;
610 return verify_policy_dir(p
->dir
);
613 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
619 for (i
= 0; i
< nr
; i
++, ut
++) {
620 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
622 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
623 memcpy(&t
->saddr
, &ut
->saddr
,
624 sizeof(xfrm_address_t
));
625 t
->reqid
= ut
->reqid
;
627 t
->share
= ut
->share
;
628 t
->optional
= ut
->optional
;
629 t
->aalgos
= ut
->aalgos
;
630 t
->ealgos
= ut
->ealgos
;
631 t
->calgos
= ut
->calgos
;
635 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
637 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
638 struct xfrm_user_tmpl
*utmpl
;
644 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
646 if (nr
> XFRM_MAX_DEPTH
)
649 copy_templates(pol
, RTA_DATA(rt
), nr
);
654 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
656 xp
->priority
= p
->priority
;
657 xp
->index
= p
->index
;
658 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
659 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
660 xp
->action
= p
->action
;
661 xp
->flags
= p
->flags
;
662 xp
->family
= p
->sel
.family
;
663 /* XXX xp->share = p->share; */
666 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
668 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
669 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
670 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
671 p
->priority
= xp
->priority
;
672 p
->index
= xp
->index
;
673 p
->sel
.family
= xp
->family
;
675 p
->action
= xp
->action
;
676 p
->flags
= xp
->flags
;
677 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
680 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
682 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
690 copy_from_user_policy(xp
, p
);
691 err
= copy_from_user_tmpl(xp
, xfrma
);
701 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
703 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
704 struct xfrm_policy
*xp
;
709 err
= verify_newpolicy_info(p
);
713 xp
= xfrm_policy_construct(p
, (struct rtattr
**) xfrma
, &err
);
717 /* shouldnt excl be based on nlh flags??
718 * Aha! this is anti-netlink really i.e more pfkey derived
719 * in netlink excl is a flag and you wouldnt need
720 * a type XFRM_MSG_UPDPOLICY - JHS */
721 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
722 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
728 c
.event
= nlh
->nlmsg_type
;
729 c
.seq
= nlh
->nlmsg_seq
;
730 c
.pid
= nlh
->nlmsg_pid
;
731 km_policy_notify(xp
, p
->dir
, &c
);
738 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
740 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
743 if (xp
->xfrm_nr
== 0)
746 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
747 struct xfrm_user_tmpl
*up
= &vec
[i
];
748 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
750 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
751 up
->family
= xp
->family
;
752 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
753 up
->reqid
= kp
->reqid
;
755 up
->share
= kp
->share
;
756 up
->optional
= kp
->optional
;
757 up
->aalgos
= kp
->aalgos
;
758 up
->ealgos
= kp
->ealgos
;
759 up
->calgos
= kp
->calgos
;
761 RTA_PUT(skb
, XFRMA_TMPL
,
762 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
771 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
773 struct xfrm_dump_info
*sp
= ptr
;
774 struct xfrm_userpolicy_info
*p
;
775 struct sk_buff
*in_skb
= sp
->in_skb
;
776 struct sk_buff
*skb
= sp
->out_skb
;
777 struct nlmsghdr
*nlh
;
778 unsigned char *b
= skb
->tail
;
780 if (sp
->this_idx
< sp
->start_idx
)
783 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
785 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
787 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
789 copy_to_user_policy(xp
, p
, dir
);
790 if (copy_to_user_tmpl(xp
, skb
) < 0)
793 nlh
->nlmsg_len
= skb
->tail
- b
;
799 skb_trim(skb
, b
- skb
->data
);
803 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
805 struct xfrm_dump_info info
;
807 info
.in_skb
= cb
->skb
;
809 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
810 info
.nlmsg_flags
= NLM_F_MULTI
;
812 info
.start_idx
= cb
->args
[0];
813 (void) xfrm_policy_walk(dump_one_policy
, &info
);
814 cb
->args
[0] = info
.this_idx
;
819 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
820 struct xfrm_policy
*xp
,
823 struct xfrm_dump_info info
;
826 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
828 return ERR_PTR(-ENOMEM
);
830 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
831 info
.in_skb
= in_skb
;
833 info
.nlmsg_seq
= seq
;
834 info
.nlmsg_flags
= 0;
835 info
.this_idx
= info
.start_idx
= 0;
837 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
845 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
847 struct xfrm_policy
*xp
;
848 struct xfrm_userpolicy_id
*p
;
854 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
856 err
= verify_policy_dir(p
->dir
);
861 xp
= xfrm_policy_byid(p
->dir
, p
->index
, delete);
863 xp
= xfrm_policy_bysel(p
->dir
, &p
->sel
, delete);
868 struct sk_buff
*resp_skb
;
870 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
871 if (IS_ERR(resp_skb
)) {
872 err
= PTR_ERR(resp_skb
);
874 err
= netlink_unicast(xfrm_nl
, resp_skb
,
879 c
.data
.byid
= p
->index
;
880 c
.event
= nlh
->nlmsg_type
;
881 c
.seq
= nlh
->nlmsg_seq
;
882 c
.pid
= nlh
->nlmsg_pid
;
883 km_policy_notify(xp
, p
->dir
, &c
);
891 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
894 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
896 xfrm_state_flush(p
->proto
);
897 c
.data
.proto
= p
->proto
;
898 c
.event
= nlh
->nlmsg_type
;
899 c
.seq
= nlh
->nlmsg_seq
;
900 c
.pid
= nlh
->nlmsg_pid
;
901 km_state_notify(NULL
, &c
);
906 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
911 c
.event
= nlh
->nlmsg_type
;
912 c
.seq
= nlh
->nlmsg_seq
;
913 c
.pid
= nlh
->nlmsg_pid
;
914 km_policy_notify(NULL
, 0, &c
);
918 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
920 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
921 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
922 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
923 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
924 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
925 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
926 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
927 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
928 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
929 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
930 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
931 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
932 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
933 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
934 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
939 static struct xfrm_link
{
940 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
941 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
942 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
943 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
944 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
945 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
946 .dump
= xfrm_dump_sa
},
947 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
948 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
949 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
950 .dump
= xfrm_dump_policy
},
951 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
952 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
953 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
954 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
955 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
958 static int xfrm_done(struct netlink_callback
*cb
)
963 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
965 struct rtattr
*xfrma
[XFRMA_MAX
];
966 struct xfrm_link
*link
;
969 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
972 type
= nlh
->nlmsg_type
;
974 /* A control message: ignore them */
975 if (type
< XFRM_MSG_BASE
)
978 /* Unknown message: reply with EINVAL */
979 if (type
> XFRM_MSG_MAX
)
982 type
-= XFRM_MSG_BASE
;
983 link
= &xfrm_dispatch
[type
];
985 /* All operations require privileges, even GET */
986 if (security_netlink_recv(skb
)) {
991 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
992 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
993 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
996 if (link
->dump
== NULL
)
999 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
1004 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1005 if (rlen
> skb
->len
)
1007 skb_pull(skb
, rlen
);
1011 memset(xfrma
, 0, sizeof(xfrma
));
1013 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1016 if (nlh
->nlmsg_len
> min_len
) {
1017 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1018 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1020 while (RTA_OK(attr
, attrlen
)) {
1021 unsigned short flavor
= attr
->rta_type
;
1023 if (flavor
> XFRMA_MAX
)
1025 xfrma
[flavor
- 1] = attr
;
1027 attr
= RTA_NEXT(attr
, attrlen
);
1031 if (link
->doit
== NULL
)
1033 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
1042 static int xfrm_user_rcv_skb(struct sk_buff
*skb
)
1045 struct nlmsghdr
*nlh
;
1047 while (skb
->len
>= NLMSG_SPACE(0)) {
1050 nlh
= (struct nlmsghdr
*) skb
->data
;
1051 if (nlh
->nlmsg_len
< sizeof(*nlh
) ||
1052 skb
->len
< nlh
->nlmsg_len
)
1054 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
1055 if (rlen
> skb
->len
)
1057 if (xfrm_user_rcv_msg(skb
, nlh
, &err
) < 0) {
1060 netlink_ack(skb
, nlh
, err
);
1061 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
1062 netlink_ack(skb
, nlh
, 0);
1063 skb_pull(skb
, rlen
);
1069 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1071 unsigned int qlen
= skb_queue_len(&sk
->sk_receive_queue
);
1074 struct sk_buff
*skb
;
1076 down(&xfrm_cfg_sem
);
1078 if (qlen
> skb_queue_len(&sk
->sk_receive_queue
))
1079 qlen
= skb_queue_len(&sk
->sk_receive_queue
);
1081 for (; qlen
; qlen
--) {
1082 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1083 if (xfrm_user_rcv_skb(skb
)) {
1085 skb_queue_head(&sk
->sk_receive_queue
,
1101 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, int hard
)
1103 struct xfrm_user_expire
*ue
;
1104 struct nlmsghdr
*nlh
;
1105 unsigned char *b
= skb
->tail
;
1107 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_EXPIRE
,
1109 ue
= NLMSG_DATA(nlh
);
1110 nlh
->nlmsg_flags
= 0;
1112 copy_to_user_state(x
, &ue
->state
);
1113 ue
->hard
= (hard
!= 0) ? 1 : 0;
1115 nlh
->nlmsg_len
= skb
->tail
- b
;
1119 skb_trim(skb
, b
- skb
->data
);
1123 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1125 struct sk_buff
*skb
;
1127 /* fix to do alloc using NLM macros */
1128 skb
= alloc_skb(sizeof(struct xfrm_user_expire
) + 16, GFP_ATOMIC
);
1132 if (build_expire(skb
, x
, c
->data
.hard
) < 0)
1135 NETLINK_CB(skb
).dst_groups
= XFRMGRP_EXPIRE
;
1137 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_EXPIRE
, GFP_ATOMIC
);
1140 static int xfrm_notify_sa_flush(struct km_event
*c
)
1142 struct xfrm_usersa_flush
*p
;
1143 struct nlmsghdr
*nlh
;
1144 struct sk_buff
*skb
;
1146 int len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1148 skb
= alloc_skb(len
, GFP_ATOMIC
);
1153 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
,
1154 XFRM_MSG_FLUSHSA
, sizeof(*p
));
1155 nlh
->nlmsg_flags
= 0;
1157 p
= NLMSG_DATA(nlh
);
1158 p
->proto
= c
->data
.proto
;
1160 nlh
->nlmsg_len
= skb
->tail
- b
;
1162 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_SA
, GFP_ATOMIC
);
1169 static int inline xfrm_sa_len(struct xfrm_state
*x
)
1171 int l
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1173 l
+= RTA_SPACE(sizeof(*x
->aalg
) + (x
->aalg
->alg_key_len
+7)/8);
1175 l
+= RTA_SPACE(sizeof(*x
->ealg
) + (x
->ealg
->alg_key_len
+7)/8);
1177 l
+= RTA_SPACE(sizeof(*x
->calg
));
1179 l
+= RTA_SPACE(sizeof(*x
->encap
));
1184 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1186 struct xfrm_usersa_info
*p
;
1187 struct nlmsghdr
*nlh
;
1188 struct sk_buff
*skb
;
1190 int len
= xfrm_sa_len(x
);
1192 skb
= alloc_skb(len
, GFP_ATOMIC
);
1197 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, sizeof(*p
));
1198 nlh
->nlmsg_flags
= 0;
1200 p
= NLMSG_DATA(nlh
);
1201 copy_to_user_state(x
, p
);
1204 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
1205 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
1207 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
1208 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
1210 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
1213 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
1215 nlh
->nlmsg_len
= skb
->tail
- b
;
1217 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_SA
, GFP_ATOMIC
);
1225 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1229 case XFRM_MSG_EXPIRE
:
1230 return xfrm_exp_state_notify(x
, c
);
1231 case XFRM_MSG_DELSA
:
1232 case XFRM_MSG_UPDSA
:
1233 case XFRM_MSG_NEWSA
:
1234 return xfrm_notify_sa(x
, c
);
1235 case XFRM_MSG_FLUSHSA
:
1236 return xfrm_notify_sa_flush(c
);
1238 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
1246 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1247 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1250 struct xfrm_user_acquire
*ua
;
1251 struct nlmsghdr
*nlh
;
1252 unsigned char *b
= skb
->tail
;
1253 __u32 seq
= xfrm_get_acqseq();
1255 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1257 ua
= NLMSG_DATA(nlh
);
1258 nlh
->nlmsg_flags
= 0;
1260 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1261 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1262 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1263 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1264 ua
->aalgos
= xt
->aalgos
;
1265 ua
->ealgos
= xt
->ealgos
;
1266 ua
->calgos
= xt
->calgos
;
1267 ua
->seq
= x
->km
.seq
= seq
;
1269 if (copy_to_user_tmpl(xp
, skb
) < 0)
1272 nlh
->nlmsg_len
= skb
->tail
- b
;
1276 skb_trim(skb
, b
- skb
->data
);
1280 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1281 struct xfrm_policy
*xp
, int dir
)
1283 struct sk_buff
*skb
;
1286 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1287 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1288 skb
= alloc_skb(len
, GFP_ATOMIC
);
1292 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1295 NETLINK_CB(skb
).dst_groups
= XFRMGRP_ACQUIRE
;
1297 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_ACQUIRE
, GFP_ATOMIC
);
1300 /* User gives us xfrm_user_policy_info followed by an array of 0
1301 * or more templates.
1303 static struct xfrm_policy
*xfrm_compile_policy(u16 family
, int opt
,
1304 u8
*data
, int len
, int *dir
)
1306 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1307 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1308 struct xfrm_policy
*xp
;
1313 if (opt
!= IP_XFRM_POLICY
) {
1318 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1320 if (opt
!= IPV6_XFRM_POLICY
) {
1333 if (len
< sizeof(*p
) ||
1334 verify_newpolicy_info(p
))
1337 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1338 if (nr
> XFRM_MAX_DEPTH
)
1341 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1347 copy_from_user_policy(xp
, p
);
1348 copy_templates(xp
, ut
, nr
);
1355 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
1358 struct xfrm_user_polexpire
*upe
;
1359 struct nlmsghdr
*nlh
;
1360 unsigned char *b
= skb
->tail
;
1362 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
1363 upe
= NLMSG_DATA(nlh
);
1364 nlh
->nlmsg_flags
= 0;
1366 copy_to_user_policy(xp
, &upe
->pol
, dir
);
1367 if (copy_to_user_tmpl(xp
, skb
) < 0)
1371 nlh
->nlmsg_len
= skb
->tail
- b
;
1375 skb_trim(skb
, b
- skb
->data
);
1379 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1381 struct sk_buff
*skb
;
1384 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1385 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
1386 skb
= alloc_skb(len
, GFP_ATOMIC
);
1390 if (build_polexpire(skb
, xp
, dir
, c
->data
.hard
) < 0)
1393 NETLINK_CB(skb
).dst_groups
= XFRMGRP_EXPIRE
;
1395 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_EXPIRE
, GFP_ATOMIC
);
1398 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1400 struct xfrm_userpolicy_info
*p
;
1401 struct nlmsghdr
*nlh
;
1402 struct sk_buff
*skb
;
1404 int len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1405 len
+= NLMSG_SPACE(sizeof(struct xfrm_userpolicy_info
));
1407 skb
= alloc_skb(len
, GFP_ATOMIC
);
1412 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, sizeof(*p
));
1414 p
= NLMSG_DATA(nlh
);
1416 nlh
->nlmsg_flags
= 0;
1418 copy_to_user_policy(xp
, p
, dir
);
1419 if (copy_to_user_tmpl(xp
, skb
) < 0)
1422 nlh
->nlmsg_len
= skb
->tail
- b
;
1424 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_POLICY
, GFP_ATOMIC
);
1431 static int xfrm_notify_policy_flush(struct km_event
*c
)
1433 struct nlmsghdr
*nlh
;
1434 struct sk_buff
*skb
;
1436 int len
= NLMSG_LENGTH(0);
1438 skb
= alloc_skb(len
, GFP_ATOMIC
);
1444 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0);
1446 nlh
->nlmsg_len
= skb
->tail
- b
;
1448 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_POLICY
, GFP_ATOMIC
);
1455 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
1459 case XFRM_MSG_NEWPOLICY
:
1460 case XFRM_MSG_UPDPOLICY
:
1461 case XFRM_MSG_DELPOLICY
:
1462 return xfrm_notify_policy(xp
, dir
, c
);
1463 case XFRM_MSG_FLUSHPOLICY
:
1464 return xfrm_notify_policy_flush(c
);
1465 case XFRM_MSG_POLEXPIRE
:
1466 return xfrm_exp_policy_notify(xp
, dir
, c
);
1468 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
1475 static struct xfrm_mgr netlink_mgr
= {
1477 .notify
= xfrm_send_state_notify
,
1478 .acquire
= xfrm_send_acquire
,
1479 .compile_policy
= xfrm_compile_policy
,
1480 .notify_policy
= xfrm_send_policy_notify
,
1483 static int __init
xfrm_user_init(void)
1485 printk(KERN_INFO
"Initializing IPsec netlink socket\n");
1487 xfrm_nl
= netlink_kernel_create(NETLINK_XFRM
, xfrm_netlink_rcv
);
1488 if (xfrm_nl
== NULL
)
1491 xfrm_register_km(&netlink_mgr
);
1496 static void __exit
xfrm_user_exit(void)
1498 xfrm_unregister_km(&netlink_mgr
);
1499 sock_release(xfrm_nl
->sk_socket
);
1502 module_init(xfrm_user_init
);
1503 module_exit(xfrm_user_exit
);
1504 MODULE_LICENSE("GPL");