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 <linux/slab.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
39 static unsigned int pfkey_net_id __read_mostly
;
41 /* List of all pfkey sockets. */
42 struct hlist_head table
;
45 static DEFINE_MUTEX(pfkey_mutex
);
48 static const struct xfrm_mark dummy_mark
= {0, 0};
50 /* struct sock must be the first member of struct pfkey_sock */
58 int (*dump
)(struct pfkey_sock
*sk
);
59 void (*done
)(struct pfkey_sock
*sk
);
61 struct xfrm_policy_walk policy
;
62 struct xfrm_state_walk state
;
66 struct mutex dump_lock
;
69 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
70 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
73 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
75 return (struct pfkey_sock
*)sk
;
78 static int pfkey_can_dump(const struct sock
*sk
)
80 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
85 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
89 kfree_skb(pfk
->dump
.skb
);
93 pfk
->dump
.dump
= NULL
;
94 pfk
->dump
.done
= NULL
;
98 static void pfkey_sock_destruct(struct sock
*sk
)
100 struct net
*net
= sock_net(sk
);
101 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
103 pfkey_terminate_dump(pfkey_sk(sk
));
104 skb_queue_purge(&sk
->sk_receive_queue
);
106 if (!sock_flag(sk
, SOCK_DEAD
)) {
107 pr_err("Attempt to release alive pfkey socket: %p\n", sk
);
111 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
112 WARN_ON(refcount_read(&sk
->sk_wmem_alloc
));
114 atomic_dec(&net_pfkey
->socks_nr
);
117 static const struct proto_ops pfkey_ops
;
119 static void pfkey_insert(struct sock
*sk
)
121 struct net
*net
= sock_net(sk
);
122 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
124 mutex_lock(&pfkey_mutex
);
125 sk_add_node_rcu(sk
, &net_pfkey
->table
);
126 mutex_unlock(&pfkey_mutex
);
129 static void pfkey_remove(struct sock
*sk
)
131 mutex_lock(&pfkey_mutex
);
132 sk_del_node_init_rcu(sk
);
133 mutex_unlock(&pfkey_mutex
);
136 static struct proto key_proto
= {
138 .owner
= THIS_MODULE
,
139 .obj_size
= sizeof(struct pfkey_sock
),
142 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
,
145 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
147 struct pfkey_sock
*pfk
;
150 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
152 if (sock
->type
!= SOCK_RAW
)
153 return -ESOCKTNOSUPPORT
;
154 if (protocol
!= PF_KEY_V2
)
155 return -EPROTONOSUPPORT
;
158 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
, kern
);
163 mutex_init(&pfk
->dump_lock
);
165 sock
->ops
= &pfkey_ops
;
166 sock_init_data(sock
, sk
);
168 sk
->sk_family
= PF_KEY
;
169 sk
->sk_destruct
= pfkey_sock_destruct
;
171 atomic_inc(&net_pfkey
->socks_nr
);
180 static int pfkey_release(struct socket
*sock
)
182 struct sock
*sk
= sock
->sk
;
191 skb_queue_purge(&sk
->sk_write_queue
);
199 static int pfkey_broadcast_one(struct sk_buff
*skb
, gfp_t allocation
,
204 if (atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
)
207 skb
= skb_clone(skb
, allocation
);
210 skb_set_owner_r(skb
, sk
);
211 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
212 sk
->sk_data_ready(sk
);
218 /* Send SKB to all pfkey sockets matching selected criteria. */
219 #define BROADCAST_ALL 0
220 #define BROADCAST_ONE 1
221 #define BROADCAST_REGISTERED 2
222 #define BROADCAST_PROMISC_ONLY 4
223 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
224 int broadcast_flags
, struct sock
*one_sk
,
227 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
231 /* XXX Do we need something like netlink_overrun? I think
232 * XXX PF_KEY socket apps will not mind current behavior.
238 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
239 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
242 /* Yes, it means that if you are meant to receive this
243 * pfkey message you receive it twice as promiscuous
247 pfkey_broadcast_one(skb
, GFP_ATOMIC
, sk
);
249 /* the exact target will be processed later */
252 if (broadcast_flags
!= BROADCAST_ALL
) {
253 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
255 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
258 if (broadcast_flags
& BROADCAST_ONE
)
262 err2
= pfkey_broadcast_one(skb
, GFP_ATOMIC
, sk
);
264 /* Error is cleared after successful sending to at least one
266 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
272 err
= pfkey_broadcast_one(skb
, allocation
, one_sk
);
278 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
280 struct sadb_msg
*hdr
;
283 mutex_lock(&pfk
->dump_lock
);
284 if (!pfk
->dump
.dump
) {
289 rc
= pfk
->dump
.dump(pfk
);
290 if (rc
== -ENOBUFS
) {
296 if (!pfkey_can_dump(&pfk
->sk
)) {
301 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
302 hdr
->sadb_msg_seq
= 0;
303 hdr
->sadb_msg_errno
= rc
;
304 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
305 &pfk
->sk
, sock_net(&pfk
->sk
));
306 pfk
->dump
.skb
= NULL
;
309 pfkey_terminate_dump(pfk
);
312 mutex_unlock(&pfk
->dump_lock
);
316 static inline void pfkey_hdr_dup(struct sadb_msg
*new,
317 const struct sadb_msg
*orig
)
322 static int pfkey_error(const struct sadb_msg
*orig
, int err
, struct sock
*sk
)
324 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
325 struct sadb_msg
*hdr
;
330 /* Woe be to the platform trying to support PFKEY yet
331 * having normal errnos outside the 1-255 range, inclusive.
334 if (err
== ERESTARTSYS
||
335 err
== ERESTARTNOHAND
||
336 err
== ERESTARTNOINTR
)
340 BUG_ON(err
<= 0 || err
>= 256);
342 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
343 pfkey_hdr_dup(hdr
, orig
);
344 hdr
->sadb_msg_errno
= (uint8_t) err
;
345 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
348 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, sock_net(sk
));
353 static const u8 sadb_ext_min_len
[] = {
354 [SADB_EXT_RESERVED
] = (u8
) 0,
355 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
356 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
357 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
358 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
359 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
360 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
361 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
362 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
363 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
364 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
365 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
366 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
367 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
368 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
369 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
370 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
371 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
372 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
373 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
374 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
375 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
376 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
377 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
378 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
379 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
380 [SADB_X_EXT_FILTER
] = (u8
) sizeof(struct sadb_x_filter
),
383 /* Verify sadb_address_{len,prefixlen} against sa_family. */
384 static int verify_address_len(const void *p
)
386 const struct sadb_address
*sp
= p
;
387 const struct sockaddr
*addr
= (const struct sockaddr
*)(sp
+ 1);
388 const struct sockaddr_in
*sin
;
389 #if IS_ENABLED(CONFIG_IPV6)
390 const struct sockaddr_in6
*sin6
;
394 if (sp
->sadb_address_len
<
395 DIV_ROUND_UP(sizeof(*sp
) + offsetofend(typeof(*addr
), sa_family
),
399 switch (addr
->sa_family
) {
401 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
402 if (sp
->sadb_address_len
!= len
||
403 sp
->sadb_address_prefixlen
> 32)
406 #if IS_ENABLED(CONFIG_IPV6)
408 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
409 if (sp
->sadb_address_len
!= len
||
410 sp
->sadb_address_prefixlen
> 128)
415 /* It is user using kernel to keep track of security
416 * associations for another protocol, such as
417 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
420 * XXX Actually, association/policy database is not yet
421 * XXX able to cope with arbitrary sockaddr families.
422 * XXX When it can, remove this -EINVAL. -DaveM
430 static inline int sadb_key_len(const struct sadb_key
*key
)
432 int key_bytes
= DIV_ROUND_UP(key
->sadb_key_bits
, 8);
434 return DIV_ROUND_UP(sizeof(struct sadb_key
) + key_bytes
,
438 static int verify_key_len(const void *p
)
440 const struct sadb_key
*key
= p
;
442 if (sadb_key_len(key
) > key
->sadb_key_len
)
448 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx
*sec_ctx
)
450 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
451 sec_ctx
->sadb_x_ctx_len
,
455 static inline int verify_sec_ctx_len(const void *p
)
457 const struct sadb_x_sec_ctx
*sec_ctx
= p
;
458 int len
= sec_ctx
->sadb_x_ctx_len
;
463 len
= pfkey_sec_ctx_len(sec_ctx
);
465 if (sec_ctx
->sadb_x_sec_len
!= len
)
471 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx
*sec_ctx
,
474 struct xfrm_user_sec_ctx
*uctx
= NULL
;
475 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
477 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), gfp
);
482 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
483 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
484 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
485 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
486 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
487 memcpy(uctx
+ 1, sec_ctx
+ 1,
493 static int present_and_same_family(const struct sadb_address
*src
,
494 const struct sadb_address
*dst
)
496 const struct sockaddr
*s_addr
, *d_addr
;
501 s_addr
= (const struct sockaddr
*)(src
+ 1);
502 d_addr
= (const struct sockaddr
*)(dst
+ 1);
503 if (s_addr
->sa_family
!= d_addr
->sa_family
)
505 if (s_addr
->sa_family
!= AF_INET
506 #if IS_ENABLED(CONFIG_IPV6)
507 && s_addr
->sa_family
!= AF_INET6
515 static int parse_exthdrs(struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void **ext_hdrs
)
517 const char *p
= (char *) hdr
;
523 const struct sadb_ext
*ehdr
= (const struct sadb_ext
*) p
;
527 if (len
< sizeof(*ehdr
))
530 ext_len
= ehdr
->sadb_ext_len
;
531 ext_len
*= sizeof(uint64_t);
532 ext_type
= ehdr
->sadb_ext_type
;
533 if (ext_len
< sizeof(uint64_t) ||
535 ext_type
== SADB_EXT_RESERVED
)
538 if (ext_type
<= SADB_EXT_MAX
) {
539 int min
= (int) sadb_ext_min_len
[ext_type
];
542 if (ext_hdrs
[ext_type
-1] != NULL
)
545 case SADB_EXT_ADDRESS_SRC
:
546 case SADB_EXT_ADDRESS_DST
:
547 case SADB_EXT_ADDRESS_PROXY
:
548 case SADB_X_EXT_NAT_T_OA
:
549 if (verify_address_len(p
))
552 case SADB_X_EXT_SEC_CTX
:
553 if (verify_sec_ctx_len(p
))
556 case SADB_EXT_KEY_AUTH
:
557 case SADB_EXT_KEY_ENCRYPT
:
558 if (verify_key_len(p
))
564 ext_hdrs
[ext_type
-1] = (void *) p
;
574 pfkey_satype2proto(uint8_t satype
)
577 case SADB_SATYPE_UNSPEC
:
578 return IPSEC_PROTO_ANY
;
581 case SADB_SATYPE_ESP
:
583 case SADB_X_SATYPE_IPCOMP
:
592 pfkey_proto2satype(uint16_t proto
)
596 return SADB_SATYPE_AH
;
598 return SADB_SATYPE_ESP
;
600 return SADB_X_SATYPE_IPCOMP
;
607 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
608 * say specifically 'just raw sockets' as we encode them as 255.
611 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
613 return proto
== IPSEC_PROTO_ANY
? 0 : proto
;
616 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
618 return proto
? proto
: IPSEC_PROTO_ANY
;
621 static inline int pfkey_sockaddr_len(sa_family_t family
)
625 return sizeof(struct sockaddr_in
);
626 #if IS_ENABLED(CONFIG_IPV6)
628 return sizeof(struct sockaddr_in6
);
635 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
637 switch (sa
->sa_family
) {
640 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
642 #if IS_ENABLED(CONFIG_IPV6)
645 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
646 sizeof(struct in6_addr
));
654 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
656 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
660 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct net
*net
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
662 const struct sadb_sa
*sa
;
663 const struct sadb_address
*addr
;
665 unsigned short family
;
666 xfrm_address_t
*xaddr
;
668 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
672 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
676 /* sadb_address_len should be checked by caller */
677 addr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
681 family
= ((const struct sockaddr
*)(addr
+ 1))->sa_family
;
684 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
686 #if IS_ENABLED(CONFIG_IPV6)
688 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
698 return xfrm_state_lookup(net
, DUMMY_MARK
, xaddr
, sa
->sadb_sa_spi
, proto
, family
);
701 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
704 pfkey_sockaddr_size(sa_family_t family
)
706 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
709 static inline int pfkey_mode_from_xfrm(int mode
)
712 case XFRM_MODE_TRANSPORT
:
713 return IPSEC_MODE_TRANSPORT
;
714 case XFRM_MODE_TUNNEL
:
715 return IPSEC_MODE_TUNNEL
;
717 return IPSEC_MODE_BEET
;
723 static inline int pfkey_mode_to_xfrm(int mode
)
726 case IPSEC_MODE_ANY
: /*XXX*/
727 case IPSEC_MODE_TRANSPORT
:
728 return XFRM_MODE_TRANSPORT
;
729 case IPSEC_MODE_TUNNEL
:
730 return XFRM_MODE_TUNNEL
;
731 case IPSEC_MODE_BEET
:
732 return XFRM_MODE_BEET
;
738 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t
*xaddr
, __be16 port
,
740 unsigned short family
)
745 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
746 sin
->sin_family
= AF_INET
;
747 sin
->sin_port
= port
;
748 sin
->sin_addr
.s_addr
= xaddr
->a4
;
749 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
752 #if IS_ENABLED(CONFIG_IPV6)
755 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
756 sin6
->sin6_family
= AF_INET6
;
757 sin6
->sin6_port
= port
;
758 sin6
->sin6_flowinfo
= 0;
759 sin6
->sin6_addr
= xaddr
->in6
;
760 sin6
->sin6_scope_id
= 0;
768 static struct sk_buff
*__pfkey_xfrm_state2msg(const struct xfrm_state
*x
,
769 int add_keys
, int hsc
)
772 struct sadb_msg
*hdr
;
774 struct sadb_lifetime
*lifetime
;
775 struct sadb_address
*addr
;
776 struct sadb_key
*key
;
777 struct sadb_x_sa2
*sa2
;
778 struct sadb_x_sec_ctx
*sec_ctx
;
779 struct xfrm_sec_ctx
*xfrm_ctx
;
782 int auth_key_size
= 0;
783 int encrypt_key_size
= 0;
785 struct xfrm_encap_tmpl
*natt
= NULL
;
788 /* address family check */
789 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
791 return ERR_PTR(-EINVAL
);
793 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
794 key(AE), (identity(SD),) (sensitivity)> */
795 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
796 sizeof(struct sadb_lifetime
) +
797 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
798 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
799 sizeof(struct sadb_address
)*2 +
801 sizeof(struct sadb_x_sa2
);
803 if ((xfrm_ctx
= x
->security
)) {
804 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
805 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
808 /* identity & sensitivity */
809 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
810 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
813 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
815 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
816 size
+= sizeof(struct sadb_key
) + auth_key_size
;
818 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
820 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
821 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
827 if (natt
&& natt
->encap_type
) {
828 size
+= sizeof(struct sadb_x_nat_t_type
);
829 size
+= sizeof(struct sadb_x_nat_t_port
);
830 size
+= sizeof(struct sadb_x_nat_t_port
);
833 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
835 return ERR_PTR(-ENOBUFS
);
837 /* call should fill header later */
838 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
839 memset(hdr
, 0, size
); /* XXX do we need this ? */
840 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
843 sa
= skb_put(skb
, sizeof(struct sadb_sa
));
844 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
845 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
846 sa
->sadb_sa_spi
= x
->id
.spi
;
847 sa
->sadb_sa_replay
= x
->props
.replay_window
;
848 switch (x
->km
.state
) {
849 case XFRM_STATE_VALID
:
850 sa
->sadb_sa_state
= x
->km
.dying
?
851 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
854 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
857 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
860 sa
->sadb_sa_auth
= 0;
862 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
863 sa
->sadb_sa_auth
= (a
&& a
->pfkey_supported
) ?
864 a
->desc
.sadb_alg_id
: 0;
866 sa
->sadb_sa_encrypt
= 0;
867 BUG_ON(x
->ealg
&& x
->calg
);
869 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
870 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
871 a
->desc
.sadb_alg_id
: 0;
873 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
875 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
876 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
877 a
->desc
.sadb_alg_id
: 0;
880 sa
->sadb_sa_flags
= 0;
881 if (x
->props
.flags
& XFRM_STATE_NOECN
)
882 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
883 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
884 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
885 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
886 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
890 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
891 lifetime
->sadb_lifetime_len
=
892 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
893 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
894 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
895 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
896 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
897 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
901 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
902 lifetime
->sadb_lifetime_len
=
903 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
904 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
905 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
906 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
907 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
908 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
911 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
912 lifetime
->sadb_lifetime_len
=
913 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
914 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
915 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
916 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
917 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
918 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
920 addr
= skb_put(skb
, 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_SRC
;
925 /* "if the ports are non-zero, then the sadb_address_proto field,
926 normally zero, MUST be filled in with the transport
927 protocol's number." - RFC2367 */
928 addr
->sadb_address_proto
= 0;
929 addr
->sadb_address_reserved
= 0;
931 addr
->sadb_address_prefixlen
=
932 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
933 (struct sockaddr
*) (addr
+ 1),
935 if (!addr
->sadb_address_prefixlen
)
939 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
940 addr
->sadb_address_len
=
941 (sizeof(struct sadb_address
)+sockaddr_size
)/
943 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
944 addr
->sadb_address_proto
= 0;
945 addr
->sadb_address_reserved
= 0;
947 addr
->sadb_address_prefixlen
=
948 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
949 (struct sockaddr
*) (addr
+ 1),
951 if (!addr
->sadb_address_prefixlen
)
954 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
,
957 sizeof(struct sadb_address
) + sockaddr_size
);
958 addr
->sadb_address_len
=
959 (sizeof(struct sadb_address
)+sockaddr_size
)/
961 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
962 addr
->sadb_address_proto
=
963 pfkey_proto_from_xfrm(x
->sel
.proto
);
964 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
965 addr
->sadb_address_reserved
= 0;
967 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
968 (struct sockaddr
*) (addr
+ 1),
973 if (add_keys
&& auth_key_size
) {
974 key
= skb_put(skb
, sizeof(struct sadb_key
) + auth_key_size
);
975 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
977 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
978 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
979 key
->sadb_key_reserved
= 0;
980 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
983 if (add_keys
&& encrypt_key_size
) {
984 key
= skb_put(skb
, sizeof(struct sadb_key
) + encrypt_key_size
);
985 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
986 encrypt_key_size
) / sizeof(uint64_t);
987 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
988 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
989 key
->sadb_key_reserved
= 0;
990 memcpy(key
+ 1, x
->ealg
->alg_key
,
991 (x
->ealg
->alg_key_len
+7)/8);
995 sa2
= skb_put(skb
, sizeof(struct sadb_x_sa2
));
996 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
997 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
998 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
1000 return ERR_PTR(-EINVAL
);
1002 sa2
->sadb_x_sa2_mode
= mode
;
1003 sa2
->sadb_x_sa2_reserved1
= 0;
1004 sa2
->sadb_x_sa2_reserved2
= 0;
1005 sa2
->sadb_x_sa2_sequence
= 0;
1006 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
1008 if (natt
&& natt
->encap_type
) {
1009 struct sadb_x_nat_t_type
*n_type
;
1010 struct sadb_x_nat_t_port
*n_port
;
1013 n_type
= skb_put(skb
, sizeof(*n_type
));
1014 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
1015 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1016 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
1017 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
1018 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
1019 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
1022 n_port
= skb_put(skb
, sizeof(*n_port
));
1023 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1024 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1025 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
1026 n_port
->sadb_x_nat_t_port_reserved
= 0;
1029 n_port
= skb_put(skb
, sizeof(*n_port
));
1030 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1031 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
1032 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
1033 n_port
->sadb_x_nat_t_port_reserved
= 0;
1036 /* security context */
1038 sec_ctx
= skb_put(skb
,
1039 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1040 sec_ctx
->sadb_x_sec_len
=
1041 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1042 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1043 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1044 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1045 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1046 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1054 static inline struct sk_buff
*pfkey_xfrm_state2msg(const struct xfrm_state
*x
)
1056 struct sk_buff
*skb
;
1058 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1063 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(const struct xfrm_state
*x
,
1066 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1069 static struct xfrm_state
* pfkey_msg2xfrm_state(struct net
*net
,
1070 const struct sadb_msg
*hdr
,
1071 void * const *ext_hdrs
)
1073 struct xfrm_state
*x
;
1074 const struct sadb_lifetime
*lifetime
;
1075 const struct sadb_sa
*sa
;
1076 const struct sadb_key
*key
;
1077 const struct sadb_x_sec_ctx
*sec_ctx
;
1082 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
1084 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1085 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1086 return ERR_PTR(-EINVAL
);
1087 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1088 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1089 return ERR_PTR(-EINVAL
);
1090 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1091 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1092 return ERR_PTR(-EINVAL
);
1093 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1094 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1095 return ERR_PTR(-EINVAL
);
1097 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1099 return ERR_PTR(-EINVAL
);
1101 /* default error is no buffer space */
1106 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1107 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1108 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1109 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1110 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1113 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1114 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1116 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1117 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1118 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1119 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1120 return ERR_PTR(-EINVAL
);
1121 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1123 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1124 key
->sadb_key_bits
== 0)
1125 return ERR_PTR(-EINVAL
);
1126 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1128 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1129 key
->sadb_key_bits
== 0)
1130 return ERR_PTR(-EINVAL
);
1132 x
= xfrm_state_alloc(net
);
1134 return ERR_PTR(-ENOBUFS
);
1136 x
->id
.proto
= proto
;
1137 x
->id
.spi
= sa
->sadb_sa_spi
;
1138 x
->props
.replay_window
= min_t(unsigned int, sa
->sadb_sa_replay
,
1139 (sizeof(x
->replay
.bitmap
) * 8));
1140 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1141 x
->props
.flags
|= XFRM_STATE_NOECN
;
1142 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1143 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1144 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1145 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1147 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
- 1];
1148 if (lifetime
!= NULL
) {
1149 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1150 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1151 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1152 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1154 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
- 1];
1155 if (lifetime
!= NULL
) {
1156 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1157 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1158 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1159 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1162 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
1163 if (sec_ctx
!= NULL
) {
1164 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
1169 err
= security_xfrm_state_alloc(x
, uctx
);
1177 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1178 if (sa
->sadb_sa_auth
) {
1180 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1181 if (!a
|| !a
->pfkey_supported
) {
1186 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1187 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1192 strcpy(x
->aalg
->alg_name
, a
->name
);
1193 x
->aalg
->alg_key_len
= 0;
1195 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1196 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1198 x
->aalg
->alg_trunc_len
= a
->uinfo
.auth
.icv_truncbits
;
1199 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1200 /* x->algo.flags = sa->sadb_sa_flags; */
1202 if (sa
->sadb_sa_encrypt
) {
1203 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1204 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1205 if (!a
|| !a
->pfkey_supported
) {
1209 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1214 strcpy(x
->calg
->alg_name
, a
->name
);
1215 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1218 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1219 if (!a
|| !a
->pfkey_supported
) {
1223 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1225 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1226 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1231 strcpy(x
->ealg
->alg_name
, a
->name
);
1232 x
->ealg
->alg_key_len
= 0;
1234 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1235 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1237 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1238 x
->geniv
= a
->uinfo
.encr
.geniv
;
1241 /* x->algo.flags = sa->sadb_sa_flags; */
1243 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1245 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1248 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1249 const struct sadb_x_sa2
*sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1];
1250 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1255 x
->props
.mode
= mode
;
1256 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1259 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1260 const struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1262 /* Nobody uses this, but we try. */
1263 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1264 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1268 x
->sel
.family
= x
->props
.family
;
1270 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1271 const struct sadb_x_nat_t_type
* n_type
;
1272 struct xfrm_encap_tmpl
*natt
;
1274 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1281 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1282 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1284 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1285 const struct sadb_x_nat_t_port
*n_port
=
1286 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1287 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1289 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1290 const struct sadb_x_nat_t_port
*n_port
=
1291 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1292 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1294 memset(&natt
->encap_oa
, 0, sizeof(natt
->encap_oa
));
1297 err
= xfrm_init_state(x
);
1301 x
->km
.seq
= hdr
->sadb_msg_seq
;
1305 x
->km
.state
= XFRM_STATE_DEAD
;
1307 return ERR_PTR(err
);
1310 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1315 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1317 struct net
*net
= sock_net(sk
);
1318 struct sk_buff
*resp_skb
;
1319 struct sadb_x_sa2
*sa2
;
1320 struct sadb_address
*saddr
, *daddr
;
1321 struct sadb_msg
*out_hdr
;
1322 struct sadb_spirange
*range
;
1323 struct xfrm_state
*x
= NULL
;
1326 u32 min_spi
, max_spi
;
1329 unsigned short family
;
1330 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1332 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1333 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1336 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1340 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1341 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1344 reqid
= sa2
->sadb_x_sa2_reqid
;
1350 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1351 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1353 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1356 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1357 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1359 #if IS_ENABLED(CONFIG_IPV6)
1361 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1362 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1367 if (hdr
->sadb_msg_seq
) {
1368 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1369 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, xdaddr
, family
)) {
1376 x
= xfrm_find_acq(net
, &dummy_mark
, mode
, reqid
, 0, proto
, xdaddr
, xsaddr
, 1, family
);
1382 max_spi
= 0x0fffffff;
1384 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1386 min_spi
= range
->sadb_spirange_min
;
1387 max_spi
= range
->sadb_spirange_max
;
1390 err
= verify_spi_info(x
->id
.proto
, min_spi
, max_spi
);
1396 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1397 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1399 if (IS_ERR(resp_skb
)) {
1401 return PTR_ERR(resp_skb
);
1404 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1405 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1406 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1407 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1408 out_hdr
->sadb_msg_errno
= 0;
1409 out_hdr
->sadb_msg_reserved
= 0;
1410 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1411 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1415 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, net
);
1420 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1422 struct net
*net
= sock_net(sk
);
1423 struct xfrm_state
*x
;
1425 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1428 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1431 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1435 spin_lock_bh(&x
->lock
);
1436 if (x
->km
.state
== XFRM_STATE_ACQ
)
1437 x
->km
.state
= XFRM_STATE_ERROR
;
1439 spin_unlock_bh(&x
->lock
);
1444 static inline int event2poltype(int event
)
1447 case XFRM_MSG_DELPOLICY
:
1448 return SADB_X_SPDDELETE
;
1449 case XFRM_MSG_NEWPOLICY
:
1450 return SADB_X_SPDADD
;
1451 case XFRM_MSG_UPDPOLICY
:
1452 return SADB_X_SPDUPDATE
;
1453 case XFRM_MSG_POLEXPIRE
:
1454 // return SADB_X_SPDEXPIRE;
1456 pr_err("pfkey: Unknown policy event %d\n", event
);
1463 static inline int event2keytype(int event
)
1466 case XFRM_MSG_DELSA
:
1468 case XFRM_MSG_NEWSA
:
1470 case XFRM_MSG_UPDSA
:
1472 case XFRM_MSG_EXPIRE
:
1475 pr_err("pfkey: Unknown SA event %d\n", event
);
1483 static int key_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
1485 struct sk_buff
*skb
;
1486 struct sadb_msg
*hdr
;
1488 skb
= pfkey_xfrm_state2msg(x
);
1491 return PTR_ERR(skb
);
1493 hdr
= (struct sadb_msg
*) skb
->data
;
1494 hdr
->sadb_msg_version
= PF_KEY_V2
;
1495 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1496 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1497 hdr
->sadb_msg_errno
= 0;
1498 hdr
->sadb_msg_reserved
= 0;
1499 hdr
->sadb_msg_seq
= c
->seq
;
1500 hdr
->sadb_msg_pid
= c
->portid
;
1502 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xs_net(x
));
1507 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1509 struct net
*net
= sock_net(sk
);
1510 struct xfrm_state
*x
;
1514 x
= pfkey_msg2xfrm_state(net
, hdr
, ext_hdrs
);
1519 if (hdr
->sadb_msg_type
== SADB_ADD
)
1520 err
= xfrm_state_add(x
);
1522 err
= xfrm_state_update(x
);
1524 xfrm_audit_state_add(x
, err
? 0 : 1, true);
1527 x
->km
.state
= XFRM_STATE_DEAD
;
1528 __xfrm_state_put(x
);
1532 if (hdr
->sadb_msg_type
== SADB_ADD
)
1533 c
.event
= XFRM_MSG_NEWSA
;
1535 c
.event
= XFRM_MSG_UPDSA
;
1536 c
.seq
= hdr
->sadb_msg_seq
;
1537 c
.portid
= hdr
->sadb_msg_pid
;
1538 km_state_notify(x
, &c
);
1544 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1546 struct net
*net
= sock_net(sk
);
1547 struct xfrm_state
*x
;
1551 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1552 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1553 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1556 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1560 if ((err
= security_xfrm_state_delete(x
)))
1563 if (xfrm_state_kern(x
)) {
1568 err
= xfrm_state_delete(x
);
1573 c
.seq
= hdr
->sadb_msg_seq
;
1574 c
.portid
= hdr
->sadb_msg_pid
;
1575 c
.event
= XFRM_MSG_DELSA
;
1576 km_state_notify(x
, &c
);
1578 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
1584 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1586 struct net
*net
= sock_net(sk
);
1588 struct sk_buff
*out_skb
;
1589 struct sadb_msg
*out_hdr
;
1590 struct xfrm_state
*x
;
1592 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1593 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1594 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1597 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1601 out_skb
= pfkey_xfrm_state2msg(x
);
1602 proto
= x
->id
.proto
;
1604 if (IS_ERR(out_skb
))
1605 return PTR_ERR(out_skb
);
1607 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1608 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1609 out_hdr
->sadb_msg_type
= SADB_GET
;
1610 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1611 out_hdr
->sadb_msg_errno
= 0;
1612 out_hdr
->sadb_msg_reserved
= 0;
1613 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1614 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1615 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, sock_net(sk
));
1620 static struct sk_buff
*compose_sadb_supported(const struct sadb_msg
*orig
,
1623 struct sk_buff
*skb
;
1624 struct sadb_msg
*hdr
;
1625 int len
, auth_len
, enc_len
, i
;
1627 auth_len
= xfrm_count_pfkey_auth_supported();
1629 auth_len
*= sizeof(struct sadb_alg
);
1630 auth_len
+= sizeof(struct sadb_supported
);
1633 enc_len
= xfrm_count_pfkey_enc_supported();
1635 enc_len
*= sizeof(struct sadb_alg
);
1636 enc_len
+= sizeof(struct sadb_supported
);
1639 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1641 skb
= alloc_skb(len
+ 16, allocation
);
1645 hdr
= skb_put(skb
, sizeof(*hdr
));
1646 pfkey_hdr_dup(hdr
, orig
);
1647 hdr
->sadb_msg_errno
= 0;
1648 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1651 struct sadb_supported
*sp
;
1652 struct sadb_alg
*ap
;
1654 sp
= skb_put(skb
, auth_len
);
1655 ap
= (struct sadb_alg
*) (sp
+ 1);
1657 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1658 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1660 for (i
= 0; ; i
++) {
1661 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1664 if (!aalg
->pfkey_supported
)
1666 if (aalg
->available
)
1672 struct sadb_supported
*sp
;
1673 struct sadb_alg
*ap
;
1675 sp
= skb_put(skb
, enc_len
);
1676 ap
= (struct sadb_alg
*) (sp
+ 1);
1678 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1679 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1681 for (i
= 0; ; i
++) {
1682 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1685 if (!ealg
->pfkey_supported
)
1687 if (ealg
->available
)
1696 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1698 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1699 struct sk_buff
*supp_skb
;
1701 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1704 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1705 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1707 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1712 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1714 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1715 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1720 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
,
1725 static int unicast_flush_resp(struct sock
*sk
, const struct sadb_msg
*ihdr
)
1727 struct sk_buff
*skb
;
1728 struct sadb_msg
*hdr
;
1730 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1734 hdr
= skb_put_data(skb
, ihdr
, sizeof(struct sadb_msg
));
1735 hdr
->sadb_msg_errno
= (uint8_t) 0;
1736 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1738 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
,
1742 static int key_notify_sa_flush(const struct km_event
*c
)
1744 struct sk_buff
*skb
;
1745 struct sadb_msg
*hdr
;
1747 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1750 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
1751 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1752 hdr
->sadb_msg_type
= SADB_FLUSH
;
1753 hdr
->sadb_msg_seq
= c
->seq
;
1754 hdr
->sadb_msg_pid
= c
->portid
;
1755 hdr
->sadb_msg_version
= PF_KEY_V2
;
1756 hdr
->sadb_msg_errno
= (uint8_t) 0;
1757 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1758 hdr
->sadb_msg_reserved
= 0;
1760 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
1765 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1767 struct net
*net
= sock_net(sk
);
1772 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1776 err
= xfrm_state_flush(net
, proto
, true, false);
1777 err2
= unicast_flush_resp(sk
, hdr
);
1779 if (err
== -ESRCH
) /* empty table - go quietly */
1781 return err
? err
: err2
;
1784 c
.data
.proto
= proto
;
1785 c
.seq
= hdr
->sadb_msg_seq
;
1786 c
.portid
= hdr
->sadb_msg_pid
;
1787 c
.event
= XFRM_MSG_FLUSHSA
;
1789 km_state_notify(NULL
, &c
);
1794 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1796 struct pfkey_sock
*pfk
= ptr
;
1797 struct sk_buff
*out_skb
;
1798 struct sadb_msg
*out_hdr
;
1800 if (!pfkey_can_dump(&pfk
->sk
))
1803 out_skb
= pfkey_xfrm_state2msg(x
);
1804 if (IS_ERR(out_skb
))
1805 return PTR_ERR(out_skb
);
1807 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1808 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1809 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1810 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1811 out_hdr
->sadb_msg_errno
= 0;
1812 out_hdr
->sadb_msg_reserved
= 0;
1813 out_hdr
->sadb_msg_seq
= count
+ 1;
1814 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
1817 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
1818 &pfk
->sk
, sock_net(&pfk
->sk
));
1819 pfk
->dump
.skb
= out_skb
;
1824 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1826 struct net
*net
= sock_net(&pfk
->sk
);
1827 return xfrm_state_walk(net
, &pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1830 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1832 struct net
*net
= sock_net(&pfk
->sk
);
1834 xfrm_state_walk_done(&pfk
->dump
.u
.state
, net
);
1837 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1840 struct xfrm_address_filter
*filter
= NULL
;
1841 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1843 mutex_lock(&pfk
->dump_lock
);
1844 if (pfk
->dump
.dump
!= NULL
) {
1845 mutex_unlock(&pfk
->dump_lock
);
1849 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1851 mutex_unlock(&pfk
->dump_lock
);
1855 if (ext_hdrs
[SADB_X_EXT_FILTER
- 1]) {
1856 struct sadb_x_filter
*xfilter
= ext_hdrs
[SADB_X_EXT_FILTER
- 1];
1858 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
1859 if (filter
== NULL
) {
1860 mutex_unlock(&pfk
->dump_lock
);
1864 memcpy(&filter
->saddr
, &xfilter
->sadb_x_filter_saddr
,
1865 sizeof(xfrm_address_t
));
1866 memcpy(&filter
->daddr
, &xfilter
->sadb_x_filter_daddr
,
1867 sizeof(xfrm_address_t
));
1868 filter
->family
= xfilter
->sadb_x_filter_family
;
1869 filter
->splen
= xfilter
->sadb_x_filter_splen
;
1870 filter
->dplen
= xfilter
->sadb_x_filter_dplen
;
1873 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1874 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
1875 pfk
->dump
.dump
= pfkey_dump_sa
;
1876 pfk
->dump
.done
= pfkey_dump_sa_done
;
1877 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
, filter
);
1878 mutex_unlock(&pfk
->dump_lock
);
1880 return pfkey_do_dump(pfk
);
1883 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1885 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1886 int satype
= hdr
->sadb_msg_satype
;
1887 bool reset_errno
= false;
1889 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1891 if (satype
!= 0 && satype
!= 1)
1893 pfk
->promisc
= satype
;
1895 if (reset_errno
&& skb_cloned(skb
))
1896 skb
= skb_copy(skb
, GFP_KERNEL
);
1898 skb
= skb_clone(skb
, GFP_KERNEL
);
1900 if (reset_errno
&& skb
) {
1901 struct sadb_msg
*new_hdr
= (struct sadb_msg
*) skb
->data
;
1902 new_hdr
->sadb_msg_errno
= 0;
1905 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ALL
, NULL
, sock_net(sk
));
1909 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1912 u32 reqid
= *(u32
*)ptr
;
1914 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1915 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1921 static u32
gen_reqid(struct net
*net
)
1923 struct xfrm_policy_walk walk
;
1926 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1932 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1933 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1934 rc
= xfrm_policy_walk(net
, &walk
, check_reqid
, (void*)&reqid
);
1935 xfrm_policy_walk_done(&walk
, net
);
1938 } while (reqid
!= start
);
1943 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1945 struct net
*net
= xp_net(xp
);
1946 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1949 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1952 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1954 if (!xfrm_id_proto_valid(rq
->sadb_x_ipsecrequest_proto
))
1957 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
;
1958 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1961 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1963 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1964 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1965 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1967 if (!t
->reqid
&& !(t
->reqid
= gen_reqid(net
)))
1971 /* addresses present only in tunnel mode */
1972 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1975 err
= parse_sockaddr_pair(
1976 (struct sockaddr
*)(rq
+ 1),
1977 rq
->sadb_x_ipsecrequest_len
- sizeof(*rq
),
1978 &t
->saddr
, &t
->id
.daddr
, &t
->encap_family
);
1982 t
->encap_family
= xp
->family
;
1984 /* No way to set this via kame pfkey */
1991 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1994 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1995 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1997 if (pol
->sadb_x_policy_len
* 8 < sizeof(struct sadb_x_policy
))
2000 while (len
>= sizeof(*rq
)) {
2001 if (len
< rq
->sadb_x_ipsecrequest_len
||
2002 rq
->sadb_x_ipsecrequest_len
< sizeof(*rq
))
2005 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
2007 len
-= rq
->sadb_x_ipsecrequest_len
;
2008 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
2013 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy
*xp
)
2015 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
2018 int len
= sizeof(struct sadb_x_sec_ctx
);
2019 len
+= xfrm_ctx
->ctx_len
;
2020 return PFKEY_ALIGN8(len
);
2025 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy
*xp
)
2027 const struct xfrm_tmpl
*t
;
2028 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2032 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2033 t
= xp
->xfrm_vec
+ i
;
2034 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
2037 return sizeof(struct sadb_msg
) +
2038 (sizeof(struct sadb_lifetime
) * 3) +
2039 (sizeof(struct sadb_address
) * 2) +
2040 (sockaddr_size
* 2) +
2041 sizeof(struct sadb_x_policy
) +
2042 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
2044 pfkey_xfrm_policy2sec_ctx_size(xp
);
2047 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(const struct xfrm_policy
*xp
)
2049 struct sk_buff
*skb
;
2052 size
= pfkey_xfrm_policy2msg_size(xp
);
2054 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2056 return ERR_PTR(-ENOBUFS
);
2061 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, const struct xfrm_policy
*xp
, int dir
)
2063 struct sadb_msg
*hdr
;
2064 struct sadb_address
*addr
;
2065 struct sadb_lifetime
*lifetime
;
2066 struct sadb_x_policy
*pol
;
2067 struct sadb_x_sec_ctx
*sec_ctx
;
2068 struct xfrm_sec_ctx
*xfrm_ctx
;
2071 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2072 int socklen
= pfkey_sockaddr_len(xp
->family
);
2074 size
= pfkey_xfrm_policy2msg_size(xp
);
2076 /* call should fill header later */
2077 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
2078 memset(hdr
, 0, size
); /* XXX do we need this ? */
2081 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
2082 addr
->sadb_address_len
=
2083 (sizeof(struct sadb_address
)+sockaddr_size
)/
2085 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2086 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2087 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
2088 addr
->sadb_address_reserved
= 0;
2089 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2091 (struct sockaddr
*) (addr
+ 1),
2096 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
2097 addr
->sadb_address_len
=
2098 (sizeof(struct sadb_address
)+sockaddr_size
)/
2100 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2101 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2102 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2103 addr
->sadb_address_reserved
= 0;
2105 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2106 (struct sockaddr
*) (addr
+ 1),
2110 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2111 lifetime
->sadb_lifetime_len
=
2112 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2113 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2114 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2115 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2116 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2117 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2119 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2120 lifetime
->sadb_lifetime_len
=
2121 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2122 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2123 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2124 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2125 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2126 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2128 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2129 lifetime
->sadb_lifetime_len
=
2130 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2131 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2132 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2133 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2134 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2135 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2137 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
2138 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2139 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2140 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2141 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2143 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2145 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2147 pol
->sadb_x_policy_dir
= dir
+1;
2148 pol
->sadb_x_policy_reserved
= 0;
2149 pol
->sadb_x_policy_id
= xp
->index
;
2150 pol
->sadb_x_policy_priority
= xp
->priority
;
2152 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2153 const struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2154 struct sadb_x_ipsecrequest
*rq
;
2158 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2159 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2160 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2161 req_size
+= socklen
* 2;
2165 rq
= skb_put(skb
, req_size
);
2166 pol
->sadb_x_policy_len
+= req_size
/8;
2167 memset(rq
, 0, sizeof(*rq
));
2168 rq
->sadb_x_ipsecrequest_len
= req_size
;
2169 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2170 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2172 rq
->sadb_x_ipsecrequest_mode
= mode
;
2173 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2175 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2177 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2178 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2180 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2181 u8
*sa
= (void *)(rq
+ 1);
2182 pfkey_sockaddr_fill(&t
->saddr
, 0,
2183 (struct sockaddr
*)sa
,
2185 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2186 (struct sockaddr
*) (sa
+ socklen
),
2191 /* security context */
2192 if ((xfrm_ctx
= xp
->security
)) {
2193 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2195 sec_ctx
= skb_put(skb
, ctx_size
);
2196 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2197 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2198 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2199 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2200 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2201 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2205 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2206 hdr
->sadb_msg_reserved
= refcount_read(&xp
->refcnt
);
2211 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2213 struct sk_buff
*out_skb
;
2214 struct sadb_msg
*out_hdr
;
2217 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2218 if (IS_ERR(out_skb
))
2219 return PTR_ERR(out_skb
);
2221 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2227 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2228 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2230 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2231 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2233 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2234 out_hdr
->sadb_msg_errno
= 0;
2235 out_hdr
->sadb_msg_seq
= c
->seq
;
2236 out_hdr
->sadb_msg_pid
= c
->portid
;
2237 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xp_net(xp
));
2242 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2244 struct net
*net
= sock_net(sk
);
2246 struct sadb_lifetime
*lifetime
;
2247 struct sadb_address
*sa
;
2248 struct sadb_x_policy
*pol
;
2249 struct xfrm_policy
*xp
;
2251 struct sadb_x_sec_ctx
*sec_ctx
;
2253 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2254 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2255 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2258 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2259 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2261 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2264 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2268 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2269 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2270 xp
->priority
= pol
->sadb_x_policy_priority
;
2272 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2273 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2274 xp
->selector
.family
= xp
->family
;
2275 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2276 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2277 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2278 if (xp
->selector
.sport
)
2279 xp
->selector
.sport_mask
= htons(0xffff);
2281 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2282 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2283 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2285 /* Amusing, we set this twice. KAME apps appear to set same value
2286 * in both addresses.
2288 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2290 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2291 if (xp
->selector
.dport
)
2292 xp
->selector
.dport_mask
= htons(0xffff);
2294 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2295 if (sec_ctx
!= NULL
) {
2296 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2303 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_KERNEL
);
2310 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2311 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2312 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2313 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2314 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2315 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2316 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2317 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2318 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2320 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2321 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2322 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2323 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2324 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2327 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2328 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2331 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2332 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2334 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
2339 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2340 c
.event
= XFRM_MSG_UPDPOLICY
;
2342 c
.event
= XFRM_MSG_NEWPOLICY
;
2344 c
.seq
= hdr
->sadb_msg_seq
;
2345 c
.portid
= hdr
->sadb_msg_pid
;
2347 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2353 xfrm_policy_destroy(xp
);
2357 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2359 struct net
*net
= sock_net(sk
);
2361 struct sadb_address
*sa
;
2362 struct sadb_x_policy
*pol
;
2363 struct xfrm_policy
*xp
;
2364 struct xfrm_selector sel
;
2366 struct sadb_x_sec_ctx
*sec_ctx
;
2367 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2369 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2370 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2371 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2374 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2375 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2378 memset(&sel
, 0, sizeof(sel
));
2380 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2381 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2382 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2383 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2384 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2386 sel
.sport_mask
= htons(0xffff);
2388 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2389 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2390 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2391 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2392 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2394 sel
.dport_mask
= htons(0xffff);
2396 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2397 if (sec_ctx
!= NULL
) {
2398 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2403 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
, GFP_KERNEL
);
2409 xp
= xfrm_policy_bysel_ctx(net
, DUMMY_MARK
, 0, XFRM_POLICY_TYPE_MAIN
,
2410 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2412 security_xfrm_policy_free(pol_ctx
);
2416 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2421 c
.seq
= hdr
->sadb_msg_seq
;
2422 c
.portid
= hdr
->sadb_msg_pid
;
2424 c
.event
= XFRM_MSG_DELPOLICY
;
2425 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2432 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, const struct sadb_msg
*hdr
, int dir
)
2435 struct sk_buff
*out_skb
;
2436 struct sadb_msg
*out_hdr
;
2439 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2440 if (IS_ERR(out_skb
)) {
2441 err
= PTR_ERR(out_skb
);
2444 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2448 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2449 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2450 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2451 out_hdr
->sadb_msg_satype
= 0;
2452 out_hdr
->sadb_msg_errno
= 0;
2453 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2454 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2455 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, xp_net(xp
));
2462 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2464 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2467 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2468 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2473 if (ext_len
< 2 || ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2476 af
= pfkey_sockaddr_extract(sa
, saddr
);
2480 socklen
= pfkey_sockaddr_len(af
);
2481 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2489 #ifdef CONFIG_NET_KEY_MIGRATE
2490 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2491 struct xfrm_migrate
*m
)
2494 struct sadb_x_ipsecrequest
*rq2
;
2497 if (len
< sizeof(*rq1
) ||
2498 len
< rq1
->sadb_x_ipsecrequest_len
||
2499 rq1
->sadb_x_ipsecrequest_len
< sizeof(*rq1
))
2503 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2504 rq1
->sadb_x_ipsecrequest_len
- sizeof(*rq1
),
2505 &m
->old_saddr
, &m
->old_daddr
,
2510 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2511 len
-= rq1
->sadb_x_ipsecrequest_len
;
2513 if (len
<= sizeof(*rq2
) ||
2514 len
< rq2
->sadb_x_ipsecrequest_len
||
2515 rq2
->sadb_x_ipsecrequest_len
< sizeof(*rq2
))
2519 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2520 rq2
->sadb_x_ipsecrequest_len
- sizeof(*rq2
),
2521 &m
->new_saddr
, &m
->new_daddr
,
2526 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2527 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2528 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2531 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2532 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2535 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2537 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2538 rq2
->sadb_x_ipsecrequest_len
));
2541 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2542 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2544 int i
, len
, ret
, err
= -EINVAL
;
2546 struct sadb_address
*sa
;
2547 struct sadb_x_kmaddress
*kma
;
2548 struct sadb_x_policy
*pol
;
2549 struct sadb_x_ipsecrequest
*rq
;
2550 struct xfrm_selector sel
;
2551 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2552 struct xfrm_kmaddress k
;
2553 struct net
*net
= sock_net(sk
);
2555 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2556 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2557 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2562 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2563 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2565 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2571 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2572 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2573 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2574 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2575 &k
.local
, &k
.remote
, &k
.family
);
2582 dir
= pol
->sadb_x_policy_dir
- 1;
2583 memset(&sel
, 0, sizeof(sel
));
2585 /* set source address info of selector */
2586 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2587 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2588 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2589 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2590 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2592 sel
.sport_mask
= htons(0xffff);
2594 /* set destination address info of selector */
2595 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
2596 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2597 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2598 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2599 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2601 sel
.dport_mask
= htons(0xffff);
2603 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2605 /* extract ipsecrequests */
2607 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2609 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2610 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2615 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2621 if (!i
|| len
> 0) {
2626 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2627 kma
? &k
: NULL
, net
, NULL
);
2633 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2634 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2636 return -ENOPROTOOPT
;
2641 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2643 struct net
*net
= sock_net(sk
);
2645 int err
= 0, delete;
2646 struct sadb_x_policy
*pol
;
2647 struct xfrm_policy
*xp
;
2650 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2653 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2654 if (dir
>= XFRM_POLICY_MAX
)
2657 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2658 xp
= xfrm_policy_byid(net
, DUMMY_MARK
, 0, XFRM_POLICY_TYPE_MAIN
,
2659 dir
, pol
->sadb_x_policy_id
, delete, &err
);
2664 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2668 c
.seq
= hdr
->sadb_msg_seq
;
2669 c
.portid
= hdr
->sadb_msg_pid
;
2671 c
.event
= XFRM_MSG_DELPOLICY
;
2672 km_policy_notify(xp
, dir
, &c
);
2674 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2682 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2684 struct pfkey_sock
*pfk
= ptr
;
2685 struct sk_buff
*out_skb
;
2686 struct sadb_msg
*out_hdr
;
2689 if (!pfkey_can_dump(&pfk
->sk
))
2692 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2693 if (IS_ERR(out_skb
))
2694 return PTR_ERR(out_skb
);
2696 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2700 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2701 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2702 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2703 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2704 out_hdr
->sadb_msg_errno
= 0;
2705 out_hdr
->sadb_msg_seq
= count
+ 1;
2706 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
2709 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
2710 &pfk
->sk
, sock_net(&pfk
->sk
));
2711 pfk
->dump
.skb
= out_skb
;
2716 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2718 struct net
*net
= sock_net(&pfk
->sk
);
2719 return xfrm_policy_walk(net
, &pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2722 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2724 struct net
*net
= sock_net((struct sock
*)pfk
);
2726 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
, net
);
2729 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2731 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2733 mutex_lock(&pfk
->dump_lock
);
2734 if (pfk
->dump
.dump
!= NULL
) {
2735 mutex_unlock(&pfk
->dump_lock
);
2739 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2740 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
2741 pfk
->dump
.dump
= pfkey_dump_sp
;
2742 pfk
->dump
.done
= pfkey_dump_sp_done
;
2743 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2744 mutex_unlock(&pfk
->dump_lock
);
2746 return pfkey_do_dump(pfk
);
2749 static int key_notify_policy_flush(const struct km_event
*c
)
2751 struct sk_buff
*skb_out
;
2752 struct sadb_msg
*hdr
;
2754 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2757 hdr
= skb_put(skb_out
, sizeof(struct sadb_msg
));
2758 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2759 hdr
->sadb_msg_seq
= c
->seq
;
2760 hdr
->sadb_msg_pid
= c
->portid
;
2761 hdr
->sadb_msg_version
= PF_KEY_V2
;
2762 hdr
->sadb_msg_errno
= (uint8_t) 0;
2763 hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2764 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2765 hdr
->sadb_msg_reserved
= 0;
2766 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
2771 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2773 struct net
*net
= sock_net(sk
);
2777 err
= xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, true);
2778 err2
= unicast_flush_resp(sk
, hdr
);
2780 if (err
== -ESRCH
) /* empty table - old silent behavior */
2785 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2786 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2787 c
.portid
= hdr
->sadb_msg_pid
;
2788 c
.seq
= hdr
->sadb_msg_seq
;
2790 km_policy_notify(NULL
, 0, &c
);
2795 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2796 const struct sadb_msg
*hdr
, void * const *ext_hdrs
);
2797 static const pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2798 [SADB_RESERVED
] = pfkey_reserved
,
2799 [SADB_GETSPI
] = pfkey_getspi
,
2800 [SADB_UPDATE
] = pfkey_add
,
2801 [SADB_ADD
] = pfkey_add
,
2802 [SADB_DELETE
] = pfkey_delete
,
2803 [SADB_GET
] = pfkey_get
,
2804 [SADB_ACQUIRE
] = pfkey_acquire
,
2805 [SADB_REGISTER
] = pfkey_register
,
2806 [SADB_EXPIRE
] = NULL
,
2807 [SADB_FLUSH
] = pfkey_flush
,
2808 [SADB_DUMP
] = pfkey_dump
,
2809 [SADB_X_PROMISC
] = pfkey_promisc
,
2810 [SADB_X_PCHANGE
] = NULL
,
2811 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2812 [SADB_X_SPDADD
] = pfkey_spdadd
,
2813 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2814 [SADB_X_SPDGET
] = pfkey_spdget
,
2815 [SADB_X_SPDACQUIRE
] = NULL
,
2816 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2817 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2818 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2819 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2820 [SADB_X_MIGRATE
] = pfkey_migrate
,
2823 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
)
2825 void *ext_hdrs
[SADB_EXT_MAX
];
2828 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2829 BROADCAST_PROMISC_ONLY
, NULL
, sock_net(sk
));
2831 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2832 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2835 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2836 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2841 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2843 struct sadb_msg
*hdr
= NULL
;
2845 if (skb
->len
< sizeof(*hdr
)) {
2848 hdr
= (struct sadb_msg
*) skb
->data
;
2849 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2850 hdr
->sadb_msg_reserved
!= 0 ||
2851 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2852 hdr
->sadb_msg_type
> SADB_MAX
)) {
2855 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2856 sizeof(uint64_t)) ||
2857 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2858 sizeof(uint64_t))) {
2868 static inline int aalg_tmpl_set(const struct xfrm_tmpl
*t
,
2869 const struct xfrm_algo_desc
*d
)
2871 unsigned int id
= d
->desc
.sadb_alg_id
;
2873 if (id
>= sizeof(t
->aalgos
) * 8)
2876 return (t
->aalgos
>> id
) & 1;
2879 static inline int ealg_tmpl_set(const struct xfrm_tmpl
*t
,
2880 const struct xfrm_algo_desc
*d
)
2882 unsigned int id
= d
->desc
.sadb_alg_id
;
2884 if (id
>= sizeof(t
->ealgos
) * 8)
2887 return (t
->ealgos
>> id
) & 1;
2890 static int count_ah_combs(const struct xfrm_tmpl
*t
)
2894 for (i
= 0; ; i
++) {
2895 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2898 if (!aalg
->pfkey_supported
)
2900 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2901 sz
+= sizeof(struct sadb_comb
);
2903 return sz
+ sizeof(struct sadb_prop
);
2906 static int count_esp_combs(const struct xfrm_tmpl
*t
)
2910 for (i
= 0; ; i
++) {
2911 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2915 if (!ealg
->pfkey_supported
)
2918 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2921 for (k
= 1; ; k
++) {
2922 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2926 if (!aalg
->pfkey_supported
)
2929 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2930 sz
+= sizeof(struct sadb_comb
);
2933 return sz
+ sizeof(struct sadb_prop
);
2936 static void dump_ah_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2938 struct sadb_prop
*p
;
2941 p
= skb_put(skb
, sizeof(struct sadb_prop
));
2942 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2943 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2944 p
->sadb_prop_replay
= 32;
2945 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2947 for (i
= 0; ; i
++) {
2948 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2952 if (!aalg
->pfkey_supported
)
2955 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2956 struct sadb_comb
*c
;
2957 c
= skb_put_zero(skb
, sizeof(struct sadb_comb
));
2958 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2959 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2960 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2961 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2962 c
->sadb_comb_hard_addtime
= 24*60*60;
2963 c
->sadb_comb_soft_addtime
= 20*60*60;
2964 c
->sadb_comb_hard_usetime
= 8*60*60;
2965 c
->sadb_comb_soft_usetime
= 7*60*60;
2970 static void dump_esp_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2972 struct sadb_prop
*p
;
2975 p
= skb_put(skb
, sizeof(struct sadb_prop
));
2976 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2977 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2978 p
->sadb_prop_replay
= 32;
2979 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2982 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2986 if (!ealg
->pfkey_supported
)
2989 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2992 for (k
= 1; ; k
++) {
2993 struct sadb_comb
*c
;
2994 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2997 if (!aalg
->pfkey_supported
)
2999 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
3001 c
= skb_put(skb
, sizeof(struct sadb_comb
));
3002 memset(c
, 0, sizeof(*c
));
3003 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
3004 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
3005 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
3006 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
3007 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
3008 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
3009 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
3010 c
->sadb_comb_hard_addtime
= 24*60*60;
3011 c
->sadb_comb_soft_addtime
= 20*60*60;
3012 c
->sadb_comb_hard_usetime
= 8*60*60;
3013 c
->sadb_comb_soft_usetime
= 7*60*60;
3018 static int key_notify_policy_expire(struct xfrm_policy
*xp
, const struct km_event
*c
)
3023 static int key_notify_sa_expire(struct xfrm_state
*x
, const struct km_event
*c
)
3025 struct sk_buff
*out_skb
;
3026 struct sadb_msg
*out_hdr
;
3030 hard
= c
->data
.hard
;
3036 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
3037 if (IS_ERR(out_skb
))
3038 return PTR_ERR(out_skb
);
3040 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
3041 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
3042 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
3043 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3044 out_hdr
->sadb_msg_errno
= 0;
3045 out_hdr
->sadb_msg_reserved
= 0;
3046 out_hdr
->sadb_msg_seq
= 0;
3047 out_hdr
->sadb_msg_pid
= 0;
3049 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3054 static int pfkey_send_notify(struct xfrm_state
*x
, const struct km_event
*c
)
3056 struct net
*net
= x
? xs_net(x
) : c
->net
;
3057 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3059 if (atomic_read(&net_pfkey
->socks_nr
) == 0)
3063 case XFRM_MSG_EXPIRE
:
3064 return key_notify_sa_expire(x
, c
);
3065 case XFRM_MSG_DELSA
:
3066 case XFRM_MSG_NEWSA
:
3067 case XFRM_MSG_UPDSA
:
3068 return key_notify_sa(x
, c
);
3069 case XFRM_MSG_FLUSHSA
:
3070 return key_notify_sa_flush(c
);
3071 case XFRM_MSG_NEWAE
: /* not yet supported */
3074 pr_err("pfkey: Unknown SA event %d\n", c
->event
);
3081 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3083 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
3087 case XFRM_MSG_POLEXPIRE
:
3088 return key_notify_policy_expire(xp
, c
);
3089 case XFRM_MSG_DELPOLICY
:
3090 case XFRM_MSG_NEWPOLICY
:
3091 case XFRM_MSG_UPDPOLICY
:
3092 return key_notify_policy(xp
, dir
, c
);
3093 case XFRM_MSG_FLUSHPOLICY
:
3094 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
3096 return key_notify_policy_flush(c
);
3098 pr_err("pfkey: Unknown policy event %d\n", c
->event
);
3105 static u32
get_acqseq(void)
3108 static atomic_t acqseq
;
3111 res
= atomic_inc_return(&acqseq
);
3116 static bool pfkey_is_alive(const struct km_event
*c
)
3118 struct netns_pfkey
*net_pfkey
= net_generic(c
->net
, pfkey_net_id
);
3120 bool is_alive
= false;
3123 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
3124 if (pfkey_sk(sk
)->registered
) {
3134 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
)
3136 struct sk_buff
*skb
;
3137 struct sadb_msg
*hdr
;
3138 struct sadb_address
*addr
;
3139 struct sadb_x_policy
*pol
;
3142 struct sadb_x_sec_ctx
*sec_ctx
;
3143 struct xfrm_sec_ctx
*xfrm_ctx
;
3146 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3150 size
= sizeof(struct sadb_msg
) +
3151 (sizeof(struct sadb_address
) * 2) +
3152 (sockaddr_size
* 2) +
3153 sizeof(struct sadb_x_policy
);
3155 if (x
->id
.proto
== IPPROTO_AH
)
3156 size
+= count_ah_combs(t
);
3157 else if (x
->id
.proto
== IPPROTO_ESP
)
3158 size
+= count_esp_combs(t
);
3160 if ((xfrm_ctx
= x
->security
)) {
3161 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3162 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3165 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3169 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3170 hdr
->sadb_msg_version
= PF_KEY_V2
;
3171 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3172 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3173 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3174 hdr
->sadb_msg_errno
= 0;
3175 hdr
->sadb_msg_reserved
= 0;
3176 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3177 hdr
->sadb_msg_pid
= 0;
3180 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3181 addr
->sadb_address_len
=
3182 (sizeof(struct sadb_address
)+sockaddr_size
)/
3184 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3185 addr
->sadb_address_proto
= 0;
3186 addr
->sadb_address_reserved
= 0;
3187 addr
->sadb_address_prefixlen
=
3188 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3189 (struct sockaddr
*) (addr
+ 1),
3191 if (!addr
->sadb_address_prefixlen
)
3195 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3196 addr
->sadb_address_len
=
3197 (sizeof(struct sadb_address
)+sockaddr_size
)/
3199 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3200 addr
->sadb_address_proto
= 0;
3201 addr
->sadb_address_reserved
= 0;
3202 addr
->sadb_address_prefixlen
=
3203 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3204 (struct sockaddr
*) (addr
+ 1),
3206 if (!addr
->sadb_address_prefixlen
)
3209 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
3210 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3211 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3212 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3213 pol
->sadb_x_policy_dir
= XFRM_POLICY_OUT
+ 1;
3214 pol
->sadb_x_policy_reserved
= 0;
3215 pol
->sadb_x_policy_id
= xp
->index
;
3216 pol
->sadb_x_policy_priority
= xp
->priority
;
3218 /* Set sadb_comb's. */
3219 if (x
->id
.proto
== IPPROTO_AH
)
3220 dump_ah_combs(skb
, t
);
3221 else if (x
->id
.proto
== IPPROTO_ESP
)
3222 dump_esp_combs(skb
, t
);
3224 /* security context */
3226 sec_ctx
= skb_put(skb
,
3227 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3228 sec_ctx
->sadb_x_sec_len
=
3229 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3230 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3231 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3232 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3233 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3234 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3238 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3242 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3243 u8
*data
, int len
, int *dir
)
3245 struct net
*net
= sock_net(sk
);
3246 struct xfrm_policy
*xp
;
3247 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3248 struct sadb_x_sec_ctx
*sec_ctx
;
3250 switch (sk
->sk_family
) {
3252 if (opt
!= IP_IPSEC_POLICY
) {
3257 #if IS_ENABLED(CONFIG_IPV6)
3259 if (opt
!= IPV6_IPSEC_POLICY
) {
3272 if (len
< sizeof(struct sadb_x_policy
) ||
3273 pol
->sadb_x_policy_len
*8 > len
||
3274 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3275 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3278 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
3284 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3285 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3287 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3288 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3289 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3290 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3291 xp
->family
= sk
->sk_family
;
3294 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3295 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3298 /* security context too */
3299 if (len
>= (pol
->sadb_x_policy_len
*8 +
3300 sizeof(struct sadb_x_sec_ctx
))) {
3301 char *p
= (char *)pol
;
3302 struct xfrm_user_sec_ctx
*uctx
;
3304 p
+= pol
->sadb_x_policy_len
*8;
3305 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3306 if (len
< pol
->sadb_x_policy_len
*8 +
3307 sec_ctx
->sadb_x_sec_len
*8) {
3311 if ((*dir
= verify_sec_ctx_len(p
)))
3313 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_ATOMIC
);
3314 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_ATOMIC
);
3321 *dir
= pol
->sadb_x_policy_dir
-1;
3326 xfrm_policy_destroy(xp
);
3330 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3332 struct sk_buff
*skb
;
3333 struct sadb_msg
*hdr
;
3335 struct sadb_address
*addr
;
3336 struct sadb_x_nat_t_port
*n_port
;
3339 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3340 struct xfrm_encap_tmpl
*natt
= NULL
;
3342 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3354 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3356 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3357 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3360 size
= sizeof(struct sadb_msg
) +
3361 sizeof(struct sadb_sa
) +
3362 (sizeof(struct sadb_address
) * 2) +
3363 (sockaddr_size
* 2) +
3364 (sizeof(struct sadb_x_nat_t_port
) * 2);
3366 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3370 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3371 hdr
->sadb_msg_version
= PF_KEY_V2
;
3372 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3373 hdr
->sadb_msg_satype
= satype
;
3374 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3375 hdr
->sadb_msg_errno
= 0;
3376 hdr
->sadb_msg_reserved
= 0;
3377 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3378 hdr
->sadb_msg_pid
= 0;
3381 sa
= skb_put(skb
, sizeof(struct sadb_sa
));
3382 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3383 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3384 sa
->sadb_sa_spi
= x
->id
.spi
;
3385 sa
->sadb_sa_replay
= 0;
3386 sa
->sadb_sa_state
= 0;
3387 sa
->sadb_sa_auth
= 0;
3388 sa
->sadb_sa_encrypt
= 0;
3389 sa
->sadb_sa_flags
= 0;
3391 /* ADDRESS_SRC (old addr) */
3392 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3393 addr
->sadb_address_len
=
3394 (sizeof(struct sadb_address
)+sockaddr_size
)/
3396 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3397 addr
->sadb_address_proto
= 0;
3398 addr
->sadb_address_reserved
= 0;
3399 addr
->sadb_address_prefixlen
=
3400 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3401 (struct sockaddr
*) (addr
+ 1),
3403 if (!addr
->sadb_address_prefixlen
)
3406 /* NAT_T_SPORT (old port) */
3407 n_port
= skb_put(skb
, sizeof(*n_port
));
3408 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3409 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3410 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3411 n_port
->sadb_x_nat_t_port_reserved
= 0;
3413 /* ADDRESS_DST (new addr) */
3414 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3415 addr
->sadb_address_len
=
3416 (sizeof(struct sadb_address
)+sockaddr_size
)/
3418 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3419 addr
->sadb_address_proto
= 0;
3420 addr
->sadb_address_reserved
= 0;
3421 addr
->sadb_address_prefixlen
=
3422 pfkey_sockaddr_fill(ipaddr
, 0,
3423 (struct sockaddr
*) (addr
+ 1),
3425 if (!addr
->sadb_address_prefixlen
)
3428 /* NAT_T_DPORT (new port) */
3429 n_port
= skb_put(skb
, sizeof(*n_port
));
3430 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3431 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3432 n_port
->sadb_x_nat_t_port_port
= sport
;
3433 n_port
->sadb_x_nat_t_port_reserved
= 0;
3435 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3439 #ifdef CONFIG_NET_KEY_MIGRATE
3440 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3441 const struct xfrm_selector
*sel
)
3443 struct sadb_address
*addr
;
3444 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3445 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3446 addr
->sadb_address_exttype
= type
;
3447 addr
->sadb_address_proto
= sel
->proto
;
3448 addr
->sadb_address_reserved
= 0;
3451 case SADB_EXT_ADDRESS_SRC
:
3452 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3453 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3454 (struct sockaddr
*)(addr
+ 1),
3457 case SADB_EXT_ADDRESS_DST
:
3458 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3459 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3460 (struct sockaddr
*)(addr
+ 1),
3471 static int set_sadb_kmaddress(struct sk_buff
*skb
, const struct xfrm_kmaddress
*k
)
3473 struct sadb_x_kmaddress
*kma
;
3475 int family
= k
->family
;
3476 int socklen
= pfkey_sockaddr_len(family
);
3479 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3480 pfkey_sockaddr_pair_size(family
));
3482 kma
= skb_put_zero(skb
, size_req
);
3483 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3484 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3485 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3487 sa
= (u8
*)(kma
+ 1);
3488 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3489 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3495 static int set_ipsecrequest(struct sk_buff
*skb
,
3496 uint8_t proto
, uint8_t mode
, int level
,
3497 uint32_t reqid
, uint8_t family
,
3498 const xfrm_address_t
*src
, const xfrm_address_t
*dst
)
3500 struct sadb_x_ipsecrequest
*rq
;
3502 int socklen
= pfkey_sockaddr_len(family
);
3505 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3506 pfkey_sockaddr_pair_size(family
);
3508 rq
= skb_put_zero(skb
, size_req
);
3509 rq
->sadb_x_ipsecrequest_len
= size_req
;
3510 rq
->sadb_x_ipsecrequest_proto
= proto
;
3511 rq
->sadb_x_ipsecrequest_mode
= mode
;
3512 rq
->sadb_x_ipsecrequest_level
= level
;
3513 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3515 sa
= (u8
*) (rq
+ 1);
3516 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3517 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3524 #ifdef CONFIG_NET_KEY_MIGRATE
3525 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3526 const struct xfrm_migrate
*m
, int num_bundles
,
3527 const struct xfrm_kmaddress
*k
,
3528 const struct xfrm_encap_tmpl
*encap
)
3534 struct sk_buff
*skb
;
3535 struct sadb_msg
*hdr
;
3536 struct sadb_x_policy
*pol
;
3537 const struct xfrm_migrate
*mp
;
3539 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3542 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3546 /* addresses for KM */
3547 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3548 pfkey_sockaddr_pair_size(k
->family
));
3552 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3555 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3558 size_pol
+= sizeof(struct sadb_x_policy
);
3561 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3562 /* old locator pair */
3563 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3564 pfkey_sockaddr_pair_size(mp
->old_family
);
3565 /* new locator pair */
3566 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3567 pfkey_sockaddr_pair_size(mp
->new_family
);
3570 size
+= sizeof(struct sadb_msg
) + size_pol
;
3573 skb
= alloc_skb(size
, GFP_ATOMIC
);
3577 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3578 hdr
->sadb_msg_version
= PF_KEY_V2
;
3579 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3580 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3581 hdr
->sadb_msg_len
= size
/ 8;
3582 hdr
->sadb_msg_errno
= 0;
3583 hdr
->sadb_msg_reserved
= 0;
3584 hdr
->sadb_msg_seq
= 0;
3585 hdr
->sadb_msg_pid
= 0;
3587 /* Addresses to be used by KM for negotiation, if ext is available */
3588 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3592 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3595 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3597 /* policy information */
3598 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
3599 pol
->sadb_x_policy_len
= size_pol
/ 8;
3600 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3601 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3602 pol
->sadb_x_policy_dir
= dir
+ 1;
3603 pol
->sadb_x_policy_reserved
= 0;
3604 pol
->sadb_x_policy_id
= 0;
3605 pol
->sadb_x_policy_priority
= 0;
3607 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3608 /* old ipsecrequest */
3609 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3612 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3613 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3614 mp
->reqid
, mp
->old_family
,
3615 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3618 /* new ipsecrequest */
3619 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3620 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3621 mp
->reqid
, mp
->new_family
,
3622 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3626 /* broadcast migrate message to sockets */
3627 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, &init_net
);
3636 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3637 const struct xfrm_migrate
*m
, int num_bundles
,
3638 const struct xfrm_kmaddress
*k
,
3639 const struct xfrm_encap_tmpl
*encap
)
3641 return -ENOPROTOOPT
;
3645 static int pfkey_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3647 struct sock
*sk
= sock
->sk
;
3648 struct sk_buff
*skb
= NULL
;
3649 struct sadb_msg
*hdr
= NULL
;
3651 struct net
*net
= sock_net(sk
);
3654 if (msg
->msg_flags
& MSG_OOB
)
3658 if ((unsigned int)len
> sk
->sk_sndbuf
- 32)
3662 skb
= alloc_skb(len
, GFP_KERNEL
);
3667 if (memcpy_from_msg(skb_put(skb
,len
), msg
, len
))
3670 hdr
= pfkey_get_base_msg(skb
, &err
);
3674 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
3675 err
= pfkey_process(sk
, skb
, hdr
);
3676 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
3679 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3686 static int pfkey_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3689 struct sock
*sk
= sock
->sk
;
3690 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3691 struct sk_buff
*skb
;
3695 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3698 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3704 msg
->msg_flags
|= MSG_TRUNC
;
3708 skb_reset_transport_header(skb
);
3709 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
3713 sock_recv_ts_and_drops(msg
, sk
, skb
);
3715 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3717 if (pfk
->dump
.dump
!= NULL
&&
3718 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3722 skb_free_datagram(sk
, skb
);
3727 static const struct proto_ops pfkey_ops
= {
3729 .owner
= THIS_MODULE
,
3730 /* Operations that make no sense on pfkey sockets. */
3731 .bind
= sock_no_bind
,
3732 .connect
= sock_no_connect
,
3733 .socketpair
= sock_no_socketpair
,
3734 .accept
= sock_no_accept
,
3735 .getname
= sock_no_getname
,
3736 .ioctl
= sock_no_ioctl
,
3737 .listen
= sock_no_listen
,
3738 .shutdown
= sock_no_shutdown
,
3739 .setsockopt
= sock_no_setsockopt
,
3740 .getsockopt
= sock_no_getsockopt
,
3741 .mmap
= sock_no_mmap
,
3742 .sendpage
= sock_no_sendpage
,
3744 /* Now the operations that really occur. */
3745 .release
= pfkey_release
,
3746 .poll
= datagram_poll
,
3747 .sendmsg
= pfkey_sendmsg
,
3748 .recvmsg
= pfkey_recvmsg
,
3751 static const struct net_proto_family pfkey_family_ops
= {
3753 .create
= pfkey_create
,
3754 .owner
= THIS_MODULE
,
3757 #ifdef CONFIG_PROC_FS
3758 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3760 struct sock
*s
= sk_entry(v
);
3762 if (v
== SEQ_START_TOKEN
)
3763 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3765 seq_printf(f
, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3767 refcount_read(&s
->sk_refcnt
),
3768 sk_rmem_alloc_get(s
),
3769 sk_wmem_alloc_get(s
),
3770 from_kuid_munged(seq_user_ns(f
), sock_i_uid(s
)),
3776 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3779 struct net
*net
= seq_file_net(f
);
3780 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3783 return seq_hlist_start_head_rcu(&net_pfkey
->table
, *ppos
);
3786 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3788 struct net
*net
= seq_file_net(f
);
3789 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3791 return seq_hlist_next_rcu(v
, &net_pfkey
->table
, ppos
);
3794 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3800 static const struct seq_operations pfkey_seq_ops
= {
3801 .start
= pfkey_seq_start
,
3802 .next
= pfkey_seq_next
,
3803 .stop
= pfkey_seq_stop
,
3804 .show
= pfkey_seq_show
,
3807 static int __net_init
pfkey_init_proc(struct net
*net
)
3809 struct proc_dir_entry
*e
;
3811 e
= proc_create_net("pfkey", 0, net
->proc_net
, &pfkey_seq_ops
,
3812 sizeof(struct seq_net_private
));
3819 static void __net_exit
pfkey_exit_proc(struct net
*net
)
3821 remove_proc_entry("pfkey", net
->proc_net
);
3824 static inline int pfkey_init_proc(struct net
*net
)
3829 static inline void pfkey_exit_proc(struct net
*net
)
3834 static struct xfrm_mgr pfkeyv2_mgr
=
3836 .notify
= pfkey_send_notify
,
3837 .acquire
= pfkey_send_acquire
,
3838 .compile_policy
= pfkey_compile_policy
,
3839 .new_mapping
= pfkey_send_new_mapping
,
3840 .notify_policy
= pfkey_send_policy_notify
,
3841 .migrate
= pfkey_send_migrate
,
3842 .is_alive
= pfkey_is_alive
,
3845 static int __net_init
pfkey_net_init(struct net
*net
)
3847 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3850 INIT_HLIST_HEAD(&net_pfkey
->table
);
3851 atomic_set(&net_pfkey
->socks_nr
, 0);
3853 rv
= pfkey_init_proc(net
);
3858 static void __net_exit
pfkey_net_exit(struct net
*net
)
3860 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3862 pfkey_exit_proc(net
);
3863 WARN_ON(!hlist_empty(&net_pfkey
->table
));
3866 static struct pernet_operations pfkey_net_ops
= {
3867 .init
= pfkey_net_init
,
3868 .exit
= pfkey_net_exit
,
3869 .id
= &pfkey_net_id
,
3870 .size
= sizeof(struct netns_pfkey
),
3873 static void __exit
ipsec_pfkey_exit(void)
3875 xfrm_unregister_km(&pfkeyv2_mgr
);
3876 sock_unregister(PF_KEY
);
3877 unregister_pernet_subsys(&pfkey_net_ops
);
3878 proto_unregister(&key_proto
);
3881 static int __init
ipsec_pfkey_init(void)
3883 int err
= proto_register(&key_proto
, 0);
3888 err
= register_pernet_subsys(&pfkey_net_ops
);
3890 goto out_unregister_key_proto
;
3891 err
= sock_register(&pfkey_family_ops
);
3893 goto out_unregister_pernet
;
3894 err
= xfrm_register_km(&pfkeyv2_mgr
);
3896 goto out_sock_unregister
;
3900 out_sock_unregister
:
3901 sock_unregister(PF_KEY
);
3902 out_unregister_pernet
:
3903 unregister_pernet_subsys(&pfkey_net_ops
);
3904 out_unregister_key_proto
:
3905 proto_unregister(&key_proto
);
3909 module_init(ipsec_pfkey_init
);
3910 module_exit(ipsec_pfkey_exit
);
3911 MODULE_LICENSE("GPL");
3912 MODULE_ALIAS_NETPROTO(PF_KEY
);