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
;
41 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*algp
))
47 if (!algp
->alg_key_len
&&
48 strcmp(algp
->alg_name
, "digest_null") != 0)
53 if (!algp
->alg_key_len
&&
54 strcmp(algp
->alg_name
, "cipher_null") != 0)
59 /* Zero length keys are legal. */
66 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
70 static int verify_encap_tmpl(struct rtattr
**xfrma
)
72 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
73 struct xfrm_encap_tmpl
*encap
;
78 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
84 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
85 struct rtattr
**xfrma
)
95 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
107 switch (p
->id
.proto
) {
109 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
110 xfrma
[XFRMA_ALG_CRYPT
-1] ||
111 xfrma
[XFRMA_ALG_COMP
-1])
116 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
117 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
118 xfrma
[XFRMA_ALG_COMP
-1])
123 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
124 xfrma
[XFRMA_ALG_AUTH
-1] ||
125 xfrma
[XFRMA_ALG_CRYPT
-1])
133 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
135 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
137 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
139 if ((err
= verify_encap_tmpl(xfrma
)))
158 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
159 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
160 struct rtattr
*u_arg
)
162 struct rtattr
*rta
= u_arg
;
163 struct xfrm_algo
*p
, *ualg
;
164 struct xfrm_algo_desc
*algo
;
169 ualg
= RTA_DATA(rta
);
171 algo
= get_byname(ualg
->alg_name
, 1);
174 *props
= algo
->desc
.sadb_alg_id
;
176 p
= kmalloc(sizeof(*ualg
) + ualg
->alg_key_len
, GFP_KERNEL
);
180 memcpy(p
, ualg
, sizeof(*ualg
) + ualg
->alg_key_len
);
185 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
187 struct rtattr
*rta
= u_arg
;
188 struct xfrm_encap_tmpl
*p
, *uencap
;
193 uencap
= RTA_DATA(rta
);
194 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
198 memcpy(p
, uencap
, sizeof(*p
));
203 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
205 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
206 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
207 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
208 x
->props
.mode
= p
->mode
;
209 x
->props
.replay_window
= p
->replay_window
;
210 x
->props
.reqid
= p
->reqid
;
211 x
->props
.family
= p
->family
;
212 x
->props
.saddr
= p
->saddr
;
213 x
->props
.flags
= p
->flags
;
216 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
217 struct rtattr
**xfrma
,
220 struct xfrm_state
*x
= xfrm_state_alloc();
226 copy_from_user_state(x
, p
);
228 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
229 xfrm_aalg_get_byname
,
230 xfrma
[XFRMA_ALG_AUTH
-1])))
232 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
233 xfrm_ealg_get_byname
,
234 xfrma
[XFRMA_ALG_CRYPT
-1])))
236 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
237 xfrm_calg_get_byname
,
238 xfrma
[XFRMA_ALG_COMP
-1])))
240 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
244 x
->type
= xfrm_get_type(x
->id
.proto
, x
->props
.family
);
248 err
= x
->type
->init_state(x
, NULL
);
252 x
->curlft
.add_time
= (unsigned long) xtime
.tv_sec
;
253 x
->km
.state
= XFRM_STATE_VALID
;
259 x
->km
.state
= XFRM_STATE_DEAD
;
266 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
268 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
269 struct xfrm_state
*x
;
272 err
= verify_newsa_info(p
, (struct rtattr
**) xfrma
);
276 x
= xfrm_state_construct(p
, (struct rtattr
**) xfrma
, &err
);
280 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
281 err
= xfrm_state_add(x
);
283 err
= xfrm_state_update(x
);
286 x
->km
.state
= XFRM_STATE_DEAD
;
293 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
295 struct xfrm_state
*x
;
296 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
298 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
302 if (xfrm_state_kern(x
)) {
307 xfrm_state_delete(x
);
313 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
315 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
316 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
317 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
318 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
319 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
320 p
->saddr
= x
->props
.saddr
;
321 p
->mode
= x
->props
.mode
;
322 p
->replay_window
= x
->props
.replay_window
;
323 p
->reqid
= x
->props
.reqid
;
324 p
->family
= x
->props
.family
;
325 p
->flags
= x
->props
.flags
;
329 struct xfrm_dump_info
{
330 struct sk_buff
*in_skb
;
331 struct sk_buff
*out_skb
;
338 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
340 struct xfrm_dump_info
*sp
= ptr
;
341 struct sk_buff
*in_skb
= sp
->in_skb
;
342 struct sk_buff
*skb
= sp
->out_skb
;
343 struct xfrm_usersa_info
*p
;
344 struct nlmsghdr
*nlh
;
345 unsigned char *b
= skb
->tail
;
347 if (sp
->this_idx
< sp
->start_idx
)
350 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
352 XFRM_MSG_NEWSA
, sizeof(*p
));
353 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
356 copy_to_user_state(x
, p
);
359 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
360 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
362 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
363 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
365 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
368 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
370 nlh
->nlmsg_len
= skb
->tail
- b
;
377 skb_trim(skb
, b
- skb
->data
);
381 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
383 struct xfrm_dump_info info
;
385 info
.in_skb
= cb
->skb
;
387 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
388 info
.nlmsg_flags
= NLM_F_MULTI
;
390 info
.start_idx
= cb
->args
[0];
391 (void) xfrm_state_walk(IPSEC_PROTO_ANY
, dump_one_state
, &info
);
392 cb
->args
[0] = info
.this_idx
;
397 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
398 struct xfrm_state
*x
, u32 seq
)
400 struct xfrm_dump_info info
;
403 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
405 return ERR_PTR(-ENOMEM
);
407 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
408 info
.in_skb
= in_skb
;
410 info
.nlmsg_seq
= seq
;
411 info
.nlmsg_flags
= 0;
412 info
.this_idx
= info
.start_idx
= 0;
414 if (dump_one_state(x
, 0, &info
)) {
422 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
424 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
425 struct xfrm_state
*x
;
426 struct sk_buff
*resp_skb
;
429 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
434 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
435 if (IS_ERR(resp_skb
)) {
436 err
= PTR_ERR(resp_skb
);
438 err
= netlink_unicast(xfrm_nl
, resp_skb
,
439 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
446 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
448 switch (p
->info
.id
.proto
) {
454 /* IPCOMP spi is 16-bits. */
455 if (p
->max
>= 0x10000)
469 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
471 struct xfrm_state
*x
;
472 struct xfrm_userspi_info
*p
;
473 struct sk_buff
*resp_skb
;
474 xfrm_address_t
*daddr
;
479 err
= verify_userspi_info(p
);
483 family
= p
->info
.family
;
484 daddr
= &p
->info
.id
.daddr
;
488 x
= xfrm_find_acq_byseq(p
->info
.seq
);
489 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
496 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
497 p
->info
.id
.proto
, daddr
,
504 resp_skb
= ERR_PTR(-ENOENT
);
506 spin_lock_bh(&x
->lock
);
507 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
508 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
510 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
512 spin_unlock_bh(&x
->lock
);
514 if (IS_ERR(resp_skb
)) {
515 err
= PTR_ERR(resp_skb
);
519 err
= netlink_unicast(xfrm_nl
, resp_skb
,
520 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
528 static int verify_policy_dir(__u8 dir
)
532 case XFRM_POLICY_OUT
:
533 case XFRM_POLICY_FWD
:
543 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
547 case XFRM_SHARE_SESSION
:
548 case XFRM_SHARE_USER
:
549 case XFRM_SHARE_UNIQUE
:
557 case XFRM_POLICY_ALLOW
:
558 case XFRM_POLICY_BLOCK
:
565 switch (p
->sel
.family
) {
570 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
573 return -EAFNOSUPPORT
;
580 return verify_policy_dir(p
->dir
);
583 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
589 for (i
= 0; i
< nr
; i
++, ut
++) {
590 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
592 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
593 memcpy(&t
->saddr
, &ut
->saddr
,
594 sizeof(xfrm_address_t
));
595 t
->reqid
= ut
->reqid
;
597 t
->share
= ut
->share
;
598 t
->optional
= ut
->optional
;
599 t
->aalgos
= ut
->aalgos
;
600 t
->ealgos
= ut
->ealgos
;
601 t
->calgos
= ut
->calgos
;
605 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
607 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
608 struct xfrm_user_tmpl
*utmpl
;
614 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
616 if (nr
> XFRM_MAX_DEPTH
)
619 copy_templates(pol
, RTA_DATA(rt
), nr
);
624 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
626 xp
->priority
= p
->priority
;
627 xp
->index
= p
->index
;
628 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
629 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
630 xp
->action
= p
->action
;
631 xp
->flags
= p
->flags
;
632 xp
->family
= p
->sel
.family
;
633 /* XXX xp->share = p->share; */
636 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
638 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
639 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
640 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
641 p
->priority
= xp
->priority
;
642 p
->index
= xp
->index
;
643 p
->sel
.family
= xp
->family
;
645 p
->action
= xp
->action
;
646 p
->flags
= xp
->flags
;
647 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
650 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
652 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
660 copy_from_user_policy(xp
, p
);
661 err
= copy_from_user_tmpl(xp
, xfrma
);
671 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
673 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
674 struct xfrm_policy
*xp
;
678 err
= verify_newpolicy_info(p
);
682 xp
= xfrm_policy_construct(p
, (struct rtattr
**) xfrma
, &err
);
686 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
687 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
698 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
700 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
703 if (xp
->xfrm_nr
== 0)
706 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
707 struct xfrm_user_tmpl
*up
= &vec
[i
];
708 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
710 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
711 up
->family
= xp
->family
;
712 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
713 up
->reqid
= kp
->reqid
;
715 up
->share
= kp
->share
;
716 up
->optional
= kp
->optional
;
717 up
->aalgos
= kp
->aalgos
;
718 up
->ealgos
= kp
->ealgos
;
719 up
->calgos
= kp
->calgos
;
721 RTA_PUT(skb
, XFRMA_TMPL
,
722 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
731 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
733 struct xfrm_dump_info
*sp
= ptr
;
734 struct xfrm_userpolicy_info
*p
;
735 struct sk_buff
*in_skb
= sp
->in_skb
;
736 struct sk_buff
*skb
= sp
->out_skb
;
737 struct nlmsghdr
*nlh
;
738 unsigned char *b
= skb
->tail
;
740 if (sp
->this_idx
< sp
->start_idx
)
743 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
745 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
747 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
749 copy_to_user_policy(xp
, p
, dir
);
750 if (copy_to_user_tmpl(xp
, skb
) < 0)
753 nlh
->nlmsg_len
= skb
->tail
- b
;
759 skb_trim(skb
, b
- skb
->data
);
763 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
765 struct xfrm_dump_info info
;
767 info
.in_skb
= cb
->skb
;
769 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
770 info
.nlmsg_flags
= NLM_F_MULTI
;
772 info
.start_idx
= cb
->args
[0];
773 (void) xfrm_policy_walk(dump_one_policy
, &info
);
774 cb
->args
[0] = info
.this_idx
;
779 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
780 struct xfrm_policy
*xp
,
783 struct xfrm_dump_info info
;
786 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
788 return ERR_PTR(-ENOMEM
);
790 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
791 info
.in_skb
= in_skb
;
793 info
.nlmsg_seq
= seq
;
794 info
.nlmsg_flags
= 0;
795 info
.this_idx
= info
.start_idx
= 0;
797 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
805 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
807 struct xfrm_policy
*xp
;
808 struct xfrm_userpolicy_id
*p
;
813 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
815 err
= verify_policy_dir(p
->dir
);
820 xp
= xfrm_policy_byid(p
->dir
, p
->index
, delete);
822 xp
= xfrm_policy_bysel(p
->dir
, &p
->sel
, delete);
827 struct sk_buff
*resp_skb
;
829 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
830 if (IS_ERR(resp_skb
)) {
831 err
= PTR_ERR(resp_skb
);
833 err
= netlink_unicast(xfrm_nl
, resp_skb
,
844 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
846 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
848 xfrm_state_flush(p
->proto
);
852 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
858 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
860 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
861 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
862 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
863 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
864 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
865 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
866 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
867 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
868 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
869 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
870 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
871 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
872 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
873 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
874 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
879 static struct xfrm_link
{
880 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
881 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
882 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
883 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
884 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
885 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
886 .dump
= xfrm_dump_sa
},
887 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
888 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
889 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
890 .dump
= xfrm_dump_policy
},
891 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
892 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
893 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
894 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
895 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
898 static int xfrm_done(struct netlink_callback
*cb
)
903 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
905 struct rtattr
*xfrma
[XFRMA_MAX
];
906 struct xfrm_link
*link
;
909 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
912 type
= nlh
->nlmsg_type
;
914 /* A control message: ignore them */
915 if (type
< XFRM_MSG_BASE
)
918 /* Unknown message: reply with EINVAL */
919 if (type
> XFRM_MSG_MAX
)
922 type
-= XFRM_MSG_BASE
;
923 link
= &xfrm_dispatch
[type
];
925 /* All operations require privileges, even GET */
926 if (security_netlink_recv(skb
)) {
931 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
932 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
933 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
936 if (link
->dump
== NULL
)
939 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
944 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
951 memset(xfrma
, 0, sizeof(xfrma
));
953 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
956 if (nlh
->nlmsg_len
> min_len
) {
957 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
958 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
960 while (RTA_OK(attr
, attrlen
)) {
961 unsigned short flavor
= attr
->rta_type
;
963 if (flavor
> XFRMA_MAX
)
965 xfrma
[flavor
- 1] = attr
;
967 attr
= RTA_NEXT(attr
, attrlen
);
971 if (link
->doit
== NULL
)
973 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
982 static int xfrm_user_rcv_skb(struct sk_buff
*skb
)
985 struct nlmsghdr
*nlh
;
987 while (skb
->len
>= NLMSG_SPACE(0)) {
990 nlh
= (struct nlmsghdr
*) skb
->data
;
991 if (nlh
->nlmsg_len
< sizeof(*nlh
) ||
992 skb
->len
< nlh
->nlmsg_len
)
994 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
997 if (xfrm_user_rcv_msg(skb
, nlh
, &err
) < 0) {
1000 netlink_ack(skb
, nlh
, err
);
1001 } else if (nlh
->nlmsg_flags
& NLM_F_ACK
)
1002 netlink_ack(skb
, nlh
, 0);
1003 skb_pull(skb
, rlen
);
1009 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1011 unsigned int qlen
= skb_queue_len(&sk
->sk_receive_queue
);
1014 struct sk_buff
*skb
;
1016 down(&xfrm_cfg_sem
);
1018 if (qlen
> skb_queue_len(&sk
->sk_receive_queue
))
1019 qlen
= skb_queue_len(&sk
->sk_receive_queue
);
1021 for (; qlen
; qlen
--) {
1022 skb
= skb_dequeue(&sk
->sk_receive_queue
);
1023 if (xfrm_user_rcv_skb(skb
)) {
1025 skb_queue_head(&sk
->sk_receive_queue
,
1041 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, int hard
)
1043 struct xfrm_user_expire
*ue
;
1044 struct nlmsghdr
*nlh
;
1045 unsigned char *b
= skb
->tail
;
1047 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_EXPIRE
,
1049 ue
= NLMSG_DATA(nlh
);
1050 nlh
->nlmsg_flags
= 0;
1052 copy_to_user_state(x
, &ue
->state
);
1053 ue
->hard
= (hard
!= 0) ? 1 : 0;
1055 nlh
->nlmsg_len
= skb
->tail
- b
;
1059 skb_trim(skb
, b
- skb
->data
);
1063 static int xfrm_send_state_notify(struct xfrm_state
*x
, int hard
)
1065 struct sk_buff
*skb
;
1067 skb
= alloc_skb(sizeof(struct xfrm_user_expire
) + 16, GFP_ATOMIC
);
1071 if (build_expire(skb
, x
, hard
) < 0)
1074 NETLINK_CB(skb
).dst_groups
= XFRMGRP_EXPIRE
;
1076 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_EXPIRE
, GFP_ATOMIC
);
1079 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1080 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1083 struct xfrm_user_acquire
*ua
;
1084 struct nlmsghdr
*nlh
;
1085 unsigned char *b
= skb
->tail
;
1086 __u32 seq
= xfrm_get_acqseq();
1088 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1090 ua
= NLMSG_DATA(nlh
);
1091 nlh
->nlmsg_flags
= 0;
1093 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1094 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1095 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1096 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1097 ua
->aalgos
= xt
->aalgos
;
1098 ua
->ealgos
= xt
->ealgos
;
1099 ua
->calgos
= xt
->calgos
;
1100 ua
->seq
= x
->km
.seq
= seq
;
1102 if (copy_to_user_tmpl(xp
, skb
) < 0)
1105 nlh
->nlmsg_len
= skb
->tail
- b
;
1109 skb_trim(skb
, b
- skb
->data
);
1113 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1114 struct xfrm_policy
*xp
, int dir
)
1116 struct sk_buff
*skb
;
1119 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1120 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1121 skb
= alloc_skb(len
, GFP_ATOMIC
);
1125 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1128 NETLINK_CB(skb
).dst_groups
= XFRMGRP_ACQUIRE
;
1130 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_ACQUIRE
, GFP_ATOMIC
);
1133 /* User gives us xfrm_user_policy_info followed by an array of 0
1134 * or more templates.
1136 static struct xfrm_policy
*xfrm_compile_policy(u16 family
, int opt
,
1137 u8
*data
, int len
, int *dir
)
1139 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1140 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1141 struct xfrm_policy
*xp
;
1146 if (opt
!= IP_XFRM_POLICY
) {
1151 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1153 if (opt
!= IPV6_XFRM_POLICY
) {
1166 if (len
< sizeof(*p
) ||
1167 verify_newpolicy_info(p
))
1170 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1171 if (nr
> XFRM_MAX_DEPTH
)
1174 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1180 copy_from_user_policy(xp
, p
);
1181 copy_templates(xp
, ut
, nr
);
1188 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
1191 struct xfrm_user_polexpire
*upe
;
1192 struct nlmsghdr
*nlh
;
1193 unsigned char *b
= skb
->tail
;
1195 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
1196 upe
= NLMSG_DATA(nlh
);
1197 nlh
->nlmsg_flags
= 0;
1199 copy_to_user_policy(xp
, &upe
->pol
, dir
);
1200 if (copy_to_user_tmpl(xp
, skb
) < 0)
1204 nlh
->nlmsg_len
= skb
->tail
- b
;
1208 skb_trim(skb
, b
- skb
->data
);
1212 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, int hard
)
1214 struct sk_buff
*skb
;
1217 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1218 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
1219 skb
= alloc_skb(len
, GFP_ATOMIC
);
1223 if (build_polexpire(skb
, xp
, dir
, hard
) < 0)
1226 NETLINK_CB(skb
).dst_groups
= XFRMGRP_EXPIRE
;
1228 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMGRP_EXPIRE
, GFP_ATOMIC
);
1231 static struct xfrm_mgr netlink_mgr
= {
1233 .notify
= xfrm_send_state_notify
,
1234 .acquire
= xfrm_send_acquire
,
1235 .compile_policy
= xfrm_compile_policy
,
1236 .notify_policy
= xfrm_send_policy_notify
,
1239 static int __init
xfrm_user_init(void)
1241 printk(KERN_INFO
"Initializing IPsec netlink socket\n");
1243 xfrm_nl
= netlink_kernel_create(NETLINK_XFRM
, xfrm_netlink_rcv
);
1244 if (xfrm_nl
== NULL
)
1247 xfrm_register_km(&netlink_mgr
);
1252 static void __exit
xfrm_user_exit(void)
1254 xfrm_unregister_km(&netlink_mgr
);
1255 sock_release(xfrm_nl
->sk_socket
);
1258 module_init(xfrm_user_init
);
1259 module_exit(xfrm_user_exit
);
1260 MODULE_LICENSE("GPL");