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 */
55 int (*dump)(struct pfkey_sock *sk);
56 void (*done)(struct pfkey_sock *sk);
58 struct xfrm_policy_walk policy;
59 struct xfrm_state_walk state;
64 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
66 return (struct pfkey_sock *)sk;
69 static int pfkey_can_dump(struct sock *sk)
71 if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
76 static void pfkey_terminate_dump(struct pfkey_sock *pfk)
80 pfk->dump.dump = NULL;
81 pfk->dump.done = NULL;
85 static void pfkey_sock_destruct(struct sock *sk)
87 pfkey_terminate_dump(pfkey_sk(sk));
88 skb_queue_purge(&sk->sk_receive_queue);
90 if (!sock_flag(sk, SOCK_DEAD)) {
91 printk("Attempt to release alive pfkey socket: %p\n", sk);
95 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
96 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
98 atomic_dec(&pfkey_socks_nr);
101 static void pfkey_table_grab(void)
103 write_lock_bh(&pfkey_table_lock);
105 if (atomic_read(&pfkey_table_users)) {
106 DECLARE_WAITQUEUE(wait, current);
108 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
110 set_current_state(TASK_UNINTERRUPTIBLE);
111 if (atomic_read(&pfkey_table_users) == 0)
113 write_unlock_bh(&pfkey_table_lock);
115 write_lock_bh(&pfkey_table_lock);
118 __set_current_state(TASK_RUNNING);
119 remove_wait_queue(&pfkey_table_wait, &wait);
123 static __inline__ void pfkey_table_ungrab(void)
125 write_unlock_bh(&pfkey_table_lock);
126 wake_up(&pfkey_table_wait);
129 static __inline__ void pfkey_lock_table(void)
131 /* read_lock() synchronizes us to pfkey_table_grab */
133 read_lock(&pfkey_table_lock);
134 atomic_inc(&pfkey_table_users);
135 read_unlock(&pfkey_table_lock);
138 static __inline__ void pfkey_unlock_table(void)
140 if (atomic_dec_and_test(&pfkey_table_users))
141 wake_up(&pfkey_table_wait);
145 static const struct proto_ops pfkey_ops;
147 static void pfkey_insert(struct sock *sk)
150 sk_add_node(sk, &pfkey_table);
151 pfkey_table_ungrab();
154 static void pfkey_remove(struct sock *sk)
157 sk_del_node_init(sk);
158 pfkey_table_ungrab();
161 static struct proto key_proto = {
163 .owner = THIS_MODULE,
164 .obj_size = sizeof(struct pfkey_sock),
167 static int pfkey_create(struct net *net, struct socket *sock, int protocol)
172 if (net != &init_net)
173 return -EAFNOSUPPORT;
175 if (!capable(CAP_NET_ADMIN))
177 if (sock->type != SOCK_RAW)
178 return -ESOCKTNOSUPPORT;
179 if (protocol != PF_KEY_V2)
180 return -EPROTONOSUPPORT;
183 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
187 sock->ops = &pfkey_ops;
188 sock_init_data(sock, sk);
190 sk->sk_family = PF_KEY;
191 sk->sk_destruct = pfkey_sock_destruct;
193 atomic_inc(&pfkey_socks_nr);
202 static int pfkey_release(struct socket *sock)
204 struct sock *sk = sock->sk;
213 skb_queue_purge(&sk->sk_write_queue);
219 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
220 gfp_t allocation, struct sock *sk)
226 if (atomic_read(&skb->users) != 1) {
227 *skb2 = skb_clone(skb, allocation);
230 atomic_inc(&skb->users);
234 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
236 skb_set_owner_r(*skb2, sk);
237 skb_queue_tail(&sk->sk_receive_queue, *skb2);
238 sk->sk_data_ready(sk, (*skb2)->len);
247 /* Send SKB to all pfkey sockets matching selected criteria. */
248 #define BROADCAST_ALL 0
249 #define BROADCAST_ONE 1
250 #define BROADCAST_REGISTERED 2
251 #define BROADCAST_PROMISC_ONLY 4
252 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
253 int broadcast_flags, struct sock *one_sk)
256 struct hlist_node *node;
257 struct sk_buff *skb2 = NULL;
260 /* XXX Do we need something like netlink_overrun? I think
261 * XXX PF_KEY socket apps will not mind current behavior.
267 sk_for_each(sk, node, &pfkey_table) {
268 struct pfkey_sock *pfk = pfkey_sk(sk);
271 /* Yes, it means that if you are meant to receive this
272 * pfkey message you receive it twice as promiscuous
276 pfkey_broadcast_one(skb, &skb2, allocation, sk);
278 /* the exact target will be processed later */
281 if (broadcast_flags != BROADCAST_ALL) {
282 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
284 if ((broadcast_flags & BROADCAST_REGISTERED) &&
287 if (broadcast_flags & BROADCAST_ONE)
291 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
293 /* Error is cleare after succecful sending to at least one
295 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
298 pfkey_unlock_table();
301 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
309 static int pfkey_do_dump(struct pfkey_sock *pfk)
313 rc = pfk->dump.dump(pfk);
317 pfkey_terminate_dump(pfk);
321 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
326 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
328 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
329 struct sadb_msg *hdr;
334 /* Woe be to the platform trying to support PFKEY yet
335 * having normal errnos outside the 1-255 range, inclusive.
338 if (err == ERESTARTSYS ||
339 err == ERESTARTNOHAND ||
340 err == ERESTARTNOINTR)
344 BUG_ON(err <= 0 || err >= 256);
346 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
347 pfkey_hdr_dup(hdr, orig);
348 hdr->sadb_msg_errno = (uint8_t) err;
349 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
352 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
357 static u8 sadb_ext_min_len[] = {
358 [SADB_EXT_RESERVED] = (u8) 0,
359 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
360 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
361 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
362 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
363 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
364 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
365 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
366 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
367 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
368 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
369 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
370 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
371 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
372 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
373 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
374 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
375 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
376 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
377 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
378 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
379 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
380 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
381 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
382 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
385 /* Verify sadb_address_{len,prefixlen} against sa_family. */
386 static int verify_address_len(void *p)
388 struct sadb_address *sp = p;
389 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
390 struct sockaddr_in *sin;
391 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
392 struct sockaddr_in6 *sin6;
396 switch (addr->sa_family) {
398 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
399 if (sp->sadb_address_len != len ||
400 sp->sadb_address_prefixlen > 32)
403 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
405 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
406 if (sp->sadb_address_len != len ||
407 sp->sadb_address_prefixlen > 128)
412 /* It is user using kernel to keep track of security
413 * associations for another protocol, such as
414 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
417 * XXX Actually, association/policy database is not yet
418 * XXX able to cope with arbitrary sockaddr families.
419 * XXX When it can, remove this -EINVAL. -DaveM
428 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
430 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
431 sec_ctx->sadb_x_ctx_len,
435 static inline int verify_sec_ctx_len(void *p)
437 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
438 int len = sec_ctx->sadb_x_ctx_len;
443 len = pfkey_sec_ctx_len(sec_ctx);
445 if (sec_ctx->sadb_x_sec_len != len)
451 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
453 struct xfrm_user_sec_ctx *uctx = NULL;
454 int ctx_size = sec_ctx->sadb_x_ctx_len;
456 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
461 uctx->len = pfkey_sec_ctx_len(sec_ctx);
462 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
463 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
464 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
465 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
466 memcpy(uctx + 1, sec_ctx + 1,
472 static int present_and_same_family(struct sadb_address *src,
473 struct sadb_address *dst)
475 struct sockaddr *s_addr, *d_addr;
480 s_addr = (struct sockaddr *)(src + 1);
481 d_addr = (struct sockaddr *)(dst + 1);
482 if (s_addr->sa_family != d_addr->sa_family)
484 if (s_addr->sa_family != AF_INET
485 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
486 && s_addr->sa_family != AF_INET6
494 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
496 char *p = (char *) hdr;
502 struct sadb_ext *ehdr = (struct sadb_ext *) p;
506 ext_len = ehdr->sadb_ext_len;
507 ext_len *= sizeof(uint64_t);
508 ext_type = ehdr->sadb_ext_type;
509 if (ext_len < sizeof(uint64_t) ||
511 ext_type == SADB_EXT_RESERVED)
514 if (ext_type <= SADB_EXT_MAX) {
515 int min = (int) sadb_ext_min_len[ext_type];
518 if (ext_hdrs[ext_type-1] != NULL)
520 if (ext_type == SADB_EXT_ADDRESS_SRC ||
521 ext_type == SADB_EXT_ADDRESS_DST ||
522 ext_type == SADB_EXT_ADDRESS_PROXY ||
523 ext_type == SADB_X_EXT_NAT_T_OA) {
524 if (verify_address_len(p))
527 if (ext_type == SADB_X_EXT_SEC_CTX) {
528 if (verify_sec_ctx_len(p))
531 ext_hdrs[ext_type-1] = p;
541 pfkey_satype2proto(uint8_t satype)
544 case SADB_SATYPE_UNSPEC:
545 return IPSEC_PROTO_ANY;
548 case SADB_SATYPE_ESP:
550 case SADB_X_SATYPE_IPCOMP:
560 pfkey_proto2satype(uint16_t proto)
564 return SADB_SATYPE_AH;
566 return SADB_SATYPE_ESP;
568 return SADB_X_SATYPE_IPCOMP;
576 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
577 * say specifically 'just raw sockets' as we encode them as 255.
580 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
582 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
585 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
587 return (proto ? proto : IPSEC_PROTO_ANY);
590 static inline int pfkey_sockaddr_len(sa_family_t family)
594 return sizeof(struct sockaddr_in);
595 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
597 return sizeof(struct sockaddr_in6);
604 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
606 switch (sa->sa_family) {
609 ((struct sockaddr_in *)sa)->sin_addr.s_addr;
611 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
614 &((struct sockaddr_in6 *)sa)->sin6_addr,
615 sizeof(struct in6_addr));
623 int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr, xfrm_address_t *xaddr)
625 return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
629 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
632 struct sadb_address *addr;
634 unsigned short family;
635 xfrm_address_t *xaddr;
637 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
641 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
645 /* sadb_address_len should be checked by caller */
646 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
650 family = ((struct sockaddr *)(addr + 1))->sa_family;
653 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
655 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
657 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
667 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
670 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
673 pfkey_sockaddr_size(sa_family_t family)
675 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
678 static inline int pfkey_mode_from_xfrm(int mode)
681 case XFRM_MODE_TRANSPORT:
682 return IPSEC_MODE_TRANSPORT;
683 case XFRM_MODE_TUNNEL:
684 return IPSEC_MODE_TUNNEL;
686 return IPSEC_MODE_BEET;
692 static inline int pfkey_mode_to_xfrm(int mode)
695 case IPSEC_MODE_ANY: /*XXX*/
696 case IPSEC_MODE_TRANSPORT:
697 return XFRM_MODE_TRANSPORT;
698 case IPSEC_MODE_TUNNEL:
699 return XFRM_MODE_TUNNEL;
700 case IPSEC_MODE_BEET:
701 return XFRM_MODE_BEET;
707 static unsigned int pfkey_sockaddr_fill(xfrm_address_t *xaddr, __be16 port,
709 unsigned short family)
714 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
715 sin->sin_family = AF_INET;
716 sin->sin_port = port;
717 sin->sin_addr.s_addr = xaddr->a4;
718 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
721 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
724 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
725 sin6->sin6_family = AF_INET6;
726 sin6->sin6_port = port;
727 sin6->sin6_flowinfo = 0;
728 ipv6_addr_copy(&sin6->sin6_addr, (struct in6_addr *)xaddr->a6);
729 sin6->sin6_scope_id = 0;
737 static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
738 int add_keys, int hsc)
741 struct sadb_msg *hdr;
743 struct sadb_lifetime *lifetime;
744 struct sadb_address *addr;
745 struct sadb_key *key;
746 struct sadb_x_sa2 *sa2;
747 struct sadb_x_sec_ctx *sec_ctx;
748 struct xfrm_sec_ctx *xfrm_ctx;
751 int auth_key_size = 0;
752 int encrypt_key_size = 0;
754 struct xfrm_encap_tmpl *natt = NULL;
757 /* address family check */
758 sockaddr_size = pfkey_sockaddr_size(x->props.family);
760 return ERR_PTR(-EINVAL);
762 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
763 key(AE), (identity(SD),) (sensitivity)> */
764 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
765 sizeof(struct sadb_lifetime) +
766 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
767 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
768 sizeof(struct sadb_address)*2 +
770 sizeof(struct sadb_x_sa2);
772 if ((xfrm_ctx = x->security)) {
773 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
774 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
777 /* identity & sensitivity */
778 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr, x->props.family))
779 size += sizeof(struct sadb_address) + sockaddr_size;
782 if (x->aalg && x->aalg->alg_key_len) {
784 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
785 size += sizeof(struct sadb_key) + auth_key_size;
787 if (x->ealg && x->ealg->alg_key_len) {
789 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
790 size += sizeof(struct sadb_key) + encrypt_key_size;
796 if (natt && natt->encap_type) {
797 size += sizeof(struct sadb_x_nat_t_type);
798 size += sizeof(struct sadb_x_nat_t_port);
799 size += sizeof(struct sadb_x_nat_t_port);
802 skb = alloc_skb(size + 16, GFP_ATOMIC);
804 return ERR_PTR(-ENOBUFS);
806 /* call should fill header later */
807 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
808 memset(hdr, 0, size); /* XXX do we need this ? */
809 hdr->sadb_msg_len = size / sizeof(uint64_t);
812 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
813 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
814 sa->sadb_sa_exttype = SADB_EXT_SA;
815 sa->sadb_sa_spi = x->id.spi;
816 sa->sadb_sa_replay = x->props.replay_window;
817 switch (x->km.state) {
818 case XFRM_STATE_VALID:
819 sa->sadb_sa_state = x->km.dying ?
820 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
823 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
826 sa->sadb_sa_state = SADB_SASTATE_DEAD;
829 sa->sadb_sa_auth = 0;
831 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
832 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
834 sa->sadb_sa_encrypt = 0;
835 BUG_ON(x->ealg && x->calg);
837 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
838 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
840 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
842 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
843 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
846 sa->sadb_sa_flags = 0;
847 if (x->props.flags & XFRM_STATE_NOECN)
848 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
849 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
850 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
851 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
852 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
856 lifetime = (struct sadb_lifetime *) skb_put(skb,
857 sizeof(struct sadb_lifetime));
858 lifetime->sadb_lifetime_len =
859 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
860 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
861 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
862 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
863 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
864 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
868 lifetime = (struct sadb_lifetime *) skb_put(skb,
869 sizeof(struct sadb_lifetime));
870 lifetime->sadb_lifetime_len =
871 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
872 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
873 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
874 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
875 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
876 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
879 lifetime = (struct sadb_lifetime *) skb_put(skb,
880 sizeof(struct sadb_lifetime));
881 lifetime->sadb_lifetime_len =
882 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
883 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
884 lifetime->sadb_lifetime_allocations = x->curlft.packets;
885 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
886 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
887 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
889 addr = (struct sadb_address*) skb_put(skb,
890 sizeof(struct sadb_address)+sockaddr_size);
891 addr->sadb_address_len =
892 (sizeof(struct sadb_address)+sockaddr_size)/
894 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
895 /* "if the ports are non-zero, then the sadb_address_proto field,
896 normally zero, MUST be filled in with the transport
897 protocol's number." - RFC2367 */
898 addr->sadb_address_proto = 0;
899 addr->sadb_address_reserved = 0;
901 addr->sadb_address_prefixlen =
902 pfkey_sockaddr_fill(&x->props.saddr, 0,
903 (struct sockaddr *) (addr + 1),
905 if (!addr->sadb_address_prefixlen)
909 addr = (struct sadb_address*) skb_put(skb,
910 sizeof(struct sadb_address)+sockaddr_size);
911 addr->sadb_address_len =
912 (sizeof(struct sadb_address)+sockaddr_size)/
914 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
915 addr->sadb_address_proto = 0;
916 addr->sadb_address_reserved = 0;
918 addr->sadb_address_prefixlen =
919 pfkey_sockaddr_fill(&x->id.daddr, 0,
920 (struct sockaddr *) (addr + 1),
922 if (!addr->sadb_address_prefixlen)
925 if (xfrm_addr_cmp(&x->sel.saddr, &x->props.saddr,
927 addr = (struct sadb_address*) skb_put(skb,
928 sizeof(struct sadb_address)+sockaddr_size);
929 addr->sadb_address_len =
930 (sizeof(struct sadb_address)+sockaddr_size)/
932 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
933 addr->sadb_address_proto =
934 pfkey_proto_from_xfrm(x->sel.proto);
935 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
936 addr->sadb_address_reserved = 0;
938 pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
939 (struct sockaddr *) (addr + 1),
944 if (add_keys && auth_key_size) {
945 key = (struct sadb_key *) skb_put(skb,
946 sizeof(struct sadb_key)+auth_key_size);
947 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
949 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
950 key->sadb_key_bits = x->aalg->alg_key_len;
951 key->sadb_key_reserved = 0;
952 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
955 if (add_keys && encrypt_key_size) {
956 key = (struct sadb_key *) skb_put(skb,
957 sizeof(struct sadb_key)+encrypt_key_size);
958 key->sadb_key_len = (sizeof(struct sadb_key) +
959 encrypt_key_size) / sizeof(uint64_t);
960 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
961 key->sadb_key_bits = x->ealg->alg_key_len;
962 key->sadb_key_reserved = 0;
963 memcpy(key + 1, x->ealg->alg_key,
964 (x->ealg->alg_key_len+7)/8);
968 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
969 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
970 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
971 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
973 return ERR_PTR(-EINVAL);
975 sa2->sadb_x_sa2_mode = mode;
976 sa2->sadb_x_sa2_reserved1 = 0;
977 sa2->sadb_x_sa2_reserved2 = 0;
978 sa2->sadb_x_sa2_sequence = 0;
979 sa2->sadb_x_sa2_reqid = x->props.reqid;
981 if (natt && natt->encap_type) {
982 struct sadb_x_nat_t_type *n_type;
983 struct sadb_x_nat_t_port *n_port;
986 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
987 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
988 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
989 n_type->sadb_x_nat_t_type_type = natt->encap_type;
990 n_type->sadb_x_nat_t_type_reserved[0] = 0;
991 n_type->sadb_x_nat_t_type_reserved[1] = 0;
992 n_type->sadb_x_nat_t_type_reserved[2] = 0;
995 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
996 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
997 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
998 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
999 n_port->sadb_x_nat_t_port_reserved = 0;
1002 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1003 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1004 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1005 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1006 n_port->sadb_x_nat_t_port_reserved = 0;
1009 /* security context */
1011 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1012 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1013 sec_ctx->sadb_x_sec_len =
1014 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1015 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1016 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1017 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1018 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1019 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1027 static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1029 struct sk_buff *skb;
1031 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1036 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1039 return __pfkey_xfrm_state2msg(x, 0, hsc);
1042 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
1045 struct xfrm_state *x;
1046 struct sadb_lifetime *lifetime;
1048 struct sadb_key *key;
1049 struct sadb_x_sec_ctx *sec_ctx;
1054 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1056 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1057 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1058 return ERR_PTR(-EINVAL);
1059 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1060 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1061 return ERR_PTR(-EINVAL);
1062 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1063 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1064 return ERR_PTR(-EINVAL);
1065 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1066 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1067 return ERR_PTR(-EINVAL);
1069 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1071 return ERR_PTR(-EINVAL);
1073 /* default error is no buffer space */
1078 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1079 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1080 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1081 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1082 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1085 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1086 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1088 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1089 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1090 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1091 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1092 return ERR_PTR(-EINVAL);
1093 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1095 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1096 ((key->sadb_key_bits+7) / 8 == 0 ||
1097 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1098 return ERR_PTR(-EINVAL);
1099 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1101 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1102 ((key->sadb_key_bits+7) / 8 == 0 ||
1103 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1104 return ERR_PTR(-EINVAL);
1106 x = xfrm_state_alloc();
1108 return ERR_PTR(-ENOBUFS);
1110 x->id.proto = proto;
1111 x->id.spi = sa->sadb_sa_spi;
1112 x->props.replay_window = sa->sadb_sa_replay;
1113 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1114 x->props.flags |= XFRM_STATE_NOECN;
1115 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1116 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1117 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1118 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1120 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1121 if (lifetime != NULL) {
1122 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1123 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1124 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1125 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1127 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1128 if (lifetime != NULL) {
1129 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1130 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1131 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1132 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1135 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1136 if (sec_ctx != NULL) {
1137 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1142 err = security_xfrm_state_alloc(x, uctx);
1149 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1150 if (sa->sadb_sa_auth) {
1152 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1158 keysize = (key->sadb_key_bits + 7) / 8;
1159 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1162 strcpy(x->aalg->alg_name, a->name);
1163 x->aalg->alg_key_len = 0;
1165 x->aalg->alg_key_len = key->sadb_key_bits;
1166 memcpy(x->aalg->alg_key, key+1, keysize);
1168 x->props.aalgo = sa->sadb_sa_auth;
1169 /* x->algo.flags = sa->sadb_sa_flags; */
1171 if (sa->sadb_sa_encrypt) {
1172 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1173 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1178 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1181 strcpy(x->calg->alg_name, a->name);
1182 x->props.calgo = sa->sadb_sa_encrypt;
1185 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1190 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1192 keysize = (key->sadb_key_bits + 7) / 8;
1193 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1196 strcpy(x->ealg->alg_name, a->name);
1197 x->ealg->alg_key_len = 0;
1199 x->ealg->alg_key_len = key->sadb_key_bits;
1200 memcpy(x->ealg->alg_key, key+1, keysize);
1202 x->props.ealgo = sa->sadb_sa_encrypt;
1205 /* x->algo.flags = sa->sadb_sa_flags; */
1207 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1209 if (!x->props.family) {
1210 err = -EAFNOSUPPORT;
1213 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1216 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1217 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1218 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1223 x->props.mode = mode;
1224 x->props.reqid = sa2->sadb_x_sa2_reqid;
1227 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1228 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1230 /* Nobody uses this, but we try. */
1231 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1232 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1236 x->sel.family = x->props.family;
1238 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1239 struct sadb_x_nat_t_type* n_type;
1240 struct xfrm_encap_tmpl *natt;
1242 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1247 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1248 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1250 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1251 struct sadb_x_nat_t_port* n_port =
1252 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1253 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1255 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1256 struct sadb_x_nat_t_port* n_port =
1257 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1258 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1262 err = xfrm_init_state(x);
1266 x->km.seq = hdr->sadb_msg_seq;
1270 x->km.state = XFRM_STATE_DEAD;
1272 return ERR_PTR(err);
1275 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1280 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1282 struct sk_buff *resp_skb;
1283 struct sadb_x_sa2 *sa2;
1284 struct sadb_address *saddr, *daddr;
1285 struct sadb_msg *out_hdr;
1286 struct sadb_spirange *range;
1287 struct xfrm_state *x = NULL;
1290 u32 min_spi, max_spi;
1293 unsigned short family;
1294 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1296 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1297 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1300 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1304 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1305 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1308 reqid = sa2->sadb_x_sa2_reqid;
1314 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1315 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1317 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1320 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1321 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1323 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1325 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1326 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1331 if (hdr->sadb_msg_seq) {
1332 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1333 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1340 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1346 max_spi = 0x0fffffff;
1348 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1350 min_spi = range->sadb_spirange_min;
1351 max_spi = range->sadb_spirange_max;
1354 err = xfrm_alloc_spi(x, min_spi, max_spi);
1355 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1357 if (IS_ERR(resp_skb)) {
1359 return PTR_ERR(resp_skb);
1362 out_hdr = (struct sadb_msg *) resp_skb->data;
1363 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1364 out_hdr->sadb_msg_type = SADB_GETSPI;
1365 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1366 out_hdr->sadb_msg_errno = 0;
1367 out_hdr->sadb_msg_reserved = 0;
1368 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1369 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1373 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1378 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1380 struct xfrm_state *x;
1382 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1385 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1388 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1392 spin_lock_bh(&x->lock);
1393 if (x->km.state == XFRM_STATE_ACQ) {
1394 x->km.state = XFRM_STATE_ERROR;
1397 spin_unlock_bh(&x->lock);
1402 static inline int event2poltype(int event)
1405 case XFRM_MSG_DELPOLICY:
1406 return SADB_X_SPDDELETE;
1407 case XFRM_MSG_NEWPOLICY:
1408 return SADB_X_SPDADD;
1409 case XFRM_MSG_UPDPOLICY:
1410 return SADB_X_SPDUPDATE;
1411 case XFRM_MSG_POLEXPIRE:
1412 // return SADB_X_SPDEXPIRE;
1414 printk("pfkey: Unknown policy event %d\n", event);
1421 static inline int event2keytype(int event)
1424 case XFRM_MSG_DELSA:
1426 case XFRM_MSG_NEWSA:
1428 case XFRM_MSG_UPDSA:
1430 case XFRM_MSG_EXPIRE:
1433 printk("pfkey: Unknown SA event %d\n", event);
1441 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1443 struct sk_buff *skb;
1444 struct sadb_msg *hdr;
1446 skb = pfkey_xfrm_state2msg(x);
1449 return PTR_ERR(skb);
1451 hdr = (struct sadb_msg *) skb->data;
1452 hdr->sadb_msg_version = PF_KEY_V2;
1453 hdr->sadb_msg_type = event2keytype(c->event);
1454 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1455 hdr->sadb_msg_errno = 0;
1456 hdr->sadb_msg_reserved = 0;
1457 hdr->sadb_msg_seq = c->seq;
1458 hdr->sadb_msg_pid = c->pid;
1460 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1465 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1467 struct xfrm_state *x;
1471 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1476 if (hdr->sadb_msg_type == SADB_ADD)
1477 err = xfrm_state_add(x);
1479 err = xfrm_state_update(x);
1481 xfrm_audit_state_add(x, err ? 0 : 1,
1482 audit_get_loginuid(current),
1483 audit_get_sessionid(current), 0);
1486 x->km.state = XFRM_STATE_DEAD;
1487 __xfrm_state_put(x);
1491 if (hdr->sadb_msg_type == SADB_ADD)
1492 c.event = XFRM_MSG_NEWSA;
1494 c.event = XFRM_MSG_UPDSA;
1495 c.seq = hdr->sadb_msg_seq;
1496 c.pid = hdr->sadb_msg_pid;
1497 km_state_notify(x, &c);
1503 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1505 struct xfrm_state *x;
1509 if (!ext_hdrs[SADB_EXT_SA-1] ||
1510 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1511 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1514 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1518 if ((err = security_xfrm_state_delete(x)))
1521 if (xfrm_state_kern(x)) {
1526 err = xfrm_state_delete(x);
1531 c.seq = hdr->sadb_msg_seq;
1532 c.pid = hdr->sadb_msg_pid;
1533 c.event = XFRM_MSG_DELSA;
1534 km_state_notify(x, &c);
1536 xfrm_audit_state_delete(x, err ? 0 : 1,
1537 audit_get_loginuid(current),
1538 audit_get_sessionid(current), 0);
1544 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1547 struct sk_buff *out_skb;
1548 struct sadb_msg *out_hdr;
1549 struct xfrm_state *x;
1551 if (!ext_hdrs[SADB_EXT_SA-1] ||
1552 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1553 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1556 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1560 out_skb = pfkey_xfrm_state2msg(x);
1561 proto = x->id.proto;
1563 if (IS_ERR(out_skb))
1564 return PTR_ERR(out_skb);
1566 out_hdr = (struct sadb_msg *) out_skb->data;
1567 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1568 out_hdr->sadb_msg_type = SADB_GET;
1569 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1570 out_hdr->sadb_msg_errno = 0;
1571 out_hdr->sadb_msg_reserved = 0;
1572 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1573 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1574 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1579 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1582 struct sk_buff *skb;
1583 struct sadb_msg *hdr;
1584 int len, auth_len, enc_len, i;
1586 auth_len = xfrm_count_auth_supported();
1588 auth_len *= sizeof(struct sadb_alg);
1589 auth_len += sizeof(struct sadb_supported);
1592 enc_len = xfrm_count_enc_supported();
1594 enc_len *= sizeof(struct sadb_alg);
1595 enc_len += sizeof(struct sadb_supported);
1598 len = enc_len + auth_len + sizeof(struct sadb_msg);
1600 skb = alloc_skb(len + 16, allocation);
1604 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1605 pfkey_hdr_dup(hdr, orig);
1606 hdr->sadb_msg_errno = 0;
1607 hdr->sadb_msg_len = len / sizeof(uint64_t);
1610 struct sadb_supported *sp;
1611 struct sadb_alg *ap;
1613 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1614 ap = (struct sadb_alg *) (sp + 1);
1616 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1617 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1619 for (i = 0; ; i++) {
1620 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1623 if (aalg->available)
1629 struct sadb_supported *sp;
1630 struct sadb_alg *ap;
1632 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1633 ap = (struct sadb_alg *) (sp + 1);
1635 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1636 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1638 for (i = 0; ; i++) {
1639 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1642 if (ealg->available)
1651 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1653 struct pfkey_sock *pfk = pfkey_sk(sk);
1654 struct sk_buff *supp_skb;
1656 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1659 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1660 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1662 pfk->registered |= (1<<hdr->sadb_msg_satype);
1667 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1669 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1670 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1675 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1680 static int key_notify_sa_flush(struct km_event *c)
1682 struct sk_buff *skb;
1683 struct sadb_msg *hdr;
1685 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1688 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1689 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1690 hdr->sadb_msg_type = SADB_FLUSH;
1691 hdr->sadb_msg_seq = c->seq;
1692 hdr->sadb_msg_pid = c->pid;
1693 hdr->sadb_msg_version = PF_KEY_V2;
1694 hdr->sadb_msg_errno = (uint8_t) 0;
1695 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1697 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1702 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1706 struct xfrm_audit audit_info;
1709 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1713 audit_info.loginuid = audit_get_loginuid(current);
1714 audit_info.sessionid = audit_get_sessionid(current);
1715 audit_info.secid = 0;
1716 err = xfrm_state_flush(proto, &audit_info);
1719 c.data.proto = proto;
1720 c.seq = hdr->sadb_msg_seq;
1721 c.pid = hdr->sadb_msg_pid;
1722 c.event = XFRM_MSG_FLUSHSA;
1723 km_state_notify(NULL, &c);
1728 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1730 struct pfkey_sock *pfk = ptr;
1731 struct sk_buff *out_skb;
1732 struct sadb_msg *out_hdr;
1734 if (!pfkey_can_dump(&pfk->sk))
1737 out_skb = pfkey_xfrm_state2msg(x);
1738 if (IS_ERR(out_skb))
1739 return PTR_ERR(out_skb);
1741 out_hdr = (struct sadb_msg *) out_skb->data;
1742 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1743 out_hdr->sadb_msg_type = SADB_DUMP;
1744 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1745 out_hdr->sadb_msg_errno = 0;
1746 out_hdr->sadb_msg_reserved = 0;
1747 out_hdr->sadb_msg_seq = count;
1748 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
1749 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
1753 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1755 return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk);
1758 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1760 xfrm_state_walk_done(&pfk->dump.u.state);
1763 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1766 struct pfkey_sock *pfk = pfkey_sk(sk);
1768 if (pfk->dump.dump != NULL)
1771 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1775 pfk->dump.msg_version = hdr->sadb_msg_version;
1776 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1777 pfk->dump.dump = pfkey_dump_sa;
1778 pfk->dump.done = pfkey_dump_sa_done;
1779 xfrm_state_walk_init(&pfk->dump.u.state, proto);
1781 return pfkey_do_dump(pfk);
1784 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1786 struct pfkey_sock *pfk = pfkey_sk(sk);
1787 int satype = hdr->sadb_msg_satype;
1789 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1790 /* XXX we mangle packet... */
1791 hdr->sadb_msg_errno = 0;
1792 if (satype != 0 && satype != 1)
1794 pfk->promisc = satype;
1796 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1800 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1803 u32 reqid = *(u32*)ptr;
1805 for (i=0; i<xp->xfrm_nr; i++) {
1806 if (xp->xfrm_vec[i].reqid == reqid)
1812 static u32 gen_reqid(void)
1814 struct xfrm_policy_walk walk;
1817 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1823 reqid = IPSEC_MANUAL_REQID_MAX+1;
1824 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1825 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1826 xfrm_policy_walk_done(&walk);
1829 } while (reqid != start);
1834 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1836 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1839 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1842 if (rq->sadb_x_ipsecrequest_mode == 0)
1845 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1846 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1849 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1851 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1852 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1853 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1855 if (!t->reqid && !(t->reqid = gen_reqid()))
1859 /* addresses present only in tunnel mode */
1860 if (t->mode == XFRM_MODE_TUNNEL) {
1861 u8 *sa = (u8 *) (rq + 1);
1862 int family, socklen;
1864 family = pfkey_sockaddr_extract((struct sockaddr *)sa,
1869 socklen = pfkey_sockaddr_len(family);
1870 if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
1871 &t->id.daddr) != family)
1873 t->encap_family = family;
1875 t->encap_family = xp->family;
1877 /* No way to set this via kame pfkey */
1884 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1887 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1888 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1890 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1891 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1893 len -= rq->sadb_x_ipsecrequest_len;
1894 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1899 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1901 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1904 int len = sizeof(struct sadb_x_sec_ctx);
1905 len += xfrm_ctx->ctx_len;
1906 return PFKEY_ALIGN8(len);
1911 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1913 struct xfrm_tmpl *t;
1914 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1918 for (i=0; i<xp->xfrm_nr; i++) {
1919 t = xp->xfrm_vec + i;
1920 socklen += pfkey_sockaddr_len(t->encap_family);
1923 return sizeof(struct sadb_msg) +
1924 (sizeof(struct sadb_lifetime) * 3) +
1925 (sizeof(struct sadb_address) * 2) +
1926 (sockaddr_size * 2) +
1927 sizeof(struct sadb_x_policy) +
1928 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1930 pfkey_xfrm_policy2sec_ctx_size(xp);
1933 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1935 struct sk_buff *skb;
1938 size = pfkey_xfrm_policy2msg_size(xp);
1940 skb = alloc_skb(size + 16, GFP_ATOMIC);
1942 return ERR_PTR(-ENOBUFS);
1947 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1949 struct sadb_msg *hdr;
1950 struct sadb_address *addr;
1951 struct sadb_lifetime *lifetime;
1952 struct sadb_x_policy *pol;
1953 struct sadb_x_sec_ctx *sec_ctx;
1954 struct xfrm_sec_ctx *xfrm_ctx;
1957 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1958 int socklen = pfkey_sockaddr_len(xp->family);
1960 size = pfkey_xfrm_policy2msg_size(xp);
1962 /* call should fill header later */
1963 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1964 memset(hdr, 0, size); /* XXX do we need this ? */
1967 addr = (struct sadb_address*) skb_put(skb,
1968 sizeof(struct sadb_address)+sockaddr_size);
1969 addr->sadb_address_len =
1970 (sizeof(struct sadb_address)+sockaddr_size)/
1972 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1973 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1974 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
1975 addr->sadb_address_reserved = 0;
1976 if (!pfkey_sockaddr_fill(&xp->selector.saddr,
1978 (struct sockaddr *) (addr + 1),
1983 addr = (struct sadb_address*) skb_put(skb,
1984 sizeof(struct sadb_address)+sockaddr_size);
1985 addr->sadb_address_len =
1986 (sizeof(struct sadb_address)+sockaddr_size)/
1988 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1989 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
1990 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
1991 addr->sadb_address_reserved = 0;
1993 pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
1994 (struct sockaddr *) (addr + 1),
1998 lifetime = (struct sadb_lifetime *) skb_put(skb,
1999 sizeof(struct sadb_lifetime));
2000 lifetime->sadb_lifetime_len =
2001 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2002 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2003 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2004 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2005 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2006 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2008 lifetime = (struct sadb_lifetime *) skb_put(skb,
2009 sizeof(struct sadb_lifetime));
2010 lifetime->sadb_lifetime_len =
2011 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2012 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2013 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2014 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2015 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2016 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2018 lifetime = (struct sadb_lifetime *) skb_put(skb,
2019 sizeof(struct sadb_lifetime));
2020 lifetime->sadb_lifetime_len =
2021 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2022 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2023 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2024 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2025 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2026 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2028 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2029 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2030 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2031 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2032 if (xp->action == XFRM_POLICY_ALLOW) {
2034 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2036 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2038 pol->sadb_x_policy_dir = dir+1;
2039 pol->sadb_x_policy_id = xp->index;
2040 pol->sadb_x_policy_priority = xp->priority;
2042 for (i=0; i<xp->xfrm_nr; i++) {
2043 struct sadb_x_ipsecrequest *rq;
2044 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2048 req_size = sizeof(struct sadb_x_ipsecrequest);
2049 if (t->mode == XFRM_MODE_TUNNEL) {
2050 socklen = pfkey_sockaddr_len(t->encap_family);
2051 req_size += socklen * 2;
2056 rq = (void*)skb_put(skb, req_size);
2057 pol->sadb_x_policy_len += req_size/8;
2058 memset(rq, 0, sizeof(*rq));
2059 rq->sadb_x_ipsecrequest_len = req_size;
2060 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2061 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2063 rq->sadb_x_ipsecrequest_mode = mode;
2064 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2066 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2068 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2069 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2071 if (t->mode == XFRM_MODE_TUNNEL) {
2072 u8 *sa = (void *)(rq + 1);
2073 pfkey_sockaddr_fill(&t->saddr, 0,
2074 (struct sockaddr *)sa,
2076 pfkey_sockaddr_fill(&t->id.daddr, 0,
2077 (struct sockaddr *) (sa + socklen),
2082 /* security context */
2083 if ((xfrm_ctx = xp->security)) {
2084 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2086 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2087 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2088 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2089 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2090 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2091 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2092 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2096 hdr->sadb_msg_len = size / sizeof(uint64_t);
2097 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2102 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2104 struct sk_buff *out_skb;
2105 struct sadb_msg *out_hdr;
2108 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2109 if (IS_ERR(out_skb)) {
2110 err = PTR_ERR(out_skb);
2113 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2117 out_hdr = (struct sadb_msg *) out_skb->data;
2118 out_hdr->sadb_msg_version = PF_KEY_V2;
2120 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2121 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2123 out_hdr->sadb_msg_type = event2poltype(c->event);
2124 out_hdr->sadb_msg_errno = 0;
2125 out_hdr->sadb_msg_seq = c->seq;
2126 out_hdr->sadb_msg_pid = c->pid;
2127 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2133 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2136 struct sadb_lifetime *lifetime;
2137 struct sadb_address *sa;
2138 struct sadb_x_policy *pol;
2139 struct xfrm_policy *xp;
2141 struct sadb_x_sec_ctx *sec_ctx;
2143 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2144 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2145 !ext_hdrs[SADB_X_EXT_POLICY-1])
2148 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2149 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2151 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2154 xp = xfrm_policy_alloc(GFP_KERNEL);
2158 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2159 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2160 xp->priority = pol->sadb_x_policy_priority;
2162 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2163 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2168 xp->selector.family = xp->family;
2169 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2170 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2171 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2172 if (xp->selector.sport)
2173 xp->selector.sport_mask = htons(0xffff);
2175 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2176 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2177 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2179 /* Amusing, we set this twice. KAME apps appear to set same value
2180 * in both addresses.
2182 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2184 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2185 if (xp->selector.dport)
2186 xp->selector.dport_mask = htons(0xffff);
2188 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2189 if (sec_ctx != NULL) {
2190 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2197 err = security_xfrm_policy_alloc(&xp->security, uctx);
2204 xp->lft.soft_byte_limit = XFRM_INF;
2205 xp->lft.hard_byte_limit = XFRM_INF;
2206 xp->lft.soft_packet_limit = XFRM_INF;
2207 xp->lft.hard_packet_limit = XFRM_INF;
2208 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2209 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2210 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2211 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2212 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2214 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2215 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2216 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2217 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2218 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2221 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2222 (err = parse_ipsecrequests(xp, pol)) < 0)
2225 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2226 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2228 xfrm_audit_policy_add(xp, err ? 0 : 1,
2229 audit_get_loginuid(current),
2230 audit_get_sessionid(current), 0);
2235 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2236 c.event = XFRM_MSG_UPDPOLICY;
2238 c.event = XFRM_MSG_NEWPOLICY;
2240 c.seq = hdr->sadb_msg_seq;
2241 c.pid = hdr->sadb_msg_pid;
2243 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2249 xfrm_policy_destroy(xp);
2253 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2256 struct sadb_address *sa;
2257 struct sadb_x_policy *pol;
2258 struct xfrm_policy *xp;
2259 struct xfrm_selector sel;
2261 struct sadb_x_sec_ctx *sec_ctx;
2262 struct xfrm_sec_ctx *pol_ctx = NULL;
2264 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2265 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2266 !ext_hdrs[SADB_X_EXT_POLICY-1])
2269 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2270 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2273 memset(&sel, 0, sizeof(sel));
2275 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2276 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2277 sel.prefixlen_s = sa->sadb_address_prefixlen;
2278 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2279 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2281 sel.sport_mask = htons(0xffff);
2283 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2284 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2285 sel.prefixlen_d = sa->sadb_address_prefixlen;
2286 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2287 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2289 sel.dport_mask = htons(0xffff);
2291 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2292 if (sec_ctx != NULL) {
2293 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2298 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
2304 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN,
2305 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2307 security_xfrm_policy_free(pol_ctx);
2311 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2312 audit_get_loginuid(current),
2313 audit_get_sessionid(current), 0);
2318 c.seq = hdr->sadb_msg_seq;
2319 c.pid = hdr->sadb_msg_pid;
2320 c.event = XFRM_MSG_DELPOLICY;
2321 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2328 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2331 struct sk_buff *out_skb;
2332 struct sadb_msg *out_hdr;
2335 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2336 if (IS_ERR(out_skb)) {
2337 err = PTR_ERR(out_skb);
2340 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2344 out_hdr = (struct sadb_msg *) out_skb->data;
2345 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2346 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2347 out_hdr->sadb_msg_satype = 0;
2348 out_hdr->sadb_msg_errno = 0;
2349 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2350 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2351 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2358 #ifdef CONFIG_NET_KEY_MIGRATE
2359 static int pfkey_sockaddr_pair_size(sa_family_t family)
2361 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2364 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2365 xfrm_address_t *saddr, xfrm_address_t *daddr,
2368 u8 *sa = (u8 *) (rq + 1);
2371 if (rq->sadb_x_ipsecrequest_len <
2372 pfkey_sockaddr_pair_size(((struct sockaddr *)sa)->sa_family))
2375 af = pfkey_sockaddr_extract((struct sockaddr *) sa,
2380 socklen = pfkey_sockaddr_len(af);
2381 if (pfkey_sockaddr_extract((struct sockaddr *) (sa + socklen),
2389 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2390 struct xfrm_migrate *m)
2393 struct sadb_x_ipsecrequest *rq2;
2396 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2397 len < rq1->sadb_x_ipsecrequest_len)
2401 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2406 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2407 len -= rq1->sadb_x_ipsecrequest_len;
2409 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2410 len < rq2->sadb_x_ipsecrequest_len)
2414 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2419 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2420 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2421 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2424 m->proto = rq1->sadb_x_ipsecrequest_proto;
2425 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2428 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2430 return ((int)(rq1->sadb_x_ipsecrequest_len +
2431 rq2->sadb_x_ipsecrequest_len));
2434 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2435 struct sadb_msg *hdr, void **ext_hdrs)
2437 int i, len, ret, err = -EINVAL;
2439 struct sadb_address *sa;
2440 struct sadb_x_policy *pol;
2441 struct sadb_x_ipsecrequest *rq;
2442 struct xfrm_selector sel;
2443 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2445 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2446 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2447 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2452 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2458 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2463 dir = pol->sadb_x_policy_dir - 1;
2464 memset(&sel, 0, sizeof(sel));
2466 /* set source address info of selector */
2467 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2468 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2469 sel.prefixlen_s = sa->sadb_address_prefixlen;
2470 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2471 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2473 sel.sport_mask = htons(0xffff);
2475 /* set destination address info of selector */
2476 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2477 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2478 sel.prefixlen_d = sa->sadb_address_prefixlen;
2479 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2480 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2482 sel.dport_mask = htons(0xffff);
2484 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2486 /* extract ipsecrequests */
2488 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2490 while (len > 0 && i < XFRM_MAX_DEPTH) {
2491 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2496 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2502 if (!i || len > 0) {
2507 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2513 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2514 struct sadb_msg *hdr, void **ext_hdrs)
2516 return -ENOPROTOOPT;
2521 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2524 int err = 0, delete;
2525 struct sadb_x_policy *pol;
2526 struct xfrm_policy *xp;
2529 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2532 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2533 if (dir >= XFRM_POLICY_MAX)
2536 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2537 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
2543 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2544 audit_get_loginuid(current),
2545 audit_get_sessionid(current), 0);
2549 c.seq = hdr->sadb_msg_seq;
2550 c.pid = hdr->sadb_msg_pid;
2552 c.event = XFRM_MSG_DELPOLICY;
2553 km_policy_notify(xp, dir, &c);
2555 err = key_pol_get_resp(sk, xp, hdr, dir);
2563 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2565 struct pfkey_sock *pfk = ptr;
2566 struct sk_buff *out_skb;
2567 struct sadb_msg *out_hdr;
2570 if (!pfkey_can_dump(&pfk->sk))
2573 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2574 if (IS_ERR(out_skb))
2575 return PTR_ERR(out_skb);
2577 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2581 out_hdr = (struct sadb_msg *) out_skb->data;
2582 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2583 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2584 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2585 out_hdr->sadb_msg_errno = 0;
2586 out_hdr->sadb_msg_seq = count;
2587 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
2588 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
2592 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2594 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2597 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2599 xfrm_policy_walk_done(&pfk->dump.u.policy);
2602 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2604 struct pfkey_sock *pfk = pfkey_sk(sk);
2606 if (pfk->dump.dump != NULL)
2609 pfk->dump.msg_version = hdr->sadb_msg_version;
2610 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2611 pfk->dump.dump = pfkey_dump_sp;
2612 pfk->dump.done = pfkey_dump_sp_done;
2613 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2615 return pfkey_do_dump(pfk);
2618 static int key_notify_policy_flush(struct km_event *c)
2620 struct sk_buff *skb_out;
2621 struct sadb_msg *hdr;
2623 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2626 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2627 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2628 hdr->sadb_msg_seq = c->seq;
2629 hdr->sadb_msg_pid = c->pid;
2630 hdr->sadb_msg_version = PF_KEY_V2;
2631 hdr->sadb_msg_errno = (uint8_t) 0;
2632 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2633 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2638 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2641 struct xfrm_audit audit_info;
2644 audit_info.loginuid = audit_get_loginuid(current);
2645 audit_info.sessionid = audit_get_sessionid(current);
2646 audit_info.secid = 0;
2647 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2650 c.data.type = XFRM_POLICY_TYPE_MAIN;
2651 c.event = XFRM_MSG_FLUSHPOLICY;
2652 c.pid = hdr->sadb_msg_pid;
2653 c.seq = hdr->sadb_msg_seq;
2654 km_policy_notify(NULL, 0, &c);
2659 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2660 struct sadb_msg *hdr, void **ext_hdrs);
2661 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2662 [SADB_RESERVED] = pfkey_reserved,
2663 [SADB_GETSPI] = pfkey_getspi,
2664 [SADB_UPDATE] = pfkey_add,
2665 [SADB_ADD] = pfkey_add,
2666 [SADB_DELETE] = pfkey_delete,
2667 [SADB_GET] = pfkey_get,
2668 [SADB_ACQUIRE] = pfkey_acquire,
2669 [SADB_REGISTER] = pfkey_register,
2670 [SADB_EXPIRE] = NULL,
2671 [SADB_FLUSH] = pfkey_flush,
2672 [SADB_DUMP] = pfkey_dump,
2673 [SADB_X_PROMISC] = pfkey_promisc,
2674 [SADB_X_PCHANGE] = NULL,
2675 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2676 [SADB_X_SPDADD] = pfkey_spdadd,
2677 [SADB_X_SPDDELETE] = pfkey_spddelete,
2678 [SADB_X_SPDGET] = pfkey_spdget,
2679 [SADB_X_SPDACQUIRE] = NULL,
2680 [SADB_X_SPDDUMP] = pfkey_spddump,
2681 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2682 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2683 [SADB_X_SPDDELETE2] = pfkey_spdget,
2684 [SADB_X_MIGRATE] = pfkey_migrate,
2687 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2689 void *ext_hdrs[SADB_EXT_MAX];
2692 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2693 BROADCAST_PROMISC_ONLY, NULL);
2695 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2696 err = parse_exthdrs(skb, hdr, ext_hdrs);
2699 if (pfkey_funcs[hdr->sadb_msg_type])
2700 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2705 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2707 struct sadb_msg *hdr = NULL;
2709 if (skb->len < sizeof(*hdr)) {
2712 hdr = (struct sadb_msg *) skb->data;
2713 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2714 hdr->sadb_msg_reserved != 0 ||
2715 (hdr->sadb_msg_type <= SADB_RESERVED ||
2716 hdr->sadb_msg_type > SADB_MAX)) {
2719 } else if (hdr->sadb_msg_len != (skb->len /
2720 sizeof(uint64_t)) ||
2721 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2722 sizeof(uint64_t))) {
2732 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2734 unsigned int id = d->desc.sadb_alg_id;
2736 if (id >= sizeof(t->aalgos) * 8)
2739 return (t->aalgos >> id) & 1;
2742 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2744 unsigned int id = d->desc.sadb_alg_id;
2746 if (id >= sizeof(t->ealgos) * 8)
2749 return (t->ealgos >> id) & 1;
2752 static int count_ah_combs(struct xfrm_tmpl *t)
2756 for (i = 0; ; i++) {
2757 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2760 if (aalg_tmpl_set(t, aalg) && aalg->available)
2761 sz += sizeof(struct sadb_comb);
2763 return sz + sizeof(struct sadb_prop);
2766 static int count_esp_combs(struct xfrm_tmpl *t)
2770 for (i = 0; ; i++) {
2771 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2775 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2778 for (k = 1; ; k++) {
2779 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2783 if (aalg_tmpl_set(t, aalg) && aalg->available)
2784 sz += sizeof(struct sadb_comb);
2787 return sz + sizeof(struct sadb_prop);
2790 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2792 struct sadb_prop *p;
2795 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2796 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2797 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2798 p->sadb_prop_replay = 32;
2799 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2801 for (i = 0; ; i++) {
2802 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2806 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2807 struct sadb_comb *c;
2808 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2809 memset(c, 0, sizeof(*c));
2810 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2811 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2812 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2813 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2814 c->sadb_comb_hard_addtime = 24*60*60;
2815 c->sadb_comb_soft_addtime = 20*60*60;
2816 c->sadb_comb_hard_usetime = 8*60*60;
2817 c->sadb_comb_soft_usetime = 7*60*60;
2822 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2824 struct sadb_prop *p;
2827 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2828 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2829 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2830 p->sadb_prop_replay = 32;
2831 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2834 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2838 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2841 for (k = 1; ; k++) {
2842 struct sadb_comb *c;
2843 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2846 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2848 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2849 memset(c, 0, sizeof(*c));
2850 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2851 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2852 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2853 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2854 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2855 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2856 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2857 c->sadb_comb_hard_addtime = 24*60*60;
2858 c->sadb_comb_soft_addtime = 20*60*60;
2859 c->sadb_comb_hard_usetime = 8*60*60;
2860 c->sadb_comb_soft_usetime = 7*60*60;
2865 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2870 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2872 struct sk_buff *out_skb;
2873 struct sadb_msg *out_hdr;
2877 hard = c->data.hard;
2883 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
2884 if (IS_ERR(out_skb))
2885 return PTR_ERR(out_skb);
2887 out_hdr = (struct sadb_msg *) out_skb->data;
2888 out_hdr->sadb_msg_version = PF_KEY_V2;
2889 out_hdr->sadb_msg_type = SADB_EXPIRE;
2890 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2891 out_hdr->sadb_msg_errno = 0;
2892 out_hdr->sadb_msg_reserved = 0;
2893 out_hdr->sadb_msg_seq = 0;
2894 out_hdr->sadb_msg_pid = 0;
2896 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
2900 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
2902 if (atomic_read(&pfkey_socks_nr) == 0)
2906 case XFRM_MSG_EXPIRE:
2907 return key_notify_sa_expire(x, c);
2908 case XFRM_MSG_DELSA:
2909 case XFRM_MSG_NEWSA:
2910 case XFRM_MSG_UPDSA:
2911 return key_notify_sa(x, c);
2912 case XFRM_MSG_FLUSHSA:
2913 return key_notify_sa_flush(c);
2914 case XFRM_MSG_NEWAE: /* not yet supported */
2917 printk("pfkey: Unknown SA event %d\n", c->event);
2924 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2926 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
2930 case XFRM_MSG_POLEXPIRE:
2931 return key_notify_policy_expire(xp, c);
2932 case XFRM_MSG_DELPOLICY:
2933 case XFRM_MSG_NEWPOLICY:
2934 case XFRM_MSG_UPDPOLICY:
2935 return key_notify_policy(xp, dir, c);
2936 case XFRM_MSG_FLUSHPOLICY:
2937 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
2939 return key_notify_policy_flush(c);
2941 printk("pfkey: Unknown policy event %d\n", c->event);
2948 static u32 get_acqseq(void)
2952 static DEFINE_SPINLOCK(acqseq_lock);
2954 spin_lock_bh(&acqseq_lock);
2955 res = (++acqseq ? : ++acqseq);
2956 spin_unlock_bh(&acqseq_lock);
2960 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
2962 struct sk_buff *skb;
2963 struct sadb_msg *hdr;
2964 struct sadb_address *addr;
2965 struct sadb_x_policy *pol;
2968 struct sadb_x_sec_ctx *sec_ctx;
2969 struct xfrm_sec_ctx *xfrm_ctx;
2972 sockaddr_size = pfkey_sockaddr_size(x->props.family);
2976 size = sizeof(struct sadb_msg) +
2977 (sizeof(struct sadb_address) * 2) +
2978 (sockaddr_size * 2) +
2979 sizeof(struct sadb_x_policy);
2981 if (x->id.proto == IPPROTO_AH)
2982 size += count_ah_combs(t);
2983 else if (x->id.proto == IPPROTO_ESP)
2984 size += count_esp_combs(t);
2986 if ((xfrm_ctx = x->security)) {
2987 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
2988 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
2991 skb = alloc_skb(size + 16, GFP_ATOMIC);
2995 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2996 hdr->sadb_msg_version = PF_KEY_V2;
2997 hdr->sadb_msg_type = SADB_ACQUIRE;
2998 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
2999 hdr->sadb_msg_len = size / sizeof(uint64_t);
3000 hdr->sadb_msg_errno = 0;
3001 hdr->sadb_msg_reserved = 0;
3002 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3003 hdr->sadb_msg_pid = 0;
3006 addr = (struct sadb_address*) skb_put(skb,
3007 sizeof(struct sadb_address)+sockaddr_size);
3008 addr->sadb_address_len =
3009 (sizeof(struct sadb_address)+sockaddr_size)/
3011 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3012 addr->sadb_address_proto = 0;
3013 addr->sadb_address_reserved = 0;
3014 addr->sadb_address_prefixlen =
3015 pfkey_sockaddr_fill(&x->props.saddr, 0,
3016 (struct sockaddr *) (addr + 1),
3018 if (!addr->sadb_address_prefixlen)
3022 addr = (struct sadb_address*) skb_put(skb,
3023 sizeof(struct sadb_address)+sockaddr_size);
3024 addr->sadb_address_len =
3025 (sizeof(struct sadb_address)+sockaddr_size)/
3027 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3028 addr->sadb_address_proto = 0;
3029 addr->sadb_address_reserved = 0;
3030 addr->sadb_address_prefixlen =
3031 pfkey_sockaddr_fill(&x->id.daddr, 0,
3032 (struct sockaddr *) (addr + 1),
3034 if (!addr->sadb_address_prefixlen)
3037 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3038 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3039 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3040 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3041 pol->sadb_x_policy_dir = dir+1;
3042 pol->sadb_x_policy_id = xp->index;
3044 /* Set sadb_comb's. */
3045 if (x->id.proto == IPPROTO_AH)
3046 dump_ah_combs(skb, t);
3047 else if (x->id.proto == IPPROTO_ESP)
3048 dump_esp_combs(skb, t);
3050 /* security context */
3052 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3053 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3054 sec_ctx->sadb_x_sec_len =
3055 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3056 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3057 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3058 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3059 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3060 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3064 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3067 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3068 u8 *data, int len, int *dir)
3070 struct xfrm_policy *xp;
3071 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3072 struct sadb_x_sec_ctx *sec_ctx;
3074 switch (sk->sk_family) {
3076 if (opt != IP_IPSEC_POLICY) {
3081 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3083 if (opt != IPV6_IPSEC_POLICY) {
3096 if (len < sizeof(struct sadb_x_policy) ||
3097 pol->sadb_x_policy_len*8 > len ||
3098 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3099 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3102 xp = xfrm_policy_alloc(GFP_ATOMIC);
3108 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3109 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3111 xp->lft.soft_byte_limit = XFRM_INF;
3112 xp->lft.hard_byte_limit = XFRM_INF;
3113 xp->lft.soft_packet_limit = XFRM_INF;
3114 xp->lft.hard_packet_limit = XFRM_INF;
3115 xp->family = sk->sk_family;
3118 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3119 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3122 /* security context too */
3123 if (len >= (pol->sadb_x_policy_len*8 +
3124 sizeof(struct sadb_x_sec_ctx))) {
3125 char *p = (char *)pol;
3126 struct xfrm_user_sec_ctx *uctx;
3128 p += pol->sadb_x_policy_len*8;
3129 sec_ctx = (struct sadb_x_sec_ctx *)p;
3130 if (len < pol->sadb_x_policy_len*8 +
3131 sec_ctx->sadb_x_sec_len) {
3135 if ((*dir = verify_sec_ctx_len(p)))
3137 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3138 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
3145 *dir = pol->sadb_x_policy_dir-1;
3149 xfrm_policy_destroy(xp);
3153 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3155 struct sk_buff *skb;
3156 struct sadb_msg *hdr;
3158 struct sadb_address *addr;
3159 struct sadb_x_nat_t_port *n_port;
3162 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3163 struct xfrm_encap_tmpl *natt = NULL;
3165 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3177 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3179 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3180 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3183 size = sizeof(struct sadb_msg) +
3184 sizeof(struct sadb_sa) +
3185 (sizeof(struct sadb_address) * 2) +
3186 (sockaddr_size * 2) +
3187 (sizeof(struct sadb_x_nat_t_port) * 2);
3189 skb = alloc_skb(size + 16, GFP_ATOMIC);
3193 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3194 hdr->sadb_msg_version = PF_KEY_V2;
3195 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3196 hdr->sadb_msg_satype = satype;
3197 hdr->sadb_msg_len = size / sizeof(uint64_t);
3198 hdr->sadb_msg_errno = 0;
3199 hdr->sadb_msg_reserved = 0;
3200 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3201 hdr->sadb_msg_pid = 0;
3204 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3205 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3206 sa->sadb_sa_exttype = SADB_EXT_SA;
3207 sa->sadb_sa_spi = x->id.spi;
3208 sa->sadb_sa_replay = 0;
3209 sa->sadb_sa_state = 0;
3210 sa->sadb_sa_auth = 0;
3211 sa->sadb_sa_encrypt = 0;
3212 sa->sadb_sa_flags = 0;
3214 /* ADDRESS_SRC (old addr) */
3215 addr = (struct sadb_address*)
3216 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3217 addr->sadb_address_len =
3218 (sizeof(struct sadb_address)+sockaddr_size)/
3220 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3221 addr->sadb_address_proto = 0;
3222 addr->sadb_address_reserved = 0;
3223 addr->sadb_address_prefixlen =
3224 pfkey_sockaddr_fill(&x->props.saddr, 0,
3225 (struct sockaddr *) (addr + 1),
3227 if (!addr->sadb_address_prefixlen)
3230 /* NAT_T_SPORT (old port) */
3231 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3232 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3233 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3234 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3235 n_port->sadb_x_nat_t_port_reserved = 0;
3237 /* ADDRESS_DST (new addr) */
3238 addr = (struct sadb_address*)
3239 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3240 addr->sadb_address_len =
3241 (sizeof(struct sadb_address)+sockaddr_size)/
3243 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3244 addr->sadb_address_proto = 0;
3245 addr->sadb_address_reserved = 0;
3246 addr->sadb_address_prefixlen =
3247 pfkey_sockaddr_fill(ipaddr, 0,
3248 (struct sockaddr *) (addr + 1),
3250 if (!addr->sadb_address_prefixlen)
3253 /* NAT_T_DPORT (new port) */
3254 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3255 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3256 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3257 n_port->sadb_x_nat_t_port_port = sport;
3258 n_port->sadb_x_nat_t_port_reserved = 0;
3260 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3263 #ifdef CONFIG_NET_KEY_MIGRATE
3264 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3265 struct xfrm_selector *sel)
3267 struct sadb_address *addr;
3268 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3269 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3270 addr->sadb_address_exttype = type;
3271 addr->sadb_address_proto = sel->proto;
3272 addr->sadb_address_reserved = 0;
3275 case SADB_EXT_ADDRESS_SRC:
3276 addr->sadb_address_prefixlen = sel->prefixlen_s;
3277 pfkey_sockaddr_fill(&sel->saddr, 0,
3278 (struct sockaddr *)(addr + 1),
3281 case SADB_EXT_ADDRESS_DST:
3282 addr->sadb_address_prefixlen = sel->prefixlen_d;
3283 pfkey_sockaddr_fill(&sel->daddr, 0,
3284 (struct sockaddr *)(addr + 1),
3294 static int set_ipsecrequest(struct sk_buff *skb,
3295 uint8_t proto, uint8_t mode, int level,
3296 uint32_t reqid, uint8_t family,
3297 xfrm_address_t *src, xfrm_address_t *dst)
3299 struct sadb_x_ipsecrequest *rq;
3301 int socklen = pfkey_sockaddr_len(family);
3304 size_req = sizeof(struct sadb_x_ipsecrequest) +
3305 pfkey_sockaddr_pair_size(family);
3307 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3308 memset(rq, 0, size_req);
3309 rq->sadb_x_ipsecrequest_len = size_req;
3310 rq->sadb_x_ipsecrequest_proto = proto;
3311 rq->sadb_x_ipsecrequest_mode = mode;
3312 rq->sadb_x_ipsecrequest_level = level;
3313 rq->sadb_x_ipsecrequest_reqid = reqid;
3315 sa = (u8 *) (rq + 1);
3316 if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3317 !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3324 #ifdef CONFIG_NET_KEY_MIGRATE
3325 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3326 struct xfrm_migrate *m, int num_bundles)
3332 struct sk_buff *skb;
3333 struct sadb_msg *hdr;
3334 struct sadb_x_policy *pol;
3335 struct xfrm_migrate *mp;
3337 if (type != XFRM_POLICY_TYPE_MAIN)
3340 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3344 sasize_sel = pfkey_sockaddr_size(sel->family);
3347 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3350 size_pol += sizeof(struct sadb_x_policy);
3353 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3354 /* old locator pair */
3355 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3356 pfkey_sockaddr_pair_size(mp->old_family);
3357 /* new locator pair */
3358 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3359 pfkey_sockaddr_pair_size(mp->new_family);
3362 size += sizeof(struct sadb_msg) + size_pol;
3365 skb = alloc_skb(size, GFP_ATOMIC);
3369 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3370 hdr->sadb_msg_version = PF_KEY_V2;
3371 hdr->sadb_msg_type = SADB_X_MIGRATE;
3372 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3373 hdr->sadb_msg_len = size / 8;
3374 hdr->sadb_msg_errno = 0;
3375 hdr->sadb_msg_reserved = 0;
3376 hdr->sadb_msg_seq = 0;
3377 hdr->sadb_msg_pid = 0;
3380 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3383 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3385 /* policy information */
3386 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3387 pol->sadb_x_policy_len = size_pol / 8;
3388 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3389 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3390 pol->sadb_x_policy_dir = dir + 1;
3391 pol->sadb_x_policy_id = 0;
3392 pol->sadb_x_policy_priority = 0;
3394 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3395 /* old ipsecrequest */
3396 int mode = pfkey_mode_from_xfrm(mp->mode);
3399 if (set_ipsecrequest(skb, mp->proto, mode,
3400 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3401 mp->reqid, mp->old_family,
3402 &mp->old_saddr, &mp->old_daddr) < 0)
3405 /* new ipsecrequest */
3406 if (set_ipsecrequest(skb, mp->proto, mode,
3407 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3408 mp->reqid, mp->new_family,
3409 &mp->new_saddr, &mp->new_daddr) < 0)
3413 /* broadcast migrate message to sockets */
3414 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3423 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3424 struct xfrm_migrate *m, int num_bundles)
3426 return -ENOPROTOOPT;
3430 static int pfkey_sendmsg(struct kiocb *kiocb,
3431 struct socket *sock, struct msghdr *msg, size_t len)
3433 struct sock *sk = sock->sk;
3434 struct sk_buff *skb = NULL;
3435 struct sadb_msg *hdr = NULL;
3439 if (msg->msg_flags & MSG_OOB)
3443 if ((unsigned)len > sk->sk_sndbuf - 32)
3447 skb = alloc_skb(len, GFP_KERNEL);
3452 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3455 hdr = pfkey_get_base_msg(skb, &err);
3459 mutex_lock(&xfrm_cfg_mutex);
3460 err = pfkey_process(sk, skb, hdr);
3461 mutex_unlock(&xfrm_cfg_mutex);
3464 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3472 static int pfkey_recvmsg(struct kiocb *kiocb,
3473 struct socket *sock, struct msghdr *msg, size_t len,
3476 struct sock *sk = sock->sk;
3477 struct pfkey_sock *pfk = pfkey_sk(sk);
3478 struct sk_buff *skb;
3482 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3485 msg->msg_namelen = 0;
3486 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3492 msg->msg_flags |= MSG_TRUNC;
3496 skb_reset_transport_header(skb);
3497 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3501 sock_recv_timestamp(msg, sk, skb);
3503 err = (flags & MSG_TRUNC) ? skb->len : copied;
3505 if (pfk->dump.dump != NULL &&
3506 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3510 skb_free_datagram(sk, skb);
3515 static const struct proto_ops pfkey_ops = {
3517 .owner = THIS_MODULE,
3518 /* Operations that make no sense on pfkey sockets. */
3519 .bind = sock_no_bind,
3520 .connect = sock_no_connect,
3521 .socketpair = sock_no_socketpair,
3522 .accept = sock_no_accept,
3523 .getname = sock_no_getname,
3524 .ioctl = sock_no_ioctl,
3525 .listen = sock_no_listen,
3526 .shutdown = sock_no_shutdown,
3527 .setsockopt = sock_no_setsockopt,
3528 .getsockopt = sock_no_getsockopt,
3529 .mmap = sock_no_mmap,
3530 .sendpage = sock_no_sendpage,
3532 /* Now the operations that really occur. */
3533 .release = pfkey_release,
3534 .poll = datagram_poll,
3535 .sendmsg = pfkey_sendmsg,
3536 .recvmsg = pfkey_recvmsg,
3539 static struct net_proto_family pfkey_family_ops = {
3541 .create = pfkey_create,
3542 .owner = THIS_MODULE,
3545 #ifdef CONFIG_PROC_FS
3546 static int pfkey_seq_show(struct seq_file *f, void *v)
3550 s = (struct sock *)v;
3551 if (v == SEQ_START_TOKEN)
3552 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3554 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3556 atomic_read(&s->sk_refcnt),
3557 atomic_read(&s->sk_rmem_alloc),
3558 atomic_read(&s->sk_wmem_alloc),
3565 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3568 struct hlist_node *node;
3571 read_lock(&pfkey_table_lock);
3573 return SEQ_START_TOKEN;
3575 sk_for_each(s, node, &pfkey_table)
3582 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3585 return (v == SEQ_START_TOKEN) ?
3586 sk_head(&pfkey_table) :
3587 sk_next((struct sock *)v);
3590 static void pfkey_seq_stop(struct seq_file *f, void *v)
3592 read_unlock(&pfkey_table_lock);
3595 static struct seq_operations pfkey_seq_ops = {
3596 .start = pfkey_seq_start,
3597 .next = pfkey_seq_next,
3598 .stop = pfkey_seq_stop,
3599 .show = pfkey_seq_show,
3602 static int pfkey_seq_open(struct inode *inode, struct file *file)
3604 return seq_open(file, &pfkey_seq_ops);
3607 static struct file_operations pfkey_proc_ops = {
3608 .open = pfkey_seq_open,
3610 .llseek = seq_lseek,
3611 .release = seq_release,
3614 static int pfkey_init_proc(void)
3616 struct proc_dir_entry *e;
3618 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
3625 static void pfkey_exit_proc(void)
3627 proc_net_remove(&init_net, "pfkey");
3630 static inline int pfkey_init_proc(void)
3635 static inline void pfkey_exit_proc(void)
3640 static struct xfrm_mgr pfkeyv2_mgr =
3643 .notify = pfkey_send_notify,
3644 .acquire = pfkey_send_acquire,
3645 .compile_policy = pfkey_compile_policy,
3646 .new_mapping = pfkey_send_new_mapping,
3647 .notify_policy = pfkey_send_policy_notify,
3648 .migrate = pfkey_send_migrate,
3651 static void __exit ipsec_pfkey_exit(void)
3653 xfrm_unregister_km(&pfkeyv2_mgr);
3655 sock_unregister(PF_KEY);
3656 proto_unregister(&key_proto);
3659 static int __init ipsec_pfkey_init(void)
3661 int err = proto_register(&key_proto, 0);
3666 err = sock_register(&pfkey_family_ops);
3668 goto out_unregister_key_proto;
3669 err = pfkey_init_proc();
3671 goto out_sock_unregister;
3672 err = xfrm_register_km(&pfkeyv2_mgr);
3674 goto out_remove_proc_entry;
3677 out_remove_proc_entry:
3679 out_sock_unregister:
3680 sock_unregister(PF_KEY);
3681 out_unregister_key_proto:
3682 proto_unregister(&key_proto);
3686 module_init(ipsec_pfkey_init);
3687 module_exit(ipsec_pfkey_exit);
3688 MODULE_LICENSE("GPL");
3689 MODULE_ALIAS_NETPROTO(PF_KEY);