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>
30 #include <net/netns/generic.h>
35 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
36 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 static int pfkey_net_id
;
40 /* List of all pfkey sockets. */
41 struct hlist_head table
;
44 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait
);
45 static DEFINE_RWLOCK(pfkey_table_lock
);
46 static atomic_t pfkey_table_users
= ATOMIC_INIT(0);
49 /* struct sock must be the first member of struct pfkey_sock */
57 int (*dump
)(struct pfkey_sock
*sk
);
58 void (*done
)(struct pfkey_sock
*sk
);
60 struct xfrm_policy_walk policy
;
61 struct xfrm_state_walk state
;
67 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
69 return (struct pfkey_sock
*)sk
;
72 static int pfkey_can_dump(struct sock
*sk
)
74 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
79 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
83 kfree_skb(pfk
->dump
.skb
);
87 pfk
->dump
.dump
= NULL
;
88 pfk
->dump
.done
= NULL
;
92 static void pfkey_sock_destruct(struct sock
*sk
)
94 struct net
*net
= sock_net(sk
);
95 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
97 pfkey_terminate_dump(pfkey_sk(sk
));
98 skb_queue_purge(&sk
->sk_receive_queue
);
100 if (!sock_flag(sk
, SOCK_DEAD
)) {
101 printk("Attempt to release alive pfkey socket: %p\n", sk
);
105 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
106 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
108 atomic_dec(&net_pfkey
->socks_nr
);
111 static void pfkey_table_grab(void)
113 write_lock_bh(&pfkey_table_lock
);
115 if (atomic_read(&pfkey_table_users
)) {
116 DECLARE_WAITQUEUE(wait
, current
);
118 add_wait_queue_exclusive(&pfkey_table_wait
, &wait
);
120 set_current_state(TASK_UNINTERRUPTIBLE
);
121 if (atomic_read(&pfkey_table_users
) == 0)
123 write_unlock_bh(&pfkey_table_lock
);
125 write_lock_bh(&pfkey_table_lock
);
128 __set_current_state(TASK_RUNNING
);
129 remove_wait_queue(&pfkey_table_wait
, &wait
);
133 static __inline__
void pfkey_table_ungrab(void)
135 write_unlock_bh(&pfkey_table_lock
);
136 wake_up(&pfkey_table_wait
);
139 static __inline__
void pfkey_lock_table(void)
141 /* read_lock() synchronizes us to pfkey_table_grab */
143 read_lock(&pfkey_table_lock
);
144 atomic_inc(&pfkey_table_users
);
145 read_unlock(&pfkey_table_lock
);
148 static __inline__
void pfkey_unlock_table(void)
150 if (atomic_dec_and_test(&pfkey_table_users
))
151 wake_up(&pfkey_table_wait
);
155 static const struct proto_ops pfkey_ops
;
157 static void pfkey_insert(struct sock
*sk
)
159 struct net
*net
= sock_net(sk
);
160 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
163 sk_add_node(sk
, &net_pfkey
->table
);
164 pfkey_table_ungrab();
167 static void pfkey_remove(struct sock
*sk
)
170 sk_del_node_init(sk
);
171 pfkey_table_ungrab();
174 static struct proto key_proto
= {
176 .owner
= THIS_MODULE
,
177 .obj_size
= sizeof(struct pfkey_sock
),
180 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
)
182 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
186 if (!capable(CAP_NET_ADMIN
))
188 if (sock
->type
!= SOCK_RAW
)
189 return -ESOCKTNOSUPPORT
;
190 if (protocol
!= PF_KEY_V2
)
191 return -EPROTONOSUPPORT
;
194 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
);
198 sock
->ops
= &pfkey_ops
;
199 sock_init_data(sock
, sk
);
201 sk
->sk_family
= PF_KEY
;
202 sk
->sk_destruct
= pfkey_sock_destruct
;
204 atomic_inc(&net_pfkey
->socks_nr
);
213 static int pfkey_release(struct socket
*sock
)
215 struct sock
*sk
= sock
->sk
;
224 skb_queue_purge(&sk
->sk_write_queue
);
230 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
231 gfp_t allocation
, struct sock
*sk
)
237 if (atomic_read(&skb
->users
) != 1) {
238 *skb2
= skb_clone(skb
, allocation
);
241 atomic_inc(&skb
->users
);
245 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
247 skb_set_owner_r(*skb2
, sk
);
248 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
249 sk
->sk_data_ready(sk
, (*skb2
)->len
);
258 /* Send SKB to all pfkey sockets matching selected criteria. */
259 #define BROADCAST_ALL 0
260 #define BROADCAST_ONE 1
261 #define BROADCAST_REGISTERED 2
262 #define BROADCAST_PROMISC_ONLY 4
263 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
264 int broadcast_flags
, struct sock
*one_sk
,
267 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
269 struct hlist_node
*node
;
270 struct sk_buff
*skb2
= NULL
;
273 /* XXX Do we need something like netlink_overrun? I think
274 * XXX PF_KEY socket apps will not mind current behavior.
280 sk_for_each(sk
, node
, &net_pfkey
->table
) {
281 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
284 /* Yes, it means that if you are meant to receive this
285 * pfkey message you receive it twice as promiscuous
289 pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
291 /* the exact target will be processed later */
294 if (broadcast_flags
!= BROADCAST_ALL
) {
295 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
297 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
300 if (broadcast_flags
& BROADCAST_ONE
)
304 err2
= pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
306 /* Error is cleare after succecful sending to at least one
308 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
311 pfkey_unlock_table();
314 err
= pfkey_broadcast_one(skb
, &skb2
, allocation
, one_sk
);
321 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
323 struct sadb_msg
*hdr
;
326 rc
= pfk
->dump
.dump(pfk
);
331 if (!pfkey_can_dump(&pfk
->sk
))
334 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
335 hdr
->sadb_msg_seq
= 0;
336 hdr
->sadb_msg_errno
= rc
;
337 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
338 &pfk
->sk
, sock_net(&pfk
->sk
));
339 pfk
->dump
.skb
= NULL
;
342 pfkey_terminate_dump(pfk
);
346 static inline void pfkey_hdr_dup(struct sadb_msg
*new, struct sadb_msg
*orig
)
351 static int pfkey_error(struct sadb_msg
*orig
, int err
, struct sock
*sk
)
353 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
354 struct sadb_msg
*hdr
;
359 /* Woe be to the platform trying to support PFKEY yet
360 * having normal errnos outside the 1-255 range, inclusive.
363 if (err
== ERESTARTSYS
||
364 err
== ERESTARTNOHAND
||
365 err
== ERESTARTNOINTR
)
369 BUG_ON(err
<= 0 || err
>= 256);
371 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
372 pfkey_hdr_dup(hdr
, orig
);
373 hdr
->sadb_msg_errno
= (uint8_t) err
;
374 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
377 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, sock_net(sk
));
382 static u8 sadb_ext_min_len
[] = {
383 [SADB_EXT_RESERVED
] = (u8
) 0,
384 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
385 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
386 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
387 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
388 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
389 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
390 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
391 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
392 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
393 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
394 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
395 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
396 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
397 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
398 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
399 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
400 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
401 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
402 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
403 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
404 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
405 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
406 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
407 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
408 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
411 /* Verify sadb_address_{len,prefixlen} against sa_family. */
412 static int verify_address_len(void *p
)
414 struct sadb_address
*sp
= p
;
415 struct sockaddr
*addr
= (struct sockaddr
*)(sp
+ 1);
416 struct sockaddr_in
*sin
;
417 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
418 struct sockaddr_in6
*sin6
;
422 switch (addr
->sa_family
) {
424 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
425 if (sp
->sadb_address_len
!= len
||
426 sp
->sadb_address_prefixlen
> 32)
429 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
431 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
432 if (sp
->sadb_address_len
!= len
||
433 sp
->sadb_address_prefixlen
> 128)
438 /* It is user using kernel to keep track of security
439 * associations for another protocol, such as
440 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
443 * XXX Actually, association/policy database is not yet
444 * XXX able to cope with arbitrary sockaddr families.
445 * XXX When it can, remove this -EINVAL. -DaveM
454 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx
*sec_ctx
)
456 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
457 sec_ctx
->sadb_x_ctx_len
,
461 static inline int verify_sec_ctx_len(void *p
)
463 struct sadb_x_sec_ctx
*sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
464 int len
= sec_ctx
->sadb_x_ctx_len
;
469 len
= pfkey_sec_ctx_len(sec_ctx
);
471 if (sec_ctx
->sadb_x_sec_len
!= len
)
477 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx
*sec_ctx
)
479 struct xfrm_user_sec_ctx
*uctx
= NULL
;
480 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
482 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), GFP_KERNEL
);
487 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
488 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
489 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
490 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
491 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
492 memcpy(uctx
+ 1, sec_ctx
+ 1,
498 static int present_and_same_family(struct sadb_address
*src
,
499 struct sadb_address
*dst
)
501 struct sockaddr
*s_addr
, *d_addr
;
506 s_addr
= (struct sockaddr
*)(src
+ 1);
507 d_addr
= (struct sockaddr
*)(dst
+ 1);
508 if (s_addr
->sa_family
!= d_addr
->sa_family
)
510 if (s_addr
->sa_family
!= AF_INET
511 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
512 && s_addr
->sa_family
!= AF_INET6
520 static int parse_exthdrs(struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
522 char *p
= (char *) hdr
;
528 struct sadb_ext
*ehdr
= (struct sadb_ext
*) p
;
532 ext_len
= ehdr
->sadb_ext_len
;
533 ext_len
*= sizeof(uint64_t);
534 ext_type
= ehdr
->sadb_ext_type
;
535 if (ext_len
< sizeof(uint64_t) ||
537 ext_type
== SADB_EXT_RESERVED
)
540 if (ext_type
<= SADB_EXT_MAX
) {
541 int min
= (int) sadb_ext_min_len
[ext_type
];
544 if (ext_hdrs
[ext_type
-1] != NULL
)
546 if (ext_type
== SADB_EXT_ADDRESS_SRC
||
547 ext_type
== SADB_EXT_ADDRESS_DST
||
548 ext_type
== SADB_EXT_ADDRESS_PROXY
||
549 ext_type
== SADB_X_EXT_NAT_T_OA
) {
550 if (verify_address_len(p
))
553 if (ext_type
== SADB_X_EXT_SEC_CTX
) {
554 if (verify_sec_ctx_len(p
))
557 ext_hdrs
[ext_type
-1] = p
;
567 pfkey_satype2proto(uint8_t satype
)
570 case SADB_SATYPE_UNSPEC
:
571 return IPSEC_PROTO_ANY
;
574 case SADB_SATYPE_ESP
:
576 case SADB_X_SATYPE_IPCOMP
:
586 pfkey_proto2satype(uint16_t proto
)
590 return SADB_SATYPE_AH
;
592 return SADB_SATYPE_ESP
;
594 return SADB_X_SATYPE_IPCOMP
;
602 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
603 * say specifically 'just raw sockets' as we encode them as 255.
606 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
608 return (proto
== IPSEC_PROTO_ANY
? 0 : proto
);
611 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
613 return (proto
? proto
: IPSEC_PROTO_ANY
);
616 static inline int pfkey_sockaddr_len(sa_family_t family
)
620 return sizeof(struct sockaddr_in
);
621 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
623 return sizeof(struct sockaddr_in6
);
630 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
632 switch (sa
->sa_family
) {
635 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
637 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
640 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
641 sizeof(struct in6_addr
));
649 int pfkey_sadb_addr2xfrm_addr(struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
651 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
655 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct net
*net
, struct sadb_msg
*hdr
, void **ext_hdrs
)
658 struct sadb_address
*addr
;
660 unsigned short family
;
661 xfrm_address_t
*xaddr
;
663 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
667 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
671 /* sadb_address_len should be checked by caller */
672 addr
= (struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
676 family
= ((struct sockaddr
*)(addr
+ 1))->sa_family
;
679 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
681 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
683 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
693 return xfrm_state_lookup(net
, xaddr
, sa
->sadb_sa_spi
, proto
, family
);
696 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
699 pfkey_sockaddr_size(sa_family_t family
)
701 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
704 static inline int pfkey_mode_from_xfrm(int mode
)
707 case XFRM_MODE_TRANSPORT
:
708 return IPSEC_MODE_TRANSPORT
;
709 case XFRM_MODE_TUNNEL
:
710 return IPSEC_MODE_TUNNEL
;
712 return IPSEC_MODE_BEET
;
718 static inline int pfkey_mode_to_xfrm(int mode
)
721 case IPSEC_MODE_ANY
: /*XXX*/
722 case IPSEC_MODE_TRANSPORT
:
723 return XFRM_MODE_TRANSPORT
;
724 case IPSEC_MODE_TUNNEL
:
725 return XFRM_MODE_TUNNEL
;
726 case IPSEC_MODE_BEET
:
727 return XFRM_MODE_BEET
;
733 static unsigned int pfkey_sockaddr_fill(xfrm_address_t
*xaddr
, __be16 port
,
735 unsigned short family
)
740 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
741 sin
->sin_family
= AF_INET
;
742 sin
->sin_port
= port
;
743 sin
->sin_addr
.s_addr
= xaddr
->a4
;
744 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
747 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
750 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
751 sin6
->sin6_family
= AF_INET6
;
752 sin6
->sin6_port
= port
;
753 sin6
->sin6_flowinfo
= 0;
754 ipv6_addr_copy(&sin6
->sin6_addr
, (struct in6_addr
*)xaddr
->a6
);
755 sin6
->sin6_scope_id
= 0;
763 static struct sk_buff
*__pfkey_xfrm_state2msg(struct xfrm_state
*x
,
764 int add_keys
, int hsc
)
767 struct sadb_msg
*hdr
;
769 struct sadb_lifetime
*lifetime
;
770 struct sadb_address
*addr
;
771 struct sadb_key
*key
;
772 struct sadb_x_sa2
*sa2
;
773 struct sadb_x_sec_ctx
*sec_ctx
;
774 struct xfrm_sec_ctx
*xfrm_ctx
;
777 int auth_key_size
= 0;
778 int encrypt_key_size
= 0;
780 struct xfrm_encap_tmpl
*natt
= NULL
;
783 /* address family check */
784 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
786 return ERR_PTR(-EINVAL
);
788 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
789 key(AE), (identity(SD),) (sensitivity)> */
790 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
791 sizeof(struct sadb_lifetime
) +
792 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
793 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
794 sizeof(struct sadb_address
)*2 +
796 sizeof(struct sadb_x_sa2
);
798 if ((xfrm_ctx
= x
->security
)) {
799 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
800 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
803 /* identity & sensitivity */
804 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
805 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
808 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
810 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
811 size
+= sizeof(struct sadb_key
) + auth_key_size
;
813 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
815 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
816 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
822 if (natt
&& natt
->encap_type
) {
823 size
+= sizeof(struct sadb_x_nat_t_type
);
824 size
+= sizeof(struct sadb_x_nat_t_port
);
825 size
+= sizeof(struct sadb_x_nat_t_port
);
828 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
830 return ERR_PTR(-ENOBUFS
);
832 /* call should fill header later */
833 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
834 memset(hdr
, 0, size
); /* XXX do we need this ? */
835 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
838 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
839 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
840 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
841 sa
->sadb_sa_spi
= x
->id
.spi
;
842 sa
->sadb_sa_replay
= x
->props
.replay_window
;
843 switch (x
->km
.state
) {
844 case XFRM_STATE_VALID
:
845 sa
->sadb_sa_state
= x
->km
.dying
?
846 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
849 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
852 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
855 sa
->sadb_sa_auth
= 0;
857 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
858 sa
->sadb_sa_auth
= a
? a
->desc
.sadb_alg_id
: 0;
860 sa
->sadb_sa_encrypt
= 0;
861 BUG_ON(x
->ealg
&& x
->calg
);
863 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
864 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
866 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
868 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
869 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
872 sa
->sadb_sa_flags
= 0;
873 if (x
->props
.flags
& XFRM_STATE_NOECN
)
874 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
875 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
876 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
877 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
878 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
882 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
883 sizeof(struct sadb_lifetime
));
884 lifetime
->sadb_lifetime_len
=
885 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
886 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
887 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
888 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
889 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
890 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
894 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
895 sizeof(struct sadb_lifetime
));
896 lifetime
->sadb_lifetime_len
=
897 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
898 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
899 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
900 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
901 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
902 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
905 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
906 sizeof(struct sadb_lifetime
));
907 lifetime
->sadb_lifetime_len
=
908 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
909 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
910 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
911 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
912 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
913 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
915 addr
= (struct sadb_address
*) skb_put(skb
,
916 sizeof(struct sadb_address
)+sockaddr_size
);
917 addr
->sadb_address_len
=
918 (sizeof(struct sadb_address
)+sockaddr_size
)/
920 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
921 /* "if the ports are non-zero, then the sadb_address_proto field,
922 normally zero, MUST be filled in with the transport
923 protocol's number." - RFC2367 */
924 addr
->sadb_address_proto
= 0;
925 addr
->sadb_address_reserved
= 0;
927 addr
->sadb_address_prefixlen
=
928 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
929 (struct sockaddr
*) (addr
+ 1),
931 if (!addr
->sadb_address_prefixlen
)
935 addr
= (struct sadb_address
*) skb_put(skb
,
936 sizeof(struct sadb_address
)+sockaddr_size
);
937 addr
->sadb_address_len
=
938 (sizeof(struct sadb_address
)+sockaddr_size
)/
940 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
941 addr
->sadb_address_proto
= 0;
942 addr
->sadb_address_reserved
= 0;
944 addr
->sadb_address_prefixlen
=
945 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
946 (struct sockaddr
*) (addr
+ 1),
948 if (!addr
->sadb_address_prefixlen
)
951 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
,
953 addr
= (struct sadb_address
*) skb_put(skb
,
954 sizeof(struct sadb_address
)+sockaddr_size
);
955 addr
->sadb_address_len
=
956 (sizeof(struct sadb_address
)+sockaddr_size
)/
958 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
959 addr
->sadb_address_proto
=
960 pfkey_proto_from_xfrm(x
->sel
.proto
);
961 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
962 addr
->sadb_address_reserved
= 0;
964 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
965 (struct sockaddr
*) (addr
+ 1),
970 if (add_keys
&& auth_key_size
) {
971 key
= (struct sadb_key
*) skb_put(skb
,
972 sizeof(struct sadb_key
)+auth_key_size
);
973 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
975 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
976 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
977 key
->sadb_key_reserved
= 0;
978 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
981 if (add_keys
&& encrypt_key_size
) {
982 key
= (struct sadb_key
*) skb_put(skb
,
983 sizeof(struct sadb_key
)+encrypt_key_size
);
984 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
985 encrypt_key_size
) / sizeof(uint64_t);
986 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
987 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
988 key
->sadb_key_reserved
= 0;
989 memcpy(key
+ 1, x
->ealg
->alg_key
,
990 (x
->ealg
->alg_key_len
+7)/8);
994 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
995 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
996 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
997 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
999 return ERR_PTR(-EINVAL
);
1001 sa2
->sadb_x_sa2_mode
= mode
;
1002 sa2
->sadb_x_sa2_reserved1
= 0;
1003 sa2
->sadb_x_sa2_reserved2
= 0;
1004 sa2
->sadb_x_sa2_sequence
= 0;
1005 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
1007 if (natt
&& natt
->encap_type
) {
1008 struct sadb_x_nat_t_type
*n_type
;
1009 struct sadb_x_nat_t_port
*n_port
;
1012 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
1013 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
1014 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1015 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
1016 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
1017 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
1018 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
1021 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
1022 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1023 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1024 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
1025 n_port
->sadb_x_nat_t_port_reserved
= 0;
1028 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
1029 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1030 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
1031 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
1032 n_port
->sadb_x_nat_t_port_reserved
= 0;
1035 /* security context */
1037 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
1038 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1039 sec_ctx
->sadb_x_sec_len
=
1040 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1041 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1042 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1043 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1044 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1045 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1053 static inline struct sk_buff
*pfkey_xfrm_state2msg(struct xfrm_state
*x
)
1055 struct sk_buff
*skb
;
1057 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1062 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(struct xfrm_state
*x
,
1065 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1068 static struct xfrm_state
* pfkey_msg2xfrm_state(struct net
*net
,
1069 struct sadb_msg
*hdr
,
1072 struct xfrm_state
*x
;
1073 struct sadb_lifetime
*lifetime
;
1075 struct sadb_key
*key
;
1076 struct sadb_x_sec_ctx
*sec_ctx
;
1081 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
1083 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1084 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1085 return ERR_PTR(-EINVAL
);
1086 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1087 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1088 return ERR_PTR(-EINVAL
);
1089 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1090 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1091 return ERR_PTR(-EINVAL
);
1092 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1093 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1094 return ERR_PTR(-EINVAL
);
1096 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1098 return ERR_PTR(-EINVAL
);
1100 /* default error is no buffer space */
1105 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1106 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1107 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1108 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1109 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1112 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1113 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1115 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1116 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1117 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1118 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1119 return ERR_PTR(-EINVAL
);
1120 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1122 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1123 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1124 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1125 return ERR_PTR(-EINVAL
);
1126 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1128 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1129 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1130 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1131 return ERR_PTR(-EINVAL
);
1133 x
= xfrm_state_alloc(net
);
1135 return ERR_PTR(-ENOBUFS
);
1137 x
->id
.proto
= proto
;
1138 x
->id
.spi
= sa
->sadb_sa_spi
;
1139 x
->props
.replay_window
= sa
->sadb_sa_replay
;
1140 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1141 x
->props
.flags
|= XFRM_STATE_NOECN
;
1142 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1143 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1144 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1145 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1147 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1];
1148 if (lifetime
!= NULL
) {
1149 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1150 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1151 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1152 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1154 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1];
1155 if (lifetime
!= NULL
) {
1156 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1157 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1158 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1159 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1162 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
1163 if (sec_ctx
!= NULL
) {
1164 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
1169 err
= security_xfrm_state_alloc(x
, uctx
);
1176 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1177 if (sa
->sadb_sa_auth
) {
1179 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1185 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1186 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1189 strcpy(x
->aalg
->alg_name
, a
->name
);
1190 x
->aalg
->alg_key_len
= 0;
1192 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1193 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1195 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1196 /* x->algo.flags = sa->sadb_sa_flags; */
1198 if (sa
->sadb_sa_encrypt
) {
1199 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1200 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1205 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1208 strcpy(x
->calg
->alg_name
, a
->name
);
1209 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1212 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1217 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1219 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1220 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1223 strcpy(x
->ealg
->alg_name
, a
->name
);
1224 x
->ealg
->alg_key_len
= 0;
1226 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1227 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1229 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1232 /* x->algo.flags = sa->sadb_sa_flags; */
1234 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1236 if (!x
->props
.family
) {
1237 err
= -EAFNOSUPPORT
;
1240 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1243 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1244 struct sadb_x_sa2
*sa2
= (void*)ext_hdrs
[SADB_X_EXT_SA2
-1];
1245 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1250 x
->props
.mode
= mode
;
1251 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1254 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1255 struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1257 /* Nobody uses this, but we try. */
1258 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1259 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1263 x
->sel
.family
= x
->props
.family
;
1265 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1266 struct sadb_x_nat_t_type
* n_type
;
1267 struct xfrm_encap_tmpl
*natt
;
1269 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1274 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1275 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1277 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1278 struct sadb_x_nat_t_port
* n_port
=
1279 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1280 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1282 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1283 struct sadb_x_nat_t_port
* n_port
=
1284 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1285 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1287 memset(&natt
->encap_oa
, 0, sizeof(natt
->encap_oa
));
1290 err
= xfrm_init_state(x
);
1294 x
->km
.seq
= hdr
->sadb_msg_seq
;
1298 x
->km
.state
= XFRM_STATE_DEAD
;
1300 return ERR_PTR(err
);
1303 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1308 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1310 struct net
*net
= sock_net(sk
);
1311 struct sk_buff
*resp_skb
;
1312 struct sadb_x_sa2
*sa2
;
1313 struct sadb_address
*saddr
, *daddr
;
1314 struct sadb_msg
*out_hdr
;
1315 struct sadb_spirange
*range
;
1316 struct xfrm_state
*x
= NULL
;
1319 u32 min_spi
, max_spi
;
1322 unsigned short family
;
1323 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1325 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1326 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1329 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1333 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1334 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1337 reqid
= sa2
->sadb_x_sa2_reqid
;
1343 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1344 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1346 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1349 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1350 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1352 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1354 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1355 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1360 if (hdr
->sadb_msg_seq
) {
1361 x
= xfrm_find_acq_byseq(net
, hdr
->sadb_msg_seq
);
1362 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, xdaddr
, family
)) {
1369 x
= xfrm_find_acq(net
, mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1375 max_spi
= 0x0fffffff;
1377 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1379 min_spi
= range
->sadb_spirange_min
;
1380 max_spi
= range
->sadb_spirange_max
;
1383 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1384 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1386 if (IS_ERR(resp_skb
)) {
1388 return PTR_ERR(resp_skb
);
1391 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1392 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1393 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1394 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1395 out_hdr
->sadb_msg_errno
= 0;
1396 out_hdr
->sadb_msg_reserved
= 0;
1397 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1398 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1402 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, net
);
1407 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1409 struct net
*net
= sock_net(sk
);
1410 struct xfrm_state
*x
;
1412 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1415 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1418 x
= xfrm_find_acq_byseq(net
, hdr
->sadb_msg_seq
);
1422 spin_lock_bh(&x
->lock
);
1423 if (x
->km
.state
== XFRM_STATE_ACQ
) {
1424 x
->km
.state
= XFRM_STATE_ERROR
;
1425 wake_up(&net
->xfrm
.km_waitq
);
1427 spin_unlock_bh(&x
->lock
);
1432 static inline int event2poltype(int event
)
1435 case XFRM_MSG_DELPOLICY
:
1436 return SADB_X_SPDDELETE
;
1437 case XFRM_MSG_NEWPOLICY
:
1438 return SADB_X_SPDADD
;
1439 case XFRM_MSG_UPDPOLICY
:
1440 return SADB_X_SPDUPDATE
;
1441 case XFRM_MSG_POLEXPIRE
:
1442 // return SADB_X_SPDEXPIRE;
1444 printk("pfkey: Unknown policy event %d\n", event
);
1451 static inline int event2keytype(int event
)
1454 case XFRM_MSG_DELSA
:
1456 case XFRM_MSG_NEWSA
:
1458 case XFRM_MSG_UPDSA
:
1460 case XFRM_MSG_EXPIRE
:
1463 printk("pfkey: Unknown SA event %d\n", event
);
1471 static int key_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1473 struct sk_buff
*skb
;
1474 struct sadb_msg
*hdr
;
1476 skb
= pfkey_xfrm_state2msg(x
);
1479 return PTR_ERR(skb
);
1481 hdr
= (struct sadb_msg
*) skb
->data
;
1482 hdr
->sadb_msg_version
= PF_KEY_V2
;
1483 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1484 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1485 hdr
->sadb_msg_errno
= 0;
1486 hdr
->sadb_msg_reserved
= 0;
1487 hdr
->sadb_msg_seq
= c
->seq
;
1488 hdr
->sadb_msg_pid
= c
->pid
;
1490 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xs_net(x
));
1495 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1497 struct net
*net
= sock_net(sk
);
1498 struct xfrm_state
*x
;
1502 x
= pfkey_msg2xfrm_state(net
, hdr
, ext_hdrs
);
1507 if (hdr
->sadb_msg_type
== SADB_ADD
)
1508 err
= xfrm_state_add(x
);
1510 err
= xfrm_state_update(x
);
1512 xfrm_audit_state_add(x
, err
? 0 : 1,
1513 audit_get_loginuid(current
),
1514 audit_get_sessionid(current
), 0);
1517 x
->km
.state
= XFRM_STATE_DEAD
;
1518 __xfrm_state_put(x
);
1522 if (hdr
->sadb_msg_type
== SADB_ADD
)
1523 c
.event
= XFRM_MSG_NEWSA
;
1525 c
.event
= XFRM_MSG_UPDSA
;
1526 c
.seq
= hdr
->sadb_msg_seq
;
1527 c
.pid
= hdr
->sadb_msg_pid
;
1528 km_state_notify(x
, &c
);
1534 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1536 struct net
*net
= sock_net(sk
);
1537 struct xfrm_state
*x
;
1541 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1542 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1543 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1546 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1550 if ((err
= security_xfrm_state_delete(x
)))
1553 if (xfrm_state_kern(x
)) {
1558 err
= xfrm_state_delete(x
);
1563 c
.seq
= hdr
->sadb_msg_seq
;
1564 c
.pid
= hdr
->sadb_msg_pid
;
1565 c
.event
= XFRM_MSG_DELSA
;
1566 km_state_notify(x
, &c
);
1568 xfrm_audit_state_delete(x
, err
? 0 : 1,
1569 audit_get_loginuid(current
),
1570 audit_get_sessionid(current
), 0);
1576 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1578 struct net
*net
= sock_net(sk
);
1580 struct sk_buff
*out_skb
;
1581 struct sadb_msg
*out_hdr
;
1582 struct xfrm_state
*x
;
1584 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1585 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1586 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1589 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1593 out_skb
= pfkey_xfrm_state2msg(x
);
1594 proto
= x
->id
.proto
;
1596 if (IS_ERR(out_skb
))
1597 return PTR_ERR(out_skb
);
1599 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1600 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1601 out_hdr
->sadb_msg_type
= SADB_GET
;
1602 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1603 out_hdr
->sadb_msg_errno
= 0;
1604 out_hdr
->sadb_msg_reserved
= 0;
1605 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1606 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1607 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, sock_net(sk
));
1612 static struct sk_buff
*compose_sadb_supported(struct sadb_msg
*orig
,
1615 struct sk_buff
*skb
;
1616 struct sadb_msg
*hdr
;
1617 int len
, auth_len
, enc_len
, i
;
1619 auth_len
= xfrm_count_auth_supported();
1621 auth_len
*= sizeof(struct sadb_alg
);
1622 auth_len
+= sizeof(struct sadb_supported
);
1625 enc_len
= xfrm_count_enc_supported();
1627 enc_len
*= sizeof(struct sadb_alg
);
1628 enc_len
+= sizeof(struct sadb_supported
);
1631 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1633 skb
= alloc_skb(len
+ 16, allocation
);
1637 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1638 pfkey_hdr_dup(hdr
, orig
);
1639 hdr
->sadb_msg_errno
= 0;
1640 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1643 struct sadb_supported
*sp
;
1644 struct sadb_alg
*ap
;
1646 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1647 ap
= (struct sadb_alg
*) (sp
+ 1);
1649 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1650 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1652 for (i
= 0; ; i
++) {
1653 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1656 if (aalg
->available
)
1662 struct sadb_supported
*sp
;
1663 struct sadb_alg
*ap
;
1665 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1666 ap
= (struct sadb_alg
*) (sp
+ 1);
1668 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1669 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1671 for (i
= 0; ; i
++) {
1672 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1675 if (ealg
->available
)
1684 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1686 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1687 struct sk_buff
*supp_skb
;
1689 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1692 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1693 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1695 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1700 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1702 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1703 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1708 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
, sock_net(sk
));
1713 static int key_notify_sa_flush(struct km_event
*c
)
1715 struct sk_buff
*skb
;
1716 struct sadb_msg
*hdr
;
1718 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1721 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1722 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1723 hdr
->sadb_msg_type
= SADB_FLUSH
;
1724 hdr
->sadb_msg_seq
= c
->seq
;
1725 hdr
->sadb_msg_pid
= c
->pid
;
1726 hdr
->sadb_msg_version
= PF_KEY_V2
;
1727 hdr
->sadb_msg_errno
= (uint8_t) 0;
1728 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1730 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
1735 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1737 struct net
*net
= sock_net(sk
);
1740 struct xfrm_audit audit_info
;
1743 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1747 audit_info
.loginuid
= audit_get_loginuid(current
);
1748 audit_info
.sessionid
= audit_get_sessionid(current
);
1749 audit_info
.secid
= 0;
1750 err
= xfrm_state_flush(net
, proto
, &audit_info
);
1753 c
.data
.proto
= proto
;
1754 c
.seq
= hdr
->sadb_msg_seq
;
1755 c
.pid
= hdr
->sadb_msg_pid
;
1756 c
.event
= XFRM_MSG_FLUSHSA
;
1758 km_state_notify(NULL
, &c
);
1763 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1765 struct pfkey_sock
*pfk
= ptr
;
1766 struct sk_buff
*out_skb
;
1767 struct sadb_msg
*out_hdr
;
1769 if (!pfkey_can_dump(&pfk
->sk
))
1772 out_skb
= pfkey_xfrm_state2msg(x
);
1773 if (IS_ERR(out_skb
))
1774 return PTR_ERR(out_skb
);
1776 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1777 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1778 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1779 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1780 out_hdr
->sadb_msg_errno
= 0;
1781 out_hdr
->sadb_msg_reserved
= 0;
1782 out_hdr
->sadb_msg_seq
= count
+ 1;
1783 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
1786 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
1787 &pfk
->sk
, sock_net(&pfk
->sk
));
1788 pfk
->dump
.skb
= out_skb
;
1793 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1795 struct net
*net
= sock_net(&pfk
->sk
);
1796 return xfrm_state_walk(net
, &pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1799 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1801 xfrm_state_walk_done(&pfk
->dump
.u
.state
);
1804 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1807 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1809 if (pfk
->dump
.dump
!= NULL
)
1812 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1816 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1817 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
1818 pfk
->dump
.dump
= pfkey_dump_sa
;
1819 pfk
->dump
.done
= pfkey_dump_sa_done
;
1820 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
);
1822 return pfkey_do_dump(pfk
);
1825 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1827 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1828 int satype
= hdr
->sadb_msg_satype
;
1830 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1831 /* XXX we mangle packet... */
1832 hdr
->sadb_msg_errno
= 0;
1833 if (satype
!= 0 && satype
!= 1)
1835 pfk
->promisc
= satype
;
1837 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
, BROADCAST_ALL
, NULL
, sock_net(sk
));
1841 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1844 u32 reqid
= *(u32
*)ptr
;
1846 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1847 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1853 static u32
gen_reqid(struct net
*net
)
1855 struct xfrm_policy_walk walk
;
1858 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1864 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1865 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1866 rc
= xfrm_policy_walk(net
, &walk
, check_reqid
, (void*)&reqid
);
1867 xfrm_policy_walk_done(&walk
);
1870 } while (reqid
!= start
);
1875 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1877 struct net
*net
= xp_net(xp
);
1878 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1881 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1884 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1887 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1888 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1891 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1893 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1894 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1895 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1897 if (!t
->reqid
&& !(t
->reqid
= gen_reqid(net
)))
1901 /* addresses present only in tunnel mode */
1902 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1903 u8
*sa
= (u8
*) (rq
+ 1);
1904 int family
, socklen
;
1906 family
= pfkey_sockaddr_extract((struct sockaddr
*)sa
,
1911 socklen
= pfkey_sockaddr_len(family
);
1912 if (pfkey_sockaddr_extract((struct sockaddr
*)(sa
+ socklen
),
1913 &t
->id
.daddr
) != family
)
1915 t
->encap_family
= family
;
1917 t
->encap_family
= xp
->family
;
1919 /* No way to set this via kame pfkey */
1926 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1929 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1930 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1932 while (len
>= sizeof(struct sadb_x_ipsecrequest
)) {
1933 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
1935 len
-= rq
->sadb_x_ipsecrequest_len
;
1936 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
1941 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy
*xp
)
1943 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
1946 int len
= sizeof(struct sadb_x_sec_ctx
);
1947 len
+= xfrm_ctx
->ctx_len
;
1948 return PFKEY_ALIGN8(len
);
1953 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy
*xp
)
1955 struct xfrm_tmpl
*t
;
1956 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1960 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1961 t
= xp
->xfrm_vec
+ i
;
1962 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
1965 return sizeof(struct sadb_msg
) +
1966 (sizeof(struct sadb_lifetime
) * 3) +
1967 (sizeof(struct sadb_address
) * 2) +
1968 (sockaddr_size
* 2) +
1969 sizeof(struct sadb_x_policy
) +
1970 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
1972 pfkey_xfrm_policy2sec_ctx_size(xp
);
1975 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(struct xfrm_policy
*xp
)
1977 struct sk_buff
*skb
;
1980 size
= pfkey_xfrm_policy2msg_size(xp
);
1982 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
1984 return ERR_PTR(-ENOBUFS
);
1989 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, struct xfrm_policy
*xp
, int dir
)
1991 struct sadb_msg
*hdr
;
1992 struct sadb_address
*addr
;
1993 struct sadb_lifetime
*lifetime
;
1994 struct sadb_x_policy
*pol
;
1995 struct sadb_x_sec_ctx
*sec_ctx
;
1996 struct xfrm_sec_ctx
*xfrm_ctx
;
1999 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2000 int socklen
= pfkey_sockaddr_len(xp
->family
);
2002 size
= pfkey_xfrm_policy2msg_size(xp
);
2004 /* call should fill header later */
2005 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2006 memset(hdr
, 0, size
); /* XXX do we need this ? */
2009 addr
= (struct sadb_address
*) skb_put(skb
,
2010 sizeof(struct sadb_address
)+sockaddr_size
);
2011 addr
->sadb_address_len
=
2012 (sizeof(struct sadb_address
)+sockaddr_size
)/
2014 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2015 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2016 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
2017 addr
->sadb_address_reserved
= 0;
2018 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2020 (struct sockaddr
*) (addr
+ 1),
2025 addr
= (struct sadb_address
*) skb_put(skb
,
2026 sizeof(struct sadb_address
)+sockaddr_size
);
2027 addr
->sadb_address_len
=
2028 (sizeof(struct sadb_address
)+sockaddr_size
)/
2030 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2031 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2032 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2033 addr
->sadb_address_reserved
= 0;
2035 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2036 (struct sockaddr
*) (addr
+ 1),
2040 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2041 sizeof(struct sadb_lifetime
));
2042 lifetime
->sadb_lifetime_len
=
2043 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2044 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2045 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2046 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2047 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2048 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2050 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2051 sizeof(struct sadb_lifetime
));
2052 lifetime
->sadb_lifetime_len
=
2053 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2054 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2055 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2056 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2057 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2058 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2060 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2061 sizeof(struct sadb_lifetime
));
2062 lifetime
->sadb_lifetime_len
=
2063 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2064 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2065 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2066 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2067 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2068 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2070 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2071 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2072 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2073 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2074 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2076 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2078 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2080 pol
->sadb_x_policy_dir
= dir
+1;
2081 pol
->sadb_x_policy_id
= xp
->index
;
2082 pol
->sadb_x_policy_priority
= xp
->priority
;
2084 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2085 struct sadb_x_ipsecrequest
*rq
;
2086 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2090 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2091 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2092 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2093 req_size
+= socklen
* 2;
2097 rq
= (void*)skb_put(skb
, req_size
);
2098 pol
->sadb_x_policy_len
+= req_size
/8;
2099 memset(rq
, 0, sizeof(*rq
));
2100 rq
->sadb_x_ipsecrequest_len
= req_size
;
2101 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2102 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2104 rq
->sadb_x_ipsecrequest_mode
= mode
;
2105 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2107 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2109 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2110 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2112 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2113 u8
*sa
= (void *)(rq
+ 1);
2114 pfkey_sockaddr_fill(&t
->saddr
, 0,
2115 (struct sockaddr
*)sa
,
2117 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2118 (struct sockaddr
*) (sa
+ socklen
),
2123 /* security context */
2124 if ((xfrm_ctx
= xp
->security
)) {
2125 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2127 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
, ctx_size
);
2128 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2129 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2130 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2131 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2132 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2133 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2137 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2138 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
2143 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2145 struct sk_buff
*out_skb
;
2146 struct sadb_msg
*out_hdr
;
2149 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2150 if (IS_ERR(out_skb
)) {
2151 err
= PTR_ERR(out_skb
);
2154 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2158 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2159 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2161 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2162 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2164 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2165 out_hdr
->sadb_msg_errno
= 0;
2166 out_hdr
->sadb_msg_seq
= c
->seq
;
2167 out_hdr
->sadb_msg_pid
= c
->pid
;
2168 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xp_net(xp
));
2174 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2176 struct net
*net
= sock_net(sk
);
2178 struct sadb_lifetime
*lifetime
;
2179 struct sadb_address
*sa
;
2180 struct sadb_x_policy
*pol
;
2181 struct xfrm_policy
*xp
;
2183 struct sadb_x_sec_ctx
*sec_ctx
;
2185 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2186 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2187 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2190 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2191 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2193 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2196 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2200 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2201 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2202 xp
->priority
= pol
->sadb_x_policy_priority
;
2204 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2205 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2210 xp
->selector
.family
= xp
->family
;
2211 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2212 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2213 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2214 if (xp
->selector
.sport
)
2215 xp
->selector
.sport_mask
= htons(0xffff);
2217 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2218 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2219 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2221 /* Amusing, we set this twice. KAME apps appear to set same value
2222 * in both addresses.
2224 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2226 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2227 if (xp
->selector
.dport
)
2228 xp
->selector
.dport_mask
= htons(0xffff);
2230 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2231 if (sec_ctx
!= NULL
) {
2232 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2239 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
2246 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2247 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2248 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2249 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2250 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2251 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2252 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2253 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2254 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2256 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2257 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2258 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2259 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2260 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2263 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2264 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2267 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2268 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2270 xfrm_audit_policy_add(xp
, err
? 0 : 1,
2271 audit_get_loginuid(current
),
2272 audit_get_sessionid(current
), 0);
2277 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2278 c
.event
= XFRM_MSG_UPDPOLICY
;
2280 c
.event
= XFRM_MSG_NEWPOLICY
;
2282 c
.seq
= hdr
->sadb_msg_seq
;
2283 c
.pid
= hdr
->sadb_msg_pid
;
2285 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2291 xfrm_policy_destroy(xp
);
2295 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2297 struct net
*net
= sock_net(sk
);
2299 struct sadb_address
*sa
;
2300 struct sadb_x_policy
*pol
;
2301 struct xfrm_policy
*xp
;
2302 struct xfrm_selector sel
;
2304 struct sadb_x_sec_ctx
*sec_ctx
;
2305 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2307 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2308 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2309 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2312 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2313 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2316 memset(&sel
, 0, sizeof(sel
));
2318 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2319 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2320 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2321 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2322 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2324 sel
.sport_mask
= htons(0xffff);
2326 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2327 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2328 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2329 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2330 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2332 sel
.dport_mask
= htons(0xffff);
2334 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2335 if (sec_ctx
!= NULL
) {
2336 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2341 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
);
2347 xp
= xfrm_policy_bysel_ctx(net
, XFRM_POLICY_TYPE_MAIN
,
2348 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2350 security_xfrm_policy_free(pol_ctx
);
2354 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2355 audit_get_loginuid(current
),
2356 audit_get_sessionid(current
), 0);
2361 c
.seq
= hdr
->sadb_msg_seq
;
2362 c
.pid
= hdr
->sadb_msg_pid
;
2364 c
.event
= XFRM_MSG_DELPOLICY
;
2365 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2372 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, struct sadb_msg
*hdr
, int dir
)
2375 struct sk_buff
*out_skb
;
2376 struct sadb_msg
*out_hdr
;
2379 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2380 if (IS_ERR(out_skb
)) {
2381 err
= PTR_ERR(out_skb
);
2384 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2388 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2389 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2390 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2391 out_hdr
->sadb_msg_satype
= 0;
2392 out_hdr
->sadb_msg_errno
= 0;
2393 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2394 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2395 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, xp_net(xp
));
2402 #ifdef CONFIG_NET_KEY_MIGRATE
2403 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2405 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2408 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2409 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2414 if (ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2417 af
= pfkey_sockaddr_extract(sa
, saddr
);
2421 socklen
= pfkey_sockaddr_len(af
);
2422 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2430 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2431 struct xfrm_migrate
*m
)
2434 struct sadb_x_ipsecrequest
*rq2
;
2437 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2438 len
< rq1
->sadb_x_ipsecrequest_len
)
2442 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2443 rq1
->sadb_x_ipsecrequest_len
,
2444 &m
->old_saddr
, &m
->old_daddr
,
2449 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2450 len
-= rq1
->sadb_x_ipsecrequest_len
;
2452 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2453 len
< rq2
->sadb_x_ipsecrequest_len
)
2457 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2458 rq2
->sadb_x_ipsecrequest_len
,
2459 &m
->new_saddr
, &m
->new_daddr
,
2464 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2465 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2466 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2469 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2470 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2473 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2475 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2476 rq2
->sadb_x_ipsecrequest_len
));
2479 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2480 struct sadb_msg
*hdr
, void **ext_hdrs
)
2482 int i
, len
, ret
, err
= -EINVAL
;
2484 struct sadb_address
*sa
;
2485 struct sadb_x_kmaddress
*kma
;
2486 struct sadb_x_policy
*pol
;
2487 struct sadb_x_ipsecrequest
*rq
;
2488 struct xfrm_selector sel
;
2489 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2490 struct xfrm_kmaddress k
;
2492 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2493 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2494 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2499 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2500 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2502 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2508 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2509 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2510 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2511 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2512 &k
.local
, &k
.remote
, &k
.family
);
2519 dir
= pol
->sadb_x_policy_dir
- 1;
2520 memset(&sel
, 0, sizeof(sel
));
2522 /* set source address info of selector */
2523 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2524 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2525 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2526 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2527 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2529 sel
.sport_mask
= htons(0xffff);
2531 /* set destination address info of selector */
2532 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1],
2533 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2534 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2535 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2536 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2538 sel
.dport_mask
= htons(0xffff);
2540 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2542 /* extract ipsecrequests */
2544 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2546 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2547 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2552 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2558 if (!i
|| len
> 0) {
2563 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2570 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2571 struct sadb_msg
*hdr
, void **ext_hdrs
)
2573 return -ENOPROTOOPT
;
2578 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2580 struct net
*net
= sock_net(sk
);
2582 int err
= 0, delete;
2583 struct sadb_x_policy
*pol
;
2584 struct xfrm_policy
*xp
;
2587 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2590 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2591 if (dir
>= XFRM_POLICY_MAX
)
2594 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2595 xp
= xfrm_policy_byid(net
, XFRM_POLICY_TYPE_MAIN
, dir
,
2596 pol
->sadb_x_policy_id
, delete, &err
);
2601 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2602 audit_get_loginuid(current
),
2603 audit_get_sessionid(current
), 0);
2607 c
.seq
= hdr
->sadb_msg_seq
;
2608 c
.pid
= hdr
->sadb_msg_pid
;
2610 c
.event
= XFRM_MSG_DELPOLICY
;
2611 km_policy_notify(xp
, dir
, &c
);
2613 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2621 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2623 struct pfkey_sock
*pfk
= ptr
;
2624 struct sk_buff
*out_skb
;
2625 struct sadb_msg
*out_hdr
;
2628 if (!pfkey_can_dump(&pfk
->sk
))
2631 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2632 if (IS_ERR(out_skb
))
2633 return PTR_ERR(out_skb
);
2635 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2639 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2640 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2641 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2642 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2643 out_hdr
->sadb_msg_errno
= 0;
2644 out_hdr
->sadb_msg_seq
= count
+ 1;
2645 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
2648 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
2649 &pfk
->sk
, sock_net(&pfk
->sk
));
2650 pfk
->dump
.skb
= out_skb
;
2655 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2657 struct net
*net
= sock_net(&pfk
->sk
);
2658 return xfrm_policy_walk(net
, &pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2661 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2663 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
);
2666 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2668 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2670 if (pfk
->dump
.dump
!= NULL
)
2673 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2674 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
2675 pfk
->dump
.dump
= pfkey_dump_sp
;
2676 pfk
->dump
.done
= pfkey_dump_sp_done
;
2677 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2679 return pfkey_do_dump(pfk
);
2682 static int key_notify_policy_flush(struct km_event
*c
)
2684 struct sk_buff
*skb_out
;
2685 struct sadb_msg
*hdr
;
2687 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2690 hdr
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2691 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2692 hdr
->sadb_msg_seq
= c
->seq
;
2693 hdr
->sadb_msg_pid
= c
->pid
;
2694 hdr
->sadb_msg_version
= PF_KEY_V2
;
2695 hdr
->sadb_msg_errno
= (uint8_t) 0;
2696 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2697 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
2702 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2704 struct net
*net
= sock_net(sk
);
2706 struct xfrm_audit audit_info
;
2709 audit_info
.loginuid
= audit_get_loginuid(current
);
2710 audit_info
.sessionid
= audit_get_sessionid(current
);
2711 audit_info
.secid
= 0;
2712 err
= xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, &audit_info
);
2715 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2716 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2717 c
.pid
= hdr
->sadb_msg_pid
;
2718 c
.seq
= hdr
->sadb_msg_seq
;
2720 km_policy_notify(NULL
, 0, &c
);
2725 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2726 struct sadb_msg
*hdr
, void **ext_hdrs
);
2727 static pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2728 [SADB_RESERVED
] = pfkey_reserved
,
2729 [SADB_GETSPI
] = pfkey_getspi
,
2730 [SADB_UPDATE
] = pfkey_add
,
2731 [SADB_ADD
] = pfkey_add
,
2732 [SADB_DELETE
] = pfkey_delete
,
2733 [SADB_GET
] = pfkey_get
,
2734 [SADB_ACQUIRE
] = pfkey_acquire
,
2735 [SADB_REGISTER
] = pfkey_register
,
2736 [SADB_EXPIRE
] = NULL
,
2737 [SADB_FLUSH
] = pfkey_flush
,
2738 [SADB_DUMP
] = pfkey_dump
,
2739 [SADB_X_PROMISC
] = pfkey_promisc
,
2740 [SADB_X_PCHANGE
] = NULL
,
2741 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2742 [SADB_X_SPDADD
] = pfkey_spdadd
,
2743 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2744 [SADB_X_SPDGET
] = pfkey_spdget
,
2745 [SADB_X_SPDACQUIRE
] = NULL
,
2746 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2747 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2748 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2749 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2750 [SADB_X_MIGRATE
] = pfkey_migrate
,
2753 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
)
2755 void *ext_hdrs
[SADB_EXT_MAX
];
2758 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2759 BROADCAST_PROMISC_ONLY
, NULL
, sock_net(sk
));
2761 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2762 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2765 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2766 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2771 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2773 struct sadb_msg
*hdr
= NULL
;
2775 if (skb
->len
< sizeof(*hdr
)) {
2778 hdr
= (struct sadb_msg
*) skb
->data
;
2779 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2780 hdr
->sadb_msg_reserved
!= 0 ||
2781 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2782 hdr
->sadb_msg_type
> SADB_MAX
)) {
2785 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2786 sizeof(uint64_t)) ||
2787 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2788 sizeof(uint64_t))) {
2798 static inline int aalg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2800 unsigned int id
= d
->desc
.sadb_alg_id
;
2802 if (id
>= sizeof(t
->aalgos
) * 8)
2805 return (t
->aalgos
>> id
) & 1;
2808 static inline int ealg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2810 unsigned int id
= d
->desc
.sadb_alg_id
;
2812 if (id
>= sizeof(t
->ealgos
) * 8)
2815 return (t
->ealgos
>> id
) & 1;
2818 static int count_ah_combs(struct xfrm_tmpl
*t
)
2822 for (i
= 0; ; i
++) {
2823 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2826 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2827 sz
+= sizeof(struct sadb_comb
);
2829 return sz
+ sizeof(struct sadb_prop
);
2832 static int count_esp_combs(struct xfrm_tmpl
*t
)
2836 for (i
= 0; ; i
++) {
2837 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2841 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2844 for (k
= 1; ; k
++) {
2845 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2849 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2850 sz
+= sizeof(struct sadb_comb
);
2853 return sz
+ sizeof(struct sadb_prop
);
2856 static void dump_ah_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2858 struct sadb_prop
*p
;
2861 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2862 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2863 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2864 p
->sadb_prop_replay
= 32;
2865 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2867 for (i
= 0; ; i
++) {
2868 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2872 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2873 struct sadb_comb
*c
;
2874 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2875 memset(c
, 0, sizeof(*c
));
2876 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2877 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2878 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2879 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2880 c
->sadb_comb_hard_addtime
= 24*60*60;
2881 c
->sadb_comb_soft_addtime
= 20*60*60;
2882 c
->sadb_comb_hard_usetime
= 8*60*60;
2883 c
->sadb_comb_soft_usetime
= 7*60*60;
2888 static void dump_esp_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2890 struct sadb_prop
*p
;
2893 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2894 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2895 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2896 p
->sadb_prop_replay
= 32;
2897 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2900 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2904 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2907 for (k
= 1; ; k
++) {
2908 struct sadb_comb
*c
;
2909 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2912 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2914 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2915 memset(c
, 0, sizeof(*c
));
2916 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2917 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2918 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2919 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2920 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
2921 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
2922 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
2923 c
->sadb_comb_hard_addtime
= 24*60*60;
2924 c
->sadb_comb_soft_addtime
= 20*60*60;
2925 c
->sadb_comb_hard_usetime
= 8*60*60;
2926 c
->sadb_comb_soft_usetime
= 7*60*60;
2931 static int key_notify_policy_expire(struct xfrm_policy
*xp
, struct km_event
*c
)
2936 static int key_notify_sa_expire(struct xfrm_state
*x
, struct km_event
*c
)
2938 struct sk_buff
*out_skb
;
2939 struct sadb_msg
*out_hdr
;
2943 hard
= c
->data
.hard
;
2949 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
2950 if (IS_ERR(out_skb
))
2951 return PTR_ERR(out_skb
);
2953 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2954 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2955 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
2956 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2957 out_hdr
->sadb_msg_errno
= 0;
2958 out_hdr
->sadb_msg_reserved
= 0;
2959 out_hdr
->sadb_msg_seq
= 0;
2960 out_hdr
->sadb_msg_pid
= 0;
2962 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
2966 static int pfkey_send_notify(struct xfrm_state
*x
, struct km_event
*c
)
2968 struct net
*net
= x
? xs_net(x
) : c
->net
;
2969 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
2971 if (atomic_read(&net_pfkey
->socks_nr
) == 0)
2975 case XFRM_MSG_EXPIRE
:
2976 return key_notify_sa_expire(x
, c
);
2977 case XFRM_MSG_DELSA
:
2978 case XFRM_MSG_NEWSA
:
2979 case XFRM_MSG_UPDSA
:
2980 return key_notify_sa(x
, c
);
2981 case XFRM_MSG_FLUSHSA
:
2982 return key_notify_sa_flush(c
);
2983 case XFRM_MSG_NEWAE
: /* not yet supported */
2986 printk("pfkey: Unknown SA event %d\n", c
->event
);
2993 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2995 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
2999 case XFRM_MSG_POLEXPIRE
:
3000 return key_notify_policy_expire(xp
, c
);
3001 case XFRM_MSG_DELPOLICY
:
3002 case XFRM_MSG_NEWPOLICY
:
3003 case XFRM_MSG_UPDPOLICY
:
3004 return key_notify_policy(xp
, dir
, c
);
3005 case XFRM_MSG_FLUSHPOLICY
:
3006 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
3008 return key_notify_policy_flush(c
);
3010 printk("pfkey: Unknown policy event %d\n", c
->event
);
3017 static u32
get_acqseq(void)
3021 static DEFINE_SPINLOCK(acqseq_lock
);
3023 spin_lock_bh(&acqseq_lock
);
3024 res
= (++acqseq
? : ++acqseq
);
3025 spin_unlock_bh(&acqseq_lock
);
3029 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
, int dir
)
3031 struct sk_buff
*skb
;
3032 struct sadb_msg
*hdr
;
3033 struct sadb_address
*addr
;
3034 struct sadb_x_policy
*pol
;
3037 struct sadb_x_sec_ctx
*sec_ctx
;
3038 struct xfrm_sec_ctx
*xfrm_ctx
;
3041 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3045 size
= sizeof(struct sadb_msg
) +
3046 (sizeof(struct sadb_address
) * 2) +
3047 (sockaddr_size
* 2) +
3048 sizeof(struct sadb_x_policy
);
3050 if (x
->id
.proto
== IPPROTO_AH
)
3051 size
+= count_ah_combs(t
);
3052 else if (x
->id
.proto
== IPPROTO_ESP
)
3053 size
+= count_esp_combs(t
);
3055 if ((xfrm_ctx
= x
->security
)) {
3056 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3057 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3060 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3064 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3065 hdr
->sadb_msg_version
= PF_KEY_V2
;
3066 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3067 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3068 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3069 hdr
->sadb_msg_errno
= 0;
3070 hdr
->sadb_msg_reserved
= 0;
3071 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3072 hdr
->sadb_msg_pid
= 0;
3075 addr
= (struct sadb_address
*) skb_put(skb
,
3076 sizeof(struct sadb_address
)+sockaddr_size
);
3077 addr
->sadb_address_len
=
3078 (sizeof(struct sadb_address
)+sockaddr_size
)/
3080 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3081 addr
->sadb_address_proto
= 0;
3082 addr
->sadb_address_reserved
= 0;
3083 addr
->sadb_address_prefixlen
=
3084 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3085 (struct sockaddr
*) (addr
+ 1),
3087 if (!addr
->sadb_address_prefixlen
)
3091 addr
= (struct sadb_address
*) skb_put(skb
,
3092 sizeof(struct sadb_address
)+sockaddr_size
);
3093 addr
->sadb_address_len
=
3094 (sizeof(struct sadb_address
)+sockaddr_size
)/
3096 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3097 addr
->sadb_address_proto
= 0;
3098 addr
->sadb_address_reserved
= 0;
3099 addr
->sadb_address_prefixlen
=
3100 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3101 (struct sockaddr
*) (addr
+ 1),
3103 if (!addr
->sadb_address_prefixlen
)
3106 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
3107 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3108 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3109 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3110 pol
->sadb_x_policy_dir
= dir
+1;
3111 pol
->sadb_x_policy_id
= xp
->index
;
3113 /* Set sadb_comb's. */
3114 if (x
->id
.proto
== IPPROTO_AH
)
3115 dump_ah_combs(skb
, t
);
3116 else if (x
->id
.proto
== IPPROTO_ESP
)
3117 dump_esp_combs(skb
, t
);
3119 /* security context */
3121 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
3122 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3123 sec_ctx
->sadb_x_sec_len
=
3124 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3125 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3126 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3127 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3128 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3129 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3133 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
3136 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3137 u8
*data
, int len
, int *dir
)
3139 struct net
*net
= sock_net(sk
);
3140 struct xfrm_policy
*xp
;
3141 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3142 struct sadb_x_sec_ctx
*sec_ctx
;
3144 switch (sk
->sk_family
) {
3146 if (opt
!= IP_IPSEC_POLICY
) {
3151 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3153 if (opt
!= IPV6_IPSEC_POLICY
) {
3166 if (len
< sizeof(struct sadb_x_policy
) ||
3167 pol
->sadb_x_policy_len
*8 > len
||
3168 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3169 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3172 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
3178 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3179 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3181 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3182 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3183 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3184 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3185 xp
->family
= sk
->sk_family
;
3188 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3189 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3192 /* security context too */
3193 if (len
>= (pol
->sadb_x_policy_len
*8 +
3194 sizeof(struct sadb_x_sec_ctx
))) {
3195 char *p
= (char *)pol
;
3196 struct xfrm_user_sec_ctx
*uctx
;
3198 p
+= pol
->sadb_x_policy_len
*8;
3199 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3200 if (len
< pol
->sadb_x_policy_len
*8 +
3201 sec_ctx
->sadb_x_sec_len
) {
3205 if ((*dir
= verify_sec_ctx_len(p
)))
3207 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
3208 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
3215 *dir
= pol
->sadb_x_policy_dir
-1;
3220 xfrm_policy_destroy(xp
);
3224 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3226 struct sk_buff
*skb
;
3227 struct sadb_msg
*hdr
;
3229 struct sadb_address
*addr
;
3230 struct sadb_x_nat_t_port
*n_port
;
3233 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3234 struct xfrm_encap_tmpl
*natt
= NULL
;
3236 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3248 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3250 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3251 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3254 size
= sizeof(struct sadb_msg
) +
3255 sizeof(struct sadb_sa
) +
3256 (sizeof(struct sadb_address
) * 2) +
3257 (sockaddr_size
* 2) +
3258 (sizeof(struct sadb_x_nat_t_port
) * 2);
3260 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3264 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3265 hdr
->sadb_msg_version
= PF_KEY_V2
;
3266 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3267 hdr
->sadb_msg_satype
= satype
;
3268 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3269 hdr
->sadb_msg_errno
= 0;
3270 hdr
->sadb_msg_reserved
= 0;
3271 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3272 hdr
->sadb_msg_pid
= 0;
3275 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
3276 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3277 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3278 sa
->sadb_sa_spi
= x
->id
.spi
;
3279 sa
->sadb_sa_replay
= 0;
3280 sa
->sadb_sa_state
= 0;
3281 sa
->sadb_sa_auth
= 0;
3282 sa
->sadb_sa_encrypt
= 0;
3283 sa
->sadb_sa_flags
= 0;
3285 /* ADDRESS_SRC (old addr) */
3286 addr
= (struct sadb_address
*)
3287 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3288 addr
->sadb_address_len
=
3289 (sizeof(struct sadb_address
)+sockaddr_size
)/
3291 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3292 addr
->sadb_address_proto
= 0;
3293 addr
->sadb_address_reserved
= 0;
3294 addr
->sadb_address_prefixlen
=
3295 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3296 (struct sockaddr
*) (addr
+ 1),
3298 if (!addr
->sadb_address_prefixlen
)
3301 /* NAT_T_SPORT (old port) */
3302 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3303 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3304 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3305 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3306 n_port
->sadb_x_nat_t_port_reserved
= 0;
3308 /* ADDRESS_DST (new addr) */
3309 addr
= (struct sadb_address
*)
3310 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3311 addr
->sadb_address_len
=
3312 (sizeof(struct sadb_address
)+sockaddr_size
)/
3314 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3315 addr
->sadb_address_proto
= 0;
3316 addr
->sadb_address_reserved
= 0;
3317 addr
->sadb_address_prefixlen
=
3318 pfkey_sockaddr_fill(ipaddr
, 0,
3319 (struct sockaddr
*) (addr
+ 1),
3321 if (!addr
->sadb_address_prefixlen
)
3324 /* NAT_T_DPORT (new port) */
3325 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3326 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3327 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3328 n_port
->sadb_x_nat_t_port_port
= sport
;
3329 n_port
->sadb_x_nat_t_port_reserved
= 0;
3331 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
3334 #ifdef CONFIG_NET_KEY_MIGRATE
3335 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3336 struct xfrm_selector
*sel
)
3338 struct sadb_address
*addr
;
3339 addr
= (struct sadb_address
*)skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3340 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3341 addr
->sadb_address_exttype
= type
;
3342 addr
->sadb_address_proto
= sel
->proto
;
3343 addr
->sadb_address_reserved
= 0;
3346 case SADB_EXT_ADDRESS_SRC
:
3347 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3348 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3349 (struct sockaddr
*)(addr
+ 1),
3352 case SADB_EXT_ADDRESS_DST
:
3353 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3354 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3355 (struct sockaddr
*)(addr
+ 1),
3366 static int set_sadb_kmaddress(struct sk_buff
*skb
, struct xfrm_kmaddress
*k
)
3368 struct sadb_x_kmaddress
*kma
;
3370 int family
= k
->family
;
3371 int socklen
= pfkey_sockaddr_len(family
);
3374 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3375 pfkey_sockaddr_pair_size(family
));
3377 kma
= (struct sadb_x_kmaddress
*)skb_put(skb
, size_req
);
3378 memset(kma
, 0, size_req
);
3379 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3380 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3381 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3383 sa
= (u8
*)(kma
+ 1);
3384 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3385 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3391 static int set_ipsecrequest(struct sk_buff
*skb
,
3392 uint8_t proto
, uint8_t mode
, int level
,
3393 uint32_t reqid
, uint8_t family
,
3394 xfrm_address_t
*src
, xfrm_address_t
*dst
)
3396 struct sadb_x_ipsecrequest
*rq
;
3398 int socklen
= pfkey_sockaddr_len(family
);
3401 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3402 pfkey_sockaddr_pair_size(family
);
3404 rq
= (struct sadb_x_ipsecrequest
*)skb_put(skb
, size_req
);
3405 memset(rq
, 0, size_req
);
3406 rq
->sadb_x_ipsecrequest_len
= size_req
;
3407 rq
->sadb_x_ipsecrequest_proto
= proto
;
3408 rq
->sadb_x_ipsecrequest_mode
= mode
;
3409 rq
->sadb_x_ipsecrequest_level
= level
;
3410 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3412 sa
= (u8
*) (rq
+ 1);
3413 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3414 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3421 #ifdef CONFIG_NET_KEY_MIGRATE
3422 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3423 struct xfrm_migrate
*m
, int num_bundles
,
3424 struct xfrm_kmaddress
*k
)
3430 struct sk_buff
*skb
;
3431 struct sadb_msg
*hdr
;
3432 struct sadb_x_policy
*pol
;
3433 struct xfrm_migrate
*mp
;
3435 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3438 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3442 /* addresses for KM */
3443 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3444 pfkey_sockaddr_pair_size(k
->family
));
3448 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3451 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3454 size_pol
+= sizeof(struct sadb_x_policy
);
3457 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3458 /* old locator pair */
3459 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3460 pfkey_sockaddr_pair_size(mp
->old_family
);
3461 /* new locator pair */
3462 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3463 pfkey_sockaddr_pair_size(mp
->new_family
);
3466 size
+= sizeof(struct sadb_msg
) + size_pol
;
3469 skb
= alloc_skb(size
, GFP_ATOMIC
);
3473 hdr
= (struct sadb_msg
*)skb_put(skb
, sizeof(struct sadb_msg
));
3474 hdr
->sadb_msg_version
= PF_KEY_V2
;
3475 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3476 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3477 hdr
->sadb_msg_len
= size
/ 8;
3478 hdr
->sadb_msg_errno
= 0;
3479 hdr
->sadb_msg_reserved
= 0;
3480 hdr
->sadb_msg_seq
= 0;
3481 hdr
->sadb_msg_pid
= 0;
3483 /* Addresses to be used by KM for negotiation, if ext is available */
3484 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3488 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3491 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3493 /* policy information */
3494 pol
= (struct sadb_x_policy
*)skb_put(skb
, sizeof(struct sadb_x_policy
));
3495 pol
->sadb_x_policy_len
= size_pol
/ 8;
3496 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3497 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3498 pol
->sadb_x_policy_dir
= dir
+ 1;
3499 pol
->sadb_x_policy_id
= 0;
3500 pol
->sadb_x_policy_priority
= 0;
3502 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3503 /* old ipsecrequest */
3504 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3507 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3508 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3509 mp
->reqid
, mp
->old_family
,
3510 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3513 /* new ipsecrequest */
3514 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3515 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3516 mp
->reqid
, mp
->new_family
,
3517 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3521 /* broadcast migrate message to sockets */
3522 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, &init_net
);
3531 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3532 struct xfrm_migrate
*m
, int num_bundles
,
3533 struct xfrm_kmaddress
*k
)
3535 return -ENOPROTOOPT
;
3539 static int pfkey_sendmsg(struct kiocb
*kiocb
,
3540 struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3542 struct sock
*sk
= sock
->sk
;
3543 struct sk_buff
*skb
= NULL
;
3544 struct sadb_msg
*hdr
= NULL
;
3548 if (msg
->msg_flags
& MSG_OOB
)
3552 if ((unsigned)len
> sk
->sk_sndbuf
- 32)
3556 skb
= alloc_skb(len
, GFP_KERNEL
);
3561 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
))
3564 hdr
= pfkey_get_base_msg(skb
, &err
);
3568 mutex_lock(&xfrm_cfg_mutex
);
3569 err
= pfkey_process(sk
, skb
, hdr
);
3570 mutex_unlock(&xfrm_cfg_mutex
);
3573 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3580 static int pfkey_recvmsg(struct kiocb
*kiocb
,
3581 struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3584 struct sock
*sk
= sock
->sk
;
3585 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3586 struct sk_buff
*skb
;
3590 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3593 msg
->msg_namelen
= 0;
3594 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3600 msg
->msg_flags
|= MSG_TRUNC
;
3604 skb_reset_transport_header(skb
);
3605 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
3609 sock_recv_timestamp(msg
, sk
, skb
);
3611 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3613 if (pfk
->dump
.dump
!= NULL
&&
3614 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3618 skb_free_datagram(sk
, skb
);
3623 static const struct proto_ops pfkey_ops
= {
3625 .owner
= THIS_MODULE
,
3626 /* Operations that make no sense on pfkey sockets. */
3627 .bind
= sock_no_bind
,
3628 .connect
= sock_no_connect
,
3629 .socketpair
= sock_no_socketpair
,
3630 .accept
= sock_no_accept
,
3631 .getname
= sock_no_getname
,
3632 .ioctl
= sock_no_ioctl
,
3633 .listen
= sock_no_listen
,
3634 .shutdown
= sock_no_shutdown
,
3635 .setsockopt
= sock_no_setsockopt
,
3636 .getsockopt
= sock_no_getsockopt
,
3637 .mmap
= sock_no_mmap
,
3638 .sendpage
= sock_no_sendpage
,
3640 /* Now the operations that really occur. */
3641 .release
= pfkey_release
,
3642 .poll
= datagram_poll
,
3643 .sendmsg
= pfkey_sendmsg
,
3644 .recvmsg
= pfkey_recvmsg
,
3647 static struct net_proto_family pfkey_family_ops
= {
3649 .create
= pfkey_create
,
3650 .owner
= THIS_MODULE
,
3653 #ifdef CONFIG_PROC_FS
3654 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3658 s
= (struct sock
*)v
;
3659 if (v
== SEQ_START_TOKEN
)
3660 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3662 seq_printf(f
,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3664 atomic_read(&s
->sk_refcnt
),
3665 sk_rmem_alloc_get(s
),
3666 sk_wmem_alloc_get(s
),
3673 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3675 struct net
*net
= seq_file_net(f
);
3676 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3678 struct hlist_node
*node
;
3681 read_lock(&pfkey_table_lock
);
3683 return SEQ_START_TOKEN
;
3685 sk_for_each(s
, node
, &net_pfkey
->table
)
3692 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3694 struct net
*net
= seq_file_net(f
);
3695 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3698 return (v
== SEQ_START_TOKEN
) ?
3699 sk_head(&net_pfkey
->table
) :
3700 sk_next((struct sock
*)v
);
3703 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3705 read_unlock(&pfkey_table_lock
);
3708 static const struct seq_operations pfkey_seq_ops
= {
3709 .start
= pfkey_seq_start
,
3710 .next
= pfkey_seq_next
,
3711 .stop
= pfkey_seq_stop
,
3712 .show
= pfkey_seq_show
,
3715 static int pfkey_seq_open(struct inode
*inode
, struct file
*file
)
3717 return seq_open_net(inode
, file
, &pfkey_seq_ops
,
3718 sizeof(struct seq_net_private
));
3721 static const struct file_operations pfkey_proc_ops
= {
3722 .open
= pfkey_seq_open
,
3724 .llseek
= seq_lseek
,
3725 .release
= seq_release_net
,
3728 static int __net_init
pfkey_init_proc(struct net
*net
)
3730 struct proc_dir_entry
*e
;
3732 e
= proc_net_fops_create(net
, "pfkey", 0, &pfkey_proc_ops
);
3739 static void pfkey_exit_proc(struct net
*net
)
3741 proc_net_remove(net
, "pfkey");
3744 static int __net_init
pfkey_init_proc(struct net
*net
)
3749 static void pfkey_exit_proc(struct net
*net
)
3754 static struct xfrm_mgr pfkeyv2_mgr
=
3757 .notify
= pfkey_send_notify
,
3758 .acquire
= pfkey_send_acquire
,
3759 .compile_policy
= pfkey_compile_policy
,
3760 .new_mapping
= pfkey_send_new_mapping
,
3761 .notify_policy
= pfkey_send_policy_notify
,
3762 .migrate
= pfkey_send_migrate
,
3765 static int __net_init
pfkey_net_init(struct net
*net
)
3767 struct netns_pfkey
*net_pfkey
;
3770 net_pfkey
= kmalloc(sizeof(struct netns_pfkey
), GFP_KERNEL
);
3775 INIT_HLIST_HEAD(&net_pfkey
->table
);
3776 atomic_set(&net_pfkey
->socks_nr
, 0);
3777 rv
= net_assign_generic(net
, pfkey_net_id
, net_pfkey
);
3780 rv
= pfkey_init_proc(net
);
3792 static void __net_exit
pfkey_net_exit(struct net
*net
)
3794 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3796 pfkey_exit_proc(net
);
3797 BUG_ON(!hlist_empty(&net_pfkey
->table
));
3801 static struct pernet_operations pfkey_net_ops
= {
3802 .init
= pfkey_net_init
,
3803 .exit
= pfkey_net_exit
,
3806 static void __exit
ipsec_pfkey_exit(void)
3808 unregister_pernet_gen_subsys(pfkey_net_id
, &pfkey_net_ops
);
3809 xfrm_unregister_km(&pfkeyv2_mgr
);
3810 sock_unregister(PF_KEY
);
3811 proto_unregister(&key_proto
);
3814 static int __init
ipsec_pfkey_init(void)
3816 int err
= proto_register(&key_proto
, 0);
3821 err
= sock_register(&pfkey_family_ops
);
3823 goto out_unregister_key_proto
;
3824 err
= xfrm_register_km(&pfkeyv2_mgr
);
3826 goto out_sock_unregister
;
3827 err
= register_pernet_gen_subsys(&pfkey_net_id
, &pfkey_net_ops
);
3829 goto out_xfrm_unregister_km
;
3832 out_xfrm_unregister_km
:
3833 xfrm_unregister_km(&pfkeyv2_mgr
);
3834 out_sock_unregister
:
3835 sock_unregister(PF_KEY
);
3836 out_unregister_key_proto
:
3837 proto_unregister(&key_proto
);
3841 module_init(ipsec_pfkey_init
);
3842 module_exit(ipsec_pfkey_exit
);
3843 MODULE_LICENSE("GPL");
3844 MODULE_ALIAS_NETPROTO(PF_KEY
);