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/capability.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>
29 #include <net/net_namespace.h>
34 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
35 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 /* List of all pfkey sockets. */
39 static HLIST_HEAD(pfkey_table);
40 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
41 static DEFINE_RWLOCK(pfkey_table_lock);
42 static atomic_t pfkey_table_users = ATOMIC_INIT(0);
44 static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);
47 /* struct sock must be the first member of struct pfkey_sock */
53 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
55 return (struct pfkey_sock *)sk;
58 static void pfkey_sock_destruct(struct sock *sk)
60 skb_queue_purge(&sk->sk_receive_queue);
62 if (!sock_flag(sk, SOCK_DEAD)) {
63 printk("Attempt to release alive pfkey socket: %p\n", sk);
67 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
68 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
70 atomic_dec(&pfkey_socks_nr);
73 static void pfkey_table_grab(void)
75 write_lock_bh(&pfkey_table_lock);
77 if (atomic_read(&pfkey_table_users)) {
78 DECLARE_WAITQUEUE(wait, current);
80 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
82 set_current_state(TASK_UNINTERRUPTIBLE);
83 if (atomic_read(&pfkey_table_users) == 0)
85 write_unlock_bh(&pfkey_table_lock);
87 write_lock_bh(&pfkey_table_lock);
90 __set_current_state(TASK_RUNNING);
91 remove_wait_queue(&pfkey_table_wait, &wait);
95 static __inline__ void pfkey_table_ungrab(void)
97 write_unlock_bh(&pfkey_table_lock);
98 wake_up(&pfkey_table_wait);
101 static __inline__ void pfkey_lock_table(void)
103 /* read_lock() synchronizes us to pfkey_table_grab */
105 read_lock(&pfkey_table_lock);
106 atomic_inc(&pfkey_table_users);
107 read_unlock(&pfkey_table_lock);
110 static __inline__ void pfkey_unlock_table(void)
112 if (atomic_dec_and_test(&pfkey_table_users))
113 wake_up(&pfkey_table_wait);
117 static const struct proto_ops pfkey_ops;
119 static void pfkey_insert(struct sock *sk)
122 sk_add_node(sk, &pfkey_table);
123 pfkey_table_ungrab();
126 static void pfkey_remove(struct sock *sk)
129 sk_del_node_init(sk);
130 pfkey_table_ungrab();
133 static struct proto key_proto = {
135 .owner = THIS_MODULE,
136 .obj_size = sizeof(struct pfkey_sock),
139 static int pfkey_create(struct net *net, struct socket *sock, int protocol)
144 if (net != &init_net)
145 return -EAFNOSUPPORT;
147 if (!capable(CAP_NET_ADMIN))
149 if (sock->type != SOCK_RAW)
150 return -ESOCKTNOSUPPORT;
151 if (protocol != PF_KEY_V2)
152 return -EPROTONOSUPPORT;
155 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
159 sock->ops = &pfkey_ops;
160 sock_init_data(sock, sk);
162 sk->sk_family = PF_KEY;
163 sk->sk_destruct = pfkey_sock_destruct;
165 atomic_inc(&pfkey_socks_nr);
174 static int pfkey_release(struct socket *sock)
176 struct sock *sk = sock->sk;
185 skb_queue_purge(&sk->sk_write_queue);
191 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
192 gfp_t allocation, struct sock *sk)
198 if (atomic_read(&skb->users) != 1) {
199 *skb2 = skb_clone(skb, allocation);
202 atomic_inc(&skb->users);
206 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
208 skb_set_owner_r(*skb2, sk);
209 skb_queue_tail(&sk->sk_receive_queue, *skb2);
210 sk->sk_data_ready(sk, (*skb2)->len);
219 /* Send SKB to all pfkey sockets matching selected criteria. */
220 #define BROADCAST_ALL 0
221 #define BROADCAST_ONE 1
222 #define BROADCAST_REGISTERED 2
223 #define BROADCAST_PROMISC_ONLY 4
224 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
225 int broadcast_flags, struct sock *one_sk)
228 struct hlist_node *node;
229 struct sk_buff *skb2 = NULL;
232 /* XXX Do we need something like netlink_overrun? I think
233 * XXX PF_KEY socket apps will not mind current behavior.
239 sk_for_each(sk, node, &pfkey_table) {
240 struct pfkey_sock *pfk = pfkey_sk(sk);
243 /* Yes, it means that if you are meant to receive this
244 * pfkey message you receive it twice as promiscuous
248 pfkey_broadcast_one(skb, &skb2, allocation, sk);
250 /* the exact target will be processed later */
253 if (broadcast_flags != BROADCAST_ALL) {
254 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
256 if ((broadcast_flags & BROADCAST_REGISTERED) &&
259 if (broadcast_flags & BROADCAST_ONE)
263 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
265 /* Error is cleare after succecful sending to at least one
267 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
270 pfkey_unlock_table();
273 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
281 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
286 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
288 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
289 struct sadb_msg *hdr;
294 /* Woe be to the platform trying to support PFKEY yet
295 * having normal errnos outside the 1-255 range, inclusive.
298 if (err == ERESTARTSYS ||
299 err == ERESTARTNOHAND ||
300 err == ERESTARTNOINTR)
304 BUG_ON(err <= 0 || err >= 256);
306 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
307 pfkey_hdr_dup(hdr, orig);
308 hdr->sadb_msg_errno = (uint8_t) err;
309 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
312 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
317 static u8 sadb_ext_min_len[] = {
318 [SADB_EXT_RESERVED] = (u8) 0,
319 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
320 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
321 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
322 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
323 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
324 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
325 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
326 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
327 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
328 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
329 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
330 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
331 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
332 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
333 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
334 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
335 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
336 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
337 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
338 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
339 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
340 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
341 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
342 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
345 /* Verify sadb_address_{len,prefixlen} against sa_family. */
346 static int verify_address_len(void *p)
348 struct sadb_address *sp = p;
349 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
350 struct sockaddr_in *sin;
351 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
352 struct sockaddr_in6 *sin6;
356 switch (addr->sa_family) {
358 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
359 if (sp->sadb_address_len != len ||
360 sp->sadb_address_prefixlen > 32)
363 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
365 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
366 if (sp->sadb_address_len != len ||
367 sp->sadb_address_prefixlen > 128)
372 /* It is user using kernel to keep track of security
373 * associations for another protocol, such as
374 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
377 * XXX Actually, association/policy database is not yet
378 * XXX able to cope with arbitrary sockaddr families.
379 * XXX When it can, remove this -EINVAL. -DaveM
388 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
390 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
391 sec_ctx->sadb_x_ctx_len,
395 static inline int verify_sec_ctx_len(void *p)
397 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
398 int len = sec_ctx->sadb_x_ctx_len;
403 len = pfkey_sec_ctx_len(sec_ctx);
405 if (sec_ctx->sadb_x_sec_len != len)
411 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
413 struct xfrm_user_sec_ctx *uctx = NULL;
414 int ctx_size = sec_ctx->sadb_x_ctx_len;
416 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
421 uctx->len = pfkey_sec_ctx_len(sec_ctx);
422 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
423 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
424 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
425 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
426 memcpy(uctx + 1, sec_ctx + 1,
432 static int present_and_same_family(struct sadb_address *src,
433 struct sadb_address *dst)
435 struct sockaddr *s_addr, *d_addr;
440 s_addr = (struct sockaddr *)(src + 1);
441 d_addr = (struct sockaddr *)(dst + 1);
442 if (s_addr->sa_family != d_addr->sa_family)
444 if (s_addr->sa_family != AF_INET
445 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
446 && s_addr->sa_family != AF_INET6
454 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
456 char *p = (char *) hdr;
462 struct sadb_ext *ehdr = (struct sadb_ext *) p;
466 ext_len = ehdr->sadb_ext_len;
467 ext_len *= sizeof(uint64_t);
468 ext_type = ehdr->sadb_ext_type;
469 if (ext_len < sizeof(uint64_t) ||
471 ext_type == SADB_EXT_RESERVED)
474 if (ext_type <= SADB_EXT_MAX) {
475 int min = (int) sadb_ext_min_len[ext_type];
478 if (ext_hdrs[ext_type-1] != NULL)
480 if (ext_type == SADB_EXT_ADDRESS_SRC ||
481 ext_type == SADB_EXT_ADDRESS_DST ||
482 ext_type == SADB_EXT_ADDRESS_PROXY ||
483 ext_type == SADB_X_EXT_NAT_T_OA) {
484 if (verify_address_len(p))
487 if (ext_type == SADB_X_EXT_SEC_CTX) {
488 if (verify_sec_ctx_len(p))
491 ext_hdrs[ext_type-1] = p;
501 pfkey_satype2proto(uint8_t satype)
504 case SADB_SATYPE_UNSPEC:
505 return IPSEC_PROTO_ANY;
508 case SADB_SATYPE_ESP:
510 case SADB_X_SATYPE_IPCOMP:
520 pfkey_proto2satype(uint16_t proto)
524 return SADB_SATYPE_AH;
526 return SADB_SATYPE_ESP;
528 return SADB_X_SATYPE_IPCOMP;
536 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
537 * say specifically 'just raw sockets' as we encode them as 255.
540 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
542 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
545 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
547 return (proto ? proto : IPSEC_PROTO_ANY);
550 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
551 xfrm_address_t *xaddr)
553 switch (((struct sockaddr*)(addr + 1))->sa_family) {
556 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
558 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
561 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
562 sizeof(struct in6_addr));
571 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
574 struct sadb_address *addr;
576 unsigned short family;
577 xfrm_address_t *xaddr;
579 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
583 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
587 /* sadb_address_len should be checked by caller */
588 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
592 family = ((struct sockaddr *)(addr + 1))->sa_family;
595 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
597 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
599 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
609 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
612 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
614 pfkey_sockaddr_size(sa_family_t family)
618 return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
619 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
621 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
629 static inline int pfkey_mode_from_xfrm(int mode)
632 case XFRM_MODE_TRANSPORT:
633 return IPSEC_MODE_TRANSPORT;
634 case XFRM_MODE_TUNNEL:
635 return IPSEC_MODE_TUNNEL;
637 return IPSEC_MODE_BEET;
643 static inline int pfkey_mode_to_xfrm(int mode)
646 case IPSEC_MODE_ANY: /*XXX*/
647 case IPSEC_MODE_TRANSPORT:
648 return XFRM_MODE_TRANSPORT;
649 case IPSEC_MODE_TUNNEL:
650 return XFRM_MODE_TUNNEL;
651 case IPSEC_MODE_BEET:
652 return XFRM_MODE_BEET;
658 static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
659 int add_keys, int hsc)
662 struct sadb_msg *hdr;
664 struct sadb_lifetime *lifetime;
665 struct sadb_address *addr;
666 struct sadb_key *key;
667 struct sadb_x_sa2 *sa2;
668 struct sockaddr_in *sin;
669 struct sadb_x_sec_ctx *sec_ctx;
670 struct xfrm_sec_ctx *xfrm_ctx;
672 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
673 struct sockaddr_in6 *sin6;
676 int auth_key_size = 0;
677 int encrypt_key_size = 0;
679 struct xfrm_encap_tmpl *natt = NULL;
682 /* address family check */
683 sockaddr_size = pfkey_sockaddr_size(x->props.family);
685 return ERR_PTR(-EINVAL);
687 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
688 key(AE), (identity(SD),) (sensitivity)> */
689 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
690 sizeof(struct sadb_lifetime) +
691 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
692 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
693 sizeof(struct sadb_address)*2 +
695 sizeof(struct sadb_x_sa2);
697 if ((xfrm_ctx = x->security)) {
698 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
699 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
702 /* identity & sensitivity */
704 if ((x->props.family == AF_INET &&
705 x->sel.saddr.a4 != x->props.saddr.a4)
706 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
707 || (x->props.family == AF_INET6 &&
708 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
711 size += sizeof(struct sadb_address) + sockaddr_size;
714 if (x->aalg && x->aalg->alg_key_len) {
716 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
717 size += sizeof(struct sadb_key) + auth_key_size;
719 if (x->ealg && x->ealg->alg_key_len) {
721 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
722 size += sizeof(struct sadb_key) + encrypt_key_size;
728 if (natt && natt->encap_type) {
729 size += sizeof(struct sadb_x_nat_t_type);
730 size += sizeof(struct sadb_x_nat_t_port);
731 size += sizeof(struct sadb_x_nat_t_port);
734 skb = alloc_skb(size + 16, GFP_ATOMIC);
736 return ERR_PTR(-ENOBUFS);
738 /* call should fill header later */
739 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
740 memset(hdr, 0, size); /* XXX do we need this ? */
741 hdr->sadb_msg_len = size / sizeof(uint64_t);
744 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
745 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
746 sa->sadb_sa_exttype = SADB_EXT_SA;
747 sa->sadb_sa_spi = x->id.spi;
748 sa->sadb_sa_replay = x->props.replay_window;
749 switch (x->km.state) {
750 case XFRM_STATE_VALID:
751 sa->sadb_sa_state = x->km.dying ?
752 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
755 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
758 sa->sadb_sa_state = SADB_SASTATE_DEAD;
761 sa->sadb_sa_auth = 0;
763 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
764 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
766 sa->sadb_sa_encrypt = 0;
767 BUG_ON(x->ealg && x->calg);
769 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
770 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
772 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
774 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
775 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
778 sa->sadb_sa_flags = 0;
779 if (x->props.flags & XFRM_STATE_NOECN)
780 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
781 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
782 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
783 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
784 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
788 lifetime = (struct sadb_lifetime *) skb_put(skb,
789 sizeof(struct sadb_lifetime));
790 lifetime->sadb_lifetime_len =
791 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
792 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
793 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
794 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
795 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
796 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
800 lifetime = (struct sadb_lifetime *) skb_put(skb,
801 sizeof(struct sadb_lifetime));
802 lifetime->sadb_lifetime_len =
803 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
804 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
805 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
806 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
807 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
808 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
811 lifetime = (struct sadb_lifetime *) skb_put(skb,
812 sizeof(struct sadb_lifetime));
813 lifetime->sadb_lifetime_len =
814 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
815 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
816 lifetime->sadb_lifetime_allocations = x->curlft.packets;
817 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
818 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
819 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
821 addr = (struct sadb_address*) skb_put(skb,
822 sizeof(struct sadb_address)+sockaddr_size);
823 addr->sadb_address_len =
824 (sizeof(struct sadb_address)+sockaddr_size)/
826 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
827 /* "if the ports are non-zero, then the sadb_address_proto field,
828 normally zero, MUST be filled in with the transport
829 protocol's number." - RFC2367 */
830 addr->sadb_address_proto = 0;
831 addr->sadb_address_reserved = 0;
832 if (x->props.family == AF_INET) {
833 addr->sadb_address_prefixlen = 32;
835 sin = (struct sockaddr_in *) (addr + 1);
836 sin->sin_family = AF_INET;
837 sin->sin_addr.s_addr = x->props.saddr.a4;
839 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
841 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
842 else if (x->props.family == AF_INET6) {
843 addr->sadb_address_prefixlen = 128;
845 sin6 = (struct sockaddr_in6 *) (addr + 1);
846 sin6->sin6_family = AF_INET6;
848 sin6->sin6_flowinfo = 0;
849 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
850 sizeof(struct in6_addr));
851 sin6->sin6_scope_id = 0;
858 addr = (struct sadb_address*) skb_put(skb,
859 sizeof(struct sadb_address)+sockaddr_size);
860 addr->sadb_address_len =
861 (sizeof(struct sadb_address)+sockaddr_size)/
863 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
864 addr->sadb_address_proto = 0;
865 addr->sadb_address_prefixlen = 32; /* XXX */
866 addr->sadb_address_reserved = 0;
867 if (x->props.family == AF_INET) {
868 sin = (struct sockaddr_in *) (addr + 1);
869 sin->sin_family = AF_INET;
870 sin->sin_addr.s_addr = x->id.daddr.a4;
872 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
874 if (x->sel.saddr.a4 != x->props.saddr.a4) {
875 addr = (struct sadb_address*) skb_put(skb,
876 sizeof(struct sadb_address)+sockaddr_size);
877 addr->sadb_address_len =
878 (sizeof(struct sadb_address)+sockaddr_size)/
880 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
881 addr->sadb_address_proto =
882 pfkey_proto_from_xfrm(x->sel.proto);
883 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
884 addr->sadb_address_reserved = 0;
886 sin = (struct sockaddr_in *) (addr + 1);
887 sin->sin_family = AF_INET;
888 sin->sin_addr.s_addr = x->sel.saddr.a4;
889 sin->sin_port = x->sel.sport;
890 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
893 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
894 else if (x->props.family == AF_INET6) {
895 addr->sadb_address_prefixlen = 128;
897 sin6 = (struct sockaddr_in6 *) (addr + 1);
898 sin6->sin6_family = AF_INET6;
900 sin6->sin6_flowinfo = 0;
901 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
902 sin6->sin6_scope_id = 0;
904 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
905 sizeof(struct in6_addr))) {
906 addr = (struct sadb_address *) skb_put(skb,
907 sizeof(struct sadb_address)+sockaddr_size);
908 addr->sadb_address_len =
909 (sizeof(struct sadb_address)+sockaddr_size)/
911 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
912 addr->sadb_address_proto =
913 pfkey_proto_from_xfrm(x->sel.proto);
914 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
915 addr->sadb_address_reserved = 0;
917 sin6 = (struct sockaddr_in6 *) (addr + 1);
918 sin6->sin6_family = AF_INET6;
919 sin6->sin6_port = x->sel.sport;
920 sin6->sin6_flowinfo = 0;
921 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
922 sizeof(struct in6_addr));
923 sin6->sin6_scope_id = 0;
931 if (add_keys && auth_key_size) {
932 key = (struct sadb_key *) skb_put(skb,
933 sizeof(struct sadb_key)+auth_key_size);
934 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
936 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
937 key->sadb_key_bits = x->aalg->alg_key_len;
938 key->sadb_key_reserved = 0;
939 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
942 if (add_keys && encrypt_key_size) {
943 key = (struct sadb_key *) skb_put(skb,
944 sizeof(struct sadb_key)+encrypt_key_size);
945 key->sadb_key_len = (sizeof(struct sadb_key) +
946 encrypt_key_size) / sizeof(uint64_t);
947 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
948 key->sadb_key_bits = x->ealg->alg_key_len;
949 key->sadb_key_reserved = 0;
950 memcpy(key + 1, x->ealg->alg_key,
951 (x->ealg->alg_key_len+7)/8);
955 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
956 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
957 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
958 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
960 return ERR_PTR(-EINVAL);
962 sa2->sadb_x_sa2_mode = mode;
963 sa2->sadb_x_sa2_reserved1 = 0;
964 sa2->sadb_x_sa2_reserved2 = 0;
965 sa2->sadb_x_sa2_sequence = 0;
966 sa2->sadb_x_sa2_reqid = x->props.reqid;
968 if (natt && natt->encap_type) {
969 struct sadb_x_nat_t_type *n_type;
970 struct sadb_x_nat_t_port *n_port;
973 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
974 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
975 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
976 n_type->sadb_x_nat_t_type_type = natt->encap_type;
977 n_type->sadb_x_nat_t_type_reserved[0] = 0;
978 n_type->sadb_x_nat_t_type_reserved[1] = 0;
979 n_type->sadb_x_nat_t_type_reserved[2] = 0;
982 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
983 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
984 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
985 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
986 n_port->sadb_x_nat_t_port_reserved = 0;
989 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
990 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
991 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
992 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
993 n_port->sadb_x_nat_t_port_reserved = 0;
996 /* security context */
998 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
999 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1000 sec_ctx->sadb_x_sec_len =
1001 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1002 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1003 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1004 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1005 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1006 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1014 static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1016 struct sk_buff *skb;
1018 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1023 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1026 return __pfkey_xfrm_state2msg(x, 0, hsc);
1029 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
1032 struct xfrm_state *x;
1033 struct sadb_lifetime *lifetime;
1035 struct sadb_key *key;
1036 struct sadb_x_sec_ctx *sec_ctx;
1041 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1043 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1044 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1045 return ERR_PTR(-EINVAL);
1046 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1047 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1048 return ERR_PTR(-EINVAL);
1049 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1050 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1051 return ERR_PTR(-EINVAL);
1052 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1053 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1054 return ERR_PTR(-EINVAL);
1056 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1058 return ERR_PTR(-EINVAL);
1060 /* default error is no buffer space */
1065 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1066 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1067 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1068 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1069 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1072 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1073 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1075 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1076 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1077 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1078 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1079 return ERR_PTR(-EINVAL);
1080 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1082 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1083 ((key->sadb_key_bits+7) / 8 == 0 ||
1084 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1085 return ERR_PTR(-EINVAL);
1086 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1088 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1089 ((key->sadb_key_bits+7) / 8 == 0 ||
1090 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1091 return ERR_PTR(-EINVAL);
1093 x = xfrm_state_alloc();
1095 return ERR_PTR(-ENOBUFS);
1097 x->id.proto = proto;
1098 x->id.spi = sa->sadb_sa_spi;
1099 x->props.replay_window = sa->sadb_sa_replay;
1100 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1101 x->props.flags |= XFRM_STATE_NOECN;
1102 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1103 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1104 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1105 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1107 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1108 if (lifetime != NULL) {
1109 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1110 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1111 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1112 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1114 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1115 if (lifetime != NULL) {
1116 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1117 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1118 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1119 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1122 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1123 if (sec_ctx != NULL) {
1124 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1129 err = security_xfrm_state_alloc(x, uctx);
1136 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1137 if (sa->sadb_sa_auth) {
1139 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1145 keysize = (key->sadb_key_bits + 7) / 8;
1146 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1149 strcpy(x->aalg->alg_name, a->name);
1150 x->aalg->alg_key_len = 0;
1152 x->aalg->alg_key_len = key->sadb_key_bits;
1153 memcpy(x->aalg->alg_key, key+1, keysize);
1155 x->props.aalgo = sa->sadb_sa_auth;
1156 /* x->algo.flags = sa->sadb_sa_flags; */
1158 if (sa->sadb_sa_encrypt) {
1159 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1160 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1165 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1168 strcpy(x->calg->alg_name, a->name);
1169 x->props.calgo = sa->sadb_sa_encrypt;
1172 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1177 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1179 keysize = (key->sadb_key_bits + 7) / 8;
1180 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1183 strcpy(x->ealg->alg_name, a->name);
1184 x->ealg->alg_key_len = 0;
1186 x->ealg->alg_key_len = key->sadb_key_bits;
1187 memcpy(x->ealg->alg_key, key+1, keysize);
1189 x->props.ealgo = sa->sadb_sa_encrypt;
1192 /* x->algo.flags = sa->sadb_sa_flags; */
1194 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1196 if (!x->props.family) {
1197 err = -EAFNOSUPPORT;
1200 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1203 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1204 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1205 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1210 x->props.mode = mode;
1211 x->props.reqid = sa2->sadb_x_sa2_reqid;
1214 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1215 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1217 /* Nobody uses this, but we try. */
1218 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1219 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1223 x->sel.family = x->props.family;
1225 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1226 struct sadb_x_nat_t_type* n_type;
1227 struct xfrm_encap_tmpl *natt;
1229 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1234 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1235 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1237 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1238 struct sadb_x_nat_t_port* n_port =
1239 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1240 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1242 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1243 struct sadb_x_nat_t_port* n_port =
1244 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1245 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1249 err = xfrm_init_state(x);
1253 x->km.seq = hdr->sadb_msg_seq;
1257 x->km.state = XFRM_STATE_DEAD;
1259 return ERR_PTR(err);
1262 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1267 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1269 struct sk_buff *resp_skb;
1270 struct sadb_x_sa2 *sa2;
1271 struct sadb_address *saddr, *daddr;
1272 struct sadb_msg *out_hdr;
1273 struct sadb_spirange *range;
1274 struct xfrm_state *x = NULL;
1277 u32 min_spi, max_spi;
1280 unsigned short family;
1281 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1283 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1284 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1287 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1291 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1292 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1295 reqid = sa2->sadb_x_sa2_reqid;
1301 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1302 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1304 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1307 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1308 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1310 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1312 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1313 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1318 if (hdr->sadb_msg_seq) {
1319 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1320 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1327 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1333 max_spi = 0x0fffffff;
1335 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1337 min_spi = range->sadb_spirange_min;
1338 max_spi = range->sadb_spirange_max;
1341 err = xfrm_alloc_spi(x, min_spi, max_spi);
1342 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1344 if (IS_ERR(resp_skb)) {
1346 return PTR_ERR(resp_skb);
1349 out_hdr = (struct sadb_msg *) resp_skb->data;
1350 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1351 out_hdr->sadb_msg_type = SADB_GETSPI;
1352 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1353 out_hdr->sadb_msg_errno = 0;
1354 out_hdr->sadb_msg_reserved = 0;
1355 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1356 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1360 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1365 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1367 struct xfrm_state *x;
1369 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1372 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1375 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1379 spin_lock_bh(&x->lock);
1380 if (x->km.state == XFRM_STATE_ACQ) {
1381 x->km.state = XFRM_STATE_ERROR;
1384 spin_unlock_bh(&x->lock);
1389 static inline int event2poltype(int event)
1392 case XFRM_MSG_DELPOLICY:
1393 return SADB_X_SPDDELETE;
1394 case XFRM_MSG_NEWPOLICY:
1395 return SADB_X_SPDADD;
1396 case XFRM_MSG_UPDPOLICY:
1397 return SADB_X_SPDUPDATE;
1398 case XFRM_MSG_POLEXPIRE:
1399 // return SADB_X_SPDEXPIRE;
1401 printk("pfkey: Unknown policy event %d\n", event);
1408 static inline int event2keytype(int event)
1411 case XFRM_MSG_DELSA:
1413 case XFRM_MSG_NEWSA:
1415 case XFRM_MSG_UPDSA:
1417 case XFRM_MSG_EXPIRE:
1420 printk("pfkey: Unknown SA event %d\n", event);
1428 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1430 struct sk_buff *skb;
1431 struct sadb_msg *hdr;
1433 skb = pfkey_xfrm_state2msg(x);
1436 return PTR_ERR(skb);
1438 hdr = (struct sadb_msg *) skb->data;
1439 hdr->sadb_msg_version = PF_KEY_V2;
1440 hdr->sadb_msg_type = event2keytype(c->event);
1441 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1442 hdr->sadb_msg_errno = 0;
1443 hdr->sadb_msg_reserved = 0;
1444 hdr->sadb_msg_seq = c->seq;
1445 hdr->sadb_msg_pid = c->pid;
1447 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1452 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1454 struct xfrm_state *x;
1458 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1463 if (hdr->sadb_msg_type == SADB_ADD)
1464 err = xfrm_state_add(x);
1466 err = xfrm_state_update(x);
1468 xfrm_audit_state_add(x, err ? 0 : 1,
1469 audit_get_loginuid(current), 0);
1472 x->km.state = XFRM_STATE_DEAD;
1473 __xfrm_state_put(x);
1477 if (hdr->sadb_msg_type == SADB_ADD)
1478 c.event = XFRM_MSG_NEWSA;
1480 c.event = XFRM_MSG_UPDSA;
1481 c.seq = hdr->sadb_msg_seq;
1482 c.pid = hdr->sadb_msg_pid;
1483 km_state_notify(x, &c);
1489 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1491 struct xfrm_state *x;
1495 if (!ext_hdrs[SADB_EXT_SA-1] ||
1496 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1497 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1500 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1504 if ((err = security_xfrm_state_delete(x)))
1507 if (xfrm_state_kern(x)) {
1512 err = xfrm_state_delete(x);
1517 c.seq = hdr->sadb_msg_seq;
1518 c.pid = hdr->sadb_msg_pid;
1519 c.event = XFRM_MSG_DELSA;
1520 km_state_notify(x, &c);
1522 xfrm_audit_state_delete(x, err ? 0 : 1,
1523 audit_get_loginuid(current), 0);
1529 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1532 struct sk_buff *out_skb;
1533 struct sadb_msg *out_hdr;
1534 struct xfrm_state *x;
1536 if (!ext_hdrs[SADB_EXT_SA-1] ||
1537 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1538 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1541 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1545 out_skb = pfkey_xfrm_state2msg(x);
1546 proto = x->id.proto;
1548 if (IS_ERR(out_skb))
1549 return PTR_ERR(out_skb);
1551 out_hdr = (struct sadb_msg *) out_skb->data;
1552 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1553 out_hdr->sadb_msg_type = SADB_GET;
1554 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1555 out_hdr->sadb_msg_errno = 0;
1556 out_hdr->sadb_msg_reserved = 0;
1557 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1558 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1559 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1564 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1567 struct sk_buff *skb;
1568 struct sadb_msg *hdr;
1569 int len, auth_len, enc_len, i;
1571 auth_len = xfrm_count_auth_supported();
1573 auth_len *= sizeof(struct sadb_alg);
1574 auth_len += sizeof(struct sadb_supported);
1577 enc_len = xfrm_count_enc_supported();
1579 enc_len *= sizeof(struct sadb_alg);
1580 enc_len += sizeof(struct sadb_supported);
1583 len = enc_len + auth_len + sizeof(struct sadb_msg);
1585 skb = alloc_skb(len + 16, allocation);
1589 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1590 pfkey_hdr_dup(hdr, orig);
1591 hdr->sadb_msg_errno = 0;
1592 hdr->sadb_msg_len = len / sizeof(uint64_t);
1595 struct sadb_supported *sp;
1596 struct sadb_alg *ap;
1598 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1599 ap = (struct sadb_alg *) (sp + 1);
1601 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1602 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1604 for (i = 0; ; i++) {
1605 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1608 if (aalg->available)
1614 struct sadb_supported *sp;
1615 struct sadb_alg *ap;
1617 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1618 ap = (struct sadb_alg *) (sp + 1);
1620 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1621 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1623 for (i = 0; ; i++) {
1624 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1627 if (ealg->available)
1636 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1638 struct pfkey_sock *pfk = pfkey_sk(sk);
1639 struct sk_buff *supp_skb;
1641 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1644 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1645 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1647 pfk->registered |= (1<<hdr->sadb_msg_satype);
1652 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1654 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1655 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1660 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1665 static int key_notify_sa_flush(struct km_event *c)
1667 struct sk_buff *skb;
1668 struct sadb_msg *hdr;
1670 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1673 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1674 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1675 hdr->sadb_msg_type = SADB_FLUSH;
1676 hdr->sadb_msg_seq = c->seq;
1677 hdr->sadb_msg_pid = c->pid;
1678 hdr->sadb_msg_version = PF_KEY_V2;
1679 hdr->sadb_msg_errno = (uint8_t) 0;
1680 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1682 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1687 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1691 struct xfrm_audit audit_info;
1694 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1698 audit_info.loginuid = audit_get_loginuid(current);
1699 audit_info.secid = 0;
1700 err = xfrm_state_flush(proto, &audit_info);
1703 c.data.proto = proto;
1704 c.seq = hdr->sadb_msg_seq;
1705 c.pid = hdr->sadb_msg_pid;
1706 c.event = XFRM_MSG_FLUSHSA;
1707 km_state_notify(NULL, &c);
1712 struct pfkey_dump_data
1714 struct sk_buff *skb;
1715 struct sadb_msg *hdr;
1719 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1721 struct pfkey_dump_data *data = ptr;
1722 struct sk_buff *out_skb;
1723 struct sadb_msg *out_hdr;
1725 out_skb = pfkey_xfrm_state2msg(x);
1726 if (IS_ERR(out_skb))
1727 return PTR_ERR(out_skb);
1729 out_hdr = (struct sadb_msg *) out_skb->data;
1730 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
1731 out_hdr->sadb_msg_type = SADB_DUMP;
1732 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1733 out_hdr->sadb_msg_errno = 0;
1734 out_hdr->sadb_msg_reserved = 0;
1735 out_hdr->sadb_msg_seq = count;
1736 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
1737 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
1741 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1744 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
1745 struct xfrm_state_walk walk;
1748 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1752 xfrm_state_walk_init(&walk, proto);
1753 rc = xfrm_state_walk(&walk, dump_sa, &data);
1754 xfrm_state_walk_done(&walk);
1759 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1761 struct pfkey_sock *pfk = pfkey_sk(sk);
1762 int satype = hdr->sadb_msg_satype;
1764 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1765 /* XXX we mangle packet... */
1766 hdr->sadb_msg_errno = 0;
1767 if (satype != 0 && satype != 1)
1769 pfk->promisc = satype;
1771 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1775 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1778 u32 reqid = *(u32*)ptr;
1780 for (i=0; i<xp->xfrm_nr; i++) {
1781 if (xp->xfrm_vec[i].reqid == reqid)
1787 static u32 gen_reqid(void)
1789 struct xfrm_policy_walk walk;
1792 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1798 reqid = IPSEC_MANUAL_REQID_MAX+1;
1799 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1800 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1801 xfrm_policy_walk_done(&walk);
1804 } while (reqid != start);
1809 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1811 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1812 struct sockaddr_in *sin;
1813 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1814 struct sockaddr_in6 *sin6;
1818 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1821 if (rq->sadb_x_ipsecrequest_mode == 0)
1824 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1825 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1828 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1830 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1831 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1832 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1834 if (!t->reqid && !(t->reqid = gen_reqid()))
1838 /* addresses present only in tunnel mode */
1839 if (t->mode == XFRM_MODE_TUNNEL) {
1840 struct sockaddr *sa;
1841 sa = (struct sockaddr *)(rq+1);
1842 switch(sa->sa_family) {
1844 sin = (struct sockaddr_in*)sa;
1845 t->saddr.a4 = sin->sin_addr.s_addr;
1847 if (sin->sin_family != AF_INET)
1849 t->id.daddr.a4 = sin->sin_addr.s_addr;
1851 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1853 sin6 = (struct sockaddr_in6*)sa;
1854 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1856 if (sin6->sin6_family != AF_INET6)
1858 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1864 t->encap_family = sa->sa_family;
1866 t->encap_family = xp->family;
1868 /* No way to set this via kame pfkey */
1869 t->aalgos = t->ealgos = t->calgos = ~0;
1875 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1878 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1879 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1881 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1882 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1884 len -= rq->sadb_x_ipsecrequest_len;
1885 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1890 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1892 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1895 int len = sizeof(struct sadb_x_sec_ctx);
1896 len += xfrm_ctx->ctx_len;
1897 return PFKEY_ALIGN8(len);
1902 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1904 struct xfrm_tmpl *t;
1905 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1909 for (i=0; i<xp->xfrm_nr; i++) {
1910 t = xp->xfrm_vec + i;
1911 socklen += (t->encap_family == AF_INET ?
1912 sizeof(struct sockaddr_in) :
1913 sizeof(struct sockaddr_in6));
1916 return sizeof(struct sadb_msg) +
1917 (sizeof(struct sadb_lifetime) * 3) +
1918 (sizeof(struct sadb_address) * 2) +
1919 (sockaddr_size * 2) +
1920 sizeof(struct sadb_x_policy) +
1921 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1923 pfkey_xfrm_policy2sec_ctx_size(xp);
1926 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1928 struct sk_buff *skb;
1931 size = pfkey_xfrm_policy2msg_size(xp);
1933 skb = alloc_skb(size + 16, GFP_ATOMIC);
1935 return ERR_PTR(-ENOBUFS);
1940 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1942 struct sadb_msg *hdr;
1943 struct sadb_address *addr;
1944 struct sadb_lifetime *lifetime;
1945 struct sadb_x_policy *pol;
1946 struct sockaddr_in *sin;
1947 struct sadb_x_sec_ctx *sec_ctx;
1948 struct xfrm_sec_ctx *xfrm_ctx;
1949 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1950 struct sockaddr_in6 *sin6;
1954 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1955 int socklen = (xp->family == AF_INET ?
1956 sizeof(struct sockaddr_in) :
1957 sizeof(struct sockaddr_in6));
1959 size = pfkey_xfrm_policy2msg_size(xp);
1961 /* call should fill header later */
1962 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1963 memset(hdr, 0, size); /* XXX do we need this ? */
1966 addr = (struct sadb_address*) skb_put(skb,
1967 sizeof(struct sadb_address)+sockaddr_size);
1968 addr->sadb_address_len =
1969 (sizeof(struct sadb_address)+sockaddr_size)/
1971 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1972 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1973 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1974 addr->sadb_address_reserved = 0;
1976 if (xp->family == AF_INET) {
1977 sin = (struct sockaddr_in *) (addr + 1);
1978 sin->sin_family = AF_INET;
1979 sin->sin_addr.s_addr = xp->selector.saddr.a4;
1980 sin->sin_port = xp->selector.sport;
1981 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1983 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1984 else if (xp->family == AF_INET6) {
1985 sin6 = (struct sockaddr_in6 *) (addr + 1);
1986 sin6->sin6_family = AF_INET6;
1987 sin6->sin6_port = xp->selector.sport;
1988 sin6->sin6_flowinfo = 0;
1989 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1990 sizeof(struct in6_addr));
1991 sin6->sin6_scope_id = 0;
1998 addr = (struct sadb_address*) skb_put(skb,
1999 sizeof(struct sadb_address)+sockaddr_size);
2000 addr->sadb_address_len =
2001 (sizeof(struct sadb_address)+sockaddr_size)/
2003 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2004 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2005 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2006 addr->sadb_address_reserved = 0;
2007 if (xp->family == AF_INET) {
2008 sin = (struct sockaddr_in *) (addr + 1);
2009 sin->sin_family = AF_INET;
2010 sin->sin_addr.s_addr = xp->selector.daddr.a4;
2011 sin->sin_port = xp->selector.dport;
2012 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2014 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2015 else if (xp->family == AF_INET6) {
2016 sin6 = (struct sockaddr_in6 *) (addr + 1);
2017 sin6->sin6_family = AF_INET6;
2018 sin6->sin6_port = xp->selector.dport;
2019 sin6->sin6_flowinfo = 0;
2020 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
2021 sizeof(struct in6_addr));
2022 sin6->sin6_scope_id = 0;
2029 lifetime = (struct sadb_lifetime *) skb_put(skb,
2030 sizeof(struct sadb_lifetime));
2031 lifetime->sadb_lifetime_len =
2032 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2033 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2034 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2035 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2036 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2037 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2039 lifetime = (struct sadb_lifetime *) skb_put(skb,
2040 sizeof(struct sadb_lifetime));
2041 lifetime->sadb_lifetime_len =
2042 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2043 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2044 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2045 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2046 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2047 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2049 lifetime = (struct sadb_lifetime *) skb_put(skb,
2050 sizeof(struct sadb_lifetime));
2051 lifetime->sadb_lifetime_len =
2052 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2053 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2054 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2055 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2056 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2057 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2059 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2060 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2061 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2062 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2063 if (xp->action == XFRM_POLICY_ALLOW) {
2065 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2067 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2069 pol->sadb_x_policy_dir = dir+1;
2070 pol->sadb_x_policy_id = xp->index;
2071 pol->sadb_x_policy_priority = xp->priority;
2073 for (i=0; i<xp->xfrm_nr; i++) {
2074 struct sadb_x_ipsecrequest *rq;
2075 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2079 req_size = sizeof(struct sadb_x_ipsecrequest);
2080 if (t->mode == XFRM_MODE_TUNNEL)
2081 req_size += ((t->encap_family == AF_INET ?
2082 sizeof(struct sockaddr_in) :
2083 sizeof(struct sockaddr_in6)) * 2);
2086 rq = (void*)skb_put(skb, req_size);
2087 pol->sadb_x_policy_len += req_size/8;
2088 memset(rq, 0, sizeof(*rq));
2089 rq->sadb_x_ipsecrequest_len = req_size;
2090 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2091 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2093 rq->sadb_x_ipsecrequest_mode = mode;
2094 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2096 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2098 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2099 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2100 if (t->mode == XFRM_MODE_TUNNEL) {
2101 switch (t->encap_family) {
2103 sin = (void*)(rq+1);
2104 sin->sin_family = AF_INET;
2105 sin->sin_addr.s_addr = t->saddr.a4;
2107 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2109 sin->sin_family = AF_INET;
2110 sin->sin_addr.s_addr = t->id.daddr.a4;
2112 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2114 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2116 sin6 = (void*)(rq+1);
2117 sin6->sin6_family = AF_INET6;
2118 sin6->sin6_port = 0;
2119 sin6->sin6_flowinfo = 0;
2120 memcpy(&sin6->sin6_addr, t->saddr.a6,
2121 sizeof(struct in6_addr));
2122 sin6->sin6_scope_id = 0;
2125 sin6->sin6_family = AF_INET6;
2126 sin6->sin6_port = 0;
2127 sin6->sin6_flowinfo = 0;
2128 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2129 sizeof(struct in6_addr));
2130 sin6->sin6_scope_id = 0;
2139 /* security context */
2140 if ((xfrm_ctx = xp->security)) {
2141 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2143 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2144 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2145 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2146 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2147 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2148 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2149 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2153 hdr->sadb_msg_len = size / sizeof(uint64_t);
2154 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2159 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2161 struct sk_buff *out_skb;
2162 struct sadb_msg *out_hdr;
2165 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2166 if (IS_ERR(out_skb)) {
2167 err = PTR_ERR(out_skb);
2170 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2174 out_hdr = (struct sadb_msg *) out_skb->data;
2175 out_hdr->sadb_msg_version = PF_KEY_V2;
2177 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2178 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2180 out_hdr->sadb_msg_type = event2poltype(c->event);
2181 out_hdr->sadb_msg_errno = 0;
2182 out_hdr->sadb_msg_seq = c->seq;
2183 out_hdr->sadb_msg_pid = c->pid;
2184 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2190 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2193 struct sadb_lifetime *lifetime;
2194 struct sadb_address *sa;
2195 struct sadb_x_policy *pol;
2196 struct xfrm_policy *xp;
2198 struct sadb_x_sec_ctx *sec_ctx;
2200 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2201 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2202 !ext_hdrs[SADB_X_EXT_POLICY-1])
2205 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2206 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2208 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2211 xp = xfrm_policy_alloc(GFP_KERNEL);
2215 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2216 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2217 xp->priority = pol->sadb_x_policy_priority;
2219 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2220 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2225 xp->selector.family = xp->family;
2226 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2227 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2228 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2229 if (xp->selector.sport)
2230 xp->selector.sport_mask = htons(0xffff);
2232 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2233 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2234 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2236 /* Amusing, we set this twice. KAME apps appear to set same value
2237 * in both addresses.
2239 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2241 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2242 if (xp->selector.dport)
2243 xp->selector.dport_mask = htons(0xffff);
2245 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2246 if (sec_ctx != NULL) {
2247 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2254 err = security_xfrm_policy_alloc(xp, uctx);
2261 xp->lft.soft_byte_limit = XFRM_INF;
2262 xp->lft.hard_byte_limit = XFRM_INF;
2263 xp->lft.soft_packet_limit = XFRM_INF;
2264 xp->lft.hard_packet_limit = XFRM_INF;
2265 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2266 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2267 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2268 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2269 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2271 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2272 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2273 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2274 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2275 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2278 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2279 (err = parse_ipsecrequests(xp, pol)) < 0)
2282 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2283 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2285 xfrm_audit_policy_add(xp, err ? 0 : 1,
2286 audit_get_loginuid(current), 0);
2291 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2292 c.event = XFRM_MSG_UPDPOLICY;
2294 c.event = XFRM_MSG_NEWPOLICY;
2296 c.seq = hdr->sadb_msg_seq;
2297 c.pid = hdr->sadb_msg_pid;
2299 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2305 xfrm_policy_destroy(xp);
2309 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2312 struct sadb_address *sa;
2313 struct sadb_x_policy *pol;
2314 struct xfrm_policy *xp, tmp;
2315 struct xfrm_selector sel;
2317 struct sadb_x_sec_ctx *sec_ctx;
2319 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2320 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2321 !ext_hdrs[SADB_X_EXT_POLICY-1])
2324 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2325 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2328 memset(&sel, 0, sizeof(sel));
2330 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2331 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2332 sel.prefixlen_s = sa->sadb_address_prefixlen;
2333 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2334 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2336 sel.sport_mask = htons(0xffff);
2338 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2339 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2340 sel.prefixlen_d = sa->sadb_address_prefixlen;
2341 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2342 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2344 sel.dport_mask = htons(0xffff);
2346 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2347 memset(&tmp, 0, sizeof(struct xfrm_policy));
2349 if (sec_ctx != NULL) {
2350 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2355 err = security_xfrm_policy_alloc(&tmp, uctx);
2362 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN, pol->sadb_x_policy_dir-1,
2363 &sel, tmp.security, 1, &err);
2364 security_xfrm_policy_free(&tmp);
2369 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2370 audit_get_loginuid(current), 0);
2375 c.seq = hdr->sadb_msg_seq;
2376 c.pid = hdr->sadb_msg_pid;
2377 c.event = XFRM_MSG_DELPOLICY;
2378 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2385 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2388 struct sk_buff *out_skb;
2389 struct sadb_msg *out_hdr;
2392 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2393 if (IS_ERR(out_skb)) {
2394 err = PTR_ERR(out_skb);
2397 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2401 out_hdr = (struct sadb_msg *) out_skb->data;
2402 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2403 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2404 out_hdr->sadb_msg_satype = 0;
2405 out_hdr->sadb_msg_errno = 0;
2406 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2407 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2408 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2415 #ifdef CONFIG_NET_KEY_MIGRATE
2416 static int pfkey_sockaddr_pair_size(sa_family_t family)
2420 return PFKEY_ALIGN8(sizeof(struct sockaddr_in) * 2);
2421 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2423 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6) * 2);
2431 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2432 xfrm_address_t *saddr, xfrm_address_t *daddr,
2435 struct sockaddr *sa = (struct sockaddr *)(rq + 1);
2436 if (rq->sadb_x_ipsecrequest_len <
2437 pfkey_sockaddr_pair_size(sa->sa_family))
2440 switch (sa->sa_family) {
2443 struct sockaddr_in *sin;
2444 sin = (struct sockaddr_in *)sa;
2445 if ((sin+1)->sin_family != AF_INET)
2447 memcpy(&saddr->a4, &sin->sin_addr, sizeof(saddr->a4));
2449 memcpy(&daddr->a4, &sin->sin_addr, sizeof(daddr->a4));
2453 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2456 struct sockaddr_in6 *sin6;
2457 sin6 = (struct sockaddr_in6 *)sa;
2458 if ((sin6+1)->sin6_family != AF_INET6)
2460 memcpy(&saddr->a6, &sin6->sin6_addr,
2463 memcpy(&daddr->a6, &sin6->sin6_addr,
2476 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2477 struct xfrm_migrate *m)
2480 struct sadb_x_ipsecrequest *rq2;
2483 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2484 len < rq1->sadb_x_ipsecrequest_len)
2488 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2493 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2494 len -= rq1->sadb_x_ipsecrequest_len;
2496 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2497 len < rq2->sadb_x_ipsecrequest_len)
2501 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2506 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2507 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2508 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2511 m->proto = rq1->sadb_x_ipsecrequest_proto;
2512 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2515 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2517 return ((int)(rq1->sadb_x_ipsecrequest_len +
2518 rq2->sadb_x_ipsecrequest_len));
2521 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2522 struct sadb_msg *hdr, void **ext_hdrs)
2524 int i, len, ret, err = -EINVAL;
2526 struct sadb_address *sa;
2527 struct sadb_x_policy *pol;
2528 struct sadb_x_ipsecrequest *rq;
2529 struct xfrm_selector sel;
2530 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2532 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2533 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2534 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2539 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2545 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2550 dir = pol->sadb_x_policy_dir - 1;
2551 memset(&sel, 0, sizeof(sel));
2553 /* set source address info of selector */
2554 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2555 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2556 sel.prefixlen_s = sa->sadb_address_prefixlen;
2557 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2558 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2560 sel.sport_mask = htons(0xffff);
2562 /* set destination address info of selector */
2563 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2564 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2565 sel.prefixlen_d = sa->sadb_address_prefixlen;
2566 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2567 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2569 sel.dport_mask = htons(0xffff);
2571 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2573 /* extract ipsecrequests */
2575 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2577 while (len > 0 && i < XFRM_MAX_DEPTH) {
2578 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2583 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2589 if (!i || len > 0) {
2594 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2600 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2601 struct sadb_msg *hdr, void **ext_hdrs)
2603 return -ENOPROTOOPT;
2608 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2611 int err = 0, delete;
2612 struct sadb_x_policy *pol;
2613 struct xfrm_policy *xp;
2616 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2619 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2620 if (dir >= XFRM_POLICY_MAX)
2623 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2624 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
2630 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2631 audit_get_loginuid(current), 0);
2635 c.seq = hdr->sadb_msg_seq;
2636 c.pid = hdr->sadb_msg_pid;
2638 c.event = XFRM_MSG_DELPOLICY;
2639 km_policy_notify(xp, dir, &c);
2641 err = key_pol_get_resp(sk, xp, hdr, dir);
2649 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2651 struct pfkey_dump_data *data = ptr;
2652 struct sk_buff *out_skb;
2653 struct sadb_msg *out_hdr;
2656 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2657 if (IS_ERR(out_skb))
2658 return PTR_ERR(out_skb);
2660 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2664 out_hdr = (struct sadb_msg *) out_skb->data;
2665 out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
2666 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2667 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2668 out_hdr->sadb_msg_errno = 0;
2669 out_hdr->sadb_msg_seq = count;
2670 out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
2671 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
2675 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2677 struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };
2678 struct xfrm_policy_walk walk;
2681 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
2682 rc = xfrm_policy_walk(&walk, dump_sp, &data);
2683 xfrm_policy_walk_done(&walk);
2688 static int key_notify_policy_flush(struct km_event *c)
2690 struct sk_buff *skb_out;
2691 struct sadb_msg *hdr;
2693 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2696 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2697 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2698 hdr->sadb_msg_seq = c->seq;
2699 hdr->sadb_msg_pid = c->pid;
2700 hdr->sadb_msg_version = PF_KEY_V2;
2701 hdr->sadb_msg_errno = (uint8_t) 0;
2702 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2703 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2708 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2711 struct xfrm_audit audit_info;
2714 audit_info.loginuid = audit_get_loginuid(current);
2715 audit_info.secid = 0;
2716 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2719 c.data.type = XFRM_POLICY_TYPE_MAIN;
2720 c.event = XFRM_MSG_FLUSHPOLICY;
2721 c.pid = hdr->sadb_msg_pid;
2722 c.seq = hdr->sadb_msg_seq;
2723 km_policy_notify(NULL, 0, &c);
2728 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2729 struct sadb_msg *hdr, void **ext_hdrs);
2730 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2731 [SADB_RESERVED] = pfkey_reserved,
2732 [SADB_GETSPI] = pfkey_getspi,
2733 [SADB_UPDATE] = pfkey_add,
2734 [SADB_ADD] = pfkey_add,
2735 [SADB_DELETE] = pfkey_delete,
2736 [SADB_GET] = pfkey_get,
2737 [SADB_ACQUIRE] = pfkey_acquire,
2738 [SADB_REGISTER] = pfkey_register,
2739 [SADB_EXPIRE] = NULL,
2740 [SADB_FLUSH] = pfkey_flush,
2741 [SADB_DUMP] = pfkey_dump,
2742 [SADB_X_PROMISC] = pfkey_promisc,
2743 [SADB_X_PCHANGE] = NULL,
2744 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2745 [SADB_X_SPDADD] = pfkey_spdadd,
2746 [SADB_X_SPDDELETE] = pfkey_spddelete,
2747 [SADB_X_SPDGET] = pfkey_spdget,
2748 [SADB_X_SPDACQUIRE] = NULL,
2749 [SADB_X_SPDDUMP] = pfkey_spddump,
2750 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2751 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2752 [SADB_X_SPDDELETE2] = pfkey_spdget,
2753 [SADB_X_MIGRATE] = pfkey_migrate,
2756 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2758 void *ext_hdrs[SADB_EXT_MAX];
2761 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2762 BROADCAST_PROMISC_ONLY, NULL);
2764 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2765 err = parse_exthdrs(skb, hdr, ext_hdrs);
2768 if (pfkey_funcs[hdr->sadb_msg_type])
2769 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2774 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2776 struct sadb_msg *hdr = NULL;
2778 if (skb->len < sizeof(*hdr)) {
2781 hdr = (struct sadb_msg *) skb->data;
2782 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2783 hdr->sadb_msg_reserved != 0 ||
2784 (hdr->sadb_msg_type <= SADB_RESERVED ||
2785 hdr->sadb_msg_type > SADB_MAX)) {
2788 } else if (hdr->sadb_msg_len != (skb->len /
2789 sizeof(uint64_t)) ||
2790 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2791 sizeof(uint64_t))) {
2801 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2803 unsigned int id = d->desc.sadb_alg_id;
2805 if (id >= sizeof(t->aalgos) * 8)
2808 return (t->aalgos >> id) & 1;
2811 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2813 unsigned int id = d->desc.sadb_alg_id;
2815 if (id >= sizeof(t->ealgos) * 8)
2818 return (t->ealgos >> id) & 1;
2821 static int count_ah_combs(struct xfrm_tmpl *t)
2825 for (i = 0; ; i++) {
2826 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2829 if (aalg_tmpl_set(t, aalg) && aalg->available)
2830 sz += sizeof(struct sadb_comb);
2832 return sz + sizeof(struct sadb_prop);
2835 static int count_esp_combs(struct xfrm_tmpl *t)
2839 for (i = 0; ; i++) {
2840 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2844 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2847 for (k = 1; ; k++) {
2848 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2852 if (aalg_tmpl_set(t, aalg) && aalg->available)
2853 sz += sizeof(struct sadb_comb);
2856 return sz + sizeof(struct sadb_prop);
2859 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2861 struct sadb_prop *p;
2864 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2865 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2866 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2867 p->sadb_prop_replay = 32;
2868 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2870 for (i = 0; ; i++) {
2871 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2875 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2876 struct sadb_comb *c;
2877 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2878 memset(c, 0, sizeof(*c));
2879 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2880 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2881 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2882 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2883 c->sadb_comb_hard_addtime = 24*60*60;
2884 c->sadb_comb_soft_addtime = 20*60*60;
2885 c->sadb_comb_hard_usetime = 8*60*60;
2886 c->sadb_comb_soft_usetime = 7*60*60;
2891 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2893 struct sadb_prop *p;
2896 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2897 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2898 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2899 p->sadb_prop_replay = 32;
2900 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2903 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2907 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2910 for (k = 1; ; k++) {
2911 struct sadb_comb *c;
2912 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2915 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2917 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2918 memset(c, 0, sizeof(*c));
2919 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2920 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2921 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2922 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2923 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2924 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2925 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2926 c->sadb_comb_hard_addtime = 24*60*60;
2927 c->sadb_comb_soft_addtime = 20*60*60;
2928 c->sadb_comb_hard_usetime = 8*60*60;
2929 c->sadb_comb_soft_usetime = 7*60*60;
2934 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2939 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2941 struct sk_buff *out_skb;
2942 struct sadb_msg *out_hdr;
2946 hard = c->data.hard;
2952 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
2953 if (IS_ERR(out_skb))
2954 return PTR_ERR(out_skb);
2956 out_hdr = (struct sadb_msg *) out_skb->data;
2957 out_hdr->sadb_msg_version = PF_KEY_V2;
2958 out_hdr->sadb_msg_type = SADB_EXPIRE;
2959 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2960 out_hdr->sadb_msg_errno = 0;
2961 out_hdr->sadb_msg_reserved = 0;
2962 out_hdr->sadb_msg_seq = 0;
2963 out_hdr->sadb_msg_pid = 0;
2965 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2969 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2972 case XFRM_MSG_EXPIRE:
2973 return key_notify_sa_expire(x, c);
2974 case XFRM_MSG_DELSA:
2975 case XFRM_MSG_NEWSA:
2976 case XFRM_MSG_UPDSA:
2977 return key_notify_sa(x, c);
2978 case XFRM_MSG_FLUSHSA:
2979 return key_notify_sa_flush(c);
2980 case XFRM_MSG_NEWAE: /* not yet supported */
2983 printk("pfkey: Unknown SA event %d\n", c->event);
2990 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2992 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2996 case XFRM_MSG_POLEXPIRE:
2997 return key_notify_policy_expire(xp, c);
2998 case XFRM_MSG_DELPOLICY:
2999 case XFRM_MSG_NEWPOLICY:
3000 case XFRM_MSG_UPDPOLICY:
3001 return key_notify_policy(xp, dir, c);
3002 case XFRM_MSG_FLUSHPOLICY:
3003 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3005 return key_notify_policy_flush(c);
3007 printk("pfkey: Unknown policy event %d\n", c->event);
3014 static u32 get_acqseq(void)
3018 static DEFINE_SPINLOCK(acqseq_lock);
3020 spin_lock_bh(&acqseq_lock);
3021 res = (++acqseq ? : ++acqseq);
3022 spin_unlock_bh(&acqseq_lock);
3026 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
3028 struct sk_buff *skb;
3029 struct sadb_msg *hdr;
3030 struct sadb_address *addr;
3031 struct sadb_x_policy *pol;
3032 struct sockaddr_in *sin;
3033 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3034 struct sockaddr_in6 *sin6;
3038 struct sadb_x_sec_ctx *sec_ctx;
3039 struct xfrm_sec_ctx *xfrm_ctx;
3042 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3046 size = sizeof(struct sadb_msg) +
3047 (sizeof(struct sadb_address) * 2) +
3048 (sockaddr_size * 2) +
3049 sizeof(struct sadb_x_policy);
3051 if (x->id.proto == IPPROTO_AH)
3052 size += count_ah_combs(t);
3053 else if (x->id.proto == IPPROTO_ESP)
3054 size += count_esp_combs(t);
3056 if ((xfrm_ctx = x->security)) {
3057 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3058 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3061 skb = alloc_skb(size + 16, GFP_ATOMIC);
3065 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3066 hdr->sadb_msg_version = PF_KEY_V2;
3067 hdr->sadb_msg_type = SADB_ACQUIRE;
3068 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3069 hdr->sadb_msg_len = size / sizeof(uint64_t);
3070 hdr->sadb_msg_errno = 0;
3071 hdr->sadb_msg_reserved = 0;
3072 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3073 hdr->sadb_msg_pid = 0;
3076 addr = (struct sadb_address*) skb_put(skb,
3077 sizeof(struct sadb_address)+sockaddr_size);
3078 addr->sadb_address_len =
3079 (sizeof(struct sadb_address)+sockaddr_size)/
3081 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3082 addr->sadb_address_proto = 0;
3083 addr->sadb_address_reserved = 0;
3084 if (x->props.family == AF_INET) {
3085 addr->sadb_address_prefixlen = 32;
3087 sin = (struct sockaddr_in *) (addr + 1);
3088 sin->sin_family = AF_INET;
3089 sin->sin_addr.s_addr = x->props.saddr.a4;
3091 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3093 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3094 else if (x->props.family == AF_INET6) {
3095 addr->sadb_address_prefixlen = 128;
3097 sin6 = (struct sockaddr_in6 *) (addr + 1);
3098 sin6->sin6_family = AF_INET6;
3099 sin6->sin6_port = 0;
3100 sin6->sin6_flowinfo = 0;
3101 memcpy(&sin6->sin6_addr,
3102 x->props.saddr.a6, sizeof(struct in6_addr));
3103 sin6->sin6_scope_id = 0;
3110 addr = (struct sadb_address*) skb_put(skb,
3111 sizeof(struct sadb_address)+sockaddr_size);
3112 addr->sadb_address_len =
3113 (sizeof(struct sadb_address)+sockaddr_size)/
3115 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3116 addr->sadb_address_proto = 0;
3117 addr->sadb_address_reserved = 0;
3118 if (x->props.family == AF_INET) {
3119 addr->sadb_address_prefixlen = 32;
3121 sin = (struct sockaddr_in *) (addr + 1);
3122 sin->sin_family = AF_INET;
3123 sin->sin_addr.s_addr = x->id.daddr.a4;
3125 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3127 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3128 else if (x->props.family == AF_INET6) {
3129 addr->sadb_address_prefixlen = 128;
3131 sin6 = (struct sockaddr_in6 *) (addr + 1);
3132 sin6->sin6_family = AF_INET6;
3133 sin6->sin6_port = 0;
3134 sin6->sin6_flowinfo = 0;
3135 memcpy(&sin6->sin6_addr,
3136 x->id.daddr.a6, sizeof(struct in6_addr));
3137 sin6->sin6_scope_id = 0;
3143 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3144 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3145 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3146 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3147 pol->sadb_x_policy_dir = dir+1;
3148 pol->sadb_x_policy_id = xp->index;
3150 /* Set sadb_comb's. */
3151 if (x->id.proto == IPPROTO_AH)
3152 dump_ah_combs(skb, t);
3153 else if (x->id.proto == IPPROTO_ESP)
3154 dump_esp_combs(skb, t);
3156 /* security context */
3158 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3159 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3160 sec_ctx->sadb_x_sec_len =
3161 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3162 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3163 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3164 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3165 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3166 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3170 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3173 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3174 u8 *data, int len, int *dir)
3176 struct xfrm_policy *xp;
3177 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3178 struct sadb_x_sec_ctx *sec_ctx;
3180 switch (sk->sk_family) {
3182 if (opt != IP_IPSEC_POLICY) {
3187 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3189 if (opt != IPV6_IPSEC_POLICY) {
3202 if (len < sizeof(struct sadb_x_policy) ||
3203 pol->sadb_x_policy_len*8 > len ||
3204 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3205 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3208 xp = xfrm_policy_alloc(GFP_ATOMIC);
3214 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3215 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3217 xp->lft.soft_byte_limit = XFRM_INF;
3218 xp->lft.hard_byte_limit = XFRM_INF;
3219 xp->lft.soft_packet_limit = XFRM_INF;
3220 xp->lft.hard_packet_limit = XFRM_INF;
3221 xp->family = sk->sk_family;
3224 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3225 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3228 /* security context too */
3229 if (len >= (pol->sadb_x_policy_len*8 +
3230 sizeof(struct sadb_x_sec_ctx))) {
3231 char *p = (char *)pol;
3232 struct xfrm_user_sec_ctx *uctx;
3234 p += pol->sadb_x_policy_len*8;
3235 sec_ctx = (struct sadb_x_sec_ctx *)p;
3236 if (len < pol->sadb_x_policy_len*8 +
3237 sec_ctx->sadb_x_sec_len) {
3241 if ((*dir = verify_sec_ctx_len(p)))
3243 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3244 *dir = security_xfrm_policy_alloc(xp, uctx);
3251 *dir = pol->sadb_x_policy_dir-1;
3255 xfrm_policy_destroy(xp);
3259 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3261 struct sk_buff *skb;
3262 struct sadb_msg *hdr;
3264 struct sadb_address *addr;
3265 struct sadb_x_nat_t_port *n_port;
3266 struct sockaddr_in *sin;
3267 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3268 struct sockaddr_in6 *sin6;
3272 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3273 struct xfrm_encap_tmpl *natt = NULL;
3275 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3287 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3289 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3290 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3293 size = sizeof(struct sadb_msg) +
3294 sizeof(struct sadb_sa) +
3295 (sizeof(struct sadb_address) * 2) +
3296 (sockaddr_size * 2) +
3297 (sizeof(struct sadb_x_nat_t_port) * 2);
3299 skb = alloc_skb(size + 16, GFP_ATOMIC);
3303 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3304 hdr->sadb_msg_version = PF_KEY_V2;
3305 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3306 hdr->sadb_msg_satype = satype;
3307 hdr->sadb_msg_len = size / sizeof(uint64_t);
3308 hdr->sadb_msg_errno = 0;
3309 hdr->sadb_msg_reserved = 0;
3310 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3311 hdr->sadb_msg_pid = 0;
3314 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3315 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3316 sa->sadb_sa_exttype = SADB_EXT_SA;
3317 sa->sadb_sa_spi = x->id.spi;
3318 sa->sadb_sa_replay = 0;
3319 sa->sadb_sa_state = 0;
3320 sa->sadb_sa_auth = 0;
3321 sa->sadb_sa_encrypt = 0;
3322 sa->sadb_sa_flags = 0;
3324 /* ADDRESS_SRC (old addr) */
3325 addr = (struct sadb_address*)
3326 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3327 addr->sadb_address_len =
3328 (sizeof(struct sadb_address)+sockaddr_size)/
3330 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3331 addr->sadb_address_proto = 0;
3332 addr->sadb_address_reserved = 0;
3333 if (x->props.family == AF_INET) {
3334 addr->sadb_address_prefixlen = 32;
3336 sin = (struct sockaddr_in *) (addr + 1);
3337 sin->sin_family = AF_INET;
3338 sin->sin_addr.s_addr = x->props.saddr.a4;
3340 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3342 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3343 else if (x->props.family == AF_INET6) {
3344 addr->sadb_address_prefixlen = 128;
3346 sin6 = (struct sockaddr_in6 *) (addr + 1);
3347 sin6->sin6_family = AF_INET6;
3348 sin6->sin6_port = 0;
3349 sin6->sin6_flowinfo = 0;
3350 memcpy(&sin6->sin6_addr,
3351 x->props.saddr.a6, sizeof(struct in6_addr));
3352 sin6->sin6_scope_id = 0;
3358 /* NAT_T_SPORT (old port) */
3359 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3360 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3361 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3362 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3363 n_port->sadb_x_nat_t_port_reserved = 0;
3365 /* ADDRESS_DST (new addr) */
3366 addr = (struct sadb_address*)
3367 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3368 addr->sadb_address_len =
3369 (sizeof(struct sadb_address)+sockaddr_size)/
3371 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3372 addr->sadb_address_proto = 0;
3373 addr->sadb_address_reserved = 0;
3374 if (x->props.family == AF_INET) {
3375 addr->sadb_address_prefixlen = 32;
3377 sin = (struct sockaddr_in *) (addr + 1);
3378 sin->sin_family = AF_INET;
3379 sin->sin_addr.s_addr = ipaddr->a4;
3381 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3383 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3384 else if (x->props.family == AF_INET6) {
3385 addr->sadb_address_prefixlen = 128;
3387 sin6 = (struct sockaddr_in6 *) (addr + 1);
3388 sin6->sin6_family = AF_INET6;
3389 sin6->sin6_port = 0;
3390 sin6->sin6_flowinfo = 0;
3391 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3392 sin6->sin6_scope_id = 0;
3398 /* NAT_T_DPORT (new port) */
3399 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3400 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3401 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3402 n_port->sadb_x_nat_t_port_port = sport;
3403 n_port->sadb_x_nat_t_port_reserved = 0;
3405 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3408 #ifdef CONFIG_NET_KEY_MIGRATE
3409 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3410 struct xfrm_selector *sel)
3412 struct sadb_address *addr;
3413 struct sockaddr_in *sin;
3414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3415 struct sockaddr_in6 *sin6;
3417 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3418 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3419 addr->sadb_address_exttype = type;
3420 addr->sadb_address_proto = sel->proto;
3421 addr->sadb_address_reserved = 0;
3424 case SADB_EXT_ADDRESS_SRC:
3425 if (sel->family == AF_INET) {
3426 addr->sadb_address_prefixlen = sel->prefixlen_s;
3427 sin = (struct sockaddr_in *)(addr + 1);
3428 sin->sin_family = AF_INET;
3429 memcpy(&sin->sin_addr.s_addr, &sel->saddr,
3430 sizeof(sin->sin_addr.s_addr));
3432 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3434 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3435 else if (sel->family == AF_INET6) {
3436 addr->sadb_address_prefixlen = sel->prefixlen_s;
3437 sin6 = (struct sockaddr_in6 *)(addr + 1);
3438 sin6->sin6_family = AF_INET6;
3439 sin6->sin6_port = 0;
3440 sin6->sin6_flowinfo = 0;
3441 sin6->sin6_scope_id = 0;
3442 memcpy(&sin6->sin6_addr.s6_addr, &sel->saddr,
3443 sizeof(sin6->sin6_addr.s6_addr));
3447 case SADB_EXT_ADDRESS_DST:
3448 if (sel->family == AF_INET) {
3449 addr->sadb_address_prefixlen = sel->prefixlen_d;
3450 sin = (struct sockaddr_in *)(addr + 1);
3451 sin->sin_family = AF_INET;
3452 memcpy(&sin->sin_addr.s_addr, &sel->daddr,
3453 sizeof(sin->sin_addr.s_addr));
3455 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3457 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3458 else if (sel->family == AF_INET6) {
3459 addr->sadb_address_prefixlen = sel->prefixlen_d;
3460 sin6 = (struct sockaddr_in6 *)(addr + 1);
3461 sin6->sin6_family = AF_INET6;
3462 sin6->sin6_port = 0;
3463 sin6->sin6_flowinfo = 0;
3464 sin6->sin6_scope_id = 0;
3465 memcpy(&sin6->sin6_addr.s6_addr, &sel->daddr,
3466 sizeof(sin6->sin6_addr.s6_addr));
3477 static int set_ipsecrequest(struct sk_buff *skb,
3478 uint8_t proto, uint8_t mode, int level,
3479 uint32_t reqid, uint8_t family,
3480 xfrm_address_t *src, xfrm_address_t *dst)
3482 struct sadb_x_ipsecrequest *rq;
3483 struct sockaddr_in *sin;
3484 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3485 struct sockaddr_in6 *sin6;
3489 size_req = sizeof(struct sadb_x_ipsecrequest) +
3490 pfkey_sockaddr_pair_size(family);
3492 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3493 memset(rq, 0, size_req);
3494 rq->sadb_x_ipsecrequest_len = size_req;
3495 rq->sadb_x_ipsecrequest_proto = proto;
3496 rq->sadb_x_ipsecrequest_mode = mode;
3497 rq->sadb_x_ipsecrequest_level = level;
3498 rq->sadb_x_ipsecrequest_reqid = reqid;
3502 sin = (struct sockaddr_in *)(rq + 1);
3503 sin->sin_family = AF_INET;
3504 memcpy(&sin->sin_addr.s_addr, src,
3505 sizeof(sin->sin_addr.s_addr));
3507 sin->sin_family = AF_INET;
3508 memcpy(&sin->sin_addr.s_addr, dst,
3509 sizeof(sin->sin_addr.s_addr));
3511 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3513 sin6 = (struct sockaddr_in6 *)(rq + 1);
3514 sin6->sin6_family = AF_INET6;
3515 sin6->sin6_port = 0;
3516 sin6->sin6_flowinfo = 0;
3517 sin6->sin6_scope_id = 0;
3518 memcpy(&sin6->sin6_addr.s6_addr, src,
3519 sizeof(sin6->sin6_addr.s6_addr));
3521 sin6->sin6_family = AF_INET6;
3522 sin6->sin6_port = 0;
3523 sin6->sin6_flowinfo = 0;
3524 sin6->sin6_scope_id = 0;
3525 memcpy(&sin6->sin6_addr.s6_addr, dst,
3526 sizeof(sin6->sin6_addr.s6_addr));
3537 #ifdef CONFIG_NET_KEY_MIGRATE
3538 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3539 struct xfrm_migrate *m, int num_bundles)
3545 struct sk_buff *skb;
3546 struct sadb_msg *hdr;
3547 struct sadb_x_policy *pol;
3548 struct xfrm_migrate *mp;
3550 if (type != XFRM_POLICY_TYPE_MAIN)
3553 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3557 sasize_sel = pfkey_sockaddr_size(sel->family);
3560 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3563 size_pol += sizeof(struct sadb_x_policy);
3566 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3567 /* old locator pair */
3568 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3569 pfkey_sockaddr_pair_size(mp->old_family);
3570 /* new locator pair */
3571 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3572 pfkey_sockaddr_pair_size(mp->new_family);
3575 size += sizeof(struct sadb_msg) + size_pol;
3578 skb = alloc_skb(size, GFP_ATOMIC);
3582 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3583 hdr->sadb_msg_version = PF_KEY_V2;
3584 hdr->sadb_msg_type = SADB_X_MIGRATE;
3585 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3586 hdr->sadb_msg_len = size / 8;
3587 hdr->sadb_msg_errno = 0;
3588 hdr->sadb_msg_reserved = 0;
3589 hdr->sadb_msg_seq = 0;
3590 hdr->sadb_msg_pid = 0;
3593 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3596 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3598 /* policy information */
3599 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3600 pol->sadb_x_policy_len = size_pol / 8;
3601 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3602 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3603 pol->sadb_x_policy_dir = dir + 1;
3604 pol->sadb_x_policy_id = 0;
3605 pol->sadb_x_policy_priority = 0;
3607 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3608 /* old ipsecrequest */
3609 int mode = pfkey_mode_from_xfrm(mp->mode);
3612 if (set_ipsecrequest(skb, mp->proto, mode,
3613 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3614 mp->reqid, mp->old_family,
3615 &mp->old_saddr, &mp->old_daddr) < 0)
3618 /* new ipsecrequest */
3619 if (set_ipsecrequest(skb, mp->proto, mode,
3620 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3621 mp->reqid, mp->new_family,
3622 &mp->new_saddr, &mp->new_daddr) < 0)
3626 /* broadcast migrate message to sockets */
3627 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3636 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3637 struct xfrm_migrate *m, int num_bundles)
3639 return -ENOPROTOOPT;
3643 static int pfkey_sendmsg(struct kiocb *kiocb,
3644 struct socket *sock, struct msghdr *msg, size_t len)
3646 struct sock *sk = sock->sk;
3647 struct sk_buff *skb = NULL;
3648 struct sadb_msg *hdr = NULL;
3652 if (msg->msg_flags & MSG_OOB)
3656 if ((unsigned)len > sk->sk_sndbuf - 32)
3660 skb = alloc_skb(len, GFP_KERNEL);
3665 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3668 hdr = pfkey_get_base_msg(skb, &err);
3672 mutex_lock(&xfrm_cfg_mutex);
3673 err = pfkey_process(sk, skb, hdr);
3674 mutex_unlock(&xfrm_cfg_mutex);
3677 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3685 static int pfkey_recvmsg(struct kiocb *kiocb,
3686 struct socket *sock, struct msghdr *msg, size_t len,
3689 struct sock *sk = sock->sk;
3690 struct sk_buff *skb;
3694 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3697 msg->msg_namelen = 0;
3698 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3704 msg->msg_flags |= MSG_TRUNC;
3708 skb_reset_transport_header(skb);
3709 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3713 sock_recv_timestamp(msg, sk, skb);
3715 err = (flags & MSG_TRUNC) ? skb->len : copied;
3718 skb_free_datagram(sk, skb);
3723 static const struct proto_ops pfkey_ops = {
3725 .owner = THIS_MODULE,
3726 /* Operations that make no sense on pfkey sockets. */
3727 .bind = sock_no_bind,
3728 .connect = sock_no_connect,
3729 .socketpair = sock_no_socketpair,
3730 .accept = sock_no_accept,
3731 .getname = sock_no_getname,
3732 .ioctl = sock_no_ioctl,
3733 .listen = sock_no_listen,
3734 .shutdown = sock_no_shutdown,
3735 .setsockopt = sock_no_setsockopt,
3736 .getsockopt = sock_no_getsockopt,
3737 .mmap = sock_no_mmap,
3738 .sendpage = sock_no_sendpage,
3740 /* Now the operations that really occur. */
3741 .release = pfkey_release,
3742 .poll = datagram_poll,
3743 .sendmsg = pfkey_sendmsg,
3744 .recvmsg = pfkey_recvmsg,
3747 static struct net_proto_family pfkey_family_ops = {
3749 .create = pfkey_create,
3750 .owner = THIS_MODULE,
3753 #ifdef CONFIG_PROC_FS
3754 static int pfkey_seq_show(struct seq_file *f, void *v)
3758 s = (struct sock *)v;
3759 if (v == SEQ_START_TOKEN)
3760 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3762 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3764 atomic_read(&s->sk_refcnt),
3765 atomic_read(&s->sk_rmem_alloc),
3766 atomic_read(&s->sk_wmem_alloc),
3773 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3776 struct hlist_node *node;
3779 read_lock(&pfkey_table_lock);
3781 return SEQ_START_TOKEN;
3783 sk_for_each(s, node, &pfkey_table)
3790 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3793 return (v == SEQ_START_TOKEN) ?
3794 sk_head(&pfkey_table) :
3795 sk_next((struct sock *)v);
3798 static void pfkey_seq_stop(struct seq_file *f, void *v)
3800 read_unlock(&pfkey_table_lock);
3803 static struct seq_operations pfkey_seq_ops = {
3804 .start = pfkey_seq_start,
3805 .next = pfkey_seq_next,
3806 .stop = pfkey_seq_stop,
3807 .show = pfkey_seq_show,
3810 static int pfkey_seq_open(struct inode *inode, struct file *file)
3812 return seq_open(file, &pfkey_seq_ops);
3815 static struct file_operations pfkey_proc_ops = {
3816 .open = pfkey_seq_open,
3818 .llseek = seq_lseek,
3819 .release = seq_release,
3822 static int pfkey_init_proc(void)
3824 struct proc_dir_entry *e;
3826 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
3833 static void pfkey_exit_proc(void)
3835 proc_net_remove(&init_net, "pfkey");
3838 static inline int pfkey_init_proc(void)
3843 static inline void pfkey_exit_proc(void)
3848 static struct xfrm_mgr pfkeyv2_mgr =
3851 .notify = pfkey_send_notify,
3852 .acquire = pfkey_send_acquire,
3853 .compile_policy = pfkey_compile_policy,
3854 .new_mapping = pfkey_send_new_mapping,
3855 .notify_policy = pfkey_send_policy_notify,
3856 .migrate = pfkey_send_migrate,
3859 static void __exit ipsec_pfkey_exit(void)
3861 xfrm_unregister_km(&pfkeyv2_mgr);
3863 sock_unregister(PF_KEY);
3864 proto_unregister(&key_proto);
3867 static int __init ipsec_pfkey_init(void)
3869 int err = proto_register(&key_proto, 0);
3874 err = sock_register(&pfkey_family_ops);
3876 goto out_unregister_key_proto;
3877 err = pfkey_init_proc();
3879 goto out_sock_unregister;
3880 err = xfrm_register_km(&pfkeyv2_mgr);
3882 goto out_remove_proc_entry;
3885 out_remove_proc_entry:
3887 out_sock_unregister:
3888 sock_unregister(PF_KEY);
3889 out_unregister_key_proto:
3890 proto_unregister(&key_proto);
3894 module_init(ipsec_pfkey_init);
3895 module_exit(ipsec_pfkey_exit);
3896 MODULE_LICENSE("GPL");
3897 MODULE_ALIAS_NETPROTO(PF_KEY);