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 if (RTA_PAYLOAD(rt
) < alg_len(algp
))
55 if (!algp
->alg_key_len
&&
56 strcmp(algp
->alg_name
, "digest_null") != 0)
61 if (!algp
->alg_key_len
&&
62 strcmp(algp
->alg_name
, "cipher_null") != 0)
67 /* Zero length keys are legal. */
74 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
78 static void verify_one_addr(struct rtattr
**xfrma
, enum xfrm_attr_type_t type
,
79 xfrm_address_t
**addrp
)
81 struct rtattr
*rt
= xfrma
[type
- 1];
84 *addrp
= RTA_DATA(rt
);
87 static inline int verify_sec_ctx_len(struct rtattr
**xfrma
)
89 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
- 1];
90 struct xfrm_user_sec_ctx
*uctx
;
96 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
103 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
104 struct rtattr
**xfrma
)
114 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
126 switch (p
->id
.proto
) {
128 if (!xfrma
[XFRMA_ALG_AUTH
-1] ||
129 xfrma
[XFRMA_ALG_CRYPT
-1] ||
130 xfrma
[XFRMA_ALG_COMP
-1])
135 if ((!xfrma
[XFRMA_ALG_AUTH
-1] &&
136 !xfrma
[XFRMA_ALG_CRYPT
-1]) ||
137 xfrma
[XFRMA_ALG_COMP
-1])
142 if (!xfrma
[XFRMA_ALG_COMP
-1] ||
143 xfrma
[XFRMA_ALG_AUTH
-1] ||
144 xfrma
[XFRMA_ALG_CRYPT
-1])
148 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
149 case IPPROTO_DSTOPTS
:
150 case IPPROTO_ROUTING
:
151 if (xfrma
[XFRMA_ALG_COMP
-1] ||
152 xfrma
[XFRMA_ALG_AUTH
-1] ||
153 xfrma
[XFRMA_ALG_CRYPT
-1] ||
154 xfrma
[XFRMA_ENCAP
-1] ||
155 xfrma
[XFRMA_SEC_CTX
-1] ||
156 !xfrma
[XFRMA_COADDR
-1])
165 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_AUTH
)))
167 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_CRYPT
)))
169 if ((err
= verify_one_alg(xfrma
, XFRMA_ALG_COMP
)))
171 if ((err
= verify_sec_ctx_len(xfrma
)))
176 case XFRM_MODE_TRANSPORT
:
177 case XFRM_MODE_TUNNEL
:
178 case XFRM_MODE_ROUTEOPTIMIZATION
:
192 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
193 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
194 struct rtattr
*u_arg
)
196 struct rtattr
*rta
= u_arg
;
197 struct xfrm_algo
*p
, *ualg
;
198 struct xfrm_algo_desc
*algo
;
203 ualg
= RTA_DATA(rta
);
205 algo
= get_byname(ualg
->alg_name
, 1);
208 *props
= algo
->desc
.sadb_alg_id
;
210 p
= kmemdup(ualg
, alg_len(ualg
), GFP_KERNEL
);
214 strcpy(p
->alg_name
, algo
->name
);
219 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct rtattr
*u_arg
)
221 struct rtattr
*rta
= u_arg
;
222 struct xfrm_encap_tmpl
*p
, *uencap
;
227 uencap
= RTA_DATA(rta
);
228 p
= kmemdup(uencap
, sizeof(*p
), GFP_KERNEL
);
237 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
242 len
+= sizeof(struct xfrm_user_sec_ctx
);
243 len
+= xfrm_ctx
->ctx_len
;
248 static int attach_sec_ctx(struct xfrm_state
*x
, struct rtattr
*u_arg
)
250 struct xfrm_user_sec_ctx
*uctx
;
255 uctx
= RTA_DATA(u_arg
);
256 return security_xfrm_state_alloc(x
, uctx
);
259 static int attach_one_addr(xfrm_address_t
**addrpp
, struct rtattr
*u_arg
)
261 struct rtattr
*rta
= u_arg
;
262 xfrm_address_t
*p
, *uaddrp
;
267 uaddrp
= RTA_DATA(rta
);
268 p
= kmemdup(uaddrp
, sizeof(*p
), GFP_KERNEL
);
276 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
278 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
279 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
280 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
281 x
->props
.mode
= p
->mode
;
282 x
->props
.replay_window
= p
->replay_window
;
283 x
->props
.reqid
= p
->reqid
;
284 x
->props
.family
= p
->family
;
285 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
286 x
->props
.flags
= p
->flags
;
289 * Set inner address family if the KM left it as zero.
290 * See comment in validate_tmpl.
293 x
->sel
.family
= p
->family
;
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
301 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct rtattr
**xfrma
)
303 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
304 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
305 struct rtattr
*et
= xfrma
[XFRMA_ETIMER_THRESH
-1];
306 struct rtattr
*rt
= xfrma
[XFRMA_REPLAY_THRESH
-1];
309 struct xfrm_replay_state
*replay
;
310 replay
= RTA_DATA(rp
);
311 memcpy(&x
->replay
, replay
, sizeof(*replay
));
312 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
316 struct xfrm_lifetime_cur
*ltime
;
317 ltime
= RTA_DATA(lt
);
318 x
->curlft
.bytes
= ltime
->bytes
;
319 x
->curlft
.packets
= ltime
->packets
;
320 x
->curlft
.add_time
= ltime
->add_time
;
321 x
->curlft
.use_time
= ltime
->use_time
;
325 x
->replay_maxage
= *(u32
*)RTA_DATA(et
);
328 x
->replay_maxdiff
= *(u32
*)RTA_DATA(rt
);
331 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
332 struct rtattr
**xfrma
,
335 struct xfrm_state
*x
= xfrm_state_alloc();
341 copy_from_user_state(x
, p
);
343 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
344 xfrm_aalg_get_byname
,
345 xfrma
[XFRMA_ALG_AUTH
-1])))
347 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
348 xfrm_ealg_get_byname
,
349 xfrma
[XFRMA_ALG_CRYPT
-1])))
351 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
352 xfrm_calg_get_byname
,
353 xfrma
[XFRMA_ALG_COMP
-1])))
355 if ((err
= attach_encap_tmpl(&x
->encap
, xfrma
[XFRMA_ENCAP
-1])))
357 if ((err
= attach_one_addr(&x
->coaddr
, xfrma
[XFRMA_COADDR
-1])))
359 err
= xfrm_init_state(x
);
363 if ((err
= attach_sec_ctx(x
, xfrma
[XFRMA_SEC_CTX
-1])))
367 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
368 /* sysctl_xfrm_aevent_etime is in 100ms units */
369 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
370 x
->preplay
.bitmap
= 0;
371 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
372 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
374 /* override default values from above */
376 xfrm_update_ae_params(x
, (struct rtattr
**)xfrma
);
381 x
->km
.state
= XFRM_STATE_DEAD
;
388 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
389 struct rtattr
**xfrma
)
391 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
392 struct xfrm_state
*x
;
396 err
= verify_newsa_info(p
, xfrma
);
400 x
= xfrm_state_construct(p
, xfrma
, &err
);
405 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
406 err
= xfrm_state_add(x
);
408 err
= xfrm_state_update(x
);
410 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
411 AUDIT_MAC_IPSEC_ADDSA
, err
? 0 : 1, NULL
, x
);
414 x
->km
.state
= XFRM_STATE_DEAD
;
419 c
.seq
= nlh
->nlmsg_seq
;
420 c
.pid
= nlh
->nlmsg_pid
;
421 c
.event
= nlh
->nlmsg_type
;
423 km_state_notify(x
, &c
);
429 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
430 struct rtattr
**xfrma
,
433 struct xfrm_state
*x
= NULL
;
436 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
438 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
440 xfrm_address_t
*saddr
= NULL
;
442 verify_one_addr(xfrma
, XFRMA_SRCADDR
, &saddr
);
449 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
459 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
460 struct rtattr
**xfrma
)
462 struct xfrm_state
*x
;
465 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
467 x
= xfrm_user_state_lookup(p
, xfrma
, &err
);
471 if ((err
= security_xfrm_state_delete(x
)) != 0)
474 if (xfrm_state_kern(x
)) {
479 err
= xfrm_state_delete(x
);
484 c
.seq
= nlh
->nlmsg_seq
;
485 c
.pid
= nlh
->nlmsg_pid
;
486 c
.event
= nlh
->nlmsg_type
;
487 km_state_notify(x
, &c
);
490 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
491 AUDIT_MAC_IPSEC_DELSA
, err
? 0 : 1, NULL
, x
);
496 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
498 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
499 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
500 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
501 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
502 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
503 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
504 p
->mode
= x
->props
.mode
;
505 p
->replay_window
= x
->props
.replay_window
;
506 p
->reqid
= x
->props
.reqid
;
507 p
->family
= x
->props
.family
;
508 p
->flags
= x
->props
.flags
;
512 struct xfrm_dump_info
{
513 struct sk_buff
*in_skb
;
514 struct sk_buff
*out_skb
;
521 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
523 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
524 struct xfrm_user_sec_ctx
*uctx
;
527 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
531 uctx
= nla_data(attr
);
532 uctx
->exttype
= XFRMA_SEC_CTX
;
533 uctx
->len
= ctx_size
;
534 uctx
->ctx_doi
= s
->ctx_doi
;
535 uctx
->ctx_alg
= s
->ctx_alg
;
536 uctx
->ctx_len
= s
->ctx_len
;
537 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
542 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
544 struct xfrm_dump_info
*sp
= ptr
;
545 struct sk_buff
*in_skb
= sp
->in_skb
;
546 struct sk_buff
*skb
= sp
->out_skb
;
547 struct xfrm_usersa_info
*p
;
548 struct nlmsghdr
*nlh
;
550 if (sp
->this_idx
< sp
->start_idx
)
553 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
554 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
559 copy_to_user_state(x
, p
);
562 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
564 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
566 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
569 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
571 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
572 goto nla_put_failure
;
575 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
578 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
586 nlmsg_cancel(skb
, nlh
);
590 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
592 struct xfrm_dump_info info
;
594 info
.in_skb
= cb
->skb
;
596 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
597 info
.nlmsg_flags
= NLM_F_MULTI
;
599 info
.start_idx
= cb
->args
[0];
600 (void) xfrm_state_walk(0, dump_one_state
, &info
);
601 cb
->args
[0] = info
.this_idx
;
606 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
607 struct xfrm_state
*x
, u32 seq
)
609 struct xfrm_dump_info info
;
612 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
614 return ERR_PTR(-ENOMEM
);
616 info
.in_skb
= in_skb
;
618 info
.nlmsg_seq
= seq
;
619 info
.nlmsg_flags
= 0;
620 info
.this_idx
= info
.start_idx
= 0;
622 if (dump_one_state(x
, 0, &info
)) {
630 static inline size_t xfrm_spdinfo_msgsize(void)
632 return NLMSG_ALIGN(4)
633 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
634 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
637 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
639 struct xfrmk_spdinfo si
;
640 struct xfrmu_spdinfo spc
;
641 struct xfrmu_spdhinfo sph
;
642 struct nlmsghdr
*nlh
;
645 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
646 if (nlh
== NULL
) /* shouldnt really happen ... */
651 xfrm_spd_getinfo(&si
);
652 spc
.incnt
= si
.incnt
;
653 spc
.outcnt
= si
.outcnt
;
654 spc
.fwdcnt
= si
.fwdcnt
;
655 spc
.inscnt
= si
.inscnt
;
656 spc
.outscnt
= si
.outscnt
;
657 spc
.fwdscnt
= si
.fwdscnt
;
658 sph
.spdhcnt
= si
.spdhcnt
;
659 sph
.spdhmcnt
= si
.spdhmcnt
;
661 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
662 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
664 return nlmsg_end(skb
, nlh
);
667 nlmsg_cancel(skb
, nlh
);
671 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
672 struct rtattr
**xfrma
)
674 struct sk_buff
*r_skb
;
675 u32
*flags
= nlmsg_data(nlh
);
676 u32 spid
= NETLINK_CB(skb
).pid
;
677 u32 seq
= nlh
->nlmsg_seq
;
679 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
683 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
686 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
689 static inline size_t xfrm_sadinfo_msgsize(void)
691 return NLMSG_ALIGN(4)
692 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
693 + nla_total_size(4); /* XFRMA_SAD_CNT */
696 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
698 struct xfrmk_sadinfo si
;
699 struct xfrmu_sadhinfo sh
;
700 struct nlmsghdr
*nlh
;
703 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
704 if (nlh
== NULL
) /* shouldnt really happen ... */
709 xfrm_sad_getinfo(&si
);
711 sh
.sadhmcnt
= si
.sadhmcnt
;
712 sh
.sadhcnt
= si
.sadhcnt
;
714 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
715 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
717 return nlmsg_end(skb
, nlh
);
720 nlmsg_cancel(skb
, nlh
);
724 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
725 struct rtattr
**xfrma
)
727 struct sk_buff
*r_skb
;
728 u32
*flags
= nlmsg_data(nlh
);
729 u32 spid
= NETLINK_CB(skb
).pid
;
730 u32 seq
= nlh
->nlmsg_seq
;
732 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
736 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
739 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
742 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
743 struct rtattr
**xfrma
)
745 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
746 struct xfrm_state
*x
;
747 struct sk_buff
*resp_skb
;
750 x
= xfrm_user_state_lookup(p
, xfrma
, &err
);
754 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
755 if (IS_ERR(resp_skb
)) {
756 err
= PTR_ERR(resp_skb
);
758 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
765 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
767 switch (p
->info
.id
.proto
) {
773 /* IPCOMP spi is 16-bits. */
774 if (p
->max
>= 0x10000)
788 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
789 struct rtattr
**xfrma
)
791 struct xfrm_state
*x
;
792 struct xfrm_userspi_info
*p
;
793 struct sk_buff
*resp_skb
;
794 xfrm_address_t
*daddr
;
799 err
= verify_userspi_info(p
);
803 family
= p
->info
.family
;
804 daddr
= &p
->info
.id
.daddr
;
808 x
= xfrm_find_acq_byseq(p
->info
.seq
);
809 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
816 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
817 p
->info
.id
.proto
, daddr
,
824 resp_skb
= ERR_PTR(-ENOENT
);
826 spin_lock_bh(&x
->lock
);
827 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
828 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
830 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
832 spin_unlock_bh(&x
->lock
);
834 if (IS_ERR(resp_skb
)) {
835 err
= PTR_ERR(resp_skb
);
839 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
847 static int verify_policy_dir(u8 dir
)
851 case XFRM_POLICY_OUT
:
852 case XFRM_POLICY_FWD
:
862 static int verify_policy_type(u8 type
)
865 case XFRM_POLICY_TYPE_MAIN
:
866 #ifdef CONFIG_XFRM_SUB_POLICY
867 case XFRM_POLICY_TYPE_SUB
:
878 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
882 case XFRM_SHARE_SESSION
:
883 case XFRM_SHARE_USER
:
884 case XFRM_SHARE_UNIQUE
:
892 case XFRM_POLICY_ALLOW
:
893 case XFRM_POLICY_BLOCK
:
900 switch (p
->sel
.family
) {
905 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
908 return -EAFNOSUPPORT
;
915 return verify_policy_dir(p
->dir
);
918 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
920 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
921 struct xfrm_user_sec_ctx
*uctx
;
927 return security_xfrm_policy_alloc(pol
, uctx
);
930 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
936 for (i
= 0; i
< nr
; i
++, ut
++) {
937 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
939 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
940 memcpy(&t
->saddr
, &ut
->saddr
,
941 sizeof(xfrm_address_t
));
942 t
->reqid
= ut
->reqid
;
944 t
->share
= ut
->share
;
945 t
->optional
= ut
->optional
;
946 t
->aalgos
= ut
->aalgos
;
947 t
->ealgos
= ut
->ealgos
;
948 t
->calgos
= ut
->calgos
;
949 t
->encap_family
= ut
->family
;
953 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
957 if (nr
> XFRM_MAX_DEPTH
)
960 for (i
= 0; i
< nr
; i
++) {
961 /* We never validated the ut->family value, so many
962 * applications simply leave it at zero. The check was
963 * never made and ut->family was ignored because all
964 * templates could be assumed to have the same family as
965 * the policy itself. Now that we will have ipv4-in-ipv6
966 * and ipv6-in-ipv4 tunnels, this is no longer true.
969 ut
[i
].family
= family
;
971 switch (ut
[i
].family
) {
974 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
986 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct rtattr
**xfrma
)
988 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
993 struct xfrm_user_tmpl
*utmpl
= RTA_DATA(rt
);
994 int nr
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*utmpl
);
997 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1001 copy_templates(pol
, RTA_DATA(rt
), nr
);
1006 static int copy_from_user_policy_type(u8
*tp
, struct rtattr
**xfrma
)
1008 struct rtattr
*rt
= xfrma
[XFRMA_POLICY_TYPE
-1];
1009 struct xfrm_userpolicy_type
*upt
;
1010 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1018 err
= verify_policy_type(type
);
1026 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1028 xp
->priority
= p
->priority
;
1029 xp
->index
= p
->index
;
1030 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1031 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1032 xp
->action
= p
->action
;
1033 xp
->flags
= p
->flags
;
1034 xp
->family
= p
->sel
.family
;
1035 /* XXX xp->share = p->share; */
1038 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1040 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1041 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1042 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1043 p
->priority
= xp
->priority
;
1044 p
->index
= xp
->index
;
1045 p
->sel
.family
= xp
->family
;
1047 p
->action
= xp
->action
;
1048 p
->flags
= xp
->flags
;
1049 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1052 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct rtattr
**xfrma
, int *errp
)
1054 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1062 copy_from_user_policy(xp
, p
);
1064 err
= copy_from_user_policy_type(&xp
->type
, xfrma
);
1068 if (!(err
= copy_from_user_tmpl(xp
, xfrma
)))
1069 err
= copy_from_user_sec_ctx(xp
, xfrma
);
1080 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1081 struct rtattr
**xfrma
)
1083 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1084 struct xfrm_policy
*xp
;
1089 err
= verify_newpolicy_info(p
);
1092 err
= verify_sec_ctx_len(xfrma
);
1096 xp
= xfrm_policy_construct(p
, xfrma
, &err
);
1100 /* shouldnt excl be based on nlh flags??
1101 * Aha! this is anti-netlink really i.e more pfkey derived
1102 * in netlink excl is a flag and you wouldnt need
1103 * a type XFRM_MSG_UPDPOLICY - JHS */
1104 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1105 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1106 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1107 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1110 security_xfrm_policy_free(xp
);
1115 c
.event
= nlh
->nlmsg_type
;
1116 c
.seq
= nlh
->nlmsg_seq
;
1117 c
.pid
= nlh
->nlmsg_pid
;
1118 km_policy_notify(xp
, p
->dir
, &c
);
1125 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1127 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1130 if (xp
->xfrm_nr
== 0)
1133 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1134 struct xfrm_user_tmpl
*up
= &vec
[i
];
1135 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1137 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1138 up
->family
= kp
->encap_family
;
1139 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1140 up
->reqid
= kp
->reqid
;
1141 up
->mode
= kp
->mode
;
1142 up
->share
= kp
->share
;
1143 up
->optional
= kp
->optional
;
1144 up
->aalgos
= kp
->aalgos
;
1145 up
->ealgos
= kp
->ealgos
;
1146 up
->calgos
= kp
->calgos
;
1149 return nla_put(skb
, XFRMA_TMPL
,
1150 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1153 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1156 return copy_sec_ctx(x
->security
, skb
);
1161 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1164 return copy_sec_ctx(xp
->security
, skb
);
1168 static inline size_t userpolicy_type_attrsize(void)
1170 #ifdef CONFIG_XFRM_SUB_POLICY
1171 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1177 #ifdef CONFIG_XFRM_SUB_POLICY
1178 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1180 struct xfrm_userpolicy_type upt
= {
1184 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1188 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1194 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1196 struct xfrm_dump_info
*sp
= ptr
;
1197 struct xfrm_userpolicy_info
*p
;
1198 struct sk_buff
*in_skb
= sp
->in_skb
;
1199 struct sk_buff
*skb
= sp
->out_skb
;
1200 struct nlmsghdr
*nlh
;
1202 if (sp
->this_idx
< sp
->start_idx
)
1205 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1206 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1210 p
= nlmsg_data(nlh
);
1211 copy_to_user_policy(xp
, p
, dir
);
1212 if (copy_to_user_tmpl(xp
, skb
) < 0)
1214 if (copy_to_user_sec_ctx(xp
, skb
))
1216 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1219 nlmsg_end(skb
, nlh
);
1225 nlmsg_cancel(skb
, nlh
);
1229 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1231 struct xfrm_dump_info info
;
1233 info
.in_skb
= cb
->skb
;
1235 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1236 info
.nlmsg_flags
= NLM_F_MULTI
;
1238 info
.start_idx
= cb
->args
[0];
1239 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1240 #ifdef CONFIG_XFRM_SUB_POLICY
1241 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1243 cb
->args
[0] = info
.this_idx
;
1248 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1249 struct xfrm_policy
*xp
,
1252 struct xfrm_dump_info info
;
1253 struct sk_buff
*skb
;
1255 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1257 return ERR_PTR(-ENOMEM
);
1259 info
.in_skb
= in_skb
;
1261 info
.nlmsg_seq
= seq
;
1262 info
.nlmsg_flags
= 0;
1263 info
.this_idx
= info
.start_idx
= 0;
1265 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1273 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1274 struct rtattr
**xfrma
)
1276 struct xfrm_policy
*xp
;
1277 struct xfrm_userpolicy_id
*p
;
1278 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1283 p
= nlmsg_data(nlh
);
1284 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1286 err
= copy_from_user_policy_type(&type
, xfrma
);
1290 err
= verify_policy_dir(p
->dir
);
1295 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1297 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
1298 struct xfrm_policy tmp
;
1300 err
= verify_sec_ctx_len(xfrma
);
1304 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1306 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1308 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1311 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1313 security_xfrm_policy_free(&tmp
);
1319 struct sk_buff
*resp_skb
;
1321 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1322 if (IS_ERR(resp_skb
)) {
1323 err
= PTR_ERR(resp_skb
);
1325 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1326 NETLINK_CB(skb
).pid
);
1329 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1330 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1335 c
.data
.byid
= p
->index
;
1336 c
.event
= nlh
->nlmsg_type
;
1337 c
.seq
= nlh
->nlmsg_seq
;
1338 c
.pid
= nlh
->nlmsg_pid
;
1339 km_policy_notify(xp
, p
->dir
, &c
);
1347 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1348 struct rtattr
**xfrma
)
1351 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1352 struct xfrm_audit audit_info
;
1355 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1356 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1357 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1360 c
.data
.proto
= p
->proto
;
1361 c
.event
= nlh
->nlmsg_type
;
1362 c
.seq
= nlh
->nlmsg_seq
;
1363 c
.pid
= nlh
->nlmsg_pid
;
1364 km_state_notify(NULL
, &c
);
1369 static inline size_t xfrm_aevent_msgsize(void)
1371 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1372 + nla_total_size(sizeof(struct xfrm_replay_state
))
1373 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1374 + nla_total_size(4) /* XFRM_AE_RTHR */
1375 + nla_total_size(4); /* XFRM_AE_ETHR */
1378 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1380 struct xfrm_aevent_id
*id
;
1381 struct nlmsghdr
*nlh
;
1383 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1387 id
= nlmsg_data(nlh
);
1388 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1389 id
->sa_id
.spi
= x
->id
.spi
;
1390 id
->sa_id
.family
= x
->props
.family
;
1391 id
->sa_id
.proto
= x
->id
.proto
;
1392 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1393 id
->reqid
= x
->props
.reqid
;
1394 id
->flags
= c
->data
.aevent
;
1396 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1397 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1399 if (id
->flags
& XFRM_AE_RTHR
)
1400 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1402 if (id
->flags
& XFRM_AE_ETHR
)
1403 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1404 x
->replay_maxage
* 10 / HZ
);
1406 return nlmsg_end(skb
, nlh
);
1409 nlmsg_cancel(skb
, nlh
);
1413 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1414 struct rtattr
**xfrma
)
1416 struct xfrm_state
*x
;
1417 struct sk_buff
*r_skb
;
1420 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1421 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1423 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1427 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1434 * XXX: is this lock really needed - none of the other
1435 * gets lock (the concern is things getting updated
1436 * while we are still reading) - jhs
1438 spin_lock_bh(&x
->lock
);
1439 c
.data
.aevent
= p
->flags
;
1440 c
.seq
= nlh
->nlmsg_seq
;
1441 c
.pid
= nlh
->nlmsg_pid
;
1443 if (build_aevent(r_skb
, x
, &c
) < 0)
1445 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1446 spin_unlock_bh(&x
->lock
);
1451 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1452 struct rtattr
**xfrma
)
1454 struct xfrm_state
*x
;
1457 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1458 struct rtattr
*rp
= xfrma
[XFRMA_REPLAY_VAL
-1];
1459 struct rtattr
*lt
= xfrma
[XFRMA_LTIME_VAL
-1];
1464 /* pedantic mode - thou shalt sayeth replaceth */
1465 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1468 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1472 if (x
->km
.state
!= XFRM_STATE_VALID
)
1475 spin_lock_bh(&x
->lock
);
1476 xfrm_update_ae_params(x
, xfrma
);
1477 spin_unlock_bh(&x
->lock
);
1479 c
.event
= nlh
->nlmsg_type
;
1480 c
.seq
= nlh
->nlmsg_seq
;
1481 c
.pid
= nlh
->nlmsg_pid
;
1482 c
.data
.aevent
= XFRM_AE_CU
;
1483 km_state_notify(x
, &c
);
1490 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1491 struct rtattr
**xfrma
)
1494 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1496 struct xfrm_audit audit_info
;
1498 err
= copy_from_user_policy_type(&type
, xfrma
);
1502 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1503 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1504 err
= xfrm_policy_flush(type
, &audit_info
);
1508 c
.event
= nlh
->nlmsg_type
;
1509 c
.seq
= nlh
->nlmsg_seq
;
1510 c
.pid
= nlh
->nlmsg_pid
;
1511 km_policy_notify(NULL
, 0, &c
);
1515 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1516 struct rtattr
**xfrma
)
1518 struct xfrm_policy
*xp
;
1519 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1520 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1521 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1524 err
= copy_from_user_policy_type(&type
, xfrma
);
1529 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1531 struct rtattr
*rt
= xfrma
[XFRMA_SEC_CTX
-1];
1532 struct xfrm_policy tmp
;
1534 err
= verify_sec_ctx_len(xfrma
);
1538 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1540 struct xfrm_user_sec_ctx
*uctx
= RTA_DATA(rt
);
1542 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1545 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1547 security_xfrm_policy_free(&tmp
);
1552 read_lock(&xp
->lock
);
1554 read_unlock(&xp
->lock
);
1558 read_unlock(&xp
->lock
);
1561 xfrm_policy_delete(xp
, p
->dir
);
1562 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1563 AUDIT_MAC_IPSEC_DELSPD
, 1, xp
, NULL
);
1566 // reset the timers here?
1567 printk("Dont know what to do with soft policy expire\n");
1569 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1576 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1577 struct rtattr
**xfrma
)
1579 struct xfrm_state
*x
;
1581 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1582 struct xfrm_usersa_info
*p
= &ue
->state
;
1584 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1590 spin_lock_bh(&x
->lock
);
1592 if (x
->km
.state
!= XFRM_STATE_VALID
)
1594 km_state_expired(x
, ue
->hard
, current
->pid
);
1597 __xfrm_state_delete(x
);
1598 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1599 AUDIT_MAC_IPSEC_DELSA
, 1, NULL
, x
);
1603 spin_unlock_bh(&x
->lock
);
1608 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1609 struct rtattr
**xfrma
)
1611 struct xfrm_policy
*xp
;
1612 struct xfrm_user_tmpl
*ut
;
1614 struct rtattr
*rt
= xfrma
[XFRMA_TMPL
-1];
1616 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1617 struct xfrm_state
*x
= xfrm_state_alloc();
1623 err
= verify_newpolicy_info(&ua
->policy
);
1625 printk("BAD policy passed\n");
1631 xp
= xfrm_policy_construct(&ua
->policy
, (struct rtattr
**) xfrma
, &err
);
1637 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1638 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1639 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1642 /* extract the templates and for each call km_key */
1643 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1644 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1645 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1646 x
->props
.mode
= t
->mode
;
1647 x
->props
.reqid
= t
->reqid
;
1648 x
->props
.family
= ut
->family
;
1649 t
->aalgos
= ua
->aalgos
;
1650 t
->ealgos
= ua
->ealgos
;
1651 t
->calgos
= ua
->calgos
;
1652 err
= km_query(x
, t
, xp
);
1662 #ifdef CONFIG_XFRM_MIGRATE
1663 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1664 struct rtattr
**xfrma
, int *num
)
1666 struct rtattr
*rt
= xfrma
[XFRMA_MIGRATE
-1];
1667 struct xfrm_user_migrate
*um
;
1671 num_migrate
= (rt
->rta_len
- sizeof(*rt
)) / sizeof(*um
);
1673 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1676 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1677 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1678 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1679 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1680 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1682 ma
->proto
= um
->proto
;
1683 ma
->mode
= um
->mode
;
1684 ma
->reqid
= um
->reqid
;
1686 ma
->old_family
= um
->old_family
;
1687 ma
->new_family
= um
->new_family
;
1694 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1695 struct rtattr
**xfrma
)
1697 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1698 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1703 if (xfrma
[XFRMA_MIGRATE
-1] == NULL
)
1706 err
= copy_from_user_policy_type(&type
, (struct rtattr
**)xfrma
);
1710 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1711 (struct rtattr
**)xfrma
, &n
);
1718 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1723 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1724 struct rtattr
**xfrma
)
1726 return -ENOPROTOOPT
;
1730 #ifdef CONFIG_XFRM_MIGRATE
1731 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1733 struct xfrm_user_migrate um
;
1735 memset(&um
, 0, sizeof(um
));
1736 um
.proto
= m
->proto
;
1738 um
.reqid
= m
->reqid
;
1739 um
.old_family
= m
->old_family
;
1740 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1741 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1742 um
.new_family
= m
->new_family
;
1743 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1744 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1746 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1749 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1751 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1752 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1753 + userpolicy_type_attrsize();
1756 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1757 int num_migrate
, struct xfrm_selector
*sel
,
1760 struct xfrm_migrate
*mp
;
1761 struct xfrm_userpolicy_id
*pol_id
;
1762 struct nlmsghdr
*nlh
;
1765 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1769 pol_id
= nlmsg_data(nlh
);
1770 /* copy data from selector, dir, and type to the pol_id */
1771 memset(pol_id
, 0, sizeof(*pol_id
));
1772 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1775 if (copy_to_user_policy_type(type
, skb
) < 0)
1778 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1779 if (copy_to_user_migrate(mp
, skb
) < 0)
1783 return nlmsg_end(skb
, nlh
);
1785 nlmsg_cancel(skb
, nlh
);
1789 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1790 struct xfrm_migrate
*m
, int num_migrate
)
1792 struct sk_buff
*skb
;
1794 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1799 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1802 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1805 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1806 struct xfrm_migrate
*m
, int num_migrate
)
1808 return -ENOPROTOOPT
;
1812 #define XMSGSIZE(type) sizeof(struct type)
1814 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1815 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1816 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1817 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1818 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1819 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1820 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1821 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1822 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1823 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1824 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1825 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1826 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1827 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1828 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1829 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1830 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1831 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1832 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1833 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1834 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1839 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1840 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1841 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1842 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1843 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1844 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1845 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1846 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1847 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1848 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1849 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1850 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1851 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1852 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1853 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1856 static struct xfrm_link
{
1857 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct rtattr
**);
1858 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1859 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1860 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1861 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1862 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1863 .dump
= xfrm_dump_sa
},
1864 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1865 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1866 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1867 .dump
= xfrm_dump_policy
},
1868 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1869 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1870 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1871 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1872 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1873 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1874 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1875 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1876 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1877 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1878 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1879 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1880 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1883 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1885 struct nlattr
*xfrma
[XFRMA_MAX
+1];
1886 struct xfrm_link
*link
;
1889 type
= nlh
->nlmsg_type
;
1890 if (type
> XFRM_MSG_MAX
)
1893 type
-= XFRM_MSG_BASE
;
1894 link
= &xfrm_dispatch
[type
];
1896 /* All operations require privileges, even GET */
1897 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1900 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1901 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1902 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1903 if (link
->dump
== NULL
)
1906 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, NULL
);
1909 /* FIXME: Temporary hack, nlmsg_parse() starts at xfrma[1], old code
1910 * expects first attribute at xfrma[0] */
1911 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], xfrma
-1, XFRMA_MAX
,
1916 if (link
->doit
== NULL
)
1919 return link
->doit(skb
, nlh
, (struct rtattr
**) xfrma
);
1922 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1924 unsigned int qlen
= 0;
1927 mutex_lock(&xfrm_cfg_mutex
);
1928 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1929 mutex_unlock(&xfrm_cfg_mutex
);
1934 static inline size_t xfrm_expire_msgsize(void)
1936 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1939 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1941 struct xfrm_user_expire
*ue
;
1942 struct nlmsghdr
*nlh
;
1944 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1948 ue
= nlmsg_data(nlh
);
1949 copy_to_user_state(x
, &ue
->state
);
1950 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1952 return nlmsg_end(skb
, nlh
);
1955 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1957 struct sk_buff
*skb
;
1959 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
1963 if (build_expire(skb
, x
, c
) < 0)
1966 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1969 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1971 struct sk_buff
*skb
;
1973 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1977 if (build_aevent(skb
, x
, c
) < 0)
1980 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1983 static int xfrm_notify_sa_flush(struct km_event
*c
)
1985 struct xfrm_usersa_flush
*p
;
1986 struct nlmsghdr
*nlh
;
1987 struct sk_buff
*skb
;
1988 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
1990 skb
= nlmsg_new(len
, GFP_ATOMIC
);
1994 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2000 p
= nlmsg_data(nlh
);
2001 p
->proto
= c
->data
.proto
;
2003 nlmsg_end(skb
, nlh
);
2005 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2008 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2012 l
+= nla_total_size(alg_len(x
->aalg
));
2014 l
+= nla_total_size(alg_len(x
->ealg
));
2016 l
+= nla_total_size(sizeof(*x
->calg
));
2018 l
+= nla_total_size(sizeof(*x
->encap
));
2023 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2025 struct xfrm_usersa_info
*p
;
2026 struct xfrm_usersa_id
*id
;
2027 struct nlmsghdr
*nlh
;
2028 struct sk_buff
*skb
;
2029 int len
= xfrm_sa_len(x
);
2032 headlen
= sizeof(*p
);
2033 if (c
->event
== XFRM_MSG_DELSA
) {
2034 len
+= nla_total_size(headlen
);
2035 headlen
= sizeof(*id
);
2037 len
+= NLMSG_ALIGN(headlen
);
2039 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2043 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2045 goto nla_put_failure
;
2047 p
= nlmsg_data(nlh
);
2048 if (c
->event
== XFRM_MSG_DELSA
) {
2049 struct nlattr
*attr
;
2051 id
= nlmsg_data(nlh
);
2052 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2053 id
->spi
= x
->id
.spi
;
2054 id
->family
= x
->props
.family
;
2055 id
->proto
= x
->id
.proto
;
2057 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2059 goto nla_put_failure
;
2064 copy_to_user_state(x
, p
);
2067 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
2069 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
2071 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
2074 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
2076 nlmsg_end(skb
, nlh
);
2078 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2085 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2089 case XFRM_MSG_EXPIRE
:
2090 return xfrm_exp_state_notify(x
, c
);
2091 case XFRM_MSG_NEWAE
:
2092 return xfrm_aevent_state_notify(x
, c
);
2093 case XFRM_MSG_DELSA
:
2094 case XFRM_MSG_UPDSA
:
2095 case XFRM_MSG_NEWSA
:
2096 return xfrm_notify_sa(x
, c
);
2097 case XFRM_MSG_FLUSHSA
:
2098 return xfrm_notify_sa_flush(c
);
2100 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2108 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2109 struct xfrm_policy
*xp
)
2111 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2112 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2113 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2114 + userpolicy_type_attrsize();
2117 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2118 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2121 struct xfrm_user_acquire
*ua
;
2122 struct nlmsghdr
*nlh
;
2123 __u32 seq
= xfrm_get_acqseq();
2125 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2129 ua
= nlmsg_data(nlh
);
2130 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2131 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2132 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2133 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2134 ua
->aalgos
= xt
->aalgos
;
2135 ua
->ealgos
= xt
->ealgos
;
2136 ua
->calgos
= xt
->calgos
;
2137 ua
->seq
= x
->km
.seq
= seq
;
2139 if (copy_to_user_tmpl(xp
, skb
) < 0)
2141 if (copy_to_user_state_sec_ctx(x
, skb
))
2143 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2146 return nlmsg_end(skb
, nlh
);
2149 nlmsg_cancel(skb
, nlh
);
2153 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2154 struct xfrm_policy
*xp
, int dir
)
2156 struct sk_buff
*skb
;
2158 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2162 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2165 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2168 /* User gives us xfrm_user_policy_info followed by an array of 0
2169 * or more templates.
2171 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2172 u8
*data
, int len
, int *dir
)
2174 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2175 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2176 struct xfrm_policy
*xp
;
2179 switch (sk
->sk_family
) {
2181 if (opt
!= IP_XFRM_POLICY
) {
2186 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2188 if (opt
!= IPV6_XFRM_POLICY
) {
2201 if (len
< sizeof(*p
) ||
2202 verify_newpolicy_info(p
))
2205 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2206 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2209 if (p
->dir
> XFRM_POLICY_OUT
)
2212 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2218 copy_from_user_policy(xp
, p
);
2219 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2220 copy_templates(xp
, ut
, nr
);
2227 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2229 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2230 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2231 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2232 + userpolicy_type_attrsize();
2235 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2236 int dir
, struct km_event
*c
)
2238 struct xfrm_user_polexpire
*upe
;
2239 struct nlmsghdr
*nlh
;
2240 int hard
= c
->data
.hard
;
2242 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2246 upe
= nlmsg_data(nlh
);
2247 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2248 if (copy_to_user_tmpl(xp
, skb
) < 0)
2250 if (copy_to_user_sec_ctx(xp
, skb
))
2252 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2256 return nlmsg_end(skb
, nlh
);
2259 nlmsg_cancel(skb
, nlh
);
2263 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2265 struct sk_buff
*skb
;
2267 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2271 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2274 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2277 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2279 struct xfrm_userpolicy_info
*p
;
2280 struct xfrm_userpolicy_id
*id
;
2281 struct nlmsghdr
*nlh
;
2282 struct sk_buff
*skb
;
2283 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2286 headlen
= sizeof(*p
);
2287 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2288 len
+= nla_total_size(headlen
);
2289 headlen
= sizeof(*id
);
2291 len
+= userpolicy_type_attrsize();
2292 len
+= NLMSG_ALIGN(headlen
);
2294 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2298 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2302 p
= nlmsg_data(nlh
);
2303 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2304 struct nlattr
*attr
;
2306 id
= nlmsg_data(nlh
);
2307 memset(id
, 0, sizeof(*id
));
2310 id
->index
= xp
->index
;
2312 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2314 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2321 copy_to_user_policy(xp
, p
, dir
);
2322 if (copy_to_user_tmpl(xp
, skb
) < 0)
2324 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2327 nlmsg_end(skb
, nlh
);
2329 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2336 static int xfrm_notify_policy_flush(struct km_event
*c
)
2338 struct nlmsghdr
*nlh
;
2339 struct sk_buff
*skb
;
2341 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2345 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2348 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2351 nlmsg_end(skb
, nlh
);
2353 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2360 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2364 case XFRM_MSG_NEWPOLICY
:
2365 case XFRM_MSG_UPDPOLICY
:
2366 case XFRM_MSG_DELPOLICY
:
2367 return xfrm_notify_policy(xp
, dir
, c
);
2368 case XFRM_MSG_FLUSHPOLICY
:
2369 return xfrm_notify_policy_flush(c
);
2370 case XFRM_MSG_POLEXPIRE
:
2371 return xfrm_exp_policy_notify(xp
, dir
, c
);
2373 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2380 static inline size_t xfrm_report_msgsize(void)
2382 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2385 static int build_report(struct sk_buff
*skb
, u8 proto
,
2386 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2388 struct xfrm_user_report
*ur
;
2389 struct nlmsghdr
*nlh
;
2391 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2395 ur
= nlmsg_data(nlh
);
2397 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2400 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2402 return nlmsg_end(skb
, nlh
);
2405 nlmsg_cancel(skb
, nlh
);
2409 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2410 xfrm_address_t
*addr
)
2412 struct sk_buff
*skb
;
2414 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2418 if (build_report(skb
, proto
, sel
, addr
) < 0)
2421 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2424 static struct xfrm_mgr netlink_mgr
= {
2426 .notify
= xfrm_send_state_notify
,
2427 .acquire
= xfrm_send_acquire
,
2428 .compile_policy
= xfrm_compile_policy
,
2429 .notify_policy
= xfrm_send_policy_notify
,
2430 .report
= xfrm_send_report
,
2431 .migrate
= xfrm_send_migrate
,
2434 static int __init
xfrm_user_init(void)
2438 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2440 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2441 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2444 rcu_assign_pointer(xfrm_nl
, nlsk
);
2446 xfrm_register_km(&netlink_mgr
);
2451 static void __exit
xfrm_user_exit(void)
2453 struct sock
*nlsk
= xfrm_nl
;
2455 xfrm_unregister_km(&netlink_mgr
);
2456 rcu_assign_pointer(xfrm_nl
, NULL
);
2458 sock_release(nlsk
->sk_socket
);
2461 module_init(xfrm_user_init
);
2462 module_exit(xfrm_user_exit
);
2463 MODULE_LICENSE("GPL");
2464 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);