2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
33 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
34 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
37 /* List of all pfkey sockets. */
38 static HLIST_HEAD(pfkey_table);
39 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
40 static DEFINE_RWLOCK(pfkey_table_lock);
41 static atomic_t pfkey_table_users = ATOMIC_INIT(0);
43 static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
46 /* struct sock must be the first member of struct pfkey_sock */
52 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
54 return (struct pfkey_sock *)sk;
57 static void pfkey_sock_destruct(struct sock *sk)
59 skb_queue_purge(&sk->sk_receive_queue);
61 if (!sock_flag(sk, SOCK_DEAD)) {
62 printk("Attempt to release alive pfkey socket: %p\n", sk);
66 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
67 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
69 atomic_dec(&pfkey_socks_nr);
72 static void pfkey_table_grab(void)
74 write_lock_bh(&pfkey_table_lock);
76 if (atomic_read(&pfkey_table_users)) {
77 DECLARE_WAITQUEUE(wait, current);
79 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
81 set_current_state(TASK_UNINTERRUPTIBLE);
82 if (atomic_read(&pfkey_table_users) == 0)
84 write_unlock_bh(&pfkey_table_lock);
86 write_lock_bh(&pfkey_table_lock);
89 __set_current_state(TASK_RUNNING);
90 remove_wait_queue(&pfkey_table_wait, &wait);
94 static __inline__ void pfkey_table_ungrab(void)
96 write_unlock_bh(&pfkey_table_lock);
97 wake_up(&pfkey_table_wait);
100 static __inline__ void pfkey_lock_table(void)
102 /* read_lock() synchronizes us to pfkey_table_grab */
104 read_lock(&pfkey_table_lock);
105 atomic_inc(&pfkey_table_users);
106 read_unlock(&pfkey_table_lock);
109 static __inline__ void pfkey_unlock_table(void)
111 if (atomic_dec_and_test(&pfkey_table_users))
112 wake_up(&pfkey_table_wait);
116 static const struct proto_ops pfkey_ops;
118 static void pfkey_insert(struct sock *sk)
121 sk_add_node(sk, &pfkey_table);
122 pfkey_table_ungrab();
125 static void pfkey_remove(struct sock *sk)
128 sk_del_node_init(sk);
129 pfkey_table_ungrab();
132 static struct proto key_proto = {
134 .owner = THIS_MODULE,
135 .obj_size = sizeof(struct pfkey_sock),
138 static int pfkey_create(struct socket *sock, int protocol)
143 if (!capable(CAP_NET_ADMIN))
145 if (sock->type != SOCK_RAW)
146 return -ESOCKTNOSUPPORT;
147 if (protocol != PF_KEY_V2)
148 return -EPROTONOSUPPORT;
151 sk = sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
155 sock->ops = &pfkey_ops;
156 sock_init_data(sock, sk);
158 sk->sk_family = PF_KEY;
159 sk->sk_destruct = pfkey_sock_destruct;
161 atomic_inc(&pfkey_socks_nr);
170 static int pfkey_release(struct socket *sock)
172 struct sock *sk = sock->sk;
181 skb_queue_purge(&sk->sk_write_queue);
187 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
188 gfp_t allocation, struct sock *sk)
194 if (atomic_read(&skb->users) != 1) {
195 *skb2 = skb_clone(skb, allocation);
198 atomic_inc(&skb->users);
202 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
204 skb_set_owner_r(*skb2, sk);
205 skb_queue_tail(&sk->sk_receive_queue, *skb2);
206 sk->sk_data_ready(sk, (*skb2)->len);
215 /* Send SKB to all pfkey sockets matching selected criteria. */
216 #define BROADCAST_ALL 0
217 #define BROADCAST_ONE 1
218 #define BROADCAST_REGISTERED 2
219 #define BROADCAST_PROMISC_ONLY 4
220 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
221 int broadcast_flags, struct sock *one_sk)
224 struct hlist_node *node;
225 struct sk_buff *skb2 = NULL;
228 /* XXX Do we need something like netlink_overrun? I think
229 * XXX PF_KEY socket apps will not mind current behavior.
235 sk_for_each(sk, node, &pfkey_table) {
236 struct pfkey_sock *pfk = pfkey_sk(sk);
239 /* Yes, it means that if you are meant to receive this
240 * pfkey message you receive it twice as promiscuous
244 pfkey_broadcast_one(skb, &skb2, allocation, sk);
246 /* the exact target will be processed later */
249 if (broadcast_flags != BROADCAST_ALL) {
250 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
252 if ((broadcast_flags & BROADCAST_REGISTERED) &&
255 if (broadcast_flags & BROADCAST_ONE)
259 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
261 /* Error is cleare after succecful sending to at least one
263 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
266 pfkey_unlock_table();
269 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
277 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
282 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
284 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
285 struct sadb_msg *hdr;
290 /* Woe be to the platform trying to support PFKEY yet
291 * having normal errnos outside the 1-255 range, inclusive.
294 if (err == ERESTARTSYS ||
295 err == ERESTARTNOHAND ||
296 err == ERESTARTNOINTR)
300 if (err <= 0 || err >= 256)
303 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
304 pfkey_hdr_dup(hdr, orig);
305 hdr->sadb_msg_errno = (uint8_t) err;
306 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
309 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
314 static u8 sadb_ext_min_len[] = {
315 [SADB_EXT_RESERVED] = (u8) 0,
316 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
317 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
318 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
319 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
320 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
321 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
322 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
323 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
324 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
325 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
326 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
327 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
328 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
329 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
330 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
331 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
332 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
333 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
334 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
335 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
336 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
337 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
338 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
339 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
342 /* Verify sadb_address_{len,prefixlen} against sa_family. */
343 static int verify_address_len(void *p)
345 struct sadb_address *sp = p;
346 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
347 struct sockaddr_in *sin;
348 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
349 struct sockaddr_in6 *sin6;
353 switch (addr->sa_family) {
355 len = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
356 len /= sizeof(uint64_t);
357 if (sp->sadb_address_len != len ||
358 sp->sadb_address_prefixlen > 32)
361 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
363 len = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
364 len /= sizeof(uint64_t);
365 if (sp->sadb_address_len != len ||
366 sp->sadb_address_prefixlen > 128)
371 /* It is user using kernel to keep track of security
372 * associations for another protocol, such as
373 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
376 * XXX Actually, association/policy database is not yet
377 * XXX able to cope with arbitrary sockaddr families.
378 * XXX When it can, remove this -EINVAL. -DaveM
387 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
391 len += sizeof(struct sadb_x_sec_ctx);
392 len += sec_ctx->sadb_x_ctx_len;
393 len += sizeof(uint64_t) - 1;
394 len /= sizeof(uint64_t);
399 static inline int verify_sec_ctx_len(void *p)
401 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
404 if (sec_ctx->sadb_x_ctx_len > PAGE_SIZE)
407 len = pfkey_sec_ctx_len(sec_ctx);
409 if (sec_ctx->sadb_x_sec_len != len)
415 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
417 struct xfrm_user_sec_ctx *uctx = NULL;
418 int ctx_size = sec_ctx->sadb_x_ctx_len;
420 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
425 uctx->len = pfkey_sec_ctx_len(sec_ctx);
426 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
427 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
428 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
429 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
430 memcpy(uctx + 1, sec_ctx + 1,
436 static int present_and_same_family(struct sadb_address *src,
437 struct sadb_address *dst)
439 struct sockaddr *s_addr, *d_addr;
444 s_addr = (struct sockaddr *)(src + 1);
445 d_addr = (struct sockaddr *)(dst + 1);
446 if (s_addr->sa_family != d_addr->sa_family)
448 if (s_addr->sa_family != AF_INET
449 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
450 && s_addr->sa_family != AF_INET6
458 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
460 char *p = (char *) hdr;
466 struct sadb_ext *ehdr = (struct sadb_ext *) p;
470 ext_len = ehdr->sadb_ext_len;
471 ext_len *= sizeof(uint64_t);
472 ext_type = ehdr->sadb_ext_type;
473 if (ext_len < sizeof(uint64_t) ||
475 ext_type == SADB_EXT_RESERVED)
478 if (ext_type <= SADB_EXT_MAX) {
479 int min = (int) sadb_ext_min_len[ext_type];
482 if (ext_hdrs[ext_type-1] != NULL)
484 if (ext_type == SADB_EXT_ADDRESS_SRC ||
485 ext_type == SADB_EXT_ADDRESS_DST ||
486 ext_type == SADB_EXT_ADDRESS_PROXY ||
487 ext_type == SADB_X_EXT_NAT_T_OA) {
488 if (verify_address_len(p))
491 if (ext_type == SADB_X_EXT_SEC_CTX) {
492 if (verify_sec_ctx_len(p))
495 ext_hdrs[ext_type-1] = p;
505 pfkey_satype2proto(uint8_t satype)
508 case SADB_SATYPE_UNSPEC:
509 return IPSEC_PROTO_ANY;
512 case SADB_SATYPE_ESP:
514 case SADB_X_SATYPE_IPCOMP:
524 pfkey_proto2satype(uint16_t proto)
528 return SADB_SATYPE_AH;
530 return SADB_SATYPE_ESP;
532 return SADB_X_SATYPE_IPCOMP;
540 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
541 * say specifically 'just raw sockets' as we encode them as 255.
544 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
546 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
549 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
551 return (proto ? proto : IPSEC_PROTO_ANY);
554 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
555 xfrm_address_t *xaddr)
557 switch (((struct sockaddr*)(addr + 1))->sa_family) {
560 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
562 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
565 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
566 sizeof(struct in6_addr));
575 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
578 struct sadb_address *addr;
580 unsigned short family;
581 xfrm_address_t *xaddr;
583 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
587 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
591 /* sadb_address_len should be checked by caller */
592 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
596 family = ((struct sockaddr *)(addr + 1))->sa_family;
599 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
601 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
603 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
613 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
616 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
618 pfkey_sockaddr_size(sa_family_t family)
622 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
623 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
625 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
633 static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
636 struct sadb_msg *hdr;
638 struct sadb_lifetime *lifetime;
639 struct sadb_address *addr;
640 struct sadb_key *key;
641 struct sadb_x_sa2 *sa2;
642 struct sockaddr_in *sin;
643 struct sadb_x_sec_ctx *sec_ctx;
644 struct xfrm_sec_ctx *xfrm_ctx;
646 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
647 struct sockaddr_in6 *sin6;
650 int auth_key_size = 0;
651 int encrypt_key_size = 0;
653 struct xfrm_encap_tmpl *natt = NULL;
655 /* address family check */
656 sockaddr_size = pfkey_sockaddr_size(x->props.family);
658 return ERR_PTR(-EINVAL);
660 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
661 key(AE), (identity(SD),) (sensitivity)> */
662 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
663 sizeof(struct sadb_lifetime) +
664 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
665 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
666 sizeof(struct sadb_address)*2 +
668 sizeof(struct sadb_x_sa2);
670 if ((xfrm_ctx = x->security)) {
671 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
672 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
675 /* identity & sensitivity */
677 if ((x->props.family == AF_INET &&
678 x->sel.saddr.a4 != x->props.saddr.a4)
679 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
680 || (x->props.family == AF_INET6 &&
681 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
684 size += sizeof(struct sadb_address) + sockaddr_size;
687 if (x->aalg && x->aalg->alg_key_len) {
689 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
690 size += sizeof(struct sadb_key) + auth_key_size;
692 if (x->ealg && x->ealg->alg_key_len) {
694 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
695 size += sizeof(struct sadb_key) + encrypt_key_size;
701 if (natt && natt->encap_type) {
702 size += sizeof(struct sadb_x_nat_t_type);
703 size += sizeof(struct sadb_x_nat_t_port);
704 size += sizeof(struct sadb_x_nat_t_port);
707 skb = alloc_skb(size + 16, GFP_ATOMIC);
709 return ERR_PTR(-ENOBUFS);
711 /* call should fill header later */
712 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
713 memset(hdr, 0, size); /* XXX do we need this ? */
714 hdr->sadb_msg_len = size / sizeof(uint64_t);
717 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
718 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
719 sa->sadb_sa_exttype = SADB_EXT_SA;
720 sa->sadb_sa_spi = x->id.spi;
721 sa->sadb_sa_replay = x->props.replay_window;
722 switch (x->km.state) {
723 case XFRM_STATE_VALID:
724 sa->sadb_sa_state = x->km.dying ?
725 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
728 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
731 sa->sadb_sa_state = SADB_SASTATE_DEAD;
734 sa->sadb_sa_auth = 0;
736 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
737 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
739 sa->sadb_sa_encrypt = 0;
740 BUG_ON(x->ealg && x->calg);
742 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
743 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
745 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
747 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
748 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
751 sa->sadb_sa_flags = 0;
752 if (x->props.flags & XFRM_STATE_NOECN)
753 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
754 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
755 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
756 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
757 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
761 lifetime = (struct sadb_lifetime *) skb_put(skb,
762 sizeof(struct sadb_lifetime));
763 lifetime->sadb_lifetime_len =
764 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
765 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
766 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
767 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
768 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
769 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
773 lifetime = (struct sadb_lifetime *) skb_put(skb,
774 sizeof(struct sadb_lifetime));
775 lifetime->sadb_lifetime_len =
776 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
777 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
778 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
779 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
780 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
781 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
784 lifetime = (struct sadb_lifetime *) skb_put(skb,
785 sizeof(struct sadb_lifetime));
786 lifetime->sadb_lifetime_len =
787 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
788 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
789 lifetime->sadb_lifetime_allocations = x->curlft.packets;
790 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
791 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
792 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
794 addr = (struct sadb_address*) skb_put(skb,
795 sizeof(struct sadb_address)+sockaddr_size);
796 addr->sadb_address_len =
797 (sizeof(struct sadb_address)+sockaddr_size)/
799 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
800 /* "if the ports are non-zero, then the sadb_address_proto field,
801 normally zero, MUST be filled in with the transport
802 protocol's number." - RFC2367 */
803 addr->sadb_address_proto = 0;
804 addr->sadb_address_reserved = 0;
805 if (x->props.family == AF_INET) {
806 addr->sadb_address_prefixlen = 32;
808 sin = (struct sockaddr_in *) (addr + 1);
809 sin->sin_family = AF_INET;
810 sin->sin_addr.s_addr = x->props.saddr.a4;
812 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
814 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
815 else if (x->props.family == AF_INET6) {
816 addr->sadb_address_prefixlen = 128;
818 sin6 = (struct sockaddr_in6 *) (addr + 1);
819 sin6->sin6_family = AF_INET6;
821 sin6->sin6_flowinfo = 0;
822 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
823 sizeof(struct in6_addr));
824 sin6->sin6_scope_id = 0;
831 addr = (struct sadb_address*) skb_put(skb,
832 sizeof(struct sadb_address)+sockaddr_size);
833 addr->sadb_address_len =
834 (sizeof(struct sadb_address)+sockaddr_size)/
836 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
837 addr->sadb_address_proto = 0;
838 addr->sadb_address_prefixlen = 32; /* XXX */
839 addr->sadb_address_reserved = 0;
840 if (x->props.family == AF_INET) {
841 sin = (struct sockaddr_in *) (addr + 1);
842 sin->sin_family = AF_INET;
843 sin->sin_addr.s_addr = x->id.daddr.a4;
845 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
847 if (x->sel.saddr.a4 != x->props.saddr.a4) {
848 addr = (struct sadb_address*) skb_put(skb,
849 sizeof(struct sadb_address)+sockaddr_size);
850 addr->sadb_address_len =
851 (sizeof(struct sadb_address)+sockaddr_size)/
853 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
854 addr->sadb_address_proto =
855 pfkey_proto_from_xfrm(x->sel.proto);
856 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
857 addr->sadb_address_reserved = 0;
859 sin = (struct sockaddr_in *) (addr + 1);
860 sin->sin_family = AF_INET;
861 sin->sin_addr.s_addr = x->sel.saddr.a4;
862 sin->sin_port = x->sel.sport;
863 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
866 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
867 else if (x->props.family == AF_INET6) {
868 addr->sadb_address_prefixlen = 128;
870 sin6 = (struct sockaddr_in6 *) (addr + 1);
871 sin6->sin6_family = AF_INET6;
873 sin6->sin6_flowinfo = 0;
874 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
875 sin6->sin6_scope_id = 0;
877 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
878 sizeof(struct in6_addr))) {
879 addr = (struct sadb_address *) skb_put(skb,
880 sizeof(struct sadb_address)+sockaddr_size);
881 addr->sadb_address_len =
882 (sizeof(struct sadb_address)+sockaddr_size)/
884 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
885 addr->sadb_address_proto =
886 pfkey_proto_from_xfrm(x->sel.proto);
887 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
888 addr->sadb_address_reserved = 0;
890 sin6 = (struct sockaddr_in6 *) (addr + 1);
891 sin6->sin6_family = AF_INET6;
892 sin6->sin6_port = x->sel.sport;
893 sin6->sin6_flowinfo = 0;
894 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
895 sizeof(struct in6_addr));
896 sin6->sin6_scope_id = 0;
904 if (add_keys && auth_key_size) {
905 key = (struct sadb_key *) skb_put(skb,
906 sizeof(struct sadb_key)+auth_key_size);
907 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
909 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
910 key->sadb_key_bits = x->aalg->alg_key_len;
911 key->sadb_key_reserved = 0;
912 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
915 if (add_keys && encrypt_key_size) {
916 key = (struct sadb_key *) skb_put(skb,
917 sizeof(struct sadb_key)+encrypt_key_size);
918 key->sadb_key_len = (sizeof(struct sadb_key) +
919 encrypt_key_size) / sizeof(uint64_t);
920 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
921 key->sadb_key_bits = x->ealg->alg_key_len;
922 key->sadb_key_reserved = 0;
923 memcpy(key + 1, x->ealg->alg_key,
924 (x->ealg->alg_key_len+7)/8);
928 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
929 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
930 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
931 sa2->sadb_x_sa2_mode = x->props.mode + 1;
932 sa2->sadb_x_sa2_reserved1 = 0;
933 sa2->sadb_x_sa2_reserved2 = 0;
934 sa2->sadb_x_sa2_sequence = 0;
935 sa2->sadb_x_sa2_reqid = x->props.reqid;
937 if (natt && natt->encap_type) {
938 struct sadb_x_nat_t_type *n_type;
939 struct sadb_x_nat_t_port *n_port;
942 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
943 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
944 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
945 n_type->sadb_x_nat_t_type_type = natt->encap_type;
946 n_type->sadb_x_nat_t_type_reserved[0] = 0;
947 n_type->sadb_x_nat_t_type_reserved[1] = 0;
948 n_type->sadb_x_nat_t_type_reserved[2] = 0;
951 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
952 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
953 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
954 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
955 n_port->sadb_x_nat_t_port_reserved = 0;
958 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
959 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
960 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
961 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
962 n_port->sadb_x_nat_t_port_reserved = 0;
965 /* security context */
967 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
968 sizeof(struct sadb_x_sec_ctx) + ctx_size);
969 sec_ctx->sadb_x_sec_len =
970 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
971 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
972 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
973 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
974 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
975 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
982 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
985 struct xfrm_state *x;
986 struct sadb_lifetime *lifetime;
988 struct sadb_key *key;
989 struct sadb_x_sec_ctx *sec_ctx;
994 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
996 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
997 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
998 return ERR_PTR(-EINVAL);
999 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1000 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1001 return ERR_PTR(-EINVAL);
1002 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1003 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1004 return ERR_PTR(-EINVAL);
1005 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1006 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1007 return ERR_PTR(-EINVAL);
1009 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1011 return ERR_PTR(-EINVAL);
1013 /* default error is no buffer space */
1018 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1019 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1020 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1021 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1022 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1025 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1026 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1028 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1029 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1030 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1031 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1032 return ERR_PTR(-EINVAL);
1033 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1035 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1036 ((key->sadb_key_bits+7) / 8 == 0 ||
1037 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1038 return ERR_PTR(-EINVAL);
1039 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1041 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1042 ((key->sadb_key_bits+7) / 8 == 0 ||
1043 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1044 return ERR_PTR(-EINVAL);
1046 x = xfrm_state_alloc();
1048 return ERR_PTR(-ENOBUFS);
1050 x->id.proto = proto;
1051 x->id.spi = sa->sadb_sa_spi;
1052 x->props.replay_window = sa->sadb_sa_replay;
1053 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1054 x->props.flags |= XFRM_STATE_NOECN;
1055 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1056 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1057 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1058 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1060 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1061 if (lifetime != NULL) {
1062 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1063 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1064 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1065 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1067 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1068 if (lifetime != NULL) {
1069 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1070 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1071 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1072 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1075 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1076 if (sec_ctx != NULL) {
1077 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1082 err = security_xfrm_state_alloc(x, uctx);
1089 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1090 if (sa->sadb_sa_auth) {
1092 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1098 keysize = (key->sadb_key_bits + 7) / 8;
1099 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1102 strcpy(x->aalg->alg_name, a->name);
1103 x->aalg->alg_key_len = 0;
1105 x->aalg->alg_key_len = key->sadb_key_bits;
1106 memcpy(x->aalg->alg_key, key+1, keysize);
1108 x->props.aalgo = sa->sadb_sa_auth;
1109 /* x->algo.flags = sa->sadb_sa_flags; */
1111 if (sa->sadb_sa_encrypt) {
1112 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1113 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1118 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1121 strcpy(x->calg->alg_name, a->name);
1122 x->props.calgo = sa->sadb_sa_encrypt;
1125 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1130 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1132 keysize = (key->sadb_key_bits + 7) / 8;
1133 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1136 strcpy(x->ealg->alg_name, a->name);
1137 x->ealg->alg_key_len = 0;
1139 x->ealg->alg_key_len = key->sadb_key_bits;
1140 memcpy(x->ealg->alg_key, key+1, keysize);
1142 x->props.ealgo = sa->sadb_sa_encrypt;
1145 /* x->algo.flags = sa->sadb_sa_flags; */
1147 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1149 if (!x->props.family) {
1150 err = -EAFNOSUPPORT;
1153 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1156 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1157 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1158 x->props.mode = sa2->sadb_x_sa2_mode;
1161 x->props.reqid = sa2->sadb_x_sa2_reqid;
1164 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1165 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1167 /* Nobody uses this, but we try. */
1168 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1169 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1172 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1173 struct sadb_x_nat_t_type* n_type;
1174 struct xfrm_encap_tmpl *natt;
1176 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1181 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1182 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1184 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1185 struct sadb_x_nat_t_port* n_port =
1186 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1187 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1189 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1190 struct sadb_x_nat_t_port* n_port =
1191 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1192 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1196 err = xfrm_init_state(x);
1200 x->km.seq = hdr->sadb_msg_seq;
1204 x->km.state = XFRM_STATE_DEAD;
1206 return ERR_PTR(err);
1209 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1214 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1216 struct sk_buff *resp_skb;
1217 struct sadb_x_sa2 *sa2;
1218 struct sadb_address *saddr, *daddr;
1219 struct sadb_msg *out_hdr;
1220 struct xfrm_state *x = NULL;
1224 unsigned short family;
1225 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1227 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1228 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1231 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1235 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1236 mode = sa2->sadb_x_sa2_mode - 1;
1237 reqid = sa2->sadb_x_sa2_reqid;
1243 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1244 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1246 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1249 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1250 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1252 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1254 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1255 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1260 if (hdr->sadb_msg_seq) {
1261 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1262 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1269 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1274 resp_skb = ERR_PTR(-ENOENT);
1276 spin_lock_bh(&x->lock);
1277 if (x->km.state != XFRM_STATE_DEAD) {
1278 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1279 u32 min_spi, max_spi;
1281 if (range != NULL) {
1282 min_spi = range->sadb_spirange_min;
1283 max_spi = range->sadb_spirange_max;
1286 max_spi = 0x0fffffff;
1288 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1290 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1292 spin_unlock_bh(&x->lock);
1294 if (IS_ERR(resp_skb)) {
1296 return PTR_ERR(resp_skb);
1299 out_hdr = (struct sadb_msg *) resp_skb->data;
1300 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1301 out_hdr->sadb_msg_type = SADB_GETSPI;
1302 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1303 out_hdr->sadb_msg_errno = 0;
1304 out_hdr->sadb_msg_reserved = 0;
1305 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1306 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1310 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1315 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1317 struct xfrm_state *x;
1319 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1322 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1325 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1329 spin_lock_bh(&x->lock);
1330 if (x->km.state == XFRM_STATE_ACQ) {
1331 x->km.state = XFRM_STATE_ERROR;
1334 spin_unlock_bh(&x->lock);
1339 static inline int event2poltype(int event)
1342 case XFRM_MSG_DELPOLICY:
1343 return SADB_X_SPDDELETE;
1344 case XFRM_MSG_NEWPOLICY:
1345 return SADB_X_SPDADD;
1346 case XFRM_MSG_UPDPOLICY:
1347 return SADB_X_SPDUPDATE;
1348 case XFRM_MSG_POLEXPIRE:
1349 // return SADB_X_SPDEXPIRE;
1351 printk("pfkey: Unknown policy event %d\n", event);
1358 static inline int event2keytype(int event)
1361 case XFRM_MSG_DELSA:
1363 case XFRM_MSG_NEWSA:
1365 case XFRM_MSG_UPDSA:
1367 case XFRM_MSG_EXPIRE:
1370 printk("pfkey: Unknown SA event %d\n", event);
1378 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1380 struct sk_buff *skb;
1381 struct sadb_msg *hdr;
1384 if (c->event == XFRM_MSG_DELSA)
1387 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1390 return PTR_ERR(skb);
1392 hdr = (struct sadb_msg *) skb->data;
1393 hdr->sadb_msg_version = PF_KEY_V2;
1394 hdr->sadb_msg_type = event2keytype(c->event);
1395 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1396 hdr->sadb_msg_errno = 0;
1397 hdr->sadb_msg_reserved = 0;
1398 hdr->sadb_msg_seq = c->seq;
1399 hdr->sadb_msg_pid = c->pid;
1401 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1406 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1408 struct xfrm_state *x;
1414 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1419 if (hdr->sadb_msg_type == SADB_ADD)
1420 err = xfrm_state_add(x);
1422 err = xfrm_state_update(x);
1425 x->km.state = XFRM_STATE_DEAD;
1430 if (hdr->sadb_msg_type == SADB_ADD)
1431 c.event = XFRM_MSG_NEWSA;
1433 c.event = XFRM_MSG_UPDSA;
1434 c.seq = hdr->sadb_msg_seq;
1435 c.pid = hdr->sadb_msg_pid;
1436 km_state_notify(x, &c);
1442 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1444 struct xfrm_state *x;
1448 if (!ext_hdrs[SADB_EXT_SA-1] ||
1449 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1450 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1453 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1457 if (xfrm_state_kern(x)) {
1462 err = xfrm_state_delete(x);
1468 c.seq = hdr->sadb_msg_seq;
1469 c.pid = hdr->sadb_msg_pid;
1470 c.event = XFRM_MSG_DELSA;
1471 km_state_notify(x, &c);
1477 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1480 struct sk_buff *out_skb;
1481 struct sadb_msg *out_hdr;
1482 struct xfrm_state *x;
1484 if (!ext_hdrs[SADB_EXT_SA-1] ||
1485 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1486 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1489 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1493 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1494 proto = x->id.proto;
1496 if (IS_ERR(out_skb))
1497 return PTR_ERR(out_skb);
1499 out_hdr = (struct sadb_msg *) out_skb->data;
1500 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1501 out_hdr->sadb_msg_type = SADB_DUMP;
1502 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1503 out_hdr->sadb_msg_errno = 0;
1504 out_hdr->sadb_msg_reserved = 0;
1505 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1506 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1507 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1512 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1515 struct sk_buff *skb;
1516 struct sadb_msg *hdr;
1517 int len, auth_len, enc_len, i;
1519 auth_len = xfrm_count_auth_supported();
1521 auth_len *= sizeof(struct sadb_alg);
1522 auth_len += sizeof(struct sadb_supported);
1525 enc_len = xfrm_count_enc_supported();
1527 enc_len *= sizeof(struct sadb_alg);
1528 enc_len += sizeof(struct sadb_supported);
1531 len = enc_len + auth_len + sizeof(struct sadb_msg);
1533 skb = alloc_skb(len + 16, allocation);
1537 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1538 pfkey_hdr_dup(hdr, orig);
1539 hdr->sadb_msg_errno = 0;
1540 hdr->sadb_msg_len = len / sizeof(uint64_t);
1543 struct sadb_supported *sp;
1544 struct sadb_alg *ap;
1546 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1547 ap = (struct sadb_alg *) (sp + 1);
1549 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1550 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1552 for (i = 0; ; i++) {
1553 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1556 if (aalg->available)
1562 struct sadb_supported *sp;
1563 struct sadb_alg *ap;
1565 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1566 ap = (struct sadb_alg *) (sp + 1);
1568 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1569 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1571 for (i = 0; ; i++) {
1572 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1575 if (ealg->available)
1584 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1586 struct pfkey_sock *pfk = pfkey_sk(sk);
1587 struct sk_buff *supp_skb;
1589 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1592 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1593 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1595 pfk->registered |= (1<<hdr->sadb_msg_satype);
1600 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1602 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1603 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1608 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1613 static int key_notify_sa_flush(struct km_event *c)
1615 struct sk_buff *skb;
1616 struct sadb_msg *hdr;
1618 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1621 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1622 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1623 hdr->sadb_msg_seq = c->seq;
1624 hdr->sadb_msg_pid = c->pid;
1625 hdr->sadb_msg_version = PF_KEY_V2;
1626 hdr->sadb_msg_errno = (uint8_t) 0;
1627 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1629 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1634 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1639 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1643 xfrm_state_flush(proto);
1644 c.data.proto = proto;
1645 c.seq = hdr->sadb_msg_seq;
1646 c.pid = hdr->sadb_msg_pid;
1647 c.event = XFRM_MSG_FLUSHSA;
1648 km_state_notify(NULL, &c);
1653 struct pfkey_dump_data
1655 struct sk_buff *skb;
1656 struct sadb_msg *hdr;
1660 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1662 struct pfkey_dump_data *data = ptr;
1663 struct sk_buff *out_skb;
1664 struct sadb_msg *out_hdr;
1666 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1667 if (IS_ERR(out_skb))
1668 return PTR_ERR(out_skb);
1670 out_hdr = (struct sadb_msg *) out_skb->data;
1671 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1672 out_hdr->sadb_msg_type = SADB_DUMP;
1673 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1674 out_hdr->sadb_msg_errno = 0;
1675 out_hdr->sadb_msg_reserved = 0;
1676 out_hdr->sadb_msg_seq = count;
1677 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1678 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1682 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1685 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1687 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1691 return xfrm_state_walk(proto, dump_sa, &data);
1694 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1696 struct pfkey_sock *pfk = pfkey_sk(sk);
1697 int satype = hdr->sadb_msg_satype;
1699 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1700 /* XXX we mangle packet... */
1701 hdr->sadb_msg_errno = 0;
1702 if (satype != 0 && satype != 1)
1704 pfk->promisc = satype;
1706 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1710 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1713 u32 reqid = *(u32*)ptr;
1715 for (i=0; i<xp->xfrm_nr; i++) {
1716 if (xp->xfrm_vec[i].reqid == reqid)
1722 static u32 gen_reqid(void)
1725 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1731 reqid = IPSEC_MANUAL_REQID_MAX+1;
1732 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1734 } while (reqid != start);
1739 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1741 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1742 struct sockaddr_in *sin;
1743 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1744 struct sockaddr_in6 *sin6;
1747 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1750 if (rq->sadb_x_ipsecrequest_mode == 0)
1753 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1754 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1755 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1757 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1758 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1759 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1761 if (!t->reqid && !(t->reqid = gen_reqid()))
1765 /* addresses present only in tunnel mode */
1767 switch (xp->family) {
1769 sin = (void*)(rq+1);
1770 if (sin->sin_family != AF_INET)
1772 t->saddr.a4 = sin->sin_addr.s_addr;
1774 if (sin->sin_family != AF_INET)
1776 t->id.daddr.a4 = sin->sin_addr.s_addr;
1778 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1780 sin6 = (void *)(rq+1);
1781 if (sin6->sin6_family != AF_INET6)
1783 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1785 if (sin6->sin6_family != AF_INET6)
1787 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1794 /* No way to set this via kame pfkey */
1795 t->aalgos = t->ealgos = t->calgos = ~0;
1801 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1804 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1805 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1807 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1808 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1810 len -= rq->sadb_x_ipsecrequest_len;
1811 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1816 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1818 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1821 int len = sizeof(struct sadb_x_sec_ctx);
1822 len += xfrm_ctx->ctx_len;
1823 return PFKEY_ALIGN8(len);
1828 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1830 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1831 int socklen = (xp->family == AF_INET ?
1832 sizeof(struct sockaddr_in) :
1833 sizeof(struct sockaddr_in6));
1835 return sizeof(struct sadb_msg) +
1836 (sizeof(struct sadb_lifetime) * 3) +
1837 (sizeof(struct sadb_address) * 2) +
1838 (sockaddr_size * 2) +
1839 sizeof(struct sadb_x_policy) +
1840 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1842 pfkey_xfrm_policy2sec_ctx_size(xp);
1845 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1847 struct sk_buff *skb;
1850 size = pfkey_xfrm_policy2msg_size(xp);
1852 skb = alloc_skb(size + 16, GFP_ATOMIC);
1854 return ERR_PTR(-ENOBUFS);
1859 static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1861 struct sadb_msg *hdr;
1862 struct sadb_address *addr;
1863 struct sadb_lifetime *lifetime;
1864 struct sadb_x_policy *pol;
1865 struct sockaddr_in *sin;
1866 struct sadb_x_sec_ctx *sec_ctx;
1867 struct xfrm_sec_ctx *xfrm_ctx;
1868 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1869 struct sockaddr_in6 *sin6;
1873 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1874 int socklen = (xp->family == AF_INET ?
1875 sizeof(struct sockaddr_in) :
1876 sizeof(struct sockaddr_in6));
1878 size = pfkey_xfrm_policy2msg_size(xp);
1880 /* call should fill header later */
1881 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1882 memset(hdr, 0, size); /* XXX do we need this ? */
1885 addr = (struct sadb_address*) skb_put(skb,
1886 sizeof(struct sadb_address)+sockaddr_size);
1887 addr->sadb_address_len =
1888 (sizeof(struct sadb_address)+sockaddr_size)/
1890 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1891 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1892 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1893 addr->sadb_address_reserved = 0;
1895 if (xp->family == AF_INET) {
1896 sin = (struct sockaddr_in *) (addr + 1);
1897 sin->sin_family = AF_INET;
1898 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1899 sin->sin_port = xp->selector.sport;
1900 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1902 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1903 else if (xp->family == AF_INET6) {
1904 sin6 = (struct sockaddr_in6 *) (addr + 1);
1905 sin6->sin6_family = AF_INET6;
1906 sin6->sin6_port = xp->selector.sport;
1907 sin6->sin6_flowinfo = 0;
1908 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1909 sizeof(struct in6_addr));
1910 sin6->sin6_scope_id = 0;
1917 addr = (struct sadb_address*) skb_put(skb,
1918 sizeof(struct sadb_address)+sockaddr_size);
1919 addr->sadb_address_len =
1920 (sizeof(struct sadb_address)+sockaddr_size)/
1922 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1923 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1924 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1925 addr->sadb_address_reserved = 0;
1926 if (xp->family == AF_INET) {
1927 sin = (struct sockaddr_in *) (addr + 1);
1928 sin->sin_family = AF_INET;
1929 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1930 sin->sin_port = xp->selector.dport;
1931 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1933 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1934 else if (xp->family == AF_INET6) {
1935 sin6 = (struct sockaddr_in6 *) (addr + 1);
1936 sin6->sin6_family = AF_INET6;
1937 sin6->sin6_port = xp->selector.dport;
1938 sin6->sin6_flowinfo = 0;
1939 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1940 sizeof(struct in6_addr));
1941 sin6->sin6_scope_id = 0;
1948 lifetime = (struct sadb_lifetime *) skb_put(skb,
1949 sizeof(struct sadb_lifetime));
1950 lifetime->sadb_lifetime_len =
1951 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1952 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1953 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1954 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1955 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1956 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1958 lifetime = (struct sadb_lifetime *) skb_put(skb,
1959 sizeof(struct sadb_lifetime));
1960 lifetime->sadb_lifetime_len =
1961 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1962 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1963 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1964 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1965 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1966 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1968 lifetime = (struct sadb_lifetime *) skb_put(skb,
1969 sizeof(struct sadb_lifetime));
1970 lifetime->sadb_lifetime_len =
1971 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1972 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1973 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1974 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1975 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1976 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1978 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1979 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1980 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1981 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1982 if (xp->action == XFRM_POLICY_ALLOW) {
1984 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1986 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1988 pol->sadb_x_policy_dir = dir+1;
1989 pol->sadb_x_policy_id = xp->index;
1990 pol->sadb_x_policy_priority = xp->priority;
1992 for (i=0; i<xp->xfrm_nr; i++) {
1993 struct sadb_x_ipsecrequest *rq;
1994 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1997 req_size = sizeof(struct sadb_x_ipsecrequest);
1999 req_size += 2*socklen;
2002 rq = (void*)skb_put(skb, req_size);
2003 pol->sadb_x_policy_len += req_size/8;
2004 memset(rq, 0, sizeof(*rq));
2005 rq->sadb_x_ipsecrequest_len = req_size;
2006 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2007 rq->sadb_x_ipsecrequest_mode = t->mode+1;
2008 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2010 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2012 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2013 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2015 switch (xp->family) {
2017 sin = (void*)(rq+1);
2018 sin->sin_family = AF_INET;
2019 sin->sin_addr.s_addr = t->saddr.a4;
2021 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2023 sin->sin_family = AF_INET;
2024 sin->sin_addr.s_addr = t->id.daddr.a4;
2026 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2028 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2030 sin6 = (void*)(rq+1);
2031 sin6->sin6_family = AF_INET6;
2032 sin6->sin6_port = 0;
2033 sin6->sin6_flowinfo = 0;
2034 memcpy(&sin6->sin6_addr, t->saddr.a6,
2035 sizeof(struct in6_addr));
2036 sin6->sin6_scope_id = 0;
2039 sin6->sin6_family = AF_INET6;
2040 sin6->sin6_port = 0;
2041 sin6->sin6_flowinfo = 0;
2042 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2043 sizeof(struct in6_addr));
2044 sin6->sin6_scope_id = 0;
2053 /* security context */
2054 if ((xfrm_ctx = xp->security)) {
2055 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2057 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2058 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2059 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2060 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2061 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2062 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2063 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2067 hdr->sadb_msg_len = size / sizeof(uint64_t);
2068 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2071 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2073 struct sk_buff *out_skb;
2074 struct sadb_msg *out_hdr;
2077 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2078 if (IS_ERR(out_skb)) {
2079 err = PTR_ERR(out_skb);
2082 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2084 out_hdr = (struct sadb_msg *) out_skb->data;
2085 out_hdr->sadb_msg_version = PF_KEY_V2;
2087 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2088 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2090 out_hdr->sadb_msg_type = event2poltype(c->event);
2091 out_hdr->sadb_msg_errno = 0;
2092 out_hdr->sadb_msg_seq = c->seq;
2093 out_hdr->sadb_msg_pid = c->pid;
2094 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2100 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2103 struct sadb_lifetime *lifetime;
2104 struct sadb_address *sa;
2105 struct sadb_x_policy *pol;
2106 struct xfrm_policy *xp;
2108 struct sadb_x_sec_ctx *sec_ctx;
2110 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2111 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2112 !ext_hdrs[SADB_X_EXT_POLICY-1])
2115 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2116 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2118 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2121 xp = xfrm_policy_alloc(GFP_KERNEL);
2125 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2126 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2127 xp->priority = pol->sadb_x_policy_priority;
2129 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2130 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2135 xp->selector.family = xp->family;
2136 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2137 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2138 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2139 if (xp->selector.sport)
2140 xp->selector.sport_mask = ~0;
2142 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2143 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2144 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2146 /* Amusing, we set this twice. KAME apps appear to set same value
2147 * in both addresses.
2149 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2151 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2152 if (xp->selector.dport)
2153 xp->selector.dport_mask = ~0;
2155 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2156 if (sec_ctx != NULL) {
2157 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2164 err = security_xfrm_policy_alloc(xp, uctx);
2171 xp->lft.soft_byte_limit = XFRM_INF;
2172 xp->lft.hard_byte_limit = XFRM_INF;
2173 xp->lft.soft_packet_limit = XFRM_INF;
2174 xp->lft.hard_packet_limit = XFRM_INF;
2175 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2176 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2177 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2178 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2179 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2181 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2182 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2183 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2184 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2185 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2188 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2189 (err = parse_ipsecrequests(xp, pol)) < 0)
2192 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2193 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2198 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2199 c.event = XFRM_MSG_UPDPOLICY;
2201 c.event = XFRM_MSG_NEWPOLICY;
2203 c.seq = hdr->sadb_msg_seq;
2204 c.pid = hdr->sadb_msg_pid;
2206 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2211 security_xfrm_policy_free(xp);
2216 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2219 struct sadb_address *sa;
2220 struct sadb_x_policy *pol;
2221 struct xfrm_policy *xp, tmp;
2222 struct xfrm_selector sel;
2224 struct sadb_x_sec_ctx *sec_ctx;
2226 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2227 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2228 !ext_hdrs[SADB_X_EXT_POLICY-1])
2231 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2232 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2235 memset(&sel, 0, sizeof(sel));
2237 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2238 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2239 sel.prefixlen_s = sa->sadb_address_prefixlen;
2240 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2241 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2243 sel.sport_mask = ~0;
2245 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2246 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2247 sel.prefixlen_d = sa->sadb_address_prefixlen;
2248 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2249 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2251 sel.dport_mask = ~0;
2253 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2254 memset(&tmp, 0, sizeof(struct xfrm_policy));
2256 if (sec_ctx != NULL) {
2257 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2262 err = security_xfrm_policy_alloc(&tmp, uctx);
2269 xp = xfrm_policy_bysel_ctx(pol->sadb_x_policy_dir-1, &sel, tmp.security, 1);
2270 security_xfrm_policy_free(&tmp);
2276 c.seq = hdr->sadb_msg_seq;
2277 c.pid = hdr->sadb_msg_pid;
2278 c.event = XFRM_MSG_DELPOLICY;
2279 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2285 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2288 struct sk_buff *out_skb;
2289 struct sadb_msg *out_hdr;
2292 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2293 if (IS_ERR(out_skb)) {
2294 err = PTR_ERR(out_skb);
2297 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2299 out_hdr = (struct sadb_msg *) out_skb->data;
2300 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2301 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2302 out_hdr->sadb_msg_satype = 0;
2303 out_hdr->sadb_msg_errno = 0;
2304 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2305 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2306 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2313 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2317 struct sadb_x_policy *pol;
2318 struct xfrm_policy *xp;
2321 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2324 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2325 if (dir >= XFRM_POLICY_MAX)
2328 xp = xfrm_policy_byid(dir, pol->sadb_x_policy_id,
2329 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2335 c.seq = hdr->sadb_msg_seq;
2336 c.pid = hdr->sadb_msg_pid;
2337 if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
2339 c.event = XFRM_MSG_DELPOLICY;
2340 km_policy_notify(xp, dir, &c);
2342 err = key_pol_get_resp(sk, xp, hdr, dir);
2349 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2351 struct pfkey_dump_data *data = ptr;
2352 struct sk_buff *out_skb;
2353 struct sadb_msg *out_hdr;
2355 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2356 if (IS_ERR(out_skb))
2357 return PTR_ERR(out_skb);
2359 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2361 out_hdr = (struct sadb_msg *) out_skb->data;
2362 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2363 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2364 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2365 out_hdr->sadb_msg_errno = 0;
2366 out_hdr->sadb_msg_seq = count;
2367 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2368 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2372 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2374 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2376 return xfrm_policy_walk(dump_sp, &data);
2379 static int key_notify_policy_flush(struct km_event *c)
2381 struct sk_buff *skb_out;
2382 struct sadb_msg *hdr;
2384 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2387 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2388 hdr->sadb_msg_seq = c->seq;
2389 hdr->sadb_msg_pid = c->pid;
2390 hdr->sadb_msg_version = PF_KEY_V2;
2391 hdr->sadb_msg_errno = (uint8_t) 0;
2392 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2393 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2398 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2402 xfrm_policy_flush();
2403 c.event = XFRM_MSG_FLUSHPOLICY;
2404 c.pid = hdr->sadb_msg_pid;
2405 c.seq = hdr->sadb_msg_seq;
2406 km_policy_notify(NULL, 0, &c);
2411 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2412 struct sadb_msg *hdr, void **ext_hdrs);
2413 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2414 [SADB_RESERVED] = pfkey_reserved,
2415 [SADB_GETSPI] = pfkey_getspi,
2416 [SADB_UPDATE] = pfkey_add,
2417 [SADB_ADD] = pfkey_add,
2418 [SADB_DELETE] = pfkey_delete,
2419 [SADB_GET] = pfkey_get,
2420 [SADB_ACQUIRE] = pfkey_acquire,
2421 [SADB_REGISTER] = pfkey_register,
2422 [SADB_EXPIRE] = NULL,
2423 [SADB_FLUSH] = pfkey_flush,
2424 [SADB_DUMP] = pfkey_dump,
2425 [SADB_X_PROMISC] = pfkey_promisc,
2426 [SADB_X_PCHANGE] = NULL,
2427 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2428 [SADB_X_SPDADD] = pfkey_spdadd,
2429 [SADB_X_SPDDELETE] = pfkey_spddelete,
2430 [SADB_X_SPDGET] = pfkey_spdget,
2431 [SADB_X_SPDACQUIRE] = NULL,
2432 [SADB_X_SPDDUMP] = pfkey_spddump,
2433 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2434 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2435 [SADB_X_SPDDELETE2] = pfkey_spdget,
2438 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2440 void *ext_hdrs[SADB_EXT_MAX];
2443 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2444 BROADCAST_PROMISC_ONLY, NULL);
2446 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2447 err = parse_exthdrs(skb, hdr, ext_hdrs);
2450 if (pfkey_funcs[hdr->sadb_msg_type])
2451 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2456 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2458 struct sadb_msg *hdr = NULL;
2460 if (skb->len < sizeof(*hdr)) {
2463 hdr = (struct sadb_msg *) skb->data;
2464 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2465 hdr->sadb_msg_reserved != 0 ||
2466 (hdr->sadb_msg_type <= SADB_RESERVED ||
2467 hdr->sadb_msg_type > SADB_MAX)) {
2470 } else if (hdr->sadb_msg_len != (skb->len /
2471 sizeof(uint64_t)) ||
2472 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2473 sizeof(uint64_t))) {
2483 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2485 return t->aalgos & (1 << d->desc.sadb_alg_id);
2488 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2490 return t->ealgos & (1 << d->desc.sadb_alg_id);
2493 static int count_ah_combs(struct xfrm_tmpl *t)
2497 for (i = 0; ; i++) {
2498 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2501 if (aalg_tmpl_set(t, aalg) && aalg->available)
2502 sz += sizeof(struct sadb_comb);
2504 return sz + sizeof(struct sadb_prop);
2507 static int count_esp_combs(struct xfrm_tmpl *t)
2511 for (i = 0; ; i++) {
2512 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2516 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2519 for (k = 1; ; k++) {
2520 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2524 if (aalg_tmpl_set(t, aalg) && aalg->available)
2525 sz += sizeof(struct sadb_comb);
2528 return sz + sizeof(struct sadb_prop);
2531 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2533 struct sadb_prop *p;
2536 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2537 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2538 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2539 p->sadb_prop_replay = 32;
2540 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2542 for (i = 0; ; i++) {
2543 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2547 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2548 struct sadb_comb *c;
2549 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2550 memset(c, 0, sizeof(*c));
2551 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2552 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2553 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2554 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2555 c->sadb_comb_hard_addtime = 24*60*60;
2556 c->sadb_comb_soft_addtime = 20*60*60;
2557 c->sadb_comb_hard_usetime = 8*60*60;
2558 c->sadb_comb_soft_usetime = 7*60*60;
2563 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2565 struct sadb_prop *p;
2568 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2569 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2570 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2571 p->sadb_prop_replay = 32;
2572 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2575 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2579 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2582 for (k = 1; ; k++) {
2583 struct sadb_comb *c;
2584 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2587 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2589 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2590 memset(c, 0, sizeof(*c));
2591 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2592 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2593 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2594 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2595 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2596 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2597 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2598 c->sadb_comb_hard_addtime = 24*60*60;
2599 c->sadb_comb_soft_addtime = 20*60*60;
2600 c->sadb_comb_hard_usetime = 8*60*60;
2601 c->sadb_comb_soft_usetime = 7*60*60;
2606 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2611 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2613 struct sk_buff *out_skb;
2614 struct sadb_msg *out_hdr;
2618 hard = c->data.hard;
2624 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2625 if (IS_ERR(out_skb))
2626 return PTR_ERR(out_skb);
2628 out_hdr = (struct sadb_msg *) out_skb->data;
2629 out_hdr->sadb_msg_version = PF_KEY_V2;
2630 out_hdr->sadb_msg_type = SADB_EXPIRE;
2631 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2632 out_hdr->sadb_msg_errno = 0;
2633 out_hdr->sadb_msg_reserved = 0;
2634 out_hdr->sadb_msg_seq = 0;
2635 out_hdr->sadb_msg_pid = 0;
2637 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2641 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2644 case XFRM_MSG_EXPIRE:
2645 return key_notify_sa_expire(x, c);
2646 case XFRM_MSG_DELSA:
2647 case XFRM_MSG_NEWSA:
2648 case XFRM_MSG_UPDSA:
2649 return key_notify_sa(x, c);
2650 case XFRM_MSG_FLUSHSA:
2651 return key_notify_sa_flush(c);
2653 printk("pfkey: Unknown SA event %d\n", c->event);
2660 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2663 case XFRM_MSG_POLEXPIRE:
2664 return key_notify_policy_expire(xp, c);
2665 case XFRM_MSG_DELPOLICY:
2666 case XFRM_MSG_NEWPOLICY:
2667 case XFRM_MSG_UPDPOLICY:
2668 return key_notify_policy(xp, dir, c);
2669 case XFRM_MSG_FLUSHPOLICY:
2670 return key_notify_policy_flush(c);
2672 printk("pfkey: Unknown policy event %d\n", c->event);
2679 static u32 get_acqseq(void)
2683 static DEFINE_SPINLOCK(acqseq_lock);
2685 spin_lock_bh(&acqseq_lock);
2686 res = (++acqseq ? : ++acqseq);
2687 spin_unlock_bh(&acqseq_lock);
2691 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2693 struct sk_buff *skb;
2694 struct sadb_msg *hdr;
2695 struct sadb_address *addr;
2696 struct sadb_x_policy *pol;
2697 struct sockaddr_in *sin;
2698 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2699 struct sockaddr_in6 *sin6;
2704 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2708 size = sizeof(struct sadb_msg) +
2709 (sizeof(struct sadb_address) * 2) +
2710 (sockaddr_size * 2) +
2711 sizeof(struct sadb_x_policy);
2713 if (x->id.proto == IPPROTO_AH)
2714 size += count_ah_combs(t);
2715 else if (x->id.proto == IPPROTO_ESP)
2716 size += count_esp_combs(t);
2718 skb = alloc_skb(size + 16, GFP_ATOMIC);
2722 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2723 hdr->sadb_msg_version = PF_KEY_V2;
2724 hdr->sadb_msg_type = SADB_ACQUIRE;
2725 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2726 hdr->sadb_msg_len = size / sizeof(uint64_t);
2727 hdr->sadb_msg_errno = 0;
2728 hdr->sadb_msg_reserved = 0;
2729 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2730 hdr->sadb_msg_pid = 0;
2733 addr = (struct sadb_address*) skb_put(skb,
2734 sizeof(struct sadb_address)+sockaddr_size);
2735 addr->sadb_address_len =
2736 (sizeof(struct sadb_address)+sockaddr_size)/
2738 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2739 addr->sadb_address_proto = 0;
2740 addr->sadb_address_reserved = 0;
2741 if (x->props.family == AF_INET) {
2742 addr->sadb_address_prefixlen = 32;
2744 sin = (struct sockaddr_in *) (addr + 1);
2745 sin->sin_family = AF_INET;
2746 sin->sin_addr.s_addr = x->props.saddr.a4;
2748 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2750 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2751 else if (x->props.family == AF_INET6) {
2752 addr->sadb_address_prefixlen = 128;
2754 sin6 = (struct sockaddr_in6 *) (addr + 1);
2755 sin6->sin6_family = AF_INET6;
2756 sin6->sin6_port = 0;
2757 sin6->sin6_flowinfo = 0;
2758 memcpy(&sin6->sin6_addr,
2759 x->props.saddr.a6, sizeof(struct in6_addr));
2760 sin6->sin6_scope_id = 0;
2767 addr = (struct sadb_address*) skb_put(skb,
2768 sizeof(struct sadb_address)+sockaddr_size);
2769 addr->sadb_address_len =
2770 (sizeof(struct sadb_address)+sockaddr_size)/
2772 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2773 addr->sadb_address_proto = 0;
2774 addr->sadb_address_reserved = 0;
2775 if (x->props.family == AF_INET) {
2776 addr->sadb_address_prefixlen = 32;
2778 sin = (struct sockaddr_in *) (addr + 1);
2779 sin->sin_family = AF_INET;
2780 sin->sin_addr.s_addr = x->id.daddr.a4;
2782 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2784 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2785 else if (x->props.family == AF_INET6) {
2786 addr->sadb_address_prefixlen = 128;
2788 sin6 = (struct sockaddr_in6 *) (addr + 1);
2789 sin6->sin6_family = AF_INET6;
2790 sin6->sin6_port = 0;
2791 sin6->sin6_flowinfo = 0;
2792 memcpy(&sin6->sin6_addr,
2793 x->id.daddr.a6, sizeof(struct in6_addr));
2794 sin6->sin6_scope_id = 0;
2800 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2801 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2802 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2803 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2804 pol->sadb_x_policy_dir = dir+1;
2805 pol->sadb_x_policy_id = xp->index;
2807 /* Set sadb_comb's. */
2808 if (x->id.proto == IPPROTO_AH)
2809 dump_ah_combs(skb, t);
2810 else if (x->id.proto == IPPROTO_ESP)
2811 dump_esp_combs(skb, t);
2813 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2816 static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2817 u8 *data, int len, int *dir)
2819 struct xfrm_policy *xp;
2820 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2821 struct sadb_x_sec_ctx *sec_ctx;
2825 if (opt != IP_IPSEC_POLICY) {
2830 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2832 if (opt != IPV6_IPSEC_POLICY) {
2845 if (len < sizeof(struct sadb_x_policy) ||
2846 pol->sadb_x_policy_len*8 > len ||
2847 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2848 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2851 xp = xfrm_policy_alloc(GFP_ATOMIC);
2857 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2858 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2860 xp->lft.soft_byte_limit = XFRM_INF;
2861 xp->lft.hard_byte_limit = XFRM_INF;
2862 xp->lft.soft_packet_limit = XFRM_INF;
2863 xp->lft.hard_packet_limit = XFRM_INF;
2864 xp->family = family;
2867 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2868 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2871 /* security context too */
2872 if (len >= (pol->sadb_x_policy_len*8 +
2873 sizeof(struct sadb_x_sec_ctx))) {
2874 char *p = (char *)pol;
2875 struct xfrm_user_sec_ctx *uctx;
2877 p += pol->sadb_x_policy_len*8;
2878 sec_ctx = (struct sadb_x_sec_ctx *)p;
2879 if (len < pol->sadb_x_policy_len*8 +
2880 sec_ctx->sadb_x_sec_len)
2882 if ((*dir = verify_sec_ctx_len(p)))
2884 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2885 *dir = security_xfrm_policy_alloc(xp, uctx);
2892 *dir = pol->sadb_x_policy_dir-1;
2896 security_xfrm_policy_free(xp);
2901 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2903 struct sk_buff *skb;
2904 struct sadb_msg *hdr;
2906 struct sadb_address *addr;
2907 struct sadb_x_nat_t_port *n_port;
2908 struct sockaddr_in *sin;
2909 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2910 struct sockaddr_in6 *sin6;
2914 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2915 struct xfrm_encap_tmpl *natt = NULL;
2917 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2929 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2931 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2932 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2935 size = sizeof(struct sadb_msg) +
2936 sizeof(struct sadb_sa) +
2937 (sizeof(struct sadb_address) * 2) +
2938 (sockaddr_size * 2) +
2939 (sizeof(struct sadb_x_nat_t_port) * 2);
2941 skb = alloc_skb(size + 16, GFP_ATOMIC);
2945 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2946 hdr->sadb_msg_version = PF_KEY_V2;
2947 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2948 hdr->sadb_msg_satype = satype;
2949 hdr->sadb_msg_len = size / sizeof(uint64_t);
2950 hdr->sadb_msg_errno = 0;
2951 hdr->sadb_msg_reserved = 0;
2952 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2953 hdr->sadb_msg_pid = 0;
2956 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2957 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2958 sa->sadb_sa_exttype = SADB_EXT_SA;
2959 sa->sadb_sa_spi = x->id.spi;
2960 sa->sadb_sa_replay = 0;
2961 sa->sadb_sa_state = 0;
2962 sa->sadb_sa_auth = 0;
2963 sa->sadb_sa_encrypt = 0;
2964 sa->sadb_sa_flags = 0;
2966 /* ADDRESS_SRC (old addr) */
2967 addr = (struct sadb_address*)
2968 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2969 addr->sadb_address_len =
2970 (sizeof(struct sadb_address)+sockaddr_size)/
2972 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2973 addr->sadb_address_proto = 0;
2974 addr->sadb_address_reserved = 0;
2975 if (x->props.family == AF_INET) {
2976 addr->sadb_address_prefixlen = 32;
2978 sin = (struct sockaddr_in *) (addr + 1);
2979 sin->sin_family = AF_INET;
2980 sin->sin_addr.s_addr = x->props.saddr.a4;
2982 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2984 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2985 else if (x->props.family == AF_INET6) {
2986 addr->sadb_address_prefixlen = 128;
2988 sin6 = (struct sockaddr_in6 *) (addr + 1);
2989 sin6->sin6_family = AF_INET6;
2990 sin6->sin6_port = 0;
2991 sin6->sin6_flowinfo = 0;
2992 memcpy(&sin6->sin6_addr,
2993 x->props.saddr.a6, sizeof(struct in6_addr));
2994 sin6->sin6_scope_id = 0;
3000 /* NAT_T_SPORT (old port) */
3001 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3002 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3003 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3004 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3005 n_port->sadb_x_nat_t_port_reserved = 0;
3007 /* ADDRESS_DST (new addr) */
3008 addr = (struct sadb_address*)
3009 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3010 addr->sadb_address_len =
3011 (sizeof(struct sadb_address)+sockaddr_size)/
3013 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3014 addr->sadb_address_proto = 0;
3015 addr->sadb_address_reserved = 0;
3016 if (x->props.family == AF_INET) {
3017 addr->sadb_address_prefixlen = 32;
3019 sin = (struct sockaddr_in *) (addr + 1);
3020 sin->sin_family = AF_INET;
3021 sin->sin_addr.s_addr = ipaddr->a4;
3023 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3025 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3026 else if (x->props.family == AF_INET6) {
3027 addr->sadb_address_prefixlen = 128;
3029 sin6 = (struct sockaddr_in6 *) (addr + 1);
3030 sin6->sin6_family = AF_INET6;
3031 sin6->sin6_port = 0;
3032 sin6->sin6_flowinfo = 0;
3033 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3034 sin6->sin6_scope_id = 0;
3040 /* NAT_T_DPORT (new port) */
3041 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3042 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3043 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3044 n_port->sadb_x_nat_t_port_port = sport;
3045 n_port->sadb_x_nat_t_port_reserved = 0;
3047 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3050 static int pfkey_sendmsg(struct kiocb *kiocb,
3051 struct socket *sock, struct msghdr *msg, size_t len)
3053 struct sock *sk = sock->sk;
3054 struct sk_buff *skb = NULL;
3055 struct sadb_msg *hdr = NULL;
3059 if (msg->msg_flags & MSG_OOB)
3063 if ((unsigned)len > sk->sk_sndbuf - 32)
3067 skb = alloc_skb(len, GFP_KERNEL);
3072 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3075 hdr = pfkey_get_base_msg(skb, &err);
3079 down(&xfrm_cfg_sem);
3080 err = pfkey_process(sk, skb, hdr);
3084 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3092 static int pfkey_recvmsg(struct kiocb *kiocb,
3093 struct socket *sock, struct msghdr *msg, size_t len,
3096 struct sock *sk = sock->sk;
3097 struct sk_buff *skb;
3101 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3104 msg->msg_namelen = 0;
3105 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3111 msg->msg_flags |= MSG_TRUNC;
3115 skb->h.raw = skb->data;
3116 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3120 sock_recv_timestamp(msg, sk, skb);
3122 err = (flags & MSG_TRUNC) ? skb->len : copied;
3125 skb_free_datagram(sk, skb);
3130 static const struct proto_ops pfkey_ops = {
3132 .owner = THIS_MODULE,
3133 /* Operations that make no sense on pfkey sockets. */
3134 .bind = sock_no_bind,
3135 .connect = sock_no_connect,
3136 .socketpair = sock_no_socketpair,
3137 .accept = sock_no_accept,
3138 .getname = sock_no_getname,
3139 .ioctl = sock_no_ioctl,
3140 .listen = sock_no_listen,
3141 .shutdown = sock_no_shutdown,
3142 .setsockopt = sock_no_setsockopt,
3143 .getsockopt = sock_no_getsockopt,
3144 .mmap = sock_no_mmap,
3145 .sendpage = sock_no_sendpage,
3147 /* Now the operations that really occur. */
3148 .release = pfkey_release,
3149 .poll = datagram_poll,
3150 .sendmsg = pfkey_sendmsg,
3151 .recvmsg = pfkey_recvmsg,
3154 static struct net_proto_family pfkey_family_ops = {
3156 .create = pfkey_create,
3157 .owner = THIS_MODULE,
3160 #ifdef CONFIG_PROC_FS
3161 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
3162 int length, int *eof, void *data)
3168 struct hlist_node *node;
3170 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
3172 read_lock(&pfkey_table_lock);
3174 sk_for_each(s, node, &pfkey_table) {
3175 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
3177 atomic_read(&s->sk_refcnt),
3178 atomic_read(&s->sk_rmem_alloc),
3179 atomic_read(&s->sk_wmem_alloc),
3184 buffer[len++] = '\n';
3191 if(pos > offset + length)
3197 read_unlock(&pfkey_table_lock);
3199 *start = buffer + (offset - begin);
3200 len -= (offset - begin);
3211 static struct xfrm_mgr pfkeyv2_mgr =
3214 .notify = pfkey_send_notify,
3215 .acquire = pfkey_send_acquire,
3216 .compile_policy = pfkey_compile_policy,
3217 .new_mapping = pfkey_send_new_mapping,
3218 .notify_policy = pfkey_send_policy_notify,
3221 static void __exit ipsec_pfkey_exit(void)
3223 xfrm_unregister_km(&pfkeyv2_mgr);
3224 remove_proc_entry("net/pfkey", NULL);
3225 sock_unregister(PF_KEY);
3226 proto_unregister(&key_proto);
3229 static int __init ipsec_pfkey_init(void)
3231 int err = proto_register(&key_proto, 0);
3236 err = sock_register(&pfkey_family_ops);
3238 goto out_unregister_key_proto;
3239 #ifdef CONFIG_PROC_FS
3241 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3242 goto out_sock_unregister;
3244 err = xfrm_register_km(&pfkeyv2_mgr);
3246 goto out_remove_proc_entry;
3249 out_remove_proc_entry:
3250 #ifdef CONFIG_PROC_FS
3251 remove_proc_entry("net/pfkey", NULL);
3252 out_sock_unregister:
3254 sock_unregister(PF_KEY);
3255 out_unregister_key_proto:
3256 proto_unregister(&key_proto);
3260 module_init(ipsec_pfkey_init);
3261 module_exit(ipsec_pfkey_exit);
3262 MODULE_LICENSE("GPL");
3263 MODULE_ALIAS_NETPROTO(PF_KEY);