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>
34 static inline int alg_len(struct xfrm_algo
*alg
)
36 return sizeof(*alg
) + ((alg
->alg_key_len
+ 7) / 8);
39 static int verify_one_alg(struct nlattr
**attrs
, enum xfrm_attr_type_t type
)
41 struct nlattr
*rt
= attrs
[type
];
42 struct xfrm_algo
*algp
;
48 if (nla_len(rt
) < alg_len(algp
))
53 if (!algp
->alg_key_len
&&
54 strcmp(algp
->alg_name
, "digest_null") != 0)
59 if (!algp
->alg_key_len
&&
60 strcmp(algp
->alg_name
, "cipher_null") != 0)
65 /* Zero length keys are legal. */
72 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
76 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
77 xfrm_address_t
**addrp
)
79 struct nlattr
*rt
= attrs
[type
];
82 *addrp
= nla_data(rt
);
85 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
87 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
88 struct xfrm_user_sec_ctx
*uctx
;
94 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
101 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
102 struct nlattr
**attrs
)
112 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
124 switch (p
->id
.proto
) {
126 if (!attrs
[XFRMA_ALG_AUTH
] ||
127 attrs
[XFRMA_ALG_CRYPT
] ||
128 attrs
[XFRMA_ALG_COMP
])
133 if ((!attrs
[XFRMA_ALG_AUTH
] &&
134 !attrs
[XFRMA_ALG_CRYPT
]) ||
135 attrs
[XFRMA_ALG_COMP
])
140 if (!attrs
[XFRMA_ALG_COMP
] ||
141 attrs
[XFRMA_ALG_AUTH
] ||
142 attrs
[XFRMA_ALG_CRYPT
])
146 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
147 case IPPROTO_DSTOPTS
:
148 case IPPROTO_ROUTING
:
149 if (attrs
[XFRMA_ALG_COMP
] ||
150 attrs
[XFRMA_ALG_AUTH
] ||
151 attrs
[XFRMA_ALG_CRYPT
] ||
152 attrs
[XFRMA_ENCAP
] ||
153 attrs
[XFRMA_SEC_CTX
] ||
154 !attrs
[XFRMA_COADDR
])
163 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
165 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
167 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
169 if ((err
= verify_sec_ctx_len(attrs
)))
174 case XFRM_MODE_TRANSPORT
:
175 case XFRM_MODE_TUNNEL
:
176 case XFRM_MODE_ROUTEOPTIMIZATION
:
190 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
191 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
194 struct xfrm_algo
*p
, *ualg
;
195 struct xfrm_algo_desc
*algo
;
200 ualg
= nla_data(rta
);
202 algo
= get_byname(ualg
->alg_name
, 1);
205 *props
= algo
->desc
.sadb_alg_id
;
207 p
= kmemdup(ualg
, alg_len(ualg
), GFP_KERNEL
);
211 strcpy(p
->alg_name
, algo
->name
);
216 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
221 len
+= sizeof(struct xfrm_user_sec_ctx
);
222 len
+= xfrm_ctx
->ctx_len
;
227 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
229 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
230 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
231 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
232 x
->props
.mode
= p
->mode
;
233 x
->props
.replay_window
= p
->replay_window
;
234 x
->props
.reqid
= p
->reqid
;
235 x
->props
.family
= p
->family
;
236 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
237 x
->props
.flags
= p
->flags
;
240 * Set inner address family if the KM left it as zero.
241 * See comment in validate_tmpl.
244 x
->sel
.family
= p
->family
;
248 * someday when pfkey also has support, we could have the code
249 * somehow made shareable and move it to xfrm_state.c - JHS
252 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
254 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
255 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
256 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
257 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
260 struct xfrm_replay_state
*replay
;
261 replay
= nla_data(rp
);
262 memcpy(&x
->replay
, replay
, sizeof(*replay
));
263 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
267 struct xfrm_lifetime_cur
*ltime
;
268 ltime
= nla_data(lt
);
269 x
->curlft
.bytes
= ltime
->bytes
;
270 x
->curlft
.packets
= ltime
->packets
;
271 x
->curlft
.add_time
= ltime
->add_time
;
272 x
->curlft
.use_time
= ltime
->use_time
;
276 x
->replay_maxage
= nla_get_u32(et
);
279 x
->replay_maxdiff
= nla_get_u32(rt
);
282 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
283 struct nlattr
**attrs
,
286 struct xfrm_state
*x
= xfrm_state_alloc();
292 copy_from_user_state(x
, p
);
294 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
295 xfrm_aalg_get_byname
,
296 attrs
[XFRMA_ALG_AUTH
])))
298 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
299 xfrm_ealg_get_byname
,
300 attrs
[XFRMA_ALG_CRYPT
])))
302 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
303 xfrm_calg_get_byname
,
304 attrs
[XFRMA_ALG_COMP
])))
307 if (attrs
[XFRMA_ENCAP
]) {
308 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
309 sizeof(*x
->encap
), GFP_KERNEL
);
310 if (x
->encap
== NULL
)
314 if (attrs
[XFRMA_COADDR
]) {
315 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
316 sizeof(*x
->coaddr
), GFP_KERNEL
);
317 if (x
->coaddr
== NULL
)
321 err
= xfrm_init_state(x
);
325 if (attrs
[XFRMA_SEC_CTX
] &&
326 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
330 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
331 /* sysctl_xfrm_aevent_etime is in 100ms units */
332 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
333 x
->preplay
.bitmap
= 0;
334 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
335 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
337 /* override default values from above */
339 xfrm_update_ae_params(x
, attrs
);
344 x
->km
.state
= XFRM_STATE_DEAD
;
351 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
352 struct nlattr
**attrs
)
354 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
355 struct xfrm_state
*x
;
359 err
= verify_newsa_info(p
, attrs
);
363 x
= xfrm_state_construct(p
, attrs
, &err
);
368 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
369 err
= xfrm_state_add(x
);
371 err
= xfrm_state_update(x
);
373 xfrm_audit_state_add(x
, err
? 0 : 1, NETLINK_CB(skb
).loginuid
,
374 NETLINK_CB(skb
).sid
);
377 x
->km
.state
= XFRM_STATE_DEAD
;
382 c
.seq
= nlh
->nlmsg_seq
;
383 c
.pid
= nlh
->nlmsg_pid
;
384 c
.event
= nlh
->nlmsg_type
;
386 km_state_notify(x
, &c
);
392 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
393 struct nlattr
**attrs
,
396 struct xfrm_state
*x
= NULL
;
399 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
401 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
403 xfrm_address_t
*saddr
= NULL
;
405 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
412 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
422 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
423 struct nlattr
**attrs
)
425 struct xfrm_state
*x
;
428 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
430 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
434 if ((err
= security_xfrm_state_delete(x
)) != 0)
437 if (xfrm_state_kern(x
)) {
442 err
= xfrm_state_delete(x
);
447 c
.seq
= nlh
->nlmsg_seq
;
448 c
.pid
= nlh
->nlmsg_pid
;
449 c
.event
= nlh
->nlmsg_type
;
450 km_state_notify(x
, &c
);
453 xfrm_audit_state_delete(x
, err
? 0 : 1, NETLINK_CB(skb
).loginuid
,
454 NETLINK_CB(skb
).sid
);
459 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
461 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
462 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
463 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
464 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
465 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
466 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
467 p
->mode
= x
->props
.mode
;
468 p
->replay_window
= x
->props
.replay_window
;
469 p
->reqid
= x
->props
.reqid
;
470 p
->family
= x
->props
.family
;
471 p
->flags
= x
->props
.flags
;
475 struct xfrm_dump_info
{
476 struct sk_buff
*in_skb
;
477 struct sk_buff
*out_skb
;
484 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
486 struct xfrm_user_sec_ctx
*uctx
;
488 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
490 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
494 uctx
= nla_data(attr
);
495 uctx
->exttype
= XFRMA_SEC_CTX
;
496 uctx
->len
= ctx_size
;
497 uctx
->ctx_doi
= s
->ctx_doi
;
498 uctx
->ctx_alg
= s
->ctx_alg
;
499 uctx
->ctx_len
= s
->ctx_len
;
500 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
505 /* Don't change this without updating xfrm_sa_len! */
506 static int copy_to_user_state_extra(struct xfrm_state
*x
,
507 struct xfrm_usersa_info
*p
,
510 spin_lock_bh(&x
->lock
);
511 copy_to_user_state(x
, p
);
514 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
517 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
518 spin_unlock_bh(&x
->lock
);
521 NLA_PUT(skb
, XFRMA_ALG_AUTH
, alg_len(x
->aalg
), x
->aalg
);
523 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, alg_len(x
->ealg
), x
->ealg
);
525 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
528 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
530 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
531 goto nla_put_failure
;
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
;
548 if (sp
->this_idx
< sp
->start_idx
)
551 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
552 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
558 err
= copy_to_user_state_extra(x
, p
, skb
);
560 goto nla_put_failure
;
568 nlmsg_cancel(skb
, nlh
);
572 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
574 struct xfrm_dump_info info
;
576 info
.in_skb
= cb
->skb
;
578 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
579 info
.nlmsg_flags
= NLM_F_MULTI
;
581 info
.start_idx
= cb
->args
[0];
582 (void) xfrm_state_walk(0, dump_one_state
, &info
);
583 cb
->args
[0] = info
.this_idx
;
588 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
589 struct xfrm_state
*x
, u32 seq
)
591 struct xfrm_dump_info info
;
594 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
596 return ERR_PTR(-ENOMEM
);
598 info
.in_skb
= in_skb
;
600 info
.nlmsg_seq
= seq
;
601 info
.nlmsg_flags
= 0;
602 info
.this_idx
= info
.start_idx
= 0;
604 if (dump_one_state(x
, 0, &info
)) {
612 static inline size_t xfrm_spdinfo_msgsize(void)
614 return NLMSG_ALIGN(4)
615 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
616 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
619 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
621 struct xfrmk_spdinfo si
;
622 struct xfrmu_spdinfo spc
;
623 struct xfrmu_spdhinfo sph
;
624 struct nlmsghdr
*nlh
;
627 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
628 if (nlh
== NULL
) /* shouldnt really happen ... */
633 xfrm_spd_getinfo(&si
);
634 spc
.incnt
= si
.incnt
;
635 spc
.outcnt
= si
.outcnt
;
636 spc
.fwdcnt
= si
.fwdcnt
;
637 spc
.inscnt
= si
.inscnt
;
638 spc
.outscnt
= si
.outscnt
;
639 spc
.fwdscnt
= si
.fwdscnt
;
640 sph
.spdhcnt
= si
.spdhcnt
;
641 sph
.spdhmcnt
= si
.spdhmcnt
;
643 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
644 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
646 return nlmsg_end(skb
, nlh
);
649 nlmsg_cancel(skb
, nlh
);
653 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
654 struct nlattr
**attrs
)
656 struct sk_buff
*r_skb
;
657 u32
*flags
= nlmsg_data(nlh
);
658 u32 spid
= NETLINK_CB(skb
).pid
;
659 u32 seq
= nlh
->nlmsg_seq
;
661 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
665 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
668 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
671 static inline size_t xfrm_sadinfo_msgsize(void)
673 return NLMSG_ALIGN(4)
674 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
675 + nla_total_size(4); /* XFRMA_SAD_CNT */
678 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
680 struct xfrmk_sadinfo si
;
681 struct xfrmu_sadhinfo sh
;
682 struct nlmsghdr
*nlh
;
685 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
686 if (nlh
== NULL
) /* shouldnt really happen ... */
691 xfrm_sad_getinfo(&si
);
693 sh
.sadhmcnt
= si
.sadhmcnt
;
694 sh
.sadhcnt
= si
.sadhcnt
;
696 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
697 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
699 return nlmsg_end(skb
, nlh
);
702 nlmsg_cancel(skb
, nlh
);
706 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
707 struct nlattr
**attrs
)
709 struct sk_buff
*r_skb
;
710 u32
*flags
= nlmsg_data(nlh
);
711 u32 spid
= NETLINK_CB(skb
).pid
;
712 u32 seq
= nlh
->nlmsg_seq
;
714 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
718 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
721 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
724 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
725 struct nlattr
**attrs
)
727 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
728 struct xfrm_state
*x
;
729 struct sk_buff
*resp_skb
;
732 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
736 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
737 if (IS_ERR(resp_skb
)) {
738 err
= PTR_ERR(resp_skb
);
740 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
747 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
749 switch (p
->info
.id
.proto
) {
755 /* IPCOMP spi is 16-bits. */
756 if (p
->max
>= 0x10000)
770 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
771 struct nlattr
**attrs
)
773 struct xfrm_state
*x
;
774 struct xfrm_userspi_info
*p
;
775 struct sk_buff
*resp_skb
;
776 xfrm_address_t
*daddr
;
781 err
= verify_userspi_info(p
);
785 family
= p
->info
.family
;
786 daddr
= &p
->info
.id
.daddr
;
790 x
= xfrm_find_acq_byseq(p
->info
.seq
);
791 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
798 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
799 p
->info
.id
.proto
, daddr
,
806 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
810 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
811 if (IS_ERR(resp_skb
)) {
812 err
= PTR_ERR(resp_skb
);
816 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
824 static int verify_policy_dir(u8 dir
)
828 case XFRM_POLICY_OUT
:
829 case XFRM_POLICY_FWD
:
839 static int verify_policy_type(u8 type
)
842 case XFRM_POLICY_TYPE_MAIN
:
843 #ifdef CONFIG_XFRM_SUB_POLICY
844 case XFRM_POLICY_TYPE_SUB
:
855 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
859 case XFRM_SHARE_SESSION
:
860 case XFRM_SHARE_USER
:
861 case XFRM_SHARE_UNIQUE
:
869 case XFRM_POLICY_ALLOW
:
870 case XFRM_POLICY_BLOCK
:
877 switch (p
->sel
.family
) {
882 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
885 return -EAFNOSUPPORT
;
892 return verify_policy_dir(p
->dir
);
895 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
897 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
898 struct xfrm_user_sec_ctx
*uctx
;
904 return security_xfrm_policy_alloc(pol
, uctx
);
907 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
913 for (i
= 0; i
< nr
; i
++, ut
++) {
914 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
916 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
917 memcpy(&t
->saddr
, &ut
->saddr
,
918 sizeof(xfrm_address_t
));
919 t
->reqid
= ut
->reqid
;
921 t
->share
= ut
->share
;
922 t
->optional
= ut
->optional
;
923 t
->aalgos
= ut
->aalgos
;
924 t
->ealgos
= ut
->ealgos
;
925 t
->calgos
= ut
->calgos
;
926 t
->encap_family
= ut
->family
;
930 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
934 if (nr
> XFRM_MAX_DEPTH
)
937 for (i
= 0; i
< nr
; i
++) {
938 /* We never validated the ut->family value, so many
939 * applications simply leave it at zero. The check was
940 * never made and ut->family was ignored because all
941 * templates could be assumed to have the same family as
942 * the policy itself. Now that we will have ipv4-in-ipv6
943 * and ipv6-in-ipv4 tunnels, this is no longer true.
946 ut
[i
].family
= family
;
948 switch (ut
[i
].family
) {
951 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
963 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
965 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
970 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
971 int nr
= nla_len(rt
) / sizeof(*utmpl
);
974 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
978 copy_templates(pol
, utmpl
, nr
);
983 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
985 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
986 struct xfrm_userpolicy_type
*upt
;
987 u8 type
= XFRM_POLICY_TYPE_MAIN
;
995 err
= verify_policy_type(type
);
1003 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1005 xp
->priority
= p
->priority
;
1006 xp
->index
= p
->index
;
1007 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1008 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1009 xp
->action
= p
->action
;
1010 xp
->flags
= p
->flags
;
1011 xp
->family
= p
->sel
.family
;
1012 /* XXX xp->share = p->share; */
1015 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1017 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1018 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1019 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1020 p
->priority
= xp
->priority
;
1021 p
->index
= xp
->index
;
1022 p
->sel
.family
= xp
->family
;
1024 p
->action
= xp
->action
;
1025 p
->flags
= xp
->flags
;
1026 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1029 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1031 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1039 copy_from_user_policy(xp
, p
);
1041 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1045 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1046 err
= copy_from_user_sec_ctx(xp
, attrs
);
1057 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1058 struct nlattr
**attrs
)
1060 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1061 struct xfrm_policy
*xp
;
1066 err
= verify_newpolicy_info(p
);
1069 err
= verify_sec_ctx_len(attrs
);
1073 xp
= xfrm_policy_construct(p
, attrs
, &err
);
1077 /* shouldnt excl be based on nlh flags??
1078 * Aha! this is anti-netlink really i.e more pfkey derived
1079 * in netlink excl is a flag and you wouldnt need
1080 * a type XFRM_MSG_UPDPOLICY - JHS */
1081 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1082 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1083 xfrm_audit_policy_add(xp
, err
? 0 : 1, NETLINK_CB(skb
).loginuid
,
1084 NETLINK_CB(skb
).sid
);
1087 security_xfrm_policy_free(xp
);
1092 c
.event
= nlh
->nlmsg_type
;
1093 c
.seq
= nlh
->nlmsg_seq
;
1094 c
.pid
= nlh
->nlmsg_pid
;
1095 km_policy_notify(xp
, p
->dir
, &c
);
1102 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1104 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1107 if (xp
->xfrm_nr
== 0)
1110 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1111 struct xfrm_user_tmpl
*up
= &vec
[i
];
1112 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1114 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1115 up
->family
= kp
->encap_family
;
1116 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1117 up
->reqid
= kp
->reqid
;
1118 up
->mode
= kp
->mode
;
1119 up
->share
= kp
->share
;
1120 up
->optional
= kp
->optional
;
1121 up
->aalgos
= kp
->aalgos
;
1122 up
->ealgos
= kp
->ealgos
;
1123 up
->calgos
= kp
->calgos
;
1126 return nla_put(skb
, XFRMA_TMPL
,
1127 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1130 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1133 return copy_sec_ctx(x
->security
, skb
);
1138 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1141 return copy_sec_ctx(xp
->security
, skb
);
1145 static inline size_t userpolicy_type_attrsize(void)
1147 #ifdef CONFIG_XFRM_SUB_POLICY
1148 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1154 #ifdef CONFIG_XFRM_SUB_POLICY
1155 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1157 struct xfrm_userpolicy_type upt
= {
1161 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1165 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1171 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1173 struct xfrm_dump_info
*sp
= ptr
;
1174 struct xfrm_userpolicy_info
*p
;
1175 struct sk_buff
*in_skb
= sp
->in_skb
;
1176 struct sk_buff
*skb
= sp
->out_skb
;
1177 struct nlmsghdr
*nlh
;
1179 if (sp
->this_idx
< sp
->start_idx
)
1182 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1183 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1187 p
= nlmsg_data(nlh
);
1188 copy_to_user_policy(xp
, p
, dir
);
1189 if (copy_to_user_tmpl(xp
, skb
) < 0)
1191 if (copy_to_user_sec_ctx(xp
, skb
))
1193 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1196 nlmsg_end(skb
, nlh
);
1202 nlmsg_cancel(skb
, nlh
);
1206 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1208 struct xfrm_dump_info info
;
1210 info
.in_skb
= cb
->skb
;
1212 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1213 info
.nlmsg_flags
= NLM_F_MULTI
;
1215 info
.start_idx
= cb
->args
[0];
1216 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN
, dump_one_policy
, &info
);
1217 #ifdef CONFIG_XFRM_SUB_POLICY
1218 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB
, dump_one_policy
, &info
);
1220 cb
->args
[0] = info
.this_idx
;
1225 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1226 struct xfrm_policy
*xp
,
1229 struct xfrm_dump_info info
;
1230 struct sk_buff
*skb
;
1232 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1234 return ERR_PTR(-ENOMEM
);
1236 info
.in_skb
= in_skb
;
1238 info
.nlmsg_seq
= seq
;
1239 info
.nlmsg_flags
= 0;
1240 info
.this_idx
= info
.start_idx
= 0;
1242 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1250 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1251 struct nlattr
**attrs
)
1253 struct xfrm_policy
*xp
;
1254 struct xfrm_userpolicy_id
*p
;
1255 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1260 p
= nlmsg_data(nlh
);
1261 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1263 err
= copy_from_user_policy_type(&type
, attrs
);
1267 err
= verify_policy_dir(p
->dir
);
1272 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1274 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1275 struct xfrm_policy tmp
;
1277 err
= verify_sec_ctx_len(attrs
);
1281 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1283 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1285 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1288 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1290 security_xfrm_policy_free(&tmp
);
1296 struct sk_buff
*resp_skb
;
1298 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1299 if (IS_ERR(resp_skb
)) {
1300 err
= PTR_ERR(resp_skb
);
1302 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1303 NETLINK_CB(skb
).pid
);
1306 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
1307 NETLINK_CB(skb
).loginuid
,
1308 NETLINK_CB(skb
).sid
);
1313 c
.data
.byid
= p
->index
;
1314 c
.event
= nlh
->nlmsg_type
;
1315 c
.seq
= nlh
->nlmsg_seq
;
1316 c
.pid
= nlh
->nlmsg_pid
;
1317 km_policy_notify(xp
, p
->dir
, &c
);
1325 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1326 struct nlattr
**attrs
)
1329 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1330 struct xfrm_audit audit_info
;
1333 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1334 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1335 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1338 c
.data
.proto
= p
->proto
;
1339 c
.event
= nlh
->nlmsg_type
;
1340 c
.seq
= nlh
->nlmsg_seq
;
1341 c
.pid
= nlh
->nlmsg_pid
;
1342 km_state_notify(NULL
, &c
);
1347 static inline size_t xfrm_aevent_msgsize(void)
1349 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1350 + nla_total_size(sizeof(struct xfrm_replay_state
))
1351 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1352 + nla_total_size(4) /* XFRM_AE_RTHR */
1353 + nla_total_size(4); /* XFRM_AE_ETHR */
1356 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1358 struct xfrm_aevent_id
*id
;
1359 struct nlmsghdr
*nlh
;
1361 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1365 id
= nlmsg_data(nlh
);
1366 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1367 id
->sa_id
.spi
= x
->id
.spi
;
1368 id
->sa_id
.family
= x
->props
.family
;
1369 id
->sa_id
.proto
= x
->id
.proto
;
1370 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1371 id
->reqid
= x
->props
.reqid
;
1372 id
->flags
= c
->data
.aevent
;
1374 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1375 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1377 if (id
->flags
& XFRM_AE_RTHR
)
1378 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1380 if (id
->flags
& XFRM_AE_ETHR
)
1381 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1382 x
->replay_maxage
* 10 / HZ
);
1384 return nlmsg_end(skb
, nlh
);
1387 nlmsg_cancel(skb
, nlh
);
1391 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1392 struct nlattr
**attrs
)
1394 struct xfrm_state
*x
;
1395 struct sk_buff
*r_skb
;
1398 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1399 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1401 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1405 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1412 * XXX: is this lock really needed - none of the other
1413 * gets lock (the concern is things getting updated
1414 * while we are still reading) - jhs
1416 spin_lock_bh(&x
->lock
);
1417 c
.data
.aevent
= p
->flags
;
1418 c
.seq
= nlh
->nlmsg_seq
;
1419 c
.pid
= nlh
->nlmsg_pid
;
1421 if (build_aevent(r_skb
, x
, &c
) < 0)
1423 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1424 spin_unlock_bh(&x
->lock
);
1429 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1430 struct nlattr
**attrs
)
1432 struct xfrm_state
*x
;
1435 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1436 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1437 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1442 /* pedantic mode - thou shalt sayeth replaceth */
1443 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1446 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1450 if (x
->km
.state
!= XFRM_STATE_VALID
)
1453 spin_lock_bh(&x
->lock
);
1454 xfrm_update_ae_params(x
, attrs
);
1455 spin_unlock_bh(&x
->lock
);
1457 c
.event
= nlh
->nlmsg_type
;
1458 c
.seq
= nlh
->nlmsg_seq
;
1459 c
.pid
= nlh
->nlmsg_pid
;
1460 c
.data
.aevent
= XFRM_AE_CU
;
1461 km_state_notify(x
, &c
);
1468 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1469 struct nlattr
**attrs
)
1472 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1474 struct xfrm_audit audit_info
;
1476 err
= copy_from_user_policy_type(&type
, attrs
);
1480 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1481 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1482 err
= xfrm_policy_flush(type
, &audit_info
);
1486 c
.event
= nlh
->nlmsg_type
;
1487 c
.seq
= nlh
->nlmsg_seq
;
1488 c
.pid
= nlh
->nlmsg_pid
;
1489 km_policy_notify(NULL
, 0, &c
);
1493 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1494 struct nlattr
**attrs
)
1496 struct xfrm_policy
*xp
;
1497 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1498 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1499 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1502 err
= copy_from_user_policy_type(&type
, attrs
);
1507 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1509 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1510 struct xfrm_policy tmp
;
1512 err
= verify_sec_ctx_len(attrs
);
1516 memset(&tmp
, 0, sizeof(struct xfrm_policy
));
1518 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1520 if ((err
= security_xfrm_policy_alloc(&tmp
, uctx
)))
1523 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, tmp
.security
,
1525 security_xfrm_policy_free(&tmp
);
1530 read_lock(&xp
->lock
);
1532 read_unlock(&xp
->lock
);
1536 read_unlock(&xp
->lock
);
1539 xfrm_policy_delete(xp
, p
->dir
);
1540 xfrm_audit_policy_delete(xp
, 1, NETLINK_CB(skb
).loginuid
,
1541 NETLINK_CB(skb
).sid
);
1544 // reset the timers here?
1545 printk("Dont know what to do with soft policy expire\n");
1547 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1554 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1555 struct nlattr
**attrs
)
1557 struct xfrm_state
*x
;
1559 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1560 struct xfrm_usersa_info
*p
= &ue
->state
;
1562 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1568 spin_lock_bh(&x
->lock
);
1570 if (x
->km
.state
!= XFRM_STATE_VALID
)
1572 km_state_expired(x
, ue
->hard
, current
->pid
);
1575 __xfrm_state_delete(x
);
1576 xfrm_audit_state_delete(x
, 1, NETLINK_CB(skb
).loginuid
,
1577 NETLINK_CB(skb
).sid
);
1581 spin_unlock_bh(&x
->lock
);
1586 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1587 struct nlattr
**attrs
)
1589 struct xfrm_policy
*xp
;
1590 struct xfrm_user_tmpl
*ut
;
1592 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1594 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1595 struct xfrm_state
*x
= xfrm_state_alloc();
1601 err
= verify_newpolicy_info(&ua
->policy
);
1603 printk("BAD policy passed\n");
1609 xp
= xfrm_policy_construct(&ua
->policy
, attrs
, &err
);
1615 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1616 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1617 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1620 /* extract the templates and for each call km_key */
1621 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1622 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1623 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1624 x
->props
.mode
= t
->mode
;
1625 x
->props
.reqid
= t
->reqid
;
1626 x
->props
.family
= ut
->family
;
1627 t
->aalgos
= ua
->aalgos
;
1628 t
->ealgos
= ua
->ealgos
;
1629 t
->calgos
= ua
->calgos
;
1630 err
= km_query(x
, t
, xp
);
1640 #ifdef CONFIG_XFRM_MIGRATE
1641 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1642 struct nlattr
**attrs
, int *num
)
1644 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1645 struct xfrm_user_migrate
*um
;
1649 num_migrate
= nla_len(rt
) / sizeof(*um
);
1651 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1654 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1655 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1656 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1657 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1658 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1660 ma
->proto
= um
->proto
;
1661 ma
->mode
= um
->mode
;
1662 ma
->reqid
= um
->reqid
;
1664 ma
->old_family
= um
->old_family
;
1665 ma
->new_family
= um
->new_family
;
1672 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1673 struct nlattr
**attrs
)
1675 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1676 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1681 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1684 err
= copy_from_user_policy_type(&type
, attrs
);
1688 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1696 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1701 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1702 struct nlattr
**attrs
)
1704 return -ENOPROTOOPT
;
1708 #ifdef CONFIG_XFRM_MIGRATE
1709 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1711 struct xfrm_user_migrate um
;
1713 memset(&um
, 0, sizeof(um
));
1714 um
.proto
= m
->proto
;
1716 um
.reqid
= m
->reqid
;
1717 um
.old_family
= m
->old_family
;
1718 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1719 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1720 um
.new_family
= m
->new_family
;
1721 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1722 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1724 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1727 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1729 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1730 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1731 + userpolicy_type_attrsize();
1734 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1735 int num_migrate
, struct xfrm_selector
*sel
,
1738 struct xfrm_migrate
*mp
;
1739 struct xfrm_userpolicy_id
*pol_id
;
1740 struct nlmsghdr
*nlh
;
1743 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1747 pol_id
= nlmsg_data(nlh
);
1748 /* copy data from selector, dir, and type to the pol_id */
1749 memset(pol_id
, 0, sizeof(*pol_id
));
1750 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1753 if (copy_to_user_policy_type(type
, skb
) < 0)
1756 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1757 if (copy_to_user_migrate(mp
, skb
) < 0)
1761 return nlmsg_end(skb
, nlh
);
1763 nlmsg_cancel(skb
, nlh
);
1767 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1768 struct xfrm_migrate
*m
, int num_migrate
)
1770 struct sk_buff
*skb
;
1772 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1777 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1780 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1783 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1784 struct xfrm_migrate
*m
, int num_migrate
)
1786 return -ENOPROTOOPT
;
1790 #define XMSGSIZE(type) sizeof(struct type)
1792 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1793 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1794 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1795 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1796 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1797 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1798 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1799 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1800 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1801 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1802 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1803 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1804 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1805 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1806 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1807 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1808 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1809 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1810 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1811 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1812 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1817 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1818 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1819 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1820 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1821 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1822 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1823 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1824 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1825 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1826 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1827 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1828 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1829 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1830 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1831 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1834 static struct xfrm_link
{
1835 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
1836 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1837 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1838 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1839 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1840 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1841 .dump
= xfrm_dump_sa
},
1842 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1843 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1844 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1845 .dump
= xfrm_dump_policy
},
1846 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1847 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1848 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1849 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1850 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1851 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1852 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1853 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1854 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1855 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1856 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1857 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1858 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1861 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1863 struct nlattr
*attrs
[XFRMA_MAX
+1];
1864 struct xfrm_link
*link
;
1867 type
= nlh
->nlmsg_type
;
1868 if (type
> XFRM_MSG_MAX
)
1871 type
-= XFRM_MSG_BASE
;
1872 link
= &xfrm_dispatch
[type
];
1874 /* All operations require privileges, even GET */
1875 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1878 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1879 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1880 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1881 if (link
->dump
== NULL
)
1884 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, NULL
);
1887 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
1892 if (link
->doit
== NULL
)
1895 return link
->doit(skb
, nlh
, attrs
);
1898 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
1900 mutex_lock(&xfrm_cfg_mutex
);
1901 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
1902 mutex_unlock(&xfrm_cfg_mutex
);
1905 static inline size_t xfrm_expire_msgsize(void)
1907 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1910 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1912 struct xfrm_user_expire
*ue
;
1913 struct nlmsghdr
*nlh
;
1915 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1919 ue
= nlmsg_data(nlh
);
1920 copy_to_user_state(x
, &ue
->state
);
1921 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1923 return nlmsg_end(skb
, nlh
);
1926 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1928 struct sk_buff
*skb
;
1930 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
1934 if (build_expire(skb
, x
, c
) < 0)
1937 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
1940 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
1942 struct sk_buff
*skb
;
1944 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1948 if (build_aevent(skb
, x
, c
) < 0)
1951 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
1954 static int xfrm_notify_sa_flush(struct km_event
*c
)
1956 struct xfrm_usersa_flush
*p
;
1957 struct nlmsghdr
*nlh
;
1958 struct sk_buff
*skb
;
1959 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
1961 skb
= nlmsg_new(len
, GFP_ATOMIC
);
1965 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
1971 p
= nlmsg_data(nlh
);
1972 p
->proto
= c
->data
.proto
;
1974 nlmsg_end(skb
, nlh
);
1976 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
1979 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
1983 l
+= nla_total_size(alg_len(x
->aalg
));
1985 l
+= nla_total_size(alg_len(x
->ealg
));
1987 l
+= nla_total_size(sizeof(*x
->calg
));
1989 l
+= nla_total_size(sizeof(*x
->encap
));
1991 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
1992 x
->security
->ctx_len
);
1994 l
+= nla_total_size(sizeof(*x
->coaddr
));
1996 /* Must count this as this may become non-zero behind our back. */
1997 l
+= nla_total_size(sizeof(x
->lastused
));
2002 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2004 struct xfrm_usersa_info
*p
;
2005 struct xfrm_usersa_id
*id
;
2006 struct nlmsghdr
*nlh
;
2007 struct sk_buff
*skb
;
2008 int len
= xfrm_sa_len(x
);
2011 headlen
= sizeof(*p
);
2012 if (c
->event
== XFRM_MSG_DELSA
) {
2013 len
+= nla_total_size(headlen
);
2014 headlen
= sizeof(*id
);
2016 len
+= NLMSG_ALIGN(headlen
);
2018 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2022 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2024 goto nla_put_failure
;
2026 p
= nlmsg_data(nlh
);
2027 if (c
->event
== XFRM_MSG_DELSA
) {
2028 struct nlattr
*attr
;
2030 id
= nlmsg_data(nlh
);
2031 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2032 id
->spi
= x
->id
.spi
;
2033 id
->family
= x
->props
.family
;
2034 id
->proto
= x
->id
.proto
;
2036 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2038 goto nla_put_failure
;
2043 if (copy_to_user_state_extra(x
, p
, skb
))
2044 goto nla_put_failure
;
2046 nlmsg_end(skb
, nlh
);
2048 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2051 /* Somebody screwed up with xfrm_sa_len! */
2057 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2061 case XFRM_MSG_EXPIRE
:
2062 return xfrm_exp_state_notify(x
, c
);
2063 case XFRM_MSG_NEWAE
:
2064 return xfrm_aevent_state_notify(x
, c
);
2065 case XFRM_MSG_DELSA
:
2066 case XFRM_MSG_UPDSA
:
2067 case XFRM_MSG_NEWSA
:
2068 return xfrm_notify_sa(x
, c
);
2069 case XFRM_MSG_FLUSHSA
:
2070 return xfrm_notify_sa_flush(c
);
2072 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2080 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2081 struct xfrm_policy
*xp
)
2083 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2084 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2085 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2086 + userpolicy_type_attrsize();
2089 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2090 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2093 struct xfrm_user_acquire
*ua
;
2094 struct nlmsghdr
*nlh
;
2095 __u32 seq
= xfrm_get_acqseq();
2097 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2101 ua
= nlmsg_data(nlh
);
2102 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2103 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2104 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2105 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2106 ua
->aalgos
= xt
->aalgos
;
2107 ua
->ealgos
= xt
->ealgos
;
2108 ua
->calgos
= xt
->calgos
;
2109 ua
->seq
= x
->km
.seq
= seq
;
2111 if (copy_to_user_tmpl(xp
, skb
) < 0)
2113 if (copy_to_user_state_sec_ctx(x
, skb
))
2115 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2118 return nlmsg_end(skb
, nlh
);
2121 nlmsg_cancel(skb
, nlh
);
2125 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2126 struct xfrm_policy
*xp
, int dir
)
2128 struct sk_buff
*skb
;
2130 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2134 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2137 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2140 /* User gives us xfrm_user_policy_info followed by an array of 0
2141 * or more templates.
2143 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2144 u8
*data
, int len
, int *dir
)
2146 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2147 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2148 struct xfrm_policy
*xp
;
2151 switch (sk
->sk_family
) {
2153 if (opt
!= IP_XFRM_POLICY
) {
2158 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2160 if (opt
!= IPV6_XFRM_POLICY
) {
2173 if (len
< sizeof(*p
) ||
2174 verify_newpolicy_info(p
))
2177 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2178 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2181 if (p
->dir
> XFRM_POLICY_OUT
)
2184 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2190 copy_from_user_policy(xp
, p
);
2191 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2192 copy_templates(xp
, ut
, nr
);
2199 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2201 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2202 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2203 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2204 + userpolicy_type_attrsize();
2207 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2208 int dir
, struct km_event
*c
)
2210 struct xfrm_user_polexpire
*upe
;
2211 struct nlmsghdr
*nlh
;
2212 int hard
= c
->data
.hard
;
2214 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2218 upe
= nlmsg_data(nlh
);
2219 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2220 if (copy_to_user_tmpl(xp
, skb
) < 0)
2222 if (copy_to_user_sec_ctx(xp
, skb
))
2224 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2228 return nlmsg_end(skb
, nlh
);
2231 nlmsg_cancel(skb
, nlh
);
2235 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2237 struct sk_buff
*skb
;
2239 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2243 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2246 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2249 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2251 struct xfrm_userpolicy_info
*p
;
2252 struct xfrm_userpolicy_id
*id
;
2253 struct nlmsghdr
*nlh
;
2254 struct sk_buff
*skb
;
2255 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2258 headlen
= sizeof(*p
);
2259 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2260 len
+= nla_total_size(headlen
);
2261 headlen
= sizeof(*id
);
2263 len
+= userpolicy_type_attrsize();
2264 len
+= NLMSG_ALIGN(headlen
);
2266 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2270 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2274 p
= nlmsg_data(nlh
);
2275 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2276 struct nlattr
*attr
;
2278 id
= nlmsg_data(nlh
);
2279 memset(id
, 0, sizeof(*id
));
2282 id
->index
= xp
->index
;
2284 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2286 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2293 copy_to_user_policy(xp
, p
, dir
);
2294 if (copy_to_user_tmpl(xp
, skb
) < 0)
2296 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2299 nlmsg_end(skb
, nlh
);
2301 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2308 static int xfrm_notify_policy_flush(struct km_event
*c
)
2310 struct nlmsghdr
*nlh
;
2311 struct sk_buff
*skb
;
2313 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2317 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2320 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2323 nlmsg_end(skb
, nlh
);
2325 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2332 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2336 case XFRM_MSG_NEWPOLICY
:
2337 case XFRM_MSG_UPDPOLICY
:
2338 case XFRM_MSG_DELPOLICY
:
2339 return xfrm_notify_policy(xp
, dir
, c
);
2340 case XFRM_MSG_FLUSHPOLICY
:
2341 return xfrm_notify_policy_flush(c
);
2342 case XFRM_MSG_POLEXPIRE
:
2343 return xfrm_exp_policy_notify(xp
, dir
, c
);
2345 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2352 static inline size_t xfrm_report_msgsize(void)
2354 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2357 static int build_report(struct sk_buff
*skb
, u8 proto
,
2358 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2360 struct xfrm_user_report
*ur
;
2361 struct nlmsghdr
*nlh
;
2363 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2367 ur
= nlmsg_data(nlh
);
2369 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2372 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2374 return nlmsg_end(skb
, nlh
);
2377 nlmsg_cancel(skb
, nlh
);
2381 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2382 xfrm_address_t
*addr
)
2384 struct sk_buff
*skb
;
2386 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2390 if (build_report(skb
, proto
, sel
, addr
) < 0)
2393 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2396 static struct xfrm_mgr netlink_mgr
= {
2398 .notify
= xfrm_send_state_notify
,
2399 .acquire
= xfrm_send_acquire
,
2400 .compile_policy
= xfrm_compile_policy
,
2401 .notify_policy
= xfrm_send_policy_notify
,
2402 .report
= xfrm_send_report
,
2403 .migrate
= xfrm_send_migrate
,
2406 static int __init
xfrm_user_init(void)
2410 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2412 nlsk
= netlink_kernel_create(&init_net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2413 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2416 rcu_assign_pointer(xfrm_nl
, nlsk
);
2418 xfrm_register_km(&netlink_mgr
);
2423 static void __exit
xfrm_user_exit(void)
2425 struct sock
*nlsk
= xfrm_nl
;
2427 xfrm_unregister_km(&netlink_mgr
);
2428 rcu_assign_pointer(xfrm_nl
, NULL
);
2430 sock_release(nlsk
->sk_socket
);
2433 module_init(xfrm_user_init
);
2434 module_exit(xfrm_user_exit
);
2435 MODULE_LICENSE("GPL");
2436 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);