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 int pfkey_do_dump(struct pfkey_sock *pfk)
80 rc = pfk->dump.dump(pfk);
85 pfk->dump.dump = NULL;
86 pfk->dump.done = NULL;
90 static void pfkey_sock_destruct(struct sock *sk)
92 skb_queue_purge(&sk->sk_receive_queue);
94 if (!sock_flag(sk, SOCK_DEAD)) {
95 printk("Attempt to release alive pfkey socket: %p\n", sk);
99 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
100 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
102 atomic_dec(&pfkey_socks_nr);
105 static void pfkey_table_grab(void)
107 write_lock_bh(&pfkey_table_lock);
109 if (atomic_read(&pfkey_table_users)) {
110 DECLARE_WAITQUEUE(wait, current);
112 add_wait_queue_exclusive(&pfkey_table_wait, &wait);
114 set_current_state(TASK_UNINTERRUPTIBLE);
115 if (atomic_read(&pfkey_table_users) == 0)
117 write_unlock_bh(&pfkey_table_lock);
119 write_lock_bh(&pfkey_table_lock);
122 __set_current_state(TASK_RUNNING);
123 remove_wait_queue(&pfkey_table_wait, &wait);
127 static __inline__ void pfkey_table_ungrab(void)
129 write_unlock_bh(&pfkey_table_lock);
130 wake_up(&pfkey_table_wait);
133 static __inline__ void pfkey_lock_table(void)
135 /* read_lock() synchronizes us to pfkey_table_grab */
137 read_lock(&pfkey_table_lock);
138 atomic_inc(&pfkey_table_users);
139 read_unlock(&pfkey_table_lock);
142 static __inline__ void pfkey_unlock_table(void)
144 if (atomic_dec_and_test(&pfkey_table_users))
145 wake_up(&pfkey_table_wait);
149 static const struct proto_ops pfkey_ops;
151 static void pfkey_insert(struct sock *sk)
154 sk_add_node(sk, &pfkey_table);
155 pfkey_table_ungrab();
158 static void pfkey_remove(struct sock *sk)
161 sk_del_node_init(sk);
162 pfkey_table_ungrab();
165 static struct proto key_proto = {
167 .owner = THIS_MODULE,
168 .obj_size = sizeof(struct pfkey_sock),
171 static int pfkey_create(struct net *net, struct socket *sock, int protocol)
176 if (net != &init_net)
177 return -EAFNOSUPPORT;
179 if (!capable(CAP_NET_ADMIN))
181 if (sock->type != SOCK_RAW)
182 return -ESOCKTNOSUPPORT;
183 if (protocol != PF_KEY_V2)
184 return -EPROTONOSUPPORT;
187 sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
191 sock->ops = &pfkey_ops;
192 sock_init_data(sock, sk);
194 sk->sk_family = PF_KEY;
195 sk->sk_destruct = pfkey_sock_destruct;
197 atomic_inc(&pfkey_socks_nr);
206 static int pfkey_release(struct socket *sock)
208 struct sock *sk = sock->sk;
217 skb_queue_purge(&sk->sk_write_queue);
223 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
224 gfp_t allocation, struct sock *sk)
230 if (atomic_read(&skb->users) != 1) {
231 *skb2 = skb_clone(skb, allocation);
234 atomic_inc(&skb->users);
238 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
240 skb_set_owner_r(*skb2, sk);
241 skb_queue_tail(&sk->sk_receive_queue, *skb2);
242 sk->sk_data_ready(sk, (*skb2)->len);
251 /* Send SKB to all pfkey sockets matching selected criteria. */
252 #define BROADCAST_ALL 0
253 #define BROADCAST_ONE 1
254 #define BROADCAST_REGISTERED 2
255 #define BROADCAST_PROMISC_ONLY 4
256 static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
257 int broadcast_flags, struct sock *one_sk)
260 struct hlist_node *node;
261 struct sk_buff *skb2 = NULL;
264 /* XXX Do we need something like netlink_overrun? I think
265 * XXX PF_KEY socket apps will not mind current behavior.
271 sk_for_each(sk, node, &pfkey_table) {
272 struct pfkey_sock *pfk = pfkey_sk(sk);
275 /* Yes, it means that if you are meant to receive this
276 * pfkey message you receive it twice as promiscuous
280 pfkey_broadcast_one(skb, &skb2, allocation, sk);
282 /* the exact target will be processed later */
285 if (broadcast_flags != BROADCAST_ALL) {
286 if (broadcast_flags & BROADCAST_PROMISC_ONLY)
288 if ((broadcast_flags & BROADCAST_REGISTERED) &&
291 if (broadcast_flags & BROADCAST_ONE)
295 err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);
297 /* Error is cleare after succecful sending to at least one
299 if ((broadcast_flags & BROADCAST_REGISTERED) && err)
302 pfkey_unlock_table();
305 err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
313 static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
318 static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
320 struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
321 struct sadb_msg *hdr;
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
330 if (err == ERESTARTSYS ||
331 err == ERESTARTNOHAND ||
332 err == ERESTARTNOINTR)
336 BUG_ON(err <= 0 || err >= 256);
338 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
339 pfkey_hdr_dup(hdr, orig);
340 hdr->sadb_msg_errno = (uint8_t) err;
341 hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
344 pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);
349 static u8 sadb_ext_min_len[] = {
350 [SADB_EXT_RESERVED] = (u8) 0,
351 [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
352 [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
353 [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
354 [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
355 [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
356 [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
357 [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
358 [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
359 [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
360 [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
361 [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
362 [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
363 [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
364 [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
365 [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
366 [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
367 [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
368 [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
369 [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
370 [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
371 [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
372 [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
373 [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
374 [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
377 /* Verify sadb_address_{len,prefixlen} against sa_family. */
378 static int verify_address_len(void *p)
380 struct sadb_address *sp = p;
381 struct sockaddr *addr = (struct sockaddr *)(sp + 1);
382 struct sockaddr_in *sin;
383 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
384 struct sockaddr_in6 *sin6;
388 switch (addr->sa_family) {
390 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
391 if (sp->sadb_address_len != len ||
392 sp->sadb_address_prefixlen > 32)
395 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
397 len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
398 if (sp->sadb_address_len != len ||
399 sp->sadb_address_prefixlen > 128)
404 /* It is user using kernel to keep track of security
405 * associations for another protocol, such as
406 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
409 * XXX Actually, association/policy database is not yet
410 * XXX able to cope with arbitrary sockaddr families.
411 * XXX When it can, remove this -EINVAL. -DaveM
420 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx *sec_ctx)
422 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
423 sec_ctx->sadb_x_ctx_len,
427 static inline int verify_sec_ctx_len(void *p)
429 struct sadb_x_sec_ctx *sec_ctx = (struct sadb_x_sec_ctx *)p;
430 int len = sec_ctx->sadb_x_ctx_len;
435 len = pfkey_sec_ctx_len(sec_ctx);
437 if (sec_ctx->sadb_x_sec_len != len)
443 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx *sec_ctx)
445 struct xfrm_user_sec_ctx *uctx = NULL;
446 int ctx_size = sec_ctx->sadb_x_ctx_len;
448 uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);
453 uctx->len = pfkey_sec_ctx_len(sec_ctx);
454 uctx->exttype = sec_ctx->sadb_x_sec_exttype;
455 uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
456 uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
457 uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
458 memcpy(uctx + 1, sec_ctx + 1,
464 static int present_and_same_family(struct sadb_address *src,
465 struct sadb_address *dst)
467 struct sockaddr *s_addr, *d_addr;
472 s_addr = (struct sockaddr *)(src + 1);
473 d_addr = (struct sockaddr *)(dst + 1);
474 if (s_addr->sa_family != d_addr->sa_family)
476 if (s_addr->sa_family != AF_INET
477 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
478 && s_addr->sa_family != AF_INET6
486 static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
488 char *p = (char *) hdr;
494 struct sadb_ext *ehdr = (struct sadb_ext *) p;
498 ext_len = ehdr->sadb_ext_len;
499 ext_len *= sizeof(uint64_t);
500 ext_type = ehdr->sadb_ext_type;
501 if (ext_len < sizeof(uint64_t) ||
503 ext_type == SADB_EXT_RESERVED)
506 if (ext_type <= SADB_EXT_MAX) {
507 int min = (int) sadb_ext_min_len[ext_type];
510 if (ext_hdrs[ext_type-1] != NULL)
512 if (ext_type == SADB_EXT_ADDRESS_SRC ||
513 ext_type == SADB_EXT_ADDRESS_DST ||
514 ext_type == SADB_EXT_ADDRESS_PROXY ||
515 ext_type == SADB_X_EXT_NAT_T_OA) {
516 if (verify_address_len(p))
519 if (ext_type == SADB_X_EXT_SEC_CTX) {
520 if (verify_sec_ctx_len(p))
523 ext_hdrs[ext_type-1] = p;
533 pfkey_satype2proto(uint8_t satype)
536 case SADB_SATYPE_UNSPEC:
537 return IPSEC_PROTO_ANY;
540 case SADB_SATYPE_ESP:
542 case SADB_X_SATYPE_IPCOMP:
552 pfkey_proto2satype(uint16_t proto)
556 return SADB_SATYPE_AH;
558 return SADB_SATYPE_ESP;
560 return SADB_X_SATYPE_IPCOMP;
568 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
569 * say specifically 'just raw sockets' as we encode them as 255.
572 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
574 return (proto == IPSEC_PROTO_ANY ? 0 : proto);
577 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
579 return (proto ? proto : IPSEC_PROTO_ANY);
582 static inline int pfkey_sockaddr_len(sa_family_t family)
586 return sizeof(struct sockaddr_in);
587 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
589 return sizeof(struct sockaddr_in6);
595 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
596 xfrm_address_t *xaddr)
598 switch (((struct sockaddr*)(addr + 1))->sa_family) {
601 ((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
603 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
606 &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
607 sizeof(struct in6_addr));
616 static struct xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
619 struct sadb_address *addr;
621 unsigned short family;
622 xfrm_address_t *xaddr;
624 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
628 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
632 /* sadb_address_len should be checked by caller */
633 addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
637 family = ((struct sockaddr *)(addr + 1))->sa_family;
640 xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
642 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
644 xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
654 return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
657 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
660 pfkey_sockaddr_size(sa_family_t family)
662 return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
665 static inline int pfkey_mode_from_xfrm(int mode)
668 case XFRM_MODE_TRANSPORT:
669 return IPSEC_MODE_TRANSPORT;
670 case XFRM_MODE_TUNNEL:
671 return IPSEC_MODE_TUNNEL;
673 return IPSEC_MODE_BEET;
679 static inline int pfkey_mode_to_xfrm(int mode)
682 case IPSEC_MODE_ANY: /*XXX*/
683 case IPSEC_MODE_TRANSPORT:
684 return XFRM_MODE_TRANSPORT;
685 case IPSEC_MODE_TUNNEL:
686 return XFRM_MODE_TUNNEL;
687 case IPSEC_MODE_BEET:
688 return XFRM_MODE_BEET;
694 static struct sk_buff *__pfkey_xfrm_state2msg(struct xfrm_state *x,
695 int add_keys, int hsc)
698 struct sadb_msg *hdr;
700 struct sadb_lifetime *lifetime;
701 struct sadb_address *addr;
702 struct sadb_key *key;
703 struct sadb_x_sa2 *sa2;
704 struct sockaddr_in *sin;
705 struct sadb_x_sec_ctx *sec_ctx;
706 struct xfrm_sec_ctx *xfrm_ctx;
708 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
709 struct sockaddr_in6 *sin6;
712 int auth_key_size = 0;
713 int encrypt_key_size = 0;
715 struct xfrm_encap_tmpl *natt = NULL;
718 /* address family check */
719 sockaddr_size = pfkey_sockaddr_size(x->props.family);
721 return ERR_PTR(-EINVAL);
723 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
724 key(AE), (identity(SD),) (sensitivity)> */
725 size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
726 sizeof(struct sadb_lifetime) +
727 ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
728 ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
729 sizeof(struct sadb_address)*2 +
731 sizeof(struct sadb_x_sa2);
733 if ((xfrm_ctx = x->security)) {
734 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
735 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
738 /* identity & sensitivity */
740 if ((x->props.family == AF_INET &&
741 x->sel.saddr.a4 != x->props.saddr.a4)
742 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
743 || (x->props.family == AF_INET6 &&
744 memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
747 size += sizeof(struct sadb_address) + sockaddr_size;
750 if (x->aalg && x->aalg->alg_key_len) {
752 PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
753 size += sizeof(struct sadb_key) + auth_key_size;
755 if (x->ealg && x->ealg->alg_key_len) {
757 PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
758 size += sizeof(struct sadb_key) + encrypt_key_size;
764 if (natt && natt->encap_type) {
765 size += sizeof(struct sadb_x_nat_t_type);
766 size += sizeof(struct sadb_x_nat_t_port);
767 size += sizeof(struct sadb_x_nat_t_port);
770 skb = alloc_skb(size + 16, GFP_ATOMIC);
772 return ERR_PTR(-ENOBUFS);
774 /* call should fill header later */
775 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
776 memset(hdr, 0, size); /* XXX do we need this ? */
777 hdr->sadb_msg_len = size / sizeof(uint64_t);
780 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
781 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
782 sa->sadb_sa_exttype = SADB_EXT_SA;
783 sa->sadb_sa_spi = x->id.spi;
784 sa->sadb_sa_replay = x->props.replay_window;
785 switch (x->km.state) {
786 case XFRM_STATE_VALID:
787 sa->sadb_sa_state = x->km.dying ?
788 SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
791 sa->sadb_sa_state = SADB_SASTATE_LARVAL;
794 sa->sadb_sa_state = SADB_SASTATE_DEAD;
797 sa->sadb_sa_auth = 0;
799 struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
800 sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
802 sa->sadb_sa_encrypt = 0;
803 BUG_ON(x->ealg && x->calg);
805 struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
806 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
808 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
810 struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
811 sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
814 sa->sadb_sa_flags = 0;
815 if (x->props.flags & XFRM_STATE_NOECN)
816 sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
817 if (x->props.flags & XFRM_STATE_DECAP_DSCP)
818 sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
819 if (x->props.flags & XFRM_STATE_NOPMTUDISC)
820 sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
824 lifetime = (struct sadb_lifetime *) skb_put(skb,
825 sizeof(struct sadb_lifetime));
826 lifetime->sadb_lifetime_len =
827 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
828 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
829 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
830 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
831 lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
832 lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
836 lifetime = (struct sadb_lifetime *) skb_put(skb,
837 sizeof(struct sadb_lifetime));
838 lifetime->sadb_lifetime_len =
839 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
840 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
841 lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
842 lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
843 lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
844 lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
847 lifetime = (struct sadb_lifetime *) skb_put(skb,
848 sizeof(struct sadb_lifetime));
849 lifetime->sadb_lifetime_len =
850 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
851 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
852 lifetime->sadb_lifetime_allocations = x->curlft.packets;
853 lifetime->sadb_lifetime_bytes = x->curlft.bytes;
854 lifetime->sadb_lifetime_addtime = x->curlft.add_time;
855 lifetime->sadb_lifetime_usetime = x->curlft.use_time;
857 addr = (struct sadb_address*) skb_put(skb,
858 sizeof(struct sadb_address)+sockaddr_size);
859 addr->sadb_address_len =
860 (sizeof(struct sadb_address)+sockaddr_size)/
862 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
863 /* "if the ports are non-zero, then the sadb_address_proto field,
864 normally zero, MUST be filled in with the transport
865 protocol's number." - RFC2367 */
866 addr->sadb_address_proto = 0;
867 addr->sadb_address_reserved = 0;
868 if (x->props.family == AF_INET) {
869 addr->sadb_address_prefixlen = 32;
871 sin = (struct sockaddr_in *) (addr + 1);
872 sin->sin_family = AF_INET;
873 sin->sin_addr.s_addr = x->props.saddr.a4;
875 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
877 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
878 else if (x->props.family == AF_INET6) {
879 addr->sadb_address_prefixlen = 128;
881 sin6 = (struct sockaddr_in6 *) (addr + 1);
882 sin6->sin6_family = AF_INET6;
884 sin6->sin6_flowinfo = 0;
885 memcpy(&sin6->sin6_addr, x->props.saddr.a6,
886 sizeof(struct in6_addr));
887 sin6->sin6_scope_id = 0;
894 addr = (struct sadb_address*) skb_put(skb,
895 sizeof(struct sadb_address)+sockaddr_size);
896 addr->sadb_address_len =
897 (sizeof(struct sadb_address)+sockaddr_size)/
899 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
900 addr->sadb_address_proto = 0;
901 addr->sadb_address_prefixlen = 32; /* XXX */
902 addr->sadb_address_reserved = 0;
903 if (x->props.family == AF_INET) {
904 sin = (struct sockaddr_in *) (addr + 1);
905 sin->sin_family = AF_INET;
906 sin->sin_addr.s_addr = x->id.daddr.a4;
908 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
910 if (x->sel.saddr.a4 != x->props.saddr.a4) {
911 addr = (struct sadb_address*) skb_put(skb,
912 sizeof(struct sadb_address)+sockaddr_size);
913 addr->sadb_address_len =
914 (sizeof(struct sadb_address)+sockaddr_size)/
916 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
917 addr->sadb_address_proto =
918 pfkey_proto_from_xfrm(x->sel.proto);
919 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
920 addr->sadb_address_reserved = 0;
922 sin = (struct sockaddr_in *) (addr + 1);
923 sin->sin_family = AF_INET;
924 sin->sin_addr.s_addr = x->sel.saddr.a4;
925 sin->sin_port = x->sel.sport;
926 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
929 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
930 else if (x->props.family == AF_INET6) {
931 addr->sadb_address_prefixlen = 128;
933 sin6 = (struct sockaddr_in6 *) (addr + 1);
934 sin6->sin6_family = AF_INET6;
936 sin6->sin6_flowinfo = 0;
937 memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
938 sin6->sin6_scope_id = 0;
940 if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
941 sizeof(struct in6_addr))) {
942 addr = (struct sadb_address *) skb_put(skb,
943 sizeof(struct sadb_address)+sockaddr_size);
944 addr->sadb_address_len =
945 (sizeof(struct sadb_address)+sockaddr_size)/
947 addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
948 addr->sadb_address_proto =
949 pfkey_proto_from_xfrm(x->sel.proto);
950 addr->sadb_address_prefixlen = x->sel.prefixlen_s;
951 addr->sadb_address_reserved = 0;
953 sin6 = (struct sockaddr_in6 *) (addr + 1);
954 sin6->sin6_family = AF_INET6;
955 sin6->sin6_port = x->sel.sport;
956 sin6->sin6_flowinfo = 0;
957 memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
958 sizeof(struct in6_addr));
959 sin6->sin6_scope_id = 0;
967 if (add_keys && auth_key_size) {
968 key = (struct sadb_key *) skb_put(skb,
969 sizeof(struct sadb_key)+auth_key_size);
970 key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
972 key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
973 key->sadb_key_bits = x->aalg->alg_key_len;
974 key->sadb_key_reserved = 0;
975 memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
978 if (add_keys && encrypt_key_size) {
979 key = (struct sadb_key *) skb_put(skb,
980 sizeof(struct sadb_key)+encrypt_key_size);
981 key->sadb_key_len = (sizeof(struct sadb_key) +
982 encrypt_key_size) / sizeof(uint64_t);
983 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
984 key->sadb_key_bits = x->ealg->alg_key_len;
985 key->sadb_key_reserved = 0;
986 memcpy(key + 1, x->ealg->alg_key,
987 (x->ealg->alg_key_len+7)/8);
991 sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
992 sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
993 sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
994 if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
996 return ERR_PTR(-EINVAL);
998 sa2->sadb_x_sa2_mode = mode;
999 sa2->sadb_x_sa2_reserved1 = 0;
1000 sa2->sadb_x_sa2_reserved2 = 0;
1001 sa2->sadb_x_sa2_sequence = 0;
1002 sa2->sadb_x_sa2_reqid = x->props.reqid;
1004 if (natt && natt->encap_type) {
1005 struct sadb_x_nat_t_type *n_type;
1006 struct sadb_x_nat_t_port *n_port;
1009 n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
1010 n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
1011 n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
1012 n_type->sadb_x_nat_t_type_type = natt->encap_type;
1013 n_type->sadb_x_nat_t_type_reserved[0] = 0;
1014 n_type->sadb_x_nat_t_type_reserved[1] = 0;
1015 n_type->sadb_x_nat_t_type_reserved[2] = 0;
1018 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1019 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1020 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1021 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1022 n_port->sadb_x_nat_t_port_reserved = 0;
1025 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
1026 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1027 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1028 n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1029 n_port->sadb_x_nat_t_port_reserved = 0;
1032 /* security context */
1034 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
1035 sizeof(struct sadb_x_sec_ctx) + ctx_size);
1036 sec_ctx->sadb_x_sec_len =
1037 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1038 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1039 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1040 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1041 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1042 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1050 static inline struct sk_buff *pfkey_xfrm_state2msg(struct xfrm_state *x)
1052 struct sk_buff *skb;
1054 skb = __pfkey_xfrm_state2msg(x, 1, 3);
1059 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(struct xfrm_state *x,
1062 return __pfkey_xfrm_state2msg(x, 0, hsc);
1065 static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr,
1068 struct xfrm_state *x;
1069 struct sadb_lifetime *lifetime;
1071 struct sadb_key *key;
1072 struct sadb_x_sec_ctx *sec_ctx;
1077 sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
1079 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1080 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1081 return ERR_PTR(-EINVAL);
1082 if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1083 !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1084 return ERR_PTR(-EINVAL);
1085 if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1086 !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1087 return ERR_PTR(-EINVAL);
1088 if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1089 !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1090 return ERR_PTR(-EINVAL);
1092 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1094 return ERR_PTR(-EINVAL);
1096 /* default error is no buffer space */
1101 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1102 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1103 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1104 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1105 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1108 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1109 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1111 if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1112 (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1113 sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1114 sa->sadb_sa_encrypt > SADB_EALG_MAX)
1115 return ERR_PTR(-EINVAL);
1116 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1118 sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1119 ((key->sadb_key_bits+7) / 8 == 0 ||
1120 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1121 return ERR_PTR(-EINVAL);
1122 key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1124 sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1125 ((key->sadb_key_bits+7) / 8 == 0 ||
1126 (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1127 return ERR_PTR(-EINVAL);
1129 x = xfrm_state_alloc();
1131 return ERR_PTR(-ENOBUFS);
1133 x->id.proto = proto;
1134 x->id.spi = sa->sadb_sa_spi;
1135 x->props.replay_window = sa->sadb_sa_replay;
1136 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1137 x->props.flags |= XFRM_STATE_NOECN;
1138 if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1139 x->props.flags |= XFRM_STATE_DECAP_DSCP;
1140 if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1141 x->props.flags |= XFRM_STATE_NOPMTUDISC;
1143 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
1144 if (lifetime != NULL) {
1145 x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1146 x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1147 x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1148 x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1150 lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
1151 if (lifetime != NULL) {
1152 x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1153 x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1154 x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1155 x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1158 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
1159 if (sec_ctx != NULL) {
1160 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
1165 err = security_xfrm_state_alloc(x, uctx);
1172 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
1173 if (sa->sadb_sa_auth) {
1175 struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1181 keysize = (key->sadb_key_bits + 7) / 8;
1182 x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1185 strcpy(x->aalg->alg_name, a->name);
1186 x->aalg->alg_key_len = 0;
1188 x->aalg->alg_key_len = key->sadb_key_bits;
1189 memcpy(x->aalg->alg_key, key+1, keysize);
1191 x->props.aalgo = sa->sadb_sa_auth;
1192 /* x->algo.flags = sa->sadb_sa_flags; */
1194 if (sa->sadb_sa_encrypt) {
1195 if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1196 struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1201 x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1204 strcpy(x->calg->alg_name, a->name);
1205 x->props.calgo = sa->sadb_sa_encrypt;
1208 struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1213 key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1215 keysize = (key->sadb_key_bits + 7) / 8;
1216 x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1219 strcpy(x->ealg->alg_name, a->name);
1220 x->ealg->alg_key_len = 0;
1222 x->ealg->alg_key_len = key->sadb_key_bits;
1223 memcpy(x->ealg->alg_key, key+1, keysize);
1225 x->props.ealgo = sa->sadb_sa_encrypt;
1228 /* x->algo.flags = sa->sadb_sa_flags; */
1230 x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1232 if (!x->props.family) {
1233 err = -EAFNOSUPPORT;
1236 pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1239 if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1240 struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
1241 int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1246 x->props.mode = mode;
1247 x->props.reqid = sa2->sadb_x_sa2_reqid;
1250 if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1251 struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1253 /* Nobody uses this, but we try. */
1254 x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1255 x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1259 x->sel.family = x->props.family;
1261 if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1262 struct sadb_x_nat_t_type* n_type;
1263 struct xfrm_encap_tmpl *natt;
1265 x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1270 n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1271 natt->encap_type = n_type->sadb_x_nat_t_type_type;
1273 if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1274 struct sadb_x_nat_t_port* n_port =
1275 ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1276 natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1278 if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1279 struct sadb_x_nat_t_port* n_port =
1280 ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1281 natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1285 err = xfrm_init_state(x);
1289 x->km.seq = hdr->sadb_msg_seq;
1293 x->km.state = XFRM_STATE_DEAD;
1295 return ERR_PTR(err);
1298 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1303 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1305 struct sk_buff *resp_skb;
1306 struct sadb_x_sa2 *sa2;
1307 struct sadb_address *saddr, *daddr;
1308 struct sadb_msg *out_hdr;
1309 struct sadb_spirange *range;
1310 struct xfrm_state *x = NULL;
1313 u32 min_spi, max_spi;
1316 unsigned short family;
1317 xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1319 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1320 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1323 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1327 if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1328 mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1331 reqid = sa2->sadb_x_sa2_reqid;
1337 saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1338 daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1340 family = ((struct sockaddr *)(saddr + 1))->sa_family;
1343 xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1344 xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1346 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1348 xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1349 xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1354 if (hdr->sadb_msg_seq) {
1355 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1356 if (x && xfrm_addr_cmp(&x->id.daddr, xdaddr, family)) {
1363 x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
1369 max_spi = 0x0fffffff;
1371 range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1373 min_spi = range->sadb_spirange_min;
1374 max_spi = range->sadb_spirange_max;
1377 err = xfrm_alloc_spi(x, min_spi, max_spi);
1378 resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1380 if (IS_ERR(resp_skb)) {
1382 return PTR_ERR(resp_skb);
1385 out_hdr = (struct sadb_msg *) resp_skb->data;
1386 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1387 out_hdr->sadb_msg_type = SADB_GETSPI;
1388 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1389 out_hdr->sadb_msg_errno = 0;
1390 out_hdr->sadb_msg_reserved = 0;
1391 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1392 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1396 pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);
1401 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1403 struct xfrm_state *x;
1405 if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1408 if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1411 x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
1415 spin_lock_bh(&x->lock);
1416 if (x->km.state == XFRM_STATE_ACQ) {
1417 x->km.state = XFRM_STATE_ERROR;
1420 spin_unlock_bh(&x->lock);
1425 static inline int event2poltype(int event)
1428 case XFRM_MSG_DELPOLICY:
1429 return SADB_X_SPDDELETE;
1430 case XFRM_MSG_NEWPOLICY:
1431 return SADB_X_SPDADD;
1432 case XFRM_MSG_UPDPOLICY:
1433 return SADB_X_SPDUPDATE;
1434 case XFRM_MSG_POLEXPIRE:
1435 // return SADB_X_SPDEXPIRE;
1437 printk("pfkey: Unknown policy event %d\n", event);
1444 static inline int event2keytype(int event)
1447 case XFRM_MSG_DELSA:
1449 case XFRM_MSG_NEWSA:
1451 case XFRM_MSG_UPDSA:
1453 case XFRM_MSG_EXPIRE:
1456 printk("pfkey: Unknown SA event %d\n", event);
1464 static int key_notify_sa(struct xfrm_state *x, struct km_event *c)
1466 struct sk_buff *skb;
1467 struct sadb_msg *hdr;
1469 skb = pfkey_xfrm_state2msg(x);
1472 return PTR_ERR(skb);
1474 hdr = (struct sadb_msg *) skb->data;
1475 hdr->sadb_msg_version = PF_KEY_V2;
1476 hdr->sadb_msg_type = event2keytype(c->event);
1477 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1478 hdr->sadb_msg_errno = 0;
1479 hdr->sadb_msg_reserved = 0;
1480 hdr->sadb_msg_seq = c->seq;
1481 hdr->sadb_msg_pid = c->pid;
1483 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1488 static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1490 struct xfrm_state *x;
1494 x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
1499 if (hdr->sadb_msg_type == SADB_ADD)
1500 err = xfrm_state_add(x);
1502 err = xfrm_state_update(x);
1504 xfrm_audit_state_add(x, err ? 0 : 1,
1505 audit_get_loginuid(current),
1506 audit_get_sessionid(current), 0);
1509 x->km.state = XFRM_STATE_DEAD;
1510 __xfrm_state_put(x);
1514 if (hdr->sadb_msg_type == SADB_ADD)
1515 c.event = XFRM_MSG_NEWSA;
1517 c.event = XFRM_MSG_UPDSA;
1518 c.seq = hdr->sadb_msg_seq;
1519 c.pid = hdr->sadb_msg_pid;
1520 km_state_notify(x, &c);
1526 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1528 struct xfrm_state *x;
1532 if (!ext_hdrs[SADB_EXT_SA-1] ||
1533 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1534 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1537 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1541 if ((err = security_xfrm_state_delete(x)))
1544 if (xfrm_state_kern(x)) {
1549 err = xfrm_state_delete(x);
1554 c.seq = hdr->sadb_msg_seq;
1555 c.pid = hdr->sadb_msg_pid;
1556 c.event = XFRM_MSG_DELSA;
1557 km_state_notify(x, &c);
1559 xfrm_audit_state_delete(x, err ? 0 : 1,
1560 audit_get_loginuid(current),
1561 audit_get_sessionid(current), 0);
1567 static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1570 struct sk_buff *out_skb;
1571 struct sadb_msg *out_hdr;
1572 struct xfrm_state *x;
1574 if (!ext_hdrs[SADB_EXT_SA-1] ||
1575 !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1576 ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1579 x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
1583 out_skb = pfkey_xfrm_state2msg(x);
1584 proto = x->id.proto;
1586 if (IS_ERR(out_skb))
1587 return PTR_ERR(out_skb);
1589 out_hdr = (struct sadb_msg *) out_skb->data;
1590 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1591 out_hdr->sadb_msg_type = SADB_GET;
1592 out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1593 out_hdr->sadb_msg_errno = 0;
1594 out_hdr->sadb_msg_reserved = 0;
1595 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1596 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1597 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
1602 static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig,
1605 struct sk_buff *skb;
1606 struct sadb_msg *hdr;
1607 int len, auth_len, enc_len, i;
1609 auth_len = xfrm_count_auth_supported();
1611 auth_len *= sizeof(struct sadb_alg);
1612 auth_len += sizeof(struct sadb_supported);
1615 enc_len = xfrm_count_enc_supported();
1617 enc_len *= sizeof(struct sadb_alg);
1618 enc_len += sizeof(struct sadb_supported);
1621 len = enc_len + auth_len + sizeof(struct sadb_msg);
1623 skb = alloc_skb(len + 16, allocation);
1627 hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
1628 pfkey_hdr_dup(hdr, orig);
1629 hdr->sadb_msg_errno = 0;
1630 hdr->sadb_msg_len = len / sizeof(uint64_t);
1633 struct sadb_supported *sp;
1634 struct sadb_alg *ap;
1636 sp = (struct sadb_supported *) skb_put(skb, auth_len);
1637 ap = (struct sadb_alg *) (sp + 1);
1639 sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1640 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1642 for (i = 0; ; i++) {
1643 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1646 if (aalg->available)
1652 struct sadb_supported *sp;
1653 struct sadb_alg *ap;
1655 sp = (struct sadb_supported *) skb_put(skb, enc_len);
1656 ap = (struct sadb_alg *) (sp + 1);
1658 sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1659 sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1661 for (i = 0; ; i++) {
1662 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1665 if (ealg->available)
1674 static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1676 struct pfkey_sock *pfk = pfkey_sk(sk);
1677 struct sk_buff *supp_skb;
1679 if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1682 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1683 if (pfk->registered&(1<<hdr->sadb_msg_satype))
1685 pfk->registered |= (1<<hdr->sadb_msg_satype);
1690 supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1692 if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1693 pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1698 pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);
1703 static int key_notify_sa_flush(struct km_event *c)
1705 struct sk_buff *skb;
1706 struct sadb_msg *hdr;
1708 skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1711 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1712 hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1713 hdr->sadb_msg_type = SADB_FLUSH;
1714 hdr->sadb_msg_seq = c->seq;
1715 hdr->sadb_msg_pid = c->pid;
1716 hdr->sadb_msg_version = PF_KEY_V2;
1717 hdr->sadb_msg_errno = (uint8_t) 0;
1718 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1720 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
1725 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1729 struct xfrm_audit audit_info;
1732 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1736 audit_info.loginuid = audit_get_loginuid(current);
1737 audit_info.sessionid = audit_get_sessionid(current);
1738 audit_info.secid = 0;
1739 err = xfrm_state_flush(proto, &audit_info);
1742 c.data.proto = proto;
1743 c.seq = hdr->sadb_msg_seq;
1744 c.pid = hdr->sadb_msg_pid;
1745 c.event = XFRM_MSG_FLUSHSA;
1746 km_state_notify(NULL, &c);
1751 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1753 struct pfkey_sock *pfk = ptr;
1754 struct sk_buff *out_skb;
1755 struct sadb_msg *out_hdr;
1757 if (!pfkey_can_dump(&pfk->sk))
1760 out_skb = pfkey_xfrm_state2msg(x);
1761 if (IS_ERR(out_skb))
1762 return PTR_ERR(out_skb);
1764 out_hdr = (struct sadb_msg *) out_skb->data;
1765 out_hdr->sadb_msg_version = pfk->dump.msg_version;
1766 out_hdr->sadb_msg_type = SADB_DUMP;
1767 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1768 out_hdr->sadb_msg_errno = 0;
1769 out_hdr->sadb_msg_reserved = 0;
1770 out_hdr->sadb_msg_seq = count;
1771 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
1772 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
1776 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1778 return xfrm_state_walk(&pfk->dump.u.state, dump_sa, (void *) pfk);
1781 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1783 xfrm_state_walk_done(&pfk->dump.u.state);
1786 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1789 struct pfkey_sock *pfk = pfkey_sk(sk);
1791 if (pfk->dump.dump != NULL)
1794 proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1798 pfk->dump.msg_version = hdr->sadb_msg_version;
1799 pfk->dump.msg_pid = hdr->sadb_msg_pid;
1800 pfk->dump.dump = pfkey_dump_sa;
1801 pfk->dump.done = pfkey_dump_sa_done;
1802 xfrm_state_walk_init(&pfk->dump.u.state, proto);
1804 return pfkey_do_dump(pfk);
1807 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
1809 struct pfkey_sock *pfk = pfkey_sk(sk);
1810 int satype = hdr->sadb_msg_satype;
1812 if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1813 /* XXX we mangle packet... */
1814 hdr->sadb_msg_errno = 0;
1815 if (satype != 0 && satype != 1)
1817 pfk->promisc = satype;
1819 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
1823 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1826 u32 reqid = *(u32*)ptr;
1828 for (i=0; i<xp->xfrm_nr; i++) {
1829 if (xp->xfrm_vec[i].reqid == reqid)
1835 static u32 gen_reqid(void)
1837 struct xfrm_policy_walk walk;
1840 static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1846 reqid = IPSEC_MANUAL_REQID_MAX+1;
1847 xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1848 rc = xfrm_policy_walk(&walk, check_reqid, (void*)&reqid);
1849 xfrm_policy_walk_done(&walk);
1852 } while (reqid != start);
1857 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1859 struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1860 struct sockaddr_in *sin;
1861 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1862 struct sockaddr_in6 *sin6;
1866 if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1869 if (rq->sadb_x_ipsecrequest_mode == 0)
1872 t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1873 if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1876 if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1878 else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1879 t->reqid = rq->sadb_x_ipsecrequest_reqid;
1880 if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1882 if (!t->reqid && !(t->reqid = gen_reqid()))
1886 /* addresses present only in tunnel mode */
1887 if (t->mode == XFRM_MODE_TUNNEL) {
1888 struct sockaddr *sa;
1889 sa = (struct sockaddr *)(rq+1);
1890 switch(sa->sa_family) {
1892 sin = (struct sockaddr_in*)sa;
1893 t->saddr.a4 = sin->sin_addr.s_addr;
1895 if (sin->sin_family != AF_INET)
1897 t->id.daddr.a4 = sin->sin_addr.s_addr;
1899 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1901 sin6 = (struct sockaddr_in6*)sa;
1902 memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1904 if (sin6->sin6_family != AF_INET6)
1906 memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
1912 t->encap_family = sa->sa_family;
1914 t->encap_family = xp->family;
1916 /* No way to set this via kame pfkey */
1923 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1926 int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1927 struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1929 while (len >= sizeof(struct sadb_x_ipsecrequest)) {
1930 if ((err = parse_ipsecrequest(xp, rq)) < 0)
1932 len -= rq->sadb_x_ipsecrequest_len;
1933 rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1938 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy *xp)
1940 struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1943 int len = sizeof(struct sadb_x_sec_ctx);
1944 len += xfrm_ctx->ctx_len;
1945 return PFKEY_ALIGN8(len);
1950 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
1952 struct xfrm_tmpl *t;
1953 int sockaddr_size = pfkey_sockaddr_size(xp->family);
1957 for (i=0; i<xp->xfrm_nr; i++) {
1958 t = xp->xfrm_vec + i;
1959 socklen += pfkey_sockaddr_len(t->encap_family);
1962 return sizeof(struct sadb_msg) +
1963 (sizeof(struct sadb_lifetime) * 3) +
1964 (sizeof(struct sadb_address) * 2) +
1965 (sockaddr_size * 2) +
1966 sizeof(struct sadb_x_policy) +
1967 (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
1969 pfkey_xfrm_policy2sec_ctx_size(xp);
1972 static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
1974 struct sk_buff *skb;
1977 size = pfkey_xfrm_policy2msg_size(xp);
1979 skb = alloc_skb(size + 16, GFP_ATOMIC);
1981 return ERR_PTR(-ENOBUFS);
1986 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
1988 struct sadb_msg *hdr;
1989 struct sadb_address *addr;
1990 struct sadb_lifetime *lifetime;
1991 struct sadb_x_policy *pol;
1992 struct sockaddr_in *sin;
1993 struct sadb_x_sec_ctx *sec_ctx;
1994 struct xfrm_sec_ctx *xfrm_ctx;
1995 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1996 struct sockaddr_in6 *sin6;
2000 int sockaddr_size = pfkey_sockaddr_size(xp->family);
2001 int socklen = pfkey_sockaddr_len(xp->family);
2003 size = pfkey_xfrm_policy2msg_size(xp);
2005 /* call should fill header later */
2006 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
2007 memset(hdr, 0, size); /* XXX do we need this ? */
2010 addr = (struct sadb_address*) skb_put(skb,
2011 sizeof(struct sadb_address)+sockaddr_size);
2012 addr->sadb_address_len =
2013 (sizeof(struct sadb_address)+sockaddr_size)/
2015 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2016 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2017 addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2018 addr->sadb_address_reserved = 0;
2020 if (xp->family == AF_INET) {
2021 sin = (struct sockaddr_in *) (addr + 1);
2022 sin->sin_family = AF_INET;
2023 sin->sin_addr.s_addr = xp->selector.saddr.a4;
2024 sin->sin_port = xp->selector.sport;
2025 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2027 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2028 else if (xp->family == AF_INET6) {
2029 sin6 = (struct sockaddr_in6 *) (addr + 1);
2030 sin6->sin6_family = AF_INET6;
2031 sin6->sin6_port = xp->selector.sport;
2032 sin6->sin6_flowinfo = 0;
2033 memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
2034 sizeof(struct in6_addr));
2035 sin6->sin6_scope_id = 0;
2042 addr = (struct sadb_address*) skb_put(skb,
2043 sizeof(struct sadb_address)+sockaddr_size);
2044 addr->sadb_address_len =
2045 (sizeof(struct sadb_address)+sockaddr_size)/
2047 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2048 addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2049 addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2050 addr->sadb_address_reserved = 0;
2051 if (xp->family == AF_INET) {
2052 sin = (struct sockaddr_in *) (addr + 1);
2053 sin->sin_family = AF_INET;
2054 sin->sin_addr.s_addr = xp->selector.daddr.a4;
2055 sin->sin_port = xp->selector.dport;
2056 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2058 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2059 else if (xp->family == AF_INET6) {
2060 sin6 = (struct sockaddr_in6 *) (addr + 1);
2061 sin6->sin6_family = AF_INET6;
2062 sin6->sin6_port = xp->selector.dport;
2063 sin6->sin6_flowinfo = 0;
2064 memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
2065 sizeof(struct in6_addr));
2066 sin6->sin6_scope_id = 0;
2073 lifetime = (struct sadb_lifetime *) skb_put(skb,
2074 sizeof(struct sadb_lifetime));
2075 lifetime->sadb_lifetime_len =
2076 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2077 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2078 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
2079 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2080 lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2081 lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2083 lifetime = (struct sadb_lifetime *) skb_put(skb,
2084 sizeof(struct sadb_lifetime));
2085 lifetime->sadb_lifetime_len =
2086 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2087 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2088 lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
2089 lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2090 lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2091 lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2093 lifetime = (struct sadb_lifetime *) skb_put(skb,
2094 sizeof(struct sadb_lifetime));
2095 lifetime->sadb_lifetime_len =
2096 sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2097 lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2098 lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2099 lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2100 lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2101 lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2103 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
2104 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2105 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2106 pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2107 if (xp->action == XFRM_POLICY_ALLOW) {
2109 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2111 pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2113 pol->sadb_x_policy_dir = dir+1;
2114 pol->sadb_x_policy_id = xp->index;
2115 pol->sadb_x_policy_priority = xp->priority;
2117 for (i=0; i<xp->xfrm_nr; i++) {
2118 struct sadb_x_ipsecrequest *rq;
2119 struct xfrm_tmpl *t = xp->xfrm_vec + i;
2123 req_size = sizeof(struct sadb_x_ipsecrequest);
2124 if (t->mode == XFRM_MODE_TUNNEL)
2125 req_size += pfkey_sockaddr_len(t->encap_family) * 2;
2128 rq = (void*)skb_put(skb, req_size);
2129 pol->sadb_x_policy_len += req_size/8;
2130 memset(rq, 0, sizeof(*rq));
2131 rq->sadb_x_ipsecrequest_len = req_size;
2132 rq->sadb_x_ipsecrequest_proto = t->id.proto;
2133 if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2135 rq->sadb_x_ipsecrequest_mode = mode;
2136 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2138 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2140 rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2141 rq->sadb_x_ipsecrequest_reqid = t->reqid;
2142 if (t->mode == XFRM_MODE_TUNNEL) {
2143 switch (t->encap_family) {
2145 sin = (void*)(rq+1);
2146 sin->sin_family = AF_INET;
2147 sin->sin_addr.s_addr = t->saddr.a4;
2149 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2151 sin->sin_family = AF_INET;
2152 sin->sin_addr.s_addr = t->id.daddr.a4;
2154 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
2156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2158 sin6 = (void*)(rq+1);
2159 sin6->sin6_family = AF_INET6;
2160 sin6->sin6_port = 0;
2161 sin6->sin6_flowinfo = 0;
2162 memcpy(&sin6->sin6_addr, t->saddr.a6,
2163 sizeof(struct in6_addr));
2164 sin6->sin6_scope_id = 0;
2167 sin6->sin6_family = AF_INET6;
2168 sin6->sin6_port = 0;
2169 sin6->sin6_flowinfo = 0;
2170 memcpy(&sin6->sin6_addr, t->id.daddr.a6,
2171 sizeof(struct in6_addr));
2172 sin6->sin6_scope_id = 0;
2181 /* security context */
2182 if ((xfrm_ctx = xp->security)) {
2183 int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2185 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
2186 sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2187 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2188 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2189 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2190 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2191 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2195 hdr->sadb_msg_len = size / sizeof(uint64_t);
2196 hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2201 static int key_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2203 struct sk_buff *out_skb;
2204 struct sadb_msg *out_hdr;
2207 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2208 if (IS_ERR(out_skb)) {
2209 err = PTR_ERR(out_skb);
2212 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2216 out_hdr = (struct sadb_msg *) out_skb->data;
2217 out_hdr->sadb_msg_version = PF_KEY_V2;
2219 if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2220 out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2222 out_hdr->sadb_msg_type = event2poltype(c->event);
2223 out_hdr->sadb_msg_errno = 0;
2224 out_hdr->sadb_msg_seq = c->seq;
2225 out_hdr->sadb_msg_pid = c->pid;
2226 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
2232 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2235 struct sadb_lifetime *lifetime;
2236 struct sadb_address *sa;
2237 struct sadb_x_policy *pol;
2238 struct xfrm_policy *xp;
2240 struct sadb_x_sec_ctx *sec_ctx;
2242 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2243 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2244 !ext_hdrs[SADB_X_EXT_POLICY-1])
2247 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2248 if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2250 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2253 xp = xfrm_policy_alloc(GFP_KERNEL);
2257 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2258 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2259 xp->priority = pol->sadb_x_policy_priority;
2261 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2262 xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2267 xp->selector.family = xp->family;
2268 xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2269 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2270 xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2271 if (xp->selector.sport)
2272 xp->selector.sport_mask = htons(0xffff);
2274 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2275 pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2276 xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2278 /* Amusing, we set this twice. KAME apps appear to set same value
2279 * in both addresses.
2281 xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2283 xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2284 if (xp->selector.dport)
2285 xp->selector.dport_mask = htons(0xffff);
2287 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2288 if (sec_ctx != NULL) {
2289 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2296 err = security_xfrm_policy_alloc(&xp->security, uctx);
2303 xp->lft.soft_byte_limit = XFRM_INF;
2304 xp->lft.hard_byte_limit = XFRM_INF;
2305 xp->lft.soft_packet_limit = XFRM_INF;
2306 xp->lft.hard_packet_limit = XFRM_INF;
2307 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2308 xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2309 xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2310 xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2311 xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2313 if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2314 xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2315 xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2316 xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2317 xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2320 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2321 (err = parse_ipsecrequests(xp, pol)) < 0)
2324 err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2325 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2327 xfrm_audit_policy_add(xp, err ? 0 : 1,
2328 audit_get_loginuid(current),
2329 audit_get_sessionid(current), 0);
2334 if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2335 c.event = XFRM_MSG_UPDPOLICY;
2337 c.event = XFRM_MSG_NEWPOLICY;
2339 c.seq = hdr->sadb_msg_seq;
2340 c.pid = hdr->sadb_msg_pid;
2342 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2348 xfrm_policy_destroy(xp);
2352 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2355 struct sadb_address *sa;
2356 struct sadb_x_policy *pol;
2357 struct xfrm_policy *xp;
2358 struct xfrm_selector sel;
2360 struct sadb_x_sec_ctx *sec_ctx;
2361 struct xfrm_sec_ctx *pol_ctx = NULL;
2363 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2364 ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2365 !ext_hdrs[SADB_X_EXT_POLICY-1])
2368 pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2369 if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2372 memset(&sel, 0, sizeof(sel));
2374 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2375 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2376 sel.prefixlen_s = sa->sadb_address_prefixlen;
2377 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2378 sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2380 sel.sport_mask = htons(0xffff);
2382 sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1],
2383 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2384 sel.prefixlen_d = sa->sadb_address_prefixlen;
2385 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2386 sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2388 sel.dport_mask = htons(0xffff);
2390 sec_ctx = (struct sadb_x_sec_ctx *) ext_hdrs[SADB_X_EXT_SEC_CTX-1];
2391 if (sec_ctx != NULL) {
2392 struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
2397 err = security_xfrm_policy_alloc(&pol_ctx, uctx);
2403 xp = xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN,
2404 pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2406 security_xfrm_policy_free(pol_ctx);
2410 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2411 audit_get_loginuid(current),
2412 audit_get_sessionid(current), 0);
2417 c.seq = hdr->sadb_msg_seq;
2418 c.pid = hdr->sadb_msg_pid;
2419 c.event = XFRM_MSG_DELPOLICY;
2420 km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2427 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, struct sadb_msg *hdr, int dir)
2430 struct sk_buff *out_skb;
2431 struct sadb_msg *out_hdr;
2434 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2435 if (IS_ERR(out_skb)) {
2436 err = PTR_ERR(out_skb);
2439 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2443 out_hdr = (struct sadb_msg *) out_skb->data;
2444 out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2445 out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2446 out_hdr->sadb_msg_satype = 0;
2447 out_hdr->sadb_msg_errno = 0;
2448 out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2449 out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2450 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);
2457 #ifdef CONFIG_NET_KEY_MIGRATE
2458 static int pfkey_sockaddr_pair_size(sa_family_t family)
2460 return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2463 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest *rq,
2464 xfrm_address_t *saddr, xfrm_address_t *daddr,
2467 struct sockaddr *sa = (struct sockaddr *)(rq + 1);
2468 if (rq->sadb_x_ipsecrequest_len <
2469 pfkey_sockaddr_pair_size(sa->sa_family))
2472 switch (sa->sa_family) {
2475 struct sockaddr_in *sin;
2476 sin = (struct sockaddr_in *)sa;
2477 if ((sin+1)->sin_family != AF_INET)
2479 memcpy(&saddr->a4, &sin->sin_addr, sizeof(saddr->a4));
2481 memcpy(&daddr->a4, &sin->sin_addr, sizeof(daddr->a4));
2485 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2488 struct sockaddr_in6 *sin6;
2489 sin6 = (struct sockaddr_in6 *)sa;
2490 if ((sin6+1)->sin6_family != AF_INET6)
2492 memcpy(&saddr->a6, &sin6->sin6_addr,
2495 memcpy(&daddr->a6, &sin6->sin6_addr,
2508 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2509 struct xfrm_migrate *m)
2512 struct sadb_x_ipsecrequest *rq2;
2515 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2516 len < rq1->sadb_x_ipsecrequest_len)
2520 err = parse_sockaddr_pair(rq1, &m->old_saddr, &m->old_daddr,
2525 rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2526 len -= rq1->sadb_x_ipsecrequest_len;
2528 if (len <= sizeof(struct sadb_x_ipsecrequest) ||
2529 len < rq2->sadb_x_ipsecrequest_len)
2533 err = parse_sockaddr_pair(rq2, &m->new_saddr, &m->new_daddr,
2538 if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2539 rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2540 rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2543 m->proto = rq1->sadb_x_ipsecrequest_proto;
2544 if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2547 m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2549 return ((int)(rq1->sadb_x_ipsecrequest_len +
2550 rq2->sadb_x_ipsecrequest_len));
2553 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2554 struct sadb_msg *hdr, void **ext_hdrs)
2556 int i, len, ret, err = -EINVAL;
2558 struct sadb_address *sa;
2559 struct sadb_x_policy *pol;
2560 struct sadb_x_ipsecrequest *rq;
2561 struct xfrm_selector sel;
2562 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2564 if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2565 ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2566 !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2571 pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2577 if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2582 dir = pol->sadb_x_policy_dir - 1;
2583 memset(&sel, 0, sizeof(sel));
2585 /* set source address info of selector */
2586 sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2587 sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2588 sel.prefixlen_s = sa->sadb_address_prefixlen;
2589 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2590 sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2592 sel.sport_mask = htons(0xffff);
2594 /* set destination address info of selector */
2595 sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
2596 pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2597 sel.prefixlen_d = sa->sadb_address_prefixlen;
2598 sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2599 sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2601 sel.dport_mask = htons(0xffff);
2603 rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2605 /* extract ipsecrequests */
2607 len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2609 while (len > 0 && i < XFRM_MAX_DEPTH) {
2610 ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2615 rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2621 if (!i || len > 0) {
2626 return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i);
2632 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2633 struct sadb_msg *hdr, void **ext_hdrs)
2635 return -ENOPROTOOPT;
2640 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2643 int err = 0, delete;
2644 struct sadb_x_policy *pol;
2645 struct xfrm_policy *xp;
2648 if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2651 dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2652 if (dir >= XFRM_POLICY_MAX)
2655 delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2656 xp = xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN, dir, pol->sadb_x_policy_id,
2662 xfrm_audit_policy_delete(xp, err ? 0 : 1,
2663 audit_get_loginuid(current),
2664 audit_get_sessionid(current), 0);
2668 c.seq = hdr->sadb_msg_seq;
2669 c.pid = hdr->sadb_msg_pid;
2671 c.event = XFRM_MSG_DELPOLICY;
2672 km_policy_notify(xp, dir, &c);
2674 err = key_pol_get_resp(sk, xp, hdr, dir);
2682 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2684 struct pfkey_sock *pfk = ptr;
2685 struct sk_buff *out_skb;
2686 struct sadb_msg *out_hdr;
2689 if (!pfkey_can_dump(&pfk->sk))
2692 out_skb = pfkey_xfrm_policy2msg_prep(xp);
2693 if (IS_ERR(out_skb))
2694 return PTR_ERR(out_skb);
2696 err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2700 out_hdr = (struct sadb_msg *) out_skb->data;
2701 out_hdr->sadb_msg_version = pfk->dump.msg_version;
2702 out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2703 out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2704 out_hdr->sadb_msg_errno = 0;
2705 out_hdr->sadb_msg_seq = count;
2706 out_hdr->sadb_msg_pid = pfk->dump.msg_pid;
2707 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, &pfk->sk);
2711 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2713 return xfrm_policy_walk(&pfk->dump.u.policy, dump_sp, (void *) pfk);
2716 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2718 xfrm_policy_walk_done(&pfk->dump.u.policy);
2721 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2723 struct pfkey_sock *pfk = pfkey_sk(sk);
2725 if (pfk->dump.dump != NULL)
2728 pfk->dump.msg_version = hdr->sadb_msg_version;
2729 pfk->dump.msg_pid = hdr->sadb_msg_pid;
2730 pfk->dump.dump = pfkey_dump_sp;
2731 pfk->dump.done = pfkey_dump_sp_done;
2732 xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2734 return pfkey_do_dump(pfk);
2737 static int key_notify_policy_flush(struct km_event *c)
2739 struct sk_buff *skb_out;
2740 struct sadb_msg *hdr;
2742 skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2745 hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
2746 hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2747 hdr->sadb_msg_seq = c->seq;
2748 hdr->sadb_msg_pid = c->pid;
2749 hdr->sadb_msg_version = PF_KEY_V2;
2750 hdr->sadb_msg_errno = (uint8_t) 0;
2751 hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2752 pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL);
2757 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
2760 struct xfrm_audit audit_info;
2763 audit_info.loginuid = audit_get_loginuid(current);
2764 audit_info.sessionid = audit_get_sessionid(current);
2765 audit_info.secid = 0;
2766 err = xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN, &audit_info);
2769 c.data.type = XFRM_POLICY_TYPE_MAIN;
2770 c.event = XFRM_MSG_FLUSHPOLICY;
2771 c.pid = hdr->sadb_msg_pid;
2772 c.seq = hdr->sadb_msg_seq;
2773 km_policy_notify(NULL, 0, &c);
2778 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2779 struct sadb_msg *hdr, void **ext_hdrs);
2780 static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2781 [SADB_RESERVED] = pfkey_reserved,
2782 [SADB_GETSPI] = pfkey_getspi,
2783 [SADB_UPDATE] = pfkey_add,
2784 [SADB_ADD] = pfkey_add,
2785 [SADB_DELETE] = pfkey_delete,
2786 [SADB_GET] = pfkey_get,
2787 [SADB_ACQUIRE] = pfkey_acquire,
2788 [SADB_REGISTER] = pfkey_register,
2789 [SADB_EXPIRE] = NULL,
2790 [SADB_FLUSH] = pfkey_flush,
2791 [SADB_DUMP] = pfkey_dump,
2792 [SADB_X_PROMISC] = pfkey_promisc,
2793 [SADB_X_PCHANGE] = NULL,
2794 [SADB_X_SPDUPDATE] = pfkey_spdadd,
2795 [SADB_X_SPDADD] = pfkey_spdadd,
2796 [SADB_X_SPDDELETE] = pfkey_spddelete,
2797 [SADB_X_SPDGET] = pfkey_spdget,
2798 [SADB_X_SPDACQUIRE] = NULL,
2799 [SADB_X_SPDDUMP] = pfkey_spddump,
2800 [SADB_X_SPDFLUSH] = pfkey_spdflush,
2801 [SADB_X_SPDSETIDX] = pfkey_spdadd,
2802 [SADB_X_SPDDELETE2] = pfkey_spdget,
2803 [SADB_X_MIGRATE] = pfkey_migrate,
2806 static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
2808 void *ext_hdrs[SADB_EXT_MAX];
2811 pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2812 BROADCAST_PROMISC_ONLY, NULL);
2814 memset(ext_hdrs, 0, sizeof(ext_hdrs));
2815 err = parse_exthdrs(skb, hdr, ext_hdrs);
2818 if (pfkey_funcs[hdr->sadb_msg_type])
2819 err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2824 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2826 struct sadb_msg *hdr = NULL;
2828 if (skb->len < sizeof(*hdr)) {
2831 hdr = (struct sadb_msg *) skb->data;
2832 if (hdr->sadb_msg_version != PF_KEY_V2 ||
2833 hdr->sadb_msg_reserved != 0 ||
2834 (hdr->sadb_msg_type <= SADB_RESERVED ||
2835 hdr->sadb_msg_type > SADB_MAX)) {
2838 } else if (hdr->sadb_msg_len != (skb->len /
2839 sizeof(uint64_t)) ||
2840 hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2841 sizeof(uint64_t))) {
2851 static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2853 unsigned int id = d->desc.sadb_alg_id;
2855 if (id >= sizeof(t->aalgos) * 8)
2858 return (t->aalgos >> id) & 1;
2861 static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
2863 unsigned int id = d->desc.sadb_alg_id;
2865 if (id >= sizeof(t->ealgos) * 8)
2868 return (t->ealgos >> id) & 1;
2871 static int count_ah_combs(struct xfrm_tmpl *t)
2875 for (i = 0; ; i++) {
2876 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2879 if (aalg_tmpl_set(t, aalg) && aalg->available)
2880 sz += sizeof(struct sadb_comb);
2882 return sz + sizeof(struct sadb_prop);
2885 static int count_esp_combs(struct xfrm_tmpl *t)
2889 for (i = 0; ; i++) {
2890 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2894 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2897 for (k = 1; ; k++) {
2898 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2902 if (aalg_tmpl_set(t, aalg) && aalg->available)
2903 sz += sizeof(struct sadb_comb);
2906 return sz + sizeof(struct sadb_prop);
2909 static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2911 struct sadb_prop *p;
2914 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2915 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2916 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2917 p->sadb_prop_replay = 32;
2918 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2920 for (i = 0; ; i++) {
2921 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2925 if (aalg_tmpl_set(t, aalg) && aalg->available) {
2926 struct sadb_comb *c;
2927 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2928 memset(c, 0, sizeof(*c));
2929 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2930 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2931 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2932 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2933 c->sadb_comb_hard_addtime = 24*60*60;
2934 c->sadb_comb_soft_addtime = 20*60*60;
2935 c->sadb_comb_hard_usetime = 8*60*60;
2936 c->sadb_comb_soft_usetime = 7*60*60;
2941 static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
2943 struct sadb_prop *p;
2946 p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
2947 p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2948 p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2949 p->sadb_prop_replay = 32;
2950 memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2953 struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2957 if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2960 for (k = 1; ; k++) {
2961 struct sadb_comb *c;
2962 struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2965 if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2967 c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
2968 memset(c, 0, sizeof(*c));
2969 p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2970 c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2971 c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2972 c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2973 c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2974 c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2975 c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2976 c->sadb_comb_hard_addtime = 24*60*60;
2977 c->sadb_comb_soft_addtime = 20*60*60;
2978 c->sadb_comb_hard_usetime = 8*60*60;
2979 c->sadb_comb_soft_usetime = 7*60*60;
2984 static int key_notify_policy_expire(struct xfrm_policy *xp, struct km_event *c)
2989 static int key_notify_sa_expire(struct xfrm_state *x, struct km_event *c)
2991 struct sk_buff *out_skb;
2992 struct sadb_msg *out_hdr;
2996 hard = c->data.hard;
3002 out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
3003 if (IS_ERR(out_skb))
3004 return PTR_ERR(out_skb);
3006 out_hdr = (struct sadb_msg *) out_skb->data;
3007 out_hdr->sadb_msg_version = PF_KEY_V2;
3008 out_hdr->sadb_msg_type = SADB_EXPIRE;
3009 out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3010 out_hdr->sadb_msg_errno = 0;
3011 out_hdr->sadb_msg_reserved = 0;
3012 out_hdr->sadb_msg_seq = 0;
3013 out_hdr->sadb_msg_pid = 0;
3015 pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3019 static int pfkey_send_notify(struct xfrm_state *x, struct km_event *c)
3022 case XFRM_MSG_EXPIRE:
3023 return key_notify_sa_expire(x, c);
3024 case XFRM_MSG_DELSA:
3025 case XFRM_MSG_NEWSA:
3026 case XFRM_MSG_UPDSA:
3027 return key_notify_sa(x, c);
3028 case XFRM_MSG_FLUSHSA:
3029 return key_notify_sa_flush(c);
3030 case XFRM_MSG_NEWAE: /* not yet supported */
3033 printk("pfkey: Unknown SA event %d\n", c->event);
3040 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
3042 if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3046 case XFRM_MSG_POLEXPIRE:
3047 return key_notify_policy_expire(xp, c);
3048 case XFRM_MSG_DELPOLICY:
3049 case XFRM_MSG_NEWPOLICY:
3050 case XFRM_MSG_UPDPOLICY:
3051 return key_notify_policy(xp, dir, c);
3052 case XFRM_MSG_FLUSHPOLICY:
3053 if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3055 return key_notify_policy_flush(c);
3057 printk("pfkey: Unknown policy event %d\n", c->event);
3064 static u32 get_acqseq(void)
3068 static DEFINE_SPINLOCK(acqseq_lock);
3070 spin_lock_bh(&acqseq_lock);
3071 res = (++acqseq ? : ++acqseq);
3072 spin_unlock_bh(&acqseq_lock);
3076 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
3078 struct sk_buff *skb;
3079 struct sadb_msg *hdr;
3080 struct sadb_address *addr;
3081 struct sadb_x_policy *pol;
3082 struct sockaddr_in *sin;
3083 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3084 struct sockaddr_in6 *sin6;
3088 struct sadb_x_sec_ctx *sec_ctx;
3089 struct xfrm_sec_ctx *xfrm_ctx;
3092 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3096 size = sizeof(struct sadb_msg) +
3097 (sizeof(struct sadb_address) * 2) +
3098 (sockaddr_size * 2) +
3099 sizeof(struct sadb_x_policy);
3101 if (x->id.proto == IPPROTO_AH)
3102 size += count_ah_combs(t);
3103 else if (x->id.proto == IPPROTO_ESP)
3104 size += count_esp_combs(t);
3106 if ((xfrm_ctx = x->security)) {
3107 ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3108 size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
3111 skb = alloc_skb(size + 16, GFP_ATOMIC);
3115 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3116 hdr->sadb_msg_version = PF_KEY_V2;
3117 hdr->sadb_msg_type = SADB_ACQUIRE;
3118 hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3119 hdr->sadb_msg_len = size / sizeof(uint64_t);
3120 hdr->sadb_msg_errno = 0;
3121 hdr->sadb_msg_reserved = 0;
3122 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3123 hdr->sadb_msg_pid = 0;
3126 addr = (struct sadb_address*) skb_put(skb,
3127 sizeof(struct sadb_address)+sockaddr_size);
3128 addr->sadb_address_len =
3129 (sizeof(struct sadb_address)+sockaddr_size)/
3131 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3132 addr->sadb_address_proto = 0;
3133 addr->sadb_address_reserved = 0;
3134 if (x->props.family == AF_INET) {
3135 addr->sadb_address_prefixlen = 32;
3137 sin = (struct sockaddr_in *) (addr + 1);
3138 sin->sin_family = AF_INET;
3139 sin->sin_addr.s_addr = x->props.saddr.a4;
3141 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3143 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3144 else if (x->props.family == AF_INET6) {
3145 addr->sadb_address_prefixlen = 128;
3147 sin6 = (struct sockaddr_in6 *) (addr + 1);
3148 sin6->sin6_family = AF_INET6;
3149 sin6->sin6_port = 0;
3150 sin6->sin6_flowinfo = 0;
3151 memcpy(&sin6->sin6_addr,
3152 x->props.saddr.a6, sizeof(struct in6_addr));
3153 sin6->sin6_scope_id = 0;
3160 addr = (struct sadb_address*) skb_put(skb,
3161 sizeof(struct sadb_address)+sockaddr_size);
3162 addr->sadb_address_len =
3163 (sizeof(struct sadb_address)+sockaddr_size)/
3165 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3166 addr->sadb_address_proto = 0;
3167 addr->sadb_address_reserved = 0;
3168 if (x->props.family == AF_INET) {
3169 addr->sadb_address_prefixlen = 32;
3171 sin = (struct sockaddr_in *) (addr + 1);
3172 sin->sin_family = AF_INET;
3173 sin->sin_addr.s_addr = x->id.daddr.a4;
3175 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3177 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3178 else if (x->props.family == AF_INET6) {
3179 addr->sadb_address_prefixlen = 128;
3181 sin6 = (struct sockaddr_in6 *) (addr + 1);
3182 sin6->sin6_family = AF_INET6;
3183 sin6->sin6_port = 0;
3184 sin6->sin6_flowinfo = 0;
3185 memcpy(&sin6->sin6_addr,
3186 x->id.daddr.a6, sizeof(struct in6_addr));
3187 sin6->sin6_scope_id = 0;
3193 pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
3194 pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3195 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3196 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3197 pol->sadb_x_policy_dir = dir+1;
3198 pol->sadb_x_policy_id = xp->index;
3200 /* Set sadb_comb's. */
3201 if (x->id.proto == IPPROTO_AH)
3202 dump_ah_combs(skb, t);
3203 else if (x->id.proto == IPPROTO_ESP)
3204 dump_esp_combs(skb, t);
3206 /* security context */
3208 sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
3209 sizeof(struct sadb_x_sec_ctx) + ctx_size);
3210 sec_ctx->sadb_x_sec_len =
3211 (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3212 sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3213 sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3214 sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3215 sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3216 memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3220 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3223 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3224 u8 *data, int len, int *dir)
3226 struct xfrm_policy *xp;
3227 struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3228 struct sadb_x_sec_ctx *sec_ctx;
3230 switch (sk->sk_family) {
3232 if (opt != IP_IPSEC_POLICY) {
3237 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3239 if (opt != IPV6_IPSEC_POLICY) {
3252 if (len < sizeof(struct sadb_x_policy) ||
3253 pol->sadb_x_policy_len*8 > len ||
3254 pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3255 (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3258 xp = xfrm_policy_alloc(GFP_ATOMIC);
3264 xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3265 XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3267 xp->lft.soft_byte_limit = XFRM_INF;
3268 xp->lft.hard_byte_limit = XFRM_INF;
3269 xp->lft.soft_packet_limit = XFRM_INF;
3270 xp->lft.hard_packet_limit = XFRM_INF;
3271 xp->family = sk->sk_family;
3274 if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3275 (*dir = parse_ipsecrequests(xp, pol)) < 0)
3278 /* security context too */
3279 if (len >= (pol->sadb_x_policy_len*8 +
3280 sizeof(struct sadb_x_sec_ctx))) {
3281 char *p = (char *)pol;
3282 struct xfrm_user_sec_ctx *uctx;
3284 p += pol->sadb_x_policy_len*8;
3285 sec_ctx = (struct sadb_x_sec_ctx *)p;
3286 if (len < pol->sadb_x_policy_len*8 +
3287 sec_ctx->sadb_x_sec_len) {
3291 if ((*dir = verify_sec_ctx_len(p)))
3293 uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3294 *dir = security_xfrm_policy_alloc(&xp->security, uctx);
3301 *dir = pol->sadb_x_policy_dir-1;
3305 xfrm_policy_destroy(xp);
3309 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3311 struct sk_buff *skb;
3312 struct sadb_msg *hdr;
3314 struct sadb_address *addr;
3315 struct sadb_x_nat_t_port *n_port;
3316 struct sockaddr_in *sin;
3317 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3318 struct sockaddr_in6 *sin6;
3322 __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3323 struct xfrm_encap_tmpl *natt = NULL;
3325 sockaddr_size = pfkey_sockaddr_size(x->props.family);
3337 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3339 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3340 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3343 size = sizeof(struct sadb_msg) +
3344 sizeof(struct sadb_sa) +
3345 (sizeof(struct sadb_address) * 2) +
3346 (sockaddr_size * 2) +
3347 (sizeof(struct sadb_x_nat_t_port) * 2);
3349 skb = alloc_skb(size + 16, GFP_ATOMIC);
3353 hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
3354 hdr->sadb_msg_version = PF_KEY_V2;
3355 hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3356 hdr->sadb_msg_satype = satype;
3357 hdr->sadb_msg_len = size / sizeof(uint64_t);
3358 hdr->sadb_msg_errno = 0;
3359 hdr->sadb_msg_reserved = 0;
3360 hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3361 hdr->sadb_msg_pid = 0;
3364 sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
3365 sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3366 sa->sadb_sa_exttype = SADB_EXT_SA;
3367 sa->sadb_sa_spi = x->id.spi;
3368 sa->sadb_sa_replay = 0;
3369 sa->sadb_sa_state = 0;
3370 sa->sadb_sa_auth = 0;
3371 sa->sadb_sa_encrypt = 0;
3372 sa->sadb_sa_flags = 0;
3374 /* ADDRESS_SRC (old addr) */
3375 addr = (struct sadb_address*)
3376 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3377 addr->sadb_address_len =
3378 (sizeof(struct sadb_address)+sockaddr_size)/
3380 addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3381 addr->sadb_address_proto = 0;
3382 addr->sadb_address_reserved = 0;
3383 if (x->props.family == AF_INET) {
3384 addr->sadb_address_prefixlen = 32;
3386 sin = (struct sockaddr_in *) (addr + 1);
3387 sin->sin_family = AF_INET;
3388 sin->sin_addr.s_addr = x->props.saddr.a4;
3390 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3392 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3393 else if (x->props.family == AF_INET6) {
3394 addr->sadb_address_prefixlen = 128;
3396 sin6 = (struct sockaddr_in6 *) (addr + 1);
3397 sin6->sin6_family = AF_INET6;
3398 sin6->sin6_port = 0;
3399 sin6->sin6_flowinfo = 0;
3400 memcpy(&sin6->sin6_addr,
3401 x->props.saddr.a6, sizeof(struct in6_addr));
3402 sin6->sin6_scope_id = 0;
3408 /* NAT_T_SPORT (old port) */
3409 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3410 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3411 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3412 n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3413 n_port->sadb_x_nat_t_port_reserved = 0;
3415 /* ADDRESS_DST (new addr) */
3416 addr = (struct sadb_address*)
3417 skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
3418 addr->sadb_address_len =
3419 (sizeof(struct sadb_address)+sockaddr_size)/
3421 addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3422 addr->sadb_address_proto = 0;
3423 addr->sadb_address_reserved = 0;
3424 if (x->props.family == AF_INET) {
3425 addr->sadb_address_prefixlen = 32;
3427 sin = (struct sockaddr_in *) (addr + 1);
3428 sin->sin_family = AF_INET;
3429 sin->sin_addr.s_addr = ipaddr->a4;
3431 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3433 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3434 else if (x->props.family == AF_INET6) {
3435 addr->sadb_address_prefixlen = 128;
3437 sin6 = (struct sockaddr_in6 *) (addr + 1);
3438 sin6->sin6_family = AF_INET6;
3439 sin6->sin6_port = 0;
3440 sin6->sin6_flowinfo = 0;
3441 memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
3442 sin6->sin6_scope_id = 0;
3448 /* NAT_T_DPORT (new port) */
3449 n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
3450 n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3451 n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3452 n_port->sadb_x_nat_t_port_port = sport;
3453 n_port->sadb_x_nat_t_port_reserved = 0;
3455 return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
3458 #ifdef CONFIG_NET_KEY_MIGRATE
3459 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3460 struct xfrm_selector *sel)
3462 struct sadb_address *addr;
3463 struct sockaddr_in *sin;
3464 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3465 struct sockaddr_in6 *sin6;
3467 addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
3468 addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3469 addr->sadb_address_exttype = type;
3470 addr->sadb_address_proto = sel->proto;
3471 addr->sadb_address_reserved = 0;
3474 case SADB_EXT_ADDRESS_SRC:
3475 if (sel->family == AF_INET) {
3476 addr->sadb_address_prefixlen = sel->prefixlen_s;
3477 sin = (struct sockaddr_in *)(addr + 1);
3478 sin->sin_family = AF_INET;
3479 memcpy(&sin->sin_addr.s_addr, &sel->saddr,
3480 sizeof(sin->sin_addr.s_addr));
3482 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3484 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3485 else if (sel->family == AF_INET6) {
3486 addr->sadb_address_prefixlen = sel->prefixlen_s;
3487 sin6 = (struct sockaddr_in6 *)(addr + 1);
3488 sin6->sin6_family = AF_INET6;
3489 sin6->sin6_port = 0;
3490 sin6->sin6_flowinfo = 0;
3491 sin6->sin6_scope_id = 0;
3492 memcpy(&sin6->sin6_addr.s6_addr, &sel->saddr,
3493 sizeof(sin6->sin6_addr.s6_addr));
3497 case SADB_EXT_ADDRESS_DST:
3498 if (sel->family == AF_INET) {
3499 addr->sadb_address_prefixlen = sel->prefixlen_d;
3500 sin = (struct sockaddr_in *)(addr + 1);
3501 sin->sin_family = AF_INET;
3502 memcpy(&sin->sin_addr.s_addr, &sel->daddr,
3503 sizeof(sin->sin_addr.s_addr));
3505 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
3507 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3508 else if (sel->family == AF_INET6) {
3509 addr->sadb_address_prefixlen = sel->prefixlen_d;
3510 sin6 = (struct sockaddr_in6 *)(addr + 1);
3511 sin6->sin6_family = AF_INET6;
3512 sin6->sin6_port = 0;
3513 sin6->sin6_flowinfo = 0;
3514 sin6->sin6_scope_id = 0;
3515 memcpy(&sin6->sin6_addr.s6_addr, &sel->daddr,
3516 sizeof(sin6->sin6_addr.s6_addr));
3527 static int set_ipsecrequest(struct sk_buff *skb,
3528 uint8_t proto, uint8_t mode, int level,
3529 uint32_t reqid, uint8_t family,
3530 xfrm_address_t *src, xfrm_address_t *dst)
3532 struct sadb_x_ipsecrequest *rq;
3533 struct sockaddr_in *sin;
3534 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3535 struct sockaddr_in6 *sin6;
3539 size_req = sizeof(struct sadb_x_ipsecrequest) +
3540 pfkey_sockaddr_pair_size(family);
3542 rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
3543 memset(rq, 0, size_req);
3544 rq->sadb_x_ipsecrequest_len = size_req;
3545 rq->sadb_x_ipsecrequest_proto = proto;
3546 rq->sadb_x_ipsecrequest_mode = mode;
3547 rq->sadb_x_ipsecrequest_level = level;
3548 rq->sadb_x_ipsecrequest_reqid = reqid;
3552 sin = (struct sockaddr_in *)(rq + 1);
3553 sin->sin_family = AF_INET;
3554 memcpy(&sin->sin_addr.s_addr, src,
3555 sizeof(sin->sin_addr.s_addr));
3557 sin->sin_family = AF_INET;
3558 memcpy(&sin->sin_addr.s_addr, dst,
3559 sizeof(sin->sin_addr.s_addr));
3561 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3563 sin6 = (struct sockaddr_in6 *)(rq + 1);
3564 sin6->sin6_family = AF_INET6;
3565 sin6->sin6_port = 0;
3566 sin6->sin6_flowinfo = 0;
3567 sin6->sin6_scope_id = 0;
3568 memcpy(&sin6->sin6_addr.s6_addr, src,
3569 sizeof(sin6->sin6_addr.s6_addr));
3571 sin6->sin6_family = AF_INET6;
3572 sin6->sin6_port = 0;
3573 sin6->sin6_flowinfo = 0;
3574 sin6->sin6_scope_id = 0;
3575 memcpy(&sin6->sin6_addr.s6_addr, dst,
3576 sizeof(sin6->sin6_addr.s6_addr));
3587 #ifdef CONFIG_NET_KEY_MIGRATE
3588 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3589 struct xfrm_migrate *m, int num_bundles)
3595 struct sk_buff *skb;
3596 struct sadb_msg *hdr;
3597 struct sadb_x_policy *pol;
3598 struct xfrm_migrate *mp;
3600 if (type != XFRM_POLICY_TYPE_MAIN)
3603 if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3607 sasize_sel = pfkey_sockaddr_size(sel->family);
3610 size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3613 size_pol += sizeof(struct sadb_x_policy);
3616 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3617 /* old locator pair */
3618 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3619 pfkey_sockaddr_pair_size(mp->old_family);
3620 /* new locator pair */
3621 size_pol += sizeof(struct sadb_x_ipsecrequest) +
3622 pfkey_sockaddr_pair_size(mp->new_family);
3625 size += sizeof(struct sadb_msg) + size_pol;
3628 skb = alloc_skb(size, GFP_ATOMIC);
3632 hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
3633 hdr->sadb_msg_version = PF_KEY_V2;
3634 hdr->sadb_msg_type = SADB_X_MIGRATE;
3635 hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3636 hdr->sadb_msg_len = size / 8;
3637 hdr->sadb_msg_errno = 0;
3638 hdr->sadb_msg_reserved = 0;
3639 hdr->sadb_msg_seq = 0;
3640 hdr->sadb_msg_pid = 0;
3643 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3646 set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3648 /* policy information */
3649 pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
3650 pol->sadb_x_policy_len = size_pol / 8;
3651 pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3652 pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3653 pol->sadb_x_policy_dir = dir + 1;
3654 pol->sadb_x_policy_id = 0;
3655 pol->sadb_x_policy_priority = 0;
3657 for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3658 /* old ipsecrequest */
3659 int mode = pfkey_mode_from_xfrm(mp->mode);
3662 if (set_ipsecrequest(skb, mp->proto, mode,
3663 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3664 mp->reqid, mp->old_family,
3665 &mp->old_saddr, &mp->old_daddr) < 0)
3668 /* new ipsecrequest */
3669 if (set_ipsecrequest(skb, mp->proto, mode,
3670 (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3671 mp->reqid, mp->new_family,
3672 &mp->new_saddr, &mp->new_daddr) < 0)
3676 /* broadcast migrate message to sockets */
3677 pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL);
3686 static int pfkey_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
3687 struct xfrm_migrate *m, int num_bundles)
3689 return -ENOPROTOOPT;
3693 static int pfkey_sendmsg(struct kiocb *kiocb,
3694 struct socket *sock, struct msghdr *msg, size_t len)
3696 struct sock *sk = sock->sk;
3697 struct sk_buff *skb = NULL;
3698 struct sadb_msg *hdr = NULL;
3702 if (msg->msg_flags & MSG_OOB)
3706 if ((unsigned)len > sk->sk_sndbuf - 32)
3710 skb = alloc_skb(len, GFP_KERNEL);
3715 if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
3718 hdr = pfkey_get_base_msg(skb, &err);
3722 mutex_lock(&xfrm_cfg_mutex);
3723 err = pfkey_process(sk, skb, hdr);
3724 mutex_unlock(&xfrm_cfg_mutex);
3727 if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3735 static int pfkey_recvmsg(struct kiocb *kiocb,
3736 struct socket *sock, struct msghdr *msg, size_t len,
3739 struct sock *sk = sock->sk;
3740 struct pfkey_sock *pfk = pfkey_sk(sk);
3741 struct sk_buff *skb;
3745 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3748 msg->msg_namelen = 0;
3749 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3755 msg->msg_flags |= MSG_TRUNC;
3759 skb_reset_transport_header(skb);
3760 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
3764 sock_recv_timestamp(msg, sk, skb);
3766 err = (flags & MSG_TRUNC) ? skb->len : copied;
3768 if (pfk->dump.dump != NULL &&
3769 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3773 skb_free_datagram(sk, skb);
3778 static const struct proto_ops pfkey_ops = {
3780 .owner = THIS_MODULE,
3781 /* Operations that make no sense on pfkey sockets. */
3782 .bind = sock_no_bind,
3783 .connect = sock_no_connect,
3784 .socketpair = sock_no_socketpair,
3785 .accept = sock_no_accept,
3786 .getname = sock_no_getname,
3787 .ioctl = sock_no_ioctl,
3788 .listen = sock_no_listen,
3789 .shutdown = sock_no_shutdown,
3790 .setsockopt = sock_no_setsockopt,
3791 .getsockopt = sock_no_getsockopt,
3792 .mmap = sock_no_mmap,
3793 .sendpage = sock_no_sendpage,
3795 /* Now the operations that really occur. */
3796 .release = pfkey_release,
3797 .poll = datagram_poll,
3798 .sendmsg = pfkey_sendmsg,
3799 .recvmsg = pfkey_recvmsg,
3802 static struct net_proto_family pfkey_family_ops = {
3804 .create = pfkey_create,
3805 .owner = THIS_MODULE,
3808 #ifdef CONFIG_PROC_FS
3809 static int pfkey_seq_show(struct seq_file *f, void *v)
3813 s = (struct sock *)v;
3814 if (v == SEQ_START_TOKEN)
3815 seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
3817 seq_printf(f ,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3819 atomic_read(&s->sk_refcnt),
3820 atomic_read(&s->sk_rmem_alloc),
3821 atomic_read(&s->sk_wmem_alloc),
3828 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3831 struct hlist_node *node;
3834 read_lock(&pfkey_table_lock);
3836 return SEQ_START_TOKEN;
3838 sk_for_each(s, node, &pfkey_table)
3845 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3848 return (v == SEQ_START_TOKEN) ?
3849 sk_head(&pfkey_table) :
3850 sk_next((struct sock *)v);
3853 static void pfkey_seq_stop(struct seq_file *f, void *v)
3855 read_unlock(&pfkey_table_lock);
3858 static struct seq_operations pfkey_seq_ops = {
3859 .start = pfkey_seq_start,
3860 .next = pfkey_seq_next,
3861 .stop = pfkey_seq_stop,
3862 .show = pfkey_seq_show,
3865 static int pfkey_seq_open(struct inode *inode, struct file *file)
3867 return seq_open(file, &pfkey_seq_ops);
3870 static struct file_operations pfkey_proc_ops = {
3871 .open = pfkey_seq_open,
3873 .llseek = seq_lseek,
3874 .release = seq_release,
3877 static int pfkey_init_proc(void)
3879 struct proc_dir_entry *e;
3881 e = proc_net_fops_create(&init_net, "pfkey", 0, &pfkey_proc_ops);
3888 static void pfkey_exit_proc(void)
3890 proc_net_remove(&init_net, "pfkey");
3893 static inline int pfkey_init_proc(void)
3898 static inline void pfkey_exit_proc(void)
3903 static struct xfrm_mgr pfkeyv2_mgr =
3906 .notify = pfkey_send_notify,
3907 .acquire = pfkey_send_acquire,
3908 .compile_policy = pfkey_compile_policy,
3909 .new_mapping = pfkey_send_new_mapping,
3910 .notify_policy = pfkey_send_policy_notify,
3911 .migrate = pfkey_send_migrate,
3914 static void __exit ipsec_pfkey_exit(void)
3916 xfrm_unregister_km(&pfkeyv2_mgr);
3918 sock_unregister(PF_KEY);
3919 proto_unregister(&key_proto);
3922 static int __init ipsec_pfkey_init(void)
3924 int err = proto_register(&key_proto, 0);
3929 err = sock_register(&pfkey_family_ops);
3931 goto out_unregister_key_proto;
3932 err = pfkey_init_proc();
3934 goto out_sock_unregister;
3935 err = xfrm_register_km(&pfkeyv2_mgr);
3937 goto out_remove_proc_entry;
3940 out_remove_proc_entry:
3942 out_sock_unregister:
3943 sock_unregister(PF_KEY);
3944 out_unregister_key_proto:
3945 proto_unregister(&key_proto);
3949 module_init(ipsec_pfkey_init);
3950 module_exit(ipsec_pfkey_exit);
3951 MODULE_LICENSE("GPL");
3952 MODULE_ALIAS_NETPROTO(PF_KEY);