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/pfkeyv2.h>
23 #include <linux/ipsec.h>
24 #include <linux/init.h>
25 #include <linux/security.h>
28 #include <net/netlink.h>
29 #include <asm/uaccess.h>
30 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31 #include <linux/in6.h>
33 #include <linux/audit.h>
35 static inline int alg_len(struct xfrm_algo
*alg
)
37 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
40 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
42 struct nlattr
*rt
= attrs
[type
];
43 struct xfrm_algo
*algp
;
49 if (nla_len(rt
) < alg_len(algp
))
54 if (!algp
->alg_key_len
&&
55 strcmp(algp
->alg_name
, "digest_null") != 0)
60 if (!algp
->alg_key_len
&&
61 strcmp(algp
->alg_name
, "cipher_null") != 0)
66 /* Zero length keys are legal. */
73 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
77 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
78 xfrm_address_t
**addrp
)
80 struct nlattr
*rt
= attrs
[type
];
83 *addrp
= nla_data(rt
);
86 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
88 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
89 struct xfrm_user_sec_ctx
*uctx
;
95 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
102 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
103 struct nlattr
**attrs
)
113 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
125 switch (p
->id
.proto
) {
127 if (!attrs
[XFRMA_ALG_AUTH
] ||
128 attrs
[XFRMA_ALG_CRYPT
] ||
129 attrs
[XFRMA_ALG_COMP
])
134 if ((!attrs
[XFRMA_ALG_AUTH
] &&
135 !attrs
[XFRMA_ALG_CRYPT
]) ||
136 attrs
[XFRMA_ALG_COMP
])
141 if (!attrs
[XFRMA_ALG_COMP
] ||
142 attrs
[XFRMA_ALG_AUTH
] ||
143 attrs
[XFRMA_ALG_CRYPT
])
147 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
148 case IPPROTO_DSTOPTS
:
149 case IPPROTO_ROUTING
:
150 if (attrs
[XFRMA_ALG_COMP
] ||
151 attrs
[XFRMA_ALG_AUTH
] ||
152 attrs
[XFRMA_ALG_CRYPT
] ||
153 attrs
[XFRMA_ENCAP
] ||
154 attrs
[XFRMA_SEC_CTX
] ||
155 !attrs
[XFRMA_COADDR
])
164 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
166 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
168 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
170 if ((err
= verify_sec_ctx_len(attrs
)))
175 case XFRM_MODE_TRANSPORT
:
176 case XFRM_MODE_TUNNEL
:
177 case XFRM_MODE_ROUTEOPTIMIZATION
:
191 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
192 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
195 struct xfrm_algo
*p
, *ualg
;
196 struct xfrm_algo_desc
*algo
;
201 ualg
= nla_data(rta
);
203 algo
= get_byname(ualg
->alg_name
, 1);
206 *props
= algo
->desc
.sadb_alg_id
;
208 p
= kmemdup(ualg
, alg_len(ualg
), GFP_KERNEL
);
212 strcpy(p
->alg_name
, algo
->name
);
217 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
222 len
+= sizeof(struct xfrm_user_sec_ctx
);
223 len
+= xfrm_ctx
->ctx_len
;
228 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
230 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
231 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
232 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
233 x
->props
.mode
= p
->mode
;
234 x
->props
.replay_window
= p
->replay_window
;
235 x
->props
.reqid
= p
->reqid
;
236 x
->props
.family
= p
->family
;
237 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
238 x
->props
.flags
= p
->flags
;
241 * Set inner address family if the KM left it as zero.
242 * See comment in validate_tmpl.
245 x
->sel
.family
= p
->family
;
249 * someday when pfkey also has support, we could have the code
250 * somehow made shareable and move it to xfrm_state.c - JHS
253 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
255 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
256 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
257 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
258 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
261 struct xfrm_replay_state
*replay
;
262 replay
= nla_data(rp
);
263 memcpy(&x
->replay
, replay
, sizeof(*replay
));
264 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
268 struct xfrm_lifetime_cur
*ltime
;
269 ltime
= nla_data(lt
);
270 x
->curlft
.bytes
= ltime
->bytes
;
271 x
->curlft
.packets
= ltime
->packets
;
272 x
->curlft
.add_time
= ltime
->add_time
;
273 x
->curlft
.use_time
= ltime
->use_time
;
277 x
->replay_maxage
= nla_get_u32(et
);
280 x
->replay_maxdiff
= nla_get_u32(rt
);
283 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
284 struct nlattr
**attrs
,
287 struct xfrm_state
*x
= xfrm_state_alloc();
293 copy_from_user_state(x
, p
);
295 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
296 xfrm_aalg_get_byname
,
297 attrs
[XFRMA_ALG_AUTH
])))
299 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
300 xfrm_ealg_get_byname
,
301 attrs
[XFRMA_ALG_CRYPT
])))
303 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
304 xfrm_calg_get_byname
,
305 attrs
[XFRMA_ALG_COMP
])))
308 if (attrs
[XFRMA_ENCAP
]) {
309 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
310 sizeof(*x
->encap
), GFP_KERNEL
);
311 if (x
->encap
== NULL
)
315 if (attrs
[XFRMA_COADDR
]) {
316 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
317 sizeof(*x
->coaddr
), GFP_KERNEL
);
318 if (x
->coaddr
== NULL
)
322 err
= xfrm_init_state(x
);
326 if (attrs
[XFRMA_SEC_CTX
] &&
327 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
331 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
332 /* sysctl_xfrm_aevent_etime is in 100ms units */
333 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
334 x
->preplay
.bitmap
= 0;
335 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
336 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
338 /* override default values from above */
340 xfrm_update_ae_params(x
, attrs
);
345 x
->km
.state
= XFRM_STATE_DEAD
;
352 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
353 struct nlattr
**attrs
)
355 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
356 struct xfrm_state
*x
;
360 err
= verify_newsa_info(p
, attrs
);
364 x
= xfrm_state_construct(p
, attrs
, &err
);
369 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
370 err
= xfrm_state_add(x
);
372 err
= xfrm_state_update(x
);
374 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
375 AUDIT_MAC_IPSEC_ADDSA
, err
? 0 : 1, NULL
, x
);
378 x
->km
.state
= XFRM_STATE_DEAD
;
383 c
.seq
= nlh
->nlmsg_seq
;
384 c
.pid
= nlh
->nlmsg_pid
;
385 c
.event
= nlh
->nlmsg_type
;
387 km_state_notify(x
, &c
);
393 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
394 struct nlattr
**attrs
,
397 struct xfrm_state
*x
= NULL
;
400 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
402 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
404 xfrm_address_t
*saddr
= NULL
;
406 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
413 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
423 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
424 struct nlattr
**attrs
)
426 struct xfrm_state
*x
;
429 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
431 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
435 if ((err
= security_xfrm_state_delete(x
)) != 0)
438 if (xfrm_state_kern(x
)) {
443 err
= xfrm_state_delete(x
);
448 c
.seq
= nlh
->nlmsg_seq
;
449 c
.pid
= nlh
->nlmsg_pid
;
450 c
.event
= nlh
->nlmsg_type
;
451 km_state_notify(x
, &c
);
454 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
455 AUDIT_MAC_IPSEC_DELSA
, err
? 0 : 1, NULL
, x
);
460 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
462 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
463 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
464 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
465 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
466 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
467 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
468 p
->mode
= x
->props
.mode
;
469 p
->replay_window
= x
->props
.replay_window
;
470 p
->reqid
= x
->props
.reqid
;
471 p
->family
= x
->props
.family
;
472 p
->flags
= x
->props
.flags
;
476 struct xfrm_dump_info
{
477 struct sk_buff
*in_skb
;
478 struct sk_buff
*out_skb
;
485 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
487 int ctx_size
= sizeof(struct xfrm_sec_ctx
) + s
->ctx_len
;
488 struct xfrm_user_sec_ctx
*uctx
;
491 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
495 uctx
= nla_data(attr
);
496 uctx
->exttype
= XFRMA_SEC_CTX
;
497 uctx
->len
= ctx_size
;
498 uctx
->ctx_doi
= s
->ctx_doi
;
499 uctx
->ctx_alg
= s
->ctx_alg
;
500 uctx
->ctx_len
= s
->ctx_len
;
501 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
506 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
508 struct xfrm_dump_info
*sp
= ptr
;
509 struct sk_buff
*in_skb
= sp
->in_skb
;
510 struct sk_buff
*skb
= sp
->out_skb
;
511 struct xfrm_usersa_info
*p
;
512 struct nlmsghdr
*nlh
;
514 if (sp
->this_idx
< sp
->start_idx
)
517 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
518 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
523 copy_to_user_state(x
, p
);
526 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
528 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
530 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
533 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
535 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
536 goto nla_put_failure
;
539 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
542 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
550 nlmsg_cancel(skb
, nlh
);
554 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
556 struct xfrm_dump_info info
;
558 info
.in_skb
= cb
->skb
;
560 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
561 info
.nlmsg_flags
= NLM_F_MULTI
;
563 info
.start_idx
= cb
->args
[0];
564 (void) xfrm_state_walk(0, dump_one_state
, &info
);
565 cb
->args
[0] = info
.this_idx
;
570 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
571 struct xfrm_state
*x
, u32 seq
)
573 struct xfrm_dump_info info
;
576 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
578 return ERR_PTR(-ENOMEM
);
580 info
.in_skb
= in_skb
;
582 info
.nlmsg_seq
= seq
;
583 info
.nlmsg_flags
= 0;
584 info
.this_idx
= info
.start_idx
= 0;
586 if (dump_one_state(x
, 0, &info
)) {
594 static inline size_t xfrm_spdinfo_msgsize(void)
596 return NLMSG_ALIGN(4)
597 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
598 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
601 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
603 struct xfrmk_spdinfo si
;
604 struct xfrmu_spdinfo spc
;
605 struct xfrmu_spdhinfo sph
;
606 struct nlmsghdr
*nlh
;
609 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
610 if (nlh
== NULL
) /* shouldnt really happen ... */
615 xfrm_spd_getinfo(&si
);
616 spc
.incnt
= si
.incnt
;
617 spc
.outcnt
= si
.outcnt
;
618 spc
.fwdcnt
= si
.fwdcnt
;
619 spc
.inscnt
= si
.inscnt
;
620 spc
.outscnt
= si
.outscnt
;
621 spc
.fwdscnt
= si
.fwdscnt
;
622 sph
.spdhcnt
= si
.spdhcnt
;
623 sph
.spdhmcnt
= si
.spdhmcnt
;
625 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
626 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
628 return nlmsg_end(skb
, nlh
);
631 nlmsg_cancel(skb
, nlh
);
635 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
636 struct nlattr
**attrs
)
638 struct sk_buff
*r_skb
;
639 u32
*flags
= nlmsg_data(nlh
);
640 u32 spid
= NETLINK_CB(skb
).pid
;
641 u32 seq
= nlh
->nlmsg_seq
;
643 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
647 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
650 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
653 static inline size_t xfrm_sadinfo_msgsize(void)
655 return NLMSG_ALIGN(4)
656 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
657 + nla_total_size(4); /* XFRMA_SAD_CNT */
660 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
662 struct xfrmk_sadinfo si
;
663 struct xfrmu_sadhinfo sh
;
664 struct nlmsghdr
*nlh
;
667 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
668 if (nlh
== NULL
) /* shouldnt really happen ... */
673 xfrm_sad_getinfo(&si
);
675 sh
.sadhmcnt
= si
.sadhmcnt
;
676 sh
.sadhcnt
= si
.sadhcnt
;
678 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
679 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
681 return nlmsg_end(skb
, nlh
);
684 nlmsg_cancel(skb
, nlh
);
688 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
689 struct nlattr
**attrs
)
691 struct sk_buff
*r_skb
;
692 u32
*flags
= nlmsg_data(nlh
);
693 u32 spid
= NETLINK_CB(skb
).pid
;
694 u32 seq
= nlh
->nlmsg_seq
;
696 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
700 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
703 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
706 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
707 struct nlattr
**attrs
)
709 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
710 struct xfrm_state
*x
;
711 struct sk_buff
*resp_skb
;
714 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
718 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
719 if (IS_ERR(resp_skb
)) {
720 err
= PTR_ERR(resp_skb
);
722 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
729 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
731 switch (p
->info
.id
.proto
) {
737 /* IPCOMP spi is 16-bits. */
738 if (p
->max
>= 0x10000)
752 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
753 struct nlattr
**attrs
)
755 struct xfrm_state
*x
;
756 struct xfrm_userspi_info
*p
;
757 struct sk_buff
*resp_skb
;
758 xfrm_address_t
*daddr
;
763 err
= verify_userspi_info(p
);
767 family
= p
->info
.family
;
768 daddr
= &p
->info
.id
.daddr
;
772 x
= xfrm_find_acq_byseq(p
->info
.seq
);
773 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
780 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
781 p
->info
.id
.proto
, daddr
,
788 resp_skb
= ERR_PTR(-ENOENT
);
790 spin_lock_bh(&x
->lock
);
791 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
792 xfrm_alloc_spi(x
, htonl(p
->min
), htonl(p
->max
));
794 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
796 spin_unlock_bh(&x
->lock
);
798 if (IS_ERR(resp_skb
)) {
799 err
= PTR_ERR(resp_skb
);
803 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
811 static int verify_policy_dir(u8 dir
)
815 case XFRM_POLICY_OUT
:
816 case XFRM_POLICY_FWD
:
826 static int verify_policy_type(u8 type
)
829 case XFRM_POLICY_TYPE_MAIN
:
830 #ifdef CONFIG_XFRM_SUB_POLICY
831 case XFRM_POLICY_TYPE_SUB
:
842 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
846 case XFRM_SHARE_SESSION
:
847 case XFRM_SHARE_USER
:
848 case XFRM_SHARE_UNIQUE
:
856 case XFRM_POLICY_ALLOW
:
857 case XFRM_POLICY_BLOCK
:
864 switch (p
->sel
.family
) {
869 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
872 return -EAFNOSUPPORT
;
879 return verify_policy_dir(p
->dir
);
882 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
884 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
885 struct xfrm_user_sec_ctx
*uctx
;
891 return security_xfrm_policy_alloc(pol
, uctx
);
894 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
900 for (i
= 0; i
< nr
; i
++, ut
++) {
901 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
903 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
904 memcpy(&t
->saddr
, &ut
->saddr
,
905 sizeof(xfrm_address_t
));
906 t
->reqid
= ut
->reqid
;
908 t
->share
= ut
->share
;
909 t
->optional
= ut
->optional
;
910 t
->aalgos
= ut
->aalgos
;
911 t
->ealgos
= ut
->ealgos
;
912 t
->calgos
= ut
->calgos
;
913 t
->encap_family
= ut
->family
;
917 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
921 if (nr
> XFRM_MAX_DEPTH
)
924 for (i
= 0; i
< nr
; i
++) {
925 /* We never validated the ut->family value, so many
926 * applications simply leave it at zero. The check was
927 * never made and ut->family was ignored because all
928 * templates could be assumed to have the same family as
929 * the policy itself. Now that we will have ipv4-in-ipv6
930 * and ipv6-in-ipv4 tunnels, this is no longer true.
933 ut
[i
].family
= family
;
935 switch (ut
[i
].family
) {
938 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
950 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
952 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
957 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
958 int nr
= nla_len(rt
) / sizeof(*utmpl
);
961 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
965 copy_templates(pol
, utmpl
, nr
);
970 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
972 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
973 struct xfrm_userpolicy_type
*upt
;
974 u8 type
= XFRM_POLICY_TYPE_MAIN
;
982 err
= verify_policy_type(type
);
990 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
992 xp
->priority
= p
->priority
;
993 xp
->index
= p
->index
;
994 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
995 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
996 xp
->action
= p
->action
;
997 xp
->flags
= p
->flags
;
998 xp
->family
= p
->sel
.family
;
999 /* XXX xp->share = p->share; */
1002 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1004 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1005 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1006 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1007 p
->priority
= xp
->priority
;
1008 p
->index
= xp
->index
;
1009 p
->sel
.family
= xp
->family
;
1011 p
->action
= xp
->action
;
1012 p
->flags
= xp
->flags
;
1013 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1016 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1018 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1026 copy_from_user_policy(xp
, p
);
1028 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1032 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1033 err
= copy_from_user_sec_ctx(xp
, attrs
);
1044 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1045 struct nlattr
**attrs
)
1047 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1048 struct xfrm_policy
*xp
;
1053 err
= verify_newpolicy_info(p
);
1056 err
= verify_sec_ctx_len(attrs
);
1060 xp
= xfrm_policy_construct(p
, attrs
, &err
);
1064 /* shouldnt excl be based on nlh flags??
1065 * Aha! this is anti-netlink really i.e more pfkey derived
1066 * in netlink excl is a flag and you wouldnt need
1067 * a type XFRM_MSG_UPDPOLICY - JHS */
1068 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1069 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1070 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1071 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1074 security_xfrm_policy_free(xp
);
1079 c
.event
= nlh
->nlmsg_type
;
1080 c
.seq
= nlh
->nlmsg_seq
;
1081 c
.pid
= nlh
->nlmsg_pid
;
1082 km_policy_notify(xp
, p
->dir
, &c
);
1089 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1091 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1094 if (xp
->xfrm_nr
== 0)
1097 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1098 struct xfrm_user_tmpl
*up
= &vec
[i
];
1099 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1101 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1102 up
->family
= kp
->encap_family
;
1103 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1104 up
->reqid
= kp
->reqid
;
1105 up
->mode
= kp
->mode
;
1106 up
->share
= kp
->share
;
1107 up
->optional
= kp
->optional
;
1108 up
->aalgos
= kp
->aalgos
;
1109 up
->ealgos
= kp
->ealgos
;
1110 up
->calgos
= kp
->calgos
;
1113 return nla_put(skb
, XFRMA_TMPL
,
1114 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1117 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1120 return copy_sec_ctx(x
->security
, skb
);
1125 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1128 return copy_sec_ctx(xp
->security
, skb
);
1132 static inline size_t userpolicy_type_attrsize(void)
1134 #ifdef CONFIG_XFRM_SUB_POLICY
1135 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1141 #ifdef CONFIG_XFRM_SUB_POLICY
1142 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1144 struct xfrm_userpolicy_type upt
= {
1148 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1152 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1158 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1160 struct xfrm_dump_info
*sp
= ptr
;
1161 struct xfrm_userpolicy_info
*p
;
1162 struct sk_buff
*in_skb
= sp
->in_skb
;
1163 struct sk_buff
*skb
= sp
->out_skb
;
1164 struct nlmsghdr
*nlh
;
1166 if (sp
->this_idx
< sp
->start_idx
)
1169 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1170 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1174 p
= nlmsg_data(nlh
);
1175 copy_to_user_policy(xp
, p
, dir
);
1176 if (copy_to_user_tmpl(xp
, skb
) < 0)
1178 if (copy_to_user_sec_ctx(xp
, skb
))
1180 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1183 nlmsg_end(skb
, nlh
);
1189 nlmsg_cancel(skb
, nlh
);
1193 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1195 struct xfrm_dump_info info
;
1197 info
.in_skb
= cb
->skb
;
1199 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1200 info
.nlmsg_flags
= NLM_F_MULTI
;
1202 info
.start_idx
= cb
->args
[0];
1203 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1204 #ifdef CONFIG_XFRM_SUB_POLICY
1205 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1207 cb
->args
[0] = info
.this_idx
;
1212 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1213 struct xfrm_policy
*xp
,
1216 struct xfrm_dump_info info
;
1217 struct sk_buff
*skb
;
1219 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1221 return ERR_PTR(-ENOMEM
);
1223 info
.in_skb
= in_skb
;
1225 info
.nlmsg_seq
= seq
;
1226 info
.nlmsg_flags
= 0;
1227 info
.this_idx
= info
.start_idx
= 0;
1229 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1237 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1238 struct nlattr
**attrs
)
1240 struct xfrm_policy
*xp
;
1241 struct xfrm_userpolicy_id
*p
;
1242 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1247 p
= nlmsg_data(nlh
);
1248 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1250 err
= copy_from_user_policy_type(&type
, attrs
);
1254 err
= verify_policy_dir(p
->dir
);
1259 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1261 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1262 struct xfrm_policy tmp
;
1264 err
= verify_sec_ctx_len(attrs
);
1268 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1270 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1272 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1275 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1277 security_xfrm_policy_free(&tmp
);
1283 struct sk_buff
*resp_skb
;
1285 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1286 if (IS_ERR(resp_skb
)) {
1287 err
= PTR_ERR(resp_skb
);
1289 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1290 NETLINK_CB(skb
).pid
);
1293 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1294 AUDIT_MAC_IPSEC_DELSPD
, err
? 0 : 1, xp
, NULL
);
1299 c
.data
.byid
= p
->index
;
1300 c
.event
= nlh
->nlmsg_type
;
1301 c
.seq
= nlh
->nlmsg_seq
;
1302 c
.pid
= nlh
->nlmsg_pid
;
1303 km_policy_notify(xp
, p
->dir
, &c
);
1311 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1312 struct nlattr
**attrs
)
1315 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1316 struct xfrm_audit audit_info
;
1319 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1320 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1321 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1324 c
.data
.proto
= p
->proto
;
1325 c
.event
= nlh
->nlmsg_type
;
1326 c
.seq
= nlh
->nlmsg_seq
;
1327 c
.pid
= nlh
->nlmsg_pid
;
1328 km_state_notify(NULL
, &c
);
1333 static inline size_t xfrm_aevent_msgsize(void)
1335 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1336 + nla_total_size(sizeof(struct xfrm_replay_state
))
1337 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1338 + nla_total_size(4) /* XFRM_AE_RTHR */
1339 + nla_total_size(4); /* XFRM_AE_ETHR */
1342 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1344 struct xfrm_aevent_id
*id
;
1345 struct nlmsghdr
*nlh
;
1347 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1351 id
= nlmsg_data(nlh
);
1352 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1353 id
->sa_id
.spi
= x
->id
.spi
;
1354 id
->sa_id
.family
= x
->props
.family
;
1355 id
->sa_id
.proto
= x
->id
.proto
;
1356 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1357 id
->reqid
= x
->props
.reqid
;
1358 id
->flags
= c
->data
.aevent
;
1360 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1361 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1363 if (id
->flags
& XFRM_AE_RTHR
)
1364 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1366 if (id
->flags
& XFRM_AE_ETHR
)
1367 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1368 x
->replay_maxage
* 10 / HZ
);
1370 return nlmsg_end(skb
, nlh
);
1373 nlmsg_cancel(skb
, nlh
);
1377 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1378 struct nlattr
**attrs
)
1380 struct xfrm_state
*x
;
1381 struct sk_buff
*r_skb
;
1384 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1385 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1387 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1391 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1398 * XXX: is this lock really needed - none of the other
1399 * gets lock (the concern is things getting updated
1400 * while we are still reading) - jhs
1402 spin_lock_bh(&x
->lock
);
1403 c
.data
.aevent
= p
->flags
;
1404 c
.seq
= nlh
->nlmsg_seq
;
1405 c
.pid
= nlh
->nlmsg_pid
;
1407 if (build_aevent(r_skb
, x
, &c
) < 0)
1409 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1410 spin_unlock_bh(&x
->lock
);
1415 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1416 struct nlattr
**attrs
)
1418 struct xfrm_state
*x
;
1421 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1422 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1423 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1428 /* pedantic mode - thou shalt sayeth replaceth */
1429 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1432 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1436 if (x
->km
.state
!= XFRM_STATE_VALID
)
1439 spin_lock_bh(&x
->lock
);
1440 xfrm_update_ae_params(x
, attrs
);
1441 spin_unlock_bh(&x
->lock
);
1443 c
.event
= nlh
->nlmsg_type
;
1444 c
.seq
= nlh
->nlmsg_seq
;
1445 c
.pid
= nlh
->nlmsg_pid
;
1446 c
.data
.aevent
= XFRM_AE_CU
;
1447 km_state_notify(x
, &c
);
1454 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1455 struct nlattr
**attrs
)
1458 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1460 struct xfrm_audit audit_info
;
1462 err
= copy_from_user_policy_type(&type
, attrs
);
1466 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1467 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1468 err
= xfrm_policy_flush(type
, &audit_info
);
1472 c
.event
= nlh
->nlmsg_type
;
1473 c
.seq
= nlh
->nlmsg_seq
;
1474 c
.pid
= nlh
->nlmsg_pid
;
1475 km_policy_notify(NULL
, 0, &c
);
1479 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1480 struct nlattr
**attrs
)
1482 struct xfrm_policy
*xp
;
1483 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1484 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1485 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1488 err
= copy_from_user_policy_type(&type
, attrs
);
1493 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1495 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1496 struct xfrm_policy tmp
;
1498 err
= verify_sec_ctx_len(attrs
);
1502 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1504 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1506 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1509 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1511 security_xfrm_policy_free(&tmp
);
1516 read_lock(&xp
->lock
);
1518 read_unlock(&xp
->lock
);
1522 read_unlock(&xp
->lock
);
1525 xfrm_policy_delete(xp
, p
->dir
);
1526 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1527 AUDIT_MAC_IPSEC_DELSPD
, 1, xp
, NULL
);
1530 // reset the timers here?
1531 printk("Dont know what to do with soft policy expire\n");
1533 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1540 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1541 struct nlattr
**attrs
)
1543 struct xfrm_state
*x
;
1545 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1546 struct xfrm_usersa_info
*p
= &ue
->state
;
1548 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1554 spin_lock_bh(&x
->lock
);
1556 if (x
->km
.state
!= XFRM_STATE_VALID
)
1558 km_state_expired(x
, ue
->hard
, current
->pid
);
1561 __xfrm_state_delete(x
);
1562 xfrm_audit_log(NETLINK_CB(skb
).loginuid
, NETLINK_CB(skb
).sid
,
1563 AUDIT_MAC_IPSEC_DELSA
, 1, NULL
, x
);
1567 spin_unlock_bh(&x
->lock
);
1572 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1573 struct nlattr
**attrs
)
1575 struct xfrm_policy
*xp
;
1576 struct xfrm_user_tmpl
*ut
;
1578 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1580 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1581 struct xfrm_state
*x
= xfrm_state_alloc();
1587 err
= verify_newpolicy_info(&ua
->policy
);
1589 printk("BAD policy passed\n");
1595 xp
= xfrm_policy_construct(&ua
->policy
, attrs
, &err
);
1601 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1602 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1603 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1606 /* extract the templates and for each call km_key */
1607 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1608 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1609 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1610 x
->props
.mode
= t
->mode
;
1611 x
->props
.reqid
= t
->reqid
;
1612 x
->props
.family
= ut
->family
;
1613 t
->aalgos
= ua
->aalgos
;
1614 t
->ealgos
= ua
->ealgos
;
1615 t
->calgos
= ua
->calgos
;
1616 err
= km_query(x
, t
, xp
);
1626 #ifdef CONFIG_XFRM_MIGRATE
1627 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1628 struct nlattr
**attrs
, int *num
)
1630 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1631 struct xfrm_user_migrate
*um
;
1635 num_migrate
= nla_len(rt
) / sizeof(*um
);
1637 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1640 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1641 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1642 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1643 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1644 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1646 ma
->proto
= um
->proto
;
1647 ma
->mode
= um
->mode
;
1648 ma
->reqid
= um
->reqid
;
1650 ma
->old_family
= um
->old_family
;
1651 ma
->new_family
= um
->new_family
;
1658 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1659 struct nlattr
**attrs
)
1661 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1662 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1667 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1670 err
= copy_from_user_policy_type(&type
, attrs
);
1674 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1682 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1687 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1688 struct nlattr
**attrs
)
1690 return -ENOPROTOOPT
;
1694 #ifdef CONFIG_XFRM_MIGRATE
1695 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1697 struct xfrm_user_migrate um
;
1699 memset(&um
, 0, sizeof(um
));
1700 um
.proto
= m
->proto
;
1702 um
.reqid
= m
->reqid
;
1703 um
.old_family
= m
->old_family
;
1704 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1705 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1706 um
.new_family
= m
->new_family
;
1707 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1708 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1710 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1713 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1715 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1716 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1717 + userpolicy_type_attrsize();
1720 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1721 int num_migrate
, struct xfrm_selector
*sel
,
1724 struct xfrm_migrate
*mp
;
1725 struct xfrm_userpolicy_id
*pol_id
;
1726 struct nlmsghdr
*nlh
;
1729 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1733 pol_id
= nlmsg_data(nlh
);
1734 /* copy data from selector, dir, and type to the pol_id */
1735 memset(pol_id
, 0, sizeof(*pol_id
));
1736 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1739 if (copy_to_user_policy_type(type
, skb
) < 0)
1742 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1743 if (copy_to_user_migrate(mp
, skb
) < 0)
1747 return nlmsg_end(skb
, nlh
);
1749 nlmsg_cancel(skb
, nlh
);
1753 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1754 struct xfrm_migrate
*m
, int num_migrate
)
1756 struct sk_buff
*skb
;
1758 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1763 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1766 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1769 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1770 struct xfrm_migrate
*m
, int num_migrate
)
1772 return -ENOPROTOOPT
;
1776 #define XMSGSIZE(type) sizeof(struct type)
1778 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1779 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1780 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1781 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1782 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1783 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1784 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1785 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1786 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1787 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1788 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1789 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1790 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1791 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1792 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1793 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1794 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1795 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1796 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1797 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1798 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1803 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1804 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1805 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1806 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1807 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1808 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1809 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1810 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1811 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1812 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1813 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1814 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1815 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1816 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1817 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1820 static struct xfrm_link
{
1821 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
1822 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1823 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1824 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1825 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1826 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1827 .dump
= xfrm_dump_sa
},
1828 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1829 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1830 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1831 .dump
= xfrm_dump_policy
},
1832 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1833 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1834 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1835 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1836 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1837 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1838 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1839 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1840 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1841 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1842 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1843 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1844 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1847 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1849 struct nlattr
*attrs
[XFRMA_MAX
+1];
1850 struct xfrm_link
*link
;
1853 type
= nlh
->nlmsg_type
;
1854 if (type
> XFRM_MSG_MAX
)
1857 type
-= XFRM_MSG_BASE
;
1858 link
= &xfrm_dispatch
[type
];
1860 /* All operations require privileges, even GET */
1861 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1864 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1865 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1866 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1867 if (link
->dump
== NULL
)
1870 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, NULL
);
1873 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
1878 if (link
->doit
== NULL
)
1881 return link
->doit(skb
, nlh
, attrs
);
1884 static void xfrm_netlink_rcv(struct sock
*sk
, int len
)
1886 unsigned int qlen
= 0;
1889 mutex_lock(&xfrm_cfg_mutex
);
1890 netlink_run_queue(sk
, &qlen
, &xfrm_user_rcv_msg
);
1891 mutex_unlock(&xfrm_cfg_mutex
);
1896 static inline size_t xfrm_expire_msgsize(void)
1898 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1901 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1903 struct xfrm_user_expire
*ue
;
1904 struct nlmsghdr
*nlh
;
1906 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1910 ue
= nlmsg_data(nlh
);
1911 copy_to_user_state(x
, &ue
->state
);
1912 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1914 return nlmsg_end(skb
, nlh
);
1917 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1919 struct sk_buff
*skb
;
1921 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
1925 if (build_expire(skb
, x
, c
) < 0)
1928 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1931 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1933 struct sk_buff
*skb
;
1935 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1939 if (build_aevent(skb
, x
, c
) < 0)
1942 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1945 static int xfrm_notify_sa_flush(struct km_event
*c
)
1947 struct xfrm_usersa_flush
*p
;
1948 struct nlmsghdr
*nlh
;
1949 struct sk_buff
*skb
;
1950 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
1952 skb
= nlmsg_new(len
, GFP_ATOMIC
);
1956 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
1962 p
= nlmsg_data(nlh
);
1963 p
->proto
= c
->data
.proto
;
1965 nlmsg_end(skb
, nlh
);
1967 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1970 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
1974 l
+= nla_total_size(alg_len(x
->aalg
));
1976 l
+= nla_total_size(alg_len(x
->ealg
));
1978 l
+= nla_total_size(sizeof(*x
->calg
));
1980 l
+= nla_total_size(sizeof(*x
->encap
));
1985 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1987 struct xfrm_usersa_info
*p
;
1988 struct xfrm_usersa_id
*id
;
1989 struct nlmsghdr
*nlh
;
1990 struct sk_buff
*skb
;
1991 int len
= xfrm_sa_len(x
);
1994 headlen
= sizeof(*p
);
1995 if (c
->event
== XFRM_MSG_DELSA
) {
1996 len
+= nla_total_size(headlen
);
1997 headlen
= sizeof(*id
);
1999 len
+= NLMSG_ALIGN(headlen
);
2001 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2005 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2007 goto nla_put_failure
;
2009 p
= nlmsg_data(nlh
);
2010 if (c
->event
== XFRM_MSG_DELSA
) {
2011 struct nlattr
*attr
;
2013 id
= nlmsg_data(nlh
);
2014 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2015 id
->spi
= x
->id
.spi
;
2016 id
->family
= x
->props
.family
;
2017 id
->proto
= x
->id
.proto
;
2019 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2021 goto nla_put_failure
;
2026 copy_to_user_state(x
, p
);
2029 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
2031 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
2033 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
2036 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
2038 nlmsg_end(skb
, nlh
);
2040 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2047 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2051 case XFRM_MSG_EXPIRE
:
2052 return xfrm_exp_state_notify(x
, c
);
2053 case XFRM_MSG_NEWAE
:
2054 return xfrm_aevent_state_notify(x
, c
);
2055 case XFRM_MSG_DELSA
:
2056 case XFRM_MSG_UPDSA
:
2057 case XFRM_MSG_NEWSA
:
2058 return xfrm_notify_sa(x
, c
);
2059 case XFRM_MSG_FLUSHSA
:
2060 return xfrm_notify_sa_flush(c
);
2062 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2070 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2071 struct xfrm_policy
*xp
)
2073 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2074 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2075 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2076 + userpolicy_type_attrsize();
2079 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2080 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2083 struct xfrm_user_acquire
*ua
;
2084 struct nlmsghdr
*nlh
;
2085 __u32 seq
= xfrm_get_acqseq();
2087 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2091 ua
= nlmsg_data(nlh
);
2092 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2093 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2094 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2095 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2096 ua
->aalgos
= xt
->aalgos
;
2097 ua
->ealgos
= xt
->ealgos
;
2098 ua
->calgos
= xt
->calgos
;
2099 ua
->seq
= x
->km
.seq
= seq
;
2101 if (copy_to_user_tmpl(xp
, skb
) < 0)
2103 if (copy_to_user_state_sec_ctx(x
, skb
))
2105 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2108 return nlmsg_end(skb
, nlh
);
2111 nlmsg_cancel(skb
, nlh
);
2115 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2116 struct xfrm_policy
*xp
, int dir
)
2118 struct sk_buff
*skb
;
2120 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2124 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2127 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2130 /* User gives us xfrm_user_policy_info followed by an array of 0
2131 * or more templates.
2133 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2134 u8
*data
, int len
, int *dir
)
2136 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2137 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2138 struct xfrm_policy
*xp
;
2141 switch (sk
->sk_family
) {
2143 if (opt
!= IP_XFRM_POLICY
) {
2148 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2150 if (opt
!= IPV6_XFRM_POLICY
) {
2163 if (len
< sizeof(*p
) ||
2164 verify_newpolicy_info(p
))
2167 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2168 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2171 if (p
->dir
> XFRM_POLICY_OUT
)
2174 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2180 copy_from_user_policy(xp
, p
);
2181 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2182 copy_templates(xp
, ut
, nr
);
2189 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2191 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2192 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2193 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2194 + userpolicy_type_attrsize();
2197 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2198 int dir
, struct km_event
*c
)
2200 struct xfrm_user_polexpire
*upe
;
2201 struct nlmsghdr
*nlh
;
2202 int hard
= c
->data
.hard
;
2204 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2208 upe
= nlmsg_data(nlh
);
2209 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2210 if (copy_to_user_tmpl(xp
, skb
) < 0)
2212 if (copy_to_user_sec_ctx(xp
, skb
))
2214 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2218 return nlmsg_end(skb
, nlh
);
2221 nlmsg_cancel(skb
, nlh
);
2225 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2227 struct sk_buff
*skb
;
2229 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2233 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2236 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2239 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2241 struct xfrm_userpolicy_info
*p
;
2242 struct xfrm_userpolicy_id
*id
;
2243 struct nlmsghdr
*nlh
;
2244 struct sk_buff
*skb
;
2245 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2248 headlen
= sizeof(*p
);
2249 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2250 len
+= nla_total_size(headlen
);
2251 headlen
= sizeof(*id
);
2253 len
+= userpolicy_type_attrsize();
2254 len
+= NLMSG_ALIGN(headlen
);
2256 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2260 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2264 p
= nlmsg_data(nlh
);
2265 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2266 struct nlattr
*attr
;
2268 id
= nlmsg_data(nlh
);
2269 memset(id
, 0, sizeof(*id
));
2272 id
->index
= xp
->index
;
2274 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2276 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2283 copy_to_user_policy(xp
, p
, dir
);
2284 if (copy_to_user_tmpl(xp
, skb
) < 0)
2286 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2289 nlmsg_end(skb
, nlh
);
2291 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2298 static int xfrm_notify_policy_flush(struct km_event
*c
)
2300 struct nlmsghdr
*nlh
;
2301 struct sk_buff
*skb
;
2303 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2307 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2310 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2313 nlmsg_end(skb
, nlh
);
2315 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2322 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2326 case XFRM_MSG_NEWPOLICY
:
2327 case XFRM_MSG_UPDPOLICY
:
2328 case XFRM_MSG_DELPOLICY
:
2329 return xfrm_notify_policy(xp
, dir
, c
);
2330 case XFRM_MSG_FLUSHPOLICY
:
2331 return xfrm_notify_policy_flush(c
);
2332 case XFRM_MSG_POLEXPIRE
:
2333 return xfrm_exp_policy_notify(xp
, dir
, c
);
2335 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2342 static inline size_t xfrm_report_msgsize(void)
2344 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2347 static int build_report(struct sk_buff
*skb
, u8 proto
,
2348 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2350 struct xfrm_user_report
*ur
;
2351 struct nlmsghdr
*nlh
;
2353 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2357 ur
= nlmsg_data(nlh
);
2359 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2362 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2364 return nlmsg_end(skb
, nlh
);
2367 nlmsg_cancel(skb
, nlh
);
2371 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2372 xfrm_address_t
*addr
)
2374 struct sk_buff
*skb
;
2376 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2380 if (build_report(skb
, proto
, sel
, addr
) < 0)
2383 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2386 static struct xfrm_mgr netlink_mgr
= {
2388 .notify
= xfrm_send_state_notify
,
2389 .acquire
= xfrm_send_acquire
,
2390 .compile_policy
= xfrm_compile_policy
,
2391 .notify_policy
= xfrm_send_policy_notify
,
2392 .report
= xfrm_send_report
,
2393 .migrate
= xfrm_send_migrate
,
2396 static int __init
xfrm_user_init(void)
2400 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2402 nlsk
= netlink_kernel_create(NETLINK_XFRM
, XFRMNLGRP_MAX
,
2403 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2406 rcu_assign_pointer(xfrm_nl
, nlsk
);
2408 xfrm_register_km(&netlink_mgr
);
2413 static void __exit
xfrm_user_exit(void)
2415 struct sock
*nlsk
= xfrm_nl
;
2417 xfrm_unregister_km(&netlink_mgr
);
2418 rcu_assign_pointer(xfrm_nl
, NULL
);
2420 sock_release(nlsk
->sk_socket
);
2423 module_init(xfrm_user_init
);
2424 module_exit(xfrm_user_exit
);
2425 MODULE_LICENSE("GPL");
2426 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);