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 aead_len(struct xfrm_algo_aead
*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
) < xfrm_alg_len(algp
))
61 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
65 static int verify_aead(struct nlattr
**attrs
)
67 struct nlattr
*rt
= attrs
[XFRMA_ALG_AEAD
];
68 struct xfrm_algo_aead
*algp
;
74 if (nla_len(rt
) < aead_len(algp
))
77 algp
->alg_name
[CRYPTO_MAX_ALG_NAME
- 1] = '\0';
81 static void verify_one_addr(struct nlattr
**attrs
, enum xfrm_attr_type_t type
,
82 xfrm_address_t
**addrp
)
84 struct nlattr
*rt
= attrs
[type
];
87 *addrp
= nla_data(rt
);
90 static inline int verify_sec_ctx_len(struct nlattr
**attrs
)
92 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
93 struct xfrm_user_sec_ctx
*uctx
;
99 if (uctx
->len
!= (sizeof(struct xfrm_user_sec_ctx
) + uctx
->ctx_len
))
106 static int verify_newsa_info(struct xfrm_usersa_info
*p
,
107 struct nlattr
**attrs
)
117 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 switch (p
->id
.proto
) {
131 if (!attrs
[XFRMA_ALG_AUTH
] ||
132 attrs
[XFRMA_ALG_AEAD
] ||
133 attrs
[XFRMA_ALG_CRYPT
] ||
134 attrs
[XFRMA_ALG_COMP
])
139 if (attrs
[XFRMA_ALG_COMP
])
141 if (!attrs
[XFRMA_ALG_AUTH
] &&
142 !attrs
[XFRMA_ALG_CRYPT
] &&
143 !attrs
[XFRMA_ALG_AEAD
])
145 if ((attrs
[XFRMA_ALG_AUTH
] ||
146 attrs
[XFRMA_ALG_CRYPT
]) &&
147 attrs
[XFRMA_ALG_AEAD
])
152 if (!attrs
[XFRMA_ALG_COMP
] ||
153 attrs
[XFRMA_ALG_AEAD
] ||
154 attrs
[XFRMA_ALG_AUTH
] ||
155 attrs
[XFRMA_ALG_CRYPT
])
159 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
160 case IPPROTO_DSTOPTS
:
161 case IPPROTO_ROUTING
:
162 if (attrs
[XFRMA_ALG_COMP
] ||
163 attrs
[XFRMA_ALG_AUTH
] ||
164 attrs
[XFRMA_ALG_AEAD
] ||
165 attrs
[XFRMA_ALG_CRYPT
] ||
166 attrs
[XFRMA_ENCAP
] ||
167 attrs
[XFRMA_SEC_CTX
] ||
168 !attrs
[XFRMA_COADDR
])
177 if ((err
= verify_aead(attrs
)))
179 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_AUTH
)))
181 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_CRYPT
)))
183 if ((err
= verify_one_alg(attrs
, XFRMA_ALG_COMP
)))
185 if ((err
= verify_sec_ctx_len(attrs
)))
190 case XFRM_MODE_TRANSPORT
:
191 case XFRM_MODE_TUNNEL
:
192 case XFRM_MODE_ROUTEOPTIMIZATION
:
206 static int attach_one_algo(struct xfrm_algo
**algpp
, u8
*props
,
207 struct xfrm_algo_desc
*(*get_byname
)(char *, int),
210 struct xfrm_algo
*p
, *ualg
;
211 struct xfrm_algo_desc
*algo
;
216 ualg
= nla_data(rta
);
218 algo
= get_byname(ualg
->alg_name
, 1);
221 *props
= algo
->desc
.sadb_alg_id
;
223 p
= kmemdup(ualg
, xfrm_alg_len(ualg
), GFP_KERNEL
);
227 strcpy(p
->alg_name
, algo
->name
);
232 static int attach_aead(struct xfrm_algo_aead
**algpp
, u8
*props
,
235 struct xfrm_algo_aead
*p
, *ualg
;
236 struct xfrm_algo_desc
*algo
;
241 ualg
= nla_data(rta
);
243 algo
= xfrm_aead_get_byname(ualg
->alg_name
, ualg
->alg_icv_len
, 1);
246 *props
= algo
->desc
.sadb_alg_id
;
248 p
= kmemdup(ualg
, aead_len(ualg
), GFP_KERNEL
);
252 strcpy(p
->alg_name
, algo
->name
);
257 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx
*xfrm_ctx
)
262 len
+= sizeof(struct xfrm_user_sec_ctx
);
263 len
+= xfrm_ctx
->ctx_len
;
268 static void copy_from_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
270 memcpy(&x
->id
, &p
->id
, sizeof(x
->id
));
271 memcpy(&x
->sel
, &p
->sel
, sizeof(x
->sel
));
272 memcpy(&x
->lft
, &p
->lft
, sizeof(x
->lft
));
273 x
->props
.mode
= p
->mode
;
274 x
->props
.replay_window
= p
->replay_window
;
275 x
->props
.reqid
= p
->reqid
;
276 x
->props
.family
= p
->family
;
277 memcpy(&x
->props
.saddr
, &p
->saddr
, sizeof(x
->props
.saddr
));
278 x
->props
.flags
= p
->flags
;
280 if (!x
->sel
.family
&& !(p
->flags
& XFRM_STATE_AF_UNSPEC
))
281 x
->sel
.family
= p
->family
;
285 * someday when pfkey also has support, we could have the code
286 * somehow made shareable and move it to xfrm_state.c - JHS
289 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
291 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
292 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
293 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
294 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
297 struct xfrm_replay_state
*replay
;
298 replay
= nla_data(rp
);
299 memcpy(&x
->replay
, replay
, sizeof(*replay
));
300 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
304 struct xfrm_lifetime_cur
*ltime
;
305 ltime
= nla_data(lt
);
306 x
->curlft
.bytes
= ltime
->bytes
;
307 x
->curlft
.packets
= ltime
->packets
;
308 x
->curlft
.add_time
= ltime
->add_time
;
309 x
->curlft
.use_time
= ltime
->use_time
;
313 x
->replay_maxage
= nla_get_u32(et
);
316 x
->replay_maxdiff
= nla_get_u32(rt
);
319 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
320 struct nlattr
**attrs
,
323 struct xfrm_state
*x
= xfrm_state_alloc();
329 copy_from_user_state(x
, p
);
331 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
332 attrs
[XFRMA_ALG_AEAD
])))
334 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
335 xfrm_aalg_get_byname
,
336 attrs
[XFRMA_ALG_AUTH
])))
338 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
339 xfrm_ealg_get_byname
,
340 attrs
[XFRMA_ALG_CRYPT
])))
342 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
343 xfrm_calg_get_byname
,
344 attrs
[XFRMA_ALG_COMP
])))
347 if (attrs
[XFRMA_ENCAP
]) {
348 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
349 sizeof(*x
->encap
), GFP_KERNEL
);
350 if (x
->encap
== NULL
)
354 if (attrs
[XFRMA_COADDR
]) {
355 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
356 sizeof(*x
->coaddr
), GFP_KERNEL
);
357 if (x
->coaddr
== NULL
)
361 err
= xfrm_init_state(x
);
365 if (attrs
[XFRMA_SEC_CTX
] &&
366 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
370 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
371 /* sysctl_xfrm_aevent_etime is in 100ms units */
372 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
373 x
->preplay
.bitmap
= 0;
374 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
375 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
377 /* override default values from above */
379 xfrm_update_ae_params(x
, attrs
);
384 x
->km
.state
= XFRM_STATE_DEAD
;
391 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
392 struct nlattr
**attrs
)
394 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
395 struct xfrm_state
*x
;
398 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
399 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
400 u32 sid
= NETLINK_CB(skb
).sid
;
402 err
= verify_newsa_info(p
, attrs
);
406 x
= xfrm_state_construct(p
, attrs
, &err
);
411 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
412 err
= xfrm_state_add(x
);
414 err
= xfrm_state_update(x
);
416 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
419 x
->km
.state
= XFRM_STATE_DEAD
;
424 c
.seq
= nlh
->nlmsg_seq
;
425 c
.pid
= nlh
->nlmsg_pid
;
426 c
.event
= nlh
->nlmsg_type
;
428 km_state_notify(x
, &c
);
434 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
435 struct nlattr
**attrs
,
438 struct xfrm_state
*x
= NULL
;
441 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
443 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
445 xfrm_address_t
*saddr
= NULL
;
447 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
454 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
464 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
465 struct nlattr
**attrs
)
467 struct xfrm_state
*x
;
470 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
471 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
472 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
473 u32 sid
= NETLINK_CB(skb
).sid
;
475 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
479 if ((err
= security_xfrm_state_delete(x
)) != 0)
482 if (xfrm_state_kern(x
)) {
487 err
= xfrm_state_delete(x
);
492 c
.seq
= nlh
->nlmsg_seq
;
493 c
.pid
= nlh
->nlmsg_pid
;
494 c
.event
= nlh
->nlmsg_type
;
495 km_state_notify(x
, &c
);
498 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
503 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
505 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
506 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
507 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
508 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
509 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
510 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
511 p
->mode
= x
->props
.mode
;
512 p
->replay_window
= x
->props
.replay_window
;
513 p
->reqid
= x
->props
.reqid
;
514 p
->family
= x
->props
.family
;
515 p
->flags
= x
->props
.flags
;
519 struct xfrm_dump_info
{
520 struct sk_buff
*in_skb
;
521 struct sk_buff
*out_skb
;
526 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
528 struct xfrm_user_sec_ctx
*uctx
;
530 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
532 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
536 uctx
= nla_data(attr
);
537 uctx
->exttype
= XFRMA_SEC_CTX
;
538 uctx
->len
= ctx_size
;
539 uctx
->ctx_doi
= s
->ctx_doi
;
540 uctx
->ctx_alg
= s
->ctx_alg
;
541 uctx
->ctx_len
= s
->ctx_len
;
542 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
547 /* Don't change this without updating xfrm_sa_len! */
548 static int copy_to_user_state_extra(struct xfrm_state
*x
,
549 struct xfrm_usersa_info
*p
,
552 copy_to_user_state(x
, p
);
555 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
558 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
561 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
563 NLA_PUT(skb
, XFRMA_ALG_AUTH
, xfrm_alg_len(x
->aalg
), x
->aalg
);
565 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
567 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
570 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
572 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
573 goto nla_put_failure
;
581 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
583 struct xfrm_dump_info
*sp
= ptr
;
584 struct sk_buff
*in_skb
= sp
->in_skb
;
585 struct sk_buff
*skb
= sp
->out_skb
;
586 struct xfrm_usersa_info
*p
;
587 struct nlmsghdr
*nlh
;
590 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
591 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
597 err
= copy_to_user_state_extra(x
, p
, skb
);
599 goto nla_put_failure
;
605 nlmsg_cancel(skb
, nlh
);
609 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
611 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
612 xfrm_state_walk_done(walk
);
616 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
618 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
619 struct xfrm_dump_info info
;
621 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
622 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
624 info
.in_skb
= cb
->skb
;
626 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
627 info
.nlmsg_flags
= NLM_F_MULTI
;
631 xfrm_state_walk_init(walk
, 0);
634 (void) xfrm_state_walk(walk
, dump_one_state
, &info
);
639 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
640 struct xfrm_state
*x
, u32 seq
)
642 struct xfrm_dump_info info
;
645 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
647 return ERR_PTR(-ENOMEM
);
649 info
.in_skb
= in_skb
;
651 info
.nlmsg_seq
= seq
;
652 info
.nlmsg_flags
= 0;
654 if (dump_one_state(x
, 0, &info
)) {
662 static inline size_t xfrm_spdinfo_msgsize(void)
664 return NLMSG_ALIGN(4)
665 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
666 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
669 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
671 struct xfrmk_spdinfo si
;
672 struct xfrmu_spdinfo spc
;
673 struct xfrmu_spdhinfo sph
;
674 struct nlmsghdr
*nlh
;
677 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
678 if (nlh
== NULL
) /* shouldnt really happen ... */
683 xfrm_spd_getinfo(&si
);
684 spc
.incnt
= si
.incnt
;
685 spc
.outcnt
= si
.outcnt
;
686 spc
.fwdcnt
= si
.fwdcnt
;
687 spc
.inscnt
= si
.inscnt
;
688 spc
.outscnt
= si
.outscnt
;
689 spc
.fwdscnt
= si
.fwdscnt
;
690 sph
.spdhcnt
= si
.spdhcnt
;
691 sph
.spdhmcnt
= si
.spdhmcnt
;
693 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
694 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
696 return nlmsg_end(skb
, nlh
);
699 nlmsg_cancel(skb
, nlh
);
703 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
704 struct nlattr
**attrs
)
706 struct sk_buff
*r_skb
;
707 u32
*flags
= nlmsg_data(nlh
);
708 u32 spid
= NETLINK_CB(skb
).pid
;
709 u32 seq
= nlh
->nlmsg_seq
;
711 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
715 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
718 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
721 static inline size_t xfrm_sadinfo_msgsize(void)
723 return NLMSG_ALIGN(4)
724 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
725 + nla_total_size(4); /* XFRMA_SAD_CNT */
728 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
730 struct xfrmk_sadinfo si
;
731 struct xfrmu_sadhinfo sh
;
732 struct nlmsghdr
*nlh
;
735 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
736 if (nlh
== NULL
) /* shouldnt really happen ... */
741 xfrm_sad_getinfo(&si
);
743 sh
.sadhmcnt
= si
.sadhmcnt
;
744 sh
.sadhcnt
= si
.sadhcnt
;
746 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
747 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
749 return nlmsg_end(skb
, nlh
);
752 nlmsg_cancel(skb
, nlh
);
756 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
757 struct nlattr
**attrs
)
759 struct sk_buff
*r_skb
;
760 u32
*flags
= nlmsg_data(nlh
);
761 u32 spid
= NETLINK_CB(skb
).pid
;
762 u32 seq
= nlh
->nlmsg_seq
;
764 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
768 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
771 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
774 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
775 struct nlattr
**attrs
)
777 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
778 struct xfrm_state
*x
;
779 struct sk_buff
*resp_skb
;
782 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
786 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
787 if (IS_ERR(resp_skb
)) {
788 err
= PTR_ERR(resp_skb
);
790 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
797 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
799 switch (p
->info
.id
.proto
) {
805 /* IPCOMP spi is 16-bits. */
806 if (p
->max
>= 0x10000)
820 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
821 struct nlattr
**attrs
)
823 struct xfrm_state
*x
;
824 struct xfrm_userspi_info
*p
;
825 struct sk_buff
*resp_skb
;
826 xfrm_address_t
*daddr
;
831 err
= verify_userspi_info(p
);
835 family
= p
->info
.family
;
836 daddr
= &p
->info
.id
.daddr
;
840 x
= xfrm_find_acq_byseq(p
->info
.seq
);
841 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
848 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
849 p
->info
.id
.proto
, daddr
,
856 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
860 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
861 if (IS_ERR(resp_skb
)) {
862 err
= PTR_ERR(resp_skb
);
866 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
874 static int verify_policy_dir(u8 dir
)
878 case XFRM_POLICY_OUT
:
879 case XFRM_POLICY_FWD
:
889 static int verify_policy_type(u8 type
)
892 case XFRM_POLICY_TYPE_MAIN
:
893 #ifdef CONFIG_XFRM_SUB_POLICY
894 case XFRM_POLICY_TYPE_SUB
:
905 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
909 case XFRM_SHARE_SESSION
:
910 case XFRM_SHARE_USER
:
911 case XFRM_SHARE_UNIQUE
:
919 case XFRM_POLICY_ALLOW
:
920 case XFRM_POLICY_BLOCK
:
927 switch (p
->sel
.family
) {
932 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
935 return -EAFNOSUPPORT
;
942 return verify_policy_dir(p
->dir
);
945 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
947 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
948 struct xfrm_user_sec_ctx
*uctx
;
954 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
957 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
963 for (i
= 0; i
< nr
; i
++, ut
++) {
964 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
966 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
967 memcpy(&t
->saddr
, &ut
->saddr
,
968 sizeof(xfrm_address_t
));
969 t
->reqid
= ut
->reqid
;
971 t
->share
= ut
->share
;
972 t
->optional
= ut
->optional
;
973 t
->aalgos
= ut
->aalgos
;
974 t
->ealgos
= ut
->ealgos
;
975 t
->calgos
= ut
->calgos
;
976 /* If all masks are ~0, then we allow all algorithms. */
977 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
978 t
->encap_family
= ut
->family
;
982 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
986 if (nr
> XFRM_MAX_DEPTH
)
989 for (i
= 0; i
< nr
; i
++) {
990 /* We never validated the ut->family value, so many
991 * applications simply leave it at zero. The check was
992 * never made and ut->family was ignored because all
993 * templates could be assumed to have the same family as
994 * the policy itself. Now that we will have ipv4-in-ipv6
995 * and ipv6-in-ipv4 tunnels, this is no longer true.
998 ut
[i
].family
= family
;
1000 switch (ut
[i
].family
) {
1003 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1015 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1017 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1022 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1023 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1026 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1030 copy_templates(pol
, utmpl
, nr
);
1035 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1037 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1038 struct xfrm_userpolicy_type
*upt
;
1039 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1047 err
= verify_policy_type(type
);
1055 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1057 xp
->priority
= p
->priority
;
1058 xp
->index
= p
->index
;
1059 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1060 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1061 xp
->action
= p
->action
;
1062 xp
->flags
= p
->flags
;
1063 xp
->family
= p
->sel
.family
;
1064 /* XXX xp->share = p->share; */
1067 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1069 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1070 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1071 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1072 p
->priority
= xp
->priority
;
1073 p
->index
= xp
->index
;
1074 p
->sel
.family
= xp
->family
;
1076 p
->action
= xp
->action
;
1077 p
->flags
= xp
->flags
;
1078 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1081 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1083 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1091 copy_from_user_policy(xp
, p
);
1093 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1097 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1098 err
= copy_from_user_sec_ctx(xp
, attrs
);
1106 xfrm_policy_destroy(xp
);
1110 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1111 struct nlattr
**attrs
)
1113 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1114 struct xfrm_policy
*xp
;
1118 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1119 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1120 u32 sid
= NETLINK_CB(skb
).sid
;
1122 err
= verify_newpolicy_info(p
);
1125 err
= verify_sec_ctx_len(attrs
);
1129 xp
= xfrm_policy_construct(p
, attrs
, &err
);
1133 /* shouldnt excl be based on nlh flags??
1134 * Aha! this is anti-netlink really i.e more pfkey derived
1135 * in netlink excl is a flag and you wouldnt need
1136 * a type XFRM_MSG_UPDPOLICY - JHS */
1137 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1138 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1139 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1142 security_xfrm_policy_free(xp
->security
);
1147 c
.event
= nlh
->nlmsg_type
;
1148 c
.seq
= nlh
->nlmsg_seq
;
1149 c
.pid
= nlh
->nlmsg_pid
;
1150 km_policy_notify(xp
, p
->dir
, &c
);
1157 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1159 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1162 if (xp
->xfrm_nr
== 0)
1165 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1166 struct xfrm_user_tmpl
*up
= &vec
[i
];
1167 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1169 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1170 up
->family
= kp
->encap_family
;
1171 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1172 up
->reqid
= kp
->reqid
;
1173 up
->mode
= kp
->mode
;
1174 up
->share
= kp
->share
;
1175 up
->optional
= kp
->optional
;
1176 up
->aalgos
= kp
->aalgos
;
1177 up
->ealgos
= kp
->ealgos
;
1178 up
->calgos
= kp
->calgos
;
1181 return nla_put(skb
, XFRMA_TMPL
,
1182 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1185 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1188 return copy_sec_ctx(x
->security
, skb
);
1193 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1196 return copy_sec_ctx(xp
->security
, skb
);
1200 static inline size_t userpolicy_type_attrsize(void)
1202 #ifdef CONFIG_XFRM_SUB_POLICY
1203 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1209 #ifdef CONFIG_XFRM_SUB_POLICY
1210 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1212 struct xfrm_userpolicy_type upt
= {
1216 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1220 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1226 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1228 struct xfrm_dump_info
*sp
= ptr
;
1229 struct xfrm_userpolicy_info
*p
;
1230 struct sk_buff
*in_skb
= sp
->in_skb
;
1231 struct sk_buff
*skb
= sp
->out_skb
;
1232 struct nlmsghdr
*nlh
;
1234 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1235 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1239 p
= nlmsg_data(nlh
);
1240 copy_to_user_policy(xp
, p
, dir
);
1241 if (copy_to_user_tmpl(xp
, skb
) < 0)
1243 if (copy_to_user_sec_ctx(xp
, skb
))
1245 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1248 nlmsg_end(skb
, nlh
);
1252 nlmsg_cancel(skb
, nlh
);
1256 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1258 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1260 xfrm_policy_walk_done(walk
);
1264 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1266 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1267 struct xfrm_dump_info info
;
1269 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1270 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1272 info
.in_skb
= cb
->skb
;
1274 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1275 info
.nlmsg_flags
= NLM_F_MULTI
;
1279 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1282 (void) xfrm_policy_walk(walk
, dump_one_policy
, &info
);
1287 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1288 struct xfrm_policy
*xp
,
1291 struct xfrm_dump_info info
;
1292 struct sk_buff
*skb
;
1294 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1296 return ERR_PTR(-ENOMEM
);
1298 info
.in_skb
= in_skb
;
1300 info
.nlmsg_seq
= seq
;
1301 info
.nlmsg_flags
= 0;
1303 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1311 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1312 struct nlattr
**attrs
)
1314 struct xfrm_policy
*xp
;
1315 struct xfrm_userpolicy_id
*p
;
1316 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1321 p
= nlmsg_data(nlh
);
1322 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1324 err
= copy_from_user_policy_type(&type
, attrs
);
1328 err
= verify_policy_dir(p
->dir
);
1333 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1335 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1336 struct xfrm_sec_ctx
*ctx
;
1338 err
= verify_sec_ctx_len(attrs
);
1344 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1346 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1350 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, ctx
,
1352 security_xfrm_policy_free(ctx
);
1358 struct sk_buff
*resp_skb
;
1360 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1361 if (IS_ERR(resp_skb
)) {
1362 err
= PTR_ERR(resp_skb
);
1364 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1365 NETLINK_CB(skb
).pid
);
1368 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1369 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1370 u32 sid
= NETLINK_CB(skb
).sid
;
1372 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1378 c
.data
.byid
= p
->index
;
1379 c
.event
= nlh
->nlmsg_type
;
1380 c
.seq
= nlh
->nlmsg_seq
;
1381 c
.pid
= nlh
->nlmsg_pid
;
1382 km_policy_notify(xp
, p
->dir
, &c
);
1390 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1391 struct nlattr
**attrs
)
1394 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1395 struct xfrm_audit audit_info
;
1398 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1399 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1400 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1401 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1404 c
.data
.proto
= p
->proto
;
1405 c
.event
= nlh
->nlmsg_type
;
1406 c
.seq
= nlh
->nlmsg_seq
;
1407 c
.pid
= nlh
->nlmsg_pid
;
1408 km_state_notify(NULL
, &c
);
1413 static inline size_t xfrm_aevent_msgsize(void)
1415 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1416 + nla_total_size(sizeof(struct xfrm_replay_state
))
1417 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1418 + nla_total_size(4) /* XFRM_AE_RTHR */
1419 + nla_total_size(4); /* XFRM_AE_ETHR */
1422 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1424 struct xfrm_aevent_id
*id
;
1425 struct nlmsghdr
*nlh
;
1427 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1431 id
= nlmsg_data(nlh
);
1432 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1433 id
->sa_id
.spi
= x
->id
.spi
;
1434 id
->sa_id
.family
= x
->props
.family
;
1435 id
->sa_id
.proto
= x
->id
.proto
;
1436 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1437 id
->reqid
= x
->props
.reqid
;
1438 id
->flags
= c
->data
.aevent
;
1440 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1441 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1443 if (id
->flags
& XFRM_AE_RTHR
)
1444 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1446 if (id
->flags
& XFRM_AE_ETHR
)
1447 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1448 x
->replay_maxage
* 10 / HZ
);
1450 return nlmsg_end(skb
, nlh
);
1453 nlmsg_cancel(skb
, nlh
);
1457 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1458 struct nlattr
**attrs
)
1460 struct xfrm_state
*x
;
1461 struct sk_buff
*r_skb
;
1464 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1465 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1467 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1471 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1478 * XXX: is this lock really needed - none of the other
1479 * gets lock (the concern is things getting updated
1480 * while we are still reading) - jhs
1482 spin_lock_bh(&x
->lock
);
1483 c
.data
.aevent
= p
->flags
;
1484 c
.seq
= nlh
->nlmsg_seq
;
1485 c
.pid
= nlh
->nlmsg_pid
;
1487 if (build_aevent(r_skb
, x
, &c
) < 0)
1489 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1490 spin_unlock_bh(&x
->lock
);
1495 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1496 struct nlattr
**attrs
)
1498 struct xfrm_state
*x
;
1501 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1502 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1503 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1508 /* pedantic mode - thou shalt sayeth replaceth */
1509 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1512 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1516 if (x
->km
.state
!= XFRM_STATE_VALID
)
1519 spin_lock_bh(&x
->lock
);
1520 xfrm_update_ae_params(x
, attrs
);
1521 spin_unlock_bh(&x
->lock
);
1523 c
.event
= nlh
->nlmsg_type
;
1524 c
.seq
= nlh
->nlmsg_seq
;
1525 c
.pid
= nlh
->nlmsg_pid
;
1526 c
.data
.aevent
= XFRM_AE_CU
;
1527 km_state_notify(x
, &c
);
1534 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1535 struct nlattr
**attrs
)
1538 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1540 struct xfrm_audit audit_info
;
1542 err
= copy_from_user_policy_type(&type
, attrs
);
1546 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1547 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1548 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1549 err
= xfrm_policy_flush(type
, &audit_info
);
1553 c
.event
= nlh
->nlmsg_type
;
1554 c
.seq
= nlh
->nlmsg_seq
;
1555 c
.pid
= nlh
->nlmsg_pid
;
1556 km_policy_notify(NULL
, 0, &c
);
1560 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1561 struct nlattr
**attrs
)
1563 struct xfrm_policy
*xp
;
1564 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1565 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1566 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1569 err
= copy_from_user_policy_type(&type
, attrs
);
1574 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1576 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1577 struct xfrm_sec_ctx
*ctx
;
1579 err
= verify_sec_ctx_len(attrs
);
1585 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1587 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1591 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, ctx
, 0, &err
);
1592 security_xfrm_policy_free(ctx
);
1597 read_lock(&xp
->lock
);
1599 read_unlock(&xp
->lock
);
1603 read_unlock(&xp
->lock
);
1606 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1607 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1608 u32 sid
= NETLINK_CB(skb
).sid
;
1609 xfrm_policy_delete(xp
, p
->dir
);
1610 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1613 // reset the timers here?
1614 printk("Dont know what to do with soft policy expire\n");
1616 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1623 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1624 struct nlattr
**attrs
)
1626 struct xfrm_state
*x
;
1628 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1629 struct xfrm_usersa_info
*p
= &ue
->state
;
1631 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1637 spin_lock_bh(&x
->lock
);
1639 if (x
->km
.state
!= XFRM_STATE_VALID
)
1641 km_state_expired(x
, ue
->hard
, current
->pid
);
1644 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1645 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1646 u32 sid
= NETLINK_CB(skb
).sid
;
1647 __xfrm_state_delete(x
);
1648 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1652 spin_unlock_bh(&x
->lock
);
1657 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1658 struct nlattr
**attrs
)
1660 struct xfrm_policy
*xp
;
1661 struct xfrm_user_tmpl
*ut
;
1663 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1665 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1666 struct xfrm_state
*x
= xfrm_state_alloc();
1672 err
= verify_newpolicy_info(&ua
->policy
);
1674 printk("BAD policy passed\n");
1680 xp
= xfrm_policy_construct(&ua
->policy
, attrs
, &err
);
1686 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1687 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1688 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1691 /* extract the templates and for each call km_key */
1692 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1693 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1694 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1695 x
->props
.mode
= t
->mode
;
1696 x
->props
.reqid
= t
->reqid
;
1697 x
->props
.family
= ut
->family
;
1698 t
->aalgos
= ua
->aalgos
;
1699 t
->ealgos
= ua
->ealgos
;
1700 t
->calgos
= ua
->calgos
;
1701 err
= km_query(x
, t
, xp
);
1711 #ifdef CONFIG_XFRM_MIGRATE
1712 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1713 struct nlattr
**attrs
, int *num
)
1715 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1716 struct xfrm_user_migrate
*um
;
1720 num_migrate
= nla_len(rt
) / sizeof(*um
);
1722 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1725 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1726 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1727 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1728 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1729 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1731 ma
->proto
= um
->proto
;
1732 ma
->mode
= um
->mode
;
1733 ma
->reqid
= um
->reqid
;
1735 ma
->old_family
= um
->old_family
;
1736 ma
->new_family
= um
->new_family
;
1743 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1744 struct nlattr
**attrs
)
1746 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1747 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1752 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1755 err
= copy_from_user_policy_type(&type
, attrs
);
1759 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1767 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1772 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1773 struct nlattr
**attrs
)
1775 return -ENOPROTOOPT
;
1779 #ifdef CONFIG_XFRM_MIGRATE
1780 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1782 struct xfrm_user_migrate um
;
1784 memset(&um
, 0, sizeof(um
));
1785 um
.proto
= m
->proto
;
1787 um
.reqid
= m
->reqid
;
1788 um
.old_family
= m
->old_family
;
1789 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1790 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1791 um
.new_family
= m
->new_family
;
1792 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1793 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1795 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1798 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1800 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1801 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1802 + userpolicy_type_attrsize();
1805 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1806 int num_migrate
, struct xfrm_selector
*sel
,
1809 struct xfrm_migrate
*mp
;
1810 struct xfrm_userpolicy_id
*pol_id
;
1811 struct nlmsghdr
*nlh
;
1814 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1818 pol_id
= nlmsg_data(nlh
);
1819 /* copy data from selector, dir, and type to the pol_id */
1820 memset(pol_id
, 0, sizeof(*pol_id
));
1821 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1824 if (copy_to_user_policy_type(type
, skb
) < 0)
1827 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1828 if (copy_to_user_migrate(mp
, skb
) < 0)
1832 return nlmsg_end(skb
, nlh
);
1834 nlmsg_cancel(skb
, nlh
);
1838 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1839 struct xfrm_migrate
*m
, int num_migrate
)
1841 struct sk_buff
*skb
;
1843 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1848 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1851 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1854 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1855 struct xfrm_migrate
*m
, int num_migrate
)
1857 return -ENOPROTOOPT
;
1861 #define XMSGSIZE(type) sizeof(struct type)
1863 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1864 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1865 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1866 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1867 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1868 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1869 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1870 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1871 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1872 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1873 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1874 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1875 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1876 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1877 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1878 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1879 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1880 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1881 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1882 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1883 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1888 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1889 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
1890 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1891 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1892 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1893 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1894 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1895 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1896 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1897 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1898 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1899 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1900 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1901 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1902 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1903 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1906 static struct xfrm_link
{
1907 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
1908 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1909 int (*done
)(struct netlink_callback
*);
1910 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1911 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1912 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1913 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1914 .dump
= xfrm_dump_sa
,
1915 .done
= xfrm_dump_sa_done
},
1916 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1917 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1918 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1919 .dump
= xfrm_dump_policy
,
1920 .done
= xfrm_dump_policy_done
},
1921 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1922 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1923 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1924 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1925 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1926 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1927 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1928 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1929 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1930 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1931 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1932 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1933 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1936 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1938 struct nlattr
*attrs
[XFRMA_MAX
+1];
1939 struct xfrm_link
*link
;
1942 type
= nlh
->nlmsg_type
;
1943 if (type
> XFRM_MSG_MAX
)
1946 type
-= XFRM_MSG_BASE
;
1947 link
= &xfrm_dispatch
[type
];
1949 /* All operations require privileges, even GET */
1950 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1953 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1954 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1955 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1956 if (link
->dump
== NULL
)
1959 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, link
->done
);
1962 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
1967 if (link
->doit
== NULL
)
1970 return link
->doit(skb
, nlh
, attrs
);
1973 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
1975 mutex_lock(&xfrm_cfg_mutex
);
1976 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
1977 mutex_unlock(&xfrm_cfg_mutex
);
1980 static inline size_t xfrm_expire_msgsize(void)
1982 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1985 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1987 struct xfrm_user_expire
*ue
;
1988 struct nlmsghdr
*nlh
;
1990 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1994 ue
= nlmsg_data(nlh
);
1995 copy_to_user_state(x
, &ue
->state
);
1996 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1998 return nlmsg_end(skb
, nlh
);
2001 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2003 struct sk_buff
*skb
;
2005 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2009 if (build_expire(skb
, x
, c
) < 0)
2012 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2015 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2017 struct sk_buff
*skb
;
2019 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2023 if (build_aevent(skb
, x
, c
) < 0)
2026 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2029 static int xfrm_notify_sa_flush(struct km_event
*c
)
2031 struct xfrm_usersa_flush
*p
;
2032 struct nlmsghdr
*nlh
;
2033 struct sk_buff
*skb
;
2034 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2036 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2040 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2046 p
= nlmsg_data(nlh
);
2047 p
->proto
= c
->data
.proto
;
2049 nlmsg_end(skb
, nlh
);
2051 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2054 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2058 l
+= nla_total_size(aead_len(x
->aead
));
2060 l
+= nla_total_size(xfrm_alg_len(x
->aalg
));
2062 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2064 l
+= nla_total_size(sizeof(*x
->calg
));
2066 l
+= nla_total_size(sizeof(*x
->encap
));
2068 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2069 x
->security
->ctx_len
);
2071 l
+= nla_total_size(sizeof(*x
->coaddr
));
2073 /* Must count x->lastused as it may become non-zero behind our back. */
2074 l
+= nla_total_size(sizeof(u64
));
2079 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2081 struct xfrm_usersa_info
*p
;
2082 struct xfrm_usersa_id
*id
;
2083 struct nlmsghdr
*nlh
;
2084 struct sk_buff
*skb
;
2085 int len
= xfrm_sa_len(x
);
2088 headlen
= sizeof(*p
);
2089 if (c
->event
== XFRM_MSG_DELSA
) {
2090 len
+= nla_total_size(headlen
);
2091 headlen
= sizeof(*id
);
2093 len
+= NLMSG_ALIGN(headlen
);
2095 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2099 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2101 goto nla_put_failure
;
2103 p
= nlmsg_data(nlh
);
2104 if (c
->event
== XFRM_MSG_DELSA
) {
2105 struct nlattr
*attr
;
2107 id
= nlmsg_data(nlh
);
2108 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2109 id
->spi
= x
->id
.spi
;
2110 id
->family
= x
->props
.family
;
2111 id
->proto
= x
->id
.proto
;
2113 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2115 goto nla_put_failure
;
2120 if (copy_to_user_state_extra(x
, p
, skb
))
2121 goto nla_put_failure
;
2123 nlmsg_end(skb
, nlh
);
2125 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2128 /* Somebody screwed up with xfrm_sa_len! */
2134 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2138 case XFRM_MSG_EXPIRE
:
2139 return xfrm_exp_state_notify(x
, c
);
2140 case XFRM_MSG_NEWAE
:
2141 return xfrm_aevent_state_notify(x
, c
);
2142 case XFRM_MSG_DELSA
:
2143 case XFRM_MSG_UPDSA
:
2144 case XFRM_MSG_NEWSA
:
2145 return xfrm_notify_sa(x
, c
);
2146 case XFRM_MSG_FLUSHSA
:
2147 return xfrm_notify_sa_flush(c
);
2149 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2157 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2158 struct xfrm_policy
*xp
)
2160 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2161 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2162 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2163 + userpolicy_type_attrsize();
2166 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2167 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2170 struct xfrm_user_acquire
*ua
;
2171 struct nlmsghdr
*nlh
;
2172 __u32 seq
= xfrm_get_acqseq();
2174 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2178 ua
= nlmsg_data(nlh
);
2179 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2180 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2181 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2182 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2183 ua
->aalgos
= xt
->aalgos
;
2184 ua
->ealgos
= xt
->ealgos
;
2185 ua
->calgos
= xt
->calgos
;
2186 ua
->seq
= x
->km
.seq
= seq
;
2188 if (copy_to_user_tmpl(xp
, skb
) < 0)
2190 if (copy_to_user_state_sec_ctx(x
, skb
))
2192 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2195 return nlmsg_end(skb
, nlh
);
2198 nlmsg_cancel(skb
, nlh
);
2202 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2203 struct xfrm_policy
*xp
, int dir
)
2205 struct sk_buff
*skb
;
2207 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2211 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2214 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2217 /* User gives us xfrm_user_policy_info followed by an array of 0
2218 * or more templates.
2220 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2221 u8
*data
, int len
, int *dir
)
2223 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2224 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2225 struct xfrm_policy
*xp
;
2228 switch (sk
->sk_family
) {
2230 if (opt
!= IP_XFRM_POLICY
) {
2235 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2237 if (opt
!= IPV6_XFRM_POLICY
) {
2250 if (len
< sizeof(*p
) ||
2251 verify_newpolicy_info(p
))
2254 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2255 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2258 if (p
->dir
> XFRM_POLICY_OUT
)
2261 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2267 copy_from_user_policy(xp
, p
);
2268 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2269 copy_templates(xp
, ut
, nr
);
2276 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2278 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2279 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2280 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2281 + userpolicy_type_attrsize();
2284 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2285 int dir
, struct km_event
*c
)
2287 struct xfrm_user_polexpire
*upe
;
2288 struct nlmsghdr
*nlh
;
2289 int hard
= c
->data
.hard
;
2291 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2295 upe
= nlmsg_data(nlh
);
2296 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2297 if (copy_to_user_tmpl(xp
, skb
) < 0)
2299 if (copy_to_user_sec_ctx(xp
, skb
))
2301 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2305 return nlmsg_end(skb
, nlh
);
2308 nlmsg_cancel(skb
, nlh
);
2312 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2314 struct sk_buff
*skb
;
2316 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2320 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2323 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2326 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2328 struct xfrm_userpolicy_info
*p
;
2329 struct xfrm_userpolicy_id
*id
;
2330 struct nlmsghdr
*nlh
;
2331 struct sk_buff
*skb
;
2332 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2335 headlen
= sizeof(*p
);
2336 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2337 len
+= nla_total_size(headlen
);
2338 headlen
= sizeof(*id
);
2340 len
+= userpolicy_type_attrsize();
2341 len
+= NLMSG_ALIGN(headlen
);
2343 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2347 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2351 p
= nlmsg_data(nlh
);
2352 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2353 struct nlattr
*attr
;
2355 id
= nlmsg_data(nlh
);
2356 memset(id
, 0, sizeof(*id
));
2359 id
->index
= xp
->index
;
2361 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2363 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2370 copy_to_user_policy(xp
, p
, dir
);
2371 if (copy_to_user_tmpl(xp
, skb
) < 0)
2373 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2376 nlmsg_end(skb
, nlh
);
2378 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2385 static int xfrm_notify_policy_flush(struct km_event
*c
)
2387 struct nlmsghdr
*nlh
;
2388 struct sk_buff
*skb
;
2390 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2394 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2397 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2400 nlmsg_end(skb
, nlh
);
2402 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2409 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2413 case XFRM_MSG_NEWPOLICY
:
2414 case XFRM_MSG_UPDPOLICY
:
2415 case XFRM_MSG_DELPOLICY
:
2416 return xfrm_notify_policy(xp
, dir
, c
);
2417 case XFRM_MSG_FLUSHPOLICY
:
2418 return xfrm_notify_policy_flush(c
);
2419 case XFRM_MSG_POLEXPIRE
:
2420 return xfrm_exp_policy_notify(xp
, dir
, c
);
2422 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2429 static inline size_t xfrm_report_msgsize(void)
2431 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2434 static int build_report(struct sk_buff
*skb
, u8 proto
,
2435 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2437 struct xfrm_user_report
*ur
;
2438 struct nlmsghdr
*nlh
;
2440 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2444 ur
= nlmsg_data(nlh
);
2446 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2449 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2451 return nlmsg_end(skb
, nlh
);
2454 nlmsg_cancel(skb
, nlh
);
2458 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2459 xfrm_address_t
*addr
)
2461 struct sk_buff
*skb
;
2463 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2467 if (build_report(skb
, proto
, sel
, addr
) < 0)
2470 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2473 static struct xfrm_mgr netlink_mgr
= {
2475 .notify
= xfrm_send_state_notify
,
2476 .acquire
= xfrm_send_acquire
,
2477 .compile_policy
= xfrm_compile_policy
,
2478 .notify_policy
= xfrm_send_policy_notify
,
2479 .report
= xfrm_send_report
,
2480 .migrate
= xfrm_send_migrate
,
2483 static int __init
xfrm_user_init(void)
2487 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2489 nlsk
= netlink_kernel_create(&init_net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2490 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2493 rcu_assign_pointer(xfrm_nl
, nlsk
);
2495 xfrm_register_km(&netlink_mgr
);
2500 static void __exit
xfrm_user_exit(void)
2502 struct sock
*nlsk
= xfrm_nl
;
2504 xfrm_unregister_km(&netlink_mgr
);
2505 rcu_assign_pointer(xfrm_nl
, NULL
);
2507 netlink_kernel_release(nlsk
);
2510 module_init(xfrm_user_init
);
2511 module_exit(xfrm_user_exit
);
2512 MODULE_LICENSE("GPL");
2513 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);