1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/socket.h>
19 #include <linux/string.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/ipsec.h>
25 #include <linux/init.h>
26 #include <linux/security.h>
29 #include <net/netlink.h>
30 #include <asm/uaccess.h>
31 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
35 static int verify_one_alg(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
)
37 struct rtattr
*rt
= xfrma
[type
- 1];
38 struct xfrm_algo
*algp
;
44 len
= (rt
->rta_len
- sizeof(*rt
)) - sizeof(*algp
);
50 len
-= (algp
->alg_key_len
+ 7U) / 8;
56 if (!algp
->alg_key_len
&&
57 strcmp(algp
->alg_name
, "digest_null") != 0)
62 if (!algp
->alg_key_len
&&
63 strcmp(algp
->alg_name
, "cipher_null") != 0)
68 /* Zero length keys are legal. */
75 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
79 static int verify_encap_tmpl(struct rtattr
**xfrma
)
81 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
82 struct xfrm_encap_tmpl
*encap
;
87 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
93 static int verify_one_addr(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
,
94 xfrm_address_t
**addrp
)
96 struct rtattr
*rt
= xfrma
[type
- 1];
101 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(**addrp
))
105 *addrp
= RTA_DATA(rt
);
110 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
112 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
113 struct xfrm_user_sec_ctx
*uctx
;
119 if (rt
->rta_len
< sizeof(*uctx
))
124 len
+= sizeof(struct xfrm_user_sec_ctx
);
125 len
+= uctx
->ctx_len
;
127 if (uctx
->len
!= len
)
134 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
135 struct rtattr
**xfrma
)
145 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
157 switch (p
->id
.proto
) {
159 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
160 xfrma
[XFRMA_ALG_CRYPT
-1] ||
161 xfrma
[XFRMA_ALG_COMP
-1])
166 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
167 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
168 xfrma
[XFRMA_ALG_COMP
-1])
173 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
174 xfrma
[XFRMA_ALG_AUTH
-1] ||
175 xfrma
[XFRMA_ALG_CRYPT
-1])
179 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS
:
181 case IPPROTO_ROUTING
:
182 if (xfrma
[XFRMA_ALG_COMP
-1] ||
183 xfrma
[XFRMA_ALG_AUTH
-1] ||
184 xfrma
[XFRMA_ALG_CRYPT
-1] ||
185 xfrma
[XFRMA_ENCAP
-1] ||
186 xfrma
[XFRMA_SEC_CTX
-1] ||
187 !xfrma
[XFRMA_COADDR
-1])
196 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
198 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
200 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
202 if ((err
= verify_encap_tmpl(xfrma
)))
204 if ((err
= verify_sec_ctx_len(xfrma
)))
206 if ((err
= verify_one_addr(xfrma
, XFRMA_COADDR
, NULL
)))
211 case XFRM_MODE_TRANSPORT
:
212 case XFRM_MODE_TUNNEL
:
213 case XFRM_MODE_ROUTEOPTIMIZATION
:
227 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
228 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
229 struct rtattr
*u_arg
)
231 struct rtattr
*rta
= u_arg
;
232 struct xfrm_algo
*p
, *ualg
;
233 struct xfrm_algo_desc
*algo
;
239 ualg
= RTA_DATA(rta
);
241 algo
= get_byname(ualg
->alg_name
, 1);
244 *props
= algo
->desc
.sadb_alg_id
;
246 len
= sizeof(*ualg
) + (ualg
->alg_key_len
+ 7U) / 8;
247 p
= kmalloc(len
, GFP_KERNEL
);
251 memcpy(p
, ualg
, len
);
252 strcpy(p
->alg_name
, algo
->name
);
257 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
259 struct rtattr
*rta
= u_arg
;
260 struct xfrm_encap_tmpl
*p
, *uencap
;
265 uencap
= RTA_DATA(rta
);
266 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
270 memcpy(p
, uencap
, sizeof(*p
));
276 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy
*xp
)
278 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
282 len
+= sizeof(struct xfrm_user_sec_ctx
);
283 len
+= xfrm_ctx
->ctx_len
;
288 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
290 struct xfrm_user_sec_ctx
*uctx
;
295 uctx
= RTA_DATA(u_arg
);
296 return security_xfrm_state_alloc(x
, uctx
);
299 static int attach_one_addr(xfrm_address_t
**addrpp
, struct rtattr
*u_arg
)
301 struct rtattr
*rta
= u_arg
;
302 xfrm_address_t
*p
, *uaddrp
;
307 uaddrp
= RTA_DATA(rta
);
308 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
312 memcpy(p
, uaddrp
, sizeof(*p
));
317 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
319 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
320 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
321 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
322 x
->props
.mode
= p
->mode
;
323 x
->props
.replay_window
= p
->replay_window
;
324 x
->props
.reqid
= p
->reqid
;
325 x
->props
.family
= p
->family
;
326 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
327 x
->props
.flags
= p
->flags
;
331 * someday when pfkey also has support, we could have the code
332 * somehow made shareable and move it to xfrm_state.c - JHS
335 static int xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
338 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
339 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
340 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
341 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
344 struct xfrm_replay_state
*replay
;
345 if (RTA_PAYLOAD(rp
) < sizeof(*replay
))
347 replay
= RTA_DATA(rp
);
348 memcpy(&x
->replay
, replay
, sizeof(*replay
));
349 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
353 struct xfrm_lifetime_cur
*ltime
;
354 if (RTA_PAYLOAD(lt
) < sizeof(*ltime
))
356 ltime
= RTA_DATA(lt
);
357 x
->curlft
.bytes
= ltime
->bytes
;
358 x
->curlft
.packets
= ltime
->packets
;
359 x
->curlft
.add_time
= ltime
->add_time
;
360 x
->curlft
.use_time
= ltime
->use_time
;
364 if (RTA_PAYLOAD(et
) < sizeof(u32
))
366 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
370 if (RTA_PAYLOAD(rt
) < sizeof(u32
))
372 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
380 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
381 struct rtattr
**xfrma
,
384 struct xfrm_state
*x
= xfrm_state_alloc();
390 copy_from_user_state(x
, p
);
392 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
393 xfrm_aalg_get_byname
,
394 xfrma
[XFRMA_ALG_AUTH
-1])))
396 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
397 xfrm_ealg_get_byname
,
398 xfrma
[XFRMA_ALG_CRYPT
-1])))
400 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
401 xfrm_calg_get_byname
,
402 xfrma
[XFRMA_ALG_COMP
-1])))
404 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
406 if ((err
= attach_one_addr(&x
->coaddr
, xfrma
[XFRMA_COADDR
-1])))
408 err
= xfrm_init_state(x
);
412 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
416 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
417 /* sysctl_xfrm_aevent_etime is in 100ms units */
418 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
419 x
->preplay
.bitmap
= 0;
420 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
421 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
423 /* override default values from above */
425 err
= xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
432 x
->km
.state
= XFRM_STATE_DEAD
;
439 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
441 struct xfrm_usersa_info
*p
= NLMSG_DATA(nlh
);
442 struct xfrm_state
*x
;
446 err
= verify_newsa_info(p
, (struct rtattr
**)xfrma
);
450 x
= xfrm_state_construct(p
, (struct rtattr
**)xfrma
, &err
);
455 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
456 err
= xfrm_state_add(x
);
458 err
= xfrm_state_update(x
);
461 x
->km
.state
= XFRM_STATE_DEAD
;
466 c
.seq
= nlh
->nlmsg_seq
;
467 c
.pid
= nlh
->nlmsg_pid
;
468 c
.event
= nlh
->nlmsg_type
;
470 km_state_notify(x
, &c
);
476 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
477 struct rtattr
**xfrma
,
480 struct xfrm_state
*x
= NULL
;
483 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
485 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
487 xfrm_address_t
*saddr
= NULL
;
489 err
= verify_one_addr(xfrma
, XFRMA_SRCADDR
, &saddr
);
499 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
509 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
511 struct xfrm_state
*x
;
514 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
516 x
= xfrm_user_state_lookup(p
, (struct rtattr
**)xfrma
, &err
);
520 if ((err
= security_xfrm_state_delete(x
)) != 0)
523 if (xfrm_state_kern(x
)) {
528 err
= xfrm_state_delete(x
);
532 c
.seq
= nlh
->nlmsg_seq
;
533 c
.pid
= nlh
->nlmsg_pid
;
534 c
.event
= nlh
->nlmsg_type
;
535 km_state_notify(x
, &c
);
542 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
544 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
545 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
546 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
547 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
548 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
549 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
550 p
->mode
= x
->props
.mode
;
551 p
->replay_window
= x
->props
.replay_window
;
552 p
->reqid
= x
->props
.reqid
;
553 p
->family
= x
->props
.family
;
554 p
->flags
= x
->props
.flags
;
558 struct xfrm_dump_info
{
559 struct sk_buff
*in_skb
;
560 struct sk_buff
*out_skb
;
567 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
569 struct xfrm_dump_info
*sp
= ptr
;
570 struct sk_buff
*in_skb
= sp
->in_skb
;
571 struct sk_buff
*skb
= sp
->out_skb
;
572 struct xfrm_usersa_info
*p
;
573 struct nlmsghdr
*nlh
;
574 unsigned char *b
= skb
->tail
;
576 if (sp
->this_idx
< sp
->start_idx
)
579 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
581 XFRM_MSG_NEWSA
, sizeof(*p
));
582 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
585 copy_to_user_state(x
, p
);
588 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
589 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
591 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
592 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
594 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
597 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
600 int ctx_size
= sizeof(struct xfrm_sec_ctx
) +
601 x
->security
->ctx_len
;
602 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
603 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
605 uctx
->exttype
= XFRMA_SEC_CTX
;
606 uctx
->len
= ctx_size
;
607 uctx
->ctx_doi
= x
->security
->ctx_doi
;
608 uctx
->ctx_alg
= x
->security
->ctx_alg
;
609 uctx
->ctx_len
= x
->security
->ctx_len
;
610 memcpy(uctx
+ 1, x
->security
->ctx_str
, x
->security
->ctx_len
);
614 RTA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
617 RTA_PUT(skb
, XFRMA_LASTUSED
, sizeof(x
->lastused
), &x
->lastused
);
619 nlh
->nlmsg_len
= skb
->tail
- b
;
626 skb_trim(skb
, b
- skb
->data
);
630 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
632 struct xfrm_dump_info info
;
634 info
.in_skb
= cb
->skb
;
636 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
637 info
.nlmsg_flags
= NLM_F_MULTI
;
639 info
.start_idx
= cb
->args
[0];
640 (void) xfrm_state_walk(0, dump_one_state
, &info
);
641 cb
->args
[0] = info
.this_idx
;
646 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
647 struct xfrm_state
*x
, u32 seq
)
649 struct xfrm_dump_info info
;
652 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_ATOMIC
);
654 return ERR_PTR(-ENOMEM
);
656 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
657 info
.in_skb
= in_skb
;
659 info
.nlmsg_seq
= seq
;
660 info
.nlmsg_flags
= 0;
661 info
.this_idx
= info
.start_idx
= 0;
663 if (dump_one_state(x
, 0, &info
)) {
671 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
673 struct xfrm_usersa_id
*p
= NLMSG_DATA(nlh
);
674 struct xfrm_state
*x
;
675 struct sk_buff
*resp_skb
;
678 x
= xfrm_user_state_lookup(p
, (struct rtattr
**)xfrma
, &err
);
682 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
683 if (IS_ERR(resp_skb
)) {
684 err
= PTR_ERR(resp_skb
);
686 err
= netlink_unicast(xfrm_nl
, resp_skb
,
687 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
694 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
696 switch (p
->info
.id
.proto
) {
702 /* IPCOMP spi is 16-bits. */
703 if (p
->max
>= 0x10000)
717 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
719 struct xfrm_state
*x
;
720 struct xfrm_userspi_info
*p
;
721 struct sk_buff
*resp_skb
;
722 xfrm_address_t
*daddr
;
727 err
= verify_userspi_info(p
);
731 family
= p
->info
.family
;
732 daddr
= &p
->info
.id
.daddr
;
736 x
= xfrm_find_acq_byseq(p
->info
.seq
);
737 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
744 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
745 p
->info
.id
.proto
, daddr
,
752 resp_skb
= ERR_PTR(-ENOENT
);
754 spin_lock_bh(&x
->lock
);
755 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
756 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
758 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
760 spin_unlock_bh(&x
->lock
);
762 if (IS_ERR(resp_skb
)) {
763 err
= PTR_ERR(resp_skb
);
767 err
= netlink_unicast(xfrm_nl
, resp_skb
,
768 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
776 static int verify_policy_dir(__u8 dir
)
780 case XFRM_POLICY_OUT
:
781 case XFRM_POLICY_FWD
:
791 static int verify_policy_type(__u8 type
)
794 case XFRM_POLICY_TYPE_MAIN
:
795 #ifdef CONFIG_XFRM_SUB_POLICY
796 case XFRM_POLICY_TYPE_SUB
:
807 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
811 case XFRM_SHARE_SESSION
:
812 case XFRM_SHARE_USER
:
813 case XFRM_SHARE_UNIQUE
:
821 case XFRM_POLICY_ALLOW
:
822 case XFRM_POLICY_BLOCK
:
829 switch (p
->sel
.family
) {
834 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
837 return -EAFNOSUPPORT
;
844 return verify_policy_dir(p
->dir
);
847 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
849 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
850 struct xfrm_user_sec_ctx
*uctx
;
856 return security_xfrm_policy_alloc(pol
, uctx
);
859 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
865 for (i
= 0; i
< nr
; i
++, ut
++) {
866 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
868 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
869 memcpy(&t
->saddr
, &ut
->saddr
,
870 sizeof(xfrm_address_t
));
871 t
->reqid
= ut
->reqid
;
873 t
->share
= ut
->share
;
874 t
->optional
= ut
->optional
;
875 t
->aalgos
= ut
->aalgos
;
876 t
->ealgos
= ut
->ealgos
;
877 t
->calgos
= ut
->calgos
;
881 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
883 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
884 struct xfrm_user_tmpl
*utmpl
;
890 nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
892 if (nr
> XFRM_MAX_DEPTH
)
895 copy_templates(pol
, RTA_DATA(rt
), nr
);
900 static int copy_from_user_policy_type(u8
*tp
, struct rtattr
**xfrma
)
902 struct rtattr
*rt
= xfrma
[XFRMA_POLICY_TYPE
-1];
903 struct xfrm_userpolicy_type
*upt
;
904 __u8 type
= XFRM_POLICY_TYPE_MAIN
;
908 if (rt
->rta_len
< sizeof(*upt
))
915 err
= verify_policy_type(type
);
923 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
925 xp
->priority
= p
->priority
;
926 xp
->index
= p
->index
;
927 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
928 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
929 xp
->action
= p
->action
;
930 xp
->flags
= p
->flags
;
931 xp
->family
= p
->sel
.family
;
932 /* XXX xp->share = p->share; */
935 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
937 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
938 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
939 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
940 p
->priority
= xp
->priority
;
941 p
->index
= xp
->index
;
942 p
->sel
.family
= xp
->family
;
944 p
->action
= xp
->action
;
945 p
->flags
= xp
->flags
;
946 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
949 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
951 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
959 copy_from_user_policy(xp
, p
);
961 err
= copy_from_user_policy_type(&xp
->type
, xfrma
);
965 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
966 err
= copy_from_user_sec_ctx(xp
, xfrma
);
977 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
979 struct xfrm_userpolicy_info
*p
= NLMSG_DATA(nlh
);
980 struct xfrm_policy
*xp
;
985 err
= verify_newpolicy_info(p
);
988 err
= verify_sec_ctx_len((struct rtattr
**)xfrma
);
992 xp
= xfrm_policy_construct(p
, (struct rtattr
**)xfrma
, &err
);
996 /* shouldnt excl be based on nlh flags??
997 * Aha! this is anti-netlink really i.e more pfkey derived
998 * in netlink excl is a flag and you wouldnt need
999 * a type XFRM_MSG_UPDPOLICY - JHS */
1000 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1001 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1003 security_xfrm_policy_free(xp
);
1008 c
.event
= nlh
->nlmsg_type
;
1009 c
.seq
= nlh
->nlmsg_seq
;
1010 c
.pid
= nlh
->nlmsg_pid
;
1011 km_policy_notify(xp
, p
->dir
, &c
);
1018 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1020 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1023 if (xp
->xfrm_nr
== 0)
1026 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1027 struct xfrm_user_tmpl
*up
= &vec
[i
];
1028 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1030 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1031 up
->family
= xp
->family
;
1032 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1033 up
->reqid
= kp
->reqid
;
1034 up
->mode
= kp
->mode
;
1035 up
->share
= kp
->share
;
1036 up
->optional
= kp
->optional
;
1037 up
->aalgos
= kp
->aalgos
;
1038 up
->ealgos
= kp
->ealgos
;
1039 up
->calgos
= kp
->calgos
;
1041 RTA_PUT(skb
, XFRMA_TMPL
,
1042 (sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
),
1051 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
1053 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
1054 struct rtattr
*rt
= __RTA_PUT(skb
, XFRMA_SEC_CTX
, ctx_size
);
1055 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1057 uctx
->exttype
= XFRMA_SEC_CTX
;
1058 uctx
->len
= ctx_size
;
1059 uctx
->ctx_doi
= s
->ctx_doi
;
1060 uctx
->ctx_alg
= s
->ctx_alg
;
1061 uctx
->ctx_len
= s
->ctx_len
;
1062 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
1069 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1072 return copy_sec_ctx(x
->security
, skb
);
1077 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1080 return copy_sec_ctx(xp
->security
, skb
);
1085 #ifdef CONFIG_XFRM_SUB_POLICY
1086 static int copy_to_user_policy_type(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1088 struct xfrm_userpolicy_type upt
;
1090 memset(&upt
, 0, sizeof(upt
));
1091 upt
.type
= xp
->type
;
1093 RTA_PUT(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1102 static inline int copy_to_user_policy_type(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1108 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1110 struct xfrm_dump_info
*sp
= ptr
;
1111 struct xfrm_userpolicy_info
*p
;
1112 struct sk_buff
*in_skb
= sp
->in_skb
;
1113 struct sk_buff
*skb
= sp
->out_skb
;
1114 struct nlmsghdr
*nlh
;
1115 unsigned char *b
= skb
->tail
;
1117 if (sp
->this_idx
< sp
->start_idx
)
1120 nlh
= NLMSG_PUT(skb
, NETLINK_CB(in_skb
).pid
,
1122 XFRM_MSG_NEWPOLICY
, sizeof(*p
));
1123 p
= NLMSG_DATA(nlh
);
1124 nlh
->nlmsg_flags
= sp
->nlmsg_flags
;
1126 copy_to_user_policy(xp
, p
, dir
);
1127 if (copy_to_user_tmpl(xp
, skb
) < 0)
1129 if (copy_to_user_sec_ctx(xp
, skb
))
1131 if (copy_to_user_policy_type(xp
, skb
) < 0)
1134 nlh
->nlmsg_len
= skb
->tail
- b
;
1140 skb_trim(skb
, b
- skb
->data
);
1144 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1146 struct xfrm_dump_info info
;
1148 info
.in_skb
= cb
->skb
;
1150 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1151 info
.nlmsg_flags
= NLM_F_MULTI
;
1153 info
.start_idx
= cb
->args
[0];
1154 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1155 #ifdef CONFIG_XFRM_SUB_POLICY
1156 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1158 cb
->args
[0] = info
.this_idx
;
1163 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1164 struct xfrm_policy
*xp
,
1167 struct xfrm_dump_info info
;
1168 struct sk_buff
*skb
;
1170 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
1172 return ERR_PTR(-ENOMEM
);
1174 NETLINK_CB(skb
).dst_pid
= NETLINK_CB(in_skb
).pid
;
1175 info
.in_skb
= in_skb
;
1177 info
.nlmsg_seq
= seq
;
1178 info
.nlmsg_flags
= 0;
1179 info
.this_idx
= info
.start_idx
= 0;
1181 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1189 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1191 struct xfrm_policy
*xp
;
1192 struct xfrm_userpolicy_id
*p
;
1193 __u8 type
= XFRM_POLICY_TYPE_MAIN
;
1198 p
= NLMSG_DATA(nlh
);
1199 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1201 err
= copy_from_user_policy_type(&type
, (struct rtattr
**)xfrma
);
1205 err
= verify_policy_dir(p
->dir
);
1210 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete);
1212 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1213 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1214 struct xfrm_policy tmp
;
1216 err
= verify_sec_ctx_len(rtattrs
);
1220 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1222 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1224 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1227 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
, delete);
1228 security_xfrm_policy_free(&tmp
);
1234 struct sk_buff
*resp_skb
;
1236 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1237 if (IS_ERR(resp_skb
)) {
1238 err
= PTR_ERR(resp_skb
);
1240 err
= netlink_unicast(xfrm_nl
, resp_skb
,
1241 NETLINK_CB(skb
).pid
,
1245 if ((err
= security_xfrm_policy_delete(xp
)) != 0)
1247 c
.data
.byid
= p
->index
;
1248 c
.event
= nlh
->nlmsg_type
;
1249 c
.seq
= nlh
->nlmsg_seq
;
1250 c
.pid
= nlh
->nlmsg_pid
;
1251 km_policy_notify(xp
, p
->dir
, &c
);
1260 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1263 struct xfrm_usersa_flush
*p
= NLMSG_DATA(nlh
);
1265 xfrm_state_flush(p
->proto
);
1266 c
.data
.proto
= p
->proto
;
1267 c
.event
= nlh
->nlmsg_type
;
1268 c
.seq
= nlh
->nlmsg_seq
;
1269 c
.pid
= nlh
->nlmsg_pid
;
1270 km_state_notify(NULL
, &c
);
1276 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1278 struct xfrm_aevent_id
*id
;
1279 struct nlmsghdr
*nlh
;
1280 struct xfrm_lifetime_cur ltime
;
1281 unsigned char *b
= skb
->tail
;
1283 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
));
1284 id
= NLMSG_DATA(nlh
);
1285 nlh
->nlmsg_flags
= 0;
1287 id
->sa_id
.daddr
= x
->id
.daddr
;
1288 id
->sa_id
.spi
= x
->id
.spi
;
1289 id
->sa_id
.family
= x
->props
.family
;
1290 id
->sa_id
.proto
= x
->id
.proto
;
1291 id
->flags
= c
->data
.aevent
;
1293 RTA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1295 ltime
.bytes
= x
->curlft
.bytes
;
1296 ltime
.packets
= x
->curlft
.packets
;
1297 ltime
.add_time
= x
->curlft
.add_time
;
1298 ltime
.use_time
= x
->curlft
.use_time
;
1300 RTA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(struct xfrm_lifetime_cur
), <ime
);
1302 if (id
->flags
&XFRM_AE_RTHR
) {
1303 RTA_PUT(skb
,XFRMA_REPLAY_THRESH
,sizeof(u32
),&x
->replay_maxdiff
);
1306 if (id
->flags
&XFRM_AE_ETHR
) {
1307 u32 etimer
= x
->replay_maxage
*10/HZ
;
1308 RTA_PUT(skb
,XFRMA_ETIMER_THRESH
,sizeof(u32
),&etimer
);
1311 nlh
->nlmsg_len
= skb
->tail
- b
;
1316 skb_trim(skb
, b
- skb
->data
);
1320 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1322 struct xfrm_state
*x
;
1323 struct sk_buff
*r_skb
;
1326 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1327 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1328 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1330 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1331 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1333 if (p
->flags
&XFRM_AE_RTHR
)
1334 len
+=RTA_SPACE(sizeof(u32
));
1336 if (p
->flags
&XFRM_AE_ETHR
)
1337 len
+=RTA_SPACE(sizeof(u32
));
1339 r_skb
= alloc_skb(len
, GFP_ATOMIC
);
1343 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1350 * XXX: is this lock really needed - none of the other
1351 * gets lock (the concern is things getting updated
1352 * while we are still reading) - jhs
1354 spin_lock_bh(&x
->lock
);
1355 c
.data
.aevent
= p
->flags
;
1356 c
.seq
= nlh
->nlmsg_seq
;
1357 c
.pid
= nlh
->nlmsg_pid
;
1359 if (build_aevent(r_skb
, x
, &c
) < 0)
1361 err
= netlink_unicast(xfrm_nl
, r_skb
,
1362 NETLINK_CB(skb
).pid
, MSG_DONTWAIT
);
1363 spin_unlock_bh(&x
->lock
);
1368 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1370 struct xfrm_state
*x
;
1373 struct xfrm_aevent_id
*p
= NLMSG_DATA(nlh
);
1374 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1375 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1380 /* pedantic mode - thou shalt sayeth replaceth */
1381 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1384 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1388 if (x
->km
.state
!= XFRM_STATE_VALID
)
1391 spin_lock_bh(&x
->lock
);
1392 err
= xfrm_update_ae_params(x
,(struct rtattr
**)xfrma
);
1393 spin_unlock_bh(&x
->lock
);
1397 c
.event
= nlh
->nlmsg_type
;
1398 c
.seq
= nlh
->nlmsg_seq
;
1399 c
.pid
= nlh
->nlmsg_pid
;
1400 c
.data
.aevent
= XFRM_AE_CU
;
1401 km_state_notify(x
, &c
);
1408 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1411 __u8 type
= XFRM_POLICY_TYPE_MAIN
;
1414 err
= copy_from_user_policy_type(&type
, (struct rtattr
**)xfrma
);
1418 xfrm_policy_flush(type
);
1420 c
.event
= nlh
->nlmsg_type
;
1421 c
.seq
= nlh
->nlmsg_seq
;
1422 c
.pid
= nlh
->nlmsg_pid
;
1423 km_policy_notify(NULL
, 0, &c
);
1427 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1429 struct xfrm_policy
*xp
;
1430 struct xfrm_user_polexpire
*up
= NLMSG_DATA(nlh
);
1431 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1432 __u8 type
= XFRM_POLICY_TYPE_MAIN
;
1435 err
= copy_from_user_policy_type(&type
, (struct rtattr
**)xfrma
);
1440 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0);
1442 struct rtattr
**rtattrs
= (struct rtattr
**)xfrma
;
1443 struct rtattr
*rt
= rtattrs
[XFRMA_SEC_CTX
-1];
1444 struct xfrm_policy tmp
;
1446 err
= verify_sec_ctx_len(rtattrs
);
1450 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1452 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1454 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1457 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
, 0);
1458 security_xfrm_policy_free(&tmp
);
1463 read_lock(&xp
->lock
);
1465 read_unlock(&xp
->lock
);
1469 read_unlock(&xp
->lock
);
1472 xfrm_policy_delete(xp
, p
->dir
);
1474 // reset the timers here?
1475 printk("Dont know what to do with soft policy expire\n");
1477 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1484 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1486 struct xfrm_state
*x
;
1488 struct xfrm_user_expire
*ue
= NLMSG_DATA(nlh
);
1489 struct xfrm_usersa_info
*p
= &ue
->state
;
1491 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1499 spin_lock_bh(&x
->lock
);
1500 if (x
->km
.state
!= XFRM_STATE_VALID
)
1502 km_state_expired(x
, ue
->hard
, current
->pid
);
1505 __xfrm_state_delete(x
);
1507 spin_unlock_bh(&x
->lock
);
1512 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void **xfrma
)
1514 struct xfrm_policy
*xp
;
1515 struct xfrm_user_tmpl
*ut
;
1517 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1519 struct xfrm_user_acquire
*ua
= NLMSG_DATA(nlh
);
1520 struct xfrm_state
*x
= xfrm_state_alloc();
1526 err
= verify_newpolicy_info(&ua
->policy
);
1528 printk("BAD policy passed\n");
1534 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
); if (!xp
) {
1539 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1540 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1541 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1544 /* extract the templates and for each call km_key */
1545 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1546 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1547 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1548 x
->props
.mode
= t
->mode
;
1549 x
->props
.reqid
= t
->reqid
;
1550 x
->props
.family
= ut
->family
;
1551 t
->aalgos
= ua
->aalgos
;
1552 t
->ealgos
= ua
->ealgos
;
1553 t
->calgos
= ua
->calgos
;
1554 err
= km_query(x
, t
, xp
);
1565 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1567 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1568 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1569 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1570 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1571 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1572 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1573 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1574 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1575 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1576 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1577 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1578 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1579 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1580 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1581 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
1582 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1583 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1584 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1589 static struct xfrm_link
{
1590 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, void **);
1591 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1592 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1593 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1594 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1595 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1596 .dump
= xfrm_dump_sa
},
1597 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1598 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1599 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1600 .dump
= xfrm_dump_policy
},
1601 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1602 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1603 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1604 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1605 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1606 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1607 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1608 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1609 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1610 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1613 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
1615 struct rtattr
*xfrma
[XFRMA_MAX
];
1616 struct xfrm_link
*link
;
1619 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
1622 type
= nlh
->nlmsg_type
;
1624 /* A control message: ignore them */
1625 if (type
< XFRM_MSG_BASE
)
1628 /* Unknown message: reply with EINVAL */
1629 if (type
> XFRM_MSG_MAX
)
1632 type
-= XFRM_MSG_BASE
;
1633 link
= &xfrm_dispatch
[type
];
1635 /* All operations require privileges, even GET */
1636 if (security_netlink_recv(skb
, CAP_NET_ADMIN
)) {
1641 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1642 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1643 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1644 if (link
->dump
== NULL
)
1647 if ((*errp
= netlink_dump_start(xfrm_nl
, skb
, nlh
,
1648 link
->dump
, NULL
)) != 0) {
1652 netlink_queue_skip(nlh
, skb
);
1656 memset(xfrma
, 0, sizeof(xfrma
));
1658 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1661 if (nlh
->nlmsg_len
> min_len
) {
1662 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1663 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1665 while (RTA_OK(attr
, attrlen
)) {
1666 unsigned short flavor
= attr
->rta_type
;
1668 if (flavor
> XFRMA_MAX
)
1670 xfrma
[flavor
- 1] = attr
;
1672 attr
= RTA_NEXT(attr
, attrlen
);
1676 if (link
->doit
== NULL
)
1678 *errp
= link
->doit(skb
, nlh
, (void **) &xfrma
);
1687 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1689 unsigned int qlen
= 0;
1692 mutex_lock(&xfrm_cfg_mutex
);
1693 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1694 mutex_unlock(&xfrm_cfg_mutex
);
1699 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1701 struct xfrm_user_expire
*ue
;
1702 struct nlmsghdr
*nlh
;
1703 unsigned char *b
= skb
->tail
;
1705 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
,
1707 ue
= NLMSG_DATA(nlh
);
1708 nlh
->nlmsg_flags
= 0;
1710 copy_to_user_state(x
, &ue
->state
);
1711 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1713 nlh
->nlmsg_len
= skb
->tail
- b
;
1717 skb_trim(skb
, b
- skb
->data
);
1721 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1723 struct sk_buff
*skb
;
1724 int len
= NLMSG_LENGTH(sizeof(struct xfrm_user_expire
));
1726 skb
= alloc_skb(len
, GFP_ATOMIC
);
1730 if (build_expire(skb
, x
, c
) < 0)
1733 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
1734 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1737 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1739 struct sk_buff
*skb
;
1740 int len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1742 len
+= RTA_SPACE(sizeof(struct xfrm_replay_state
));
1743 len
+= RTA_SPACE(sizeof(struct xfrm_lifetime_cur
));
1744 skb
= alloc_skb(len
, GFP_ATOMIC
);
1748 if (build_aevent(skb
, x
, c
) < 0)
1751 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_AEVENTS
;
1752 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1755 static int xfrm_notify_sa_flush(struct km_event
*c
)
1757 struct xfrm_usersa_flush
*p
;
1758 struct nlmsghdr
*nlh
;
1759 struct sk_buff
*skb
;
1761 int len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1763 skb
= alloc_skb(len
, GFP_ATOMIC
);
1768 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
,
1769 XFRM_MSG_FLUSHSA
, sizeof(*p
));
1770 nlh
->nlmsg_flags
= 0;
1772 p
= NLMSG_DATA(nlh
);
1773 p
->proto
= c
->data
.proto
;
1775 nlh
->nlmsg_len
= skb
->tail
- b
;
1777 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1778 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1785 static int inline xfrm_sa_len(struct xfrm_state
*x
)
1789 l
+= RTA_SPACE(sizeof(*x
->aalg
) + (x
->aalg
->alg_key_len
+7)/8);
1791 l
+= RTA_SPACE(sizeof(*x
->ealg
) + (x
->ealg
->alg_key_len
+7)/8);
1793 l
+= RTA_SPACE(sizeof(*x
->calg
));
1795 l
+= RTA_SPACE(sizeof(*x
->encap
));
1800 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1802 struct xfrm_usersa_info
*p
;
1803 struct xfrm_usersa_id
*id
;
1804 struct nlmsghdr
*nlh
;
1805 struct sk_buff
*skb
;
1807 int len
= xfrm_sa_len(x
);
1810 headlen
= sizeof(*p
);
1811 if (c
->event
== XFRM_MSG_DELSA
) {
1812 len
+= RTA_SPACE(headlen
);
1813 headlen
= sizeof(*id
);
1815 len
+= NLMSG_SPACE(headlen
);
1817 skb
= alloc_skb(len
, GFP_ATOMIC
);
1822 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
1823 nlh
->nlmsg_flags
= 0;
1825 p
= NLMSG_DATA(nlh
);
1826 if (c
->event
== XFRM_MSG_DELSA
) {
1827 id
= NLMSG_DATA(nlh
);
1828 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
1829 id
->spi
= x
->id
.spi
;
1830 id
->family
= x
->props
.family
;
1831 id
->proto
= x
->id
.proto
;
1833 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_SA
, sizeof(*p
)));
1836 copy_to_user_state(x
, p
);
1839 RTA_PUT(skb
, XFRMA_ALG_AUTH
,
1840 sizeof(*(x
->aalg
))+(x
->aalg
->alg_key_len
+7)/8, x
->aalg
);
1842 RTA_PUT(skb
, XFRMA_ALG_CRYPT
,
1843 sizeof(*(x
->ealg
))+(x
->ealg
->alg_key_len
+7)/8, x
->ealg
);
1845 RTA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
1848 RTA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
1850 nlh
->nlmsg_len
= skb
->tail
- b
;
1852 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_SA
;
1853 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1861 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1865 case XFRM_MSG_EXPIRE
:
1866 return xfrm_exp_state_notify(x
, c
);
1867 case XFRM_MSG_NEWAE
:
1868 return xfrm_aevent_state_notify(x
, c
);
1869 case XFRM_MSG_DELSA
:
1870 case XFRM_MSG_UPDSA
:
1871 case XFRM_MSG_NEWSA
:
1872 return xfrm_notify_sa(x
, c
);
1873 case XFRM_MSG_FLUSHSA
:
1874 return xfrm_notify_sa_flush(c
);
1876 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
1884 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
1885 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
1888 struct xfrm_user_acquire
*ua
;
1889 struct nlmsghdr
*nlh
;
1890 unsigned char *b
= skb
->tail
;
1891 __u32 seq
= xfrm_get_acqseq();
1893 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_ACQUIRE
,
1895 ua
= NLMSG_DATA(nlh
);
1896 nlh
->nlmsg_flags
= 0;
1898 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
1899 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
1900 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
1901 copy_to_user_policy(xp
, &ua
->policy
, dir
);
1902 ua
->aalgos
= xt
->aalgos
;
1903 ua
->ealgos
= xt
->ealgos
;
1904 ua
->calgos
= xt
->calgos
;
1905 ua
->seq
= x
->km
.seq
= seq
;
1907 if (copy_to_user_tmpl(xp
, skb
) < 0)
1909 if (copy_to_user_state_sec_ctx(x
, skb
))
1911 if (copy_to_user_policy_type(xp
, skb
) < 0)
1914 nlh
->nlmsg_len
= skb
->tail
- b
;
1918 skb_trim(skb
, b
- skb
->data
);
1922 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
1923 struct xfrm_policy
*xp
, int dir
)
1925 struct sk_buff
*skb
;
1928 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
1929 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_acquire
));
1930 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
1931 #ifdef CONFIG_XFRM_SUB_POLICY
1932 len
+= RTA_SPACE(sizeof(struct xfrm_userpolicy_type
));
1934 skb
= alloc_skb(len
, GFP_ATOMIC
);
1938 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
1941 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_ACQUIRE
;
1942 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
1945 /* User gives us xfrm_user_policy_info followed by an array of 0
1946 * or more templates.
1948 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
1949 u8
*data
, int len
, int *dir
)
1951 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
1952 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
1953 struct xfrm_policy
*xp
;
1956 switch (sk
->sk_family
) {
1958 if (opt
!= IP_XFRM_POLICY
) {
1963 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1965 if (opt
!= IPV6_XFRM_POLICY
) {
1978 if (len
< sizeof(*p
) ||
1979 verify_newpolicy_info(p
))
1982 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
1983 if (nr
> XFRM_MAX_DEPTH
)
1986 if (p
->dir
> XFRM_POLICY_OUT
)
1989 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1995 copy_from_user_policy(xp
, p
);
1996 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
1997 copy_templates(xp
, ut
, nr
);
2004 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2005 int dir
, struct km_event
*c
)
2007 struct xfrm_user_polexpire
*upe
;
2008 struct nlmsghdr
*nlh
;
2009 int hard
= c
->data
.hard
;
2010 unsigned char *b
= skb
->tail
;
2012 nlh
= NLMSG_PUT(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
));
2013 upe
= NLMSG_DATA(nlh
);
2014 nlh
->nlmsg_flags
= 0;
2016 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2017 if (copy_to_user_tmpl(xp
, skb
) < 0)
2019 if (copy_to_user_sec_ctx(xp
, skb
))
2021 if (copy_to_user_policy_type(xp
, skb
) < 0)
2025 nlh
->nlmsg_len
= skb
->tail
- b
;
2029 skb_trim(skb
, b
- skb
->data
);
2033 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2035 struct sk_buff
*skb
;
2038 len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2039 len
+= NLMSG_SPACE(sizeof(struct xfrm_user_polexpire
));
2040 len
+= RTA_SPACE(xfrm_user_sec_ctx_size(xp
));
2041 #ifdef CONFIG_XFRM_SUB_POLICY
2042 len
+= RTA_SPACE(sizeof(struct xfrm_userpolicy_type
));
2044 skb
= alloc_skb(len
, GFP_ATOMIC
);
2048 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2051 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_EXPIRE
;
2052 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2055 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2057 struct xfrm_userpolicy_info
*p
;
2058 struct xfrm_userpolicy_id
*id
;
2059 struct nlmsghdr
*nlh
;
2060 struct sk_buff
*skb
;
2062 int len
= RTA_SPACE(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2065 headlen
= sizeof(*p
);
2066 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2067 len
+= RTA_SPACE(headlen
);
2068 headlen
= sizeof(*id
);
2070 #ifdef CONFIG_XFRM_SUB_POLICY
2071 len
+= RTA_SPACE(sizeof(struct xfrm_userpolicy_type
));
2073 len
+= NLMSG_SPACE(headlen
);
2075 skb
= alloc_skb(len
, GFP_ATOMIC
);
2080 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, c
->event
, headlen
);
2082 p
= NLMSG_DATA(nlh
);
2083 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2084 id
= NLMSG_DATA(nlh
);
2085 memset(id
, 0, sizeof(*id
));
2088 id
->index
= xp
->index
;
2090 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2092 p
= RTA_DATA(__RTA_PUT(skb
, XFRMA_POLICY
, sizeof(*p
)));
2095 nlh
->nlmsg_flags
= 0;
2097 copy_to_user_policy(xp
, p
, dir
);
2098 if (copy_to_user_tmpl(xp
, skb
) < 0)
2100 if (copy_to_user_policy_type(xp
, skb
) < 0)
2103 nlh
->nlmsg_len
= skb
->tail
- b
;
2105 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
2106 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2114 static int xfrm_notify_policy_flush(struct km_event
*c
)
2116 struct nlmsghdr
*nlh
;
2117 struct sk_buff
*skb
;
2120 #ifdef CONFIG_XFRM_SUB_POLICY
2121 struct xfrm_userpolicy_type upt
;
2122 len
+= RTA_SPACE(sizeof(struct xfrm_userpolicy_type
));
2124 len
+= NLMSG_LENGTH(0);
2126 skb
= alloc_skb(len
, GFP_ATOMIC
);
2132 nlh
= NLMSG_PUT(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0);
2133 nlh
->nlmsg_flags
= 0;
2135 #ifdef CONFIG_XFRM_SUB_POLICY
2136 memset(&upt
, 0, sizeof(upt
));
2137 upt
.type
= c
->data
.type
;
2138 RTA_PUT(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
2141 nlh
->nlmsg_len
= skb
->tail
- b
;
2143 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_POLICY
;
2144 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2147 #ifdef CONFIG_XFRM_SUB_POLICY
2154 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2158 case XFRM_MSG_NEWPOLICY
:
2159 case XFRM_MSG_UPDPOLICY
:
2160 case XFRM_MSG_DELPOLICY
:
2161 return xfrm_notify_policy(xp
, dir
, c
);
2162 case XFRM_MSG_FLUSHPOLICY
:
2163 return xfrm_notify_policy_flush(c
);
2164 case XFRM_MSG_POLEXPIRE
:
2165 return xfrm_exp_policy_notify(xp
, dir
, c
);
2167 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2174 static int build_report(struct sk_buff
*skb
, u8 proto
,
2175 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2177 struct xfrm_user_report
*ur
;
2178 struct nlmsghdr
*nlh
;
2179 unsigned char *b
= skb
->tail
;
2181 nlh
= NLMSG_PUT(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
));
2182 ur
= NLMSG_DATA(nlh
);
2183 nlh
->nlmsg_flags
= 0;
2186 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2189 RTA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2191 nlh
->nlmsg_len
= skb
->tail
- b
;
2196 skb_trim(skb
, b
- skb
->data
);
2200 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2201 xfrm_address_t
*addr
)
2203 struct sk_buff
*skb
;
2206 len
= NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report
)));
2207 skb
= alloc_skb(len
, GFP_ATOMIC
);
2211 if (build_report(skb
, proto
, sel
, addr
) < 0)
2214 NETLINK_CB(skb
).dst_group
= XFRMNLGRP_REPORT
;
2215 return netlink_broadcast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2218 static struct xfrm_mgr netlink_mgr
= {
2220 .notify
= xfrm_send_state_notify
,
2221 .acquire
= xfrm_send_acquire
,
2222 .compile_policy
= xfrm_compile_policy
,
2223 .notify_policy
= xfrm_send_policy_notify
,
2224 .report
= xfrm_send_report
,
2227 static int __init
xfrm_user_init(void)
2231 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2233 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2234 xfrm_netlink_rcv
, THIS_MODULE
);
2237 rcu_assign_pointer(xfrm_nl
, nlsk
);
2239 xfrm_register_km(&netlink_mgr
);
2244 static void __exit
xfrm_user_exit(void)
2246 struct sock
*nlsk
= xfrm_nl
;
2248 xfrm_unregister_km(&netlink_mgr
);
2249 rcu_assign_pointer(xfrm_nl
, NULL
);
2251 sock_release(nlsk
->sk_socket
);
2254 module_init(xfrm_user_init
);
2255 module_exit(xfrm_user_exit
);
2256 MODULE_LICENSE("GPL");
2257 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);