2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/capability.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <net/net_namespace.h>
34 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
35 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 /* List of all pfkey sockets. */
39 static HLIST_HEAD(pfkey_table
);
40 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait
);
41 static DEFINE_RWLOCK(pfkey_table_lock
);
42 static atomic_t pfkey_table_users
= ATOMIC_INIT(0);
44 static atomic_t pfkey_socks_nr
= ATOMIC_INIT(0);
47 /* struct sock must be the first member of struct pfkey_sock */
55 int (*dump
)(struct pfkey_sock
*sk
);
56 void (*done
)(struct pfkey_sock
*sk
);
58 struct xfrm_policy_walk policy
;
59 struct xfrm_state_walk state
;
65 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
67 return (struct pfkey_sock
*)sk
;
70 static int pfkey_can_dump(struct sock
*sk
)
72 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
77 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
81 kfree_skb(pfk
->dump
.skb
);
85 pfk
->dump
.dump
= NULL
;
86 pfk
->dump
.done
= NULL
;
90 static void pfkey_sock_destruct(struct sock
*sk
)
92 pfkey_terminate_dump(pfkey_sk(sk
));
93 skb_queue_purge(&sk
->sk_receive_queue
);
95 if (!sock_flag(sk
, SOCK_DEAD
)) {
96 printk("Attempt to release alive pfkey socket: %p\n", sk
);
100 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
101 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
103 atomic_dec(&pfkey_socks_nr
);
106 static void pfkey_table_grab(void)
108 write_lock_bh(&pfkey_table_lock
);
110 if (atomic_read(&pfkey_table_users
)) {
111 DECLARE_WAITQUEUE(wait
, current
);
113 add_wait_queue_exclusive(&pfkey_table_wait
, &wait
);
115 set_current_state(TASK_UNINTERRUPTIBLE
);
116 if (atomic_read(&pfkey_table_users
) == 0)
118 write_unlock_bh(&pfkey_table_lock
);
120 write_lock_bh(&pfkey_table_lock
);
123 __set_current_state(TASK_RUNNING
);
124 remove_wait_queue(&pfkey_table_wait
, &wait
);
128 static __inline__
void pfkey_table_ungrab(void)
130 write_unlock_bh(&pfkey_table_lock
);
131 wake_up(&pfkey_table_wait
);
134 static __inline__
void pfkey_lock_table(void)
136 /* read_lock() synchronizes us to pfkey_table_grab */
138 read_lock(&pfkey_table_lock
);
139 atomic_inc(&pfkey_table_users
);
140 read_unlock(&pfkey_table_lock
);
143 static __inline__
void pfkey_unlock_table(void)
145 if (atomic_dec_and_test(&pfkey_table_users
))
146 wake_up(&pfkey_table_wait
);
150 static const struct proto_ops pfkey_ops
;
152 static void pfkey_insert(struct sock
*sk
)
155 sk_add_node(sk
, &pfkey_table
);
156 pfkey_table_ungrab();
159 static void pfkey_remove(struct sock
*sk
)
162 sk_del_node_init(sk
);
163 pfkey_table_ungrab();
166 static struct proto key_proto
= {
168 .owner
= THIS_MODULE
,
169 .obj_size
= sizeof(struct pfkey_sock
),
172 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
)
177 if (net
!= &init_net
)
178 return -EAFNOSUPPORT
;
180 if (!capable(CAP_NET_ADMIN
))
182 if (sock
->type
!= SOCK_RAW
)
183 return -ESOCKTNOSUPPORT
;
184 if (protocol
!= PF_KEY_V2
)
185 return -EPROTONOSUPPORT
;
188 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
);
192 sock
->ops
= &pfkey_ops
;
193 sock_init_data(sock
, sk
);
195 sk
->sk_family
= PF_KEY
;
196 sk
->sk_destruct
= pfkey_sock_destruct
;
198 atomic_inc(&pfkey_socks_nr
);
207 static int pfkey_release(struct socket
*sock
)
209 struct sock
*sk
= sock
->sk
;
218 skb_queue_purge(&sk
->sk_write_queue
);
224 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
225 gfp_t allocation
, struct sock
*sk
)
231 if (atomic_read(&skb
->users
) != 1) {
232 *skb2
= skb_clone(skb
, allocation
);
235 atomic_inc(&skb
->users
);
239 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
241 skb_set_owner_r(*skb2
, sk
);
242 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
243 sk
->sk_data_ready(sk
, (*skb2
)->len
);
252 /* Send SKB to all pfkey sockets matching selected criteria. */
253 #define BROADCAST_ALL 0
254 #define BROADCAST_ONE 1
255 #define BROADCAST_REGISTERED 2
256 #define BROADCAST_PROMISC_ONLY 4
257 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
258 int broadcast_flags
, struct sock
*one_sk
)
261 struct hlist_node
*node
;
262 struct sk_buff
*skb2
= NULL
;
265 /* XXX Do we need something like netlink_overrun? I think
266 * XXX PF_KEY socket apps will not mind current behavior.
272 sk_for_each(sk
, node
, &pfkey_table
) {
273 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
276 /* Yes, it means that if you are meant to receive this
277 * pfkey message you receive it twice as promiscuous
281 pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
283 /* the exact target will be processed later */
286 if (broadcast_flags
!= BROADCAST_ALL
) {
287 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
289 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
292 if (broadcast_flags
& BROADCAST_ONE
)
296 err2
= pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
298 /* Error is cleare after succecful sending to at least one
300 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
303 pfkey_unlock_table();
306 err
= pfkey_broadcast_one(skb
, &skb2
, allocation
, one_sk
);
314 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
316 struct sadb_msg
*hdr
;
319 rc
= pfk
->dump
.dump(pfk
);
324 if (!pfkey_can_dump(&pfk
->sk
))
327 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
328 hdr
->sadb_msg_seq
= 0;
329 hdr
->sadb_msg_errno
= rc
;
330 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
332 pfk
->dump
.skb
= NULL
;
335 pfkey_terminate_dump(pfk
);
339 static inline void pfkey_hdr_dup(struct sadb_msg
*new, struct sadb_msg
*orig
)
344 static int pfkey_error(struct sadb_msg
*orig
, int err
, struct sock
*sk
)
346 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
347 struct sadb_msg
*hdr
;
352 /* Woe be to the platform trying to support PFKEY yet
353 * having normal errnos outside the 1-255 range, inclusive.
356 if (err
== ERESTARTSYS
||
357 err
== ERESTARTNOHAND
||
358 err
== ERESTARTNOINTR
)
362 BUG_ON(err
<= 0 || err
>= 256);
364 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
365 pfkey_hdr_dup(hdr
, orig
);
366 hdr
->sadb_msg_errno
= (uint8_t) err
;
367 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
370 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
375 static u8 sadb_ext_min_len
[] = {
376 [SADB_EXT_RESERVED
] = (u8
) 0,
377 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
378 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
379 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
380 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
381 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
382 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
383 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
384 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
385 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
386 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
387 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
388 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
389 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
390 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
391 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
392 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
393 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
394 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
395 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
396 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
397 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
398 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
399 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
400 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
401 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
404 /* Verify sadb_address_{len,prefixlen} against sa_family. */
405 static int verify_address_len(void *p
)
407 struct sadb_address
*sp
= p
;
408 struct sockaddr
*addr
= (struct sockaddr
*)(sp
+ 1);
409 struct sockaddr_in
*sin
;
410 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
411 struct sockaddr_in6
*sin6
;
415 switch (addr
->sa_family
) {
417 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
418 if (sp
->sadb_address_len
!= len
||
419 sp
->sadb_address_prefixlen
> 32)
422 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
424 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
425 if (sp
->sadb_address_len
!= len
||
426 sp
->sadb_address_prefixlen
> 128)
431 /* It is user using kernel to keep track of security
432 * associations for another protocol, such as
433 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
436 * XXX Actually, association/policy database is not yet
437 * XXX able to cope with arbitrary sockaddr families.
438 * XXX When it can, remove this -EINVAL. -DaveM
447 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx
*sec_ctx
)
449 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
450 sec_ctx
->sadb_x_ctx_len
,
454 static inline int verify_sec_ctx_len(void *p
)
456 struct sadb_x_sec_ctx
*sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
457 int len
= sec_ctx
->sadb_x_ctx_len
;
462 len
= pfkey_sec_ctx_len(sec_ctx
);
464 if (sec_ctx
->sadb_x_sec_len
!= len
)
470 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx
*sec_ctx
)
472 struct xfrm_user_sec_ctx
*uctx
= NULL
;
473 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
475 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), GFP_KERNEL
);
480 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
481 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
482 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
483 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
484 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
485 memcpy(uctx
+ 1, sec_ctx
+ 1,
491 static int present_and_same_family(struct sadb_address
*src
,
492 struct sadb_address
*dst
)
494 struct sockaddr
*s_addr
, *d_addr
;
499 s_addr
= (struct sockaddr
*)(src
+ 1);
500 d_addr
= (struct sockaddr
*)(dst
+ 1);
501 if (s_addr
->sa_family
!= d_addr
->sa_family
)
503 if (s_addr
->sa_family
!= AF_INET
504 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
505 && s_addr
->sa_family
!= AF_INET6
513 static int parse_exthdrs(struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
515 char *p
= (char *) hdr
;
521 struct sadb_ext
*ehdr
= (struct sadb_ext
*) p
;
525 ext_len
= ehdr
->sadb_ext_len
;
526 ext_len
*= sizeof(uint64_t);
527 ext_type
= ehdr
->sadb_ext_type
;
528 if (ext_len
< sizeof(uint64_t) ||
530 ext_type
== SADB_EXT_RESERVED
)
533 if (ext_type
<= SADB_EXT_MAX
) {
534 int min
= (int) sadb_ext_min_len
[ext_type
];
537 if (ext_hdrs
[ext_type
-1] != NULL
)
539 if (ext_type
== SADB_EXT_ADDRESS_SRC
||
540 ext_type
== SADB_EXT_ADDRESS_DST
||
541 ext_type
== SADB_EXT_ADDRESS_PROXY
||
542 ext_type
== SADB_X_EXT_NAT_T_OA
) {
543 if (verify_address_len(p
))
546 if (ext_type
== SADB_X_EXT_SEC_CTX
) {
547 if (verify_sec_ctx_len(p
))
550 ext_hdrs
[ext_type
-1] = p
;
560 pfkey_satype2proto(uint8_t satype
)
563 case SADB_SATYPE_UNSPEC
:
564 return IPSEC_PROTO_ANY
;
567 case SADB_SATYPE_ESP
:
569 case SADB_X_SATYPE_IPCOMP
:
579 pfkey_proto2satype(uint16_t proto
)
583 return SADB_SATYPE_AH
;
585 return SADB_SATYPE_ESP
;
587 return SADB_X_SATYPE_IPCOMP
;
595 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
596 * say specifically 'just raw sockets' as we encode them as 255.
599 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
601 return (proto
== IPSEC_PROTO_ANY
? 0 : proto
);
604 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
606 return (proto
? proto
: IPSEC_PROTO_ANY
);
609 static inline int pfkey_sockaddr_len(sa_family_t family
)
613 return sizeof(struct sockaddr_in
);
614 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
616 return sizeof(struct sockaddr_in6
);
623 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
625 switch (sa
->sa_family
) {
628 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
630 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
633 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
634 sizeof(struct in6_addr
));
642 int pfkey_sadb_addr2xfrm_addr(struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
644 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
648 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct sadb_msg
*hdr
, void **ext_hdrs
)
651 struct sadb_address
*addr
;
653 unsigned short family
;
654 xfrm_address_t
*xaddr
;
656 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
660 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
664 /* sadb_address_len should be checked by caller */
665 addr
= (struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
669 family
= ((struct sockaddr
*)(addr
+ 1))->sa_family
;
672 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
674 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
676 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
686 return xfrm_state_lookup(xaddr
, sa
->sadb_sa_spi
, proto
, family
);
689 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
692 pfkey_sockaddr_size(sa_family_t family
)
694 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
697 static inline int pfkey_mode_from_xfrm(int mode
)
700 case XFRM_MODE_TRANSPORT
:
701 return IPSEC_MODE_TRANSPORT
;
702 case XFRM_MODE_TUNNEL
:
703 return IPSEC_MODE_TUNNEL
;
705 return IPSEC_MODE_BEET
;
711 static inline int pfkey_mode_to_xfrm(int mode
)
714 case IPSEC_MODE_ANY
: /*XXX*/
715 case IPSEC_MODE_TRANSPORT
:
716 return XFRM_MODE_TRANSPORT
;
717 case IPSEC_MODE_TUNNEL
:
718 return XFRM_MODE_TUNNEL
;
719 case IPSEC_MODE_BEET
:
720 return XFRM_MODE_BEET
;
726 static unsigned int pfkey_sockaddr_fill(xfrm_address_t
*xaddr
, __be16 port
,
728 unsigned short family
)
733 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
734 sin
->sin_family
= AF_INET
;
735 sin
->sin_port
= port
;
736 sin
->sin_addr
.s_addr
= xaddr
->a4
;
737 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
740 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
743 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
744 sin6
->sin6_family
= AF_INET6
;
745 sin6
->sin6_port
= port
;
746 sin6
->sin6_flowinfo
= 0;
747 ipv6_addr_copy(&sin6
->sin6_addr
, (struct in6_addr
*)xaddr
->a6
);
748 sin6
->sin6_scope_id
= 0;
756 static struct sk_buff
*__pfkey_xfrm_state2msg(struct xfrm_state
*x
,
757 int add_keys
, int hsc
)
760 struct sadb_msg
*hdr
;
762 struct sadb_lifetime
*lifetime
;
763 struct sadb_address
*addr
;
764 struct sadb_key
*key
;
765 struct sadb_x_sa2
*sa2
;
766 struct sadb_x_sec_ctx
*sec_ctx
;
767 struct xfrm_sec_ctx
*xfrm_ctx
;
770 int auth_key_size
= 0;
771 int encrypt_key_size
= 0;
773 struct xfrm_encap_tmpl
*natt
= NULL
;
776 /* address family check */
777 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
779 return ERR_PTR(-EINVAL
);
781 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
782 key(AE), (identity(SD),) (sensitivity)> */
783 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
784 sizeof(struct sadb_lifetime
) +
785 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
786 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
787 sizeof(struct sadb_address
)*2 +
789 sizeof(struct sadb_x_sa2
);
791 if ((xfrm_ctx
= x
->security
)) {
792 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
793 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
796 /* identity & sensitivity */
797 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
798 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
801 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
803 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
804 size
+= sizeof(struct sadb_key
) + auth_key_size
;
806 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
808 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
809 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
815 if (natt
&& natt
->encap_type
) {
816 size
+= sizeof(struct sadb_x_nat_t_type
);
817 size
+= sizeof(struct sadb_x_nat_t_port
);
818 size
+= sizeof(struct sadb_x_nat_t_port
);
821 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
823 return ERR_PTR(-ENOBUFS
);
825 /* call should fill header later */
826 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
827 memset(hdr
, 0, size
); /* XXX do we need this ? */
828 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
831 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
832 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
833 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
834 sa
->sadb_sa_spi
= x
->id
.spi
;
835 sa
->sadb_sa_replay
= x
->props
.replay_window
;
836 switch (x
->km
.state
) {
837 case XFRM_STATE_VALID
:
838 sa
->sadb_sa_state
= x
->km
.dying
?
839 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
842 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
845 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
848 sa
->sadb_sa_auth
= 0;
850 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
851 sa
->sadb_sa_auth
= a
? a
->desc
.sadb_alg_id
: 0;
853 sa
->sadb_sa_encrypt
= 0;
854 BUG_ON(x
->ealg
&& x
->calg
);
856 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
857 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
859 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
861 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
862 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
865 sa
->sadb_sa_flags
= 0;
866 if (x
->props
.flags
& XFRM_STATE_NOECN
)
867 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
868 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
869 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
870 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
871 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
875 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
876 sizeof(struct sadb_lifetime
));
877 lifetime
->sadb_lifetime_len
=
878 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
879 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
880 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
881 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
882 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
883 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
887 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
888 sizeof(struct sadb_lifetime
));
889 lifetime
->sadb_lifetime_len
=
890 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
891 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
892 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
893 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
894 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
895 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
898 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
899 sizeof(struct sadb_lifetime
));
900 lifetime
->sadb_lifetime_len
=
901 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
902 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
903 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
904 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
905 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
906 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
908 addr
= (struct sadb_address
*) skb_put(skb
,
909 sizeof(struct sadb_address
)+sockaddr_size
);
910 addr
->sadb_address_len
=
911 (sizeof(struct sadb_address
)+sockaddr_size
)/
913 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
914 /* "if the ports are non-zero, then the sadb_address_proto field,
915 normally zero, MUST be filled in with the transport
916 protocol's number." - RFC2367 */
917 addr
->sadb_address_proto
= 0;
918 addr
->sadb_address_reserved
= 0;
920 addr
->sadb_address_prefixlen
=
921 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
922 (struct sockaddr
*) (addr
+ 1),
924 if (!addr
->sadb_address_prefixlen
)
928 addr
= (struct sadb_address
*) skb_put(skb
,
929 sizeof(struct sadb_address
)+sockaddr_size
);
930 addr
->sadb_address_len
=
931 (sizeof(struct sadb_address
)+sockaddr_size
)/
933 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
934 addr
->sadb_address_proto
= 0;
935 addr
->sadb_address_reserved
= 0;
937 addr
->sadb_address_prefixlen
=
938 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
939 (struct sockaddr
*) (addr
+ 1),
941 if (!addr
->sadb_address_prefixlen
)
944 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
,
946 addr
= (struct sadb_address
*) skb_put(skb
,
947 sizeof(struct sadb_address
)+sockaddr_size
);
948 addr
->sadb_address_len
=
949 (sizeof(struct sadb_address
)+sockaddr_size
)/
951 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
952 addr
->sadb_address_proto
=
953 pfkey_proto_from_xfrm(x
->sel
.proto
);
954 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
955 addr
->sadb_address_reserved
= 0;
957 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
958 (struct sockaddr
*) (addr
+ 1),
963 if (add_keys
&& auth_key_size
) {
964 key
= (struct sadb_key
*) skb_put(skb
,
965 sizeof(struct sadb_key
)+auth_key_size
);
966 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
968 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
969 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
970 key
->sadb_key_reserved
= 0;
971 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
974 if (add_keys
&& encrypt_key_size
) {
975 key
= (struct sadb_key
*) skb_put(skb
,
976 sizeof(struct sadb_key
)+encrypt_key_size
);
977 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
978 encrypt_key_size
) / sizeof(uint64_t);
979 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
980 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
981 key
->sadb_key_reserved
= 0;
982 memcpy(key
+ 1, x
->ealg
->alg_key
,
983 (x
->ealg
->alg_key_len
+7)/8);
987 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
988 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
989 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
990 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
992 return ERR_PTR(-EINVAL
);
994 sa2
->sadb_x_sa2_mode
= mode
;
995 sa2
->sadb_x_sa2_reserved1
= 0;
996 sa2
->sadb_x_sa2_reserved2
= 0;
997 sa2
->sadb_x_sa2_sequence
= 0;
998 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
1000 if (natt
&& natt
->encap_type
) {
1001 struct sadb_x_nat_t_type
*n_type
;
1002 struct sadb_x_nat_t_port
*n_port
;
1005 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
1006 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
1007 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1008 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
1009 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
1010 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
1011 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
1014 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
1015 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1016 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1017 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
1018 n_port
->sadb_x_nat_t_port_reserved
= 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_DPORT
;
1024 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
1025 n_port
->sadb_x_nat_t_port_reserved
= 0;
1028 /* security context */
1030 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
1031 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1032 sec_ctx
->sadb_x_sec_len
=
1033 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1034 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1035 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1036 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1037 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1038 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1046 static inline struct sk_buff
*pfkey_xfrm_state2msg(struct xfrm_state
*x
)
1048 struct sk_buff
*skb
;
1050 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1055 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(struct xfrm_state
*x
,
1058 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1061 static struct xfrm_state
* pfkey_msg2xfrm_state(struct sadb_msg
*hdr
,
1064 struct xfrm_state
*x
;
1065 struct sadb_lifetime
*lifetime
;
1067 struct sadb_key
*key
;
1068 struct sadb_x_sec_ctx
*sec_ctx
;
1073 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
1075 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1076 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1077 return ERR_PTR(-EINVAL
);
1078 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1079 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1080 return ERR_PTR(-EINVAL
);
1081 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1082 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1083 return ERR_PTR(-EINVAL
);
1084 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1085 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1086 return ERR_PTR(-EINVAL
);
1088 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1090 return ERR_PTR(-EINVAL
);
1092 /* default error is no buffer space */
1097 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1098 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1099 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1100 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1101 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1104 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1105 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1107 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1108 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1109 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1110 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1111 return ERR_PTR(-EINVAL
);
1112 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1114 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1115 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1116 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1117 return ERR_PTR(-EINVAL
);
1118 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1120 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1121 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1122 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1123 return ERR_PTR(-EINVAL
);
1125 x
= xfrm_state_alloc();
1127 return ERR_PTR(-ENOBUFS
);
1129 x
->id
.proto
= proto
;
1130 x
->id
.spi
= sa
->sadb_sa_spi
;
1131 x
->props
.replay_window
= sa
->sadb_sa_replay
;
1132 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1133 x
->props
.flags
|= XFRM_STATE_NOECN
;
1134 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1135 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1136 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1137 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1139 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1];
1140 if (lifetime
!= NULL
) {
1141 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1142 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1143 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1144 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1146 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1];
1147 if (lifetime
!= NULL
) {
1148 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1149 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1150 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1151 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1154 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
1155 if (sec_ctx
!= NULL
) {
1156 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
1161 err
= security_xfrm_state_alloc(x
, uctx
);
1168 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1169 if (sa
->sadb_sa_auth
) {
1171 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1177 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1178 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1181 strcpy(x
->aalg
->alg_name
, a
->name
);
1182 x
->aalg
->alg_key_len
= 0;
1184 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1185 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1187 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1188 /* x->algo.flags = sa->sadb_sa_flags; */
1190 if (sa
->sadb_sa_encrypt
) {
1191 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1192 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1197 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1200 strcpy(x
->calg
->alg_name
, a
->name
);
1201 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1204 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1209 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1211 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1212 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1215 strcpy(x
->ealg
->alg_name
, a
->name
);
1216 x
->ealg
->alg_key_len
= 0;
1218 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1219 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1221 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1224 /* x->algo.flags = sa->sadb_sa_flags; */
1226 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1228 if (!x
->props
.family
) {
1229 err
= -EAFNOSUPPORT
;
1232 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1235 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1236 struct sadb_x_sa2
*sa2
= (void*)ext_hdrs
[SADB_X_EXT_SA2
-1];
1237 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1242 x
->props
.mode
= mode
;
1243 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1246 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1247 struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1249 /* Nobody uses this, but we try. */
1250 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1251 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1255 x
->sel
.family
= x
->props
.family
;
1257 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1258 struct sadb_x_nat_t_type
* n_type
;
1259 struct xfrm_encap_tmpl
*natt
;
1261 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1266 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1267 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1269 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1270 struct sadb_x_nat_t_port
* n_port
=
1271 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1272 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1274 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1275 struct sadb_x_nat_t_port
* n_port
=
1276 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1277 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1281 err
= xfrm_init_state(x
);
1285 x
->km
.seq
= hdr
->sadb_msg_seq
;
1289 x
->km
.state
= XFRM_STATE_DEAD
;
1291 return ERR_PTR(err
);
1294 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1299 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1301 struct sk_buff
*resp_skb
;
1302 struct sadb_x_sa2
*sa2
;
1303 struct sadb_address
*saddr
, *daddr
;
1304 struct sadb_msg
*out_hdr
;
1305 struct sadb_spirange
*range
;
1306 struct xfrm_state
*x
= NULL
;
1309 u32 min_spi
, max_spi
;
1312 unsigned short family
;
1313 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1315 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1316 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1319 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1323 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1324 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1327 reqid
= sa2
->sadb_x_sa2_reqid
;
1333 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1334 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1336 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1339 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1340 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1342 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1344 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1345 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1350 if (hdr
->sadb_msg_seq
) {
1351 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1352 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, xdaddr
, family
)) {
1359 x
= xfrm_find_acq(mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1365 max_spi
= 0x0fffffff;
1367 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1369 min_spi
= range
->sadb_spirange_min
;
1370 max_spi
= range
->sadb_spirange_max
;
1373 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1374 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1376 if (IS_ERR(resp_skb
)) {
1378 return PTR_ERR(resp_skb
);
1381 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1382 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1383 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1384 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1385 out_hdr
->sadb_msg_errno
= 0;
1386 out_hdr
->sadb_msg_reserved
= 0;
1387 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1388 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1392 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
1397 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1399 struct xfrm_state
*x
;
1401 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1404 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1407 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1411 spin_lock_bh(&x
->lock
);
1412 if (x
->km
.state
== XFRM_STATE_ACQ
) {
1413 x
->km
.state
= XFRM_STATE_ERROR
;
1416 spin_unlock_bh(&x
->lock
);
1421 static inline int event2poltype(int event
)
1424 case XFRM_MSG_DELPOLICY
:
1425 return SADB_X_SPDDELETE
;
1426 case XFRM_MSG_NEWPOLICY
:
1427 return SADB_X_SPDADD
;
1428 case XFRM_MSG_UPDPOLICY
:
1429 return SADB_X_SPDUPDATE
;
1430 case XFRM_MSG_POLEXPIRE
:
1431 // return SADB_X_SPDEXPIRE;
1433 printk("pfkey: Unknown policy event %d\n", event
);
1440 static inline int event2keytype(int event
)
1443 case XFRM_MSG_DELSA
:
1445 case XFRM_MSG_NEWSA
:
1447 case XFRM_MSG_UPDSA
:
1449 case XFRM_MSG_EXPIRE
:
1452 printk("pfkey: Unknown SA event %d\n", event
);
1460 static int key_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1462 struct sk_buff
*skb
;
1463 struct sadb_msg
*hdr
;
1465 skb
= pfkey_xfrm_state2msg(x
);
1468 return PTR_ERR(skb
);
1470 hdr
= (struct sadb_msg
*) skb
->data
;
1471 hdr
->sadb_msg_version
= PF_KEY_V2
;
1472 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1473 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1474 hdr
->sadb_msg_errno
= 0;
1475 hdr
->sadb_msg_reserved
= 0;
1476 hdr
->sadb_msg_seq
= c
->seq
;
1477 hdr
->sadb_msg_pid
= c
->pid
;
1479 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
1484 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1486 struct xfrm_state
*x
;
1490 x
= pfkey_msg2xfrm_state(hdr
, ext_hdrs
);
1495 if (hdr
->sadb_msg_type
== SADB_ADD
)
1496 err
= xfrm_state_add(x
);
1498 err
= xfrm_state_update(x
);
1500 xfrm_audit_state_add(x
, err
? 0 : 1,
1501 audit_get_loginuid(current
),
1502 audit_get_sessionid(current
), 0);
1505 x
->km
.state
= XFRM_STATE_DEAD
;
1506 __xfrm_state_put(x
);
1510 if (hdr
->sadb_msg_type
== SADB_ADD
)
1511 c
.event
= XFRM_MSG_NEWSA
;
1513 c
.event
= XFRM_MSG_UPDSA
;
1514 c
.seq
= hdr
->sadb_msg_seq
;
1515 c
.pid
= hdr
->sadb_msg_pid
;
1516 km_state_notify(x
, &c
);
1522 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1524 struct xfrm_state
*x
;
1528 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1529 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1530 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1533 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1537 if ((err
= security_xfrm_state_delete(x
)))
1540 if (xfrm_state_kern(x
)) {
1545 err
= xfrm_state_delete(x
);
1550 c
.seq
= hdr
->sadb_msg_seq
;
1551 c
.pid
= hdr
->sadb_msg_pid
;
1552 c
.event
= XFRM_MSG_DELSA
;
1553 km_state_notify(x
, &c
);
1555 xfrm_audit_state_delete(x
, err
? 0 : 1,
1556 audit_get_loginuid(current
),
1557 audit_get_sessionid(current
), 0);
1563 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1566 struct sk_buff
*out_skb
;
1567 struct sadb_msg
*out_hdr
;
1568 struct xfrm_state
*x
;
1570 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1571 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1572 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1575 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1579 out_skb
= pfkey_xfrm_state2msg(x
);
1580 proto
= x
->id
.proto
;
1582 if (IS_ERR(out_skb
))
1583 return PTR_ERR(out_skb
);
1585 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1586 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1587 out_hdr
->sadb_msg_type
= SADB_GET
;
1588 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1589 out_hdr
->sadb_msg_errno
= 0;
1590 out_hdr
->sadb_msg_reserved
= 0;
1591 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1592 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1593 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
);
1598 static struct sk_buff
*compose_sadb_supported(struct sadb_msg
*orig
,
1601 struct sk_buff
*skb
;
1602 struct sadb_msg
*hdr
;
1603 int len
, auth_len
, enc_len
, i
;
1605 auth_len
= xfrm_count_auth_supported();
1607 auth_len
*= sizeof(struct sadb_alg
);
1608 auth_len
+= sizeof(struct sadb_supported
);
1611 enc_len
= xfrm_count_enc_supported();
1613 enc_len
*= sizeof(struct sadb_alg
);
1614 enc_len
+= sizeof(struct sadb_supported
);
1617 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1619 skb
= alloc_skb(len
+ 16, allocation
);
1623 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1624 pfkey_hdr_dup(hdr
, orig
);
1625 hdr
->sadb_msg_errno
= 0;
1626 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1629 struct sadb_supported
*sp
;
1630 struct sadb_alg
*ap
;
1632 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1633 ap
= (struct sadb_alg
*) (sp
+ 1);
1635 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1636 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1638 for (i
= 0; ; i
++) {
1639 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1642 if (aalg
->available
)
1648 struct sadb_supported
*sp
;
1649 struct sadb_alg
*ap
;
1651 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1652 ap
= (struct sadb_alg
*) (sp
+ 1);
1654 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1655 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1657 for (i
= 0; ; i
++) {
1658 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1661 if (ealg
->available
)
1670 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1672 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1673 struct sk_buff
*supp_skb
;
1675 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1678 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1679 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1681 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1686 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1688 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1689 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1694 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
);
1699 static int key_notify_sa_flush(struct km_event
*c
)
1701 struct sk_buff
*skb
;
1702 struct sadb_msg
*hdr
;
1704 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1707 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1708 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1709 hdr
->sadb_msg_type
= SADB_FLUSH
;
1710 hdr
->sadb_msg_seq
= c
->seq
;
1711 hdr
->sadb_msg_pid
= c
->pid
;
1712 hdr
->sadb_msg_version
= PF_KEY_V2
;
1713 hdr
->sadb_msg_errno
= (uint8_t) 0;
1714 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1716 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
1721 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1725 struct xfrm_audit audit_info
;
1728 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1732 audit_info
.loginuid
= audit_get_loginuid(current
);
1733 audit_info
.sessionid
= audit_get_sessionid(current
);
1734 audit_info
.secid
= 0;
1735 err
= xfrm_state_flush(proto
, &audit_info
);
1738 c
.data
.proto
= proto
;
1739 c
.seq
= hdr
->sadb_msg_seq
;
1740 c
.pid
= hdr
->sadb_msg_pid
;
1741 c
.event
= XFRM_MSG_FLUSHSA
;
1742 km_state_notify(NULL
, &c
);
1747 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1749 struct pfkey_sock
*pfk
= ptr
;
1750 struct sk_buff
*out_skb
;
1751 struct sadb_msg
*out_hdr
;
1753 if (!pfkey_can_dump(&pfk
->sk
))
1756 out_skb
= pfkey_xfrm_state2msg(x
);
1757 if (IS_ERR(out_skb
))
1758 return PTR_ERR(out_skb
);
1760 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1761 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1762 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1763 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1764 out_hdr
->sadb_msg_errno
= 0;
1765 out_hdr
->sadb_msg_reserved
= 0;
1766 out_hdr
->sadb_msg_seq
= count
+ 1;
1767 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
1770 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
1772 pfk
->dump
.skb
= out_skb
;
1777 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1779 return xfrm_state_walk(&pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1782 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1784 xfrm_state_walk_done(&pfk
->dump
.u
.state
);
1787 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1790 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1792 if (pfk
->dump
.dump
!= NULL
)
1795 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1799 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1800 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
1801 pfk
->dump
.dump
= pfkey_dump_sa
;
1802 pfk
->dump
.done
= pfkey_dump_sa_done
;
1803 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
);
1805 return pfkey_do_dump(pfk
);
1808 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1810 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1811 int satype
= hdr
->sadb_msg_satype
;
1813 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1814 /* XXX we mangle packet... */
1815 hdr
->sadb_msg_errno
= 0;
1816 if (satype
!= 0 && satype
!= 1)
1818 pfk
->promisc
= satype
;
1820 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
, BROADCAST_ALL
, NULL
);
1824 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1827 u32 reqid
= *(u32
*)ptr
;
1829 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1830 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1836 static u32
gen_reqid(void)
1838 struct xfrm_policy_walk walk
;
1841 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1847 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1848 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1849 rc
= xfrm_policy_walk(&walk
, check_reqid
, (void*)&reqid
);
1850 xfrm_policy_walk_done(&walk
);
1853 } while (reqid
!= start
);
1858 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1860 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1863 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1866 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1869 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1870 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1873 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1875 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1876 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1877 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1879 if (!t
->reqid
&& !(t
->reqid
= gen_reqid()))
1883 /* addresses present only in tunnel mode */
1884 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1885 u8
*sa
= (u8
*) (rq
+ 1);
1886 int family
, socklen
;
1888 family
= pfkey_sockaddr_extract((struct sockaddr
*)sa
,
1893 socklen
= pfkey_sockaddr_len(family
);
1894 if (pfkey_sockaddr_extract((struct sockaddr
*)(sa
+ socklen
),
1895 &t
->id
.daddr
) != family
)
1897 t
->encap_family
= family
;
1899 t
->encap_family
= xp
->family
;
1901 /* No way to set this via kame pfkey */
1908 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1911 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1912 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1914 while (len
>= sizeof(struct sadb_x_ipsecrequest
)) {
1915 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
1917 len
-= rq
->sadb_x_ipsecrequest_len
;
1918 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
1923 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy
*xp
)
1925 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
1928 int len
= sizeof(struct sadb_x_sec_ctx
);
1929 len
+= xfrm_ctx
->ctx_len
;
1930 return PFKEY_ALIGN8(len
);
1935 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy
*xp
)
1937 struct xfrm_tmpl
*t
;
1938 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1942 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1943 t
= xp
->xfrm_vec
+ i
;
1944 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
1947 return sizeof(struct sadb_msg
) +
1948 (sizeof(struct sadb_lifetime
) * 3) +
1949 (sizeof(struct sadb_address
) * 2) +
1950 (sockaddr_size
* 2) +
1951 sizeof(struct sadb_x_policy
) +
1952 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
1954 pfkey_xfrm_policy2sec_ctx_size(xp
);
1957 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(struct xfrm_policy
*xp
)
1959 struct sk_buff
*skb
;
1962 size
= pfkey_xfrm_policy2msg_size(xp
);
1964 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
1966 return ERR_PTR(-ENOBUFS
);
1971 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, struct xfrm_policy
*xp
, int dir
)
1973 struct sadb_msg
*hdr
;
1974 struct sadb_address
*addr
;
1975 struct sadb_lifetime
*lifetime
;
1976 struct sadb_x_policy
*pol
;
1977 struct sadb_x_sec_ctx
*sec_ctx
;
1978 struct xfrm_sec_ctx
*xfrm_ctx
;
1981 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1982 int socklen
= pfkey_sockaddr_len(xp
->family
);
1984 size
= pfkey_xfrm_policy2msg_size(xp
);
1986 /* call should fill header later */
1987 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1988 memset(hdr
, 0, size
); /* XXX do we need this ? */
1991 addr
= (struct sadb_address
*) skb_put(skb
,
1992 sizeof(struct sadb_address
)+sockaddr_size
);
1993 addr
->sadb_address_len
=
1994 (sizeof(struct sadb_address
)+sockaddr_size
)/
1996 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1997 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
1998 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
1999 addr
->sadb_address_reserved
= 0;
2000 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2002 (struct sockaddr
*) (addr
+ 1),
2007 addr
= (struct sadb_address
*) skb_put(skb
,
2008 sizeof(struct sadb_address
)+sockaddr_size
);
2009 addr
->sadb_address_len
=
2010 (sizeof(struct sadb_address
)+sockaddr_size
)/
2012 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2013 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2014 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2015 addr
->sadb_address_reserved
= 0;
2017 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2018 (struct sockaddr
*) (addr
+ 1),
2022 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2023 sizeof(struct sadb_lifetime
));
2024 lifetime
->sadb_lifetime_len
=
2025 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2026 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2027 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2028 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2029 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2030 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2032 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2033 sizeof(struct sadb_lifetime
));
2034 lifetime
->sadb_lifetime_len
=
2035 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2036 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2037 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2038 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2039 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2040 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2042 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2043 sizeof(struct sadb_lifetime
));
2044 lifetime
->sadb_lifetime_len
=
2045 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2046 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2047 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2048 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2049 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2050 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2052 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2053 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2054 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2055 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2056 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2058 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2060 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2062 pol
->sadb_x_policy_dir
= dir
+1;
2063 pol
->sadb_x_policy_id
= xp
->index
;
2064 pol
->sadb_x_policy_priority
= xp
->priority
;
2066 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2067 struct sadb_x_ipsecrequest
*rq
;
2068 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2072 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2073 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2074 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2075 req_size
+= socklen
* 2;
2080 rq
= (void*)skb_put(skb
, req_size
);
2081 pol
->sadb_x_policy_len
+= req_size
/8;
2082 memset(rq
, 0, sizeof(*rq
));
2083 rq
->sadb_x_ipsecrequest_len
= req_size
;
2084 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2085 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2087 rq
->sadb_x_ipsecrequest_mode
= mode
;
2088 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2090 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2092 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2093 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2095 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2096 u8
*sa
= (void *)(rq
+ 1);
2097 pfkey_sockaddr_fill(&t
->saddr
, 0,
2098 (struct sockaddr
*)sa
,
2100 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2101 (struct sockaddr
*) (sa
+ socklen
),
2106 /* security context */
2107 if ((xfrm_ctx
= xp
->security
)) {
2108 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2110 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
, ctx_size
);
2111 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2112 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2113 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2114 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2115 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2116 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2120 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2121 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
2126 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2128 struct sk_buff
*out_skb
;
2129 struct sadb_msg
*out_hdr
;
2132 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2133 if (IS_ERR(out_skb
)) {
2134 err
= PTR_ERR(out_skb
);
2137 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2141 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2142 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2144 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2145 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2147 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2148 out_hdr
->sadb_msg_errno
= 0;
2149 out_hdr
->sadb_msg_seq
= c
->seq
;
2150 out_hdr
->sadb_msg_pid
= c
->pid
;
2151 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
2157 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2160 struct sadb_lifetime
*lifetime
;
2161 struct sadb_address
*sa
;
2162 struct sadb_x_policy
*pol
;
2163 struct xfrm_policy
*xp
;
2165 struct sadb_x_sec_ctx
*sec_ctx
;
2167 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2168 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2169 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2172 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2173 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2175 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2178 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2182 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2183 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2184 xp
->priority
= pol
->sadb_x_policy_priority
;
2186 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2187 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2192 xp
->selector
.family
= xp
->family
;
2193 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2194 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2195 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2196 if (xp
->selector
.sport
)
2197 xp
->selector
.sport_mask
= htons(0xffff);
2199 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2200 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2201 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2203 /* Amusing, we set this twice. KAME apps appear to set same value
2204 * in both addresses.
2206 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2208 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2209 if (xp
->selector
.dport
)
2210 xp
->selector
.dport_mask
= htons(0xffff);
2212 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2213 if (sec_ctx
!= NULL
) {
2214 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2221 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
2228 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2229 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2230 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2231 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2232 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2233 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2234 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2235 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2236 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2238 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2239 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2240 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2241 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2242 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2245 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2246 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2249 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2250 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2252 xfrm_audit_policy_add(xp
, err
? 0 : 1,
2253 audit_get_loginuid(current
),
2254 audit_get_sessionid(current
), 0);
2259 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2260 c
.event
= XFRM_MSG_UPDPOLICY
;
2262 c
.event
= XFRM_MSG_NEWPOLICY
;
2264 c
.seq
= hdr
->sadb_msg_seq
;
2265 c
.pid
= hdr
->sadb_msg_pid
;
2267 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2273 xfrm_policy_destroy(xp
);
2277 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2280 struct sadb_address
*sa
;
2281 struct sadb_x_policy
*pol
;
2282 struct xfrm_policy
*xp
;
2283 struct xfrm_selector sel
;
2285 struct sadb_x_sec_ctx
*sec_ctx
;
2286 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2288 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2289 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2290 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2293 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2294 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2297 memset(&sel
, 0, sizeof(sel
));
2299 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2300 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2301 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2302 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2303 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2305 sel
.sport_mask
= htons(0xffff);
2307 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2308 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2309 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2310 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2311 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2313 sel
.dport_mask
= htons(0xffff);
2315 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2316 if (sec_ctx
!= NULL
) {
2317 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2322 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
);
2328 xp
= xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN
,
2329 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2331 security_xfrm_policy_free(pol_ctx
);
2335 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2336 audit_get_loginuid(current
),
2337 audit_get_sessionid(current
), 0);
2342 c
.seq
= hdr
->sadb_msg_seq
;
2343 c
.pid
= hdr
->sadb_msg_pid
;
2345 c
.event
= XFRM_MSG_DELPOLICY
;
2346 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2353 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, struct sadb_msg
*hdr
, int dir
)
2356 struct sk_buff
*out_skb
;
2357 struct sadb_msg
*out_hdr
;
2360 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2361 if (IS_ERR(out_skb
)) {
2362 err
= PTR_ERR(out_skb
);
2365 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2369 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2370 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2371 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2372 out_hdr
->sadb_msg_satype
= 0;
2373 out_hdr
->sadb_msg_errno
= 0;
2374 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2375 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2376 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
);
2383 #ifdef CONFIG_NET_KEY_MIGRATE
2384 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2386 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2389 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2390 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2395 if (ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2398 af
= pfkey_sockaddr_extract(sa
, saddr
);
2402 socklen
= pfkey_sockaddr_len(af
);
2403 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2411 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2412 struct xfrm_migrate
*m
)
2415 struct sadb_x_ipsecrequest
*rq2
;
2418 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2419 len
< rq1
->sadb_x_ipsecrequest_len
)
2423 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2424 rq1
->sadb_x_ipsecrequest_len
,
2425 &m
->old_saddr
, &m
->old_daddr
,
2430 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2431 len
-= rq1
->sadb_x_ipsecrequest_len
;
2433 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2434 len
< rq2
->sadb_x_ipsecrequest_len
)
2438 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2439 rq2
->sadb_x_ipsecrequest_len
,
2440 &m
->new_saddr
, &m
->new_daddr
,
2445 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2446 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2447 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2450 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2451 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2454 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2456 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2457 rq2
->sadb_x_ipsecrequest_len
));
2460 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2461 struct sadb_msg
*hdr
, void **ext_hdrs
)
2463 int i
, len
, ret
, err
= -EINVAL
;
2465 struct sadb_address
*sa
;
2466 struct sadb_x_kmaddress
*kma
;
2467 struct sadb_x_policy
*pol
;
2468 struct sadb_x_ipsecrequest
*rq
;
2469 struct xfrm_selector sel
;
2470 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2471 struct xfrm_kmaddress k
;
2473 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2474 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2475 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2480 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2481 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2483 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2489 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2490 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2491 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2492 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2493 &k
.local
, &k
.remote
, &k
.family
);
2500 dir
= pol
->sadb_x_policy_dir
- 1;
2501 memset(&sel
, 0, sizeof(sel
));
2503 /* set source address info of selector */
2504 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2505 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2506 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2507 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2508 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2510 sel
.sport_mask
= htons(0xffff);
2512 /* set destination address info of selector */
2513 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1],
2514 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2515 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2516 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2517 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2519 sel
.dport_mask
= htons(0xffff);
2521 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2523 /* extract ipsecrequests */
2525 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2527 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2528 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2533 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2539 if (!i
|| len
> 0) {
2544 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2551 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2552 struct sadb_msg
*hdr
, void **ext_hdrs
)
2554 return -ENOPROTOOPT
;
2559 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2562 int err
= 0, delete;
2563 struct sadb_x_policy
*pol
;
2564 struct xfrm_policy
*xp
;
2567 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2570 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2571 if (dir
>= XFRM_POLICY_MAX
)
2574 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2575 xp
= xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN
, dir
, pol
->sadb_x_policy_id
,
2581 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2582 audit_get_loginuid(current
),
2583 audit_get_sessionid(current
), 0);
2587 c
.seq
= hdr
->sadb_msg_seq
;
2588 c
.pid
= hdr
->sadb_msg_pid
;
2590 c
.event
= XFRM_MSG_DELPOLICY
;
2591 km_policy_notify(xp
, dir
, &c
);
2593 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2601 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2603 struct pfkey_sock
*pfk
= ptr
;
2604 struct sk_buff
*out_skb
;
2605 struct sadb_msg
*out_hdr
;
2608 if (!pfkey_can_dump(&pfk
->sk
))
2611 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2612 if (IS_ERR(out_skb
))
2613 return PTR_ERR(out_skb
);
2615 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2619 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2620 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2621 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2622 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2623 out_hdr
->sadb_msg_errno
= 0;
2624 out_hdr
->sadb_msg_seq
= count
+ 1;
2625 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
2628 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
2630 pfk
->dump
.skb
= out_skb
;
2635 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2637 return xfrm_policy_walk(&pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2640 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2642 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
);
2645 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2647 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2649 if (pfk
->dump
.dump
!= NULL
)
2652 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2653 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
2654 pfk
->dump
.dump
= pfkey_dump_sp
;
2655 pfk
->dump
.done
= pfkey_dump_sp_done
;
2656 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2658 return pfkey_do_dump(pfk
);
2661 static int key_notify_policy_flush(struct km_event
*c
)
2663 struct sk_buff
*skb_out
;
2664 struct sadb_msg
*hdr
;
2666 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2669 hdr
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2670 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2671 hdr
->sadb_msg_seq
= c
->seq
;
2672 hdr
->sadb_msg_pid
= c
->pid
;
2673 hdr
->sadb_msg_version
= PF_KEY_V2
;
2674 hdr
->sadb_msg_errno
= (uint8_t) 0;
2675 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2676 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
2681 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2684 struct xfrm_audit audit_info
;
2687 audit_info
.loginuid
= audit_get_loginuid(current
);
2688 audit_info
.sessionid
= audit_get_sessionid(current
);
2689 audit_info
.secid
= 0;
2690 err
= xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN
, &audit_info
);
2693 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2694 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2695 c
.pid
= hdr
->sadb_msg_pid
;
2696 c
.seq
= hdr
->sadb_msg_seq
;
2697 km_policy_notify(NULL
, 0, &c
);
2702 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2703 struct sadb_msg
*hdr
, void **ext_hdrs
);
2704 static pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2705 [SADB_RESERVED
] = pfkey_reserved
,
2706 [SADB_GETSPI
] = pfkey_getspi
,
2707 [SADB_UPDATE
] = pfkey_add
,
2708 [SADB_ADD
] = pfkey_add
,
2709 [SADB_DELETE
] = pfkey_delete
,
2710 [SADB_GET
] = pfkey_get
,
2711 [SADB_ACQUIRE
] = pfkey_acquire
,
2712 [SADB_REGISTER
] = pfkey_register
,
2713 [SADB_EXPIRE
] = NULL
,
2714 [SADB_FLUSH
] = pfkey_flush
,
2715 [SADB_DUMP
] = pfkey_dump
,
2716 [SADB_X_PROMISC
] = pfkey_promisc
,
2717 [SADB_X_PCHANGE
] = NULL
,
2718 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2719 [SADB_X_SPDADD
] = pfkey_spdadd
,
2720 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2721 [SADB_X_SPDGET
] = pfkey_spdget
,
2722 [SADB_X_SPDACQUIRE
] = NULL
,
2723 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2724 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2725 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2726 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2727 [SADB_X_MIGRATE
] = pfkey_migrate
,
2730 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
)
2732 void *ext_hdrs
[SADB_EXT_MAX
];
2735 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2736 BROADCAST_PROMISC_ONLY
, NULL
);
2738 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2739 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2742 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2743 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2748 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2750 struct sadb_msg
*hdr
= NULL
;
2752 if (skb
->len
< sizeof(*hdr
)) {
2755 hdr
= (struct sadb_msg
*) skb
->data
;
2756 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2757 hdr
->sadb_msg_reserved
!= 0 ||
2758 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2759 hdr
->sadb_msg_type
> SADB_MAX
)) {
2762 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2763 sizeof(uint64_t)) ||
2764 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2765 sizeof(uint64_t))) {
2775 static inline int aalg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2777 unsigned int id
= d
->desc
.sadb_alg_id
;
2779 if (id
>= sizeof(t
->aalgos
) * 8)
2782 return (t
->aalgos
>> id
) & 1;
2785 static inline int ealg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2787 unsigned int id
= d
->desc
.sadb_alg_id
;
2789 if (id
>= sizeof(t
->ealgos
) * 8)
2792 return (t
->ealgos
>> id
) & 1;
2795 static int count_ah_combs(struct xfrm_tmpl
*t
)
2799 for (i
= 0; ; i
++) {
2800 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2803 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2804 sz
+= sizeof(struct sadb_comb
);
2806 return sz
+ sizeof(struct sadb_prop
);
2809 static int count_esp_combs(struct xfrm_tmpl
*t
)
2813 for (i
= 0; ; i
++) {
2814 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2818 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2821 for (k
= 1; ; k
++) {
2822 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2826 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2827 sz
+= sizeof(struct sadb_comb
);
2830 return sz
+ sizeof(struct sadb_prop
);
2833 static void dump_ah_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2835 struct sadb_prop
*p
;
2838 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2839 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2840 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2841 p
->sadb_prop_replay
= 32;
2842 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2844 for (i
= 0; ; i
++) {
2845 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2849 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2850 struct sadb_comb
*c
;
2851 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2852 memset(c
, 0, sizeof(*c
));
2853 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2854 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2855 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2856 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2857 c
->sadb_comb_hard_addtime
= 24*60*60;
2858 c
->sadb_comb_soft_addtime
= 20*60*60;
2859 c
->sadb_comb_hard_usetime
= 8*60*60;
2860 c
->sadb_comb_soft_usetime
= 7*60*60;
2865 static void dump_esp_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2867 struct sadb_prop
*p
;
2870 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2871 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2872 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2873 p
->sadb_prop_replay
= 32;
2874 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2877 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2881 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2884 for (k
= 1; ; k
++) {
2885 struct sadb_comb
*c
;
2886 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2889 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2891 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2892 memset(c
, 0, sizeof(*c
));
2893 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2894 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2895 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2896 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2897 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
2898 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
2899 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
2900 c
->sadb_comb_hard_addtime
= 24*60*60;
2901 c
->sadb_comb_soft_addtime
= 20*60*60;
2902 c
->sadb_comb_hard_usetime
= 8*60*60;
2903 c
->sadb_comb_soft_usetime
= 7*60*60;
2908 static int key_notify_policy_expire(struct xfrm_policy
*xp
, struct km_event
*c
)
2913 static int key_notify_sa_expire(struct xfrm_state
*x
, struct km_event
*c
)
2915 struct sk_buff
*out_skb
;
2916 struct sadb_msg
*out_hdr
;
2920 hard
= c
->data
.hard
;
2926 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
2927 if (IS_ERR(out_skb
))
2928 return PTR_ERR(out_skb
);
2930 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2931 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2932 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
2933 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2934 out_hdr
->sadb_msg_errno
= 0;
2935 out_hdr
->sadb_msg_reserved
= 0;
2936 out_hdr
->sadb_msg_seq
= 0;
2937 out_hdr
->sadb_msg_pid
= 0;
2939 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
2943 static int pfkey_send_notify(struct xfrm_state
*x
, struct km_event
*c
)
2945 if (atomic_read(&pfkey_socks_nr
) == 0)
2949 case XFRM_MSG_EXPIRE
:
2950 return key_notify_sa_expire(x
, c
);
2951 case XFRM_MSG_DELSA
:
2952 case XFRM_MSG_NEWSA
:
2953 case XFRM_MSG_UPDSA
:
2954 return key_notify_sa(x
, c
);
2955 case XFRM_MSG_FLUSHSA
:
2956 return key_notify_sa_flush(c
);
2957 case XFRM_MSG_NEWAE
: /* not yet supported */
2960 printk("pfkey: Unknown SA event %d\n", c
->event
);
2967 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2969 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
2973 case XFRM_MSG_POLEXPIRE
:
2974 return key_notify_policy_expire(xp
, c
);
2975 case XFRM_MSG_DELPOLICY
:
2976 case XFRM_MSG_NEWPOLICY
:
2977 case XFRM_MSG_UPDPOLICY
:
2978 return key_notify_policy(xp
, dir
, c
);
2979 case XFRM_MSG_FLUSHPOLICY
:
2980 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
2982 return key_notify_policy_flush(c
);
2984 printk("pfkey: Unknown policy event %d\n", c
->event
);
2991 static u32
get_acqseq(void)
2995 static DEFINE_SPINLOCK(acqseq_lock
);
2997 spin_lock_bh(&acqseq_lock
);
2998 res
= (++acqseq
? : ++acqseq
);
2999 spin_unlock_bh(&acqseq_lock
);
3003 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
, int dir
)
3005 struct sk_buff
*skb
;
3006 struct sadb_msg
*hdr
;
3007 struct sadb_address
*addr
;
3008 struct sadb_x_policy
*pol
;
3011 struct sadb_x_sec_ctx
*sec_ctx
;
3012 struct xfrm_sec_ctx
*xfrm_ctx
;
3015 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3019 size
= sizeof(struct sadb_msg
) +
3020 (sizeof(struct sadb_address
) * 2) +
3021 (sockaddr_size
* 2) +
3022 sizeof(struct sadb_x_policy
);
3024 if (x
->id
.proto
== IPPROTO_AH
)
3025 size
+= count_ah_combs(t
);
3026 else if (x
->id
.proto
== IPPROTO_ESP
)
3027 size
+= count_esp_combs(t
);
3029 if ((xfrm_ctx
= x
->security
)) {
3030 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3031 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3034 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3038 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3039 hdr
->sadb_msg_version
= PF_KEY_V2
;
3040 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3041 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3042 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3043 hdr
->sadb_msg_errno
= 0;
3044 hdr
->sadb_msg_reserved
= 0;
3045 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3046 hdr
->sadb_msg_pid
= 0;
3049 addr
= (struct sadb_address
*) skb_put(skb
,
3050 sizeof(struct sadb_address
)+sockaddr_size
);
3051 addr
->sadb_address_len
=
3052 (sizeof(struct sadb_address
)+sockaddr_size
)/
3054 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3055 addr
->sadb_address_proto
= 0;
3056 addr
->sadb_address_reserved
= 0;
3057 addr
->sadb_address_prefixlen
=
3058 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3059 (struct sockaddr
*) (addr
+ 1),
3061 if (!addr
->sadb_address_prefixlen
)
3065 addr
= (struct sadb_address
*) skb_put(skb
,
3066 sizeof(struct sadb_address
)+sockaddr_size
);
3067 addr
->sadb_address_len
=
3068 (sizeof(struct sadb_address
)+sockaddr_size
)/
3070 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3071 addr
->sadb_address_proto
= 0;
3072 addr
->sadb_address_reserved
= 0;
3073 addr
->sadb_address_prefixlen
=
3074 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3075 (struct sockaddr
*) (addr
+ 1),
3077 if (!addr
->sadb_address_prefixlen
)
3080 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
3081 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3082 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3083 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3084 pol
->sadb_x_policy_dir
= dir
+1;
3085 pol
->sadb_x_policy_id
= xp
->index
;
3087 /* Set sadb_comb's. */
3088 if (x
->id
.proto
== IPPROTO_AH
)
3089 dump_ah_combs(skb
, t
);
3090 else if (x
->id
.proto
== IPPROTO_ESP
)
3091 dump_esp_combs(skb
, t
);
3093 /* security context */
3095 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
3096 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3097 sec_ctx
->sadb_x_sec_len
=
3098 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3099 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3100 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3101 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3102 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3103 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3107 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
3110 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3111 u8
*data
, int len
, int *dir
)
3113 struct xfrm_policy
*xp
;
3114 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3115 struct sadb_x_sec_ctx
*sec_ctx
;
3117 switch (sk
->sk_family
) {
3119 if (opt
!= IP_IPSEC_POLICY
) {
3124 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3126 if (opt
!= IPV6_IPSEC_POLICY
) {
3139 if (len
< sizeof(struct sadb_x_policy
) ||
3140 pol
->sadb_x_policy_len
*8 > len
||
3141 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3142 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3145 xp
= xfrm_policy_alloc(GFP_ATOMIC
);
3151 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3152 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3154 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3155 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3156 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3157 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3158 xp
->family
= sk
->sk_family
;
3161 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3162 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3165 /* security context too */
3166 if (len
>= (pol
->sadb_x_policy_len
*8 +
3167 sizeof(struct sadb_x_sec_ctx
))) {
3168 char *p
= (char *)pol
;
3169 struct xfrm_user_sec_ctx
*uctx
;
3171 p
+= pol
->sadb_x_policy_len
*8;
3172 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3173 if (len
< pol
->sadb_x_policy_len
*8 +
3174 sec_ctx
->sadb_x_sec_len
) {
3178 if ((*dir
= verify_sec_ctx_len(p
)))
3180 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
3181 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
3188 *dir
= pol
->sadb_x_policy_dir
-1;
3192 xfrm_policy_destroy(xp
);
3196 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3198 struct sk_buff
*skb
;
3199 struct sadb_msg
*hdr
;
3201 struct sadb_address
*addr
;
3202 struct sadb_x_nat_t_port
*n_port
;
3205 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3206 struct xfrm_encap_tmpl
*natt
= NULL
;
3208 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3220 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3222 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3223 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3226 size
= sizeof(struct sadb_msg
) +
3227 sizeof(struct sadb_sa
) +
3228 (sizeof(struct sadb_address
) * 2) +
3229 (sockaddr_size
* 2) +
3230 (sizeof(struct sadb_x_nat_t_port
) * 2);
3232 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3236 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3237 hdr
->sadb_msg_version
= PF_KEY_V2
;
3238 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3239 hdr
->sadb_msg_satype
= satype
;
3240 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3241 hdr
->sadb_msg_errno
= 0;
3242 hdr
->sadb_msg_reserved
= 0;
3243 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3244 hdr
->sadb_msg_pid
= 0;
3247 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
3248 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3249 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3250 sa
->sadb_sa_spi
= x
->id
.spi
;
3251 sa
->sadb_sa_replay
= 0;
3252 sa
->sadb_sa_state
= 0;
3253 sa
->sadb_sa_auth
= 0;
3254 sa
->sadb_sa_encrypt
= 0;
3255 sa
->sadb_sa_flags
= 0;
3257 /* ADDRESS_SRC (old addr) */
3258 addr
= (struct sadb_address
*)
3259 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3260 addr
->sadb_address_len
=
3261 (sizeof(struct sadb_address
)+sockaddr_size
)/
3263 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3264 addr
->sadb_address_proto
= 0;
3265 addr
->sadb_address_reserved
= 0;
3266 addr
->sadb_address_prefixlen
=
3267 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3268 (struct sockaddr
*) (addr
+ 1),
3270 if (!addr
->sadb_address_prefixlen
)
3273 /* NAT_T_SPORT (old port) */
3274 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3275 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3276 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3277 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3278 n_port
->sadb_x_nat_t_port_reserved
= 0;
3280 /* ADDRESS_DST (new addr) */
3281 addr
= (struct sadb_address
*)
3282 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3283 addr
->sadb_address_len
=
3284 (sizeof(struct sadb_address
)+sockaddr_size
)/
3286 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3287 addr
->sadb_address_proto
= 0;
3288 addr
->sadb_address_reserved
= 0;
3289 addr
->sadb_address_prefixlen
=
3290 pfkey_sockaddr_fill(ipaddr
, 0,
3291 (struct sockaddr
*) (addr
+ 1),
3293 if (!addr
->sadb_address_prefixlen
)
3296 /* NAT_T_DPORT (new port) */
3297 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3298 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3299 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3300 n_port
->sadb_x_nat_t_port_port
= sport
;
3301 n_port
->sadb_x_nat_t_port_reserved
= 0;
3303 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
3306 #ifdef CONFIG_NET_KEY_MIGRATE
3307 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3308 struct xfrm_selector
*sel
)
3310 struct sadb_address
*addr
;
3311 addr
= (struct sadb_address
*)skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3312 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3313 addr
->sadb_address_exttype
= type
;
3314 addr
->sadb_address_proto
= sel
->proto
;
3315 addr
->sadb_address_reserved
= 0;
3318 case SADB_EXT_ADDRESS_SRC
:
3319 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3320 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3321 (struct sockaddr
*)(addr
+ 1),
3324 case SADB_EXT_ADDRESS_DST
:
3325 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3326 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3327 (struct sockaddr
*)(addr
+ 1),
3338 static int set_sadb_kmaddress(struct sk_buff
*skb
, struct xfrm_kmaddress
*k
)
3340 struct sadb_x_kmaddress
*kma
;
3342 int family
= k
->family
;
3343 int socklen
= pfkey_sockaddr_len(family
);
3346 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3347 pfkey_sockaddr_pair_size(family
));
3349 kma
= (struct sadb_x_kmaddress
*)skb_put(skb
, size_req
);
3350 memset(kma
, 0, size_req
);
3351 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3352 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3353 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3355 sa
= (u8
*)(kma
+ 1);
3356 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3357 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3363 static int set_ipsecrequest(struct sk_buff
*skb
,
3364 uint8_t proto
, uint8_t mode
, int level
,
3365 uint32_t reqid
, uint8_t family
,
3366 xfrm_address_t
*src
, xfrm_address_t
*dst
)
3368 struct sadb_x_ipsecrequest
*rq
;
3370 int socklen
= pfkey_sockaddr_len(family
);
3373 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3374 pfkey_sockaddr_pair_size(family
);
3376 rq
= (struct sadb_x_ipsecrequest
*)skb_put(skb
, size_req
);
3377 memset(rq
, 0, size_req
);
3378 rq
->sadb_x_ipsecrequest_len
= size_req
;
3379 rq
->sadb_x_ipsecrequest_proto
= proto
;
3380 rq
->sadb_x_ipsecrequest_mode
= mode
;
3381 rq
->sadb_x_ipsecrequest_level
= level
;
3382 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3384 sa
= (u8
*) (rq
+ 1);
3385 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3386 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3393 #ifdef CONFIG_NET_KEY_MIGRATE
3394 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3395 struct xfrm_migrate
*m
, int num_bundles
,
3396 struct xfrm_kmaddress
*k
)
3402 struct sk_buff
*skb
;
3403 struct sadb_msg
*hdr
;
3404 struct sadb_x_policy
*pol
;
3405 struct xfrm_migrate
*mp
;
3407 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3410 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3414 /* addresses for KM */
3415 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3416 pfkey_sockaddr_pair_size(k
->family
));
3420 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3423 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3426 size_pol
+= sizeof(struct sadb_x_policy
);
3429 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3430 /* old locator pair */
3431 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3432 pfkey_sockaddr_pair_size(mp
->old_family
);
3433 /* new locator pair */
3434 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3435 pfkey_sockaddr_pair_size(mp
->new_family
);
3438 size
+= sizeof(struct sadb_msg
) + size_pol
;
3441 skb
= alloc_skb(size
, GFP_ATOMIC
);
3445 hdr
= (struct sadb_msg
*)skb_put(skb
, sizeof(struct sadb_msg
));
3446 hdr
->sadb_msg_version
= PF_KEY_V2
;
3447 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3448 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3449 hdr
->sadb_msg_len
= size
/ 8;
3450 hdr
->sadb_msg_errno
= 0;
3451 hdr
->sadb_msg_reserved
= 0;
3452 hdr
->sadb_msg_seq
= 0;
3453 hdr
->sadb_msg_pid
= 0;
3455 /* Addresses to be used by KM for negotiation, if ext is available */
3456 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3460 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3463 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3465 /* policy information */
3466 pol
= (struct sadb_x_policy
*)skb_put(skb
, sizeof(struct sadb_x_policy
));
3467 pol
->sadb_x_policy_len
= size_pol
/ 8;
3468 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3469 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3470 pol
->sadb_x_policy_dir
= dir
+ 1;
3471 pol
->sadb_x_policy_id
= 0;
3472 pol
->sadb_x_policy_priority
= 0;
3474 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3475 /* old ipsecrequest */
3476 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3479 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3480 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3481 mp
->reqid
, mp
->old_family
,
3482 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3485 /* new ipsecrequest */
3486 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3487 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3488 mp
->reqid
, mp
->new_family
,
3489 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3493 /* broadcast migrate message to sockets */
3494 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
3503 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3504 struct xfrm_migrate
*m
, int num_bundles
,
3505 struct xfrm_kmaddress
*k
)
3507 return -ENOPROTOOPT
;
3511 static int pfkey_sendmsg(struct kiocb
*kiocb
,
3512 struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3514 struct sock
*sk
= sock
->sk
;
3515 struct sk_buff
*skb
= NULL
;
3516 struct sadb_msg
*hdr
= NULL
;
3520 if (msg
->msg_flags
& MSG_OOB
)
3524 if ((unsigned)len
> sk
->sk_sndbuf
- 32)
3528 skb
= alloc_skb(len
, GFP_KERNEL
);
3533 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
))
3536 hdr
= pfkey_get_base_msg(skb
, &err
);
3540 mutex_lock(&xfrm_cfg_mutex
);
3541 err
= pfkey_process(sk
, skb
, hdr
);
3542 mutex_unlock(&xfrm_cfg_mutex
);
3545 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3553 static int pfkey_recvmsg(struct kiocb
*kiocb
,
3554 struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3557 struct sock
*sk
= sock
->sk
;
3558 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3559 struct sk_buff
*skb
;
3563 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3566 msg
->msg_namelen
= 0;
3567 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3573 msg
->msg_flags
|= MSG_TRUNC
;
3577 skb_reset_transport_header(skb
);
3578 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
3582 sock_recv_timestamp(msg
, sk
, skb
);
3584 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3586 if (pfk
->dump
.dump
!= NULL
&&
3587 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3591 skb_free_datagram(sk
, skb
);
3596 static const struct proto_ops pfkey_ops
= {
3598 .owner
= THIS_MODULE
,
3599 /* Operations that make no sense on pfkey sockets. */
3600 .bind
= sock_no_bind
,
3601 .connect
= sock_no_connect
,
3602 .socketpair
= sock_no_socketpair
,
3603 .accept
= sock_no_accept
,
3604 .getname
= sock_no_getname
,
3605 .ioctl
= sock_no_ioctl
,
3606 .listen
= sock_no_listen
,
3607 .shutdown
= sock_no_shutdown
,
3608 .setsockopt
= sock_no_setsockopt
,
3609 .getsockopt
= sock_no_getsockopt
,
3610 .mmap
= sock_no_mmap
,
3611 .sendpage
= sock_no_sendpage
,
3613 /* Now the operations that really occur. */
3614 .release
= pfkey_release
,
3615 .poll
= datagram_poll
,
3616 .sendmsg
= pfkey_sendmsg
,
3617 .recvmsg
= pfkey_recvmsg
,
3620 static struct net_proto_family pfkey_family_ops
= {
3622 .create
= pfkey_create
,
3623 .owner
= THIS_MODULE
,
3626 #ifdef CONFIG_PROC_FS
3627 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3631 s
= (struct sock
*)v
;
3632 if (v
== SEQ_START_TOKEN
)
3633 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3635 seq_printf(f
,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3637 atomic_read(&s
->sk_refcnt
),
3638 atomic_read(&s
->sk_rmem_alloc
),
3639 atomic_read(&s
->sk_wmem_alloc
),
3646 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3649 struct hlist_node
*node
;
3652 read_lock(&pfkey_table_lock
);
3654 return SEQ_START_TOKEN
;
3656 sk_for_each(s
, node
, &pfkey_table
)
3663 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3666 return (v
== SEQ_START_TOKEN
) ?
3667 sk_head(&pfkey_table
) :
3668 sk_next((struct sock
*)v
);
3671 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3673 read_unlock(&pfkey_table_lock
);
3676 static struct seq_operations pfkey_seq_ops
= {
3677 .start
= pfkey_seq_start
,
3678 .next
= pfkey_seq_next
,
3679 .stop
= pfkey_seq_stop
,
3680 .show
= pfkey_seq_show
,
3683 static int pfkey_seq_open(struct inode
*inode
, struct file
*file
)
3685 return seq_open(file
, &pfkey_seq_ops
);
3688 static struct file_operations pfkey_proc_ops
= {
3689 .open
= pfkey_seq_open
,
3691 .llseek
= seq_lseek
,
3692 .release
= seq_release
,
3695 static int pfkey_init_proc(void)
3697 struct proc_dir_entry
*e
;
3699 e
= proc_net_fops_create(&init_net
, "pfkey", 0, &pfkey_proc_ops
);
3706 static void pfkey_exit_proc(void)
3708 proc_net_remove(&init_net
, "pfkey");
3711 static inline int pfkey_init_proc(void)
3716 static inline void pfkey_exit_proc(void)
3721 static struct xfrm_mgr pfkeyv2_mgr
=
3724 .notify
= pfkey_send_notify
,
3725 .acquire
= pfkey_send_acquire
,
3726 .compile_policy
= pfkey_compile_policy
,
3727 .new_mapping
= pfkey_send_new_mapping
,
3728 .notify_policy
= pfkey_send_policy_notify
,
3729 .migrate
= pfkey_send_migrate
,
3732 static void __exit
ipsec_pfkey_exit(void)
3734 xfrm_unregister_km(&pfkeyv2_mgr
);
3736 sock_unregister(PF_KEY
);
3737 proto_unregister(&key_proto
);
3740 static int __init
ipsec_pfkey_init(void)
3742 int err
= proto_register(&key_proto
, 0);
3747 err
= sock_register(&pfkey_family_ops
);
3749 goto out_unregister_key_proto
;
3750 err
= pfkey_init_proc();
3752 goto out_sock_unregister
;
3753 err
= xfrm_register_km(&pfkeyv2_mgr
);
3755 goto out_remove_proc_entry
;
3758 out_remove_proc_entry
:
3760 out_sock_unregister
:
3761 sock_unregister(PF_KEY
);
3762 out_unregister_key_proto
:
3763 proto_unregister(&key_proto
);
3767 module_init(ipsec_pfkey_init
);
3768 module_exit(ipsec_pfkey_exit
);
3769 MODULE_LICENSE("GPL");
3770 MODULE_ALIAS_NETPROTO(PF_KEY
);