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 nlattr
**attrs
, enum xfrm_attr_type_t type
)
43 struct nlattr
*rt
= attrs
[type
];
44 struct xfrm_algo
*algp
;
50 if (nla_len(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 nlattr
**attrs
, enum xfrm_attr_type_t type
,
79 xfrm_address_t
**addrp
)
81 struct nlattr
*rt
= attrs
[type
];
84 *addrp
= nla_data(rt
);
87 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
89 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
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 nlattr
**attrs
)
114 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
126 switch (p
->id
.proto
) {
128 if (!attrs
[XFRMA_ALG_AUTH
] ||
129 attrs
[XFRMA_ALG_CRYPT
] ||
130 attrs
[XFRMA_ALG_COMP
])
135 if ((!attrs
[XFRMA_ALG_AUTH
] &&
136 !attrs
[XFRMA_ALG_CRYPT
]) ||
137 attrs
[XFRMA_ALG_COMP
])
142 if (!attrs
[XFRMA_ALG_COMP
] ||
143 attrs
[XFRMA_ALG_AUTH
] ||
144 attrs
[XFRMA_ALG_CRYPT
])
148 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
149 case IPPROTO_DSTOPTS
:
150 case IPPROTO_ROUTING
:
151 if (attrs
[XFRMA_ALG_COMP
] ||
152 attrs
[XFRMA_ALG_AUTH
] ||
153 attrs
[XFRMA_ALG_CRYPT
] ||
154 attrs
[XFRMA_ENCAP
] ||
155 attrs
[XFRMA_SEC_CTX
] ||
156 !attrs
[XFRMA_COADDR
])
165 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
167 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
169 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
171 if ((err
= verify_sec_ctx_len(attrs
)))
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),
196 struct xfrm_algo
*p
, *ualg
;
197 struct xfrm_algo_desc
*algo
;
202 ualg
= nla_data(rta
);
204 algo
= get_byname(ualg
->alg_name
, 1);
207 *props
= algo
->desc
.sadb_alg_id
;
209 p
= kmemdup(ualg
, alg_len(ualg
), GFP_KERNEL
);
213 strcpy(p
->alg_name
, algo
->name
);
218 static int attach_encap_tmpl(struct xfrm_encap_tmpl
**encapp
, struct nlattr
*rta
)
220 struct xfrm_encap_tmpl
*p
, *uencap
;
225 uencap
= nla_data(rta
);
226 p
= kmemdup(uencap
, sizeof(*p
), GFP_KERNEL
);
235 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
240 len
+= sizeof(struct xfrm_user_sec_ctx
);
241 len
+= xfrm_ctx
->ctx_len
;
246 static int attach_sec_ctx(struct xfrm_state
*x
, struct nlattr
*u_arg
)
248 struct xfrm_user_sec_ctx
*uctx
;
253 uctx
= nla_data(u_arg
);
254 return security_xfrm_state_alloc(x
, uctx
);
257 static int attach_one_addr(xfrm_address_t
**addrpp
, struct nlattr
*rta
)
259 xfrm_address_t
*p
, *uaddrp
;
264 uaddrp
= nla_data(rta
);
265 p
= kmemdup(uaddrp
, sizeof(*p
), GFP_KERNEL
);
273 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
275 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
276 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
277 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
278 x
->props
.mode
= p
->mode
;
279 x
->props
.replay_window
= p
->replay_window
;
280 x
->props
.reqid
= p
->reqid
;
281 x
->props
.family
= p
->family
;
282 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
283 x
->props
.flags
= p
->flags
;
286 * Set inner address family if the KM left it as zero.
287 * See comment in validate_tmpl.
290 x
->sel
.family
= p
->family
;
294 * someday when pfkey also has support, we could have the code
295 * somehow made shareable and move it to xfrm_state.c - JHS
298 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
300 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
301 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
302 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
303 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
306 struct xfrm_replay_state
*replay
;
307 replay
= nla_data(rp
);
308 memcpy(&x
->replay
, replay
, sizeof(*replay
));
309 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
313 struct xfrm_lifetime_cur
*ltime
;
314 ltime
= nla_data(lt
);
315 x
->curlft
.bytes
= ltime
->bytes
;
316 x
->curlft
.packets
= ltime
->packets
;
317 x
->curlft
.add_time
= ltime
->add_time
;
318 x
->curlft
.use_time
= ltime
->use_time
;
322 x
->replay_maxage
= nla_get_u32(et
);
325 x
->replay_maxdiff
= nla_get_u32(rt
);
328 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
329 struct nlattr
**attrs
,
332 struct xfrm_state
*x
= xfrm_state_alloc();
338 copy_from_user_state(x
, p
);
340 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
341 xfrm_aalg_get_byname
,
342 attrs
[XFRMA_ALG_AUTH
])))
344 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
345 xfrm_ealg_get_byname
,
346 attrs
[XFRMA_ALG_CRYPT
])))
348 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
349 xfrm_calg_get_byname
,
350 attrs
[XFRMA_ALG_COMP
])))
352 if ((err
= attach_encap_tmpl(&x
->encap
, attrs
[XFRMA_ENCAP
])))
354 if ((err
= attach_one_addr(&x
->coaddr
, attrs
[XFRMA_COADDR
])))
356 err
= xfrm_init_state(x
);
360 if ((err
= attach_sec_ctx(x
, attrs
[XFRMA_SEC_CTX
])))
364 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
365 /* sysctl_xfrm_aevent_etime is in 100ms units */
366 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
367 x
->preplay
.bitmap
= 0;
368 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
369 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
371 /* override default values from above */
373 xfrm_update_ae_params(x
, attrs
);
378 x
->km
.state
= XFRM_STATE_DEAD
;
385 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
386 struct nlattr
**attrs
)
388 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
389 struct xfrm_state
*x
;
393 err
= verify_newsa_info(p
, attrs
);
397 x
= xfrm_state_construct(p
, attrs
, &err
);
402 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
403 err
= xfrm_state_add(x
);
405 err
= xfrm_state_update(x
);
407 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
408 AUDIT_MAC_IPSEC_ADDSA
, err
? 0 : 1, NULL
, x
);
411 x
->km
.state
= XFRM_STATE_DEAD
;
416 c
.seq
= nlh
->nlmsg_seq
;
417 c
.pid
= nlh
->nlmsg_pid
;
418 c
.event
= nlh
->nlmsg_type
;
420 km_state_notify(x
, &c
);
426 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
427 struct nlattr
**attrs
,
430 struct xfrm_state
*x
= NULL
;
433 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
435 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
437 xfrm_address_t
*saddr
= NULL
;
439 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
446 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
456 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
457 struct nlattr
**attrs
)
459 struct xfrm_state
*x
;
462 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
464 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
468 if ((err
= security_xfrm_state_delete(x
)) != 0)
471 if (xfrm_state_kern(x
)) {
476 err
= xfrm_state_delete(x
);
481 c
.seq
= nlh
->nlmsg_seq
;
482 c
.pid
= nlh
->nlmsg_pid
;
483 c
.event
= nlh
->nlmsg_type
;
484 km_state_notify(x
, &c
);
487 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
488 AUDIT_MAC_IPSEC_DELSA
, err
? 0 : 1, NULL
, x
);
493 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
495 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
496 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
497 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
498 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
499 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
500 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
501 p
->mode
= x
->props
.mode
;
502 p
->replay_window
= x
->props
.replay_window
;
503 p
->reqid
= x
->props
.reqid
;
504 p
->family
= x
->props
.family
;
505 p
->flags
= x
->props
.flags
;
509 struct xfrm_dump_info
{
510 struct sk_buff
*in_skb
;
511 struct sk_buff
*out_skb
;
518 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
520 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
521 struct xfrm_user_sec_ctx
*uctx
;
524 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
528 uctx
= nla_data(attr
);
529 uctx
->exttype
= XFRMA_SEC_CTX
;
530 uctx
->len
= ctx_size
;
531 uctx
->ctx_doi
= s
->ctx_doi
;
532 uctx
->ctx_alg
= s
->ctx_alg
;
533 uctx
->ctx_len
= s
->ctx_len
;
534 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
539 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
541 struct xfrm_dump_info
*sp
= ptr
;
542 struct sk_buff
*in_skb
= sp
->in_skb
;
543 struct sk_buff
*skb
= sp
->out_skb
;
544 struct xfrm_usersa_info
*p
;
545 struct nlmsghdr
*nlh
;
547 if (sp
->this_idx
< sp
->start_idx
)
550 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
551 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
556 copy_to_user_state(x
, p
);
559 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
561 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
563 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
566 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
568 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
569 goto nla_put_failure
;
572 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
575 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
583 nlmsg_cancel(skb
, nlh
);
587 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
589 struct xfrm_dump_info info
;
591 info
.in_skb
= cb
->skb
;
593 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
594 info
.nlmsg_flags
= NLM_F_MULTI
;
596 info
.start_idx
= cb
->args
[0];
597 (void) xfrm_state_walk(0, dump_one_state
, &info
);
598 cb
->args
[0] = info
.this_idx
;
603 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
604 struct xfrm_state
*x
, u32 seq
)
606 struct xfrm_dump_info info
;
609 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
611 return ERR_PTR(-ENOMEM
);
613 info
.in_skb
= in_skb
;
615 info
.nlmsg_seq
= seq
;
616 info
.nlmsg_flags
= 0;
617 info
.this_idx
= info
.start_idx
= 0;
619 if (dump_one_state(x
, 0, &info
)) {
627 static inline size_t xfrm_spdinfo_msgsize(void)
629 return NLMSG_ALIGN(4)
630 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
631 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
634 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
636 struct xfrmk_spdinfo si
;
637 struct xfrmu_spdinfo spc
;
638 struct xfrmu_spdhinfo sph
;
639 struct nlmsghdr
*nlh
;
642 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
643 if (nlh
== NULL
) /* shouldnt really happen ... */
648 xfrm_spd_getinfo(&si
);
649 spc
.incnt
= si
.incnt
;
650 spc
.outcnt
= si
.outcnt
;
651 spc
.fwdcnt
= si
.fwdcnt
;
652 spc
.inscnt
= si
.inscnt
;
653 spc
.outscnt
= si
.outscnt
;
654 spc
.fwdscnt
= si
.fwdscnt
;
655 sph
.spdhcnt
= si
.spdhcnt
;
656 sph
.spdhmcnt
= si
.spdhmcnt
;
658 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
659 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
661 return nlmsg_end(skb
, nlh
);
664 nlmsg_cancel(skb
, nlh
);
668 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
669 struct nlattr
**attrs
)
671 struct sk_buff
*r_skb
;
672 u32
*flags
= nlmsg_data(nlh
);
673 u32 spid
= NETLINK_CB(skb
).pid
;
674 u32 seq
= nlh
->nlmsg_seq
;
676 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
680 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
683 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
686 static inline size_t xfrm_sadinfo_msgsize(void)
688 return NLMSG_ALIGN(4)
689 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
690 + nla_total_size(4); /* XFRMA_SAD_CNT */
693 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
695 struct xfrmk_sadinfo si
;
696 struct xfrmu_sadhinfo sh
;
697 struct nlmsghdr
*nlh
;
700 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
701 if (nlh
== NULL
) /* shouldnt really happen ... */
706 xfrm_sad_getinfo(&si
);
708 sh
.sadhmcnt
= si
.sadhmcnt
;
709 sh
.sadhcnt
= si
.sadhcnt
;
711 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
712 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
714 return nlmsg_end(skb
, nlh
);
717 nlmsg_cancel(skb
, nlh
);
721 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
722 struct nlattr
**attrs
)
724 struct sk_buff
*r_skb
;
725 u32
*flags
= nlmsg_data(nlh
);
726 u32 spid
= NETLINK_CB(skb
).pid
;
727 u32 seq
= nlh
->nlmsg_seq
;
729 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
733 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
736 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
739 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
740 struct nlattr
**attrs
)
742 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
743 struct xfrm_state
*x
;
744 struct sk_buff
*resp_skb
;
747 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
751 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
752 if (IS_ERR(resp_skb
)) {
753 err
= PTR_ERR(resp_skb
);
755 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
762 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
764 switch (p
->info
.id
.proto
) {
770 /* IPCOMP spi is 16-bits. */
771 if (p
->max
>= 0x10000)
785 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
786 struct nlattr
**attrs
)
788 struct xfrm_state
*x
;
789 struct xfrm_userspi_info
*p
;
790 struct sk_buff
*resp_skb
;
791 xfrm_address_t
*daddr
;
796 err
= verify_userspi_info(p
);
800 family
= p
->info
.family
;
801 daddr
= &p
->info
.id
.daddr
;
805 x
= xfrm_find_acq_byseq(p
->info
.seq
);
806 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
813 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
814 p
->info
.id
.proto
, daddr
,
821 resp_skb
= ERR_PTR(-ENOENT
);
823 spin_lock_bh(&x
->lock
);
824 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
825 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
827 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
829 spin_unlock_bh(&x
->lock
);
831 if (IS_ERR(resp_skb
)) {
832 err
= PTR_ERR(resp_skb
);
836 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
844 static int verify_policy_dir(u8 dir
)
848 case XFRM_POLICY_OUT
:
849 case XFRM_POLICY_FWD
:
859 static int verify_policy_type(u8 type
)
862 case XFRM_POLICY_TYPE_MAIN
:
863 #ifdef CONFIG_XFRM_SUB_POLICY
864 case XFRM_POLICY_TYPE_SUB
:
875 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
879 case XFRM_SHARE_SESSION
:
880 case XFRM_SHARE_USER
:
881 case XFRM_SHARE_UNIQUE
:
889 case XFRM_POLICY_ALLOW
:
890 case XFRM_POLICY_BLOCK
:
897 switch (p
->sel
.family
) {
902 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
905 return -EAFNOSUPPORT
;
912 return verify_policy_dir(p
->dir
);
915 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
917 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
918 struct xfrm_user_sec_ctx
*uctx
;
924 return security_xfrm_policy_alloc(pol
, uctx
);
927 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
933 for (i
= 0; i
< nr
; i
++, ut
++) {
934 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
936 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
937 memcpy(&t
->saddr
, &ut
->saddr
,
938 sizeof(xfrm_address_t
));
939 t
->reqid
= ut
->reqid
;
941 t
->share
= ut
->share
;
942 t
->optional
= ut
->optional
;
943 t
->aalgos
= ut
->aalgos
;
944 t
->ealgos
= ut
->ealgos
;
945 t
->calgos
= ut
->calgos
;
946 t
->encap_family
= ut
->family
;
950 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
954 if (nr
> XFRM_MAX_DEPTH
)
957 for (i
= 0; i
< nr
; i
++) {
958 /* We never validated the ut->family value, so many
959 * applications simply leave it at zero. The check was
960 * never made and ut->family was ignored because all
961 * templates could be assumed to have the same family as
962 * the policy itself. Now that we will have ipv4-in-ipv6
963 * and ipv6-in-ipv4 tunnels, this is no longer true.
966 ut
[i
].family
= family
;
968 switch (ut
[i
].family
) {
971 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
983 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
985 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
990 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
991 int nr
= nla_len(rt
) / sizeof(*utmpl
);
994 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
998 copy_templates(pol
, utmpl
, nr
);
1003 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1005 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1006 struct xfrm_userpolicy_type
*upt
;
1007 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1015 err
= verify_policy_type(type
);
1023 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1025 xp
->priority
= p
->priority
;
1026 xp
->index
= p
->index
;
1027 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1028 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1029 xp
->action
= p
->action
;
1030 xp
->flags
= p
->flags
;
1031 xp
->family
= p
->sel
.family
;
1032 /* XXX xp->share = p->share; */
1035 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1037 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1038 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1039 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1040 p
->priority
= xp
->priority
;
1041 p
->index
= xp
->index
;
1042 p
->sel
.family
= xp
->family
;
1044 p
->action
= xp
->action
;
1045 p
->flags
= xp
->flags
;
1046 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1049 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1051 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1059 copy_from_user_policy(xp
, p
);
1061 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1065 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1066 err
= copy_from_user_sec_ctx(xp
, attrs
);
1077 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1078 struct nlattr
**attrs
)
1080 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1081 struct xfrm_policy
*xp
;
1086 err
= verify_newpolicy_info(p
);
1089 err
= verify_sec_ctx_len(attrs
);
1093 xp
= xfrm_policy_construct(p
, attrs
, &err
);
1097 /* shouldnt excl be based on nlh flags??
1098 * Aha! this is anti-netlink really i.e more pfkey derived
1099 * in netlink excl is a flag and you wouldnt need
1100 * a type XFRM_MSG_UPDPOLICY - JHS */
1101 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1102 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1103 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1104 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1107 security_xfrm_policy_free(xp
);
1112 c
.event
= nlh
->nlmsg_type
;
1113 c
.seq
= nlh
->nlmsg_seq
;
1114 c
.pid
= nlh
->nlmsg_pid
;
1115 km_policy_notify(xp
, p
->dir
, &c
);
1122 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1124 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1127 if (xp
->xfrm_nr
== 0)
1130 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1131 struct xfrm_user_tmpl
*up
= &vec
[i
];
1132 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1134 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1135 up
->family
= kp
->encap_family
;
1136 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1137 up
->reqid
= kp
->reqid
;
1138 up
->mode
= kp
->mode
;
1139 up
->share
= kp
->share
;
1140 up
->optional
= kp
->optional
;
1141 up
->aalgos
= kp
->aalgos
;
1142 up
->ealgos
= kp
->ealgos
;
1143 up
->calgos
= kp
->calgos
;
1146 return nla_put(skb
, XFRMA_TMPL
,
1147 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1150 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1153 return copy_sec_ctx(x
->security
, skb
);
1158 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1161 return copy_sec_ctx(xp
->security
, skb
);
1165 static inline size_t userpolicy_type_attrsize(void)
1167 #ifdef CONFIG_XFRM_SUB_POLICY
1168 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1174 #ifdef CONFIG_XFRM_SUB_POLICY
1175 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1177 struct xfrm_userpolicy_type upt
= {
1181 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1185 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1191 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1193 struct xfrm_dump_info
*sp
= ptr
;
1194 struct xfrm_userpolicy_info
*p
;
1195 struct sk_buff
*in_skb
= sp
->in_skb
;
1196 struct sk_buff
*skb
= sp
->out_skb
;
1197 struct nlmsghdr
*nlh
;
1199 if (sp
->this_idx
< sp
->start_idx
)
1202 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1203 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1207 p
= nlmsg_data(nlh
);
1208 copy_to_user_policy(xp
, p
, dir
);
1209 if (copy_to_user_tmpl(xp
, skb
) < 0)
1211 if (copy_to_user_sec_ctx(xp
, skb
))
1213 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1216 nlmsg_end(skb
, nlh
);
1222 nlmsg_cancel(skb
, nlh
);
1226 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1228 struct xfrm_dump_info info
;
1230 info
.in_skb
= cb
->skb
;
1232 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1233 info
.nlmsg_flags
= NLM_F_MULTI
;
1235 info
.start_idx
= cb
->args
[0];
1236 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1237 #ifdef CONFIG_XFRM_SUB_POLICY
1238 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1240 cb
->args
[0] = info
.this_idx
;
1245 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1246 struct xfrm_policy
*xp
,
1249 struct xfrm_dump_info info
;
1250 struct sk_buff
*skb
;
1252 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1254 return ERR_PTR(-ENOMEM
);
1256 info
.in_skb
= in_skb
;
1258 info
.nlmsg_seq
= seq
;
1259 info
.nlmsg_flags
= 0;
1260 info
.this_idx
= info
.start_idx
= 0;
1262 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1270 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1271 struct nlattr
**attrs
)
1273 struct xfrm_policy
*xp
;
1274 struct xfrm_userpolicy_id
*p
;
1275 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1280 p
= nlmsg_data(nlh
);
1281 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1283 err
= copy_from_user_policy_type(&type
, attrs
);
1287 err
= verify_policy_dir(p
->dir
);
1292 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1294 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1295 struct xfrm_policy tmp
;
1297 err
= verify_sec_ctx_len(attrs
);
1301 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1303 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1305 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1308 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1310 security_xfrm_policy_free(&tmp
);
1316 struct sk_buff
*resp_skb
;
1318 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1319 if (IS_ERR(resp_skb
)) {
1320 err
= PTR_ERR(resp_skb
);
1322 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1323 NETLINK_CB(skb
).pid
);
1326 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1327 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1332 c
.data
.byid
= p
->index
;
1333 c
.event
= nlh
->nlmsg_type
;
1334 c
.seq
= nlh
->nlmsg_seq
;
1335 c
.pid
= nlh
->nlmsg_pid
;
1336 km_policy_notify(xp
, p
->dir
, &c
);
1344 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1345 struct nlattr
**attrs
)
1348 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1349 struct xfrm_audit audit_info
;
1352 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1353 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1354 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1357 c
.data
.proto
= p
->proto
;
1358 c
.event
= nlh
->nlmsg_type
;
1359 c
.seq
= nlh
->nlmsg_seq
;
1360 c
.pid
= nlh
->nlmsg_pid
;
1361 km_state_notify(NULL
, &c
);
1366 static inline size_t xfrm_aevent_msgsize(void)
1368 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1369 + nla_total_size(sizeof(struct xfrm_replay_state
))
1370 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1371 + nla_total_size(4) /* XFRM_AE_RTHR */
1372 + nla_total_size(4); /* XFRM_AE_ETHR */
1375 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1377 struct xfrm_aevent_id
*id
;
1378 struct nlmsghdr
*nlh
;
1380 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1384 id
= nlmsg_data(nlh
);
1385 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1386 id
->sa_id
.spi
= x
->id
.spi
;
1387 id
->sa_id
.family
= x
->props
.family
;
1388 id
->sa_id
.proto
= x
->id
.proto
;
1389 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1390 id
->reqid
= x
->props
.reqid
;
1391 id
->flags
= c
->data
.aevent
;
1393 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1394 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1396 if (id
->flags
& XFRM_AE_RTHR
)
1397 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1399 if (id
->flags
& XFRM_AE_ETHR
)
1400 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1401 x
->replay_maxage
* 10 / HZ
);
1403 return nlmsg_end(skb
, nlh
);
1406 nlmsg_cancel(skb
, nlh
);
1410 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1411 struct nlattr
**attrs
)
1413 struct xfrm_state
*x
;
1414 struct sk_buff
*r_skb
;
1417 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1418 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1420 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1424 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1431 * XXX: is this lock really needed - none of the other
1432 * gets lock (the concern is things getting updated
1433 * while we are still reading) - jhs
1435 spin_lock_bh(&x
->lock
);
1436 c
.data
.aevent
= p
->flags
;
1437 c
.seq
= nlh
->nlmsg_seq
;
1438 c
.pid
= nlh
->nlmsg_pid
;
1440 if (build_aevent(r_skb
, x
, &c
) < 0)
1442 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1443 spin_unlock_bh(&x
->lock
);
1448 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1449 struct nlattr
**attrs
)
1451 struct xfrm_state
*x
;
1454 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1455 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1456 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1461 /* pedantic mode - thou shalt sayeth replaceth */
1462 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1465 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1469 if (x
->km
.state
!= XFRM_STATE_VALID
)
1472 spin_lock_bh(&x
->lock
);
1473 xfrm_update_ae_params(x
, attrs
);
1474 spin_unlock_bh(&x
->lock
);
1476 c
.event
= nlh
->nlmsg_type
;
1477 c
.seq
= nlh
->nlmsg_seq
;
1478 c
.pid
= nlh
->nlmsg_pid
;
1479 c
.data
.aevent
= XFRM_AE_CU
;
1480 km_state_notify(x
, &c
);
1487 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1488 struct nlattr
**attrs
)
1491 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1493 struct xfrm_audit audit_info
;
1495 err
= copy_from_user_policy_type(&type
, attrs
);
1499 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1500 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1501 err
= xfrm_policy_flush(type
, &audit_info
);
1505 c
.event
= nlh
->nlmsg_type
;
1506 c
.seq
= nlh
->nlmsg_seq
;
1507 c
.pid
= nlh
->nlmsg_pid
;
1508 km_policy_notify(NULL
, 0, &c
);
1512 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1513 struct nlattr
**attrs
)
1515 struct xfrm_policy
*xp
;
1516 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1517 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1518 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1521 err
= copy_from_user_policy_type(&type
, attrs
);
1526 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1528 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1529 struct xfrm_policy tmp
;
1531 err
= verify_sec_ctx_len(attrs
);
1535 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1537 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1539 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1542 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1544 security_xfrm_policy_free(&tmp
);
1549 read_lock(&xp
->lock
);
1551 read_unlock(&xp
->lock
);
1555 read_unlock(&xp
->lock
);
1558 xfrm_policy_delete(xp
, p
->dir
);
1559 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1560 AUDIT_MAC_IPSEC_DELSPD
, 1, xp
, NULL
);
1563 // reset the timers here?
1564 printk("Dont know what to do with soft policy expire\n");
1566 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1573 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1574 struct nlattr
**attrs
)
1576 struct xfrm_state
*x
;
1578 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1579 struct xfrm_usersa_info
*p
= &ue
->state
;
1581 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1587 spin_lock_bh(&x
->lock
);
1589 if (x
->km
.state
!= XFRM_STATE_VALID
)
1591 km_state_expired(x
, ue
->hard
, current
->pid
);
1594 __xfrm_state_delete(x
);
1595 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1596 AUDIT_MAC_IPSEC_DELSA
, 1, NULL
, x
);
1600 spin_unlock_bh(&x
->lock
);
1605 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1606 struct nlattr
**attrs
)
1608 struct xfrm_policy
*xp
;
1609 struct xfrm_user_tmpl
*ut
;
1611 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1613 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1614 struct xfrm_state
*x
= xfrm_state_alloc();
1620 err
= verify_newpolicy_info(&ua
->policy
);
1622 printk("BAD policy passed\n");
1628 xp
= xfrm_policy_construct(&ua
->policy
, attrs
, &err
);
1634 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1635 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1636 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1639 /* extract the templates and for each call km_key */
1640 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1641 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1642 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1643 x
->props
.mode
= t
->mode
;
1644 x
->props
.reqid
= t
->reqid
;
1645 x
->props
.family
= ut
->family
;
1646 t
->aalgos
= ua
->aalgos
;
1647 t
->ealgos
= ua
->ealgos
;
1648 t
->calgos
= ua
->calgos
;
1649 err
= km_query(x
, t
, xp
);
1659 #ifdef CONFIG_XFRM_MIGRATE
1660 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1661 struct nlattr
**attrs
, int *num
)
1663 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1664 struct xfrm_user_migrate
*um
;
1668 num_migrate
= nla_len(rt
) / sizeof(*um
);
1670 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1673 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1674 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1675 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1676 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1677 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1679 ma
->proto
= um
->proto
;
1680 ma
->mode
= um
->mode
;
1681 ma
->reqid
= um
->reqid
;
1683 ma
->old_family
= um
->old_family
;
1684 ma
->new_family
= um
->new_family
;
1691 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1692 struct nlattr
**attrs
)
1694 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1695 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1700 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1703 err
= copy_from_user_policy_type(&type
, attrs
);
1707 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1715 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1720 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1721 struct nlattr
**attrs
)
1723 return -ENOPROTOOPT
;
1727 #ifdef CONFIG_XFRM_MIGRATE
1728 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1730 struct xfrm_user_migrate um
;
1732 memset(&um
, 0, sizeof(um
));
1733 um
.proto
= m
->proto
;
1735 um
.reqid
= m
->reqid
;
1736 um
.old_family
= m
->old_family
;
1737 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1738 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1739 um
.new_family
= m
->new_family
;
1740 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1741 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1743 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1746 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1748 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1749 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1750 + userpolicy_type_attrsize();
1753 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1754 int num_migrate
, struct xfrm_selector
*sel
,
1757 struct xfrm_migrate
*mp
;
1758 struct xfrm_userpolicy_id
*pol_id
;
1759 struct nlmsghdr
*nlh
;
1762 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1766 pol_id
= nlmsg_data(nlh
);
1767 /* copy data from selector, dir, and type to the pol_id */
1768 memset(pol_id
, 0, sizeof(*pol_id
));
1769 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1772 if (copy_to_user_policy_type(type
, skb
) < 0)
1775 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1776 if (copy_to_user_migrate(mp
, skb
) < 0)
1780 return nlmsg_end(skb
, nlh
);
1782 nlmsg_cancel(skb
, nlh
);
1786 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1787 struct xfrm_migrate
*m
, int num_migrate
)
1789 struct sk_buff
*skb
;
1791 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1796 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1799 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1802 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1803 struct xfrm_migrate
*m
, int num_migrate
)
1805 return -ENOPROTOOPT
;
1809 #define XMSGSIZE(type) sizeof(struct type)
1811 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1812 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1813 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1814 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1815 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1816 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1817 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1818 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1819 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1820 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1821 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1822 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1823 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1824 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1825 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1826 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1827 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1828 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1829 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1830 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1831 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1836 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1837 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1838 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1839 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1840 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1841 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1842 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1843 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1844 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1845 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1846 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1847 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1848 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1849 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1850 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1853 static struct xfrm_link
{
1854 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
1855 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1856 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1857 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1858 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1859 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1860 .dump
= xfrm_dump_sa
},
1861 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1862 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1863 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1864 .dump
= xfrm_dump_policy
},
1865 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1866 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1867 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1868 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1869 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1870 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1871 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1872 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1873 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1874 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1875 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1876 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1877 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1880 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1882 struct nlattr
*attrs
[XFRMA_MAX
+1];
1883 struct xfrm_link
*link
;
1886 type
= nlh
->nlmsg_type
;
1887 if (type
> XFRM_MSG_MAX
)
1890 type
-= XFRM_MSG_BASE
;
1891 link
= &xfrm_dispatch
[type
];
1893 /* All operations require privileges, even GET */
1894 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1897 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1898 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1899 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1900 if (link
->dump
== NULL
)
1903 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, NULL
);
1906 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
1911 if (link
->doit
== NULL
)
1914 return link
->doit(skb
, nlh
, attrs
);
1917 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1919 unsigned int qlen
= 0;
1922 mutex_lock(&xfrm_cfg_mutex
);
1923 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1924 mutex_unlock(&xfrm_cfg_mutex
);
1929 static inline size_t xfrm_expire_msgsize(void)
1931 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1934 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1936 struct xfrm_user_expire
*ue
;
1937 struct nlmsghdr
*nlh
;
1939 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1943 ue
= nlmsg_data(nlh
);
1944 copy_to_user_state(x
, &ue
->state
);
1945 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1947 return nlmsg_end(skb
, nlh
);
1950 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1952 struct sk_buff
*skb
;
1954 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
1958 if (build_expire(skb
, x
, c
) < 0)
1961 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1964 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1966 struct sk_buff
*skb
;
1968 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1972 if (build_aevent(skb
, x
, c
) < 0)
1975 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1978 static int xfrm_notify_sa_flush(struct km_event
*c
)
1980 struct xfrm_usersa_flush
*p
;
1981 struct nlmsghdr
*nlh
;
1982 struct sk_buff
*skb
;
1983 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
1985 skb
= nlmsg_new(len
, GFP_ATOMIC
);
1989 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
1995 p
= nlmsg_data(nlh
);
1996 p
->proto
= c
->data
.proto
;
1998 nlmsg_end(skb
, nlh
);
2000 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2003 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2007 l
+= nla_total_size(alg_len(x
->aalg
));
2009 l
+= nla_total_size(alg_len(x
->ealg
));
2011 l
+= nla_total_size(sizeof(*x
->calg
));
2013 l
+= nla_total_size(sizeof(*x
->encap
));
2018 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2020 struct xfrm_usersa_info
*p
;
2021 struct xfrm_usersa_id
*id
;
2022 struct nlmsghdr
*nlh
;
2023 struct sk_buff
*skb
;
2024 int len
= xfrm_sa_len(x
);
2027 headlen
= sizeof(*p
);
2028 if (c
->event
== XFRM_MSG_DELSA
) {
2029 len
+= nla_total_size(headlen
);
2030 headlen
= sizeof(*id
);
2032 len
+= NLMSG_ALIGN(headlen
);
2034 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2038 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2040 goto nla_put_failure
;
2042 p
= nlmsg_data(nlh
);
2043 if (c
->event
== XFRM_MSG_DELSA
) {
2044 struct nlattr
*attr
;
2046 id
= nlmsg_data(nlh
);
2047 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2048 id
->spi
= x
->id
.spi
;
2049 id
->family
= x
->props
.family
;
2050 id
->proto
= x
->id
.proto
;
2052 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2054 goto nla_put_failure
;
2059 copy_to_user_state(x
, p
);
2062 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
2064 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
2066 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
2069 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
2071 nlmsg_end(skb
, nlh
);
2073 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2080 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2084 case XFRM_MSG_EXPIRE
:
2085 return xfrm_exp_state_notify(x
, c
);
2086 case XFRM_MSG_NEWAE
:
2087 return xfrm_aevent_state_notify(x
, c
);
2088 case XFRM_MSG_DELSA
:
2089 case XFRM_MSG_UPDSA
:
2090 case XFRM_MSG_NEWSA
:
2091 return xfrm_notify_sa(x
, c
);
2092 case XFRM_MSG_FLUSHSA
:
2093 return xfrm_notify_sa_flush(c
);
2095 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2103 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2104 struct xfrm_policy
*xp
)
2106 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2107 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2108 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2109 + userpolicy_type_attrsize();
2112 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2113 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2116 struct xfrm_user_acquire
*ua
;
2117 struct nlmsghdr
*nlh
;
2118 __u32 seq
= xfrm_get_acqseq();
2120 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2124 ua
= nlmsg_data(nlh
);
2125 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2126 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2127 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2128 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2129 ua
->aalgos
= xt
->aalgos
;
2130 ua
->ealgos
= xt
->ealgos
;
2131 ua
->calgos
= xt
->calgos
;
2132 ua
->seq
= x
->km
.seq
= seq
;
2134 if (copy_to_user_tmpl(xp
, skb
) < 0)
2136 if (copy_to_user_state_sec_ctx(x
, skb
))
2138 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2141 return nlmsg_end(skb
, nlh
);
2144 nlmsg_cancel(skb
, nlh
);
2148 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2149 struct xfrm_policy
*xp
, int dir
)
2151 struct sk_buff
*skb
;
2153 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2157 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2160 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2163 /* User gives us xfrm_user_policy_info followed by an array of 0
2164 * or more templates.
2166 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2167 u8
*data
, int len
, int *dir
)
2169 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2170 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2171 struct xfrm_policy
*xp
;
2174 switch (sk
->sk_family
) {
2176 if (opt
!= IP_XFRM_POLICY
) {
2181 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2183 if (opt
!= IPV6_XFRM_POLICY
) {
2196 if (len
< sizeof(*p
) ||
2197 verify_newpolicy_info(p
))
2200 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2201 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2204 if (p
->dir
> XFRM_POLICY_OUT
)
2207 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2213 copy_from_user_policy(xp
, p
);
2214 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2215 copy_templates(xp
, ut
, nr
);
2222 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2224 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2225 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2226 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2227 + userpolicy_type_attrsize();
2230 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2231 int dir
, struct km_event
*c
)
2233 struct xfrm_user_polexpire
*upe
;
2234 struct nlmsghdr
*nlh
;
2235 int hard
= c
->data
.hard
;
2237 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2241 upe
= nlmsg_data(nlh
);
2242 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2243 if (copy_to_user_tmpl(xp
, skb
) < 0)
2245 if (copy_to_user_sec_ctx(xp
, skb
))
2247 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2251 return nlmsg_end(skb
, nlh
);
2254 nlmsg_cancel(skb
, nlh
);
2258 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2260 struct sk_buff
*skb
;
2262 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2266 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2269 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2272 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2274 struct xfrm_userpolicy_info
*p
;
2275 struct xfrm_userpolicy_id
*id
;
2276 struct nlmsghdr
*nlh
;
2277 struct sk_buff
*skb
;
2278 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2281 headlen
= sizeof(*p
);
2282 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2283 len
+= nla_total_size(headlen
);
2284 headlen
= sizeof(*id
);
2286 len
+= userpolicy_type_attrsize();
2287 len
+= NLMSG_ALIGN(headlen
);
2289 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2293 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2297 p
= nlmsg_data(nlh
);
2298 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2299 struct nlattr
*attr
;
2301 id
= nlmsg_data(nlh
);
2302 memset(id
, 0, sizeof(*id
));
2305 id
->index
= xp
->index
;
2307 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2309 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2316 copy_to_user_policy(xp
, p
, dir
);
2317 if (copy_to_user_tmpl(xp
, skb
) < 0)
2319 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2322 nlmsg_end(skb
, nlh
);
2324 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2331 static int xfrm_notify_policy_flush(struct km_event
*c
)
2333 struct nlmsghdr
*nlh
;
2334 struct sk_buff
*skb
;
2336 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2340 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2343 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2346 nlmsg_end(skb
, nlh
);
2348 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2355 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2359 case XFRM_MSG_NEWPOLICY
:
2360 case XFRM_MSG_UPDPOLICY
:
2361 case XFRM_MSG_DELPOLICY
:
2362 return xfrm_notify_policy(xp
, dir
, c
);
2363 case XFRM_MSG_FLUSHPOLICY
:
2364 return xfrm_notify_policy_flush(c
);
2365 case XFRM_MSG_POLEXPIRE
:
2366 return xfrm_exp_policy_notify(xp
, dir
, c
);
2368 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2375 static inline size_t xfrm_report_msgsize(void)
2377 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2380 static int build_report(struct sk_buff
*skb
, u8 proto
,
2381 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2383 struct xfrm_user_report
*ur
;
2384 struct nlmsghdr
*nlh
;
2386 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2390 ur
= nlmsg_data(nlh
);
2392 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2395 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2397 return nlmsg_end(skb
, nlh
);
2400 nlmsg_cancel(skb
, nlh
);
2404 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2405 xfrm_address_t
*addr
)
2407 struct sk_buff
*skb
;
2409 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2413 if (build_report(skb
, proto
, sel
, addr
) < 0)
2416 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2419 static struct xfrm_mgr netlink_mgr
= {
2421 .notify
= xfrm_send_state_notify
,
2422 .acquire
= xfrm_send_acquire
,
2423 .compile_policy
= xfrm_compile_policy
,
2424 .notify_policy
= xfrm_send_policy_notify
,
2425 .report
= xfrm_send_report
,
2426 .migrate
= xfrm_send_migrate
,
2429 static int __init
xfrm_user_init(void)
2433 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2435 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2436 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2439 rcu_assign_pointer(xfrm_nl
, nlsk
);
2441 xfrm_register_km(&netlink_mgr
);
2446 static void __exit
xfrm_user_exit(void)
2448 struct sock
*nlsk
= xfrm_nl
;
2450 xfrm_unregister_km(&netlink_mgr
);
2451 rcu_assign_pointer(xfrm_nl
, NULL
);
2453 sock_release(nlsk
->sk_socket
);
2456 module_init(xfrm_user_init
);
2457 module_exit(xfrm_user_exit
);
2458 MODULE_LICENSE("GPL");
2459 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);