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
;
281 x
->sel
.family
= p
->family
;
286 * someday when pfkey also has support, we could have the code
287 * somehow made shareable and move it to xfrm_state.c - JHS
290 static void xfrm_update_ae_params(struct xfrm_state
*x
, struct nlattr
**attrs
)
292 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
293 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
294 struct nlattr
*et
= attrs
[XFRMA_ETIMER_THRESH
];
295 struct nlattr
*rt
= attrs
[XFRMA_REPLAY_THRESH
];
298 struct xfrm_replay_state
*replay
;
299 replay
= nla_data(rp
);
300 memcpy(&x
->replay
, replay
, sizeof(*replay
));
301 memcpy(&x
->preplay
, replay
, sizeof(*replay
));
305 struct xfrm_lifetime_cur
*ltime
;
306 ltime
= nla_data(lt
);
307 x
->curlft
.bytes
= ltime
->bytes
;
308 x
->curlft
.packets
= ltime
->packets
;
309 x
->curlft
.add_time
= ltime
->add_time
;
310 x
->curlft
.use_time
= ltime
->use_time
;
314 x
->replay_maxage
= nla_get_u32(et
);
317 x
->replay_maxdiff
= nla_get_u32(rt
);
320 static struct xfrm_state
*xfrm_state_construct(struct xfrm_usersa_info
*p
,
321 struct nlattr
**attrs
,
324 struct xfrm_state
*x
= xfrm_state_alloc();
330 copy_from_user_state(x
, p
);
332 if ((err
= attach_aead(&x
->aead
, &x
->props
.ealgo
,
333 attrs
[XFRMA_ALG_AEAD
])))
335 if ((err
= attach_one_algo(&x
->aalg
, &x
->props
.aalgo
,
336 xfrm_aalg_get_byname
,
337 attrs
[XFRMA_ALG_AUTH
])))
339 if ((err
= attach_one_algo(&x
->ealg
, &x
->props
.ealgo
,
340 xfrm_ealg_get_byname
,
341 attrs
[XFRMA_ALG_CRYPT
])))
343 if ((err
= attach_one_algo(&x
->calg
, &x
->props
.calgo
,
344 xfrm_calg_get_byname
,
345 attrs
[XFRMA_ALG_COMP
])))
348 if (attrs
[XFRMA_ENCAP
]) {
349 x
->encap
= kmemdup(nla_data(attrs
[XFRMA_ENCAP
]),
350 sizeof(*x
->encap
), GFP_KERNEL
);
351 if (x
->encap
== NULL
)
355 if (attrs
[XFRMA_COADDR
]) {
356 x
->coaddr
= kmemdup(nla_data(attrs
[XFRMA_COADDR
]),
357 sizeof(*x
->coaddr
), GFP_KERNEL
);
358 if (x
->coaddr
== NULL
)
362 err
= xfrm_init_state(x
);
366 if (attrs
[XFRMA_SEC_CTX
] &&
367 security_xfrm_state_alloc(x
, nla_data(attrs
[XFRMA_SEC_CTX
])))
371 x
->replay_maxdiff
= sysctl_xfrm_aevent_rseqth
;
372 /* sysctl_xfrm_aevent_etime is in 100ms units */
373 x
->replay_maxage
= (sysctl_xfrm_aevent_etime
*HZ
)/XFRM_AE_ETH_M
;
374 x
->preplay
.bitmap
= 0;
375 x
->preplay
.seq
= x
->replay
.seq
+x
->replay_maxdiff
;
376 x
->preplay
.oseq
= x
->replay
.oseq
+x
->replay_maxdiff
;
378 /* override default values from above */
380 xfrm_update_ae_params(x
, attrs
);
385 x
->km
.state
= XFRM_STATE_DEAD
;
392 static int xfrm_add_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
393 struct nlattr
**attrs
)
395 struct xfrm_usersa_info
*p
= nlmsg_data(nlh
);
396 struct xfrm_state
*x
;
399 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
400 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
401 u32 sid
= NETLINK_CB(skb
).sid
;
403 err
= verify_newsa_info(p
, attrs
);
407 x
= xfrm_state_construct(p
, attrs
, &err
);
412 if (nlh
->nlmsg_type
== XFRM_MSG_NEWSA
)
413 err
= xfrm_state_add(x
);
415 err
= xfrm_state_update(x
);
417 xfrm_audit_state_add(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
420 x
->km
.state
= XFRM_STATE_DEAD
;
425 c
.seq
= nlh
->nlmsg_seq
;
426 c
.pid
= nlh
->nlmsg_pid
;
427 c
.event
= nlh
->nlmsg_type
;
429 km_state_notify(x
, &c
);
435 static struct xfrm_state
*xfrm_user_state_lookup(struct xfrm_usersa_id
*p
,
436 struct nlattr
**attrs
,
439 struct xfrm_state
*x
= NULL
;
442 if (xfrm_id_proto_match(p
->proto
, IPSEC_PROTO_ANY
)) {
444 x
= xfrm_state_lookup(&p
->daddr
, p
->spi
, p
->proto
, p
->family
);
446 xfrm_address_t
*saddr
= NULL
;
448 verify_one_addr(attrs
, XFRMA_SRCADDR
, &saddr
);
455 x
= xfrm_state_lookup_byaddr(&p
->daddr
, saddr
, p
->proto
,
465 static int xfrm_del_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
466 struct nlattr
**attrs
)
468 struct xfrm_state
*x
;
471 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
472 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
473 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
474 u32 sid
= NETLINK_CB(skb
).sid
;
476 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
480 if ((err
= security_xfrm_state_delete(x
)) != 0)
483 if (xfrm_state_kern(x
)) {
488 err
= xfrm_state_delete(x
);
493 c
.seq
= nlh
->nlmsg_seq
;
494 c
.pid
= nlh
->nlmsg_pid
;
495 c
.event
= nlh
->nlmsg_type
;
496 km_state_notify(x
, &c
);
499 xfrm_audit_state_delete(x
, err
? 0 : 1, loginuid
, sessionid
, sid
);
504 static void copy_to_user_state(struct xfrm_state
*x
, struct xfrm_usersa_info
*p
)
506 memcpy(&p
->id
, &x
->id
, sizeof(p
->id
));
507 memcpy(&p
->sel
, &x
->sel
, sizeof(p
->sel
));
508 memcpy(&p
->lft
, &x
->lft
, sizeof(p
->lft
));
509 memcpy(&p
->curlft
, &x
->curlft
, sizeof(p
->curlft
));
510 memcpy(&p
->stats
, &x
->stats
, sizeof(p
->stats
));
511 memcpy(&p
->saddr
, &x
->props
.saddr
, sizeof(p
->saddr
));
512 p
->mode
= x
->props
.mode
;
513 p
->replay_window
= x
->props
.replay_window
;
514 p
->reqid
= x
->props
.reqid
;
515 p
->family
= x
->props
.family
;
516 p
->flags
= x
->props
.flags
;
520 struct xfrm_dump_info
{
521 struct sk_buff
*in_skb
;
522 struct sk_buff
*out_skb
;
527 static int copy_sec_ctx(struct xfrm_sec_ctx
*s
, struct sk_buff
*skb
)
529 struct xfrm_user_sec_ctx
*uctx
;
531 int ctx_size
= sizeof(*uctx
) + s
->ctx_len
;
533 attr
= nla_reserve(skb
, XFRMA_SEC_CTX
, ctx_size
);
537 uctx
= nla_data(attr
);
538 uctx
->exttype
= XFRMA_SEC_CTX
;
539 uctx
->len
= ctx_size
;
540 uctx
->ctx_doi
= s
->ctx_doi
;
541 uctx
->ctx_alg
= s
->ctx_alg
;
542 uctx
->ctx_len
= s
->ctx_len
;
543 memcpy(uctx
+ 1, s
->ctx_str
, s
->ctx_len
);
548 /* Don't change this without updating xfrm_sa_len! */
549 static int copy_to_user_state_extra(struct xfrm_state
*x
,
550 struct xfrm_usersa_info
*p
,
553 copy_to_user_state(x
, p
);
556 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*x
->coaddr
), x
->coaddr
);
559 NLA_PUT_U64(skb
, XFRMA_LASTUSED
, x
->lastused
);
562 NLA_PUT(skb
, XFRMA_ALG_AEAD
, aead_len(x
->aead
), x
->aead
);
564 NLA_PUT(skb
, XFRMA_ALG_AUTH
, xfrm_alg_len(x
->aalg
), x
->aalg
);
566 NLA_PUT(skb
, XFRMA_ALG_CRYPT
, xfrm_alg_len(x
->ealg
), x
->ealg
);
568 NLA_PUT(skb
, XFRMA_ALG_COMP
, sizeof(*(x
->calg
)), x
->calg
);
571 NLA_PUT(skb
, XFRMA_ENCAP
, sizeof(*x
->encap
), x
->encap
);
573 if (x
->security
&& copy_sec_ctx(x
->security
, skb
) < 0)
574 goto nla_put_failure
;
582 static int dump_one_state(struct xfrm_state
*x
, int count
, void *ptr
)
584 struct xfrm_dump_info
*sp
= ptr
;
585 struct sk_buff
*in_skb
= sp
->in_skb
;
586 struct sk_buff
*skb
= sp
->out_skb
;
587 struct xfrm_usersa_info
*p
;
588 struct nlmsghdr
*nlh
;
591 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
592 XFRM_MSG_NEWSA
, sizeof(*p
), sp
->nlmsg_flags
);
598 err
= copy_to_user_state_extra(x
, p
, skb
);
600 goto nla_put_failure
;
606 nlmsg_cancel(skb
, nlh
);
610 static int xfrm_dump_sa_done(struct netlink_callback
*cb
)
612 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
613 xfrm_state_walk_done(walk
);
617 static int xfrm_dump_sa(struct sk_buff
*skb
, struct netlink_callback
*cb
)
619 struct xfrm_state_walk
*walk
= (struct xfrm_state_walk
*) &cb
->args
[1];
620 struct xfrm_dump_info info
;
622 BUILD_BUG_ON(sizeof(struct xfrm_state_walk
) >
623 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
625 info
.in_skb
= cb
->skb
;
627 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
628 info
.nlmsg_flags
= NLM_F_MULTI
;
632 xfrm_state_walk_init(walk
, 0);
635 (void) xfrm_state_walk(walk
, dump_one_state
, &info
);
640 static struct sk_buff
*xfrm_state_netlink(struct sk_buff
*in_skb
,
641 struct xfrm_state
*x
, u32 seq
)
643 struct xfrm_dump_info info
;
646 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
648 return ERR_PTR(-ENOMEM
);
650 info
.in_skb
= in_skb
;
652 info
.nlmsg_seq
= seq
;
653 info
.nlmsg_flags
= 0;
655 if (dump_one_state(x
, 0, &info
)) {
663 static inline size_t xfrm_spdinfo_msgsize(void)
665 return NLMSG_ALIGN(4)
666 + nla_total_size(sizeof(struct xfrmu_spdinfo
))
667 + nla_total_size(sizeof(struct xfrmu_spdhinfo
));
670 static int build_spdinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
672 struct xfrmk_spdinfo si
;
673 struct xfrmu_spdinfo spc
;
674 struct xfrmu_spdhinfo sph
;
675 struct nlmsghdr
*nlh
;
678 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSPDINFO
, sizeof(u32
), 0);
679 if (nlh
== NULL
) /* shouldnt really happen ... */
684 xfrm_spd_getinfo(&si
);
685 spc
.incnt
= si
.incnt
;
686 spc
.outcnt
= si
.outcnt
;
687 spc
.fwdcnt
= si
.fwdcnt
;
688 spc
.inscnt
= si
.inscnt
;
689 spc
.outscnt
= si
.outscnt
;
690 spc
.fwdscnt
= si
.fwdscnt
;
691 sph
.spdhcnt
= si
.spdhcnt
;
692 sph
.spdhmcnt
= si
.spdhmcnt
;
694 NLA_PUT(skb
, XFRMA_SPD_INFO
, sizeof(spc
), &spc
);
695 NLA_PUT(skb
, XFRMA_SPD_HINFO
, sizeof(sph
), &sph
);
697 return nlmsg_end(skb
, nlh
);
700 nlmsg_cancel(skb
, nlh
);
704 static int xfrm_get_spdinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
705 struct nlattr
**attrs
)
707 struct sk_buff
*r_skb
;
708 u32
*flags
= nlmsg_data(nlh
);
709 u32 spid
= NETLINK_CB(skb
).pid
;
710 u32 seq
= nlh
->nlmsg_seq
;
712 r_skb
= nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC
);
716 if (build_spdinfo(r_skb
, spid
, seq
, *flags
) < 0)
719 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
722 static inline size_t xfrm_sadinfo_msgsize(void)
724 return NLMSG_ALIGN(4)
725 + nla_total_size(sizeof(struct xfrmu_sadhinfo
))
726 + nla_total_size(4); /* XFRMA_SAD_CNT */
729 static int build_sadinfo(struct sk_buff
*skb
, u32 pid
, u32 seq
, u32 flags
)
731 struct xfrmk_sadinfo si
;
732 struct xfrmu_sadhinfo sh
;
733 struct nlmsghdr
*nlh
;
736 nlh
= nlmsg_put(skb
, pid
, seq
, XFRM_MSG_NEWSADINFO
, sizeof(u32
), 0);
737 if (nlh
== NULL
) /* shouldnt really happen ... */
742 xfrm_sad_getinfo(&si
);
744 sh
.sadhmcnt
= si
.sadhmcnt
;
745 sh
.sadhcnt
= si
.sadhcnt
;
747 NLA_PUT_U32(skb
, XFRMA_SAD_CNT
, si
.sadcnt
);
748 NLA_PUT(skb
, XFRMA_SAD_HINFO
, sizeof(sh
), &sh
);
750 return nlmsg_end(skb
, nlh
);
753 nlmsg_cancel(skb
, nlh
);
757 static int xfrm_get_sadinfo(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
758 struct nlattr
**attrs
)
760 struct sk_buff
*r_skb
;
761 u32
*flags
= nlmsg_data(nlh
);
762 u32 spid
= NETLINK_CB(skb
).pid
;
763 u32 seq
= nlh
->nlmsg_seq
;
765 r_skb
= nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC
);
769 if (build_sadinfo(r_skb
, spid
, seq
, *flags
) < 0)
772 return nlmsg_unicast(xfrm_nl
, r_skb
, spid
);
775 static int xfrm_get_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
776 struct nlattr
**attrs
)
778 struct xfrm_usersa_id
*p
= nlmsg_data(nlh
);
779 struct xfrm_state
*x
;
780 struct sk_buff
*resp_skb
;
783 x
= xfrm_user_state_lookup(p
, attrs
, &err
);
787 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
788 if (IS_ERR(resp_skb
)) {
789 err
= PTR_ERR(resp_skb
);
791 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
798 static int verify_userspi_info(struct xfrm_userspi_info
*p
)
800 switch (p
->info
.id
.proto
) {
806 /* IPCOMP spi is 16-bits. */
807 if (p
->max
>= 0x10000)
821 static int xfrm_alloc_userspi(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
822 struct nlattr
**attrs
)
824 struct xfrm_state
*x
;
825 struct xfrm_userspi_info
*p
;
826 struct sk_buff
*resp_skb
;
827 xfrm_address_t
*daddr
;
832 err
= verify_userspi_info(p
);
836 family
= p
->info
.family
;
837 daddr
= &p
->info
.id
.daddr
;
841 x
= xfrm_find_acq_byseq(p
->info
.seq
);
842 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, daddr
, family
)) {
849 x
= xfrm_find_acq(p
->info
.mode
, p
->info
.reqid
,
850 p
->info
.id
.proto
, daddr
,
857 err
= xfrm_alloc_spi(x
, p
->min
, p
->max
);
861 resp_skb
= xfrm_state_netlink(skb
, x
, nlh
->nlmsg_seq
);
862 if (IS_ERR(resp_skb
)) {
863 err
= PTR_ERR(resp_skb
);
867 err
= nlmsg_unicast(xfrm_nl
, resp_skb
, NETLINK_CB(skb
).pid
);
875 static int verify_policy_dir(u8 dir
)
879 case XFRM_POLICY_OUT
:
880 case XFRM_POLICY_FWD
:
890 static int verify_policy_type(u8 type
)
893 case XFRM_POLICY_TYPE_MAIN
:
894 #ifdef CONFIG_XFRM_SUB_POLICY
895 case XFRM_POLICY_TYPE_SUB
:
906 static int verify_newpolicy_info(struct xfrm_userpolicy_info
*p
)
910 case XFRM_SHARE_SESSION
:
911 case XFRM_SHARE_USER
:
912 case XFRM_SHARE_UNIQUE
:
920 case XFRM_POLICY_ALLOW
:
921 case XFRM_POLICY_BLOCK
:
928 switch (p
->sel
.family
) {
933 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
936 return -EAFNOSUPPORT
;
943 return verify_policy_dir(p
->dir
);
946 static int copy_from_user_sec_ctx(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
948 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
949 struct xfrm_user_sec_ctx
*uctx
;
955 return security_xfrm_policy_alloc(&pol
->security
, uctx
);
958 static void copy_templates(struct xfrm_policy
*xp
, struct xfrm_user_tmpl
*ut
,
964 for (i
= 0; i
< nr
; i
++, ut
++) {
965 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
967 memcpy(&t
->id
, &ut
->id
, sizeof(struct xfrm_id
));
968 memcpy(&t
->saddr
, &ut
->saddr
,
969 sizeof(xfrm_address_t
));
970 t
->reqid
= ut
->reqid
;
972 t
->share
= ut
->share
;
973 t
->optional
= ut
->optional
;
974 t
->aalgos
= ut
->aalgos
;
975 t
->ealgos
= ut
->ealgos
;
976 t
->calgos
= ut
->calgos
;
977 /* If all masks are ~0, then we allow all algorithms. */
978 t
->allalgs
= !~(t
->aalgos
& t
->ealgos
& t
->calgos
);
979 t
->encap_family
= ut
->family
;
983 static int validate_tmpl(int nr
, struct xfrm_user_tmpl
*ut
, u16 family
)
987 if (nr
> XFRM_MAX_DEPTH
)
990 for (i
= 0; i
< nr
; i
++) {
991 /* We never validated the ut->family value, so many
992 * applications simply leave it at zero. The check was
993 * never made and ut->family was ignored because all
994 * templates could be assumed to have the same family as
995 * the policy itself. Now that we will have ipv4-in-ipv6
996 * and ipv6-in-ipv4 tunnels, this is no longer true.
999 ut
[i
].family
= family
;
1001 switch (ut
[i
].family
) {
1004 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1016 static int copy_from_user_tmpl(struct xfrm_policy
*pol
, struct nlattr
**attrs
)
1018 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1023 struct xfrm_user_tmpl
*utmpl
= nla_data(rt
);
1024 int nr
= nla_len(rt
) / sizeof(*utmpl
);
1027 err
= validate_tmpl(nr
, utmpl
, pol
->family
);
1031 copy_templates(pol
, utmpl
, nr
);
1036 static int copy_from_user_policy_type(u8
*tp
, struct nlattr
**attrs
)
1038 struct nlattr
*rt
= attrs
[XFRMA_POLICY_TYPE
];
1039 struct xfrm_userpolicy_type
*upt
;
1040 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1048 err
= verify_policy_type(type
);
1056 static void copy_from_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
)
1058 xp
->priority
= p
->priority
;
1059 xp
->index
= p
->index
;
1060 memcpy(&xp
->selector
, &p
->sel
, sizeof(xp
->selector
));
1061 memcpy(&xp
->lft
, &p
->lft
, sizeof(xp
->lft
));
1062 xp
->action
= p
->action
;
1063 xp
->flags
= p
->flags
;
1064 xp
->family
= p
->sel
.family
;
1065 /* XXX xp->share = p->share; */
1068 static void copy_to_user_policy(struct xfrm_policy
*xp
, struct xfrm_userpolicy_info
*p
, int dir
)
1070 memcpy(&p
->sel
, &xp
->selector
, sizeof(p
->sel
));
1071 memcpy(&p
->lft
, &xp
->lft
, sizeof(p
->lft
));
1072 memcpy(&p
->curlft
, &xp
->curlft
, sizeof(p
->curlft
));
1073 p
->priority
= xp
->priority
;
1074 p
->index
= xp
->index
;
1075 p
->sel
.family
= xp
->family
;
1077 p
->action
= xp
->action
;
1078 p
->flags
= xp
->flags
;
1079 p
->share
= XFRM_SHARE_ANY
; /* XXX xp->share */
1082 static struct xfrm_policy
*xfrm_policy_construct(struct xfrm_userpolicy_info
*p
, struct nlattr
**attrs
, int *errp
)
1084 struct xfrm_policy
*xp
= xfrm_policy_alloc(GFP_KERNEL
);
1092 copy_from_user_policy(xp
, p
);
1094 err
= copy_from_user_policy_type(&xp
->type
, attrs
);
1098 if (!(err
= copy_from_user_tmpl(xp
, attrs
)))
1099 err
= copy_from_user_sec_ctx(xp
, attrs
);
1107 xfrm_policy_destroy(xp
);
1111 static int xfrm_add_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1112 struct nlattr
**attrs
)
1114 struct xfrm_userpolicy_info
*p
= nlmsg_data(nlh
);
1115 struct xfrm_policy
*xp
;
1119 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1120 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1121 u32 sid
= NETLINK_CB(skb
).sid
;
1123 err
= verify_newpolicy_info(p
);
1126 err
= verify_sec_ctx_len(attrs
);
1130 xp
= xfrm_policy_construct(p
, attrs
, &err
);
1134 /* shouldnt excl be based on nlh flags??
1135 * Aha! this is anti-netlink really i.e more pfkey derived
1136 * in netlink excl is a flag and you wouldnt need
1137 * a type XFRM_MSG_UPDPOLICY - JHS */
1138 excl
= nlh
->nlmsg_type
== XFRM_MSG_NEWPOLICY
;
1139 err
= xfrm_policy_insert(p
->dir
, xp
, excl
);
1140 xfrm_audit_policy_add(xp
, err
? 0 : 1, loginuid
, sessionid
, sid
);
1143 security_xfrm_policy_free(xp
->security
);
1148 c
.event
= nlh
->nlmsg_type
;
1149 c
.seq
= nlh
->nlmsg_seq
;
1150 c
.pid
= nlh
->nlmsg_pid
;
1151 km_policy_notify(xp
, p
->dir
, &c
);
1158 static int copy_to_user_tmpl(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1160 struct xfrm_user_tmpl vec
[XFRM_MAX_DEPTH
];
1163 if (xp
->xfrm_nr
== 0)
1166 for (i
= 0; i
< xp
->xfrm_nr
; i
++) {
1167 struct xfrm_user_tmpl
*up
= &vec
[i
];
1168 struct xfrm_tmpl
*kp
= &xp
->xfrm_vec
[i
];
1170 memcpy(&up
->id
, &kp
->id
, sizeof(up
->id
));
1171 up
->family
= kp
->encap_family
;
1172 memcpy(&up
->saddr
, &kp
->saddr
, sizeof(up
->saddr
));
1173 up
->reqid
= kp
->reqid
;
1174 up
->mode
= kp
->mode
;
1175 up
->share
= kp
->share
;
1176 up
->optional
= kp
->optional
;
1177 up
->aalgos
= kp
->aalgos
;
1178 up
->ealgos
= kp
->ealgos
;
1179 up
->calgos
= kp
->calgos
;
1182 return nla_put(skb
, XFRMA_TMPL
,
1183 sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
, vec
);
1186 static inline int copy_to_user_state_sec_ctx(struct xfrm_state
*x
, struct sk_buff
*skb
)
1189 return copy_sec_ctx(x
->security
, skb
);
1194 static inline int copy_to_user_sec_ctx(struct xfrm_policy
*xp
, struct sk_buff
*skb
)
1197 return copy_sec_ctx(xp
->security
, skb
);
1201 static inline size_t userpolicy_type_attrsize(void)
1203 #ifdef CONFIG_XFRM_SUB_POLICY
1204 return nla_total_size(sizeof(struct xfrm_userpolicy_type
));
1210 #ifdef CONFIG_XFRM_SUB_POLICY
1211 static int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1213 struct xfrm_userpolicy_type upt
= {
1217 return nla_put(skb
, XFRMA_POLICY_TYPE
, sizeof(upt
), &upt
);
1221 static inline int copy_to_user_policy_type(u8 type
, struct sk_buff
*skb
)
1227 static int dump_one_policy(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1229 struct xfrm_dump_info
*sp
= ptr
;
1230 struct xfrm_userpolicy_info
*p
;
1231 struct sk_buff
*in_skb
= sp
->in_skb
;
1232 struct sk_buff
*skb
= sp
->out_skb
;
1233 struct nlmsghdr
*nlh
;
1235 nlh
= nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, sp
->nlmsg_seq
,
1236 XFRM_MSG_NEWPOLICY
, sizeof(*p
), sp
->nlmsg_flags
);
1240 p
= nlmsg_data(nlh
);
1241 copy_to_user_policy(xp
, p
, dir
);
1242 if (copy_to_user_tmpl(xp
, skb
) < 0)
1244 if (copy_to_user_sec_ctx(xp
, skb
))
1246 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
1249 nlmsg_end(skb
, nlh
);
1253 nlmsg_cancel(skb
, nlh
);
1257 static int xfrm_dump_policy_done(struct netlink_callback
*cb
)
1259 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1261 xfrm_policy_walk_done(walk
);
1265 static int xfrm_dump_policy(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1267 struct xfrm_policy_walk
*walk
= (struct xfrm_policy_walk
*) &cb
->args
[1];
1268 struct xfrm_dump_info info
;
1270 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk
) >
1271 sizeof(cb
->args
) - sizeof(cb
->args
[0]));
1273 info
.in_skb
= cb
->skb
;
1275 info
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
1276 info
.nlmsg_flags
= NLM_F_MULTI
;
1280 xfrm_policy_walk_init(walk
, XFRM_POLICY_TYPE_ANY
);
1283 (void) xfrm_policy_walk(walk
, dump_one_policy
, &info
);
1288 static struct sk_buff
*xfrm_policy_netlink(struct sk_buff
*in_skb
,
1289 struct xfrm_policy
*xp
,
1292 struct xfrm_dump_info info
;
1293 struct sk_buff
*skb
;
1295 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1297 return ERR_PTR(-ENOMEM
);
1299 info
.in_skb
= in_skb
;
1301 info
.nlmsg_seq
= seq
;
1302 info
.nlmsg_flags
= 0;
1304 if (dump_one_policy(xp
, dir
, 0, &info
) < 0) {
1312 static int xfrm_get_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1313 struct nlattr
**attrs
)
1315 struct xfrm_policy
*xp
;
1316 struct xfrm_userpolicy_id
*p
;
1317 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1322 p
= nlmsg_data(nlh
);
1323 delete = nlh
->nlmsg_type
== XFRM_MSG_DELPOLICY
;
1325 err
= copy_from_user_policy_type(&type
, attrs
);
1329 err
= verify_policy_dir(p
->dir
);
1334 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, delete, &err
);
1336 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1337 struct xfrm_sec_ctx
*ctx
;
1339 err
= verify_sec_ctx_len(attrs
);
1345 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1347 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1351 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, ctx
,
1353 security_xfrm_policy_free(ctx
);
1359 struct sk_buff
*resp_skb
;
1361 resp_skb
= xfrm_policy_netlink(skb
, xp
, p
->dir
, nlh
->nlmsg_seq
);
1362 if (IS_ERR(resp_skb
)) {
1363 err
= PTR_ERR(resp_skb
);
1365 err
= nlmsg_unicast(xfrm_nl
, resp_skb
,
1366 NETLINK_CB(skb
).pid
);
1369 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1370 u32 sessionid
= NETLINK_CB(skb
).sessionid
;
1371 u32 sid
= NETLINK_CB(skb
).sid
;
1373 xfrm_audit_policy_delete(xp
, err
? 0 : 1, loginuid
, sessionid
,
1379 c
.data
.byid
= p
->index
;
1380 c
.event
= nlh
->nlmsg_type
;
1381 c
.seq
= nlh
->nlmsg_seq
;
1382 c
.pid
= nlh
->nlmsg_pid
;
1383 km_policy_notify(xp
, p
->dir
, &c
);
1391 static int xfrm_flush_sa(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1392 struct nlattr
**attrs
)
1395 struct xfrm_usersa_flush
*p
= nlmsg_data(nlh
);
1396 struct xfrm_audit audit_info
;
1399 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1400 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1401 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1402 err
= xfrm_state_flush(p
->proto
, &audit_info
);
1405 c
.data
.proto
= p
->proto
;
1406 c
.event
= nlh
->nlmsg_type
;
1407 c
.seq
= nlh
->nlmsg_seq
;
1408 c
.pid
= nlh
->nlmsg_pid
;
1409 km_state_notify(NULL
, &c
);
1414 static inline size_t xfrm_aevent_msgsize(void)
1416 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id
))
1417 + nla_total_size(sizeof(struct xfrm_replay_state
))
1418 + nla_total_size(sizeof(struct xfrm_lifetime_cur
))
1419 + nla_total_size(4) /* XFRM_AE_RTHR */
1420 + nla_total_size(4); /* XFRM_AE_ETHR */
1423 static int build_aevent(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1425 struct xfrm_aevent_id
*id
;
1426 struct nlmsghdr
*nlh
;
1428 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_NEWAE
, sizeof(*id
), 0);
1432 id
= nlmsg_data(nlh
);
1433 memcpy(&id
->sa_id
.daddr
, &x
->id
.daddr
,sizeof(x
->id
.daddr
));
1434 id
->sa_id
.spi
= x
->id
.spi
;
1435 id
->sa_id
.family
= x
->props
.family
;
1436 id
->sa_id
.proto
= x
->id
.proto
;
1437 memcpy(&id
->saddr
, &x
->props
.saddr
,sizeof(x
->props
.saddr
));
1438 id
->reqid
= x
->props
.reqid
;
1439 id
->flags
= c
->data
.aevent
;
1441 NLA_PUT(skb
, XFRMA_REPLAY_VAL
, sizeof(x
->replay
), &x
->replay
);
1442 NLA_PUT(skb
, XFRMA_LTIME_VAL
, sizeof(x
->curlft
), &x
->curlft
);
1444 if (id
->flags
& XFRM_AE_RTHR
)
1445 NLA_PUT_U32(skb
, XFRMA_REPLAY_THRESH
, x
->replay_maxdiff
);
1447 if (id
->flags
& XFRM_AE_ETHR
)
1448 NLA_PUT_U32(skb
, XFRMA_ETIMER_THRESH
,
1449 x
->replay_maxage
* 10 / HZ
);
1451 return nlmsg_end(skb
, nlh
);
1454 nlmsg_cancel(skb
, nlh
);
1458 static int xfrm_get_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1459 struct nlattr
**attrs
)
1461 struct xfrm_state
*x
;
1462 struct sk_buff
*r_skb
;
1465 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1466 struct xfrm_usersa_id
*id
= &p
->sa_id
;
1468 r_skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
1472 x
= xfrm_state_lookup(&id
->daddr
, id
->spi
, id
->proto
, id
->family
);
1479 * XXX: is this lock really needed - none of the other
1480 * gets lock (the concern is things getting updated
1481 * while we are still reading) - jhs
1483 spin_lock_bh(&x
->lock
);
1484 c
.data
.aevent
= p
->flags
;
1485 c
.seq
= nlh
->nlmsg_seq
;
1486 c
.pid
= nlh
->nlmsg_pid
;
1488 if (build_aevent(r_skb
, x
, &c
) < 0)
1490 err
= nlmsg_unicast(xfrm_nl
, r_skb
, NETLINK_CB(skb
).pid
);
1491 spin_unlock_bh(&x
->lock
);
1496 static int xfrm_new_ae(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1497 struct nlattr
**attrs
)
1499 struct xfrm_state
*x
;
1502 struct xfrm_aevent_id
*p
= nlmsg_data(nlh
);
1503 struct nlattr
*rp
= attrs
[XFRMA_REPLAY_VAL
];
1504 struct nlattr
*lt
= attrs
[XFRMA_LTIME_VAL
];
1509 /* pedantic mode - thou shalt sayeth replaceth */
1510 if (!(nlh
->nlmsg_flags
&NLM_F_REPLACE
))
1513 x
= xfrm_state_lookup(&p
->sa_id
.daddr
, p
->sa_id
.spi
, p
->sa_id
.proto
, p
->sa_id
.family
);
1517 if (x
->km
.state
!= XFRM_STATE_VALID
)
1520 spin_lock_bh(&x
->lock
);
1521 xfrm_update_ae_params(x
, attrs
);
1522 spin_unlock_bh(&x
->lock
);
1524 c
.event
= nlh
->nlmsg_type
;
1525 c
.seq
= nlh
->nlmsg_seq
;
1526 c
.pid
= nlh
->nlmsg_pid
;
1527 c
.data
.aevent
= XFRM_AE_CU
;
1528 km_state_notify(x
, &c
);
1535 static int xfrm_flush_policy(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1536 struct nlattr
**attrs
)
1539 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1541 struct xfrm_audit audit_info
;
1543 err
= copy_from_user_policy_type(&type
, attrs
);
1547 audit_info
.loginuid
= NETLINK_CB(skb
).loginuid
;
1548 audit_info
.sessionid
= NETLINK_CB(skb
).sessionid
;
1549 audit_info
.secid
= NETLINK_CB(skb
).sid
;
1550 err
= xfrm_policy_flush(type
, &audit_info
);
1554 c
.event
= nlh
->nlmsg_type
;
1555 c
.seq
= nlh
->nlmsg_seq
;
1556 c
.pid
= nlh
->nlmsg_pid
;
1557 km_policy_notify(NULL
, 0, &c
);
1561 static int xfrm_add_pol_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1562 struct nlattr
**attrs
)
1564 struct xfrm_policy
*xp
;
1565 struct xfrm_user_polexpire
*up
= nlmsg_data(nlh
);
1566 struct xfrm_userpolicy_info
*p
= &up
->pol
;
1567 u8 type
= XFRM_POLICY_TYPE_MAIN
;
1570 err
= copy_from_user_policy_type(&type
, attrs
);
1575 xp
= xfrm_policy_byid(type
, p
->dir
, p
->index
, 0, &err
);
1577 struct nlattr
*rt
= attrs
[XFRMA_SEC_CTX
];
1578 struct xfrm_sec_ctx
*ctx
;
1580 err
= verify_sec_ctx_len(attrs
);
1586 struct xfrm_user_sec_ctx
*uctx
= nla_data(rt
);
1588 err
= security_xfrm_policy_alloc(&ctx
, uctx
);
1592 xp
= xfrm_policy_bysel_ctx(type
, p
->dir
, &p
->sel
, ctx
, 0, &err
);
1593 security_xfrm_policy_free(ctx
);
1598 read_lock(&xp
->lock
);
1600 read_unlock(&xp
->lock
);
1604 read_unlock(&xp
->lock
);
1607 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1608 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1609 u32 sid
= NETLINK_CB(skb
).sid
;
1610 xfrm_policy_delete(xp
, p
->dir
);
1611 xfrm_audit_policy_delete(xp
, 1, loginuid
, sessionid
, sid
);
1614 // reset the timers here?
1615 printk("Dont know what to do with soft policy expire\n");
1617 km_policy_expired(xp
, p
->dir
, up
->hard
, current
->pid
);
1624 static int xfrm_add_sa_expire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1625 struct nlattr
**attrs
)
1627 struct xfrm_state
*x
;
1629 struct xfrm_user_expire
*ue
= nlmsg_data(nlh
);
1630 struct xfrm_usersa_info
*p
= &ue
->state
;
1632 x
= xfrm_state_lookup(&p
->id
.daddr
, p
->id
.spi
, p
->id
.proto
, p
->family
);
1638 spin_lock_bh(&x
->lock
);
1640 if (x
->km
.state
!= XFRM_STATE_VALID
)
1642 km_state_expired(x
, ue
->hard
, current
->pid
);
1645 uid_t loginuid
= NETLINK_CB(skb
).loginuid
;
1646 uid_t sessionid
= NETLINK_CB(skb
).sessionid
;
1647 u32 sid
= NETLINK_CB(skb
).sid
;
1648 __xfrm_state_delete(x
);
1649 xfrm_audit_state_delete(x
, 1, loginuid
, sessionid
, sid
);
1653 spin_unlock_bh(&x
->lock
);
1658 static int xfrm_add_acquire(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1659 struct nlattr
**attrs
)
1661 struct xfrm_policy
*xp
;
1662 struct xfrm_user_tmpl
*ut
;
1664 struct nlattr
*rt
= attrs
[XFRMA_TMPL
];
1666 struct xfrm_user_acquire
*ua
= nlmsg_data(nlh
);
1667 struct xfrm_state
*x
= xfrm_state_alloc();
1673 err
= verify_newpolicy_info(&ua
->policy
);
1675 printk("BAD policy passed\n");
1681 xp
= xfrm_policy_construct(&ua
->policy
, attrs
, &err
);
1687 memcpy(&x
->id
, &ua
->id
, sizeof(ua
->id
));
1688 memcpy(&x
->props
.saddr
, &ua
->saddr
, sizeof(ua
->saddr
));
1689 memcpy(&x
->sel
, &ua
->sel
, sizeof(ua
->sel
));
1692 /* extract the templates and for each call km_key */
1693 for (i
= 0; i
< xp
->xfrm_nr
; i
++, ut
++) {
1694 struct xfrm_tmpl
*t
= &xp
->xfrm_vec
[i
];
1695 memcpy(&x
->id
, &t
->id
, sizeof(x
->id
));
1696 x
->props
.mode
= t
->mode
;
1697 x
->props
.reqid
= t
->reqid
;
1698 x
->props
.family
= ut
->family
;
1699 t
->aalgos
= ua
->aalgos
;
1700 t
->ealgos
= ua
->ealgos
;
1701 t
->calgos
= ua
->calgos
;
1702 err
= km_query(x
, t
, xp
);
1712 #ifdef CONFIG_XFRM_MIGRATE
1713 static int copy_from_user_migrate(struct xfrm_migrate
*ma
,
1714 struct nlattr
**attrs
, int *num
)
1716 struct nlattr
*rt
= attrs
[XFRMA_MIGRATE
];
1717 struct xfrm_user_migrate
*um
;
1721 num_migrate
= nla_len(rt
) / sizeof(*um
);
1723 if (num_migrate
<= 0 || num_migrate
> XFRM_MAX_DEPTH
)
1726 for (i
= 0; i
< num_migrate
; i
++, um
++, ma
++) {
1727 memcpy(&ma
->old_daddr
, &um
->old_daddr
, sizeof(ma
->old_daddr
));
1728 memcpy(&ma
->old_saddr
, &um
->old_saddr
, sizeof(ma
->old_saddr
));
1729 memcpy(&ma
->new_daddr
, &um
->new_daddr
, sizeof(ma
->new_daddr
));
1730 memcpy(&ma
->new_saddr
, &um
->new_saddr
, sizeof(ma
->new_saddr
));
1732 ma
->proto
= um
->proto
;
1733 ma
->mode
= um
->mode
;
1734 ma
->reqid
= um
->reqid
;
1736 ma
->old_family
= um
->old_family
;
1737 ma
->new_family
= um
->new_family
;
1744 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1745 struct nlattr
**attrs
)
1747 struct xfrm_userpolicy_id
*pi
= nlmsg_data(nlh
);
1748 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
1753 if (attrs
[XFRMA_MIGRATE
] == NULL
)
1756 err
= copy_from_user_policy_type(&type
, attrs
);
1760 err
= copy_from_user_migrate((struct xfrm_migrate
*)m
,
1768 xfrm_migrate(&pi
->sel
, pi
->dir
, type
, m
, n
);
1773 static int xfrm_do_migrate(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1774 struct nlattr
**attrs
)
1776 return -ENOPROTOOPT
;
1780 #ifdef CONFIG_XFRM_MIGRATE
1781 static int copy_to_user_migrate(struct xfrm_migrate
*m
, struct sk_buff
*skb
)
1783 struct xfrm_user_migrate um
;
1785 memset(&um
, 0, sizeof(um
));
1786 um
.proto
= m
->proto
;
1788 um
.reqid
= m
->reqid
;
1789 um
.old_family
= m
->old_family
;
1790 memcpy(&um
.old_daddr
, &m
->old_daddr
, sizeof(um
.old_daddr
));
1791 memcpy(&um
.old_saddr
, &m
->old_saddr
, sizeof(um
.old_saddr
));
1792 um
.new_family
= m
->new_family
;
1793 memcpy(&um
.new_daddr
, &m
->new_daddr
, sizeof(um
.new_daddr
));
1794 memcpy(&um
.new_saddr
, &m
->new_saddr
, sizeof(um
.new_saddr
));
1796 return nla_put(skb
, XFRMA_MIGRATE
, sizeof(um
), &um
);
1799 static inline size_t xfrm_migrate_msgsize(int num_migrate
)
1801 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id
))
1802 + nla_total_size(sizeof(struct xfrm_user_migrate
) * num_migrate
)
1803 + userpolicy_type_attrsize();
1806 static int build_migrate(struct sk_buff
*skb
, struct xfrm_migrate
*m
,
1807 int num_migrate
, struct xfrm_selector
*sel
,
1810 struct xfrm_migrate
*mp
;
1811 struct xfrm_userpolicy_id
*pol_id
;
1812 struct nlmsghdr
*nlh
;
1815 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_MIGRATE
, sizeof(*pol_id
), 0);
1819 pol_id
= nlmsg_data(nlh
);
1820 /* copy data from selector, dir, and type to the pol_id */
1821 memset(pol_id
, 0, sizeof(*pol_id
));
1822 memcpy(&pol_id
->sel
, sel
, sizeof(pol_id
->sel
));
1825 if (copy_to_user_policy_type(type
, skb
) < 0)
1828 for (i
= 0, mp
= m
; i
< num_migrate
; i
++, mp
++) {
1829 if (copy_to_user_migrate(mp
, skb
) < 0)
1833 return nlmsg_end(skb
, nlh
);
1835 nlmsg_cancel(skb
, nlh
);
1839 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1840 struct xfrm_migrate
*m
, int num_migrate
)
1842 struct sk_buff
*skb
;
1844 skb
= nlmsg_new(xfrm_migrate_msgsize(num_migrate
), GFP_ATOMIC
);
1849 if (build_migrate(skb
, m
, num_migrate
, sel
, dir
, type
) < 0)
1852 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_MIGRATE
, GFP_ATOMIC
);
1855 static int xfrm_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
1856 struct xfrm_migrate
*m
, int num_migrate
)
1858 return -ENOPROTOOPT
;
1862 #define XMSGSIZE(type) sizeof(struct type)
1864 static const int xfrm_msg_min
[XFRM_NR_MSGTYPES
] = {
1865 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1866 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1867 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_id
),
1868 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1869 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1870 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1871 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userspi_info
),
1872 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_acquire
),
1873 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_expire
),
1874 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_info
),
1875 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_info
),
1876 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_polexpire
),
1877 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_usersa_flush
),
1878 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = 0,
1879 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1880 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_aevent_id
),
1881 [XFRM_MSG_REPORT
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_user_report
),
1882 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = XMSGSIZE(xfrm_userpolicy_id
),
1883 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1884 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = sizeof(u32
),
1889 static const struct nla_policy xfrma_policy
[XFRMA_MAX
+1] = {
1890 [XFRMA_ALG_AEAD
] = { .len
= sizeof(struct xfrm_algo_aead
) },
1891 [XFRMA_ALG_AUTH
] = { .len
= sizeof(struct xfrm_algo
) },
1892 [XFRMA_ALG_CRYPT
] = { .len
= sizeof(struct xfrm_algo
) },
1893 [XFRMA_ALG_COMP
] = { .len
= sizeof(struct xfrm_algo
) },
1894 [XFRMA_ENCAP
] = { .len
= sizeof(struct xfrm_encap_tmpl
) },
1895 [XFRMA_TMPL
] = { .len
= sizeof(struct xfrm_user_tmpl
) },
1896 [XFRMA_SEC_CTX
] = { .len
= sizeof(struct xfrm_sec_ctx
) },
1897 [XFRMA_LTIME_VAL
] = { .len
= sizeof(struct xfrm_lifetime_cur
) },
1898 [XFRMA_REPLAY_VAL
] = { .len
= sizeof(struct xfrm_replay_state
) },
1899 [XFRMA_REPLAY_THRESH
] = { .type
= NLA_U32
},
1900 [XFRMA_ETIMER_THRESH
] = { .type
= NLA_U32
},
1901 [XFRMA_SRCADDR
] = { .len
= sizeof(xfrm_address_t
) },
1902 [XFRMA_COADDR
] = { .len
= sizeof(xfrm_address_t
) },
1903 [XFRMA_POLICY_TYPE
] = { .len
= sizeof(struct xfrm_userpolicy_type
)},
1904 [XFRMA_MIGRATE
] = { .len
= sizeof(struct xfrm_user_migrate
) },
1907 static struct xfrm_link
{
1908 int (*doit
)(struct sk_buff
*, struct nlmsghdr
*, struct nlattr
**);
1909 int (*dump
)(struct sk_buff
*, struct netlink_callback
*);
1910 int (*done
)(struct netlink_callback
*);
1911 } xfrm_dispatch
[XFRM_NR_MSGTYPES
] = {
1912 [XFRM_MSG_NEWSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1913 [XFRM_MSG_DELSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_del_sa
},
1914 [XFRM_MSG_GETSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sa
,
1915 .dump
= xfrm_dump_sa
,
1916 .done
= xfrm_dump_sa_done
},
1917 [XFRM_MSG_NEWPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1918 [XFRM_MSG_DELPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
},
1919 [XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_policy
,
1920 .dump
= xfrm_dump_policy
,
1921 .done
= xfrm_dump_policy_done
},
1922 [XFRM_MSG_ALLOCSPI
- XFRM_MSG_BASE
] = { .doit
= xfrm_alloc_userspi
},
1923 [XFRM_MSG_ACQUIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_acquire
},
1924 [XFRM_MSG_EXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa_expire
},
1925 [XFRM_MSG_UPDPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_policy
},
1926 [XFRM_MSG_UPDSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_sa
},
1927 [XFRM_MSG_POLEXPIRE
- XFRM_MSG_BASE
] = { .doit
= xfrm_add_pol_expire
},
1928 [XFRM_MSG_FLUSHSA
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_sa
},
1929 [XFRM_MSG_FLUSHPOLICY
- XFRM_MSG_BASE
] = { .doit
= xfrm_flush_policy
},
1930 [XFRM_MSG_NEWAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_new_ae
},
1931 [XFRM_MSG_GETAE
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_ae
},
1932 [XFRM_MSG_MIGRATE
- XFRM_MSG_BASE
] = { .doit
= xfrm_do_migrate
},
1933 [XFRM_MSG_GETSADINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_sadinfo
},
1934 [XFRM_MSG_GETSPDINFO
- XFRM_MSG_BASE
] = { .doit
= xfrm_get_spdinfo
},
1937 static int xfrm_user_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1939 struct nlattr
*attrs
[XFRMA_MAX
+1];
1940 struct xfrm_link
*link
;
1943 type
= nlh
->nlmsg_type
;
1944 if (type
> XFRM_MSG_MAX
)
1947 type
-= XFRM_MSG_BASE
;
1948 link
= &xfrm_dispatch
[type
];
1950 /* All operations require privileges, even GET */
1951 if (security_netlink_recv(skb
, CAP_NET_ADMIN
))
1954 if ((type
== (XFRM_MSG_GETSA
- XFRM_MSG_BASE
) ||
1955 type
== (XFRM_MSG_GETPOLICY
- XFRM_MSG_BASE
)) &&
1956 (nlh
->nlmsg_flags
& NLM_F_DUMP
)) {
1957 if (link
->dump
== NULL
)
1960 return netlink_dump_start(xfrm_nl
, skb
, nlh
, link
->dump
, link
->done
);
1963 err
= nlmsg_parse(nlh
, xfrm_msg_min
[type
], attrs
, XFRMA_MAX
,
1968 if (link
->doit
== NULL
)
1971 return link
->doit(skb
, nlh
, attrs
);
1974 static void xfrm_netlink_rcv(struct sk_buff
*skb
)
1976 mutex_lock(&xfrm_cfg_mutex
);
1977 netlink_rcv_skb(skb
, &xfrm_user_rcv_msg
);
1978 mutex_unlock(&xfrm_cfg_mutex
);
1981 static inline size_t xfrm_expire_msgsize(void)
1983 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire
));
1986 static int build_expire(struct sk_buff
*skb
, struct xfrm_state
*x
, struct km_event
*c
)
1988 struct xfrm_user_expire
*ue
;
1989 struct nlmsghdr
*nlh
;
1991 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_EXPIRE
, sizeof(*ue
), 0);
1995 ue
= nlmsg_data(nlh
);
1996 copy_to_user_state(x
, &ue
->state
);
1997 ue
->hard
= (c
->data
.hard
!= 0) ? 1 : 0;
1999 return nlmsg_end(skb
, nlh
);
2002 static int xfrm_exp_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2004 struct sk_buff
*skb
;
2006 skb
= nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC
);
2010 if (build_expire(skb
, x
, c
) < 0)
2013 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2016 static int xfrm_aevent_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2018 struct sk_buff
*skb
;
2020 skb
= nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC
);
2024 if (build_aevent(skb
, x
, c
) < 0)
2027 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_AEVENTS
, GFP_ATOMIC
);
2030 static int xfrm_notify_sa_flush(struct km_event
*c
)
2032 struct xfrm_usersa_flush
*p
;
2033 struct nlmsghdr
*nlh
;
2034 struct sk_buff
*skb
;
2035 int len
= NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush
));
2037 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2041 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHSA
, sizeof(*p
), 0);
2047 p
= nlmsg_data(nlh
);
2048 p
->proto
= c
->data
.proto
;
2050 nlmsg_end(skb
, nlh
);
2052 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2055 static inline size_t xfrm_sa_len(struct xfrm_state
*x
)
2059 l
+= nla_total_size(aead_len(x
->aead
));
2061 l
+= nla_total_size(xfrm_alg_len(x
->aalg
));
2063 l
+= nla_total_size(xfrm_alg_len(x
->ealg
));
2065 l
+= nla_total_size(sizeof(*x
->calg
));
2067 l
+= nla_total_size(sizeof(*x
->encap
));
2069 l
+= nla_total_size(sizeof(struct xfrm_user_sec_ctx
) +
2070 x
->security
->ctx_len
);
2072 l
+= nla_total_size(sizeof(*x
->coaddr
));
2074 /* Must count x->lastused as it may become non-zero behind our back. */
2075 l
+= nla_total_size(sizeof(u64
));
2080 static int xfrm_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
2082 struct xfrm_usersa_info
*p
;
2083 struct xfrm_usersa_id
*id
;
2084 struct nlmsghdr
*nlh
;
2085 struct sk_buff
*skb
;
2086 int len
= xfrm_sa_len(x
);
2089 headlen
= sizeof(*p
);
2090 if (c
->event
== XFRM_MSG_DELSA
) {
2091 len
+= nla_total_size(headlen
);
2092 headlen
= sizeof(*id
);
2094 len
+= NLMSG_ALIGN(headlen
);
2096 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2100 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2102 goto nla_put_failure
;
2104 p
= nlmsg_data(nlh
);
2105 if (c
->event
== XFRM_MSG_DELSA
) {
2106 struct nlattr
*attr
;
2108 id
= nlmsg_data(nlh
);
2109 memcpy(&id
->daddr
, &x
->id
.daddr
, sizeof(id
->daddr
));
2110 id
->spi
= x
->id
.spi
;
2111 id
->family
= x
->props
.family
;
2112 id
->proto
= x
->id
.proto
;
2114 attr
= nla_reserve(skb
, XFRMA_SA
, sizeof(*p
));
2116 goto nla_put_failure
;
2121 if (copy_to_user_state_extra(x
, p
, skb
))
2122 goto nla_put_failure
;
2124 nlmsg_end(skb
, nlh
);
2126 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_SA
, GFP_ATOMIC
);
2129 /* Somebody screwed up with xfrm_sa_len! */
2135 static int xfrm_send_state_notify(struct xfrm_state
*x
, struct km_event
*c
)
2139 case XFRM_MSG_EXPIRE
:
2140 return xfrm_exp_state_notify(x
, c
);
2141 case XFRM_MSG_NEWAE
:
2142 return xfrm_aevent_state_notify(x
, c
);
2143 case XFRM_MSG_DELSA
:
2144 case XFRM_MSG_UPDSA
:
2145 case XFRM_MSG_NEWSA
:
2146 return xfrm_notify_sa(x
, c
);
2147 case XFRM_MSG_FLUSHSA
:
2148 return xfrm_notify_sa_flush(c
);
2150 printk("xfrm_user: Unknown SA event %d\n", c
->event
);
2158 static inline size_t xfrm_acquire_msgsize(struct xfrm_state
*x
,
2159 struct xfrm_policy
*xp
)
2161 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire
))
2162 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2163 + nla_total_size(xfrm_user_sec_ctx_size(x
->security
))
2164 + userpolicy_type_attrsize();
2167 static int build_acquire(struct sk_buff
*skb
, struct xfrm_state
*x
,
2168 struct xfrm_tmpl
*xt
, struct xfrm_policy
*xp
,
2171 struct xfrm_user_acquire
*ua
;
2172 struct nlmsghdr
*nlh
;
2173 __u32 seq
= xfrm_get_acqseq();
2175 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_ACQUIRE
, sizeof(*ua
), 0);
2179 ua
= nlmsg_data(nlh
);
2180 memcpy(&ua
->id
, &x
->id
, sizeof(ua
->id
));
2181 memcpy(&ua
->saddr
, &x
->props
.saddr
, sizeof(ua
->saddr
));
2182 memcpy(&ua
->sel
, &x
->sel
, sizeof(ua
->sel
));
2183 copy_to_user_policy(xp
, &ua
->policy
, dir
);
2184 ua
->aalgos
= xt
->aalgos
;
2185 ua
->ealgos
= xt
->ealgos
;
2186 ua
->calgos
= xt
->calgos
;
2187 ua
->seq
= x
->km
.seq
= seq
;
2189 if (copy_to_user_tmpl(xp
, skb
) < 0)
2191 if (copy_to_user_state_sec_ctx(x
, skb
))
2193 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2196 return nlmsg_end(skb
, nlh
);
2199 nlmsg_cancel(skb
, nlh
);
2203 static int xfrm_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*xt
,
2204 struct xfrm_policy
*xp
, int dir
)
2206 struct sk_buff
*skb
;
2208 skb
= nlmsg_new(xfrm_acquire_msgsize(x
, xp
), GFP_ATOMIC
);
2212 if (build_acquire(skb
, x
, xt
, xp
, dir
) < 0)
2215 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_ACQUIRE
, GFP_ATOMIC
);
2218 /* User gives us xfrm_user_policy_info followed by an array of 0
2219 * or more templates.
2221 static struct xfrm_policy
*xfrm_compile_policy(struct sock
*sk
, int opt
,
2222 u8
*data
, int len
, int *dir
)
2224 struct xfrm_userpolicy_info
*p
= (struct xfrm_userpolicy_info
*)data
;
2225 struct xfrm_user_tmpl
*ut
= (struct xfrm_user_tmpl
*) (p
+ 1);
2226 struct xfrm_policy
*xp
;
2229 switch (sk
->sk_family
) {
2231 if (opt
!= IP_XFRM_POLICY
) {
2236 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2238 if (opt
!= IPV6_XFRM_POLICY
) {
2251 if (len
< sizeof(*p
) ||
2252 verify_newpolicy_info(p
))
2255 nr
= ((len
- sizeof(*p
)) / sizeof(*ut
));
2256 if (validate_tmpl(nr
, ut
, p
->sel
.family
))
2259 if (p
->dir
> XFRM_POLICY_OUT
)
2262 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2268 copy_from_user_policy(xp
, p
);
2269 xp
->type
= XFRM_POLICY_TYPE_MAIN
;
2270 copy_templates(xp
, ut
, nr
);
2277 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy
*xp
)
2279 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire
))
2280 + nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
)
2281 + nla_total_size(xfrm_user_sec_ctx_size(xp
->security
))
2282 + userpolicy_type_attrsize();
2285 static int build_polexpire(struct sk_buff
*skb
, struct xfrm_policy
*xp
,
2286 int dir
, struct km_event
*c
)
2288 struct xfrm_user_polexpire
*upe
;
2289 struct nlmsghdr
*nlh
;
2290 int hard
= c
->data
.hard
;
2292 nlh
= nlmsg_put(skb
, c
->pid
, 0, XFRM_MSG_POLEXPIRE
, sizeof(*upe
), 0);
2296 upe
= nlmsg_data(nlh
);
2297 copy_to_user_policy(xp
, &upe
->pol
, dir
);
2298 if (copy_to_user_tmpl(xp
, skb
) < 0)
2300 if (copy_to_user_sec_ctx(xp
, skb
))
2302 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2306 return nlmsg_end(skb
, nlh
);
2309 nlmsg_cancel(skb
, nlh
);
2313 static int xfrm_exp_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2315 struct sk_buff
*skb
;
2317 skb
= nlmsg_new(xfrm_polexpire_msgsize(xp
), GFP_ATOMIC
);
2321 if (build_polexpire(skb
, xp
, dir
, c
) < 0)
2324 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_EXPIRE
, GFP_ATOMIC
);
2327 static int xfrm_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2329 struct xfrm_userpolicy_info
*p
;
2330 struct xfrm_userpolicy_id
*id
;
2331 struct nlmsghdr
*nlh
;
2332 struct sk_buff
*skb
;
2333 int len
= nla_total_size(sizeof(struct xfrm_user_tmpl
) * xp
->xfrm_nr
);
2336 headlen
= sizeof(*p
);
2337 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2338 len
+= nla_total_size(headlen
);
2339 headlen
= sizeof(*id
);
2341 len
+= userpolicy_type_attrsize();
2342 len
+= NLMSG_ALIGN(headlen
);
2344 skb
= nlmsg_new(len
, GFP_ATOMIC
);
2348 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, c
->event
, headlen
, 0);
2352 p
= nlmsg_data(nlh
);
2353 if (c
->event
== XFRM_MSG_DELPOLICY
) {
2354 struct nlattr
*attr
;
2356 id
= nlmsg_data(nlh
);
2357 memset(id
, 0, sizeof(*id
));
2360 id
->index
= xp
->index
;
2362 memcpy(&id
->sel
, &xp
->selector
, sizeof(id
->sel
));
2364 attr
= nla_reserve(skb
, XFRMA_POLICY
, sizeof(*p
));
2371 copy_to_user_policy(xp
, p
, dir
);
2372 if (copy_to_user_tmpl(xp
, skb
) < 0)
2374 if (copy_to_user_policy_type(xp
->type
, skb
) < 0)
2377 nlmsg_end(skb
, nlh
);
2379 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2386 static int xfrm_notify_policy_flush(struct km_event
*c
)
2388 struct nlmsghdr
*nlh
;
2389 struct sk_buff
*skb
;
2391 skb
= nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC
);
2395 nlh
= nlmsg_put(skb
, c
->pid
, c
->seq
, XFRM_MSG_FLUSHPOLICY
, 0, 0);
2398 if (copy_to_user_policy_type(c
->data
.type
, skb
) < 0)
2401 nlmsg_end(skb
, nlh
);
2403 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_POLICY
, GFP_ATOMIC
);
2410 static int xfrm_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2414 case XFRM_MSG_NEWPOLICY
:
2415 case XFRM_MSG_UPDPOLICY
:
2416 case XFRM_MSG_DELPOLICY
:
2417 return xfrm_notify_policy(xp
, dir
, c
);
2418 case XFRM_MSG_FLUSHPOLICY
:
2419 return xfrm_notify_policy_flush(c
);
2420 case XFRM_MSG_POLEXPIRE
:
2421 return xfrm_exp_policy_notify(xp
, dir
, c
);
2423 printk("xfrm_user: Unknown Policy event %d\n", c
->event
);
2430 static inline size_t xfrm_report_msgsize(void)
2432 return NLMSG_ALIGN(sizeof(struct xfrm_user_report
));
2435 static int build_report(struct sk_buff
*skb
, u8 proto
,
2436 struct xfrm_selector
*sel
, xfrm_address_t
*addr
)
2438 struct xfrm_user_report
*ur
;
2439 struct nlmsghdr
*nlh
;
2441 nlh
= nlmsg_put(skb
, 0, 0, XFRM_MSG_REPORT
, sizeof(*ur
), 0);
2445 ur
= nlmsg_data(nlh
);
2447 memcpy(&ur
->sel
, sel
, sizeof(ur
->sel
));
2450 NLA_PUT(skb
, XFRMA_COADDR
, sizeof(*addr
), addr
);
2452 return nlmsg_end(skb
, nlh
);
2455 nlmsg_cancel(skb
, nlh
);
2459 static int xfrm_send_report(u8 proto
, struct xfrm_selector
*sel
,
2460 xfrm_address_t
*addr
)
2462 struct sk_buff
*skb
;
2464 skb
= nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC
);
2468 if (build_report(skb
, proto
, sel
, addr
) < 0)
2471 return nlmsg_multicast(xfrm_nl
, skb
, 0, XFRMNLGRP_REPORT
, GFP_ATOMIC
);
2474 static struct xfrm_mgr netlink_mgr
= {
2476 .notify
= xfrm_send_state_notify
,
2477 .acquire
= xfrm_send_acquire
,
2478 .compile_policy
= xfrm_compile_policy
,
2479 .notify_policy
= xfrm_send_policy_notify
,
2480 .report
= xfrm_send_report
,
2481 .migrate
= xfrm_send_migrate
,
2484 static int __init
xfrm_user_init(void)
2488 printk(KERN_INFO
"Initializing XFRM netlink socket\n");
2490 nlsk
= netlink_kernel_create(&init_net
, NETLINK_XFRM
, XFRMNLGRP_MAX
,
2491 xfrm_netlink_rcv
, NULL
, THIS_MODULE
);
2494 rcu_assign_pointer(xfrm_nl
, nlsk
);
2496 xfrm_register_km(&netlink_mgr
);
2501 static void __exit
xfrm_user_exit(void)
2503 struct sock
*nlsk
= xfrm_nl
;
2505 xfrm_unregister_km(&netlink_mgr
);
2506 rcu_assign_pointer(xfrm_nl
, NULL
);
2508 netlink_kernel_release(nlsk
);
2511 module_init(xfrm_user_init
);
2512 module_exit(xfrm_user_exit
);
2513 MODULE_LICENSE("GPL");
2514 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK
, NETLINK_XFRM
);