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/config.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>
33 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
34 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
37 /* List of all pfkey sockets. */
38 static HLIST_HEAD(pfkey_table
);
39 static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait
);
40 static DEFINE_RWLOCK(pfkey_table_lock
);
41 static atomic_t pfkey_table_users
= ATOMIC_INIT(0);
43 static atomic_t pfkey_socks_nr
= ATOMIC_INIT(0);
46 /* struct sock must be the first member of struct pfkey_sock */
52 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
54 return (struct pfkey_sock
*)sk
;
57 static void pfkey_sock_destruct(struct sock
*sk
)
59 skb_queue_purge(&sk
->sk_receive_queue
);
61 if (!sock_flag(sk
, SOCK_DEAD
)) {
62 printk("Attempt to release alive pfkey socket: %p\n", sk
);
66 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
67 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
69 atomic_dec(&pfkey_socks_nr
);
72 static void pfkey_table_grab(void)
74 write_lock_bh(&pfkey_table_lock
);
76 if (atomic_read(&pfkey_table_users
)) {
77 DECLARE_WAITQUEUE(wait
, current
);
79 add_wait_queue_exclusive(&pfkey_table_wait
, &wait
);
81 set_current_state(TASK_UNINTERRUPTIBLE
);
82 if (atomic_read(&pfkey_table_users
) == 0)
84 write_unlock_bh(&pfkey_table_lock
);
86 write_lock_bh(&pfkey_table_lock
);
89 __set_current_state(TASK_RUNNING
);
90 remove_wait_queue(&pfkey_table_wait
, &wait
);
94 static __inline__
void pfkey_table_ungrab(void)
96 write_unlock_bh(&pfkey_table_lock
);
97 wake_up(&pfkey_table_wait
);
100 static __inline__
void pfkey_lock_table(void)
102 /* read_lock() synchronizes us to pfkey_table_grab */
104 read_lock(&pfkey_table_lock
);
105 atomic_inc(&pfkey_table_users
);
106 read_unlock(&pfkey_table_lock
);
109 static __inline__
void pfkey_unlock_table(void)
111 if (atomic_dec_and_test(&pfkey_table_users
))
112 wake_up(&pfkey_table_wait
);
116 static struct proto_ops pfkey_ops
;
118 static void pfkey_insert(struct sock
*sk
)
121 sk_add_node(sk
, &pfkey_table
);
122 pfkey_table_ungrab();
125 static void pfkey_remove(struct sock
*sk
)
128 sk_del_node_init(sk
);
129 pfkey_table_ungrab();
132 static struct proto key_proto
= {
134 .owner
= THIS_MODULE
,
135 .obj_size
= sizeof(struct pfkey_sock
),
138 static int pfkey_create(struct socket
*sock
, int protocol
)
143 if (!capable(CAP_NET_ADMIN
))
145 if (sock
->type
!= SOCK_RAW
)
146 return -ESOCKTNOSUPPORT
;
147 if (protocol
!= PF_KEY_V2
)
148 return -EPROTONOSUPPORT
;
151 sk
= sk_alloc(PF_KEY
, GFP_KERNEL
, &key_proto
, 1);
155 sock
->ops
= &pfkey_ops
;
156 sock_init_data(sock
, sk
);
158 sk
->sk_family
= PF_KEY
;
159 sk
->sk_destruct
= pfkey_sock_destruct
;
161 atomic_inc(&pfkey_socks_nr
);
170 static int pfkey_release(struct socket
*sock
)
172 struct sock
*sk
= sock
->sk
;
181 skb_queue_purge(&sk
->sk_write_queue
);
187 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
188 int allocation
, struct sock
*sk
)
194 if (atomic_read(&skb
->users
) != 1) {
195 *skb2
= skb_clone(skb
, allocation
);
198 atomic_inc(&skb
->users
);
202 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
204 skb_set_owner_r(*skb2
, sk
);
205 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
206 sk
->sk_data_ready(sk
, (*skb2
)->len
);
215 /* Send SKB to all pfkey sockets matching selected criteria. */
216 #define BROADCAST_ALL 0
217 #define BROADCAST_ONE 1
218 #define BROADCAST_REGISTERED 2
219 #define BROADCAST_PROMISC_ONLY 4
220 static int pfkey_broadcast(struct sk_buff
*skb
, int allocation
,
221 int broadcast_flags
, struct sock
*one_sk
)
224 struct hlist_node
*node
;
225 struct sk_buff
*skb2
= NULL
;
228 /* XXX Do we need something like netlink_overrun? I think
229 * XXX PF_KEY socket apps will not mind current behavior.
235 sk_for_each(sk
, node
, &pfkey_table
) {
236 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
239 /* Yes, it means that if you are meant to receive this
240 * pfkey message you receive it twice as promiscuous
244 pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
246 /* the exact target will be processed later */
249 if (broadcast_flags
!= BROADCAST_ALL
) {
250 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
252 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
255 if (broadcast_flags
& BROADCAST_ONE
)
259 err2
= pfkey_broadcast_one(skb
, &skb2
, allocation
, sk
);
261 /* Error is cleare after succecful sending to at least one
263 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
266 pfkey_unlock_table();
269 err
= pfkey_broadcast_one(skb
, &skb2
, allocation
, one_sk
);
277 static inline void pfkey_hdr_dup(struct sadb_msg
*new, struct sadb_msg
*orig
)
282 static int pfkey_error(struct sadb_msg
*orig
, int err
, struct sock
*sk
)
284 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
285 struct sadb_msg
*hdr
;
290 /* Woe be to the platform trying to support PFKEY yet
291 * having normal errnos outside the 1-255 range, inclusive.
294 if (err
== ERESTARTSYS
||
295 err
== ERESTARTNOHAND
||
296 err
== ERESTARTNOINTR
)
300 if (err
<= 0 || err
>= 256)
303 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
304 pfkey_hdr_dup(hdr
, orig
);
305 hdr
->sadb_msg_errno
= (uint8_t) err
;
306 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
309 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
314 static u8 sadb_ext_min_len
[] = {
315 [SADB_EXT_RESERVED
] = (u8
) 0,
316 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
317 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
318 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
319 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
320 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
321 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
322 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
323 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
324 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
325 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
326 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
327 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
328 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
329 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
330 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
331 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
332 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
333 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
334 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
335 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
336 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
337 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
338 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
341 /* Verify sadb_address_{len,prefixlen} against sa_family. */
342 static int verify_address_len(void *p
)
344 struct sadb_address
*sp
= p
;
345 struct sockaddr
*addr
= (struct sockaddr
*)(sp
+ 1);
346 struct sockaddr_in
*sin
;
347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
348 struct sockaddr_in6
*sin6
;
352 switch (addr
->sa_family
) {
354 len
= sizeof(*sp
) + sizeof(*sin
) + (sizeof(uint64_t) - 1);
355 len
/= sizeof(uint64_t);
356 if (sp
->sadb_address_len
!= len
||
357 sp
->sadb_address_prefixlen
> 32)
360 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
362 len
= sizeof(*sp
) + sizeof(*sin6
) + (sizeof(uint64_t) - 1);
363 len
/= sizeof(uint64_t);
364 if (sp
->sadb_address_len
!= len
||
365 sp
->sadb_address_prefixlen
> 128)
370 /* It is user using kernel to keep track of security
371 * associations for another protocol, such as
372 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
375 * XXX Actually, association/policy database is not yet
376 * XXX able to cope with arbitrary sockaddr families.
377 * XXX When it can, remove this -EINVAL. -DaveM
386 static int present_and_same_family(struct sadb_address
*src
,
387 struct sadb_address
*dst
)
389 struct sockaddr
*s_addr
, *d_addr
;
394 s_addr
= (struct sockaddr
*)(src
+ 1);
395 d_addr
= (struct sockaddr
*)(dst
+ 1);
396 if (s_addr
->sa_family
!= d_addr
->sa_family
)
398 if (s_addr
->sa_family
!= AF_INET
399 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
400 && s_addr
->sa_family
!= AF_INET6
408 static int parse_exthdrs(struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
410 char *p
= (char *) hdr
;
416 struct sadb_ext
*ehdr
= (struct sadb_ext
*) p
;
420 ext_len
= ehdr
->sadb_ext_len
;
421 ext_len
*= sizeof(uint64_t);
422 ext_type
= ehdr
->sadb_ext_type
;
423 if (ext_len
< sizeof(uint64_t) ||
425 ext_type
== SADB_EXT_RESERVED
)
428 if (ext_type
<= SADB_EXT_MAX
) {
429 int min
= (int) sadb_ext_min_len
[ext_type
];
432 if (ext_hdrs
[ext_type
-1] != NULL
)
434 if (ext_type
== SADB_EXT_ADDRESS_SRC
||
435 ext_type
== SADB_EXT_ADDRESS_DST
||
436 ext_type
== SADB_EXT_ADDRESS_PROXY
||
437 ext_type
== SADB_X_EXT_NAT_T_OA
) {
438 if (verify_address_len(p
))
441 ext_hdrs
[ext_type
-1] = p
;
451 pfkey_satype2proto(uint8_t satype
)
454 case SADB_SATYPE_UNSPEC
:
455 return IPSEC_PROTO_ANY
;
458 case SADB_SATYPE_ESP
:
460 case SADB_X_SATYPE_IPCOMP
:
470 pfkey_proto2satype(uint16_t proto
)
474 return SADB_SATYPE_AH
;
476 return SADB_SATYPE_ESP
;
478 return SADB_X_SATYPE_IPCOMP
;
486 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
487 * say specifically 'just raw sockets' as we encode them as 255.
490 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
492 return (proto
== IPSEC_PROTO_ANY
? 0 : proto
);
495 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
497 return (proto
? proto
: IPSEC_PROTO_ANY
);
500 static int pfkey_sadb_addr2xfrm_addr(struct sadb_address
*addr
,
501 xfrm_address_t
*xaddr
)
503 switch (((struct sockaddr
*)(addr
+ 1))->sa_family
) {
506 ((struct sockaddr_in
*)(addr
+ 1))->sin_addr
.s_addr
;
508 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
511 &((struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
,
512 sizeof(struct in6_addr
));
521 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct sadb_msg
*hdr
, void **ext_hdrs
)
524 struct sadb_address
*addr
;
526 unsigned short family
;
527 xfrm_address_t
*xaddr
;
529 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
533 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
537 /* sadb_address_len should be checked by caller */
538 addr
= (struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
542 family
= ((struct sockaddr
*)(addr
+ 1))->sa_family
;
545 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
547 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
549 xaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
559 return xfrm_state_lookup(xaddr
, sa
->sadb_sa_spi
, proto
, family
);
562 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
564 pfkey_sockaddr_size(sa_family_t family
)
568 return PFKEY_ALIGN8(sizeof(struct sockaddr_in
));
569 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
571 return PFKEY_ALIGN8(sizeof(struct sockaddr_in6
));
579 static struct sk_buff
* pfkey_xfrm_state2msg(struct xfrm_state
*x
, int add_keys
, int hsc
)
582 struct sadb_msg
*hdr
;
584 struct sadb_lifetime
*lifetime
;
585 struct sadb_address
*addr
;
586 struct sadb_key
*key
;
587 struct sadb_x_sa2
*sa2
;
588 struct sockaddr_in
*sin
;
589 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
590 struct sockaddr_in6
*sin6
;
593 int auth_key_size
= 0;
594 int encrypt_key_size
= 0;
596 struct xfrm_encap_tmpl
*natt
= NULL
;
598 /* address family check */
599 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
601 return ERR_PTR(-EINVAL
);
603 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
604 key(AE), (identity(SD),) (sensitivity)> */
605 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
606 sizeof(struct sadb_lifetime
) +
607 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
608 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
609 sizeof(struct sadb_address
)*2 +
611 sizeof(struct sadb_x_sa2
);
612 /* identity & sensitivity */
614 if ((x
->props
.family
== AF_INET
&&
615 x
->sel
.saddr
.a4
!= x
->props
.saddr
.a4
)
616 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
617 || (x
->props
.family
== AF_INET6
&&
618 memcmp (x
->sel
.saddr
.a6
, x
->props
.saddr
.a6
, sizeof (struct in6_addr
)))
621 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
624 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
626 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
627 size
+= sizeof(struct sadb_key
) + auth_key_size
;
629 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
631 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
632 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
638 if (natt
&& natt
->encap_type
) {
639 size
+= sizeof(struct sadb_x_nat_t_type
);
640 size
+= sizeof(struct sadb_x_nat_t_port
);
641 size
+= sizeof(struct sadb_x_nat_t_port
);
644 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
646 return ERR_PTR(-ENOBUFS
);
648 /* call should fill header later */
649 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
650 memset(hdr
, 0, size
); /* XXX do we need this ? */
651 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
654 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
655 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
656 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
657 sa
->sadb_sa_spi
= x
->id
.spi
;
658 sa
->sadb_sa_replay
= x
->props
.replay_window
;
659 sa
->sadb_sa_state
= SADB_SASTATE_DYING
;
660 if (x
->km
.state
== XFRM_STATE_VALID
&& !x
->km
.dying
)
661 sa
->sadb_sa_state
= SADB_SASTATE_MATURE
;
662 else if (x
->km
.state
== XFRM_STATE_ACQ
)
663 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
664 else if (x
->km
.state
== XFRM_STATE_EXPIRED
)
665 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
666 sa
->sadb_sa_auth
= 0;
668 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
669 sa
->sadb_sa_auth
= a
? a
->desc
.sadb_alg_id
: 0;
671 sa
->sadb_sa_encrypt
= 0;
672 BUG_ON(x
->ealg
&& x
->calg
);
674 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
675 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
677 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
679 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
680 sa
->sadb_sa_encrypt
= a
? a
->desc
.sadb_alg_id
: 0;
683 sa
->sadb_sa_flags
= 0;
684 if (x
->props
.flags
& XFRM_STATE_NOECN
)
685 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
686 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
687 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
691 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
692 sizeof(struct sadb_lifetime
));
693 lifetime
->sadb_lifetime_len
=
694 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
695 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
696 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
697 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
698 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
699 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
703 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
704 sizeof(struct sadb_lifetime
));
705 lifetime
->sadb_lifetime_len
=
706 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
707 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
708 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
709 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
710 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
711 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
714 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
715 sizeof(struct sadb_lifetime
));
716 lifetime
->sadb_lifetime_len
=
717 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
718 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
719 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
720 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
721 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
722 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
724 addr
= (struct sadb_address
*) skb_put(skb
,
725 sizeof(struct sadb_address
)+sockaddr_size
);
726 addr
->sadb_address_len
=
727 (sizeof(struct sadb_address
)+sockaddr_size
)/
729 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
730 /* "if the ports are non-zero, then the sadb_address_proto field,
731 normally zero, MUST be filled in with the transport
732 protocol's number." - RFC2367 */
733 addr
->sadb_address_proto
= 0;
734 addr
->sadb_address_reserved
= 0;
735 if (x
->props
.family
== AF_INET
) {
736 addr
->sadb_address_prefixlen
= 32;
738 sin
= (struct sockaddr_in
*) (addr
+ 1);
739 sin
->sin_family
= AF_INET
;
740 sin
->sin_addr
.s_addr
= x
->props
.saddr
.a4
;
742 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
744 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
745 else if (x
->props
.family
== AF_INET6
) {
746 addr
->sadb_address_prefixlen
= 128;
748 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
749 sin6
->sin6_family
= AF_INET6
;
751 sin6
->sin6_flowinfo
= 0;
752 memcpy(&sin6
->sin6_addr
, x
->props
.saddr
.a6
,
753 sizeof(struct in6_addr
));
754 sin6
->sin6_scope_id
= 0;
761 addr
= (struct sadb_address
*) skb_put(skb
,
762 sizeof(struct sadb_address
)+sockaddr_size
);
763 addr
->sadb_address_len
=
764 (sizeof(struct sadb_address
)+sockaddr_size
)/
766 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
767 addr
->sadb_address_proto
= 0;
768 addr
->sadb_address_prefixlen
= 32; /* XXX */
769 addr
->sadb_address_reserved
= 0;
770 if (x
->props
.family
== AF_INET
) {
771 sin
= (struct sockaddr_in
*) (addr
+ 1);
772 sin
->sin_family
= AF_INET
;
773 sin
->sin_addr
.s_addr
= x
->id
.daddr
.a4
;
775 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
777 if (x
->sel
.saddr
.a4
!= x
->props
.saddr
.a4
) {
778 addr
= (struct sadb_address
*) skb_put(skb
,
779 sizeof(struct sadb_address
)+sockaddr_size
);
780 addr
->sadb_address_len
=
781 (sizeof(struct sadb_address
)+sockaddr_size
)/
783 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
784 addr
->sadb_address_proto
=
785 pfkey_proto_from_xfrm(x
->sel
.proto
);
786 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
787 addr
->sadb_address_reserved
= 0;
789 sin
= (struct sockaddr_in
*) (addr
+ 1);
790 sin
->sin_family
= AF_INET
;
791 sin
->sin_addr
.s_addr
= x
->sel
.saddr
.a4
;
792 sin
->sin_port
= x
->sel
.sport
;
793 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
796 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
797 else if (x
->props
.family
== AF_INET6
) {
798 addr
->sadb_address_prefixlen
= 128;
800 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
801 sin6
->sin6_family
= AF_INET6
;
803 sin6
->sin6_flowinfo
= 0;
804 memcpy(&sin6
->sin6_addr
, x
->id
.daddr
.a6
, sizeof(struct in6_addr
));
805 sin6
->sin6_scope_id
= 0;
807 if (memcmp (x
->sel
.saddr
.a6
, x
->props
.saddr
.a6
,
808 sizeof(struct in6_addr
))) {
809 addr
= (struct sadb_address
*) skb_put(skb
,
810 sizeof(struct sadb_address
)+sockaddr_size
);
811 addr
->sadb_address_len
=
812 (sizeof(struct sadb_address
)+sockaddr_size
)/
814 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
815 addr
->sadb_address_proto
=
816 pfkey_proto_from_xfrm(x
->sel
.proto
);
817 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
818 addr
->sadb_address_reserved
= 0;
820 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
821 sin6
->sin6_family
= AF_INET6
;
822 sin6
->sin6_port
= x
->sel
.sport
;
823 sin6
->sin6_flowinfo
= 0;
824 memcpy(&sin6
->sin6_addr
, x
->sel
.saddr
.a6
,
825 sizeof(struct in6_addr
));
826 sin6
->sin6_scope_id
= 0;
834 if (add_keys
&& auth_key_size
) {
835 key
= (struct sadb_key
*) skb_put(skb
,
836 sizeof(struct sadb_key
)+auth_key_size
);
837 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
839 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
840 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
841 key
->sadb_key_reserved
= 0;
842 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
845 if (add_keys
&& encrypt_key_size
) {
846 key
= (struct sadb_key
*) skb_put(skb
,
847 sizeof(struct sadb_key
)+encrypt_key_size
);
848 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
849 encrypt_key_size
) / sizeof(uint64_t);
850 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
851 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
852 key
->sadb_key_reserved
= 0;
853 memcpy(key
+ 1, x
->ealg
->alg_key
,
854 (x
->ealg
->alg_key_len
+7)/8);
858 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
859 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
860 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
861 sa2
->sadb_x_sa2_mode
= x
->props
.mode
+ 1;
862 sa2
->sadb_x_sa2_reserved1
= 0;
863 sa2
->sadb_x_sa2_reserved2
= 0;
864 sa2
->sadb_x_sa2_sequence
= 0;
865 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
867 if (natt
&& natt
->encap_type
) {
868 struct sadb_x_nat_t_type
*n_type
;
869 struct sadb_x_nat_t_port
*n_port
;
872 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
873 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
874 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
875 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
876 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
877 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
878 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
881 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
882 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
883 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
884 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
885 n_port
->sadb_x_nat_t_port_reserved
= 0;
888 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
889 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
890 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
891 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
892 n_port
->sadb_x_nat_t_port_reserved
= 0;
898 static struct xfrm_state
* pfkey_msg2xfrm_state(struct sadb_msg
*hdr
,
901 struct xfrm_state
*x
;
902 struct sadb_lifetime
*lifetime
;
904 struct sadb_key
*key
;
909 sa
= (struct sadb_sa
*) ext_hdrs
[SADB_EXT_SA
-1];
911 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
912 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
913 return ERR_PTR(-EINVAL
);
914 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
915 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
916 return ERR_PTR(-EINVAL
);
917 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
918 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
919 return ERR_PTR(-EINVAL
);
920 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
921 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
922 return ERR_PTR(-EINVAL
);
924 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
926 return ERR_PTR(-EINVAL
);
928 /* default error is no buffer space */
933 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
934 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
935 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
936 Therefore, the sadb_sa_state field of all submitted SAs MUST be
937 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
940 However, KAME setkey always uses SADB_SASTATE_LARVAL.
941 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
943 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
944 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
945 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
946 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
947 return ERR_PTR(-EINVAL
);
948 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
950 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
951 ((key
->sadb_key_bits
+7) / 8 == 0 ||
952 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
953 return ERR_PTR(-EINVAL
);
954 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
956 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
957 ((key
->sadb_key_bits
+7) / 8 == 0 ||
958 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
959 return ERR_PTR(-EINVAL
);
961 x
= xfrm_state_alloc();
963 return ERR_PTR(-ENOBUFS
);
966 x
->id
.spi
= sa
->sadb_sa_spi
;
967 x
->props
.replay_window
= sa
->sadb_sa_replay
;
968 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
969 x
->props
.flags
|= XFRM_STATE_NOECN
;
970 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
971 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
973 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1];
974 if (lifetime
!= NULL
) {
975 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
976 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
977 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
978 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
980 lifetime
= (struct sadb_lifetime
*) ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1];
981 if (lifetime
!= NULL
) {
982 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
983 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
984 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
985 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
987 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_AUTH
-1];
988 if (sa
->sadb_sa_auth
) {
990 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
996 keysize
= (key
->sadb_key_bits
+ 7) / 8;
997 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1000 strcpy(x
->aalg
->alg_name
, a
->name
);
1001 x
->aalg
->alg_key_len
= 0;
1003 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1004 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1006 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1007 /* x->algo.flags = sa->sadb_sa_flags; */
1009 if (sa
->sadb_sa_encrypt
) {
1010 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1011 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1016 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1019 strcpy(x
->calg
->alg_name
, a
->name
);
1020 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1023 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1028 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1030 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1031 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1034 strcpy(x
->ealg
->alg_name
, a
->name
);
1035 x
->ealg
->alg_key_len
= 0;
1037 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1038 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1040 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1043 /* x->algo.flags = sa->sadb_sa_flags; */
1045 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1047 if (!x
->props
.family
) {
1048 err
= -EAFNOSUPPORT
;
1051 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1054 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1055 struct sadb_x_sa2
*sa2
= (void*)ext_hdrs
[SADB_X_EXT_SA2
-1];
1056 x
->props
.mode
= sa2
->sadb_x_sa2_mode
;
1059 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1062 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1063 struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1065 /* Nobody uses this, but we try. */
1066 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1067 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1070 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1071 struct sadb_x_nat_t_type
* n_type
;
1072 struct xfrm_encap_tmpl
*natt
;
1074 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1079 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1080 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1082 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1083 struct sadb_x_nat_t_port
* n_port
=
1084 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1085 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1087 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1088 struct sadb_x_nat_t_port
* n_port
=
1089 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1090 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1094 x
->type
= xfrm_get_type(proto
, x
->props
.family
);
1095 if (x
->type
== NULL
) {
1099 if (x
->type
->init_state(x
, NULL
)) {
1103 x
->km
.seq
= hdr
->sadb_msg_seq
;
1104 x
->km
.state
= XFRM_STATE_VALID
;
1108 x
->km
.state
= XFRM_STATE_DEAD
;
1110 return ERR_PTR(err
);
1113 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1118 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1120 struct sk_buff
*resp_skb
;
1121 struct sadb_x_sa2
*sa2
;
1122 struct sadb_address
*saddr
, *daddr
;
1123 struct sadb_msg
*out_hdr
;
1124 struct xfrm_state
*x
= NULL
;
1128 unsigned short family
;
1129 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1131 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1132 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1135 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1139 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1140 mode
= sa2
->sadb_x_sa2_mode
- 1;
1141 reqid
= sa2
->sadb_x_sa2_reqid
;
1147 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1148 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1150 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1153 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1154 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1156 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1158 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1159 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1164 if (hdr
->sadb_msg_seq
) {
1165 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1166 if (x
&& xfrm_addr_cmp(&x
->id
.daddr
, xdaddr
, family
)) {
1173 x
= xfrm_find_acq(mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1178 resp_skb
= ERR_PTR(-ENOENT
);
1180 spin_lock_bh(&x
->lock
);
1181 if (x
->km
.state
!= XFRM_STATE_DEAD
) {
1182 struct sadb_spirange
*range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1183 u32 min_spi
, max_spi
;
1185 if (range
!= NULL
) {
1186 min_spi
= range
->sadb_spirange_min
;
1187 max_spi
= range
->sadb_spirange_max
;
1190 max_spi
= 0x0fffffff;
1192 xfrm_alloc_spi(x
, htonl(min_spi
), htonl(max_spi
));
1194 resp_skb
= pfkey_xfrm_state2msg(x
, 0, 3);
1196 spin_unlock_bh(&x
->lock
);
1198 if (IS_ERR(resp_skb
)) {
1200 return PTR_ERR(resp_skb
);
1203 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1204 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1205 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1206 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1207 out_hdr
->sadb_msg_errno
= 0;
1208 out_hdr
->sadb_msg_reserved
= 0;
1209 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1210 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1214 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
);
1219 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1221 struct xfrm_state
*x
;
1223 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1226 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1229 x
= xfrm_find_acq_byseq(hdr
->sadb_msg_seq
);
1233 spin_lock_bh(&x
->lock
);
1234 if (x
->km
.state
== XFRM_STATE_ACQ
) {
1235 x
->km
.state
= XFRM_STATE_ERROR
;
1238 spin_unlock_bh(&x
->lock
);
1244 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1246 struct sk_buff
*out_skb
;
1247 struct sadb_msg
*out_hdr
;
1248 struct xfrm_state
*x
;
1253 x
= pfkey_msg2xfrm_state(hdr
, ext_hdrs
);
1257 if (hdr
->sadb_msg_type
== SADB_ADD
)
1258 err
= xfrm_state_add(x
);
1260 err
= xfrm_state_update(x
);
1263 x
->km
.state
= XFRM_STATE_DEAD
;
1268 out_skb
= pfkey_xfrm_state2msg(x
, 0, 3);
1269 if (IS_ERR(out_skb
))
1270 return PTR_ERR(out_skb
); /* XXX Should we return 0 here ? */
1272 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1273 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1274 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
1275 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1276 out_hdr
->sadb_msg_errno
= 0;
1277 out_hdr
->sadb_msg_reserved
= 0;
1278 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1279 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1281 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, sk
);
1286 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1288 struct xfrm_state
*x
;
1290 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1291 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1292 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1295 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1299 if (xfrm_state_kern(x
)) {
1304 xfrm_state_delete(x
);
1307 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
1313 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1316 struct sk_buff
*out_skb
;
1317 struct sadb_msg
*out_hdr
;
1318 struct xfrm_state
*x
;
1320 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1321 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1322 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1325 x
= pfkey_xfrm_state_lookup(hdr
, ext_hdrs
);
1329 out_skb
= pfkey_xfrm_state2msg(x
, 1, 3);
1330 proto
= x
->id
.proto
;
1332 if (IS_ERR(out_skb
))
1333 return PTR_ERR(out_skb
);
1335 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1336 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1337 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1338 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1339 out_hdr
->sadb_msg_errno
= 0;
1340 out_hdr
->sadb_msg_reserved
= 0;
1341 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1342 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1343 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
);
1348 static struct sk_buff
*compose_sadb_supported(struct sadb_msg
*orig
, int allocation
)
1350 struct sk_buff
*skb
;
1351 struct sadb_msg
*hdr
;
1352 int len
, auth_len
, enc_len
, i
;
1354 auth_len
= xfrm_count_auth_supported();
1356 auth_len
*= sizeof(struct sadb_alg
);
1357 auth_len
+= sizeof(struct sadb_supported
);
1360 enc_len
= xfrm_count_enc_supported();
1362 enc_len
*= sizeof(struct sadb_alg
);
1363 enc_len
+= sizeof(struct sadb_supported
);
1366 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1368 skb
= alloc_skb(len
+ 16, allocation
);
1372 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1373 pfkey_hdr_dup(hdr
, orig
);
1374 hdr
->sadb_msg_errno
= 0;
1375 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1378 struct sadb_supported
*sp
;
1379 struct sadb_alg
*ap
;
1381 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1382 ap
= (struct sadb_alg
*) (sp
+ 1);
1384 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1385 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1387 for (i
= 0; ; i
++) {
1388 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1391 if (aalg
->available
)
1397 struct sadb_supported
*sp
;
1398 struct sadb_alg
*ap
;
1400 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1401 ap
= (struct sadb_alg
*) (sp
+ 1);
1403 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1404 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1406 for (i
= 0; ; i
++) {
1407 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1410 if (ealg
->available
)
1419 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1421 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1422 struct sk_buff
*supp_skb
;
1424 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1427 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1428 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1430 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1435 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1437 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1438 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1443 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
);
1448 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1451 struct sk_buff
*skb_out
;
1452 struct sadb_msg
*hdr_out
;
1454 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1458 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
1462 xfrm_state_flush(proto
);
1464 hdr_out
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
1465 pfkey_hdr_dup(hdr_out
, hdr
);
1466 hdr_out
->sadb_msg_errno
= (uint8_t) 0;
1467 hdr_out
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1469 pfkey_broadcast(skb_out
, GFP_KERNEL
, BROADCAST_ALL
, NULL
);
1474 struct pfkey_dump_data
1476 struct sk_buff
*skb
;
1477 struct sadb_msg
*hdr
;
1481 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1483 struct pfkey_dump_data
*data
= ptr
;
1484 struct sk_buff
*out_skb
;
1485 struct sadb_msg
*out_hdr
;
1487 out_skb
= pfkey_xfrm_state2msg(x
, 1, 3);
1488 if (IS_ERR(out_skb
))
1489 return PTR_ERR(out_skb
);
1491 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1492 out_hdr
->sadb_msg_version
= data
->hdr
->sadb_msg_version
;
1493 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1494 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1495 out_hdr
->sadb_msg_errno
= 0;
1496 out_hdr
->sadb_msg_reserved
= 0;
1497 out_hdr
->sadb_msg_seq
= count
;
1498 out_hdr
->sadb_msg_pid
= data
->hdr
->sadb_msg_pid
;
1499 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, data
->sk
);
1503 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1506 struct pfkey_dump_data data
= { .skb
= skb
, .hdr
= hdr
, .sk
= sk
};
1508 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1512 return xfrm_state_walk(proto
, dump_sa
, &data
);
1515 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1517 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1518 int satype
= hdr
->sadb_msg_satype
;
1520 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1521 /* XXX we mangle packet... */
1522 hdr
->sadb_msg_errno
= 0;
1523 if (satype
!= 0 && satype
!= 1)
1525 pfk
->promisc
= satype
;
1527 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
, BROADCAST_ALL
, NULL
);
1531 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1534 u32 reqid
= *(u32
*)ptr
;
1536 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1537 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1543 static u32
gen_reqid(void)
1546 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1552 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1553 if (xfrm_policy_walk(check_reqid
, (void*)&reqid
) != -EEXIST
)
1555 } while (reqid
!= start
);
1560 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1562 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1563 struct sockaddr_in
*sin
;
1564 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1565 struct sockaddr_in6
*sin6
;
1568 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1571 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1574 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1575 t
->mode
= rq
->sadb_x_ipsecrequest_mode
-1;
1576 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1578 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1579 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1580 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1582 if (!t
->reqid
&& !(t
->reqid
= gen_reqid()))
1586 /* addresses present only in tunnel mode */
1588 switch (xp
->family
) {
1590 sin
= (void*)(rq
+1);
1591 if (sin
->sin_family
!= AF_INET
)
1593 t
->saddr
.a4
= sin
->sin_addr
.s_addr
;
1595 if (sin
->sin_family
!= AF_INET
)
1597 t
->id
.daddr
.a4
= sin
->sin_addr
.s_addr
;
1599 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1601 sin6
= (void *)(rq
+1);
1602 if (sin6
->sin6_family
!= AF_INET6
)
1604 memcpy(t
->saddr
.a6
, &sin6
->sin6_addr
, sizeof(struct in6_addr
));
1606 if (sin6
->sin6_family
!= AF_INET6
)
1608 memcpy(t
->id
.daddr
.a6
, &sin6
->sin6_addr
, sizeof(struct in6_addr
));
1615 /* No way to set this via kame pfkey */
1616 t
->aalgos
= t
->ealgos
= t
->calgos
= ~0;
1622 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1625 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1626 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1628 while (len
>= sizeof(struct sadb_x_ipsecrequest
)) {
1629 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
1631 len
-= rq
->sadb_x_ipsecrequest_len
;
1632 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
1637 static int pfkey_xfrm_policy2msg_size(struct xfrm_policy
*xp
)
1639 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1640 int socklen
= (xp
->family
== AF_INET
?
1641 sizeof(struct sockaddr_in
) :
1642 sizeof(struct sockaddr_in6
));
1644 return sizeof(struct sadb_msg
) +
1645 (sizeof(struct sadb_lifetime
) * 3) +
1646 (sizeof(struct sadb_address
) * 2) +
1647 (sockaddr_size
* 2) +
1648 sizeof(struct sadb_x_policy
) +
1649 (xp
->xfrm_nr
* (sizeof(struct sadb_x_ipsecrequest
) +
1653 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(struct xfrm_policy
*xp
)
1655 struct sk_buff
*skb
;
1658 size
= pfkey_xfrm_policy2msg_size(xp
);
1660 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
1662 return ERR_PTR(-ENOBUFS
);
1667 static void pfkey_xfrm_policy2msg(struct sk_buff
*skb
, struct xfrm_policy
*xp
, int dir
)
1669 struct sadb_msg
*hdr
;
1670 struct sadb_address
*addr
;
1671 struct sadb_lifetime
*lifetime
;
1672 struct sadb_x_policy
*pol
;
1673 struct sockaddr_in
*sin
;
1674 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1675 struct sockaddr_in6
*sin6
;
1679 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1680 int socklen
= (xp
->family
== AF_INET
?
1681 sizeof(struct sockaddr_in
) :
1682 sizeof(struct sockaddr_in6
));
1684 size
= pfkey_xfrm_policy2msg_size(xp
);
1686 /* call should fill header later */
1687 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1688 memset(hdr
, 0, size
); /* XXX do we need this ? */
1691 addr
= (struct sadb_address
*) skb_put(skb
,
1692 sizeof(struct sadb_address
)+sockaddr_size
);
1693 addr
->sadb_address_len
=
1694 (sizeof(struct sadb_address
)+sockaddr_size
)/
1696 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
1697 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
1698 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
1699 addr
->sadb_address_reserved
= 0;
1701 if (xp
->family
== AF_INET
) {
1702 sin
= (struct sockaddr_in
*) (addr
+ 1);
1703 sin
->sin_family
= AF_INET
;
1704 sin
->sin_addr
.s_addr
= xp
->selector
.saddr
.a4
;
1705 sin
->sin_port
= xp
->selector
.sport
;
1706 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
1708 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1709 else if (xp
->family
== AF_INET6
) {
1710 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
1711 sin6
->sin6_family
= AF_INET6
;
1712 sin6
->sin6_port
= xp
->selector
.sport
;
1713 sin6
->sin6_flowinfo
= 0;
1714 memcpy(&sin6
->sin6_addr
, xp
->selector
.saddr
.a6
,
1715 sizeof(struct in6_addr
));
1716 sin6
->sin6_scope_id
= 0;
1723 addr
= (struct sadb_address
*) skb_put(skb
,
1724 sizeof(struct sadb_address
)+sockaddr_size
);
1725 addr
->sadb_address_len
=
1726 (sizeof(struct sadb_address
)+sockaddr_size
)/
1728 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
1729 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
1730 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
1731 addr
->sadb_address_reserved
= 0;
1732 if (xp
->family
== AF_INET
) {
1733 sin
= (struct sockaddr_in
*) (addr
+ 1);
1734 sin
->sin_family
= AF_INET
;
1735 sin
->sin_addr
.s_addr
= xp
->selector
.daddr
.a4
;
1736 sin
->sin_port
= xp
->selector
.dport
;
1737 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
1739 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1740 else if (xp
->family
== AF_INET6
) {
1741 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
1742 sin6
->sin6_family
= AF_INET6
;
1743 sin6
->sin6_port
= xp
->selector
.dport
;
1744 sin6
->sin6_flowinfo
= 0;
1745 memcpy(&sin6
->sin6_addr
, xp
->selector
.daddr
.a6
,
1746 sizeof(struct in6_addr
));
1747 sin6
->sin6_scope_id
= 0;
1754 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
1755 sizeof(struct sadb_lifetime
));
1756 lifetime
->sadb_lifetime_len
=
1757 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
1758 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
1759 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
1760 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
1761 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
1762 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
1764 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
1765 sizeof(struct sadb_lifetime
));
1766 lifetime
->sadb_lifetime_len
=
1767 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
1768 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
1769 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
1770 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
1771 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
1772 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
1774 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
1775 sizeof(struct sadb_lifetime
));
1776 lifetime
->sadb_lifetime_len
=
1777 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
1778 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
1779 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
1780 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
1781 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
1782 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
1784 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
1785 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
1786 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1787 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
1788 if (xp
->action
== XFRM_POLICY_ALLOW
) {
1790 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
1792 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
1794 pol
->sadb_x_policy_dir
= dir
+1;
1795 pol
->sadb_x_policy_id
= xp
->index
;
1796 pol
->sadb_x_policy_priority
= xp
->priority
;
1798 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1799 struct sadb_x_ipsecrequest
*rq
;
1800 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
1803 req_size
= sizeof(struct sadb_x_ipsecrequest
);
1805 req_size
+= 2*socklen
;
1808 rq
= (void*)skb_put(skb
, req_size
);
1809 pol
->sadb_x_policy_len
+= req_size
/8;
1810 memset(rq
, 0, sizeof(*rq
));
1811 rq
->sadb_x_ipsecrequest_len
= req_size
;
1812 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
1813 rq
->sadb_x_ipsecrequest_mode
= t
->mode
+1;
1814 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
1816 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
1818 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
1819 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
1821 switch (xp
->family
) {
1823 sin
= (void*)(rq
+1);
1824 sin
->sin_family
= AF_INET
;
1825 sin
->sin_addr
.s_addr
= t
->saddr
.a4
;
1827 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
1829 sin
->sin_family
= AF_INET
;
1830 sin
->sin_addr
.s_addr
= t
->id
.daddr
.a4
;
1832 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
1834 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1836 sin6
= (void*)(rq
+1);
1837 sin6
->sin6_family
= AF_INET6
;
1838 sin6
->sin6_port
= 0;
1839 sin6
->sin6_flowinfo
= 0;
1840 memcpy(&sin6
->sin6_addr
, t
->saddr
.a6
,
1841 sizeof(struct in6_addr
));
1842 sin6
->sin6_scope_id
= 0;
1845 sin6
->sin6_family
= AF_INET6
;
1846 sin6
->sin6_port
= 0;
1847 sin6
->sin6_flowinfo
= 0;
1848 memcpy(&sin6
->sin6_addr
, t
->id
.daddr
.a6
,
1849 sizeof(struct in6_addr
));
1850 sin6
->sin6_scope_id
= 0;
1858 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
1859 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
1862 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1865 struct sadb_lifetime
*lifetime
;
1866 struct sadb_address
*sa
;
1867 struct sadb_x_policy
*pol
;
1868 struct xfrm_policy
*xp
;
1869 struct sk_buff
*out_skb
;
1870 struct sadb_msg
*out_hdr
;
1872 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1873 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
1874 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
1877 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
1878 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
1880 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
1883 xp
= xfrm_policy_alloc(GFP_KERNEL
);
1887 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
1888 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
1889 xp
->priority
= pol
->sadb_x_policy_priority
;
1891 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1892 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
1897 xp
->selector
.family
= xp
->family
;
1898 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
1899 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
1900 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
1901 if (xp
->selector
.sport
)
1902 xp
->selector
.sport_mask
= ~0;
1904 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1905 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
1906 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
1908 /* Amusing, we set this twice. KAME apps appear to set same value
1909 * in both addresses.
1911 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
1913 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
1914 if (xp
->selector
.dport
)
1915 xp
->selector
.dport_mask
= ~0;
1917 xp
->lft
.soft_byte_limit
= XFRM_INF
;
1918 xp
->lft
.hard_byte_limit
= XFRM_INF
;
1919 xp
->lft
.soft_packet_limit
= XFRM_INF
;
1920 xp
->lft
.hard_packet_limit
= XFRM_INF
;
1921 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
1922 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1923 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1924 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1925 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1927 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
1928 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1929 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1930 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1931 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1934 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
1935 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
1938 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
1939 if (IS_ERR(out_skb
)) {
1940 err
= PTR_ERR(out_skb
);
1944 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
1945 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
1951 pfkey_xfrm_policy2msg(out_skb
, xp
, pol
->sadb_x_policy_dir
-1);
1955 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1956 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1957 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
1958 out_hdr
->sadb_msg_satype
= 0;
1959 out_hdr
->sadb_msg_errno
= 0;
1960 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1961 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1962 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, sk
);
1970 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
1973 struct sadb_address
*sa
;
1974 struct sadb_x_policy
*pol
;
1975 struct xfrm_policy
*xp
;
1976 struct sk_buff
*out_skb
;
1977 struct sadb_msg
*out_hdr
;
1978 struct xfrm_selector sel
;
1980 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1981 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
1982 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
1985 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
1986 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
1989 memset(&sel
, 0, sizeof(sel
));
1991 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1992 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
1993 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
1994 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
1995 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
1997 sel
.sport_mask
= ~0;
1999 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
2000 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2001 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2002 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2003 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2005 sel
.dport_mask
= ~0;
2007 xp
= xfrm_policy_bysel(pol
->sadb_x_policy_dir
-1, &sel
, 1);
2013 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2014 if (IS_ERR(out_skb
)) {
2015 err
= PTR_ERR(out_skb
);
2018 pfkey_xfrm_policy2msg(out_skb
, xp
, pol
->sadb_x_policy_dir
-1);
2020 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2021 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2022 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE
;
2023 out_hdr
->sadb_msg_satype
= 0;
2024 out_hdr
->sadb_msg_errno
= 0;
2025 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2026 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2027 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, sk
);
2035 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2038 struct sadb_x_policy
*pol
;
2039 struct xfrm_policy
*xp
;
2040 struct sk_buff
*out_skb
;
2041 struct sadb_msg
*out_hdr
;
2043 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2046 xp
= xfrm_policy_byid(0, pol
->sadb_x_policy_id
,
2047 hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2053 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2054 if (IS_ERR(out_skb
)) {
2055 err
= PTR_ERR(out_skb
);
2058 pfkey_xfrm_policy2msg(out_skb
, xp
, pol
->sadb_x_policy_dir
-1);
2060 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2061 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2062 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2063 out_hdr
->sadb_msg_satype
= 0;
2064 out_hdr
->sadb_msg_errno
= 0;
2065 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2066 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2067 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, sk
);
2075 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2077 struct pfkey_dump_data
*data
= ptr
;
2078 struct sk_buff
*out_skb
;
2079 struct sadb_msg
*out_hdr
;
2081 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2082 if (IS_ERR(out_skb
))
2083 return PTR_ERR(out_skb
);
2085 pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2087 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2088 out_hdr
->sadb_msg_version
= data
->hdr
->sadb_msg_version
;
2089 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2090 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2091 out_hdr
->sadb_msg_errno
= 0;
2092 out_hdr
->sadb_msg_seq
= count
;
2093 out_hdr
->sadb_msg_pid
= data
->hdr
->sadb_msg_pid
;
2094 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, data
->sk
);
2098 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2100 struct pfkey_dump_data data
= { .skb
= skb
, .hdr
= hdr
, .sk
= sk
};
2102 return xfrm_policy_walk(dump_sp
, &data
);
2105 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
, void **ext_hdrs
)
2107 struct sk_buff
*skb_out
;
2108 struct sadb_msg
*hdr_out
;
2110 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
2114 xfrm_policy_flush();
2116 hdr_out
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2117 pfkey_hdr_dup(hdr_out
, hdr
);
2118 hdr_out
->sadb_msg_errno
= (uint8_t) 0;
2119 hdr_out
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2120 pfkey_broadcast(skb_out
, GFP_KERNEL
, BROADCAST_ALL
, NULL
);
2125 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2126 struct sadb_msg
*hdr
, void **ext_hdrs
);
2127 static pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2128 [SADB_RESERVED
] = pfkey_reserved
,
2129 [SADB_GETSPI
] = pfkey_getspi
,
2130 [SADB_UPDATE
] = pfkey_add
,
2131 [SADB_ADD
] = pfkey_add
,
2132 [SADB_DELETE
] = pfkey_delete
,
2133 [SADB_GET
] = pfkey_get
,
2134 [SADB_ACQUIRE
] = pfkey_acquire
,
2135 [SADB_REGISTER
] = pfkey_register
,
2136 [SADB_EXPIRE
] = NULL
,
2137 [SADB_FLUSH
] = pfkey_flush
,
2138 [SADB_DUMP
] = pfkey_dump
,
2139 [SADB_X_PROMISC
] = pfkey_promisc
,
2140 [SADB_X_PCHANGE
] = NULL
,
2141 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2142 [SADB_X_SPDADD
] = pfkey_spdadd
,
2143 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2144 [SADB_X_SPDGET
] = pfkey_spdget
,
2145 [SADB_X_SPDACQUIRE
] = NULL
,
2146 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2147 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2148 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2149 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2152 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, struct sadb_msg
*hdr
)
2154 void *ext_hdrs
[SADB_EXT_MAX
];
2157 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2158 BROADCAST_PROMISC_ONLY
, NULL
);
2160 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2161 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2164 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2165 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2170 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2172 struct sadb_msg
*hdr
= NULL
;
2174 if (skb
->len
< sizeof(*hdr
)) {
2177 hdr
= (struct sadb_msg
*) skb
->data
;
2178 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2179 hdr
->sadb_msg_reserved
!= 0 ||
2180 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2181 hdr
->sadb_msg_type
> SADB_MAX
)) {
2184 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2185 sizeof(uint64_t)) ||
2186 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2187 sizeof(uint64_t))) {
2197 static inline int aalg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2199 return t
->aalgos
& (1 << d
->desc
.sadb_alg_id
);
2202 static inline int ealg_tmpl_set(struct xfrm_tmpl
*t
, struct xfrm_algo_desc
*d
)
2204 return t
->ealgos
& (1 << d
->desc
.sadb_alg_id
);
2207 static int count_ah_combs(struct xfrm_tmpl
*t
)
2211 for (i
= 0; ; i
++) {
2212 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2215 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2216 sz
+= sizeof(struct sadb_comb
);
2218 return sz
+ sizeof(struct sadb_prop
);
2221 static int count_esp_combs(struct xfrm_tmpl
*t
)
2225 for (i
= 0; ; i
++) {
2226 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2230 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2233 for (k
= 1; ; k
++) {
2234 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2238 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2239 sz
+= sizeof(struct sadb_comb
);
2242 return sz
+ sizeof(struct sadb_prop
);
2245 static void dump_ah_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2247 struct sadb_prop
*p
;
2250 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2251 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2252 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2253 p
->sadb_prop_replay
= 32;
2254 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2256 for (i
= 0; ; i
++) {
2257 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2261 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2262 struct sadb_comb
*c
;
2263 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2264 memset(c
, 0, sizeof(*c
));
2265 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2266 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2267 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2268 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2269 c
->sadb_comb_hard_addtime
= 24*60*60;
2270 c
->sadb_comb_soft_addtime
= 20*60*60;
2271 c
->sadb_comb_hard_usetime
= 8*60*60;
2272 c
->sadb_comb_soft_usetime
= 7*60*60;
2277 static void dump_esp_combs(struct sk_buff
*skb
, struct xfrm_tmpl
*t
)
2279 struct sadb_prop
*p
;
2282 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2283 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2284 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2285 p
->sadb_prop_replay
= 32;
2286 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2289 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2293 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2296 for (k
= 1; ; k
++) {
2297 struct sadb_comb
*c
;
2298 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2301 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2303 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2304 memset(c
, 0, sizeof(*c
));
2305 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2306 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2307 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2308 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2309 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
2310 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
2311 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
2312 c
->sadb_comb_hard_addtime
= 24*60*60;
2313 c
->sadb_comb_soft_addtime
= 20*60*60;
2314 c
->sadb_comb_hard_usetime
= 8*60*60;
2315 c
->sadb_comb_soft_usetime
= 7*60*60;
2320 static int pfkey_send_notify(struct xfrm_state
*x
, int hard
)
2322 struct sk_buff
*out_skb
;
2323 struct sadb_msg
*out_hdr
;
2324 int hsc
= (hard
? 2 : 1);
2326 out_skb
= pfkey_xfrm_state2msg(x
, 0, hsc
);
2327 if (IS_ERR(out_skb
))
2328 return PTR_ERR(out_skb
);
2330 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2331 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2332 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
2333 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2334 out_hdr
->sadb_msg_errno
= 0;
2335 out_hdr
->sadb_msg_reserved
= 0;
2336 out_hdr
->sadb_msg_seq
= 0;
2337 out_hdr
->sadb_msg_pid
= 0;
2339 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
2343 static u32
get_acqseq(void)
2347 static DEFINE_SPINLOCK(acqseq_lock
);
2349 spin_lock_bh(&acqseq_lock
);
2350 res
= (++acqseq
? : ++acqseq
);
2351 spin_unlock_bh(&acqseq_lock
);
2355 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
, int dir
)
2357 struct sk_buff
*skb
;
2358 struct sadb_msg
*hdr
;
2359 struct sadb_address
*addr
;
2360 struct sadb_x_policy
*pol
;
2361 struct sockaddr_in
*sin
;
2362 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2363 struct sockaddr_in6
*sin6
;
2368 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
2372 size
= sizeof(struct sadb_msg
) +
2373 (sizeof(struct sadb_address
) * 2) +
2374 (sockaddr_size
* 2) +
2375 sizeof(struct sadb_x_policy
);
2377 if (x
->id
.proto
== IPPROTO_AH
)
2378 size
+= count_ah_combs(t
);
2379 else if (x
->id
.proto
== IPPROTO_ESP
)
2380 size
+= count_esp_combs(t
);
2382 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2386 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2387 hdr
->sadb_msg_version
= PF_KEY_V2
;
2388 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
2389 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2390 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2391 hdr
->sadb_msg_errno
= 0;
2392 hdr
->sadb_msg_reserved
= 0;
2393 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
2394 hdr
->sadb_msg_pid
= 0;
2397 addr
= (struct sadb_address
*) skb_put(skb
,
2398 sizeof(struct sadb_address
)+sockaddr_size
);
2399 addr
->sadb_address_len
=
2400 (sizeof(struct sadb_address
)+sockaddr_size
)/
2402 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2403 addr
->sadb_address_proto
= 0;
2404 addr
->sadb_address_reserved
= 0;
2405 if (x
->props
.family
== AF_INET
) {
2406 addr
->sadb_address_prefixlen
= 32;
2408 sin
= (struct sockaddr_in
*) (addr
+ 1);
2409 sin
->sin_family
= AF_INET
;
2410 sin
->sin_addr
.s_addr
= x
->props
.saddr
.a4
;
2412 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
2414 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2415 else if (x
->props
.family
== AF_INET6
) {
2416 addr
->sadb_address_prefixlen
= 128;
2418 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
2419 sin6
->sin6_family
= AF_INET6
;
2420 sin6
->sin6_port
= 0;
2421 sin6
->sin6_flowinfo
= 0;
2422 memcpy(&sin6
->sin6_addr
,
2423 x
->props
.saddr
.a6
, sizeof(struct in6_addr
));
2424 sin6
->sin6_scope_id
= 0;
2431 addr
= (struct sadb_address
*) skb_put(skb
,
2432 sizeof(struct sadb_address
)+sockaddr_size
);
2433 addr
->sadb_address_len
=
2434 (sizeof(struct sadb_address
)+sockaddr_size
)/
2436 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2437 addr
->sadb_address_proto
= 0;
2438 addr
->sadb_address_reserved
= 0;
2439 if (x
->props
.family
== AF_INET
) {
2440 addr
->sadb_address_prefixlen
= 32;
2442 sin
= (struct sockaddr_in
*) (addr
+ 1);
2443 sin
->sin_family
= AF_INET
;
2444 sin
->sin_addr
.s_addr
= x
->id
.daddr
.a4
;
2446 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
2448 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2449 else if (x
->props
.family
== AF_INET6
) {
2450 addr
->sadb_address_prefixlen
= 128;
2452 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
2453 sin6
->sin6_family
= AF_INET6
;
2454 sin6
->sin6_port
= 0;
2455 sin6
->sin6_flowinfo
= 0;
2456 memcpy(&sin6
->sin6_addr
,
2457 x
->id
.daddr
.a6
, sizeof(struct in6_addr
));
2458 sin6
->sin6_scope_id
= 0;
2464 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2465 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2466 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2467 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2468 pol
->sadb_x_policy_dir
= dir
+1;
2469 pol
->sadb_x_policy_id
= xp
->index
;
2471 /* Set sadb_comb's. */
2472 if (x
->id
.proto
== IPPROTO_AH
)
2473 dump_ah_combs(skb
, t
);
2474 else if (x
->id
.proto
== IPPROTO_ESP
)
2475 dump_esp_combs(skb
, t
);
2477 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
2480 static struct xfrm_policy
*pfkey_compile_policy(u16 family
, int opt
,
2481 u8
*data
, int len
, int *dir
)
2483 struct xfrm_policy
*xp
;
2484 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
2488 if (opt
!= IP_IPSEC_POLICY
) {
2493 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2495 if (opt
!= IPV6_IPSEC_POLICY
) {
2508 if (len
< sizeof(struct sadb_x_policy
) ||
2509 pol
->sadb_x_policy_len
*8 > len
||
2510 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
2511 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
2514 xp
= xfrm_policy_alloc(GFP_ATOMIC
);
2520 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2521 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2523 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2524 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2525 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2526 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2527 xp
->family
= family
;
2530 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2531 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
2534 *dir
= pol
->sadb_x_policy_dir
-1;
2542 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, u16 sport
)
2544 struct sk_buff
*skb
;
2545 struct sadb_msg
*hdr
;
2547 struct sadb_address
*addr
;
2548 struct sadb_x_nat_t_port
*n_port
;
2549 struct sockaddr_in
*sin
;
2550 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2551 struct sockaddr_in6
*sin6
;
2555 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
2556 struct xfrm_encap_tmpl
*natt
= NULL
;
2558 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
2570 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
2572 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
2573 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
2576 size
= sizeof(struct sadb_msg
) +
2577 sizeof(struct sadb_sa
) +
2578 (sizeof(struct sadb_address
) * 2) +
2579 (sockaddr_size
* 2) +
2580 (sizeof(struct sadb_x_nat_t_port
) * 2);
2582 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2586 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2587 hdr
->sadb_msg_version
= PF_KEY_V2
;
2588 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
2589 hdr
->sadb_msg_satype
= satype
;
2590 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2591 hdr
->sadb_msg_errno
= 0;
2592 hdr
->sadb_msg_reserved
= 0;
2593 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
2594 hdr
->sadb_msg_pid
= 0;
2597 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
2598 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
2599 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
2600 sa
->sadb_sa_spi
= x
->id
.spi
;
2601 sa
->sadb_sa_replay
= 0;
2602 sa
->sadb_sa_state
= 0;
2603 sa
->sadb_sa_auth
= 0;
2604 sa
->sadb_sa_encrypt
= 0;
2605 sa
->sadb_sa_flags
= 0;
2607 /* ADDRESS_SRC (old addr) */
2608 addr
= (struct sadb_address
*)
2609 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
2610 addr
->sadb_address_len
=
2611 (sizeof(struct sadb_address
)+sockaddr_size
)/
2613 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2614 addr
->sadb_address_proto
= 0;
2615 addr
->sadb_address_reserved
= 0;
2616 if (x
->props
.family
== AF_INET
) {
2617 addr
->sadb_address_prefixlen
= 32;
2619 sin
= (struct sockaddr_in
*) (addr
+ 1);
2620 sin
->sin_family
= AF_INET
;
2621 sin
->sin_addr
.s_addr
= x
->props
.saddr
.a4
;
2623 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
2625 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2626 else if (x
->props
.family
== AF_INET6
) {
2627 addr
->sadb_address_prefixlen
= 128;
2629 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
2630 sin6
->sin6_family
= AF_INET6
;
2631 sin6
->sin6_port
= 0;
2632 sin6
->sin6_flowinfo
= 0;
2633 memcpy(&sin6
->sin6_addr
,
2634 x
->props
.saddr
.a6
, sizeof(struct in6_addr
));
2635 sin6
->sin6_scope_id
= 0;
2641 /* NAT_T_SPORT (old port) */
2642 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
2643 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
2644 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
2645 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
2646 n_port
->sadb_x_nat_t_port_reserved
= 0;
2648 /* ADDRESS_DST (new addr) */
2649 addr
= (struct sadb_address
*)
2650 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
2651 addr
->sadb_address_len
=
2652 (sizeof(struct sadb_address
)+sockaddr_size
)/
2654 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2655 addr
->sadb_address_proto
= 0;
2656 addr
->sadb_address_reserved
= 0;
2657 if (x
->props
.family
== AF_INET
) {
2658 addr
->sadb_address_prefixlen
= 32;
2660 sin
= (struct sockaddr_in
*) (addr
+ 1);
2661 sin
->sin_family
= AF_INET
;
2662 sin
->sin_addr
.s_addr
= ipaddr
->a4
;
2664 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
2666 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2667 else if (x
->props
.family
== AF_INET6
) {
2668 addr
->sadb_address_prefixlen
= 128;
2670 sin6
= (struct sockaddr_in6
*) (addr
+ 1);
2671 sin6
->sin6_family
= AF_INET6
;
2672 sin6
->sin6_port
= 0;
2673 sin6
->sin6_flowinfo
= 0;
2674 memcpy(&sin6
->sin6_addr
, &ipaddr
->a6
, sizeof(struct in6_addr
));
2675 sin6
->sin6_scope_id
= 0;
2681 /* NAT_T_DPORT (new port) */
2682 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
2683 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
2684 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
2685 n_port
->sadb_x_nat_t_port_port
= sport
;
2686 n_port
->sadb_x_nat_t_port_reserved
= 0;
2688 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
);
2691 static int pfkey_sendmsg(struct kiocb
*kiocb
,
2692 struct socket
*sock
, struct msghdr
*msg
, size_t len
)
2694 struct sock
*sk
= sock
->sk
;
2695 struct sk_buff
*skb
= NULL
;
2696 struct sadb_msg
*hdr
= NULL
;
2700 if (msg
->msg_flags
& MSG_OOB
)
2704 if ((unsigned)len
> sk
->sk_sndbuf
- 32)
2708 skb
= alloc_skb(len
, GFP_KERNEL
);
2713 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
))
2716 hdr
= pfkey_get_base_msg(skb
, &err
);
2720 down(&xfrm_cfg_sem
);
2721 err
= pfkey_process(sk
, skb
, hdr
);
2725 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
2733 static int pfkey_recvmsg(struct kiocb
*kiocb
,
2734 struct socket
*sock
, struct msghdr
*msg
, size_t len
,
2737 struct sock
*sk
= sock
->sk
;
2738 struct sk_buff
*skb
;
2742 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
2745 msg
->msg_namelen
= 0;
2746 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
2752 msg
->msg_flags
|= MSG_TRUNC
;
2756 skb
->h
.raw
= skb
->data
;
2757 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
2761 sock_recv_timestamp(msg
, sk
, skb
);
2763 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
2766 skb_free_datagram(sk
, skb
);
2771 static struct proto_ops pfkey_ops
= {
2773 .owner
= THIS_MODULE
,
2774 /* Operations that make no sense on pfkey sockets. */
2775 .bind
= sock_no_bind
,
2776 .connect
= sock_no_connect
,
2777 .socketpair
= sock_no_socketpair
,
2778 .accept
= sock_no_accept
,
2779 .getname
= sock_no_getname
,
2780 .ioctl
= sock_no_ioctl
,
2781 .listen
= sock_no_listen
,
2782 .shutdown
= sock_no_shutdown
,
2783 .setsockopt
= sock_no_setsockopt
,
2784 .getsockopt
= sock_no_getsockopt
,
2785 .mmap
= sock_no_mmap
,
2786 .sendpage
= sock_no_sendpage
,
2788 /* Now the operations that really occur. */
2789 .release
= pfkey_release
,
2790 .poll
= datagram_poll
,
2791 .sendmsg
= pfkey_sendmsg
,
2792 .recvmsg
= pfkey_recvmsg
,
2795 static struct net_proto_family pfkey_family_ops
= {
2797 .create
= pfkey_create
,
2798 .owner
= THIS_MODULE
,
2801 #ifdef CONFIG_PROC_FS
2802 static int pfkey_read_proc(char *buffer
, char **start
, off_t offset
,
2803 int length
, int *eof
, void *data
)
2809 struct hlist_node
*node
;
2811 len
+= sprintf(buffer
,"sk RefCnt Rmem Wmem User Inode\n");
2813 read_lock(&pfkey_table_lock
);
2815 sk_for_each(s
, node
, &pfkey_table
) {
2816 len
+= sprintf(buffer
+len
,"%p %-6d %-6u %-6u %-6u %-6lu",
2818 atomic_read(&s
->sk_refcnt
),
2819 atomic_read(&s
->sk_rmem_alloc
),
2820 atomic_read(&s
->sk_wmem_alloc
),
2825 buffer
[len
++] = '\n';
2832 if(pos
> offset
+ length
)
2838 read_unlock(&pfkey_table_lock
);
2840 *start
= buffer
+ (offset
- begin
);
2841 len
-= (offset
- begin
);
2852 static struct xfrm_mgr pfkeyv2_mgr
=
2855 .notify
= pfkey_send_notify
,
2856 .acquire
= pfkey_send_acquire
,
2857 .compile_policy
= pfkey_compile_policy
,
2858 .new_mapping
= pfkey_send_new_mapping
,
2861 static void __exit
ipsec_pfkey_exit(void)
2863 xfrm_unregister_km(&pfkeyv2_mgr
);
2864 remove_proc_entry("net/pfkey", NULL
);
2865 sock_unregister(PF_KEY
);
2866 proto_unregister(&key_proto
);
2869 static int __init
ipsec_pfkey_init(void)
2871 int err
= proto_register(&key_proto
, 0);
2876 err
= sock_register(&pfkey_family_ops
);
2878 goto out_unregister_key_proto
;
2879 #ifdef CONFIG_PROC_FS
2881 if (create_proc_read_entry("net/pfkey", 0, NULL
, pfkey_read_proc
, NULL
) == NULL
)
2882 goto out_sock_unregister
;
2884 err
= xfrm_register_km(&pfkeyv2_mgr
);
2886 goto out_remove_proc_entry
;
2889 out_remove_proc_entry
:
2890 #ifdef CONFIG_PROC_FS
2891 remove_proc_entry("net/pfkey", NULL
);
2892 out_sock_unregister
:
2894 sock_unregister(PF_KEY
);
2895 out_unregister_key_proto
:
2896 proto_unregister(&key_proto
);
2900 module_init(ipsec_pfkey_init
);
2901 module_exit(ipsec_pfkey_exit
);
2902 MODULE_LICENSE("GPL");
2903 MODULE_ALIAS_NETPROTO(PF_KEY
);