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>
34 #include <linux/audit.h>
36 static inline int alg_len(struct xfrm_algo
*alg
)
38 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
41 static int verify_one_alg(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
)
43 struct rtattr
*rt
= xfrma
[type
- 1];
44 struct xfrm_algo
*algp
;
50 len
= (rt
->rta_len
- sizeof(*rt
)) - sizeof(*algp
);
56 len
-= (algp
->alg_key_len
+ 7U) / 8;
62 if (!algp
->alg_key_len
&&
63 strcmp(algp
->alg_name
, "digest_null") != 0)
68 if (!algp
->alg_key_len
&&
69 strcmp(algp
->alg_name
, "cipher_null") != 0)
74 /* Zero length keys are legal. */
81 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
85 static int verify_encap_tmpl(struct rtattr
**xfrma
)
87 struct rtattr
*rt
= xfrma
[XFRMA_ENCAP
- 1];
88 struct xfrm_encap_tmpl
*encap
;
93 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*encap
))
99 static int verify_one_addr(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
,
100 xfrm_address_t
**addrp
)
102 struct rtattr
*rt
= xfrma
[type
- 1];
107 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(**addrp
))
111 *addrp
= RTA_DATA(rt
);
116 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
118 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
119 struct xfrm_user_sec_ctx
*uctx
;
125 if (rt
->rta_len
< sizeof(*uctx
))
130 len
+= sizeof(struct xfrm_user_sec_ctx
);
131 len
+= uctx
->ctx_len
;
133 if (uctx
->len
!= len
)
140 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
141 struct rtattr
**xfrma
)
151 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
163 switch (p
->id
.proto
) {
165 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
166 xfrma
[XFRMA_ALG_CRYPT
-1] ||
167 xfrma
[XFRMA_ALG_COMP
-1])
172 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
173 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
174 xfrma
[XFRMA_ALG_COMP
-1])
179 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
180 xfrma
[XFRMA_ALG_AUTH
-1] ||
181 xfrma
[XFRMA_ALG_CRYPT
-1])
185 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS
:
187 case IPPROTO_ROUTING
:
188 if (xfrma
[XFRMA_ALG_COMP
-1] ||
189 xfrma
[XFRMA_ALG_AUTH
-1] ||
190 xfrma
[XFRMA_ALG_CRYPT
-1] ||
191 xfrma
[XFRMA_ENCAP
-1] ||
192 xfrma
[XFRMA_SEC_CTX
-1] ||
193 !xfrma
[XFRMA_COADDR
-1])
202 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
204 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
206 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
208 if ((err
= verify_encap_tmpl(xfrma
)))
210 if ((err
= verify_sec_ctx_len(xfrma
)))
212 if ((err
= verify_one_addr(xfrma
, XFRMA_COADDR
, NULL
)))
217 case XFRM_MODE_TRANSPORT
:
218 case XFRM_MODE_TUNNEL
:
219 case XFRM_MODE_ROUTEOPTIMIZATION
:
233 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
234 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
235 struct rtattr
*u_arg
)
237 struct rtattr
*rta
= u_arg
;
238 struct xfrm_algo
*p
, *ualg
;
239 struct xfrm_algo_desc
*algo
;
244 ualg
= RTA_DATA(rta
);
246 algo
= get_byname(ualg
->alg_name
, 1);
249 *props
= algo
->desc
.sadb_alg_id
;
251 p
= kmemdup(ualg
, alg_len(ualg
), GFP_KERNEL
);
255 strcpy(p
->alg_name
, algo
->name
);
260 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
262 struct rtattr
*rta
= u_arg
;
263 struct xfrm_encap_tmpl
*p
, *uencap
;
268 uencap
= RTA_DATA(rta
);
269 p
= kmemdup(uencap
, sizeof(*p
), GFP_KERNEL
);
278 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
283 len
+= sizeof(struct xfrm_user_sec_ctx
);
284 len
+= xfrm_ctx
->ctx_len
;
289 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
291 struct xfrm_user_sec_ctx
*uctx
;
296 uctx
= RTA_DATA(u_arg
);
297 return security_xfrm_state_alloc(x
, uctx
);
300 static int attach_one_addr(xfrm_address_t
**addrpp
, struct rtattr
*u_arg
)
302 struct rtattr
*rta
= u_arg
;
303 xfrm_address_t
*p
, *uaddrp
;
308 uaddrp
= RTA_DATA(rta
);
309 p
= kmemdup(uaddrp
, sizeof(*p
), GFP_KERNEL
);
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
;
330 * Set inner address family if the KM left it as zero.
331 * See comment in validate_tmpl.
334 x
->sel
.family
= p
->family
;
338 * someday when pfkey also has support, we could have the code
339 * somehow made shareable and move it to xfrm_state.c - JHS
342 static int xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
345 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
346 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
347 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
348 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
351 struct xfrm_replay_state
*replay
;
352 if (RTA_PAYLOAD(rp
) < sizeof(*replay
))
354 replay
= RTA_DATA(rp
);
355 memcpy(&x
->replay
, replay
, sizeof(*replay
));
356 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
360 struct xfrm_lifetime_cur
*ltime
;
361 if (RTA_PAYLOAD(lt
) < sizeof(*ltime
))
363 ltime
= RTA_DATA(lt
);
364 x
->curlft
.bytes
= ltime
->bytes
;
365 x
->curlft
.packets
= ltime
->packets
;
366 x
->curlft
.add_time
= ltime
->add_time
;
367 x
->curlft
.use_time
= ltime
->use_time
;
371 if (RTA_PAYLOAD(et
) < sizeof(u32
))
373 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
377 if (RTA_PAYLOAD(rt
) < sizeof(u32
))
379 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
387 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
388 struct rtattr
**xfrma
,
391 struct xfrm_state
*x
= xfrm_state_alloc();
397 copy_from_user_state(x
, p
);
399 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
400 xfrm_aalg_get_byname
,
401 xfrma
[XFRMA_ALG_AUTH
-1])))
403 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
404 xfrm_ealg_get_byname
,
405 xfrma
[XFRMA_ALG_CRYPT
-1])))
407 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
408 xfrm_calg_get_byname
,
409 xfrma
[XFRMA_ALG_COMP
-1])))
411 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
413 if ((err
= attach_one_addr(&x
->coaddr
, xfrma
[XFRMA_COADDR
-1])))
415 err
= xfrm_init_state(x
);
419 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
423 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
424 /* sysctl_xfrm_aevent_etime is in 100ms units */
425 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
426 x
->preplay
.bitmap
= 0;
427 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
428 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
430 /* override default values from above */
432 err
= xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
439 x
->km
.state
= XFRM_STATE_DEAD
;
446 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
447 struct rtattr
**xfrma
)
449 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
450 struct xfrm_state
*x
;
454 err
= verify_newsa_info(p
, xfrma
);
458 x
= xfrm_state_construct(p
, xfrma
, &err
);
463 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
464 err
= xfrm_state_add(x
);
466 err
= xfrm_state_update(x
);
468 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
469 AUDIT_MAC_IPSEC_ADDSA
, err
? 0 : 1, NULL
, x
);
472 x
->km
.state
= XFRM_STATE_DEAD
;
477 c
.seq
= nlh
->nlmsg_seq
;
478 c
.pid
= nlh
->nlmsg_pid
;
479 c
.event
= nlh
->nlmsg_type
;
481 km_state_notify(x
, &c
);
487 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
488 struct rtattr
**xfrma
,
491 struct xfrm_state
*x
= NULL
;
494 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
496 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
498 xfrm_address_t
*saddr
= NULL
;
500 err
= verify_one_addr(xfrma
, XFRMA_SRCADDR
, &saddr
);
510 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
520 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
521 struct rtattr
**xfrma
)
523 struct xfrm_state
*x
;
526 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
528 x
= xfrm_user_state_lookup(p
, xfrma
, &err
);
532 if ((err
= security_xfrm_state_delete(x
)) != 0)
535 if (xfrm_state_kern(x
)) {
540 err
= xfrm_state_delete(x
);
545 c
.seq
= nlh
->nlmsg_seq
;
546 c
.pid
= nlh
->nlmsg_pid
;
547 c
.event
= nlh
->nlmsg_type
;
548 km_state_notify(x
, &c
);
551 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
552 AUDIT_MAC_IPSEC_DELSA
, err
? 0 : 1, NULL
, x
);
557 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
559 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
560 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
561 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
562 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
563 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
564 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
565 p
->mode
= x
->props
.mode
;
566 p
->replay_window
= x
->props
.replay_window
;
567 p
->reqid
= x
->props
.reqid
;
568 p
->family
= x
->props
.family
;
569 p
->flags
= x
->props
.flags
;
573 struct xfrm_dump_info
{
574 struct sk_buff
*in_skb
;
575 struct sk_buff
*out_skb
;
582 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
584 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
585 struct xfrm_user_sec_ctx
*uctx
;
588 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
592 uctx
= nla_data(attr
);
593 uctx
->exttype
= XFRMA_SEC_CTX
;
594 uctx
->len
= ctx_size
;
595 uctx
->ctx_doi
= s
->ctx_doi
;
596 uctx
->ctx_alg
= s
->ctx_alg
;
597 uctx
->ctx_len
= s
->ctx_len
;
598 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
603 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
605 struct xfrm_dump_info
*sp
= ptr
;
606 struct sk_buff
*in_skb
= sp
->in_skb
;
607 struct sk_buff
*skb
= sp
->out_skb
;
608 struct xfrm_usersa_info
*p
;
609 struct nlmsghdr
*nlh
;
611 if (sp
->this_idx
< sp
->start_idx
)
614 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
615 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
620 copy_to_user_state(x
, p
);
623 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
625 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
627 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
630 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
632 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
633 goto nla_put_failure
;
636 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
639 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
647 nlmsg_cancel(skb
, nlh
);
651 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
653 struct xfrm_dump_info info
;
655 info
.in_skb
= cb
->skb
;
657 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
658 info
.nlmsg_flags
= NLM_F_MULTI
;
660 info
.start_idx
= cb
->args
[0];
661 (void) xfrm_state_walk(0, dump_one_state
, &info
);
662 cb
->args
[0] = info
.this_idx
;
667 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
668 struct xfrm_state
*x
, u32 seq
)
670 struct xfrm_dump_info info
;
673 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
675 return ERR_PTR(-ENOMEM
);
677 info
.in_skb
= in_skb
;
679 info
.nlmsg_seq
= seq
;
680 info
.nlmsg_flags
= 0;
681 info
.this_idx
= info
.start_idx
= 0;
683 if (dump_one_state(x
, 0, &info
)) {
691 static inline size_t xfrm_spdinfo_msgsize(void)
693 return NLMSG_ALIGN(4)
694 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
695 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
698 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
700 struct xfrmk_spdinfo si
;
701 struct xfrmu_spdinfo spc
;
702 struct xfrmu_spdhinfo sph
;
703 struct nlmsghdr
*nlh
;
706 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
707 if (nlh
== NULL
) /* shouldnt really happen ... */
712 xfrm_spd_getinfo(&si
);
713 spc
.incnt
= si
.incnt
;
714 spc
.outcnt
= si
.outcnt
;
715 spc
.fwdcnt
= si
.fwdcnt
;
716 spc
.inscnt
= si
.inscnt
;
717 spc
.outscnt
= si
.outscnt
;
718 spc
.fwdscnt
= si
.fwdscnt
;
719 sph
.spdhcnt
= si
.spdhcnt
;
720 sph
.spdhmcnt
= si
.spdhmcnt
;
722 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
723 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
725 return nlmsg_end(skb
, nlh
);
728 nlmsg_cancel(skb
, nlh
);
732 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
733 struct rtattr
**xfrma
)
735 struct sk_buff
*r_skb
;
736 u32
*flags
= nlmsg_data(nlh
);
737 u32 spid
= NETLINK_CB(skb
).pid
;
738 u32 seq
= nlh
->nlmsg_seq
;
740 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
744 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
747 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
750 static inline size_t xfrm_sadinfo_msgsize(void)
752 return NLMSG_ALIGN(4)
753 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
754 + nla_total_size(4); /* XFRMA_SAD_CNT */
757 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
759 struct xfrmk_sadinfo si
;
760 struct xfrmu_sadhinfo sh
;
761 struct nlmsghdr
*nlh
;
764 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
765 if (nlh
== NULL
) /* shouldnt really happen ... */
770 xfrm_sad_getinfo(&si
);
772 sh
.sadhmcnt
= si
.sadhmcnt
;
773 sh
.sadhcnt
= si
.sadhcnt
;
775 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
776 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
778 return nlmsg_end(skb
, nlh
);
781 nlmsg_cancel(skb
, nlh
);
785 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
786 struct rtattr
**xfrma
)
788 struct sk_buff
*r_skb
;
789 u32
*flags
= nlmsg_data(nlh
);
790 u32 spid
= NETLINK_CB(skb
).pid
;
791 u32 seq
= nlh
->nlmsg_seq
;
793 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
797 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
800 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
803 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
804 struct rtattr
**xfrma
)
806 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
807 struct xfrm_state
*x
;
808 struct sk_buff
*resp_skb
;
811 x
= xfrm_user_state_lookup(p
, xfrma
, &err
);
815 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
816 if (IS_ERR(resp_skb
)) {
817 err
= PTR_ERR(resp_skb
);
819 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
826 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
828 switch (p
->info
.id
.proto
) {
834 /* IPCOMP spi is 16-bits. */
835 if (p
->max
>= 0x10000)
849 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
850 struct rtattr
**xfrma
)
852 struct xfrm_state
*x
;
853 struct xfrm_userspi_info
*p
;
854 struct sk_buff
*resp_skb
;
855 xfrm_address_t
*daddr
;
860 err
= verify_userspi_info(p
);
864 family
= p
->info
.family
;
865 daddr
= &p
->info
.id
.daddr
;
869 x
= xfrm_find_acq_byseq(p
->info
.seq
);
870 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
877 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
878 p
->info
.id
.proto
, daddr
,
885 resp_skb
= ERR_PTR(-ENOENT
);
887 spin_lock_bh(&x
->lock
);
888 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
889 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
891 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
893 spin_unlock_bh(&x
->lock
);
895 if (IS_ERR(resp_skb
)) {
896 err
= PTR_ERR(resp_skb
);
900 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
908 static int verify_policy_dir(u8 dir
)
912 case XFRM_POLICY_OUT
:
913 case XFRM_POLICY_FWD
:
923 static int verify_policy_type(u8 type
)
926 case XFRM_POLICY_TYPE_MAIN
:
927 #ifdef CONFIG_XFRM_SUB_POLICY
928 case XFRM_POLICY_TYPE_SUB
:
939 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
943 case XFRM_SHARE_SESSION
:
944 case XFRM_SHARE_USER
:
945 case XFRM_SHARE_UNIQUE
:
953 case XFRM_POLICY_ALLOW
:
954 case XFRM_POLICY_BLOCK
:
961 switch (p
->sel
.family
) {
966 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
969 return -EAFNOSUPPORT
;
976 return verify_policy_dir(p
->dir
);
979 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
981 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
982 struct xfrm_user_sec_ctx
*uctx
;
988 return security_xfrm_policy_alloc(pol
, uctx
);
991 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
997 for (i
= 0; i
< nr
; i
++, ut
++) {
998 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1000 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
1001 memcpy(&t
->saddr
, &ut
->saddr
,
1002 sizeof(xfrm_address_t
));
1003 t
->reqid
= ut
->reqid
;
1005 t
->share
= ut
->share
;
1006 t
->optional
= ut
->optional
;
1007 t
->aalgos
= ut
->aalgos
;
1008 t
->ealgos
= ut
->ealgos
;
1009 t
->calgos
= ut
->calgos
;
1010 t
->encap_family
= ut
->family
;
1014 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
1018 if (nr
> XFRM_MAX_DEPTH
)
1021 for (i
= 0; i
< nr
; i
++) {
1022 /* We never validated the ut->family value, so many
1023 * applications simply leave it at zero. The check was
1024 * never made and ut->family was ignored because all
1025 * templates could be assumed to have the same family as
1026 * the policy itself. Now that we will have ipv4-in-ipv6
1027 * and ipv6-in-ipv4 tunnels, this is no longer true.
1030 ut
[i
].family
= family
;
1032 switch (ut
[i
].family
) {
1035 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1047 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
1049 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1054 struct xfrm_user_tmpl
*utmpl
= RTA_DATA(rt
);
1055 int nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
1058 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1062 copy_templates(pol
, RTA_DATA(rt
), nr
);
1067 static int copy_from_user_policy_type(u8
*tp
, struct rtattr
**xfrma
)
1069 struct rtattr
*rt
= xfrma
[XFRMA_POLICY_TYPE
-1];
1070 struct xfrm_userpolicy_type
*upt
;
1071 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1075 if (rt
->rta_len
< sizeof(*upt
))
1082 err
= verify_policy_type(type
);
1090 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1092 xp
->priority
= p
->priority
;
1093 xp
->index
= p
->index
;
1094 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1095 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1096 xp
->action
= p
->action
;
1097 xp
->flags
= p
->flags
;
1098 xp
->family
= p
->sel
.family
;
1099 /* XXX xp->share = p->share; */
1102 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1104 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1105 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1106 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1107 p
->priority
= xp
->priority
;
1108 p
->index
= xp
->index
;
1109 p
->sel
.family
= xp
->family
;
1111 p
->action
= xp
->action
;
1112 p
->flags
= xp
->flags
;
1113 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1116 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
1118 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1126 copy_from_user_policy(xp
, p
);
1128 err
= copy_from_user_policy_type(&xp
->type
, xfrma
);
1132 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
1133 err
= copy_from_user_sec_ctx(xp
, xfrma
);
1144 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1145 struct rtattr
**xfrma
)
1147 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1148 struct xfrm_policy
*xp
;
1153 err
= verify_newpolicy_info(p
);
1156 err
= verify_sec_ctx_len(xfrma
);
1160 xp
= xfrm_policy_construct(p
, xfrma
, &err
);
1164 /* shouldnt excl be based on nlh flags??
1165 * Aha! this is anti-netlink really i.e more pfkey derived
1166 * in netlink excl is a flag and you wouldnt need
1167 * a type XFRM_MSG_UPDPOLICY - JHS */
1168 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1169 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1170 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1171 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1174 security_xfrm_policy_free(xp
);
1179 c
.event
= nlh
->nlmsg_type
;
1180 c
.seq
= nlh
->nlmsg_seq
;
1181 c
.pid
= nlh
->nlmsg_pid
;
1182 km_policy_notify(xp
, p
->dir
, &c
);
1189 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1191 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1194 if (xp
->xfrm_nr
== 0)
1197 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1198 struct xfrm_user_tmpl
*up
= &vec
[i
];
1199 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1201 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1202 up
->family
= kp
->encap_family
;
1203 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1204 up
->reqid
= kp
->reqid
;
1205 up
->mode
= kp
->mode
;
1206 up
->share
= kp
->share
;
1207 up
->optional
= kp
->optional
;
1208 up
->aalgos
= kp
->aalgos
;
1209 up
->ealgos
= kp
->ealgos
;
1210 up
->calgos
= kp
->calgos
;
1213 return nla_put(skb
, XFRMA_TMPL
,
1214 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1217 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1220 return copy_sec_ctx(x
->security
, skb
);
1225 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1228 return copy_sec_ctx(xp
->security
, skb
);
1232 static inline size_t userpolicy_type_attrsize(void)
1234 #ifdef CONFIG_XFRM_SUB_POLICY
1235 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1241 #ifdef CONFIG_XFRM_SUB_POLICY
1242 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1244 struct xfrm_userpolicy_type upt
= {
1248 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1252 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1258 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1260 struct xfrm_dump_info
*sp
= ptr
;
1261 struct xfrm_userpolicy_info
*p
;
1262 struct sk_buff
*in_skb
= sp
->in_skb
;
1263 struct sk_buff
*skb
= sp
->out_skb
;
1264 struct nlmsghdr
*nlh
;
1266 if (sp
->this_idx
< sp
->start_idx
)
1269 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1270 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1274 p
= nlmsg_data(nlh
);
1275 copy_to_user_policy(xp
, p
, dir
);
1276 if (copy_to_user_tmpl(xp
, skb
) < 0)
1278 if (copy_to_user_sec_ctx(xp
, skb
))
1280 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1283 nlmsg_end(skb
, nlh
);
1289 nlmsg_cancel(skb
, nlh
);
1293 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1295 struct xfrm_dump_info info
;
1297 info
.in_skb
= cb
->skb
;
1299 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1300 info
.nlmsg_flags
= NLM_F_MULTI
;
1302 info
.start_idx
= cb
->args
[0];
1303 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1304 #ifdef CONFIG_XFRM_SUB_POLICY
1305 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1307 cb
->args
[0] = info
.this_idx
;
1312 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1313 struct xfrm_policy
*xp
,
1316 struct xfrm_dump_info info
;
1317 struct sk_buff
*skb
;
1319 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1321 return ERR_PTR(-ENOMEM
);
1323 info
.in_skb
= in_skb
;
1325 info
.nlmsg_seq
= seq
;
1326 info
.nlmsg_flags
= 0;
1327 info
.this_idx
= info
.start_idx
= 0;
1329 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1337 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1338 struct rtattr
**xfrma
)
1340 struct xfrm_policy
*xp
;
1341 struct xfrm_userpolicy_id
*p
;
1342 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1347 p
= nlmsg_data(nlh
);
1348 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1350 err
= copy_from_user_policy_type(&type
, xfrma
);
1354 err
= verify_policy_dir(p
->dir
);
1359 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1361 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
1362 struct xfrm_policy tmp
;
1364 err
= verify_sec_ctx_len(xfrma
);
1368 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1370 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1372 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1375 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1377 security_xfrm_policy_free(&tmp
);
1383 struct sk_buff
*resp_skb
;
1385 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1386 if (IS_ERR(resp_skb
)) {
1387 err
= PTR_ERR(resp_skb
);
1389 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1390 NETLINK_CB(skb
).pid
);
1393 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1394 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1399 c
.data
.byid
= p
->index
;
1400 c
.event
= nlh
->nlmsg_type
;
1401 c
.seq
= nlh
->nlmsg_seq
;
1402 c
.pid
= nlh
->nlmsg_pid
;
1403 km_policy_notify(xp
, p
->dir
, &c
);
1411 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1412 struct rtattr
**xfrma
)
1415 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1416 struct xfrm_audit audit_info
;
1419 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1420 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1421 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1424 c
.data
.proto
= p
->proto
;
1425 c
.event
= nlh
->nlmsg_type
;
1426 c
.seq
= nlh
->nlmsg_seq
;
1427 c
.pid
= nlh
->nlmsg_pid
;
1428 km_state_notify(NULL
, &c
);
1433 static inline size_t xfrm_aevent_msgsize(void)
1435 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1436 + nla_total_size(sizeof(struct xfrm_replay_state
))
1437 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1438 + nla_total_size(4) /* XFRM_AE_RTHR */
1439 + nla_total_size(4); /* XFRM_AE_ETHR */
1442 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1444 struct xfrm_aevent_id
*id
;
1445 struct nlmsghdr
*nlh
;
1447 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1451 id
= nlmsg_data(nlh
);
1452 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1453 id
->sa_id
.spi
= x
->id
.spi
;
1454 id
->sa_id
.family
= x
->props
.family
;
1455 id
->sa_id
.proto
= x
->id
.proto
;
1456 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1457 id
->reqid
= x
->props
.reqid
;
1458 id
->flags
= c
->data
.aevent
;
1460 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1461 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1463 if (id
->flags
& XFRM_AE_RTHR
)
1464 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1466 if (id
->flags
& XFRM_AE_ETHR
)
1467 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1468 x
->replay_maxage
* 10 / HZ
);
1470 return nlmsg_end(skb
, nlh
);
1473 nlmsg_cancel(skb
, nlh
);
1477 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1478 struct rtattr
**xfrma
)
1480 struct xfrm_state
*x
;
1481 struct sk_buff
*r_skb
;
1484 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1485 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1487 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1491 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1498 * XXX: is this lock really needed - none of the other
1499 * gets lock (the concern is things getting updated
1500 * while we are still reading) - jhs
1502 spin_lock_bh(&x
->lock
);
1503 c
.data
.aevent
= p
->flags
;
1504 c
.seq
= nlh
->nlmsg_seq
;
1505 c
.pid
= nlh
->nlmsg_pid
;
1507 if (build_aevent(r_skb
, x
, &c
) < 0)
1509 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1510 spin_unlock_bh(&x
->lock
);
1515 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1516 struct rtattr
**xfrma
)
1518 struct xfrm_state
*x
;
1521 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1522 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1523 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1528 /* pedantic mode - thou shalt sayeth replaceth */
1529 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1532 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1536 if (x
->km
.state
!= XFRM_STATE_VALID
)
1539 spin_lock_bh(&x
->lock
);
1540 err
= xfrm_update_ae_params(x
, xfrma
);
1541 spin_unlock_bh(&x
->lock
);
1545 c
.event
= nlh
->nlmsg_type
;
1546 c
.seq
= nlh
->nlmsg_seq
;
1547 c
.pid
= nlh
->nlmsg_pid
;
1548 c
.data
.aevent
= XFRM_AE_CU
;
1549 km_state_notify(x
, &c
);
1556 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1557 struct rtattr
**xfrma
)
1560 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1562 struct xfrm_audit audit_info
;
1564 err
= copy_from_user_policy_type(&type
, xfrma
);
1568 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1569 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1570 err
= xfrm_policy_flush(type
, &audit_info
);
1574 c
.event
= nlh
->nlmsg_type
;
1575 c
.seq
= nlh
->nlmsg_seq
;
1576 c
.pid
= nlh
->nlmsg_pid
;
1577 km_policy_notify(NULL
, 0, &c
);
1581 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1582 struct rtattr
**xfrma
)
1584 struct xfrm_policy
*xp
;
1585 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1586 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1587 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1590 err
= copy_from_user_policy_type(&type
, xfrma
);
1595 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1597 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
1598 struct xfrm_policy tmp
;
1600 err
= verify_sec_ctx_len(xfrma
);
1604 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1606 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1608 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1611 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1613 security_xfrm_policy_free(&tmp
);
1618 read_lock(&xp
->lock
);
1620 read_unlock(&xp
->lock
);
1624 read_unlock(&xp
->lock
);
1627 xfrm_policy_delete(xp
, p
->dir
);
1628 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1629 AUDIT_MAC_IPSEC_DELSPD
, 1, xp
, NULL
);
1632 // reset the timers here?
1633 printk("Dont know what to do with soft policy expire\n");
1635 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1642 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1643 struct rtattr
**xfrma
)
1645 struct xfrm_state
*x
;
1647 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1648 struct xfrm_usersa_info
*p
= &ue
->state
;
1650 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1656 spin_lock_bh(&x
->lock
);
1658 if (x
->km
.state
!= XFRM_STATE_VALID
)
1660 km_state_expired(x
, ue
->hard
, current
->pid
);
1663 __xfrm_state_delete(x
);
1664 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1665 AUDIT_MAC_IPSEC_DELSA
, 1, NULL
, x
);
1669 spin_unlock_bh(&x
->lock
);
1674 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1675 struct rtattr
**xfrma
)
1677 struct xfrm_policy
*xp
;
1678 struct xfrm_user_tmpl
*ut
;
1680 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1682 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1683 struct xfrm_state
*x
= xfrm_state_alloc();
1689 err
= verify_newpolicy_info(&ua
->policy
);
1691 printk("BAD policy passed\n");
1697 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
);
1703 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1704 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1705 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1708 /* extract the templates and for each call km_key */
1709 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1710 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1711 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1712 x
->props
.mode
= t
->mode
;
1713 x
->props
.reqid
= t
->reqid
;
1714 x
->props
.family
= ut
->family
;
1715 t
->aalgos
= ua
->aalgos
;
1716 t
->ealgos
= ua
->ealgos
;
1717 t
->calgos
= ua
->calgos
;
1718 err
= km_query(x
, t
, xp
);
1728 #ifdef CONFIG_XFRM_MIGRATE
1729 static int verify_user_migrate(struct rtattr
**xfrma
)
1731 struct rtattr
*rt
= xfrma
[XFRMA_MIGRATE
-1];
1732 struct xfrm_user_migrate
*um
;
1737 if ((rt
->rta_len
- sizeof(*rt
)) < sizeof(*um
))
1743 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1744 struct rtattr
**xfrma
, int *num
)
1746 struct rtattr
*rt
= xfrma
[XFRMA_MIGRATE
-1];
1747 struct xfrm_user_migrate
*um
;
1751 num_migrate
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*um
);
1753 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1756 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1757 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1758 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1759 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1760 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1762 ma
->proto
= um
->proto
;
1763 ma
->mode
= um
->mode
;
1764 ma
->reqid
= um
->reqid
;
1766 ma
->old_family
= um
->old_family
;
1767 ma
->new_family
= um
->new_family
;
1774 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1775 struct rtattr
**xfrma
)
1777 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1778 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1783 err
= verify_user_migrate((struct rtattr
**)xfrma
);
1787 err
= copy_from_user_policy_type(&type
, (struct rtattr
**)xfrma
);
1791 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1792 (struct rtattr
**)xfrma
, &n
);
1799 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1804 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1805 struct rtattr
**xfrma
)
1807 return -ENOPROTOOPT
;
1811 #ifdef CONFIG_XFRM_MIGRATE
1812 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1814 struct xfrm_user_migrate um
;
1816 memset(&um
, 0, sizeof(um
));
1817 um
.proto
= m
->proto
;
1819 um
.reqid
= m
->reqid
;
1820 um
.old_family
= m
->old_family
;
1821 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1822 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1823 um
.new_family
= m
->new_family
;
1824 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1825 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1827 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1830 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1832 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1833 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1834 + userpolicy_type_attrsize();
1837 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1838 int num_migrate
, struct xfrm_selector
*sel
,
1841 struct xfrm_migrate
*mp
;
1842 struct xfrm_userpolicy_id
*pol_id
;
1843 struct nlmsghdr
*nlh
;
1846 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1850 pol_id
= nlmsg_data(nlh
);
1851 /* copy data from selector, dir, and type to the pol_id */
1852 memset(pol_id
, 0, sizeof(*pol_id
));
1853 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1856 if (copy_to_user_policy_type(type
, skb
) < 0)
1859 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1860 if (copy_to_user_migrate(mp
, skb
) < 0)
1864 return nlmsg_end(skb
, nlh
);
1866 nlmsg_cancel(skb
, nlh
);
1870 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1871 struct xfrm_migrate
*m
, int num_migrate
)
1873 struct sk_buff
*skb
;
1875 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1880 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1883 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1886 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1887 struct xfrm_migrate
*m
, int num_migrate
)
1889 return -ENOPROTOOPT
;
1893 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1895 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1896 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1897 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1898 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1899 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1900 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1901 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1902 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1903 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1904 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1905 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1906 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1907 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1908 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1909 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = NLMSG_LENGTH(0),
1910 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1911 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1912 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1913 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1914 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = NLMSG_LENGTH(sizeof(u32
)),
1915 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = NLMSG_LENGTH(sizeof(u32
)),
1920 static struct xfrm_link
{
1921 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct rtattr
**);
1922 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1923 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1924 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1925 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1926 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1927 .dump
= xfrm_dump_sa
},
1928 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1929 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1930 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1931 .dump
= xfrm_dump_policy
},
1932 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1933 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1934 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1935 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1936 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1937 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1938 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1939 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1940 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1941 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1942 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1943 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1944 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1947 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1949 struct rtattr
*xfrma
[XFRMA_MAX
];
1950 struct xfrm_link
*link
;
1953 type
= nlh
->nlmsg_type
;
1954 if (type
> XFRM_MSG_MAX
)
1957 type
-= XFRM_MSG_BASE
;
1958 link
= &xfrm_dispatch
[type
];
1960 /* All operations require privileges, even GET */
1961 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1964 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1965 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1966 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1967 if (link
->dump
== NULL
)
1970 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, NULL
);
1973 memset(xfrma
, 0, sizeof(xfrma
));
1975 if (nlh
->nlmsg_len
< (min_len
= xfrm_msg_min
[type
]))
1978 if (nlh
->nlmsg_len
> min_len
) {
1979 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1980 struct rtattr
*attr
= (void *) nlh
+ NLMSG_ALIGN(min_len
);
1982 while (RTA_OK(attr
, attrlen
)) {
1983 unsigned short flavor
= attr
->rta_type
;
1985 if (flavor
> XFRMA_MAX
)
1987 xfrma
[flavor
- 1] = attr
;
1989 attr
= RTA_NEXT(attr
, attrlen
);
1993 if (link
->doit
== NULL
)
1996 return link
->doit(skb
, nlh
, xfrma
);
1999 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
2001 unsigned int qlen
= 0;
2004 mutex_lock(&xfrm_cfg_mutex
);
2005 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
2006 mutex_unlock(&xfrm_cfg_mutex
);
2011 static inline size_t xfrm_expire_msgsize(void)
2013 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
2016 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
2018 struct xfrm_user_expire
*ue
;
2019 struct nlmsghdr
*nlh
;
2021 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
2025 ue
= nlmsg_data(nlh
);
2026 copy_to_user_state(x
, &ue
->state
);
2027 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
2029 return nlmsg_end(skb
, nlh
);
2032 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2034 struct sk_buff
*skb
;
2036 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2040 if (build_expire(skb
, x
, c
) < 0)
2043 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2046 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2048 struct sk_buff
*skb
;
2050 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2054 if (build_aevent(skb
, x
, c
) < 0)
2057 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2060 static int xfrm_notify_sa_flush(struct km_event
*c
)
2062 struct xfrm_usersa_flush
*p
;
2063 struct nlmsghdr
*nlh
;
2064 struct sk_buff
*skb
;
2065 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2067 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2071 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2077 p
= nlmsg_data(nlh
);
2078 p
->proto
= c
->data
.proto
;
2080 nlmsg_end(skb
, nlh
);
2082 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2085 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2089 l
+= nla_total_size(alg_len(x
->aalg
));
2091 l
+= nla_total_size(alg_len(x
->ealg
));
2093 l
+= nla_total_size(sizeof(*x
->calg
));
2095 l
+= nla_total_size(sizeof(*x
->encap
));
2100 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2102 struct xfrm_usersa_info
*p
;
2103 struct xfrm_usersa_id
*id
;
2104 struct nlmsghdr
*nlh
;
2105 struct sk_buff
*skb
;
2106 int len
= xfrm_sa_len(x
);
2109 headlen
= sizeof(*p
);
2110 if (c
->event
== XFRM_MSG_DELSA
) {
2111 len
+= nla_total_size(headlen
);
2112 headlen
= sizeof(*id
);
2114 len
+= NLMSG_ALIGN(headlen
);
2116 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2120 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2122 goto nla_put_failure
;
2124 p
= nlmsg_data(nlh
);
2125 if (c
->event
== XFRM_MSG_DELSA
) {
2126 struct nlattr
*attr
;
2128 id
= nlmsg_data(nlh
);
2129 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2130 id
->spi
= x
->id
.spi
;
2131 id
->family
= x
->props
.family
;
2132 id
->proto
= x
->id
.proto
;
2134 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2136 goto nla_put_failure
;
2141 copy_to_user_state(x
, p
);
2144 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
2146 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
2148 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
2151 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
2153 nlmsg_end(skb
, nlh
);
2155 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2162 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2166 case XFRM_MSG_EXPIRE
:
2167 return xfrm_exp_state_notify(x
, c
);
2168 case XFRM_MSG_NEWAE
:
2169 return xfrm_aevent_state_notify(x
, c
);
2170 case XFRM_MSG_DELSA
:
2171 case XFRM_MSG_UPDSA
:
2172 case XFRM_MSG_NEWSA
:
2173 return xfrm_notify_sa(x
, c
);
2174 case XFRM_MSG_FLUSHSA
:
2175 return xfrm_notify_sa_flush(c
);
2177 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2185 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2186 struct xfrm_policy
*xp
)
2188 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2189 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2190 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2191 + userpolicy_type_attrsize();
2194 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2195 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2198 struct xfrm_user_acquire
*ua
;
2199 struct nlmsghdr
*nlh
;
2200 __u32 seq
= xfrm_get_acqseq();
2202 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2206 ua
= nlmsg_data(nlh
);
2207 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2208 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2209 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2210 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2211 ua
->aalgos
= xt
->aalgos
;
2212 ua
->ealgos
= xt
->ealgos
;
2213 ua
->calgos
= xt
->calgos
;
2214 ua
->seq
= x
->km
.seq
= seq
;
2216 if (copy_to_user_tmpl(xp
, skb
) < 0)
2218 if (copy_to_user_state_sec_ctx(x
, skb
))
2220 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2223 return nlmsg_end(skb
, nlh
);
2226 nlmsg_cancel(skb
, nlh
);
2230 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2231 struct xfrm_policy
*xp
, int dir
)
2233 struct sk_buff
*skb
;
2235 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2239 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2242 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2245 /* User gives us xfrm_user_policy_info followed by an array of 0
2246 * or more templates.
2248 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2249 u8
*data
, int len
, int *dir
)
2251 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2252 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2253 struct xfrm_policy
*xp
;
2256 switch (sk
->sk_family
) {
2258 if (opt
!= IP_XFRM_POLICY
) {
2263 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2265 if (opt
!= IPV6_XFRM_POLICY
) {
2278 if (len
< sizeof(*p
) ||
2279 verify_newpolicy_info(p
))
2282 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2283 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2286 if (p
->dir
> XFRM_POLICY_OUT
)
2289 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2295 copy_from_user_policy(xp
, p
);
2296 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2297 copy_templates(xp
, ut
, nr
);
2304 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2306 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2307 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2308 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2309 + userpolicy_type_attrsize();
2312 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2313 int dir
, struct km_event
*c
)
2315 struct xfrm_user_polexpire
*upe
;
2316 struct nlmsghdr
*nlh
;
2317 int hard
= c
->data
.hard
;
2319 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2323 upe
= nlmsg_data(nlh
);
2324 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2325 if (copy_to_user_tmpl(xp
, skb
) < 0)
2327 if (copy_to_user_sec_ctx(xp
, skb
))
2329 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2333 return nlmsg_end(skb
, nlh
);
2336 nlmsg_cancel(skb
, nlh
);
2340 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2342 struct sk_buff
*skb
;
2344 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2348 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2351 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2354 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2356 struct xfrm_userpolicy_info
*p
;
2357 struct xfrm_userpolicy_id
*id
;
2358 struct nlmsghdr
*nlh
;
2359 struct sk_buff
*skb
;
2360 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2363 headlen
= sizeof(*p
);
2364 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2365 len
+= nla_total_size(headlen
);
2366 headlen
= sizeof(*id
);
2368 len
+= userpolicy_type_attrsize();
2369 len
+= NLMSG_ALIGN(headlen
);
2371 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2375 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2379 p
= nlmsg_data(nlh
);
2380 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2381 struct nlattr
*attr
;
2383 id
= nlmsg_data(nlh
);
2384 memset(id
, 0, sizeof(*id
));
2387 id
->index
= xp
->index
;
2389 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2391 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2398 copy_to_user_policy(xp
, p
, dir
);
2399 if (copy_to_user_tmpl(xp
, skb
) < 0)
2401 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2404 nlmsg_end(skb
, nlh
);
2406 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2413 static int xfrm_notify_policy_flush(struct km_event
*c
)
2415 struct nlmsghdr
*nlh
;
2416 struct sk_buff
*skb
;
2418 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2422 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2425 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2428 nlmsg_end(skb
, nlh
);
2430 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2437 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2441 case XFRM_MSG_NEWPOLICY
:
2442 case XFRM_MSG_UPDPOLICY
:
2443 case XFRM_MSG_DELPOLICY
:
2444 return xfrm_notify_policy(xp
, dir
, c
);
2445 case XFRM_MSG_FLUSHPOLICY
:
2446 return xfrm_notify_policy_flush(c
);
2447 case XFRM_MSG_POLEXPIRE
:
2448 return xfrm_exp_policy_notify(xp
, dir
, c
);
2450 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2457 static inline size_t xfrm_report_msgsize(void)
2459 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2462 static int build_report(struct sk_buff
*skb
, u8 proto
,
2463 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2465 struct xfrm_user_report
*ur
;
2466 struct nlmsghdr
*nlh
;
2468 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2472 ur
= nlmsg_data(nlh
);
2474 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2477 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2479 return nlmsg_end(skb
, nlh
);
2482 nlmsg_cancel(skb
, nlh
);
2486 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2487 xfrm_address_t
*addr
)
2489 struct sk_buff
*skb
;
2491 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2495 if (build_report(skb
, proto
, sel
, addr
) < 0)
2498 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2501 static struct xfrm_mgr netlink_mgr
= {
2503 .notify
= xfrm_send_state_notify
,
2504 .acquire
= xfrm_send_acquire
,
2505 .compile_policy
= xfrm_compile_policy
,
2506 .notify_policy
= xfrm_send_policy_notify
,
2507 .report
= xfrm_send_report
,
2508 .migrate
= xfrm_send_migrate
,
2511 static int __init
xfrm_user_init(void)
2515 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2517 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2518 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2521 rcu_assign_pointer(xfrm_nl
, nlsk
);
2523 xfrm_register_km(&netlink_mgr
);
2528 static void __exit
xfrm_user_exit(void)
2530 struct sock
*nlsk
= xfrm_nl
;
2532 xfrm_unregister_km(&netlink_mgr
);
2533 rcu_assign_pointer(xfrm_nl
, NULL
);
2535 sock_release(nlsk
->sk_socket
);
2538 module_init(xfrm_user_init
);
2539 module_exit(xfrm_user_exit
);
2540 MODULE_LICENSE("GPL");
2541 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);