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 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 unsigned int __nocast 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, unsigned int __nocast 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),
341 /* Verify sadb_address_{len,prefixlen} against sa_family. */
342 static int verify_address_len(void *p)
344 struct sadb_address *sp = p;
345 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
346 struct sockaddr_in *sin;
347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
348 struct sockaddr_in6 *sin6;
352 switch (addr->sa_family) {
354 len = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
355 len /= sizeof(uint64_t);
356 if (sp->sadb_address_len != len ||
357 sp->sadb_address_prefixlen > 32)
360 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
362 len = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
363 len /= sizeof(uint64_t);
364 if (sp->sadb_address_len != len ||
365 sp->sadb_address_prefixlen > 128)
370 /* It is user using kernel to keep track of security
371 * associations for another protocol, such as
372 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
375 * XXX Actually, association/policy database is not yet
376 * XXX able to cope with arbitrary sockaddr families.
377 * XXX When it can, remove this -EINVAL. -DaveM
386 static int present_and_same_family(struct sadb_address *src,
387 struct sadb_address *dst)
389 struct sockaddr *s_addr, *d_addr;
394 s_addr = (struct sockaddr *)(src + 1);
395 d_addr = (struct sockaddr *)(dst + 1);
396 if (s_addr->sa_family != d_addr->sa_family)
398 if (s_addr->sa_family != AF_INET
399 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
400 && s_addr->sa_family != AF_INET6
408 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
410 char *p = (char *) hdr;
416 struct sadb_ext *ehdr = (struct sadb_ext *) p;
420 ext_len = ehdr->sadb_ext_len;
421 ext_len *= sizeof(uint64_t);
422 ext_type = ehdr->sadb_ext_type;
423 if (ext_len < sizeof(uint64_t) ||
425 ext_type == SADB_EXT_RESERVED)
428 if (ext_type <= SADB_EXT_MAX) {
429 int min = (int) sadb_ext_min_len[ext_type];
432 if (ext_hdrs[ext_type-1] != NULL)
434 if (ext_type == SADB_EXT_ADDRESS_SRC ||
435 ext_type == SADB_EXT_ADDRESS_DST ||
436 ext_type == SADB_EXT_ADDRESS_PROXY ||
437 ext_type == SADB_X_EXT_NAT_T_OA) {
438 if (verify_address_len(p))
441 ext_hdrs[ext_type-1] = p;
451 pfkey_satype2proto(uint8_t satype)
454 case SADB_SATYPE_UNSPEC:
455 return IPSEC_PROTO_ANY;
458 case SADB_SATYPE_ESP:
460 case SADB_X_SATYPE_IPCOMP:
470 pfkey_proto2satype(uint16_t proto)
474 return SADB_SATYPE_AH;
476 return SADB_SATYPE_ESP;
478 return SADB_X_SATYPE_IPCOMP;
486 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
487 * say specifically 'just raw sockets' as we encode them as 255.
490 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
492 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
495 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
497 return (proto ? proto : IPSEC_PROTO_ANY);
500 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
501 xfrm_address_t *xaddr)
503 switch (((struct sockaddr*)(addr + 1))->sa_family) {
506 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
508 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
511 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
512 sizeof(struct in6_addr));
521 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
524 struct sadb_address *addr;
526 unsigned short family;
527 xfrm_address_t *xaddr;
529 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
533 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
537 /* sadb_address_len should be checked by caller */
538 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
542 family = ((struct sockaddr *)(addr + 1))->sa_family;
545 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
547 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
549 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
559 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
562 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
564 pfkey_sockaddr_size(sa_family_t family)
568 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
569 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
571 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
579 static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
582 struct sadb_msg *hdr;
584 struct sadb_lifetime *lifetime;
585 struct sadb_address *addr;
586 struct sadb_key *key;
587 struct sadb_x_sa2 *sa2;
588 struct sockaddr_in *sin;
589 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
590 struct sockaddr_in6 *sin6;
593 int auth_key_size = 0;
594 int encrypt_key_size = 0;
596 struct xfrm_encap_tmpl *natt = NULL;
598 /* address family check */
599 sockaddr_size = pfkey_sockaddr_size(x->props.family);
601 return ERR_PTR(-EINVAL);
603 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
604 key(AE), (identity(SD),) (sensitivity)> */
605 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
606 sizeof(struct sadb_lifetime) +
607 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
608 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
609 sizeof(struct sadb_address)*2 +
611 sizeof(struct sadb_x_sa2);
612 /* identity & sensitivity */
614 if ((x->props.family == AF_INET &&
615 x->sel.saddr.a4 != x->props.saddr.a4)
616 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617 || (x->props.family == AF_INET6 &&
618 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
621 size += sizeof(struct sadb_address) + sockaddr_size;
624 if (x->aalg && x->aalg->alg_key_len) {
626 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
627 size += sizeof(struct sadb_key) + auth_key_size;
629 if (x->ealg && x->ealg->alg_key_len) {
631 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
632 size += sizeof(struct sadb_key) + encrypt_key_size;
638 if (natt && natt->encap_type) {
639 size += sizeof(struct sadb_x_nat_t_type);
640 size += sizeof(struct sadb_x_nat_t_port);
641 size += sizeof(struct sadb_x_nat_t_port);
644 skb = alloc_skb(size + 16, GFP_ATOMIC);
646 return ERR_PTR(-ENOBUFS);
648 /* call should fill header later */
649 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
650 memset(hdr, 0, size); /* XXX do we need this ? */
651 hdr->sadb_msg_len = size / sizeof(uint64_t);
654 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
655 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
656 sa->sadb_sa_exttype = SADB_EXT_SA;
657 sa->sadb_sa_spi = x->id.spi;
658 sa->sadb_sa_replay = x->props.replay_window;
659 switch (x->km.state) {
660 case XFRM_STATE_VALID:
661 sa->sadb_sa_state = x->km.dying ?
662 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
665 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
668 sa->sadb_sa_state = SADB_SASTATE_DEAD;
671 sa->sadb_sa_auth = 0;
673 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
674 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
676 sa->sadb_sa_encrypt = 0;
677 BUG_ON(x->ealg && x->calg);
679 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
680 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
682 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
684 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
685 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
688 sa->sadb_sa_flags = 0;
689 if (x->props.flags & XFRM_STATE_NOECN)
690 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
691 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
692 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
693 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
694 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
698 lifetime = (struct sadb_lifetime *) skb_put(skb,
699 sizeof(struct sadb_lifetime));
700 lifetime->sadb_lifetime_len =
701 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
702 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
703 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
704 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
705 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
706 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
710 lifetime = (struct sadb_lifetime *) skb_put(skb,
711 sizeof(struct sadb_lifetime));
712 lifetime->sadb_lifetime_len =
713 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
714 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
715 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
716 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
717 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
718 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
721 lifetime = (struct sadb_lifetime *) skb_put(skb,
722 sizeof(struct sadb_lifetime));
723 lifetime->sadb_lifetime_len =
724 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
725 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
726 lifetime->sadb_lifetime_allocations = x->curlft.packets;
727 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
728 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
729 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
731 addr = (struct sadb_address*) skb_put(skb,
732 sizeof(struct sadb_address)+sockaddr_size);
733 addr->sadb_address_len =
734 (sizeof(struct sadb_address)+sockaddr_size)/
736 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
737 /* "if the ports are non-zero, then the sadb_address_proto field,
738 normally zero, MUST be filled in with the transport
739 protocol's number." - RFC2367 */
740 addr->sadb_address_proto = 0;
741 addr->sadb_address_reserved = 0;
742 if (x->props.family == AF_INET) {
743 addr->sadb_address_prefixlen = 32;
745 sin = (struct sockaddr_in *) (addr + 1);
746 sin->sin_family = AF_INET;
747 sin->sin_addr.s_addr = x->props.saddr.a4;
749 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
751 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
752 else if (x->props.family == AF_INET6) {
753 addr->sadb_address_prefixlen = 128;
755 sin6 = (struct sockaddr_in6 *) (addr + 1);
756 sin6->sin6_family = AF_INET6;
758 sin6->sin6_flowinfo = 0;
759 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
760 sizeof(struct in6_addr));
761 sin6->sin6_scope_id = 0;
768 addr = (struct sadb_address*) skb_put(skb,
769 sizeof(struct sadb_address)+sockaddr_size);
770 addr->sadb_address_len =
771 (sizeof(struct sadb_address)+sockaddr_size)/
773 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
774 addr->sadb_address_proto = 0;
775 addr->sadb_address_prefixlen = 32; /* XXX */
776 addr->sadb_address_reserved = 0;
777 if (x->props.family == AF_INET) {
778 sin = (struct sockaddr_in *) (addr + 1);
779 sin->sin_family = AF_INET;
780 sin->sin_addr.s_addr = x->id.daddr.a4;
782 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
784 if (x->sel.saddr.a4 != x->props.saddr.a4) {
785 addr = (struct sadb_address*) skb_put(skb,
786 sizeof(struct sadb_address)+sockaddr_size);
787 addr->sadb_address_len =
788 (sizeof(struct sadb_address)+sockaddr_size)/
790 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
791 addr->sadb_address_proto =
792 pfkey_proto_from_xfrm(x->sel.proto);
793 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
794 addr->sadb_address_reserved = 0;
796 sin = (struct sockaddr_in *) (addr + 1);
797 sin->sin_family = AF_INET;
798 sin->sin_addr.s_addr = x->sel.saddr.a4;
799 sin->sin_port = x->sel.sport;
800 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
803 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
804 else if (x->props.family == AF_INET6) {
805 addr->sadb_address_prefixlen = 128;
807 sin6 = (struct sockaddr_in6 *) (addr + 1);
808 sin6->sin6_family = AF_INET6;
810 sin6->sin6_flowinfo = 0;
811 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
812 sin6->sin6_scope_id = 0;
814 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
815 sizeof(struct in6_addr))) {
816 addr = (struct sadb_address *) skb_put(skb,
817 sizeof(struct sadb_address)+sockaddr_size);
818 addr->sadb_address_len =
819 (sizeof(struct sadb_address)+sockaddr_size)/
821 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
822 addr->sadb_address_proto =
823 pfkey_proto_from_xfrm(x->sel.proto);
824 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
825 addr->sadb_address_reserved = 0;
827 sin6 = (struct sockaddr_in6 *) (addr + 1);
828 sin6->sin6_family = AF_INET6;
829 sin6->sin6_port = x->sel.sport;
830 sin6->sin6_flowinfo = 0;
831 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
832 sizeof(struct in6_addr));
833 sin6->sin6_scope_id = 0;
841 if (add_keys && auth_key_size) {
842 key = (struct sadb_key *) skb_put(skb,
843 sizeof(struct sadb_key)+auth_key_size);
844 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
846 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
847 key->sadb_key_bits = x->aalg->alg_key_len;
848 key->sadb_key_reserved = 0;
849 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
852 if (add_keys && encrypt_key_size) {
853 key = (struct sadb_key *) skb_put(skb,
854 sizeof(struct sadb_key)+encrypt_key_size);
855 key->sadb_key_len = (sizeof(struct sadb_key) +
856 encrypt_key_size) / sizeof(uint64_t);
857 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
858 key->sadb_key_bits = x->ealg->alg_key_len;
859 key->sadb_key_reserved = 0;
860 memcpy(key + 1, x->ealg->alg_key,
861 (x->ealg->alg_key_len+7)/8);
865 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
866 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
867 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
868 sa2->sadb_x_sa2_mode = x->props.mode + 1;
869 sa2->sadb_x_sa2_reserved1 = 0;
870 sa2->sadb_x_sa2_reserved2 = 0;
871 sa2->sadb_x_sa2_sequence = 0;
872 sa2->sadb_x_sa2_reqid = x->props.reqid;
874 if (natt && natt->encap_type) {
875 struct sadb_x_nat_t_type *n_type;
876 struct sadb_x_nat_t_port *n_port;
879 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
880 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
881 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
882 n_type->sadb_x_nat_t_type_type = natt->encap_type;
883 n_type->sadb_x_nat_t_type_reserved[0] = 0;
884 n_type->sadb_x_nat_t_type_reserved[1] = 0;
885 n_type->sadb_x_nat_t_type_reserved[2] = 0;
888 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
889 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
890 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
891 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
892 n_port->sadb_x_nat_t_port_reserved = 0;
895 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
896 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
897 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
898 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
899 n_port->sadb_x_nat_t_port_reserved = 0;
905 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
908 struct xfrm_state *x;
909 struct sadb_lifetime *lifetime;
911 struct sadb_key *key;
916 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
918 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
919 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
920 return ERR_PTR(-EINVAL);
921 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
922 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
923 return ERR_PTR(-EINVAL);
924 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
925 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
926 return ERR_PTR(-EINVAL);
927 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
928 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
929 return ERR_PTR(-EINVAL);
931 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
933 return ERR_PTR(-EINVAL);
935 /* default error is no buffer space */
940 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
941 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
942 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
943 Therefore, the sadb_sa_state field of all submitted SAs MUST be
944 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
947 However, KAME setkey always uses SADB_SASTATE_LARVAL.
948 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
950 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
951 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
952 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
953 sa->sadb_sa_encrypt > SADB_EALG_MAX)
954 return ERR_PTR(-EINVAL);
955 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
957 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
958 ((key->sadb_key_bits+7) / 8 == 0 ||
959 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
960 return ERR_PTR(-EINVAL);
961 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
963 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
964 ((key->sadb_key_bits+7) / 8 == 0 ||
965 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
966 return ERR_PTR(-EINVAL);
968 x = xfrm_state_alloc();
970 return ERR_PTR(-ENOBUFS);
973 x->id.spi = sa->sadb_sa_spi;
974 x->props.replay_window = sa->sadb_sa_replay;
975 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
976 x->props.flags |= XFRM_STATE_NOECN;
977 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
978 x->props.flags |= XFRM_STATE_DECAP_DSCP;
979 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
980 x->props.flags |= XFRM_STATE_NOPMTUDISC;
982 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
983 if (lifetime != NULL) {
984 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
985 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
986 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
987 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
989 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
990 if (lifetime != NULL) {
991 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
992 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
993 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
994 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
996 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
997 if (sa->sadb_sa_auth) {
999 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1005 keysize = (key->sadb_key_bits + 7) / 8;
1006 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1009 strcpy(x->aalg->alg_name, a->name);
1010 x->aalg->alg_key_len = 0;
1012 x->aalg->alg_key_len = key->sadb_key_bits;
1013 memcpy(x->aalg->alg_key, key+1, keysize);
1015 x->props.aalgo = sa->sadb_sa_auth;
1016 /* x->algo.flags = sa->sadb_sa_flags; */
1018 if (sa->sadb_sa_encrypt) {
1019 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1020 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1025 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1028 strcpy(x->calg->alg_name, a->name);
1029 x->props.calgo = sa->sadb_sa_encrypt;
1032 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1037 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1039 keysize = (key->sadb_key_bits + 7) / 8;
1040 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1043 strcpy(x->ealg->alg_name, a->name);
1044 x->ealg->alg_key_len = 0;
1046 x->ealg->alg_key_len = key->sadb_key_bits;
1047 memcpy(x->ealg->alg_key, key+1, keysize);
1049 x->props.ealgo = sa->sadb_sa_encrypt;
1052 /* x->algo.flags = sa->sadb_sa_flags; */
1054 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1056 if (!x->props.family) {
1057 err = -EAFNOSUPPORT;
1060 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1063 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1064 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1065 x->props.mode = sa2->sadb_x_sa2_mode;
1068 x->props.reqid = sa2->sadb_x_sa2_reqid;
1071 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1072 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1074 /* Nobody uses this, but we try. */
1075 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1076 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1079 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1080 struct sadb_x_nat_t_type* n_type;
1081 struct xfrm_encap_tmpl *natt;
1083 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1088 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1089 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1091 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1092 struct sadb_x_nat_t_port* n_port =
1093 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1094 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1096 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1097 struct sadb_x_nat_t_port* n_port =
1098 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1099 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1103 err = xfrm_init_state(x);
1107 x->km.seq = hdr->sadb_msg_seq;
1111 x->km.state = XFRM_STATE_DEAD;
1113 return ERR_PTR(err);
1116 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1121 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1123 struct sk_buff *resp_skb;
1124 struct sadb_x_sa2 *sa2;
1125 struct sadb_address *saddr, *daddr;
1126 struct sadb_msg *out_hdr;
1127 struct xfrm_state *x = NULL;
1131 unsigned short family;
1132 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1134 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1135 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1138 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1142 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1143 mode = sa2->sadb_x_sa2_mode - 1;
1144 reqid = sa2->sadb_x_sa2_reqid;
1150 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1151 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1153 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1156 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1157 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1159 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1161 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1162 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1167 if (hdr->sadb_msg_seq) {
1168 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1169 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1176 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1181 resp_skb = ERR_PTR(-ENOENT);
1183 spin_lock_bh(&x->lock);
1184 if (x->km.state != XFRM_STATE_DEAD) {
1185 struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1186 u32 min_spi, max_spi;
1188 if (range != NULL) {
1189 min_spi = range->sadb_spirange_min;
1190 max_spi = range->sadb_spirange_max;
1193 max_spi = 0x0fffffff;
1195 xfrm_alloc_spi(x, htonl(min_spi), htonl(max_spi));
1197 resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
1199 spin_unlock_bh(&x->lock);
1201 if (IS_ERR(resp_skb)) {
1203 return PTR_ERR(resp_skb);
1206 out_hdr = (struct sadb_msg *) resp_skb->data;
1207 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1208 out_hdr->sadb_msg_type = SADB_GETSPI;
1209 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1210 out_hdr->sadb_msg_errno = 0;
1211 out_hdr->sadb_msg_reserved = 0;
1212 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1213 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1217 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1222 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1224 struct xfrm_state *x;
1226 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1229 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1232 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1236 spin_lock_bh(&x->lock);
1237 if (x->km.state == XFRM_STATE_ACQ) {
1238 x->km.state = XFRM_STATE_ERROR;
1241 spin_unlock_bh(&x->lock);
1246 static inline int event2poltype(int event)
1249 case XFRM_MSG_DELPOLICY:
1250 return SADB_X_SPDDELETE;
1251 case XFRM_MSG_NEWPOLICY:
1252 return SADB_X_SPDADD;
1253 case XFRM_MSG_UPDPOLICY:
1254 return SADB_X_SPDUPDATE;
1255 case XFRM_MSG_POLEXPIRE:
1256 // return SADB_X_SPDEXPIRE;
1258 printk("pfkey: Unknown policy event %d\n", event);
1265 static inline int event2keytype(int event)
1268 case XFRM_MSG_DELSA:
1270 case XFRM_MSG_NEWSA:
1272 case XFRM_MSG_UPDSA:
1274 case XFRM_MSG_EXPIRE:
1277 printk("pfkey: Unknown SA event %d\n", event);
1285 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1287 struct sk_buff *skb;
1288 struct sadb_msg *hdr;
1291 if (c->event == XFRM_MSG_DELSA)
1294 skb = pfkey_xfrm_state2msg(x, 0, hsc);
1297 return PTR_ERR(skb);
1299 hdr = (struct sadb_msg *) skb->data;
1300 hdr->sadb_msg_version = PF_KEY_V2;
1301 hdr->sadb_msg_type = event2keytype(c->event);
1302 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1303 hdr->sadb_msg_errno = 0;
1304 hdr->sadb_msg_reserved = 0;
1305 hdr->sadb_msg_seq = c->seq;
1306 hdr->sadb_msg_pid = c->pid;
1308 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1313 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1315 struct xfrm_state *x;
1321 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1326 if (hdr->sadb_msg_type == SADB_ADD)
1327 err = xfrm_state_add(x);
1329 err = xfrm_state_update(x);
1332 x->km.state = XFRM_STATE_DEAD;
1337 if (hdr->sadb_msg_type == SADB_ADD)
1338 c.event = XFRM_MSG_NEWSA;
1340 c.event = XFRM_MSG_UPDSA;
1341 c.seq = hdr->sadb_msg_seq;
1342 c.pid = hdr->sadb_msg_pid;
1343 km_state_notify(x, &c);
1349 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1351 struct xfrm_state *x;
1355 if (!ext_hdrs[SADB_EXT_SA-1] ||
1356 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1357 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1360 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1364 if (xfrm_state_kern(x)) {
1369 err = xfrm_state_delete(x);
1375 c.seq = hdr->sadb_msg_seq;
1376 c.pid = hdr->sadb_msg_pid;
1377 c.event = XFRM_MSG_DELSA;
1378 km_state_notify(x, &c);
1384 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1387 struct sk_buff *out_skb;
1388 struct sadb_msg *out_hdr;
1389 struct xfrm_state *x;
1391 if (!ext_hdrs[SADB_EXT_SA-1] ||
1392 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1393 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1396 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1400 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1401 proto = x->id.proto;
1403 if (IS_ERR(out_skb))
1404 return PTR_ERR(out_skb);
1406 out_hdr = (struct sadb_msg *) out_skb->data;
1407 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1408 out_hdr->sadb_msg_type = SADB_DUMP;
1409 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1410 out_hdr->sadb_msg_errno = 0;
1411 out_hdr->sadb_msg_reserved = 0;
1412 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1413 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1414 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1419 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1420 unsigned int __nocast allocation)
1422 struct sk_buff *skb;
1423 struct sadb_msg *hdr;
1424 int len, auth_len, enc_len, i;
1426 auth_len = xfrm_count_auth_supported();
1428 auth_len *= sizeof(struct sadb_alg);
1429 auth_len += sizeof(struct sadb_supported);
1432 enc_len = xfrm_count_enc_supported();
1434 enc_len *= sizeof(struct sadb_alg);
1435 enc_len += sizeof(struct sadb_supported);
1438 len = enc_len + auth_len + sizeof(struct sadb_msg);
1440 skb = alloc_skb(len + 16, allocation);
1444 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1445 pfkey_hdr_dup(hdr, orig);
1446 hdr->sadb_msg_errno = 0;
1447 hdr->sadb_msg_len = len / sizeof(uint64_t);
1450 struct sadb_supported *sp;
1451 struct sadb_alg *ap;
1453 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1454 ap = (struct sadb_alg *) (sp + 1);
1456 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1457 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1459 for (i = 0; ; i++) {
1460 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1463 if (aalg->available)
1469 struct sadb_supported *sp;
1470 struct sadb_alg *ap;
1472 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1473 ap = (struct sadb_alg *) (sp + 1);
1475 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1476 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1478 for (i = 0; ; i++) {
1479 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1482 if (ealg->available)
1491 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1493 struct pfkey_sock *pfk = pfkey_sk(sk);
1494 struct sk_buff *supp_skb;
1496 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1499 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1500 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1502 pfk->registered |= (1<<hdr->sadb_msg_satype);
1507 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1509 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1510 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1515 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1520 static int key_notify_sa_flush(struct km_event *c)
1522 struct sk_buff *skb;
1523 struct sadb_msg *hdr;
1525 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1528 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1529 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1530 hdr->sadb_msg_seq = c->seq;
1531 hdr->sadb_msg_pid = c->pid;
1532 hdr->sadb_msg_version = PF_KEY_V2;
1533 hdr->sadb_msg_errno = (uint8_t) 0;
1534 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1536 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1541 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1546 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1550 xfrm_state_flush(proto);
1551 c.data.proto = proto;
1552 c.seq = hdr->sadb_msg_seq;
1553 c.pid = hdr->sadb_msg_pid;
1554 c.event = XFRM_MSG_FLUSHSA;
1555 km_state_notify(NULL, &c);
1560 struct pfkey_dump_data
1562 struct sk_buff *skb;
1563 struct sadb_msg *hdr;
1567 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1569 struct pfkey_dump_data *data = ptr;
1570 struct sk_buff *out_skb;
1571 struct sadb_msg *out_hdr;
1573 out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1574 if (IS_ERR(out_skb))
1575 return PTR_ERR(out_skb);
1577 out_hdr = (struct sadb_msg *) out_skb->data;
1578 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1579 out_hdr->sadb_msg_type = SADB_DUMP;
1580 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1581 out_hdr->sadb_msg_errno = 0;
1582 out_hdr->sadb_msg_reserved = 0;
1583 out_hdr->sadb_msg_seq = count;
1584 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1585 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1589 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1592 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1594 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1598 return xfrm_state_walk(proto, dump_sa, &data);
1601 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1603 struct pfkey_sock *pfk = pfkey_sk(sk);
1604 int satype = hdr->sadb_msg_satype;
1606 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1607 /* XXX we mangle packet... */
1608 hdr->sadb_msg_errno = 0;
1609 if (satype != 0 && satype != 1)
1611 pfk->promisc = satype;
1613 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1617 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1620 u32 reqid = *(u32*)ptr;
1622 for (i=0; i<xp->xfrm_nr; i++) {
1623 if (xp->xfrm_vec[i].reqid == reqid)
1629 static u32 gen_reqid(void)
1632 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1638 reqid = IPSEC_MANUAL_REQID_MAX+1;
1639 if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
1641 } while (reqid != start);
1646 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1648 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1649 struct sockaddr_in *sin;
1650 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1651 struct sockaddr_in6 *sin6;
1654 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1657 if (rq->sadb_x_ipsecrequest_mode == 0)
1660 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1661 t->mode = rq->sadb_x_ipsecrequest_mode-1;
1662 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1664 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1665 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1666 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1668 if (!t->reqid && !(t->reqid = gen_reqid()))
1672 /* addresses present only in tunnel mode */
1674 switch (xp->family) {
1676 sin = (void*)(rq+1);
1677 if (sin->sin_family != AF_INET)
1679 t->saddr.a4 = sin->sin_addr.s_addr;
1681 if (sin->sin_family != AF_INET)
1683 t->id.daddr.a4 = sin->sin_addr.s_addr;
1685 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1687 sin6 = (void *)(rq+1);
1688 if (sin6->sin6_family != AF_INET6)
1690 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1692 if (sin6->sin6_family != AF_INET6)
1694 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1701 /* No way to set this via kame pfkey */
1702 t->aalgos = t->ealgos = t->calgos = ~0;
1708 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1711 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1712 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1714 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1715 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1717 len -= rq->sadb_x_ipsecrequest_len;
1718 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1723 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1725 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1726 int socklen = (xp->family == AF_INET ?
1727 sizeof(struct sockaddr_in) :
1728 sizeof(struct sockaddr_in6));
1730 return sizeof(struct sadb_msg) +
1731 (sizeof(struct sadb_lifetime) * 3) +
1732 (sizeof(struct sadb_address) * 2) +
1733 (sockaddr_size * 2) +
1734 sizeof(struct sadb_x_policy) +
1735 (xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1739 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1741 struct sk_buff *skb;
1744 size = pfkey_xfrm_policy2msg_size(xp);
1746 skb = alloc_skb(size + 16, GFP_ATOMIC);
1748 return ERR_PTR(-ENOBUFS);
1753 static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1755 struct sadb_msg *hdr;
1756 struct sadb_address *addr;
1757 struct sadb_lifetime *lifetime;
1758 struct sadb_x_policy *pol;
1759 struct sockaddr_in *sin;
1760 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1761 struct sockaddr_in6 *sin6;
1765 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1766 int socklen = (xp->family == AF_INET ?
1767 sizeof(struct sockaddr_in) :
1768 sizeof(struct sockaddr_in6));
1770 size = pfkey_xfrm_policy2msg_size(xp);
1772 /* call should fill header later */
1773 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1774 memset(hdr, 0, size); /* XXX do we need this ? */
1777 addr = (struct sadb_address*) skb_put(skb,
1778 sizeof(struct sadb_address)+sockaddr_size);
1779 addr->sadb_address_len =
1780 (sizeof(struct sadb_address)+sockaddr_size)/
1782 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1783 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1784 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1785 addr->sadb_address_reserved = 0;
1787 if (xp->family == AF_INET) {
1788 sin = (struct sockaddr_in *) (addr + 1);
1789 sin->sin_family = AF_INET;
1790 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1791 sin->sin_port = xp->selector.sport;
1792 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1794 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1795 else if (xp->family == AF_INET6) {
1796 sin6 = (struct sockaddr_in6 *) (addr + 1);
1797 sin6->sin6_family = AF_INET6;
1798 sin6->sin6_port = xp->selector.sport;
1799 sin6->sin6_flowinfo = 0;
1800 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1801 sizeof(struct in6_addr));
1802 sin6->sin6_scope_id = 0;
1809 addr = (struct sadb_address*) skb_put(skb,
1810 sizeof(struct sadb_address)+sockaddr_size);
1811 addr->sadb_address_len =
1812 (sizeof(struct sadb_address)+sockaddr_size)/
1814 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1815 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1816 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1817 addr->sadb_address_reserved = 0;
1818 if (xp->family == AF_INET) {
1819 sin = (struct sockaddr_in *) (addr + 1);
1820 sin->sin_family = AF_INET;
1821 sin->sin_addr.s_addr = xp->selector.daddr.a4;
1822 sin->sin_port = xp->selector.dport;
1823 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1825 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1826 else if (xp->family == AF_INET6) {
1827 sin6 = (struct sockaddr_in6 *) (addr + 1);
1828 sin6->sin6_family = AF_INET6;
1829 sin6->sin6_port = xp->selector.dport;
1830 sin6->sin6_flowinfo = 0;
1831 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
1832 sizeof(struct in6_addr));
1833 sin6->sin6_scope_id = 0;
1840 lifetime = (struct sadb_lifetime *) skb_put(skb,
1841 sizeof(struct sadb_lifetime));
1842 lifetime->sadb_lifetime_len =
1843 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1844 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
1845 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
1846 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
1847 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
1848 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
1850 lifetime = (struct sadb_lifetime *) skb_put(skb,
1851 sizeof(struct sadb_lifetime));
1852 lifetime->sadb_lifetime_len =
1853 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1854 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
1855 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
1856 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
1857 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
1858 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
1860 lifetime = (struct sadb_lifetime *) skb_put(skb,
1861 sizeof(struct sadb_lifetime));
1862 lifetime->sadb_lifetime_len =
1863 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
1864 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
1865 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
1866 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
1867 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
1868 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
1870 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
1871 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
1872 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
1873 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
1874 if (xp->action == XFRM_POLICY_ALLOW) {
1876 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
1878 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
1880 pol->sadb_x_policy_dir = dir+1;
1881 pol->sadb_x_policy_id = xp->index;
1882 pol->sadb_x_policy_priority = xp->priority;
1884 for (i=0; i<xp->xfrm_nr; i++) {
1885 struct sadb_x_ipsecrequest *rq;
1886 struct xfrm_tmpl *t = xp->xfrm_vec + i;
1889 req_size = sizeof(struct sadb_x_ipsecrequest);
1891 req_size += 2*socklen;
1894 rq = (void*)skb_put(skb, req_size);
1895 pol->sadb_x_policy_len += req_size/8;
1896 memset(rq, 0, sizeof(*rq));
1897 rq->sadb_x_ipsecrequest_len = req_size;
1898 rq->sadb_x_ipsecrequest_proto = t->id.proto;
1899 rq->sadb_x_ipsecrequest_mode = t->mode+1;
1900 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1902 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
1904 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
1905 rq->sadb_x_ipsecrequest_reqid = t->reqid;
1907 switch (xp->family) {
1909 sin = (void*)(rq+1);
1910 sin->sin_family = AF_INET;
1911 sin->sin_addr.s_addr = t->saddr.a4;
1913 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1915 sin->sin_family = AF_INET;
1916 sin->sin_addr.s_addr = t->id.daddr.a4;
1918 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1920 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1922 sin6 = (void*)(rq+1);
1923 sin6->sin6_family = AF_INET6;
1924 sin6->sin6_port = 0;
1925 sin6->sin6_flowinfo = 0;
1926 memcpy(&sin6->sin6_addr, t->saddr.a6,
1927 sizeof(struct in6_addr));
1928 sin6->sin6_scope_id = 0;
1931 sin6->sin6_family = AF_INET6;
1932 sin6->sin6_port = 0;
1933 sin6->sin6_flowinfo = 0;
1934 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
1935 sizeof(struct in6_addr));
1936 sin6->sin6_scope_id = 0;
1944 hdr->sadb_msg_len = size / sizeof(uint64_t);
1945 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
1948 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1950 struct sk_buff *out_skb;
1951 struct sadb_msg *out_hdr;
1954 out_skb = pfkey_xfrm_policy2msg_prep(xp);
1955 if (IS_ERR(out_skb)) {
1956 err = PTR_ERR(out_skb);
1959 pfkey_xfrm_policy2msg(out_skb, xp, dir);
1961 out_hdr = (struct sadb_msg *) out_skb->data;
1962 out_hdr->sadb_msg_version = PF_KEY_V2;
1964 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
1965 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
1967 out_hdr->sadb_msg_type = event2poltype(c->event);
1968 out_hdr->sadb_msg_errno = 0;
1969 out_hdr->sadb_msg_seq = c->seq;
1970 out_hdr->sadb_msg_pid = c->pid;
1971 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1977 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1980 struct sadb_lifetime *lifetime;
1981 struct sadb_address *sa;
1982 struct sadb_x_policy *pol;
1983 struct xfrm_policy *xp;
1986 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1987 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
1988 !ext_hdrs[SADB_X_EXT_POLICY-1])
1991 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
1992 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
1994 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
1997 xp = xfrm_policy_alloc(GFP_KERNEL);
2001 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2002 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2003 xp->priority = pol->sadb_x_policy_priority;
2005 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2006 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2011 xp->selector.family = xp->family;
2012 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2013 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2014 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2015 if (xp->selector.sport)
2016 xp->selector.sport_mask = ~0;
2018 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2019 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2020 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2022 /* Amusing, we set this twice. KAME apps appear to set same value
2023 * in both addresses.
2025 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2027 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2028 if (xp->selector.dport)
2029 xp->selector.dport_mask = ~0;
2031 xp->lft.soft_byte_limit = XFRM_INF;
2032 xp->lft.hard_byte_limit = XFRM_INF;
2033 xp->lft.soft_packet_limit = XFRM_INF;
2034 xp->lft.hard_packet_limit = XFRM_INF;
2035 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2036 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2037 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2038 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2039 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2041 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2042 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2043 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2044 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2045 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2048 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2049 (err = parse_ipsecrequests(xp, pol)) < 0)
2052 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2053 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2059 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2060 c.event = XFRM_MSG_UPDPOLICY;
2062 c.event = XFRM_MSG_NEWPOLICY;
2064 c.seq = hdr->sadb_msg_seq;
2065 c.pid = hdr->sadb_msg_pid;
2067 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2076 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2079 struct sadb_address *sa;
2080 struct sadb_x_policy *pol;
2081 struct xfrm_policy *xp;
2082 struct xfrm_selector sel;
2085 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2086 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2087 !ext_hdrs[SADB_X_EXT_POLICY-1])
2090 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2091 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2094 memset(&sel, 0, sizeof(sel));
2096 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2097 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2098 sel.prefixlen_s = sa->sadb_address_prefixlen;
2099 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2100 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2102 sel.sport_mask = ~0;
2104 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2105 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2106 sel.prefixlen_d = sa->sadb_address_prefixlen;
2107 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2108 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2110 sel.dport_mask = ~0;
2112 xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
2118 c.seq = hdr->sadb_msg_seq;
2119 c.pid = hdr->sadb_msg_pid;
2120 c.event = XFRM_MSG_DELPOLICY;
2121 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2127 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2130 struct sk_buff *out_skb;
2131 struct sadb_msg *out_hdr;
2134 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2135 if (IS_ERR(out_skb)) {
2136 err = PTR_ERR(out_skb);
2139 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2141 out_hdr = (struct sadb_msg *) out_skb->data;
2142 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2143 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2144 out_hdr->sadb_msg_satype = 0;
2145 out_hdr->sadb_msg_errno = 0;
2146 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2147 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2148 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2155 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2159 struct sadb_x_policy *pol;
2160 struct xfrm_policy *xp;
2163 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2166 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2167 if (dir >= XFRM_POLICY_MAX)
2170 xp = xfrm_policy_byid(dir, pol->sadb_x_policy_id,
2171 hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2177 c.seq = hdr->sadb_msg_seq;
2178 c.pid = hdr->sadb_msg_pid;
2179 if (hdr->sadb_msg_type == SADB_X_SPDDELETE2) {
2181 c.event = XFRM_MSG_DELPOLICY;
2182 km_policy_notify(xp, dir, &c);
2184 err = key_pol_get_resp(sk, xp, hdr, dir);
2191 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2193 struct pfkey_dump_data *data = ptr;
2194 struct sk_buff *out_skb;
2195 struct sadb_msg *out_hdr;
2197 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2198 if (IS_ERR(out_skb))
2199 return PTR_ERR(out_skb);
2201 pfkey_xfrm_policy2msg(out_skb, xp, dir);
2203 out_hdr = (struct sadb_msg *) out_skb->data;
2204 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2205 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2206 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2207 out_hdr->sadb_msg_errno = 0;
2208 out_hdr->sadb_msg_seq = count;
2209 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2210 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2214 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2216 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2218 return xfrm_policy_walk(dump_sp, &data);
2221 static int key_notify_policy_flush(struct km_event *c)
2223 struct sk_buff *skb_out;
2224 struct sadb_msg *hdr;
2226 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2229 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2230 hdr->sadb_msg_seq = c->seq;
2231 hdr->sadb_msg_pid = c->pid;
2232 hdr->sadb_msg_version = PF_KEY_V2;
2233 hdr->sadb_msg_errno = (uint8_t) 0;
2234 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2235 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2240 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2244 xfrm_policy_flush();
2245 c.event = XFRM_MSG_FLUSHPOLICY;
2246 c.pid = hdr->sadb_msg_pid;
2247 c.seq = hdr->sadb_msg_seq;
2248 km_policy_notify(NULL, 0, &c);
2253 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2254 struct sadb_msg *hdr, void **ext_hdrs);
2255 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2256 [SADB_RESERVED] = pfkey_reserved,
2257 [SADB_GETSPI] = pfkey_getspi,
2258 [SADB_UPDATE] = pfkey_add,
2259 [SADB_ADD] = pfkey_add,
2260 [SADB_DELETE] = pfkey_delete,
2261 [SADB_GET] = pfkey_get,
2262 [SADB_ACQUIRE] = pfkey_acquire,
2263 [SADB_REGISTER] = pfkey_register,
2264 [SADB_EXPIRE] = NULL,
2265 [SADB_FLUSH] = pfkey_flush,
2266 [SADB_DUMP] = pfkey_dump,
2267 [SADB_X_PROMISC] = pfkey_promisc,
2268 [SADB_X_PCHANGE] = NULL,
2269 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2270 [SADB_X_SPDADD] = pfkey_spdadd,
2271 [SADB_X_SPDDELETE] = pfkey_spddelete,
2272 [SADB_X_SPDGET] = pfkey_spdget,
2273 [SADB_X_SPDACQUIRE] = NULL,
2274 [SADB_X_SPDDUMP] = pfkey_spddump,
2275 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2276 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2277 [SADB_X_SPDDELETE2] = pfkey_spdget,
2280 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2282 void *ext_hdrs[SADB_EXT_MAX];
2285 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2286 BROADCAST_PROMISC_ONLY, NULL);
2288 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2289 err = parse_exthdrs(skb, hdr, ext_hdrs);
2292 if (pfkey_funcs[hdr->sadb_msg_type])
2293 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2298 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2300 struct sadb_msg *hdr = NULL;
2302 if (skb->len < sizeof(*hdr)) {
2305 hdr = (struct sadb_msg *) skb->data;
2306 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2307 hdr->sadb_msg_reserved != 0 ||
2308 (hdr->sadb_msg_type <= SADB_RESERVED ||
2309 hdr->sadb_msg_type > SADB_MAX)) {
2312 } else if (hdr->sadb_msg_len != (skb->len /
2313 sizeof(uint64_t)) ||
2314 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2315 sizeof(uint64_t))) {
2325 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2327 return t->aalgos & (1 << d->desc.sadb_alg_id);
2330 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2332 return t->ealgos & (1 << d->desc.sadb_alg_id);
2335 static int count_ah_combs(struct xfrm_tmpl *t)
2339 for (i = 0; ; i++) {
2340 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2343 if (aalg_tmpl_set(t, aalg) && aalg->available)
2344 sz += sizeof(struct sadb_comb);
2346 return sz + sizeof(struct sadb_prop);
2349 static int count_esp_combs(struct xfrm_tmpl *t)
2353 for (i = 0; ; i++) {
2354 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2358 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2361 for (k = 1; ; k++) {
2362 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2366 if (aalg_tmpl_set(t, aalg) && aalg->available)
2367 sz += sizeof(struct sadb_comb);
2370 return sz + sizeof(struct sadb_prop);
2373 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2375 struct sadb_prop *p;
2378 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2379 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2380 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2381 p->sadb_prop_replay = 32;
2382 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2384 for (i = 0; ; i++) {
2385 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2389 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2390 struct sadb_comb *c;
2391 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2392 memset(c, 0, sizeof(*c));
2393 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2394 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2395 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2396 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2397 c->sadb_comb_hard_addtime = 24*60*60;
2398 c->sadb_comb_soft_addtime = 20*60*60;
2399 c->sadb_comb_hard_usetime = 8*60*60;
2400 c->sadb_comb_soft_usetime = 7*60*60;
2405 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2407 struct sadb_prop *p;
2410 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2411 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2412 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2413 p->sadb_prop_replay = 32;
2414 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2417 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2421 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2424 for (k = 1; ; k++) {
2425 struct sadb_comb *c;
2426 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2429 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2431 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2432 memset(c, 0, sizeof(*c));
2433 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2434 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2435 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2436 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2437 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2438 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2439 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2440 c->sadb_comb_hard_addtime = 24*60*60;
2441 c->sadb_comb_soft_addtime = 20*60*60;
2442 c->sadb_comb_hard_usetime = 8*60*60;
2443 c->sadb_comb_soft_usetime = 7*60*60;
2448 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2453 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2455 struct sk_buff *out_skb;
2456 struct sadb_msg *out_hdr;
2460 hard = c->data.hard;
2466 out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
2467 if (IS_ERR(out_skb))
2468 return PTR_ERR(out_skb);
2470 out_hdr = (struct sadb_msg *) out_skb->data;
2471 out_hdr->sadb_msg_version = PF_KEY_V2;
2472 out_hdr->sadb_msg_type = SADB_EXPIRE;
2473 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2474 out_hdr->sadb_msg_errno = 0;
2475 out_hdr->sadb_msg_reserved = 0;
2476 out_hdr->sadb_msg_seq = 0;
2477 out_hdr->sadb_msg_pid = 0;
2479 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2483 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2486 case XFRM_MSG_EXPIRE:
2487 return key_notify_sa_expire(x, c);
2488 case XFRM_MSG_DELSA:
2489 case XFRM_MSG_NEWSA:
2490 case XFRM_MSG_UPDSA:
2491 return key_notify_sa(x, c);
2492 case XFRM_MSG_FLUSHSA:
2493 return key_notify_sa_flush(c);
2495 printk("pfkey: Unknown SA event %d\n", c->event);
2502 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2505 case XFRM_MSG_POLEXPIRE:
2506 return key_notify_policy_expire(xp, c);
2507 case XFRM_MSG_DELPOLICY:
2508 case XFRM_MSG_NEWPOLICY:
2509 case XFRM_MSG_UPDPOLICY:
2510 return key_notify_policy(xp, dir, c);
2511 case XFRM_MSG_FLUSHPOLICY:
2512 return key_notify_policy_flush(c);
2514 printk("pfkey: Unknown policy event %d\n", c->event);
2521 static u32 get_acqseq(void)
2525 static DEFINE_SPINLOCK(acqseq_lock);
2527 spin_lock_bh(&acqseq_lock);
2528 res = (++acqseq ? : ++acqseq);
2529 spin_unlock_bh(&acqseq_lock);
2533 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2535 struct sk_buff *skb;
2536 struct sadb_msg *hdr;
2537 struct sadb_address *addr;
2538 struct sadb_x_policy *pol;
2539 struct sockaddr_in *sin;
2540 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2541 struct sockaddr_in6 *sin6;
2546 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2550 size = sizeof(struct sadb_msg) +
2551 (sizeof(struct sadb_address) * 2) +
2552 (sockaddr_size * 2) +
2553 sizeof(struct sadb_x_policy);
2555 if (x->id.proto == IPPROTO_AH)
2556 size += count_ah_combs(t);
2557 else if (x->id.proto == IPPROTO_ESP)
2558 size += count_esp_combs(t);
2560 skb = alloc_skb(size + 16, GFP_ATOMIC);
2564 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2565 hdr->sadb_msg_version = PF_KEY_V2;
2566 hdr->sadb_msg_type = SADB_ACQUIRE;
2567 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2568 hdr->sadb_msg_len = size / sizeof(uint64_t);
2569 hdr->sadb_msg_errno = 0;
2570 hdr->sadb_msg_reserved = 0;
2571 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2572 hdr->sadb_msg_pid = 0;
2575 addr = (struct sadb_address*) skb_put(skb,
2576 sizeof(struct sadb_address)+sockaddr_size);
2577 addr->sadb_address_len =
2578 (sizeof(struct sadb_address)+sockaddr_size)/
2580 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2581 addr->sadb_address_proto = 0;
2582 addr->sadb_address_reserved = 0;
2583 if (x->props.family == AF_INET) {
2584 addr->sadb_address_prefixlen = 32;
2586 sin = (struct sockaddr_in *) (addr + 1);
2587 sin->sin_family = AF_INET;
2588 sin->sin_addr.s_addr = x->props.saddr.a4;
2590 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2592 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2593 else if (x->props.family == AF_INET6) {
2594 addr->sadb_address_prefixlen = 128;
2596 sin6 = (struct sockaddr_in6 *) (addr + 1);
2597 sin6->sin6_family = AF_INET6;
2598 sin6->sin6_port = 0;
2599 sin6->sin6_flowinfo = 0;
2600 memcpy(&sin6->sin6_addr,
2601 x->props.saddr.a6, sizeof(struct in6_addr));
2602 sin6->sin6_scope_id = 0;
2609 addr = (struct sadb_address*) skb_put(skb,
2610 sizeof(struct sadb_address)+sockaddr_size);
2611 addr->sadb_address_len =
2612 (sizeof(struct sadb_address)+sockaddr_size)/
2614 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2615 addr->sadb_address_proto = 0;
2616 addr->sadb_address_reserved = 0;
2617 if (x->props.family == AF_INET) {
2618 addr->sadb_address_prefixlen = 32;
2620 sin = (struct sockaddr_in *) (addr + 1);
2621 sin->sin_family = AF_INET;
2622 sin->sin_addr.s_addr = x->id.daddr.a4;
2624 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2626 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2627 else if (x->props.family == AF_INET6) {
2628 addr->sadb_address_prefixlen = 128;
2630 sin6 = (struct sockaddr_in6 *) (addr + 1);
2631 sin6->sin6_family = AF_INET6;
2632 sin6->sin6_port = 0;
2633 sin6->sin6_flowinfo = 0;
2634 memcpy(&sin6->sin6_addr,
2635 x->id.daddr.a6, sizeof(struct in6_addr));
2636 sin6->sin6_scope_id = 0;
2642 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2643 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2644 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2645 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2646 pol->sadb_x_policy_dir = dir+1;
2647 pol->sadb_x_policy_id = xp->index;
2649 /* Set sadb_comb's. */
2650 if (x->id.proto == IPPROTO_AH)
2651 dump_ah_combs(skb, t);
2652 else if (x->id.proto == IPPROTO_ESP)
2653 dump_esp_combs(skb, t);
2655 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2658 static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
2659 u8 *data, int len, int *dir)
2661 struct xfrm_policy *xp;
2662 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
2666 if (opt != IP_IPSEC_POLICY) {
2671 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2673 if (opt != IPV6_IPSEC_POLICY) {
2686 if (len < sizeof(struct sadb_x_policy) ||
2687 pol->sadb_x_policy_len*8 > len ||
2688 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
2689 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
2692 xp = xfrm_policy_alloc(GFP_ATOMIC);
2698 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2699 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2701 xp->lft.soft_byte_limit = XFRM_INF;
2702 xp->lft.hard_byte_limit = XFRM_INF;
2703 xp->lft.soft_packet_limit = XFRM_INF;
2704 xp->lft.hard_packet_limit = XFRM_INF;
2705 xp->family = family;
2708 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2709 (*dir = parse_ipsecrequests(xp, pol)) < 0)
2712 *dir = pol->sadb_x_policy_dir-1;
2720 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
2722 struct sk_buff *skb;
2723 struct sadb_msg *hdr;
2725 struct sadb_address *addr;
2726 struct sadb_x_nat_t_port *n_port;
2727 struct sockaddr_in *sin;
2728 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2729 struct sockaddr_in6 *sin6;
2733 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
2734 struct xfrm_encap_tmpl *natt = NULL;
2736 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2748 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2750 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2751 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2754 size = sizeof(struct sadb_msg) +
2755 sizeof(struct sadb_sa) +
2756 (sizeof(struct sadb_address) * 2) +
2757 (sockaddr_size * 2) +
2758 (sizeof(struct sadb_x_nat_t_port) * 2);
2760 skb = alloc_skb(size + 16, GFP_ATOMIC);
2764 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2765 hdr->sadb_msg_version = PF_KEY_V2;
2766 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
2767 hdr->sadb_msg_satype = satype;
2768 hdr->sadb_msg_len = size / sizeof(uint64_t);
2769 hdr->sadb_msg_errno = 0;
2770 hdr->sadb_msg_reserved = 0;
2771 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
2772 hdr->sadb_msg_pid = 0;
2775 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
2776 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
2777 sa->sadb_sa_exttype = SADB_EXT_SA;
2778 sa->sadb_sa_spi = x->id.spi;
2779 sa->sadb_sa_replay = 0;
2780 sa->sadb_sa_state = 0;
2781 sa->sadb_sa_auth = 0;
2782 sa->sadb_sa_encrypt = 0;
2783 sa->sadb_sa_flags = 0;
2785 /* ADDRESS_SRC (old addr) */
2786 addr = (struct sadb_address*)
2787 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2788 addr->sadb_address_len =
2789 (sizeof(struct sadb_address)+sockaddr_size)/
2791 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2792 addr->sadb_address_proto = 0;
2793 addr->sadb_address_reserved = 0;
2794 if (x->props.family == AF_INET) {
2795 addr->sadb_address_prefixlen = 32;
2797 sin = (struct sockaddr_in *) (addr + 1);
2798 sin->sin_family = AF_INET;
2799 sin->sin_addr.s_addr = x->props.saddr.a4;
2801 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2803 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2804 else if (x->props.family == AF_INET6) {
2805 addr->sadb_address_prefixlen = 128;
2807 sin6 = (struct sockaddr_in6 *) (addr + 1);
2808 sin6->sin6_family = AF_INET6;
2809 sin6->sin6_port = 0;
2810 sin6->sin6_flowinfo = 0;
2811 memcpy(&sin6->sin6_addr,
2812 x->props.saddr.a6, sizeof(struct in6_addr));
2813 sin6->sin6_scope_id = 0;
2819 /* NAT_T_SPORT (old port) */
2820 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2821 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2822 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
2823 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
2824 n_port->sadb_x_nat_t_port_reserved = 0;
2826 /* ADDRESS_DST (new addr) */
2827 addr = (struct sadb_address*)
2828 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
2829 addr->sadb_address_len =
2830 (sizeof(struct sadb_address)+sockaddr_size)/
2832 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2833 addr->sadb_address_proto = 0;
2834 addr->sadb_address_reserved = 0;
2835 if (x->props.family == AF_INET) {
2836 addr->sadb_address_prefixlen = 32;
2838 sin = (struct sockaddr_in *) (addr + 1);
2839 sin->sin_family = AF_INET;
2840 sin->sin_addr.s_addr = ipaddr->a4;
2842 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2844 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2845 else if (x->props.family == AF_INET6) {
2846 addr->sadb_address_prefixlen = 128;
2848 sin6 = (struct sockaddr_in6 *) (addr + 1);
2849 sin6->sin6_family = AF_INET6;
2850 sin6->sin6_port = 0;
2851 sin6->sin6_flowinfo = 0;
2852 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
2853 sin6->sin6_scope_id = 0;
2859 /* NAT_T_DPORT (new port) */
2860 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
2861 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
2862 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
2863 n_port->sadb_x_nat_t_port_port = sport;
2864 n_port->sadb_x_nat_t_port_reserved = 0;
2866 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2869 static int pfkey_sendmsg(struct kiocb *kiocb,
2870 struct socket *sock, struct msghdr *msg, size_t len)
2872 struct sock *sk = sock->sk;
2873 struct sk_buff *skb = NULL;
2874 struct sadb_msg *hdr = NULL;
2878 if (msg->msg_flags & MSG_OOB)
2882 if ((unsigned)len > sk->sk_sndbuf - 32)
2886 skb = alloc_skb(len, GFP_KERNEL);
2891 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
2894 hdr = pfkey_get_base_msg(skb, &err);
2898 down(&xfrm_cfg_sem);
2899 err = pfkey_process(sk, skb, hdr);
2903 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
2911 static int pfkey_recvmsg(struct kiocb *kiocb,
2912 struct socket *sock, struct msghdr *msg, size_t len,
2915 struct sock *sk = sock->sk;
2916 struct sk_buff *skb;
2920 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
2923 msg->msg_namelen = 0;
2924 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
2930 msg->msg_flags |= MSG_TRUNC;
2934 skb->h.raw = skb->data;
2935 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2939 sock_recv_timestamp(msg, sk, skb);
2941 err = (flags & MSG_TRUNC) ? skb->len : copied;
2944 skb_free_datagram(sk, skb);
2949 static struct proto_ops pfkey_ops = {
2951 .owner = THIS_MODULE,
2952 /* Operations that make no sense on pfkey sockets. */
2953 .bind = sock_no_bind,
2954 .connect = sock_no_connect,
2955 .socketpair = sock_no_socketpair,
2956 .accept = sock_no_accept,
2957 .getname = sock_no_getname,
2958 .ioctl = sock_no_ioctl,
2959 .listen = sock_no_listen,
2960 .shutdown = sock_no_shutdown,
2961 .setsockopt = sock_no_setsockopt,
2962 .getsockopt = sock_no_getsockopt,
2963 .mmap = sock_no_mmap,
2964 .sendpage = sock_no_sendpage,
2966 /* Now the operations that really occur. */
2967 .release = pfkey_release,
2968 .poll = datagram_poll,
2969 .sendmsg = pfkey_sendmsg,
2970 .recvmsg = pfkey_recvmsg,
2973 static struct net_proto_family pfkey_family_ops = {
2975 .create = pfkey_create,
2976 .owner = THIS_MODULE,
2979 #ifdef CONFIG_PROC_FS
2980 static int pfkey_read_proc(char *buffer, char **start, off_t offset,
2981 int length, int *eof, void *data)
2987 struct hlist_node *node;
2989 len += sprintf(buffer,"sk RefCnt Rmem Wmem User Inode\n");
2991 read_lock(&pfkey_table_lock);
2993 sk_for_each(s, node, &pfkey_table) {
2994 len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
2996 atomic_read(&s->sk_refcnt),
2997 atomic_read(&s->sk_rmem_alloc),
2998 atomic_read(&s->sk_wmem_alloc),
3003 buffer[len++] = '\n';
3010 if(pos > offset + length)
3016 read_unlock(&pfkey_table_lock);
3018 *start = buffer + (offset - begin);
3019 len -= (offset - begin);
3030 static struct xfrm_mgr pfkeyv2_mgr =
3033 .notify = pfkey_send_notify,
3034 .acquire = pfkey_send_acquire,
3035 .compile_policy = pfkey_compile_policy,
3036 .new_mapping = pfkey_send_new_mapping,
3037 .notify_policy = pfkey_send_policy_notify,
3040 static void __exit ipsec_pfkey_exit(void)
3042 xfrm_unregister_km(&pfkeyv2_mgr);
3043 remove_proc_entry("net/pfkey", NULL);
3044 sock_unregister(PF_KEY);
3045 proto_unregister(&key_proto);
3048 static int __init ipsec_pfkey_init(void)
3050 int err = proto_register(&key_proto, 0);
3055 err = sock_register(&pfkey_family_ops);
3057 goto out_unregister_key_proto;
3058 #ifdef CONFIG_PROC_FS
3060 if (create_proc_read_entry("net/pfkey", 0, NULL, pfkey_read_proc, NULL) == NULL)
3061 goto out_sock_unregister;
3063 err = xfrm_register_km(&pfkeyv2_mgr);
3065 goto out_remove_proc_entry;
3068 out_remove_proc_entry:
3069 #ifdef CONFIG_PROC_FS
3070 remove_proc_entry("net/pfkey", NULL);
3071 out_sock_unregister:
3073 sock_unregister(PF_KEY);
3074 out_unregister_key_proto:
3075 proto_unregister(&key_proto);
3079 module_init(ipsec_pfkey_init);
3080 module_exit(ipsec_pfkey_exit);
3081 MODULE_LICENSE("GPL");
3082 MODULE_ALIAS_NETPROTO(PF_KEY);