2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/capability.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <net/net_namespace.h>
34 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
35 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 /* List of all pfkey sockets. */
39 static HLIST_HEAD(pfkey_table
);
40 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait
);
41 static DEFINE_RWLOCK(pfkey_table_lock
);
42 static atomic_t pfkey_table_users
= ATOMIC_INIT(0);
44 static atomic_t pfkey_socks_nr
= ATOMIC_INIT(0);
47 /* struct sock must be the first member of struct pfkey_sock */
55 int (*dump
)(struct pfkey_sock
*sk
);
56 void (*done
)(struct pfkey_sock
*sk
);
58 struct xfrm_policy_walk policy
;
59 struct xfrm_state_walk state
;
64 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
66 return (struct pfkey_sock
*)sk
;
69 static int pfkey_can_dump(struct sock
*sk
)
71 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
76 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
80 rc
= pfk
->dump
.dump(pfk
);
85 pfk
->dump
.dump
= NULL
;
86 pfk
->dump
.done
= NULL
;
90 static void pfkey_sock_destruct(struct sock
*sk
)
92 skb_queue_purge(&sk
->sk_receive_queue
);
94 if (!sock_flag(sk
, SOCK_DEAD
)) {
95 printk("Attempt to release alive pfkey socket: %p\n", sk
);
99 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
100 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
102 atomic_dec(&pfkey_socks_nr
);
105 static void pfkey_table_grab(void)
107 write_lock_bh(&pfkey_table_lock
);
109 if (atomic_read(&pfkey_table_users
)) {
110 DECLARE_WAITQUEUE(wait
, current
);
112 add_wait_queue_exclusive(&pfkey_table_wait
, &wait
);
114 set_current_state(TASK_UNINTERRUPTIBLE
);
115 if (atomic_read(&pfkey_table_users
) == 0)
117 write_unlock_bh(&pfkey_table_lock
);
119 write_lock_bh(&pfkey_table_lock
);
122 __set_current_state(TASK_RUNNING
);
123 remove_wait_queue(&pfkey_table_wait
, &wait
);
127 static __inline__
void pfkey_table_ungrab(void)
129 write_unlock_bh(&pfkey_table_lock
);
130 wake_up(&pfkey_table_wait
);
133 static __inline__
void pfkey_lock_table(void)
135 /* read_lock() synchronizes us to pfkey_table_grab */
137 read_lock(&pfkey_table_lock
);
138 atomic_inc(&pfkey_table_users
);
139 read_unlock(&pfkey_table_lock
);
142 static __inline__
void pfkey_unlock_table(void)
144 if (atomic_dec_and_test(&pfkey_table_users
))
145 wake_up(&pfkey_table_wait
);
149 static const struct proto_ops pfkey_ops
;
151 static void pfkey_insert(struct sock
*sk
)
154 sk_add_node(sk
, &pfkey_table
);
155 pfkey_table_ungrab();
158 static void pfkey_remove(struct sock
*sk
)
161 sk_del_node_init(sk
);
162 pfkey_table_ungrab();
165 static struct proto key_proto
= {
167 .owner
= THIS_MODULE
,
168 .obj_size
= sizeof(struct pfkey_sock
),
171 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
)
176 if (net
!= &init_net
)
177 return -EAFNOSUPPORT
;
179 if (!capable(CAP_NET_ADMIN
))
181 if (sock
->type
!= SOCK_RAW
)
182 return -ESOCKTNOSUPPORT
;
183 if (protocol
!= PF_KEY_V2
)
184 return -EPROTONOSUPPORT
;
187 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
);
191 sock
->ops
= &pfkey_ops
;
192 sock_init_data(sock
, sk
);
194 sk
->sk_family
= PF_KEY
;
195 sk
->sk_destruct
= pfkey_sock_destruct
;
197 atomic_inc(&pfkey_socks_nr
);
206 static int pfkey_release(struct socket
*sock
)
208 struct sock
*sk
= sock
->sk
;
217 skb_queue_purge(&sk
->sk_write_queue
);
223 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
224 gfp_t allocation
, struct sock
*sk
)
230 if (atomic_read(&skb
->users
) != 1) {
231 *skb2
= skb_clone(skb
, allocation
);
234 atomic_inc(&skb
->users
);
238 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
240 skb_set_owner_r(*skb2
, sk
);
241 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
242 sk
->sk_data_ready(sk
, (*skb2
)->len
);
251 /* Send SKB to all pfkey sockets matching selected criteria. */
252 #define BROADCAST_ALL 0
253 #define BROADCAST_ONE 1
254 #define BROADCAST_REGISTERED 2
255 #define BROADCAST_PROMISC_ONLY 4
256 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
257 int broadcast_flags
, struct sock
*one_sk
)
260 struct hlist_node
*node
;
261 struct sk_buff
*skb2
= NULL
;
264 /* XXX Do we need something like netlink_overrun? I think
265 * XXX PF_KEY socket apps will not mind current behavior.
271 sk_for_each(sk
, node
, &pfkey_table
) {
272 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
275 /* Yes, it means that if you are meant to receive this
276 * pfkey message you receive it twice as promiscuous
280 pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
282 /* the exact target will be processed later */
285 if (broadcast_flags
!= BROADCAST_ALL
) {
286 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
288 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
291 if (broadcast_flags
& BROADCAST_ONE
)
295 err2
= pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
297 /* Error is cleare after succecful sending to at least one
299 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
302 pfkey_unlock_table();
305 err
= pfkey_broadcast_one(skb
, &skb2
, allocation
, one_sk
);
313 static inline void pfkey_hdr_dup(struct sadb_msg
*new, struct sadb_msg
*orig
)
318 static int pfkey_error(struct sadb_msg
*orig
, int err
, struct sock
*sk
)
320 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
321 struct sadb_msg
*hdr
;
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
330 if (err
== ERESTARTSYS
||
331 err
== ERESTARTNOHAND
||
332 err
== ERESTARTNOINTR
)
336 BUG_ON(err
<= 0 || err
>= 256);
338 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
339 pfkey_hdr_dup(hdr
, orig
);
340 hdr
->sadb_msg_errno
= (uint8_t) err
;
341 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
344 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
349 static u8 sadb_ext_min_len
[] = {
350 [SADB_EXT_RESERVED
] = (u8
) 0,
351 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
352 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
353 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
354 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
355 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
356 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
357 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
358 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
359 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
360 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
361 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
362 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
363 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
364 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
365 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
366 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
367 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
368 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
369 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
370 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
371 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
372 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
373 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
374 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
377 /* Verify sadb_address_{len,prefixlen} against sa_family. */
378 static int verify_address_len(void *p
)
380 struct sadb_address
*sp
= p
;
381 struct sockaddr
*addr
= (struct sockaddr
*)(sp
+ 1);
382 struct sockaddr_in
*sin
;
383 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
384 struct sockaddr_in6
*sin6
;
388 switch (addr
->sa_family
) {
390 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
391 if (sp
->sadb_address_len
!= len
||
392 sp
->sadb_address_prefixlen
> 32)
395 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
397 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
398 if (sp
->sadb_address_len
!= len
||
399 sp
->sadb_address_prefixlen
> 128)
404 /* It is user using kernel to keep track of security
405 * associations for another protocol, such as
406 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
409 * XXX Actually, association/policy database is not yet
410 * XXX able to cope with arbitrary sockaddr families.
411 * XXX When it can, remove this -EINVAL. -DaveM
420 static inline int pfkey_sec_ctx_len(struct sadb_x_sec_ctx
*sec_ctx
)
422 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
423 sec_ctx
->sadb_x_ctx_len
,
427 static inline int verify_sec_ctx_len(void *p
)
429 struct sadb_x_sec_ctx
*sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
430 int len
= sec_ctx
->sadb_x_ctx_len
;
435 len
= pfkey_sec_ctx_len(sec_ctx
);
437 if (sec_ctx
->sadb_x_sec_len
!= len
)
443 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(struct sadb_x_sec_ctx
*sec_ctx
)
445 struct xfrm_user_sec_ctx
*uctx
= NULL
;
446 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
448 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), GFP_KERNEL
);
453 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
454 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
455 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
456 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
457 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
458 memcpy(uctx
+ 1, sec_ctx
+ 1,
464 static int present_and_same_family(struct sadb_address
*src
,
465 struct sadb_address
*dst
)
467 struct sockaddr
*s_addr
, *d_addr
;
472 s_addr
= (struct sockaddr
*)(src
+ 1);
473 d_addr
= (struct sockaddr
*)(dst
+ 1);
474 if (s_addr
->sa_family
!= d_addr
->sa_family
)
476 if (s_addr
->sa_family
!= AF_INET
477 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
478 && s_addr
->sa_family
!= AF_INET6
486 static int parse_exthdrs(struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
488 char *p
= (char *) hdr
;
494 struct sadb_ext
*ehdr
= (struct sadb_ext
*) p
;
498 ext_len
= ehdr
->sadb_ext_len
;
499 ext_len
*= sizeof(uint64_t);
500 ext_type
= ehdr
->sadb_ext_type
;
501 if (ext_len
< sizeof(uint64_t) ||
503 ext_type
== SADB_EXT_RESERVED
)
506 if (ext_type
<= SADB_EXT_MAX
) {
507 int min
= (int) sadb_ext_min_len
[ext_type
];
510 if (ext_hdrs
[ext_type
-1] != NULL
)
512 if (ext_type
== SADB_EXT_ADDRESS_SRC
||
513 ext_type
== SADB_EXT_ADDRESS_DST
||
514 ext_type
== SADB_EXT_ADDRESS_PROXY
||
515 ext_type
== SADB_X_EXT_NAT_T_OA
) {
516 if (verify_address_len(p
))
519 if (ext_type
== SADB_X_EXT_SEC_CTX
) {
520 if (verify_sec_ctx_len(p
))
523 ext_hdrs
[ext_type
-1] = p
;
533 pfkey_satype2proto(uint8_t satype
)
536 case SADB_SATYPE_UNSPEC
:
537 return IPSEC_PROTO_ANY
;
540 case SADB_SATYPE_ESP
:
542 case SADB_X_SATYPE_IPCOMP
:
552 pfkey_proto2satype(uint16_t proto
)
556 return SADB_SATYPE_AH
;
558 return SADB_SATYPE_ESP
;
560 return SADB_X_SATYPE_IPCOMP
;
568 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
569 * say specifically 'just raw sockets' as we encode them as 255.
572 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
574 return (proto
== IPSEC_PROTO_ANY
? 0 : proto
);
577 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
579 return (proto
? proto
: IPSEC_PROTO_ANY
);
582 static inline int pfkey_sockaddr_len(sa_family_t family
)
586 return sizeof(struct sockaddr_in
);
587 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
589 return sizeof(struct sockaddr_in6
);
596 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
598 switch (sa
->sa_family
) {
601 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
603 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
606 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
607 sizeof(struct in6_addr
));
615 int pfkey_sadb_addr2xfrm_addr(struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
617 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
621 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct sadb_msg
*hdr
, void **ext_hdrs
)
624 struct sadb_address
*addr
;
626 unsigned short family
;
627 xfrm_address_t
*xaddr
;
629 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
633 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
637 /* sadb_address_len should be checked by caller */
638 addr
= (struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
642 family
= ((struct sockaddr
*)(addr
+ 1))->sa_family
;
645 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
647 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
649 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
659 return xfrm_state_lookup(xaddr
, sa
->sadb_sa_spi
, proto
, family
);
662 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
665 pfkey_sockaddr_size(sa_family_t family
)
667 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
670 static inline int pfkey_mode_from_xfrm(int mode
)
673 case XFRM_MODE_TRANSPORT
:
674 return IPSEC_MODE_TRANSPORT
;
675 case XFRM_MODE_TUNNEL
:
676 return IPSEC_MODE_TUNNEL
;
678 return IPSEC_MODE_BEET
;
684 static inline int pfkey_mode_to_xfrm(int mode
)
687 case IPSEC_MODE_ANY
: /*XXX*/
688 case IPSEC_MODE_TRANSPORT
:
689 return XFRM_MODE_TRANSPORT
;
690 case IPSEC_MODE_TUNNEL
:
691 return XFRM_MODE_TUNNEL
;
692 case IPSEC_MODE_BEET
:
693 return XFRM_MODE_BEET
;
699 static unsigned int pfkey_sockaddr_fill(xfrm_address_t
*xaddr
, __be16 port
,
701 unsigned short family
)
706 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
707 sin
->sin_family
= AF_INET
;
708 sin
->sin_port
= port
;
709 sin
->sin_addr
.s_addr
= xaddr
->a4
;
710 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
713 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
716 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
717 sin6
->sin6_family
= AF_INET6
;
718 sin6
->sin6_port
= port
;
719 sin6
->sin6_flowinfo
= 0;
720 ipv6_addr_copy(&sin6
->sin6_addr
, (struct in6_addr
*)xaddr
->a6
);
721 sin6
->sin6_scope_id
= 0;
729 static struct sk_buff
*__pfkey_xfrm_state2msg(struct xfrm_state
*x
,
730 int add_keys
, int hsc
)
733 struct sadb_msg
*hdr
;
735 struct sadb_lifetime
*lifetime
;
736 struct sadb_address
*addr
;
737 struct sadb_key
*key
;
738 struct sadb_x_sa2
*sa2
;
739 struct sadb_x_sec_ctx
*sec_ctx
;
740 struct xfrm_sec_ctx
*xfrm_ctx
;
743 int auth_key_size
= 0;
744 int encrypt_key_size
= 0;
746 struct xfrm_encap_tmpl
*natt
= NULL
;
749 /* address family check */
750 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
752 return ERR_PTR(-EINVAL
);
754 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
755 key(AE), (identity(SD),) (sensitivity)> */
756 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
757 sizeof(struct sadb_lifetime
) +
758 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
759 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
760 sizeof(struct sadb_address
)*2 +
762 sizeof(struct sadb_x_sa2
);
764 if ((xfrm_ctx
= x
->security
)) {
765 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
766 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
769 /* identity & sensitivity */
770 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
771 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
774 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
776 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
777 size
+= sizeof(struct sadb_key
) + auth_key_size
;
779 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
781 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
782 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
788 if (natt
&& natt
->encap_type
) {
789 size
+= sizeof(struct sadb_x_nat_t_type
);
790 size
+= sizeof(struct sadb_x_nat_t_port
);
791 size
+= sizeof(struct sadb_x_nat_t_port
);
794 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
796 return ERR_PTR(-ENOBUFS
);
798 /* call should fill header later */
799 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
800 memset(hdr
, 0, size
); /* XXX do we need this ? */
801 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
804 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
805 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
806 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
807 sa
->sadb_sa_spi
= x
->id
.spi
;
808 sa
->sadb_sa_replay
= x
->props
.replay_window
;
809 switch (x
->km
.state
) {
810 case XFRM_STATE_VALID
:
811 sa
->sadb_sa_state
= x
->km
.dying
?
812 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
815 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
818 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
821 sa
->sadb_sa_auth
= 0;
823 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
824 sa
->sadb_sa_auth
= a
? a
->desc
.sadb_alg_id
: 0;
826 sa
->sadb_sa_encrypt
= 0;
827 BUG_ON(x
->ealg
&& x
->calg
);
829 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
830 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
832 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
834 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
835 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
838 sa
->sadb_sa_flags
= 0;
839 if (x
->props
.flags
& XFRM_STATE_NOECN
)
840 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
841 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
842 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
843 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
844 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
848 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
849 sizeof(struct sadb_lifetime
));
850 lifetime
->sadb_lifetime_len
=
851 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
852 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
853 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
854 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
855 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
856 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
860 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
861 sizeof(struct sadb_lifetime
));
862 lifetime
->sadb_lifetime_len
=
863 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
864 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
865 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
866 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
867 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
868 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
871 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
872 sizeof(struct sadb_lifetime
));
873 lifetime
->sadb_lifetime_len
=
874 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
875 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
876 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
877 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
878 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
879 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
881 addr
= (struct sadb_address
*) skb_put(skb
,
882 sizeof(struct sadb_address
)+sockaddr_size
);
883 addr
->sadb_address_len
=
884 (sizeof(struct sadb_address
)+sockaddr_size
)/
886 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
887 /* "if the ports are non-zero, then the sadb_address_proto field,
888 normally zero, MUST be filled in with the transport
889 protocol's number." - RFC2367 */
890 addr
->sadb_address_proto
= 0;
891 addr
->sadb_address_reserved
= 0;
893 addr
->sadb_address_prefixlen
=
894 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
895 (struct sockaddr
*) (addr
+ 1),
897 if (!addr
->sadb_address_prefixlen
)
901 addr
= (struct sadb_address
*) skb_put(skb
,
902 sizeof(struct sadb_address
)+sockaddr_size
);
903 addr
->sadb_address_len
=
904 (sizeof(struct sadb_address
)+sockaddr_size
)/
906 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
907 addr
->sadb_address_proto
= 0;
908 addr
->sadb_address_reserved
= 0;
910 addr
->sadb_address_prefixlen
=
911 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
912 (struct sockaddr
*) (addr
+ 1),
914 if (!addr
->sadb_address_prefixlen
)
917 if (xfrm_addr_cmp(&x
->sel
.saddr
, &x
->props
.saddr
,
919 addr
= (struct sadb_address
*) skb_put(skb
,
920 sizeof(struct sadb_address
)+sockaddr_size
);
921 addr
->sadb_address_len
=
922 (sizeof(struct sadb_address
)+sockaddr_size
)/
924 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
925 addr
->sadb_address_proto
=
926 pfkey_proto_from_xfrm(x
->sel
.proto
);
927 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
928 addr
->sadb_address_reserved
= 0;
930 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
931 (struct sockaddr
*) (addr
+ 1),
936 if (add_keys
&& auth_key_size
) {
937 key
= (struct sadb_key
*) skb_put(skb
,
938 sizeof(struct sadb_key
)+auth_key_size
);
939 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
941 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
942 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
943 key
->sadb_key_reserved
= 0;
944 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
947 if (add_keys
&& encrypt_key_size
) {
948 key
= (struct sadb_key
*) skb_put(skb
,
949 sizeof(struct sadb_key
)+encrypt_key_size
);
950 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
951 encrypt_key_size
) / sizeof(uint64_t);
952 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
953 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
954 key
->sadb_key_reserved
= 0;
955 memcpy(key
+ 1, x
->ealg
->alg_key
,
956 (x
->ealg
->alg_key_len
+7)/8);
960 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
961 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
962 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
963 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
965 return ERR_PTR(-EINVAL
);
967 sa2
->sadb_x_sa2_mode
= mode
;
968 sa2
->sadb_x_sa2_reserved1
= 0;
969 sa2
->sadb_x_sa2_reserved2
= 0;
970 sa2
->sadb_x_sa2_sequence
= 0;
971 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
973 if (natt
&& natt
->encap_type
) {
974 struct sadb_x_nat_t_type
*n_type
;
975 struct sadb_x_nat_t_port
*n_port
;
978 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
979 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
980 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
981 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
982 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
983 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
984 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
987 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
988 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
989 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
990 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
991 n_port
->sadb_x_nat_t_port_reserved
= 0;
994 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
995 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
996 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
997 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
998 n_port
->sadb_x_nat_t_port_reserved
= 0;
1001 /* security context */
1003 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
1004 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1005 sec_ctx
->sadb_x_sec_len
=
1006 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1007 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1008 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1009 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1010 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1011 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1019 static inline struct sk_buff
*pfkey_xfrm_state2msg(struct xfrm_state
*x
)
1021 struct sk_buff
*skb
;
1023 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1028 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(struct xfrm_state
*x
,
1031 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1034 static struct xfrm_state
* pfkey_msg2xfrm_state(struct sadb_msg
*hdr
,
1037 struct xfrm_state
*x
;
1038 struct sadb_lifetime
*lifetime
;
1040 struct sadb_key
*key
;
1041 struct sadb_x_sec_ctx
*sec_ctx
;
1046 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
1048 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1049 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1050 return ERR_PTR(-EINVAL
);
1051 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1052 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1053 return ERR_PTR(-EINVAL
);
1054 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1055 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1056 return ERR_PTR(-EINVAL
);
1057 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1058 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1059 return ERR_PTR(-EINVAL
);
1061 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1063 return ERR_PTR(-EINVAL
);
1065 /* default error is no buffer space */
1070 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1071 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1072 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1073 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1074 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1077 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1078 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1080 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1081 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1082 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1083 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1084 return ERR_PTR(-EINVAL
);
1085 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1087 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1088 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1089 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1090 return ERR_PTR(-EINVAL
);
1091 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1093 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1094 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1095 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1096 return ERR_PTR(-EINVAL
);
1098 x
= xfrm_state_alloc();
1100 return ERR_PTR(-ENOBUFS
);
1102 x
->id
.proto
= proto
;
1103 x
->id
.spi
= sa
->sadb_sa_spi
;
1104 x
->props
.replay_window
= sa
->sadb_sa_replay
;
1105 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1106 x
->props
.flags
|= XFRM_STATE_NOECN
;
1107 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1108 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1109 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1110 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1112 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1];
1113 if (lifetime
!= NULL
) {
1114 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1115 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1116 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1117 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1119 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1];
1120 if (lifetime
!= NULL
) {
1121 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1122 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1123 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1124 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1127 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
1128 if (sec_ctx
!= NULL
) {
1129 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
1134 err
= security_xfrm_state_alloc(x
, uctx
);
1141 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
1142 if (sa
->sadb_sa_auth
) {
1144 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1150 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1151 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1154 strcpy(x
->aalg
->alg_name
, a
->name
);
1155 x
->aalg
->alg_key_len
= 0;
1157 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1158 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1160 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1161 /* x->algo.flags = sa->sadb_sa_flags; */
1163 if (sa
->sadb_sa_encrypt
) {
1164 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1165 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1170 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1173 strcpy(x
->calg
->alg_name
, a
->name
);
1174 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1177 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1182 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1184 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1185 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1188 strcpy(x
->ealg
->alg_name
, a
->name
);
1189 x
->ealg
->alg_key_len
= 0;
1191 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1192 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1194 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1197 /* x->algo.flags = sa->sadb_sa_flags; */
1199 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1201 if (!x
->props
.family
) {
1202 err
= -EAFNOSUPPORT
;
1205 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1208 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1209 struct sadb_x_sa2
*sa2
= (void*)ext_hdrs
[SADB_X_EXT_SA2
-1];
1210 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1215 x
->props
.mode
= mode
;
1216 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1219 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1220 struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1222 /* Nobody uses this, but we try. */
1223 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1224 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1228 x
->sel
.family
= x
->props
.family
;
1230 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1231 struct sadb_x_nat_t_type
* n_type
;
1232 struct xfrm_encap_tmpl
*natt
;
1234 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1239 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1240 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1242 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1243 struct sadb_x_nat_t_port
* n_port
=
1244 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1245 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1247 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1248 struct sadb_x_nat_t_port
* n_port
=
1249 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1250 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1254 err
= xfrm_init_state(x
);
1258 x
->km
.seq
= hdr
->sadb_msg_seq
;
1262 x
->km
.state
= XFRM_STATE_DEAD
;
1264 return ERR_PTR(err
);
1267 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1272 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1274 struct sk_buff
*resp_skb
;
1275 struct sadb_x_sa2
*sa2
;
1276 struct sadb_address
*saddr
, *daddr
;
1277 struct sadb_msg
*out_hdr
;
1278 struct sadb_spirange
*range
;
1279 struct xfrm_state
*x
= NULL
;
1282 u32 min_spi
, max_spi
;
1285 unsigned short family
;
1286 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1288 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1289 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1292 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1296 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1297 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1300 reqid
= sa2
->sadb_x_sa2_reqid
;
1306 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1307 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1309 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1312 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1313 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1315 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1317 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1318 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1323 if (hdr
->sadb_msg_seq
) {
1324 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1325 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, xdaddr
, family
)) {
1332 x
= xfrm_find_acq(mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1338 max_spi
= 0x0fffffff;
1340 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1342 min_spi
= range
->sadb_spirange_min
;
1343 max_spi
= range
->sadb_spirange_max
;
1346 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1347 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1349 if (IS_ERR(resp_skb
)) {
1351 return PTR_ERR(resp_skb
);
1354 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1355 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1356 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1357 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1358 out_hdr
->sadb_msg_errno
= 0;
1359 out_hdr
->sadb_msg_reserved
= 0;
1360 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1361 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1365 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
1370 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1372 struct xfrm_state
*x
;
1374 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1377 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1380 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1384 spin_lock_bh(&x
->lock
);
1385 if (x
->km
.state
== XFRM_STATE_ACQ
) {
1386 x
->km
.state
= XFRM_STATE_ERROR
;
1389 spin_unlock_bh(&x
->lock
);
1394 static inline int event2poltype(int event
)
1397 case XFRM_MSG_DELPOLICY
:
1398 return SADB_X_SPDDELETE
;
1399 case XFRM_MSG_NEWPOLICY
:
1400 return SADB_X_SPDADD
;
1401 case XFRM_MSG_UPDPOLICY
:
1402 return SADB_X_SPDUPDATE
;
1403 case XFRM_MSG_POLEXPIRE
:
1404 // return SADB_X_SPDEXPIRE;
1406 printk("pfkey: Unknown policy event %d\n", event
);
1413 static inline int event2keytype(int event
)
1416 case XFRM_MSG_DELSA
:
1418 case XFRM_MSG_NEWSA
:
1420 case XFRM_MSG_UPDSA
:
1422 case XFRM_MSG_EXPIRE
:
1425 printk("pfkey: Unknown SA event %d\n", event
);
1433 static int key_notify_sa(struct xfrm_state
*x
, struct km_event
*c
)
1435 struct sk_buff
*skb
;
1436 struct sadb_msg
*hdr
;
1438 skb
= pfkey_xfrm_state2msg(x
);
1441 return PTR_ERR(skb
);
1443 hdr
= (struct sadb_msg
*) skb
->data
;
1444 hdr
->sadb_msg_version
= PF_KEY_V2
;
1445 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1446 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1447 hdr
->sadb_msg_errno
= 0;
1448 hdr
->sadb_msg_reserved
= 0;
1449 hdr
->sadb_msg_seq
= c
->seq
;
1450 hdr
->sadb_msg_pid
= c
->pid
;
1452 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
1457 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1459 struct xfrm_state
*x
;
1463 x
= pfkey_msg2xfrm_state(hdr
, ext_hdrs
);
1468 if (hdr
->sadb_msg_type
== SADB_ADD
)
1469 err
= xfrm_state_add(x
);
1471 err
= xfrm_state_update(x
);
1473 xfrm_audit_state_add(x
, err
? 0 : 1,
1474 audit_get_loginuid(current
),
1475 audit_get_sessionid(current
), 0);
1478 x
->km
.state
= XFRM_STATE_DEAD
;
1479 __xfrm_state_put(x
);
1483 if (hdr
->sadb_msg_type
== SADB_ADD
)
1484 c
.event
= XFRM_MSG_NEWSA
;
1486 c
.event
= XFRM_MSG_UPDSA
;
1487 c
.seq
= hdr
->sadb_msg_seq
;
1488 c
.pid
= hdr
->sadb_msg_pid
;
1489 km_state_notify(x
, &c
);
1495 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1497 struct xfrm_state
*x
;
1501 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1502 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1503 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1506 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1510 if ((err
= security_xfrm_state_delete(x
)))
1513 if (xfrm_state_kern(x
)) {
1518 err
= xfrm_state_delete(x
);
1523 c
.seq
= hdr
->sadb_msg_seq
;
1524 c
.pid
= hdr
->sadb_msg_pid
;
1525 c
.event
= XFRM_MSG_DELSA
;
1526 km_state_notify(x
, &c
);
1528 xfrm_audit_state_delete(x
, err
? 0 : 1,
1529 audit_get_loginuid(current
),
1530 audit_get_sessionid(current
), 0);
1536 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1539 struct sk_buff
*out_skb
;
1540 struct sadb_msg
*out_hdr
;
1541 struct xfrm_state
*x
;
1543 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1544 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1545 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1548 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1552 out_skb
= pfkey_xfrm_state2msg(x
);
1553 proto
= x
->id
.proto
;
1555 if (IS_ERR(out_skb
))
1556 return PTR_ERR(out_skb
);
1558 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1559 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1560 out_hdr
->sadb_msg_type
= SADB_GET
;
1561 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1562 out_hdr
->sadb_msg_errno
= 0;
1563 out_hdr
->sadb_msg_reserved
= 0;
1564 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1565 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1566 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
);
1571 static struct sk_buff
*compose_sadb_supported(struct sadb_msg
*orig
,
1574 struct sk_buff
*skb
;
1575 struct sadb_msg
*hdr
;
1576 int len
, auth_len
, enc_len
, i
;
1578 auth_len
= xfrm_count_auth_supported();
1580 auth_len
*= sizeof(struct sadb_alg
);
1581 auth_len
+= sizeof(struct sadb_supported
);
1584 enc_len
= xfrm_count_enc_supported();
1586 enc_len
*= sizeof(struct sadb_alg
);
1587 enc_len
+= sizeof(struct sadb_supported
);
1590 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1592 skb
= alloc_skb(len
+ 16, allocation
);
1596 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1597 pfkey_hdr_dup(hdr
, orig
);
1598 hdr
->sadb_msg_errno
= 0;
1599 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1602 struct sadb_supported
*sp
;
1603 struct sadb_alg
*ap
;
1605 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1606 ap
= (struct sadb_alg
*) (sp
+ 1);
1608 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1609 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1611 for (i
= 0; ; i
++) {
1612 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1615 if (aalg
->available
)
1621 struct sadb_supported
*sp
;
1622 struct sadb_alg
*ap
;
1624 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1625 ap
= (struct sadb_alg
*) (sp
+ 1);
1627 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1628 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1630 for (i
= 0; ; i
++) {
1631 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1634 if (ealg
->available
)
1643 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1645 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1646 struct sk_buff
*supp_skb
;
1648 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1651 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1652 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1654 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1659 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1661 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1662 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1667 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
);
1672 static int key_notify_sa_flush(struct km_event
*c
)
1674 struct sk_buff
*skb
;
1675 struct sadb_msg
*hdr
;
1677 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1680 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1681 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1682 hdr
->sadb_msg_type
= SADB_FLUSH
;
1683 hdr
->sadb_msg_seq
= c
->seq
;
1684 hdr
->sadb_msg_pid
= c
->pid
;
1685 hdr
->sadb_msg_version
= PF_KEY_V2
;
1686 hdr
->sadb_msg_errno
= (uint8_t) 0;
1687 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1689 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
1694 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1698 struct xfrm_audit audit_info
;
1701 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1705 audit_info
.loginuid
= audit_get_loginuid(current
);
1706 audit_info
.sessionid
= audit_get_sessionid(current
);
1707 audit_info
.secid
= 0;
1708 err
= xfrm_state_flush(proto
, &audit_info
);
1711 c
.data
.proto
= proto
;
1712 c
.seq
= hdr
->sadb_msg_seq
;
1713 c
.pid
= hdr
->sadb_msg_pid
;
1714 c
.event
= XFRM_MSG_FLUSHSA
;
1715 km_state_notify(NULL
, &c
);
1720 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1722 struct pfkey_sock
*pfk
= ptr
;
1723 struct sk_buff
*out_skb
;
1724 struct sadb_msg
*out_hdr
;
1726 if (!pfkey_can_dump(&pfk
->sk
))
1729 out_skb
= pfkey_xfrm_state2msg(x
);
1730 if (IS_ERR(out_skb
))
1731 return PTR_ERR(out_skb
);
1733 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1734 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1735 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1736 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1737 out_hdr
->sadb_msg_errno
= 0;
1738 out_hdr
->sadb_msg_reserved
= 0;
1739 out_hdr
->sadb_msg_seq
= count
;
1740 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
1741 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, &pfk
->sk
);
1745 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1747 return xfrm_state_walk(&pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1750 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1752 xfrm_state_walk_done(&pfk
->dump
.u
.state
);
1755 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1758 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1760 if (pfk
->dump
.dump
!= NULL
)
1763 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1767 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1768 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
1769 pfk
->dump
.dump
= pfkey_dump_sa
;
1770 pfk
->dump
.done
= pfkey_dump_sa_done
;
1771 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
);
1773 return pfkey_do_dump(pfk
);
1776 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1778 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1779 int satype
= hdr
->sadb_msg_satype
;
1781 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1782 /* XXX we mangle packet... */
1783 hdr
->sadb_msg_errno
= 0;
1784 if (satype
!= 0 && satype
!= 1)
1786 pfk
->promisc
= satype
;
1788 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
, BROADCAST_ALL
, NULL
);
1792 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1795 u32 reqid
= *(u32
*)ptr
;
1797 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1798 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1804 static u32
gen_reqid(void)
1806 struct xfrm_policy_walk walk
;
1809 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1815 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1816 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1817 rc
= xfrm_policy_walk(&walk
, check_reqid
, (void*)&reqid
);
1818 xfrm_policy_walk_done(&walk
);
1821 } while (reqid
!= start
);
1826 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1828 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1831 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1834 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1837 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1838 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1841 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1843 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1844 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1845 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1847 if (!t
->reqid
&& !(t
->reqid
= gen_reqid()))
1851 /* addresses present only in tunnel mode */
1852 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1853 u8
*sa
= (u8
*) (rq
+ 1);
1854 int family
, socklen
;
1856 family
= pfkey_sockaddr_extract((struct sockaddr
*)sa
,
1861 socklen
= pfkey_sockaddr_len(family
);
1862 if (pfkey_sockaddr_extract((struct sockaddr
*)(sa
+ socklen
),
1863 &t
->id
.daddr
) != family
)
1865 t
->encap_family
= family
;
1867 t
->encap_family
= xp
->family
;
1869 /* No way to set this via kame pfkey */
1876 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1879 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1880 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1882 while (len
>= sizeof(struct sadb_x_ipsecrequest
)) {
1883 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
1885 len
-= rq
->sadb_x_ipsecrequest_len
;
1886 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
1891 static inline int pfkey_xfrm_policy2sec_ctx_size(struct xfrm_policy
*xp
)
1893 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
1896 int len
= sizeof(struct sadb_x_sec_ctx
);
1897 len
+= xfrm_ctx
->ctx_len
;
1898 return PFKEY_ALIGN8(len
);
1903 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy
*xp
)
1905 struct xfrm_tmpl
*t
;
1906 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1910 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1911 t
= xp
->xfrm_vec
+ i
;
1912 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
1915 return sizeof(struct sadb_msg
) +
1916 (sizeof(struct sadb_lifetime
) * 3) +
1917 (sizeof(struct sadb_address
) * 2) +
1918 (sockaddr_size
* 2) +
1919 sizeof(struct sadb_x_policy
) +
1920 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
1922 pfkey_xfrm_policy2sec_ctx_size(xp
);
1925 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(struct xfrm_policy
*xp
)
1927 struct sk_buff
*skb
;
1930 size
= pfkey_xfrm_policy2msg_size(xp
);
1932 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
1934 return ERR_PTR(-ENOBUFS
);
1939 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, struct xfrm_policy
*xp
, int dir
)
1941 struct sadb_msg
*hdr
;
1942 struct sadb_address
*addr
;
1943 struct sadb_lifetime
*lifetime
;
1944 struct sadb_x_policy
*pol
;
1945 struct sadb_x_sec_ctx
*sec_ctx
;
1946 struct xfrm_sec_ctx
*xfrm_ctx
;
1949 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1950 int socklen
= pfkey_sockaddr_len(xp
->family
);
1952 size
= pfkey_xfrm_policy2msg_size(xp
);
1954 /* call should fill header later */
1955 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1956 memset(hdr
, 0, size
); /* XXX do we need this ? */
1959 addr
= (struct sadb_address
*) skb_put(skb
,
1960 sizeof(struct sadb_address
)+sockaddr_size
);
1961 addr
->sadb_address_len
=
1962 (sizeof(struct sadb_address
)+sockaddr_size
)/
1964 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1965 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
1966 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
1967 addr
->sadb_address_reserved
= 0;
1968 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
1970 (struct sockaddr
*) (addr
+ 1),
1975 addr
= (struct sadb_address
*) skb_put(skb
,
1976 sizeof(struct sadb_address
)+sockaddr_size
);
1977 addr
->sadb_address_len
=
1978 (sizeof(struct sadb_address
)+sockaddr_size
)/
1980 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1981 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
1982 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
1983 addr
->sadb_address_reserved
= 0;
1985 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
1986 (struct sockaddr
*) (addr
+ 1),
1990 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
1991 sizeof(struct sadb_lifetime
));
1992 lifetime
->sadb_lifetime_len
=
1993 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
1994 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1995 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
1996 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
1997 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
1998 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2000 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2001 sizeof(struct sadb_lifetime
));
2002 lifetime
->sadb_lifetime_len
=
2003 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2004 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2005 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2006 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2007 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2008 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2010 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2011 sizeof(struct sadb_lifetime
));
2012 lifetime
->sadb_lifetime_len
=
2013 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2014 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2015 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2016 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2017 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2018 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2020 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2021 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2022 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2023 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2024 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2026 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2028 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2030 pol
->sadb_x_policy_dir
= dir
+1;
2031 pol
->sadb_x_policy_id
= xp
->index
;
2032 pol
->sadb_x_policy_priority
= xp
->priority
;
2034 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2035 struct sadb_x_ipsecrequest
*rq
;
2036 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2040 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2041 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2042 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2043 req_size
+= socklen
* 2;
2048 rq
= (void*)skb_put(skb
, req_size
);
2049 pol
->sadb_x_policy_len
+= req_size
/8;
2050 memset(rq
, 0, sizeof(*rq
));
2051 rq
->sadb_x_ipsecrequest_len
= req_size
;
2052 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2053 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2055 rq
->sadb_x_ipsecrequest_mode
= mode
;
2056 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2058 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2060 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2061 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2063 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2064 u8
*sa
= (void *)(rq
+ 1);
2065 pfkey_sockaddr_fill(&t
->saddr
, 0,
2066 (struct sockaddr
*)sa
,
2068 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2069 (struct sockaddr
*) (sa
+ socklen
),
2074 /* security context */
2075 if ((xfrm_ctx
= xp
->security
)) {
2076 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2078 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
, ctx_size
);
2079 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2080 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2081 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2082 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2083 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2084 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2088 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2089 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
2094 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2096 struct sk_buff
*out_skb
;
2097 struct sadb_msg
*out_hdr
;
2100 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2101 if (IS_ERR(out_skb
)) {
2102 err
= PTR_ERR(out_skb
);
2105 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2109 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2110 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2112 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2113 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2115 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2116 out_hdr
->sadb_msg_errno
= 0;
2117 out_hdr
->sadb_msg_seq
= c
->seq
;
2118 out_hdr
->sadb_msg_pid
= c
->pid
;
2119 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
2125 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2128 struct sadb_lifetime
*lifetime
;
2129 struct sadb_address
*sa
;
2130 struct sadb_x_policy
*pol
;
2131 struct xfrm_policy
*xp
;
2133 struct sadb_x_sec_ctx
*sec_ctx
;
2135 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2136 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2137 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2140 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2141 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2143 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2146 xp
= xfrm_policy_alloc(GFP_KERNEL
);
2150 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2151 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2152 xp
->priority
= pol
->sadb_x_policy_priority
;
2154 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2155 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2160 xp
->selector
.family
= xp
->family
;
2161 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2162 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2163 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2164 if (xp
->selector
.sport
)
2165 xp
->selector
.sport_mask
= htons(0xffff);
2167 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2168 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2169 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2171 /* Amusing, we set this twice. KAME apps appear to set same value
2172 * in both addresses.
2174 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2176 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2177 if (xp
->selector
.dport
)
2178 xp
->selector
.dport_mask
= htons(0xffff);
2180 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2181 if (sec_ctx
!= NULL
) {
2182 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2189 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
2196 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2197 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2198 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2199 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2200 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2201 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2202 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2203 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2204 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2206 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2207 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2208 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2209 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2210 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2213 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2214 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2217 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2218 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2220 xfrm_audit_policy_add(xp
, err
? 0 : 1,
2221 audit_get_loginuid(current
),
2222 audit_get_sessionid(current
), 0);
2227 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2228 c
.event
= XFRM_MSG_UPDPOLICY
;
2230 c
.event
= XFRM_MSG_NEWPOLICY
;
2232 c
.seq
= hdr
->sadb_msg_seq
;
2233 c
.pid
= hdr
->sadb_msg_pid
;
2235 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2241 xfrm_policy_destroy(xp
);
2245 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2248 struct sadb_address
*sa
;
2249 struct sadb_x_policy
*pol
;
2250 struct xfrm_policy
*xp
;
2251 struct xfrm_selector sel
;
2253 struct sadb_x_sec_ctx
*sec_ctx
;
2254 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2256 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2257 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2258 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2261 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2262 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2265 memset(&sel
, 0, sizeof(sel
));
2267 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2268 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2269 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2270 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2271 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2273 sel
.sport_mask
= htons(0xffff);
2275 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2276 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2277 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2278 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2279 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2281 sel
.dport_mask
= htons(0xffff);
2283 sec_ctx
= (struct sadb_x_sec_ctx
*) ext_hdrs
[SADB_X_EXT_SEC_CTX
-1];
2284 if (sec_ctx
!= NULL
) {
2285 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
2290 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
);
2296 xp
= xfrm_policy_bysel_ctx(XFRM_POLICY_TYPE_MAIN
,
2297 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2299 security_xfrm_policy_free(pol_ctx
);
2303 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2304 audit_get_loginuid(current
),
2305 audit_get_sessionid(current
), 0);
2310 c
.seq
= hdr
->sadb_msg_seq
;
2311 c
.pid
= hdr
->sadb_msg_pid
;
2312 c
.event
= XFRM_MSG_DELPOLICY
;
2313 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2320 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, struct sadb_msg
*hdr
, int dir
)
2323 struct sk_buff
*out_skb
;
2324 struct sadb_msg
*out_hdr
;
2327 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2328 if (IS_ERR(out_skb
)) {
2329 err
= PTR_ERR(out_skb
);
2332 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2336 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2337 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2338 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2339 out_hdr
->sadb_msg_satype
= 0;
2340 out_hdr
->sadb_msg_errno
= 0;
2341 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2342 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2343 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
);
2350 #ifdef CONFIG_NET_KEY_MIGRATE
2351 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2353 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2356 static int parse_sockaddr_pair(struct sadb_x_ipsecrequest
*rq
,
2357 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2360 u8
*sa
= (u8
*) (rq
+ 1);
2363 if (rq
->sadb_x_ipsecrequest_len
<
2364 pfkey_sockaddr_pair_size(((struct sockaddr
*)sa
)->sa_family
))
2367 af
= pfkey_sockaddr_extract((struct sockaddr
*) sa
,
2372 socklen
= pfkey_sockaddr_len(af
);
2373 if (pfkey_sockaddr_extract((struct sockaddr
*) (sa
+ socklen
),
2381 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2382 struct xfrm_migrate
*m
)
2385 struct sadb_x_ipsecrequest
*rq2
;
2388 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2389 len
< rq1
->sadb_x_ipsecrequest_len
)
2393 err
= parse_sockaddr_pair(rq1
, &m
->old_saddr
, &m
->old_daddr
,
2398 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2399 len
-= rq1
->sadb_x_ipsecrequest_len
;
2401 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2402 len
< rq2
->sadb_x_ipsecrequest_len
)
2406 err
= parse_sockaddr_pair(rq2
, &m
->new_saddr
, &m
->new_daddr
,
2411 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2412 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2413 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2416 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2417 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2420 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2422 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2423 rq2
->sadb_x_ipsecrequest_len
));
2426 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2427 struct sadb_msg
*hdr
, void **ext_hdrs
)
2429 int i
, len
, ret
, err
= -EINVAL
;
2431 struct sadb_address
*sa
;
2432 struct sadb_x_policy
*pol
;
2433 struct sadb_x_ipsecrequest
*rq
;
2434 struct xfrm_selector sel
;
2435 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2437 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2438 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2439 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2444 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2450 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2455 dir
= pol
->sadb_x_policy_dir
- 1;
2456 memset(&sel
, 0, sizeof(sel
));
2458 /* set source address info of selector */
2459 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2460 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2461 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2462 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2463 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2465 sel
.sport_mask
= htons(0xffff);
2467 /* set destination address info of selector */
2468 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1],
2469 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2470 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2471 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2472 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2474 sel
.dport_mask
= htons(0xffff);
2476 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2478 /* extract ipsecrequests */
2480 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2482 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2483 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2488 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2494 if (!i
|| len
> 0) {
2499 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
);
2505 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2506 struct sadb_msg
*hdr
, void **ext_hdrs
)
2508 return -ENOPROTOOPT
;
2513 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2516 int err
= 0, delete;
2517 struct sadb_x_policy
*pol
;
2518 struct xfrm_policy
*xp
;
2521 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2524 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2525 if (dir
>= XFRM_POLICY_MAX
)
2528 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2529 xp
= xfrm_policy_byid(XFRM_POLICY_TYPE_MAIN
, dir
, pol
->sadb_x_policy_id
,
2535 xfrm_audit_policy_delete(xp
, err
? 0 : 1,
2536 audit_get_loginuid(current
),
2537 audit_get_sessionid(current
), 0);
2541 c
.seq
= hdr
->sadb_msg_seq
;
2542 c
.pid
= hdr
->sadb_msg_pid
;
2544 c
.event
= XFRM_MSG_DELPOLICY
;
2545 km_policy_notify(xp
, dir
, &c
);
2547 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2555 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2557 struct pfkey_sock
*pfk
= ptr
;
2558 struct sk_buff
*out_skb
;
2559 struct sadb_msg
*out_hdr
;
2562 if (!pfkey_can_dump(&pfk
->sk
))
2565 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2566 if (IS_ERR(out_skb
))
2567 return PTR_ERR(out_skb
);
2569 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2573 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2574 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2575 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2576 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2577 out_hdr
->sadb_msg_errno
= 0;
2578 out_hdr
->sadb_msg_seq
= count
;
2579 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_pid
;
2580 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, &pfk
->sk
);
2584 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2586 return xfrm_policy_walk(&pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2589 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2591 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
);
2594 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2596 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2598 if (pfk
->dump
.dump
!= NULL
)
2601 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2602 pfk
->dump
.msg_pid
= hdr
->sadb_msg_pid
;
2603 pfk
->dump
.dump
= pfkey_dump_sp
;
2604 pfk
->dump
.done
= pfkey_dump_sp_done
;
2605 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2607 return pfkey_do_dump(pfk
);
2610 static int key_notify_policy_flush(struct km_event
*c
)
2612 struct sk_buff
*skb_out
;
2613 struct sadb_msg
*hdr
;
2615 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2618 hdr
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2619 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2620 hdr
->sadb_msg_seq
= c
->seq
;
2621 hdr
->sadb_msg_pid
= c
->pid
;
2622 hdr
->sadb_msg_version
= PF_KEY_V2
;
2623 hdr
->sadb_msg_errno
= (uint8_t) 0;
2624 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2625 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
2630 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2633 struct xfrm_audit audit_info
;
2636 audit_info
.loginuid
= audit_get_loginuid(current
);
2637 audit_info
.sessionid
= audit_get_sessionid(current
);
2638 audit_info
.secid
= 0;
2639 err
= xfrm_policy_flush(XFRM_POLICY_TYPE_MAIN
, &audit_info
);
2642 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2643 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2644 c
.pid
= hdr
->sadb_msg_pid
;
2645 c
.seq
= hdr
->sadb_msg_seq
;
2646 km_policy_notify(NULL
, 0, &c
);
2651 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2652 struct sadb_msg
*hdr
, void **ext_hdrs
);
2653 static pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2654 [SADB_RESERVED
] = pfkey_reserved
,
2655 [SADB_GETSPI
] = pfkey_getspi
,
2656 [SADB_UPDATE
] = pfkey_add
,
2657 [SADB_ADD
] = pfkey_add
,
2658 [SADB_DELETE
] = pfkey_delete
,
2659 [SADB_GET
] = pfkey_get
,
2660 [SADB_ACQUIRE
] = pfkey_acquire
,
2661 [SADB_REGISTER
] = pfkey_register
,
2662 [SADB_EXPIRE
] = NULL
,
2663 [SADB_FLUSH
] = pfkey_flush
,
2664 [SADB_DUMP
] = pfkey_dump
,
2665 [SADB_X_PROMISC
] = pfkey_promisc
,
2666 [SADB_X_PCHANGE
] = NULL
,
2667 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2668 [SADB_X_SPDADD
] = pfkey_spdadd
,
2669 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2670 [SADB_X_SPDGET
] = pfkey_spdget
,
2671 [SADB_X_SPDACQUIRE
] = NULL
,
2672 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2673 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2674 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2675 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2676 [SADB_X_MIGRATE
] = pfkey_migrate
,
2679 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
)
2681 void *ext_hdrs
[SADB_EXT_MAX
];
2684 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2685 BROADCAST_PROMISC_ONLY
, NULL
);
2687 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2688 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2691 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2692 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2697 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2699 struct sadb_msg
*hdr
= NULL
;
2701 if (skb
->len
< sizeof(*hdr
)) {
2704 hdr
= (struct sadb_msg
*) skb
->data
;
2705 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2706 hdr
->sadb_msg_reserved
!= 0 ||
2707 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2708 hdr
->sadb_msg_type
> SADB_MAX
)) {
2711 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2712 sizeof(uint64_t)) ||
2713 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2714 sizeof(uint64_t))) {
2724 static inline int aalg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2726 unsigned int id
= d
->desc
.sadb_alg_id
;
2728 if (id
>= sizeof(t
->aalgos
) * 8)
2731 return (t
->aalgos
>> id
) & 1;
2734 static inline int ealg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2736 unsigned int id
= d
->desc
.sadb_alg_id
;
2738 if (id
>= sizeof(t
->ealgos
) * 8)
2741 return (t
->ealgos
>> id
) & 1;
2744 static int count_ah_combs(struct xfrm_tmpl
*t
)
2748 for (i
= 0; ; i
++) {
2749 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2752 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2753 sz
+= sizeof(struct sadb_comb
);
2755 return sz
+ sizeof(struct sadb_prop
);
2758 static int count_esp_combs(struct xfrm_tmpl
*t
)
2762 for (i
= 0; ; i
++) {
2763 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2767 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2770 for (k
= 1; ; k
++) {
2771 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2775 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2776 sz
+= sizeof(struct sadb_comb
);
2779 return sz
+ sizeof(struct sadb_prop
);
2782 static void dump_ah_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2784 struct sadb_prop
*p
;
2787 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2788 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2789 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2790 p
->sadb_prop_replay
= 32;
2791 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2793 for (i
= 0; ; i
++) {
2794 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2798 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2799 struct sadb_comb
*c
;
2800 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2801 memset(c
, 0, sizeof(*c
));
2802 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2803 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2804 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2805 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2806 c
->sadb_comb_hard_addtime
= 24*60*60;
2807 c
->sadb_comb_soft_addtime
= 20*60*60;
2808 c
->sadb_comb_hard_usetime
= 8*60*60;
2809 c
->sadb_comb_soft_usetime
= 7*60*60;
2814 static void dump_esp_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2816 struct sadb_prop
*p
;
2819 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2820 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2821 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2822 p
->sadb_prop_replay
= 32;
2823 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2826 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2830 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2833 for (k
= 1; ; k
++) {
2834 struct sadb_comb
*c
;
2835 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2838 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2840 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2841 memset(c
, 0, sizeof(*c
));
2842 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2843 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2844 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2845 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2846 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
2847 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
2848 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
2849 c
->sadb_comb_hard_addtime
= 24*60*60;
2850 c
->sadb_comb_soft_addtime
= 20*60*60;
2851 c
->sadb_comb_hard_usetime
= 8*60*60;
2852 c
->sadb_comb_soft_usetime
= 7*60*60;
2857 static int key_notify_policy_expire(struct xfrm_policy
*xp
, struct km_event
*c
)
2862 static int key_notify_sa_expire(struct xfrm_state
*x
, struct km_event
*c
)
2864 struct sk_buff
*out_skb
;
2865 struct sadb_msg
*out_hdr
;
2869 hard
= c
->data
.hard
;
2875 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
2876 if (IS_ERR(out_skb
))
2877 return PTR_ERR(out_skb
);
2879 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2880 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2881 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
2882 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2883 out_hdr
->sadb_msg_errno
= 0;
2884 out_hdr
->sadb_msg_reserved
= 0;
2885 out_hdr
->sadb_msg_seq
= 0;
2886 out_hdr
->sadb_msg_pid
= 0;
2888 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
2892 static int pfkey_send_notify(struct xfrm_state
*x
, struct km_event
*c
)
2894 if (atomic_read(&pfkey_socks_nr
) == 0)
2898 case XFRM_MSG_EXPIRE
:
2899 return key_notify_sa_expire(x
, c
);
2900 case XFRM_MSG_DELSA
:
2901 case XFRM_MSG_NEWSA
:
2902 case XFRM_MSG_UPDSA
:
2903 return key_notify_sa(x
, c
);
2904 case XFRM_MSG_FLUSHSA
:
2905 return key_notify_sa_flush(c
);
2906 case XFRM_MSG_NEWAE
: /* not yet supported */
2909 printk("pfkey: Unknown SA event %d\n", c
->event
);
2916 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, struct km_event
*c
)
2918 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
2922 case XFRM_MSG_POLEXPIRE
:
2923 return key_notify_policy_expire(xp
, c
);
2924 case XFRM_MSG_DELPOLICY
:
2925 case XFRM_MSG_NEWPOLICY
:
2926 case XFRM_MSG_UPDPOLICY
:
2927 return key_notify_policy(xp
, dir
, c
);
2928 case XFRM_MSG_FLUSHPOLICY
:
2929 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
2931 return key_notify_policy_flush(c
);
2933 printk("pfkey: Unknown policy event %d\n", c
->event
);
2940 static u32
get_acqseq(void)
2944 static DEFINE_SPINLOCK(acqseq_lock
);
2946 spin_lock_bh(&acqseq_lock
);
2947 res
= (++acqseq
? : ++acqseq
);
2948 spin_unlock_bh(&acqseq_lock
);
2952 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
, int dir
)
2954 struct sk_buff
*skb
;
2955 struct sadb_msg
*hdr
;
2956 struct sadb_address
*addr
;
2957 struct sadb_x_policy
*pol
;
2960 struct sadb_x_sec_ctx
*sec_ctx
;
2961 struct xfrm_sec_ctx
*xfrm_ctx
;
2964 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
2968 size
= sizeof(struct sadb_msg
) +
2969 (sizeof(struct sadb_address
) * 2) +
2970 (sockaddr_size
* 2) +
2971 sizeof(struct sadb_x_policy
);
2973 if (x
->id
.proto
== IPPROTO_AH
)
2974 size
+= count_ah_combs(t
);
2975 else if (x
->id
.proto
== IPPROTO_ESP
)
2976 size
+= count_esp_combs(t
);
2978 if ((xfrm_ctx
= x
->security
)) {
2979 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
2980 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
2983 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2987 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2988 hdr
->sadb_msg_version
= PF_KEY_V2
;
2989 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
2990 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2991 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2992 hdr
->sadb_msg_errno
= 0;
2993 hdr
->sadb_msg_reserved
= 0;
2994 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
2995 hdr
->sadb_msg_pid
= 0;
2998 addr
= (struct sadb_address
*) skb_put(skb
,
2999 sizeof(struct sadb_address
)+sockaddr_size
);
3000 addr
->sadb_address_len
=
3001 (sizeof(struct sadb_address
)+sockaddr_size
)/
3003 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3004 addr
->sadb_address_proto
= 0;
3005 addr
->sadb_address_reserved
= 0;
3006 addr
->sadb_address_prefixlen
=
3007 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3008 (struct sockaddr
*) (addr
+ 1),
3010 if (!addr
->sadb_address_prefixlen
)
3014 addr
= (struct sadb_address
*) skb_put(skb
,
3015 sizeof(struct sadb_address
)+sockaddr_size
);
3016 addr
->sadb_address_len
=
3017 (sizeof(struct sadb_address
)+sockaddr_size
)/
3019 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3020 addr
->sadb_address_proto
= 0;
3021 addr
->sadb_address_reserved
= 0;
3022 addr
->sadb_address_prefixlen
=
3023 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3024 (struct sockaddr
*) (addr
+ 1),
3026 if (!addr
->sadb_address_prefixlen
)
3029 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
3030 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3031 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3032 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3033 pol
->sadb_x_policy_dir
= dir
+1;
3034 pol
->sadb_x_policy_id
= xp
->index
;
3036 /* Set sadb_comb's. */
3037 if (x
->id
.proto
== IPPROTO_AH
)
3038 dump_ah_combs(skb
, t
);
3039 else if (x
->id
.proto
== IPPROTO_ESP
)
3040 dump_esp_combs(skb
, t
);
3042 /* security context */
3044 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
3045 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3046 sec_ctx
->sadb_x_sec_len
=
3047 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3048 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3049 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3050 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3051 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3052 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3056 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
3059 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3060 u8
*data
, int len
, int *dir
)
3062 struct xfrm_policy
*xp
;
3063 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3064 struct sadb_x_sec_ctx
*sec_ctx
;
3066 switch (sk
->sk_family
) {
3068 if (opt
!= IP_IPSEC_POLICY
) {
3073 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3075 if (opt
!= IPV6_IPSEC_POLICY
) {
3088 if (len
< sizeof(struct sadb_x_policy
) ||
3089 pol
->sadb_x_policy_len
*8 > len
||
3090 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3091 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3094 xp
= xfrm_policy_alloc(GFP_ATOMIC
);
3100 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3101 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3103 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3104 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3105 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3106 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3107 xp
->family
= sk
->sk_family
;
3110 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3111 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3114 /* security context too */
3115 if (len
>= (pol
->sadb_x_policy_len
*8 +
3116 sizeof(struct sadb_x_sec_ctx
))) {
3117 char *p
= (char *)pol
;
3118 struct xfrm_user_sec_ctx
*uctx
;
3120 p
+= pol
->sadb_x_policy_len
*8;
3121 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3122 if (len
< pol
->sadb_x_policy_len
*8 +
3123 sec_ctx
->sadb_x_sec_len
) {
3127 if ((*dir
= verify_sec_ctx_len(p
)))
3129 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
);
3130 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
);
3137 *dir
= pol
->sadb_x_policy_dir
-1;
3141 xfrm_policy_destroy(xp
);
3145 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3147 struct sk_buff
*skb
;
3148 struct sadb_msg
*hdr
;
3150 struct sadb_address
*addr
;
3151 struct sadb_x_nat_t_port
*n_port
;
3154 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3155 struct xfrm_encap_tmpl
*natt
= NULL
;
3157 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3169 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3171 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3172 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3175 size
= sizeof(struct sadb_msg
) +
3176 sizeof(struct sadb_sa
) +
3177 (sizeof(struct sadb_address
) * 2) +
3178 (sockaddr_size
* 2) +
3179 (sizeof(struct sadb_x_nat_t_port
) * 2);
3181 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3185 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3186 hdr
->sadb_msg_version
= PF_KEY_V2
;
3187 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3188 hdr
->sadb_msg_satype
= satype
;
3189 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3190 hdr
->sadb_msg_errno
= 0;
3191 hdr
->sadb_msg_reserved
= 0;
3192 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3193 hdr
->sadb_msg_pid
= 0;
3196 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
3197 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3198 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3199 sa
->sadb_sa_spi
= x
->id
.spi
;
3200 sa
->sadb_sa_replay
= 0;
3201 sa
->sadb_sa_state
= 0;
3202 sa
->sadb_sa_auth
= 0;
3203 sa
->sadb_sa_encrypt
= 0;
3204 sa
->sadb_sa_flags
= 0;
3206 /* ADDRESS_SRC (old addr) */
3207 addr
= (struct sadb_address
*)
3208 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3209 addr
->sadb_address_len
=
3210 (sizeof(struct sadb_address
)+sockaddr_size
)/
3212 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3213 addr
->sadb_address_proto
= 0;
3214 addr
->sadb_address_reserved
= 0;
3215 addr
->sadb_address_prefixlen
=
3216 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3217 (struct sockaddr
*) (addr
+ 1),
3219 if (!addr
->sadb_address_prefixlen
)
3222 /* NAT_T_SPORT (old port) */
3223 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3224 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3225 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3226 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3227 n_port
->sadb_x_nat_t_port_reserved
= 0;
3229 /* ADDRESS_DST (new addr) */
3230 addr
= (struct sadb_address
*)
3231 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3232 addr
->sadb_address_len
=
3233 (sizeof(struct sadb_address
)+sockaddr_size
)/
3235 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3236 addr
->sadb_address_proto
= 0;
3237 addr
->sadb_address_reserved
= 0;
3238 addr
->sadb_address_prefixlen
=
3239 pfkey_sockaddr_fill(ipaddr
, 0,
3240 (struct sockaddr
*) (addr
+ 1),
3242 if (!addr
->sadb_address_prefixlen
)
3245 /* NAT_T_DPORT (new port) */
3246 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3247 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3248 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3249 n_port
->sadb_x_nat_t_port_port
= sport
;
3250 n_port
->sadb_x_nat_t_port_reserved
= 0;
3252 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
3255 #ifdef CONFIG_NET_KEY_MIGRATE
3256 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3257 struct xfrm_selector
*sel
)
3259 struct sadb_address
*addr
;
3260 addr
= (struct sadb_address
*)skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3261 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3262 addr
->sadb_address_exttype
= type
;
3263 addr
->sadb_address_proto
= sel
->proto
;
3264 addr
->sadb_address_reserved
= 0;
3267 case SADB_EXT_ADDRESS_SRC
:
3268 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3269 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3270 (struct sockaddr
*)(addr
+ 1),
3273 case SADB_EXT_ADDRESS_DST
:
3274 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3275 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3276 (struct sockaddr
*)(addr
+ 1),
3286 static int set_ipsecrequest(struct sk_buff
*skb
,
3287 uint8_t proto
, uint8_t mode
, int level
,
3288 uint32_t reqid
, uint8_t family
,
3289 xfrm_address_t
*src
, xfrm_address_t
*dst
)
3291 struct sadb_x_ipsecrequest
*rq
;
3293 int socklen
= pfkey_sockaddr_len(family
);
3296 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3297 pfkey_sockaddr_pair_size(family
);
3299 rq
= (struct sadb_x_ipsecrequest
*)skb_put(skb
, size_req
);
3300 memset(rq
, 0, size_req
);
3301 rq
->sadb_x_ipsecrequest_len
= size_req
;
3302 rq
->sadb_x_ipsecrequest_proto
= proto
;
3303 rq
->sadb_x_ipsecrequest_mode
= mode
;
3304 rq
->sadb_x_ipsecrequest_level
= level
;
3305 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3307 sa
= (u8
*) (rq
+ 1);
3308 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3309 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3316 #ifdef CONFIG_NET_KEY_MIGRATE
3317 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3318 struct xfrm_migrate
*m
, int num_bundles
)
3324 struct sk_buff
*skb
;
3325 struct sadb_msg
*hdr
;
3326 struct sadb_x_policy
*pol
;
3327 struct xfrm_migrate
*mp
;
3329 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3332 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3336 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3339 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3342 size_pol
+= sizeof(struct sadb_x_policy
);
3345 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3346 /* old locator pair */
3347 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3348 pfkey_sockaddr_pair_size(mp
->old_family
);
3349 /* new locator pair */
3350 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3351 pfkey_sockaddr_pair_size(mp
->new_family
);
3354 size
+= sizeof(struct sadb_msg
) + size_pol
;
3357 skb
= alloc_skb(size
, GFP_ATOMIC
);
3361 hdr
= (struct sadb_msg
*)skb_put(skb
, sizeof(struct sadb_msg
));
3362 hdr
->sadb_msg_version
= PF_KEY_V2
;
3363 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3364 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3365 hdr
->sadb_msg_len
= size
/ 8;
3366 hdr
->sadb_msg_errno
= 0;
3367 hdr
->sadb_msg_reserved
= 0;
3368 hdr
->sadb_msg_seq
= 0;
3369 hdr
->sadb_msg_pid
= 0;
3372 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3375 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3377 /* policy information */
3378 pol
= (struct sadb_x_policy
*)skb_put(skb
, sizeof(struct sadb_x_policy
));
3379 pol
->sadb_x_policy_len
= size_pol
/ 8;
3380 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3381 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3382 pol
->sadb_x_policy_dir
= dir
+ 1;
3383 pol
->sadb_x_policy_id
= 0;
3384 pol
->sadb_x_policy_priority
= 0;
3386 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3387 /* old ipsecrequest */
3388 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3391 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3392 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3393 mp
->reqid
, mp
->old_family
,
3394 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3397 /* new ipsecrequest */
3398 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3399 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3400 mp
->reqid
, mp
->new_family
,
3401 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3405 /* broadcast migrate message to sockets */
3406 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
);
3415 static int pfkey_send_migrate(struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3416 struct xfrm_migrate
*m
, int num_bundles
)
3418 return -ENOPROTOOPT
;
3422 static int pfkey_sendmsg(struct kiocb
*kiocb
,
3423 struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3425 struct sock
*sk
= sock
->sk
;
3426 struct sk_buff
*skb
= NULL
;
3427 struct sadb_msg
*hdr
= NULL
;
3431 if (msg
->msg_flags
& MSG_OOB
)
3435 if ((unsigned)len
> sk
->sk_sndbuf
- 32)
3439 skb
= alloc_skb(len
, GFP_KERNEL
);
3444 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
))
3447 hdr
= pfkey_get_base_msg(skb
, &err
);
3451 mutex_lock(&xfrm_cfg_mutex
);
3452 err
= pfkey_process(sk
, skb
, hdr
);
3453 mutex_unlock(&xfrm_cfg_mutex
);
3456 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3464 static int pfkey_recvmsg(struct kiocb
*kiocb
,
3465 struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3468 struct sock
*sk
= sock
->sk
;
3469 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3470 struct sk_buff
*skb
;
3474 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3477 msg
->msg_namelen
= 0;
3478 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3484 msg
->msg_flags
|= MSG_TRUNC
;
3488 skb_reset_transport_header(skb
);
3489 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
3493 sock_recv_timestamp(msg
, sk
, skb
);
3495 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3497 if (pfk
->dump
.dump
!= NULL
&&
3498 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3502 skb_free_datagram(sk
, skb
);
3507 static const struct proto_ops pfkey_ops
= {
3509 .owner
= THIS_MODULE
,
3510 /* Operations that make no sense on pfkey sockets. */
3511 .bind
= sock_no_bind
,
3512 .connect
= sock_no_connect
,
3513 .socketpair
= sock_no_socketpair
,
3514 .accept
= sock_no_accept
,
3515 .getname
= sock_no_getname
,
3516 .ioctl
= sock_no_ioctl
,
3517 .listen
= sock_no_listen
,
3518 .shutdown
= sock_no_shutdown
,
3519 .setsockopt
= sock_no_setsockopt
,
3520 .getsockopt
= sock_no_getsockopt
,
3521 .mmap
= sock_no_mmap
,
3522 .sendpage
= sock_no_sendpage
,
3524 /* Now the operations that really occur. */
3525 .release
= pfkey_release
,
3526 .poll
= datagram_poll
,
3527 .sendmsg
= pfkey_sendmsg
,
3528 .recvmsg
= pfkey_recvmsg
,
3531 static struct net_proto_family pfkey_family_ops
= {
3533 .create
= pfkey_create
,
3534 .owner
= THIS_MODULE
,
3537 #ifdef CONFIG_PROC_FS
3538 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3542 s
= (struct sock
*)v
;
3543 if (v
== SEQ_START_TOKEN
)
3544 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3546 seq_printf(f
,"%p %-6d %-6u %-6u %-6u %-6lu\n",
3548 atomic_read(&s
->sk_refcnt
),
3549 atomic_read(&s
->sk_rmem_alloc
),
3550 atomic_read(&s
->sk_wmem_alloc
),
3557 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3560 struct hlist_node
*node
;
3563 read_lock(&pfkey_table_lock
);
3565 return SEQ_START_TOKEN
;
3567 sk_for_each(s
, node
, &pfkey_table
)
3574 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3577 return (v
== SEQ_START_TOKEN
) ?
3578 sk_head(&pfkey_table
) :
3579 sk_next((struct sock
*)v
);
3582 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3584 read_unlock(&pfkey_table_lock
);
3587 static struct seq_operations pfkey_seq_ops
= {
3588 .start
= pfkey_seq_start
,
3589 .next
= pfkey_seq_next
,
3590 .stop
= pfkey_seq_stop
,
3591 .show
= pfkey_seq_show
,
3594 static int pfkey_seq_open(struct inode
*inode
, struct file
*file
)
3596 return seq_open(file
, &pfkey_seq_ops
);
3599 static struct file_operations pfkey_proc_ops
= {
3600 .open
= pfkey_seq_open
,
3602 .llseek
= seq_lseek
,
3603 .release
= seq_release
,
3606 static int pfkey_init_proc(void)
3608 struct proc_dir_entry
*e
;
3610 e
= proc_net_fops_create(&init_net
, "pfkey", 0, &pfkey_proc_ops
);
3617 static void pfkey_exit_proc(void)
3619 proc_net_remove(&init_net
, "pfkey");
3622 static inline int pfkey_init_proc(void)
3627 static inline void pfkey_exit_proc(void)
3632 static struct xfrm_mgr pfkeyv2_mgr
=
3635 .notify
= pfkey_send_notify
,
3636 .acquire
= pfkey_send_acquire
,
3637 .compile_policy
= pfkey_compile_policy
,
3638 .new_mapping
= pfkey_send_new_mapping
,
3639 .notify_policy
= pfkey_send_policy_notify
,
3640 .migrate
= pfkey_send_migrate
,
3643 static void __exit
ipsec_pfkey_exit(void)
3645 xfrm_unregister_km(&pfkeyv2_mgr
);
3647 sock_unregister(PF_KEY
);
3648 proto_unregister(&key_proto
);
3651 static int __init
ipsec_pfkey_init(void)
3653 int err
= proto_register(&key_proto
, 0);
3658 err
= sock_register(&pfkey_family_ops
);
3660 goto out_unregister_key_proto
;
3661 err
= pfkey_init_proc();
3663 goto out_sock_unregister
;
3664 err
= xfrm_register_km(&pfkeyv2_mgr
);
3666 goto out_remove_proc_entry
;
3669 out_remove_proc_entry
:
3671 out_sock_unregister
:
3672 sock_unregister(PF_KEY
);
3673 out_unregister_key_proto
:
3674 proto_unregister(&key_proto
);
3678 module_init(ipsec_pfkey_init
);
3679 module_exit(ipsec_pfkey_exit
);
3680 MODULE_LICENSE("GPL");
3681 MODULE_ALIAS_NETPROTO(PF_KEY
);