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 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
;
68 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
70 return (struct pfkey_sock
*)sk
;
73 static int pfkey_can_dump(const struct sock
*sk
)
75 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
80 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
84 kfree_skb(pfk
->dump
.skb
);
88 pfk
->dump
.dump
= NULL
;
89 pfk
->dump
.done
= NULL
;
93 static void pfkey_sock_destruct(struct sock
*sk
)
95 struct net
*net
= sock_net(sk
);
96 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
98 pfkey_terminate_dump(pfkey_sk(sk
));
99 skb_queue_purge(&sk
->sk_receive_queue
);
101 if (!sock_flag(sk
, SOCK_DEAD
)) {
102 pr_err("Attempt to release alive pfkey socket: %p\n", sk
);
106 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
107 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
109 atomic_dec(&net_pfkey
->socks_nr
);
112 static const struct proto_ops pfkey_ops
;
114 static void pfkey_insert(struct sock
*sk
)
116 struct net
*net
= sock_net(sk
);
117 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
119 mutex_lock(&pfkey_mutex
);
120 sk_add_node_rcu(sk
, &net_pfkey
->table
);
121 mutex_unlock(&pfkey_mutex
);
124 static void pfkey_remove(struct sock
*sk
)
126 mutex_lock(&pfkey_mutex
);
127 sk_del_node_init_rcu(sk
);
128 mutex_unlock(&pfkey_mutex
);
131 static struct proto key_proto
= {
133 .owner
= THIS_MODULE
,
134 .obj_size
= sizeof(struct pfkey_sock
),
137 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
,
140 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
144 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
146 if (sock
->type
!= SOCK_RAW
)
147 return -ESOCKTNOSUPPORT
;
148 if (protocol
!= PF_KEY_V2
)
149 return -EPROTONOSUPPORT
;
152 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
, kern
);
156 sock
->ops
= &pfkey_ops
;
157 sock_init_data(sock
, sk
);
159 sk
->sk_family
= PF_KEY
;
160 sk
->sk_destruct
= pfkey_sock_destruct
;
162 atomic_inc(&net_pfkey
->socks_nr
);
171 static int pfkey_release(struct socket
*sock
)
173 struct sock
*sk
= sock
->sk
;
182 skb_queue_purge(&sk
->sk_write_queue
);
190 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
191 gfp_t allocation
, struct sock
*sk
)
197 if (atomic_read(&skb
->users
) != 1) {
198 *skb2
= skb_clone(skb
, allocation
);
201 atomic_inc(&skb
->users
);
205 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
206 skb_set_owner_r(*skb2
, sk
);
207 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
208 sk
->sk_data_ready(sk
);
217 /* Send SKB to all pfkey sockets matching selected criteria. */
218 #define BROADCAST_ALL 0
219 #define BROADCAST_ONE 1
220 #define BROADCAST_REGISTERED 2
221 #define BROADCAST_PROMISC_ONLY 4
222 static int pfkey_broadcast(struct sk_buff
*skb
,
223 int broadcast_flags
, struct sock
*one_sk
,
226 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
228 struct sk_buff
*skb2
= NULL
;
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
, &skb2
, 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
, &skb2
, 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
, &skb2
, GFP_KERNEL
, one_sk
);
279 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
281 struct sadb_msg
*hdr
;
284 rc
= pfk
->dump
.dump(pfk
);
289 if (!pfkey_can_dump(&pfk
->sk
))
292 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
293 hdr
->sadb_msg_seq
= 0;
294 hdr
->sadb_msg_errno
= rc
;
295 pfkey_broadcast(pfk
->dump
.skb
, BROADCAST_ONE
,
296 &pfk
->sk
, sock_net(&pfk
->sk
));
297 pfk
->dump
.skb
= NULL
;
300 pfkey_terminate_dump(pfk
);
304 static inline void pfkey_hdr_dup(struct sadb_msg
*new,
305 const struct sadb_msg
*orig
)
310 static int pfkey_error(const struct sadb_msg
*orig
, int err
, struct sock
*sk
)
312 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
313 struct sadb_msg
*hdr
;
318 /* Woe be to the platform trying to support PFKEY yet
319 * having normal errnos outside the 1-255 range, inclusive.
322 if (err
== ERESTARTSYS
||
323 err
== ERESTARTNOHAND
||
324 err
== ERESTARTNOINTR
)
328 BUG_ON(err
<= 0 || err
>= 256);
330 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
331 pfkey_hdr_dup(hdr
, orig
);
332 hdr
->sadb_msg_errno
= (uint8_t) err
;
333 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
336 pfkey_broadcast(skb
, BROADCAST_ONE
, sk
, sock_net(sk
));
341 static const u8 sadb_ext_min_len
[] = {
342 [SADB_EXT_RESERVED
] = (u8
) 0,
343 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
344 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
345 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
346 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
347 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
348 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
349 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
350 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
351 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
352 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
353 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
354 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
355 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
356 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
357 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
358 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
359 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
360 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
361 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
362 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
363 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
364 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
365 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
366 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
367 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
368 [SADB_X_EXT_FILTER
] = (u8
) sizeof(struct sadb_x_filter
),
371 /* Verify sadb_address_{len,prefixlen} against sa_family. */
372 static int verify_address_len(const void *p
)
374 const struct sadb_address
*sp
= p
;
375 const struct sockaddr
*addr
= (const struct sockaddr
*)(sp
+ 1);
376 const struct sockaddr_in
*sin
;
377 #if IS_ENABLED(CONFIG_IPV6)
378 const struct sockaddr_in6
*sin6
;
382 switch (addr
->sa_family
) {
384 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
385 if (sp
->sadb_address_len
!= len
||
386 sp
->sadb_address_prefixlen
> 32)
389 #if IS_ENABLED(CONFIG_IPV6)
391 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
392 if (sp
->sadb_address_len
!= len
||
393 sp
->sadb_address_prefixlen
> 128)
398 /* It is user using kernel to keep track of security
399 * associations for another protocol, such as
400 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
403 * XXX Actually, association/policy database is not yet
404 * XXX able to cope with arbitrary sockaddr families.
405 * XXX When it can, remove this -EINVAL. -DaveM
413 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx
*sec_ctx
)
415 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
416 sec_ctx
->sadb_x_ctx_len
,
420 static inline int verify_sec_ctx_len(const void *p
)
422 const struct sadb_x_sec_ctx
*sec_ctx
= p
;
423 int len
= sec_ctx
->sadb_x_ctx_len
;
428 len
= pfkey_sec_ctx_len(sec_ctx
);
430 if (sec_ctx
->sadb_x_sec_len
!= len
)
436 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx
*sec_ctx
,
439 struct xfrm_user_sec_ctx
*uctx
= NULL
;
440 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
442 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), gfp
);
447 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
448 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
449 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
450 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
451 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
452 memcpy(uctx
+ 1, sec_ctx
+ 1,
458 static int present_and_same_family(const struct sadb_address
*src
,
459 const struct sadb_address
*dst
)
461 const struct sockaddr
*s_addr
, *d_addr
;
466 s_addr
= (const struct sockaddr
*)(src
+ 1);
467 d_addr
= (const struct sockaddr
*)(dst
+ 1);
468 if (s_addr
->sa_family
!= d_addr
->sa_family
)
470 if (s_addr
->sa_family
!= AF_INET
471 #if IS_ENABLED(CONFIG_IPV6)
472 && s_addr
->sa_family
!= AF_INET6
480 static int parse_exthdrs(struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void **ext_hdrs
)
482 const char *p
= (char *) hdr
;
488 const struct sadb_ext
*ehdr
= (const struct sadb_ext
*) p
;
492 ext_len
= ehdr
->sadb_ext_len
;
493 ext_len
*= sizeof(uint64_t);
494 ext_type
= ehdr
->sadb_ext_type
;
495 if (ext_len
< sizeof(uint64_t) ||
497 ext_type
== SADB_EXT_RESERVED
)
500 if (ext_type
<= SADB_EXT_MAX
) {
501 int min
= (int) sadb_ext_min_len
[ext_type
];
504 if (ext_hdrs
[ext_type
-1] != NULL
)
506 if (ext_type
== SADB_EXT_ADDRESS_SRC
||
507 ext_type
== SADB_EXT_ADDRESS_DST
||
508 ext_type
== SADB_EXT_ADDRESS_PROXY
||
509 ext_type
== SADB_X_EXT_NAT_T_OA
) {
510 if (verify_address_len(p
))
513 if (ext_type
== SADB_X_EXT_SEC_CTX
) {
514 if (verify_sec_ctx_len(p
))
517 ext_hdrs
[ext_type
-1] = (void *) p
;
527 pfkey_satype2proto(uint8_t satype
)
530 case SADB_SATYPE_UNSPEC
:
531 return IPSEC_PROTO_ANY
;
534 case SADB_SATYPE_ESP
:
536 case SADB_X_SATYPE_IPCOMP
:
545 pfkey_proto2satype(uint16_t proto
)
549 return SADB_SATYPE_AH
;
551 return SADB_SATYPE_ESP
;
553 return SADB_X_SATYPE_IPCOMP
;
560 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
561 * say specifically 'just raw sockets' as we encode them as 255.
564 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
566 return proto
== IPSEC_PROTO_ANY
? 0 : proto
;
569 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
571 return proto
? proto
: IPSEC_PROTO_ANY
;
574 static inline int pfkey_sockaddr_len(sa_family_t family
)
578 return sizeof(struct sockaddr_in
);
579 #if IS_ENABLED(CONFIG_IPV6)
581 return sizeof(struct sockaddr_in6
);
588 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
590 switch (sa
->sa_family
) {
593 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
595 #if IS_ENABLED(CONFIG_IPV6)
598 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
599 sizeof(struct in6_addr
));
607 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
609 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
613 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct net
*net
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
615 const struct sadb_sa
*sa
;
616 const struct sadb_address
*addr
;
618 unsigned short family
;
619 xfrm_address_t
*xaddr
;
621 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
625 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
629 /* sadb_address_len should be checked by caller */
630 addr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
634 family
= ((const struct sockaddr
*)(addr
+ 1))->sa_family
;
637 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
639 #if IS_ENABLED(CONFIG_IPV6)
641 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
651 return xfrm_state_lookup(net
, DUMMY_MARK
, xaddr
, sa
->sadb_sa_spi
, proto
, family
);
654 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
657 pfkey_sockaddr_size(sa_family_t family
)
659 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
662 static inline int pfkey_mode_from_xfrm(int mode
)
665 case XFRM_MODE_TRANSPORT
:
666 return IPSEC_MODE_TRANSPORT
;
667 case XFRM_MODE_TUNNEL
:
668 return IPSEC_MODE_TUNNEL
;
670 return IPSEC_MODE_BEET
;
676 static inline int pfkey_mode_to_xfrm(int mode
)
679 case IPSEC_MODE_ANY
: /*XXX*/
680 case IPSEC_MODE_TRANSPORT
:
681 return XFRM_MODE_TRANSPORT
;
682 case IPSEC_MODE_TUNNEL
:
683 return XFRM_MODE_TUNNEL
;
684 case IPSEC_MODE_BEET
:
685 return XFRM_MODE_BEET
;
691 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t
*xaddr
, __be16 port
,
693 unsigned short family
)
698 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
699 sin
->sin_family
= AF_INET
;
700 sin
->sin_port
= port
;
701 sin
->sin_addr
.s_addr
= xaddr
->a4
;
702 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
705 #if IS_ENABLED(CONFIG_IPV6)
708 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
709 sin6
->sin6_family
= AF_INET6
;
710 sin6
->sin6_port
= port
;
711 sin6
->sin6_flowinfo
= 0;
712 sin6
->sin6_addr
= xaddr
->in6
;
713 sin6
->sin6_scope_id
= 0;
721 static struct sk_buff
*__pfkey_xfrm_state2msg(const struct xfrm_state
*x
,
722 int add_keys
, int hsc
)
725 struct sadb_msg
*hdr
;
727 struct sadb_lifetime
*lifetime
;
728 struct sadb_address
*addr
;
729 struct sadb_key
*key
;
730 struct sadb_x_sa2
*sa2
;
731 struct sadb_x_sec_ctx
*sec_ctx
;
732 struct xfrm_sec_ctx
*xfrm_ctx
;
735 int auth_key_size
= 0;
736 int encrypt_key_size
= 0;
738 struct xfrm_encap_tmpl
*natt
= NULL
;
741 /* address family check */
742 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
744 return ERR_PTR(-EINVAL
);
746 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
747 key(AE), (identity(SD),) (sensitivity)> */
748 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
749 sizeof(struct sadb_lifetime
) +
750 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
751 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
752 sizeof(struct sadb_address
)*2 +
754 sizeof(struct sadb_x_sa2
);
756 if ((xfrm_ctx
= x
->security
)) {
757 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
758 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
761 /* identity & sensitivity */
762 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
763 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
766 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
768 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
769 size
+= sizeof(struct sadb_key
) + auth_key_size
;
771 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
773 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
774 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
780 if (natt
&& natt
->encap_type
) {
781 size
+= sizeof(struct sadb_x_nat_t_type
);
782 size
+= sizeof(struct sadb_x_nat_t_port
);
783 size
+= sizeof(struct sadb_x_nat_t_port
);
786 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
788 return ERR_PTR(-ENOBUFS
);
790 /* call should fill header later */
791 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
792 memset(hdr
, 0, size
); /* XXX do we need this ? */
793 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
796 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
797 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
798 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
799 sa
->sadb_sa_spi
= x
->id
.spi
;
800 sa
->sadb_sa_replay
= x
->props
.replay_window
;
801 switch (x
->km
.state
) {
802 case XFRM_STATE_VALID
:
803 sa
->sadb_sa_state
= x
->km
.dying
?
804 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
807 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
810 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
813 sa
->sadb_sa_auth
= 0;
815 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
816 sa
->sadb_sa_auth
= (a
&& a
->pfkey_supported
) ?
817 a
->desc
.sadb_alg_id
: 0;
819 sa
->sadb_sa_encrypt
= 0;
820 BUG_ON(x
->ealg
&& x
->calg
);
822 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
823 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
824 a
->desc
.sadb_alg_id
: 0;
826 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
828 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
829 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
830 a
->desc
.sadb_alg_id
: 0;
833 sa
->sadb_sa_flags
= 0;
834 if (x
->props
.flags
& XFRM_STATE_NOECN
)
835 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
836 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
837 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
838 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
839 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
843 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
844 sizeof(struct sadb_lifetime
));
845 lifetime
->sadb_lifetime_len
=
846 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
847 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
848 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
849 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
850 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
851 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
855 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
856 sizeof(struct sadb_lifetime
));
857 lifetime
->sadb_lifetime_len
=
858 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
859 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
860 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
861 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
862 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
863 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
866 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
867 sizeof(struct sadb_lifetime
));
868 lifetime
->sadb_lifetime_len
=
869 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
870 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
871 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
872 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
873 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
874 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
876 addr
= (struct sadb_address
*) skb_put(skb
,
877 sizeof(struct sadb_address
)+sockaddr_size
);
878 addr
->sadb_address_len
=
879 (sizeof(struct sadb_address
)+sockaddr_size
)/
881 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
882 /* "if the ports are non-zero, then the sadb_address_proto field,
883 normally zero, MUST be filled in with the transport
884 protocol's number." - RFC2367 */
885 addr
->sadb_address_proto
= 0;
886 addr
->sadb_address_reserved
= 0;
888 addr
->sadb_address_prefixlen
=
889 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
890 (struct sockaddr
*) (addr
+ 1),
892 if (!addr
->sadb_address_prefixlen
)
896 addr
= (struct sadb_address
*) skb_put(skb
,
897 sizeof(struct sadb_address
)+sockaddr_size
);
898 addr
->sadb_address_len
=
899 (sizeof(struct sadb_address
)+sockaddr_size
)/
901 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
902 addr
->sadb_address_proto
= 0;
903 addr
->sadb_address_reserved
= 0;
905 addr
->sadb_address_prefixlen
=
906 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
907 (struct sockaddr
*) (addr
+ 1),
909 if (!addr
->sadb_address_prefixlen
)
912 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
,
914 addr
= (struct sadb_address
*) skb_put(skb
,
915 sizeof(struct sadb_address
)+sockaddr_size
);
916 addr
->sadb_address_len
=
917 (sizeof(struct sadb_address
)+sockaddr_size
)/
919 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
920 addr
->sadb_address_proto
=
921 pfkey_proto_from_xfrm(x
->sel
.proto
);
922 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
923 addr
->sadb_address_reserved
= 0;
925 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
926 (struct sockaddr
*) (addr
+ 1),
931 if (add_keys
&& auth_key_size
) {
932 key
= (struct sadb_key
*) skb_put(skb
,
933 sizeof(struct sadb_key
)+auth_key_size
);
934 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
936 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
937 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
938 key
->sadb_key_reserved
= 0;
939 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
942 if (add_keys
&& encrypt_key_size
) {
943 key
= (struct sadb_key
*) skb_put(skb
,
944 sizeof(struct sadb_key
)+encrypt_key_size
);
945 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
946 encrypt_key_size
) / sizeof(uint64_t);
947 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
948 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
949 key
->sadb_key_reserved
= 0;
950 memcpy(key
+ 1, x
->ealg
->alg_key
,
951 (x
->ealg
->alg_key_len
+7)/8);
955 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
956 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
957 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
958 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
960 return ERR_PTR(-EINVAL
);
962 sa2
->sadb_x_sa2_mode
= mode
;
963 sa2
->sadb_x_sa2_reserved1
= 0;
964 sa2
->sadb_x_sa2_reserved2
= 0;
965 sa2
->sadb_x_sa2_sequence
= 0;
966 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
968 if (natt
&& natt
->encap_type
) {
969 struct sadb_x_nat_t_type
*n_type
;
970 struct sadb_x_nat_t_port
*n_port
;
973 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
974 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
975 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
976 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
977 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
978 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
979 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
982 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
983 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
984 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
985 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
986 n_port
->sadb_x_nat_t_port_reserved
= 0;
989 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
990 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
991 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
992 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
993 n_port
->sadb_x_nat_t_port_reserved
= 0;
996 /* security context */
998 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
999 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1000 sec_ctx
->sadb_x_sec_len
=
1001 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1002 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1003 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1004 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1005 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1006 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1014 static inline struct sk_buff
*pfkey_xfrm_state2msg(const struct xfrm_state
*x
)
1016 struct sk_buff
*skb
;
1018 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1023 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(const struct xfrm_state
*x
,
1026 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1029 static struct xfrm_state
* pfkey_msg2xfrm_state(struct net
*net
,
1030 const struct sadb_msg
*hdr
,
1031 void * const *ext_hdrs
)
1033 struct xfrm_state
*x
;
1034 const struct sadb_lifetime
*lifetime
;
1035 const struct sadb_sa
*sa
;
1036 const struct sadb_key
*key
;
1037 const struct sadb_x_sec_ctx
*sec_ctx
;
1042 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
1044 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1045 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1046 return ERR_PTR(-EINVAL
);
1047 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1048 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1049 return ERR_PTR(-EINVAL
);
1050 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1051 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1052 return ERR_PTR(-EINVAL
);
1053 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1054 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1055 return ERR_PTR(-EINVAL
);
1057 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1059 return ERR_PTR(-EINVAL
);
1061 /* default error is no buffer space */
1066 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1067 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1068 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1069 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1070 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1073 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1074 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1076 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1077 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1078 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1079 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1080 return ERR_PTR(-EINVAL
);
1081 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1083 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1084 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1085 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1086 return ERR_PTR(-EINVAL
);
1087 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1089 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1090 ((key
->sadb_key_bits
+7) / 8 == 0 ||
1091 (key
->sadb_key_bits
+7) / 8 > key
->sadb_key_len
* sizeof(uint64_t)))
1092 return ERR_PTR(-EINVAL
);
1094 x
= xfrm_state_alloc(net
);
1096 return ERR_PTR(-ENOBUFS
);
1098 x
->id
.proto
= proto
;
1099 x
->id
.spi
= sa
->sadb_sa_spi
;
1100 x
->props
.replay_window
= min_t(unsigned int, sa
->sadb_sa_replay
,
1101 (sizeof(x
->replay
.bitmap
) * 8));
1102 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1103 x
->props
.flags
|= XFRM_STATE_NOECN
;
1104 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1105 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1106 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1107 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1109 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
- 1];
1110 if (lifetime
!= NULL
) {
1111 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1112 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1113 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1114 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1116 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
- 1];
1117 if (lifetime
!= NULL
) {
1118 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1119 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1120 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1121 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1124 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
1125 if (sec_ctx
!= NULL
) {
1126 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
1131 err
= security_xfrm_state_alloc(x
, uctx
);
1138 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1139 if (sa
->sadb_sa_auth
) {
1141 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1142 if (!a
|| !a
->pfkey_supported
) {
1147 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1148 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1151 strcpy(x
->aalg
->alg_name
, a
->name
);
1152 x
->aalg
->alg_key_len
= 0;
1154 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1155 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1157 x
->aalg
->alg_trunc_len
= a
->uinfo
.auth
.icv_truncbits
;
1158 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1159 /* x->algo.flags = sa->sadb_sa_flags; */
1161 if (sa
->sadb_sa_encrypt
) {
1162 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1163 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1164 if (!a
|| !a
->pfkey_supported
) {
1168 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1171 strcpy(x
->calg
->alg_name
, a
->name
);
1172 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1175 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1176 if (!a
|| !a
->pfkey_supported
) {
1180 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1182 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1183 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1186 strcpy(x
->ealg
->alg_name
, a
->name
);
1187 x
->ealg
->alg_key_len
= 0;
1189 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1190 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1192 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1193 x
->geniv
= a
->uinfo
.encr
.geniv
;
1196 /* x->algo.flags = sa->sadb_sa_flags; */
1198 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1200 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1203 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1204 const struct sadb_x_sa2
*sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1];
1205 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1210 x
->props
.mode
= mode
;
1211 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1214 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1215 const struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1217 /* Nobody uses this, but we try. */
1218 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1219 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1223 x
->sel
.family
= x
->props
.family
;
1225 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1226 const struct sadb_x_nat_t_type
* n_type
;
1227 struct xfrm_encap_tmpl
*natt
;
1229 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1234 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1235 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1237 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1238 const struct sadb_x_nat_t_port
*n_port
=
1239 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1240 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1242 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1243 const struct sadb_x_nat_t_port
*n_port
=
1244 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1245 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1247 memset(&natt
->encap_oa
, 0, sizeof(natt
->encap_oa
));
1250 err
= xfrm_init_state(x
);
1254 x
->km
.seq
= hdr
->sadb_msg_seq
;
1258 x
->km
.state
= XFRM_STATE_DEAD
;
1260 return ERR_PTR(err
);
1263 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1268 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1270 struct net
*net
= sock_net(sk
);
1271 struct sk_buff
*resp_skb
;
1272 struct sadb_x_sa2
*sa2
;
1273 struct sadb_address
*saddr
, *daddr
;
1274 struct sadb_msg
*out_hdr
;
1275 struct sadb_spirange
*range
;
1276 struct xfrm_state
*x
= NULL
;
1279 u32 min_spi
, max_spi
;
1282 unsigned short family
;
1283 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1285 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1286 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1289 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1293 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1294 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1297 reqid
= sa2
->sadb_x_sa2_reqid
;
1303 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1304 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1306 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1309 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1310 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1312 #if IS_ENABLED(CONFIG_IPV6)
1314 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1315 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1320 if (hdr
->sadb_msg_seq
) {
1321 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1322 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, xdaddr
, family
)) {
1329 x
= xfrm_find_acq(net
, &dummy_mark
, mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1335 max_spi
= 0x0fffffff;
1337 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1339 min_spi
= range
->sadb_spirange_min
;
1340 max_spi
= range
->sadb_spirange_max
;
1343 err
= verify_spi_info(x
->id
.proto
, min_spi
, max_spi
);
1349 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1350 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1352 if (IS_ERR(resp_skb
)) {
1354 return PTR_ERR(resp_skb
);
1357 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1358 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1359 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1360 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1361 out_hdr
->sadb_msg_errno
= 0;
1362 out_hdr
->sadb_msg_reserved
= 0;
1363 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1364 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1368 pfkey_broadcast(resp_skb
, BROADCAST_ONE
, sk
, net
);
1373 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1375 struct net
*net
= sock_net(sk
);
1376 struct xfrm_state
*x
;
1378 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1381 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1384 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1388 spin_lock_bh(&x
->lock
);
1389 if (x
->km
.state
== XFRM_STATE_ACQ
)
1390 x
->km
.state
= XFRM_STATE_ERROR
;
1392 spin_unlock_bh(&x
->lock
);
1397 static inline int event2poltype(int event
)
1400 case XFRM_MSG_DELPOLICY
:
1401 return SADB_X_SPDDELETE
;
1402 case XFRM_MSG_NEWPOLICY
:
1403 return SADB_X_SPDADD
;
1404 case XFRM_MSG_UPDPOLICY
:
1405 return SADB_X_SPDUPDATE
;
1406 case XFRM_MSG_POLEXPIRE
:
1407 // return SADB_X_SPDEXPIRE;
1409 pr_err("pfkey: Unknown policy event %d\n", event
);
1416 static inline int event2keytype(int event
)
1419 case XFRM_MSG_DELSA
:
1421 case XFRM_MSG_NEWSA
:
1423 case XFRM_MSG_UPDSA
:
1425 case XFRM_MSG_EXPIRE
:
1428 pr_err("pfkey: Unknown SA event %d\n", event
);
1436 static int key_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
1438 struct sk_buff
*skb
;
1439 struct sadb_msg
*hdr
;
1441 skb
= pfkey_xfrm_state2msg(x
);
1444 return PTR_ERR(skb
);
1446 hdr
= (struct sadb_msg
*) skb
->data
;
1447 hdr
->sadb_msg_version
= PF_KEY_V2
;
1448 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1449 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1450 hdr
->sadb_msg_errno
= 0;
1451 hdr
->sadb_msg_reserved
= 0;
1452 hdr
->sadb_msg_seq
= c
->seq
;
1453 hdr
->sadb_msg_pid
= c
->portid
;
1455 pfkey_broadcast(skb
, BROADCAST_ALL
, NULL
, xs_net(x
));
1460 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1462 struct net
*net
= sock_net(sk
);
1463 struct xfrm_state
*x
;
1467 x
= pfkey_msg2xfrm_state(net
, hdr
, ext_hdrs
);
1472 if (hdr
->sadb_msg_type
== SADB_ADD
)
1473 err
= xfrm_state_add(x
);
1475 err
= xfrm_state_update(x
);
1477 xfrm_audit_state_add(x
, err
? 0 : 1, true);
1480 x
->km
.state
= XFRM_STATE_DEAD
;
1481 __xfrm_state_put(x
);
1485 if (hdr
->sadb_msg_type
== SADB_ADD
)
1486 c
.event
= XFRM_MSG_NEWSA
;
1488 c
.event
= XFRM_MSG_UPDSA
;
1489 c
.seq
= hdr
->sadb_msg_seq
;
1490 c
.portid
= hdr
->sadb_msg_pid
;
1491 km_state_notify(x
, &c
);
1497 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1499 struct net
*net
= sock_net(sk
);
1500 struct xfrm_state
*x
;
1504 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1505 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1506 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1509 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1513 if ((err
= security_xfrm_state_delete(x
)))
1516 if (xfrm_state_kern(x
)) {
1521 err
= xfrm_state_delete(x
);
1526 c
.seq
= hdr
->sadb_msg_seq
;
1527 c
.portid
= hdr
->sadb_msg_pid
;
1528 c
.event
= XFRM_MSG_DELSA
;
1529 km_state_notify(x
, &c
);
1531 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
1537 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1539 struct net
*net
= sock_net(sk
);
1541 struct sk_buff
*out_skb
;
1542 struct sadb_msg
*out_hdr
;
1543 struct xfrm_state
*x
;
1545 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1546 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1547 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1550 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1554 out_skb
= pfkey_xfrm_state2msg(x
);
1555 proto
= x
->id
.proto
;
1557 if (IS_ERR(out_skb
))
1558 return PTR_ERR(out_skb
);
1560 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1561 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1562 out_hdr
->sadb_msg_type
= SADB_GET
;
1563 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1564 out_hdr
->sadb_msg_errno
= 0;
1565 out_hdr
->sadb_msg_reserved
= 0;
1566 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1567 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1568 pfkey_broadcast(out_skb
, BROADCAST_ONE
, sk
, sock_net(sk
));
1573 static struct sk_buff
*compose_sadb_supported(const struct sadb_msg
*orig
,
1576 struct sk_buff
*skb
;
1577 struct sadb_msg
*hdr
;
1578 int len
, auth_len
, enc_len
, i
;
1580 auth_len
= xfrm_count_pfkey_auth_supported();
1582 auth_len
*= sizeof(struct sadb_alg
);
1583 auth_len
+= sizeof(struct sadb_supported
);
1586 enc_len
= xfrm_count_pfkey_enc_supported();
1588 enc_len
*= sizeof(struct sadb_alg
);
1589 enc_len
+= sizeof(struct sadb_supported
);
1592 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1594 skb
= alloc_skb(len
+ 16, allocation
);
1598 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1599 pfkey_hdr_dup(hdr
, orig
);
1600 hdr
->sadb_msg_errno
= 0;
1601 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1604 struct sadb_supported
*sp
;
1605 struct sadb_alg
*ap
;
1607 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1608 ap
= (struct sadb_alg
*) (sp
+ 1);
1610 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1611 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1613 for (i
= 0; ; i
++) {
1614 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1617 if (!aalg
->pfkey_supported
)
1619 if (aalg
->available
)
1625 struct sadb_supported
*sp
;
1626 struct sadb_alg
*ap
;
1628 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1629 ap
= (struct sadb_alg
*) (sp
+ 1);
1631 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1632 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1634 for (i
= 0; ; i
++) {
1635 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1638 if (!ealg
->pfkey_supported
)
1640 if (ealg
->available
)
1649 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1651 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1652 struct sk_buff
*supp_skb
;
1654 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1657 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1658 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1660 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1665 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1667 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1668 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1673 pfkey_broadcast(supp_skb
, BROADCAST_REGISTERED
, sk
, sock_net(sk
));
1678 static int unicast_flush_resp(struct sock
*sk
, const struct sadb_msg
*ihdr
)
1680 struct sk_buff
*skb
;
1681 struct sadb_msg
*hdr
;
1683 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1687 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1688 memcpy(hdr
, ihdr
, sizeof(struct sadb_msg
));
1689 hdr
->sadb_msg_errno
= (uint8_t) 0;
1690 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1692 return pfkey_broadcast(skb
, BROADCAST_ONE
, sk
, sock_net(sk
));
1695 static int key_notify_sa_flush(const struct km_event
*c
)
1697 struct sk_buff
*skb
;
1698 struct sadb_msg
*hdr
;
1700 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1703 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1704 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1705 hdr
->sadb_msg_type
= SADB_FLUSH
;
1706 hdr
->sadb_msg_seq
= c
->seq
;
1707 hdr
->sadb_msg_pid
= c
->portid
;
1708 hdr
->sadb_msg_version
= PF_KEY_V2
;
1709 hdr
->sadb_msg_errno
= (uint8_t) 0;
1710 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1711 hdr
->sadb_msg_reserved
= 0;
1713 pfkey_broadcast(skb
, BROADCAST_ALL
, NULL
, c
->net
);
1718 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1720 struct net
*net
= sock_net(sk
);
1725 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1729 err
= xfrm_state_flush(net
, proto
, true);
1730 err2
= unicast_flush_resp(sk
, hdr
);
1732 if (err
== -ESRCH
) /* empty table - go quietly */
1734 return err
? err
: err2
;
1737 c
.data
.proto
= proto
;
1738 c
.seq
= hdr
->sadb_msg_seq
;
1739 c
.portid
= hdr
->sadb_msg_pid
;
1740 c
.event
= XFRM_MSG_FLUSHSA
;
1742 km_state_notify(NULL
, &c
);
1747 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1749 struct pfkey_sock
*pfk
= ptr
;
1750 struct sk_buff
*out_skb
;
1751 struct sadb_msg
*out_hdr
;
1753 if (!pfkey_can_dump(&pfk
->sk
))
1756 out_skb
= pfkey_xfrm_state2msg(x
);
1757 if (IS_ERR(out_skb
))
1758 return PTR_ERR(out_skb
);
1760 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1761 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1762 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1763 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1764 out_hdr
->sadb_msg_errno
= 0;
1765 out_hdr
->sadb_msg_reserved
= 0;
1766 out_hdr
->sadb_msg_seq
= count
+ 1;
1767 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
1770 pfkey_broadcast(pfk
->dump
.skb
, BROADCAST_ONE
,
1771 &pfk
->sk
, sock_net(&pfk
->sk
));
1772 pfk
->dump
.skb
= out_skb
;
1777 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1779 struct net
*net
= sock_net(&pfk
->sk
);
1780 return xfrm_state_walk(net
, &pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1783 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1785 struct net
*net
= sock_net(&pfk
->sk
);
1787 xfrm_state_walk_done(&pfk
->dump
.u
.state
, net
);
1790 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1793 struct xfrm_address_filter
*filter
= NULL
;
1794 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1796 if (pfk
->dump
.dump
!= NULL
)
1799 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1803 if (ext_hdrs
[SADB_X_EXT_FILTER
- 1]) {
1804 struct sadb_x_filter
*xfilter
= ext_hdrs
[SADB_X_EXT_FILTER
- 1];
1806 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
1810 memcpy(&filter
->saddr
, &xfilter
->sadb_x_filter_saddr
,
1811 sizeof(xfrm_address_t
));
1812 memcpy(&filter
->daddr
, &xfilter
->sadb_x_filter_daddr
,
1813 sizeof(xfrm_address_t
));
1814 filter
->family
= xfilter
->sadb_x_filter_family
;
1815 filter
->splen
= xfilter
->sadb_x_filter_splen
;
1816 filter
->dplen
= xfilter
->sadb_x_filter_dplen
;
1819 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1820 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
1821 pfk
->dump
.dump
= pfkey_dump_sa
;
1822 pfk
->dump
.done
= pfkey_dump_sa_done
;
1823 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
, filter
);
1825 return pfkey_do_dump(pfk
);
1828 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1830 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1831 int satype
= hdr
->sadb_msg_satype
;
1832 bool reset_errno
= false;
1834 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1836 if (satype
!= 0 && satype
!= 1)
1838 pfk
->promisc
= satype
;
1840 if (reset_errno
&& skb_cloned(skb
))
1841 skb
= skb_copy(skb
, GFP_KERNEL
);
1843 skb
= skb_clone(skb
, GFP_KERNEL
);
1845 if (reset_errno
&& skb
) {
1846 struct sadb_msg
*new_hdr
= (struct sadb_msg
*) skb
->data
;
1847 new_hdr
->sadb_msg_errno
= 0;
1850 pfkey_broadcast(skb
, BROADCAST_ALL
, NULL
, sock_net(sk
));
1854 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1857 u32 reqid
= *(u32
*)ptr
;
1859 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1860 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1866 static u32
gen_reqid(struct net
*net
)
1868 struct xfrm_policy_walk walk
;
1871 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1877 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1878 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1879 rc
= xfrm_policy_walk(net
, &walk
, check_reqid
, (void*)&reqid
);
1880 xfrm_policy_walk_done(&walk
, net
);
1883 } while (reqid
!= start
);
1888 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1890 struct net
*net
= xp_net(xp
);
1891 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1894 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1897 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1900 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1901 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1904 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1906 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1907 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1908 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1910 if (!t
->reqid
&& !(t
->reqid
= gen_reqid(net
)))
1914 /* addresses present only in tunnel mode */
1915 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1916 u8
*sa
= (u8
*) (rq
+ 1);
1917 int family
, socklen
;
1919 family
= pfkey_sockaddr_extract((struct sockaddr
*)sa
,
1924 socklen
= pfkey_sockaddr_len(family
);
1925 if (pfkey_sockaddr_extract((struct sockaddr
*)(sa
+ socklen
),
1926 &t
->id
.daddr
) != family
)
1928 t
->encap_family
= family
;
1930 t
->encap_family
= xp
->family
;
1932 /* No way to set this via kame pfkey */
1939 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1942 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1943 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1945 if (pol
->sadb_x_policy_len
* 8 < sizeof(struct sadb_x_policy
))
1948 while (len
>= sizeof(struct sadb_x_ipsecrequest
)) {
1949 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
1951 len
-= rq
->sadb_x_ipsecrequest_len
;
1952 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
1957 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy
*xp
)
1959 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
1962 int len
= sizeof(struct sadb_x_sec_ctx
);
1963 len
+= xfrm_ctx
->ctx_len
;
1964 return PFKEY_ALIGN8(len
);
1969 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy
*xp
)
1971 const struct xfrm_tmpl
*t
;
1972 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
1976 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1977 t
= xp
->xfrm_vec
+ i
;
1978 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
1981 return sizeof(struct sadb_msg
) +
1982 (sizeof(struct sadb_lifetime
) * 3) +
1983 (sizeof(struct sadb_address
) * 2) +
1984 (sockaddr_size
* 2) +
1985 sizeof(struct sadb_x_policy
) +
1986 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
1988 pfkey_xfrm_policy2sec_ctx_size(xp
);
1991 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(const struct xfrm_policy
*xp
)
1993 struct sk_buff
*skb
;
1996 size
= pfkey_xfrm_policy2msg_size(xp
);
1998 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2000 return ERR_PTR(-ENOBUFS
);
2005 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, const struct xfrm_policy
*xp
, int dir
)
2007 struct sadb_msg
*hdr
;
2008 struct sadb_address
*addr
;
2009 struct sadb_lifetime
*lifetime
;
2010 struct sadb_x_policy
*pol
;
2011 struct sadb_x_sec_ctx
*sec_ctx
;
2012 struct xfrm_sec_ctx
*xfrm_ctx
;
2015 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2016 int socklen
= pfkey_sockaddr_len(xp
->family
);
2018 size
= pfkey_xfrm_policy2msg_size(xp
);
2020 /* call should fill header later */
2021 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2022 memset(hdr
, 0, size
); /* XXX do we need this ? */
2025 addr
= (struct sadb_address
*) skb_put(skb
,
2026 sizeof(struct sadb_address
)+sockaddr_size
);
2027 addr
->sadb_address_len
=
2028 (sizeof(struct sadb_address
)+sockaddr_size
)/
2030 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2031 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2032 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
2033 addr
->sadb_address_reserved
= 0;
2034 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2036 (struct sockaddr
*) (addr
+ 1),
2041 addr
= (struct sadb_address
*) skb_put(skb
,
2042 sizeof(struct sadb_address
)+sockaddr_size
);
2043 addr
->sadb_address_len
=
2044 (sizeof(struct sadb_address
)+sockaddr_size
)/
2046 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2047 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2048 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2049 addr
->sadb_address_reserved
= 0;
2051 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2052 (struct sockaddr
*) (addr
+ 1),
2056 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2057 sizeof(struct sadb_lifetime
));
2058 lifetime
->sadb_lifetime_len
=
2059 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2060 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2061 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2062 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2063 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2064 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2066 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2067 sizeof(struct sadb_lifetime
));
2068 lifetime
->sadb_lifetime_len
=
2069 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2070 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2071 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2072 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2073 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2074 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2076 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2077 sizeof(struct sadb_lifetime
));
2078 lifetime
->sadb_lifetime_len
=
2079 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2080 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2081 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2082 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2083 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2084 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2086 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2087 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2088 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2089 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2090 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2092 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2094 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2096 pol
->sadb_x_policy_dir
= dir
+1;
2097 pol
->sadb_x_policy_reserved
= 0;
2098 pol
->sadb_x_policy_id
= xp
->index
;
2099 pol
->sadb_x_policy_priority
= xp
->priority
;
2101 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2102 const struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2103 struct sadb_x_ipsecrequest
*rq
;
2107 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2108 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2109 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2110 req_size
+= socklen
* 2;
2114 rq
= (void*)skb_put(skb
, req_size
);
2115 pol
->sadb_x_policy_len
+= req_size
/8;
2116 memset(rq
, 0, sizeof(*rq
));
2117 rq
->sadb_x_ipsecrequest_len
= req_size
;
2118 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2119 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2121 rq
->sadb_x_ipsecrequest_mode
= mode
;
2122 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2124 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2126 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2127 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2129 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2130 u8
*sa
= (void *)(rq
+ 1);
2131 pfkey_sockaddr_fill(&t
->saddr
, 0,
2132 (struct sockaddr
*)sa
,
2134 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2135 (struct sockaddr
*) (sa
+ socklen
),
2140 /* security context */
2141 if ((xfrm_ctx
= xp
->security
)) {
2142 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2144 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
, ctx_size
);
2145 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2146 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2147 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2148 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2149 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2150 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2154 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2155 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
2160 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2162 struct sk_buff
*out_skb
;
2163 struct sadb_msg
*out_hdr
;
2166 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2167 if (IS_ERR(out_skb
))
2168 return PTR_ERR(out_skb
);
2170 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2174 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2175 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2177 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2178 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2180 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2181 out_hdr
->sadb_msg_errno
= 0;
2182 out_hdr
->sadb_msg_seq
= c
->seq
;
2183 out_hdr
->sadb_msg_pid
= c
->portid
;
2184 pfkey_broadcast(out_skb
, BROADCAST_ALL
, NULL
, xp_net(xp
));
2189 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2191 struct net
*net
= sock_net(sk
);
2193 struct sadb_lifetime
*lifetime
;
2194 struct sadb_address
*sa
;
2195 struct sadb_x_policy
*pol
;
2196 struct xfrm_policy
*xp
;
2198 struct sadb_x_sec_ctx
*sec_ctx
;
2200 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2201 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2202 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2205 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2206 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2208 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2211 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2215 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2216 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2217 xp
->priority
= pol
->sadb_x_policy_priority
;
2219 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2220 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2221 xp
->selector
.family
= xp
->family
;
2222 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2223 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2224 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2225 if (xp
->selector
.sport
)
2226 xp
->selector
.sport_mask
= htons(0xffff);
2228 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2229 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2230 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2232 /* Amusing, we set this twice. KAME apps appear to set same value
2233 * in both addresses.
2235 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2237 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2238 if (xp
->selector
.dport
)
2239 xp
->selector
.dport_mask
= htons(0xffff);
2241 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2242 if (sec_ctx
!= NULL
) {
2243 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2250 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_KERNEL
);
2257 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2258 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2259 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2260 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2261 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2262 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2263 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2264 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2265 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2267 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2268 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2269 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2270 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2271 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2274 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2275 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2278 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2279 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2281 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
2286 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2287 c
.event
= XFRM_MSG_UPDPOLICY
;
2289 c
.event
= XFRM_MSG_NEWPOLICY
;
2291 c
.seq
= hdr
->sadb_msg_seq
;
2292 c
.portid
= hdr
->sadb_msg_pid
;
2294 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2300 xfrm_policy_destroy(xp
);
2304 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2306 struct net
*net
= sock_net(sk
);
2308 struct sadb_address
*sa
;
2309 struct sadb_x_policy
*pol
;
2310 struct xfrm_policy
*xp
;
2311 struct xfrm_selector sel
;
2313 struct sadb_x_sec_ctx
*sec_ctx
;
2314 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2316 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2317 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2318 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2321 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2322 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2325 memset(&sel
, 0, sizeof(sel
));
2327 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2328 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2329 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2330 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2331 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2333 sel
.sport_mask
= htons(0xffff);
2335 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2336 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2337 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2338 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2339 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2341 sel
.dport_mask
= htons(0xffff);
2343 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2344 if (sec_ctx
!= NULL
) {
2345 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2350 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
, GFP_KERNEL
);
2356 xp
= xfrm_policy_bysel_ctx(net
, DUMMY_MARK
, XFRM_POLICY_TYPE_MAIN
,
2357 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2359 security_xfrm_policy_free(pol_ctx
);
2363 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2368 c
.seq
= hdr
->sadb_msg_seq
;
2369 c
.portid
= hdr
->sadb_msg_pid
;
2371 c
.event
= XFRM_MSG_DELPOLICY
;
2372 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2377 xfrm_garbage_collect(net
);
2381 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, const struct sadb_msg
*hdr
, int dir
)
2384 struct sk_buff
*out_skb
;
2385 struct sadb_msg
*out_hdr
;
2388 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2389 if (IS_ERR(out_skb
)) {
2390 err
= PTR_ERR(out_skb
);
2393 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2397 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2398 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2399 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2400 out_hdr
->sadb_msg_satype
= 0;
2401 out_hdr
->sadb_msg_errno
= 0;
2402 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2403 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2404 pfkey_broadcast(out_skb
, BROADCAST_ONE
, sk
, xp_net(xp
));
2411 #ifdef CONFIG_NET_KEY_MIGRATE
2412 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2414 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2417 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2418 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2423 if (ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2426 af
= pfkey_sockaddr_extract(sa
, saddr
);
2430 socklen
= pfkey_sockaddr_len(af
);
2431 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2439 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2440 struct xfrm_migrate
*m
)
2443 struct sadb_x_ipsecrequest
*rq2
;
2446 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2447 len
< rq1
->sadb_x_ipsecrequest_len
)
2451 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2452 rq1
->sadb_x_ipsecrequest_len
,
2453 &m
->old_saddr
, &m
->old_daddr
,
2458 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2459 len
-= rq1
->sadb_x_ipsecrequest_len
;
2461 if (len
<= sizeof(struct sadb_x_ipsecrequest
) ||
2462 len
< rq2
->sadb_x_ipsecrequest_len
)
2466 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2467 rq2
->sadb_x_ipsecrequest_len
,
2468 &m
->new_saddr
, &m
->new_daddr
,
2473 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2474 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2475 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2478 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2479 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2482 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2484 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2485 rq2
->sadb_x_ipsecrequest_len
));
2488 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2489 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2491 int i
, len
, ret
, err
= -EINVAL
;
2493 struct sadb_address
*sa
;
2494 struct sadb_x_kmaddress
*kma
;
2495 struct sadb_x_policy
*pol
;
2496 struct sadb_x_ipsecrequest
*rq
;
2497 struct xfrm_selector sel
;
2498 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2499 struct xfrm_kmaddress k
;
2500 struct net
*net
= sock_net(sk
);
2502 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2503 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2504 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2509 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2510 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2512 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2518 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2519 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2520 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2521 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2522 &k
.local
, &k
.remote
, &k
.family
);
2529 dir
= pol
->sadb_x_policy_dir
- 1;
2530 memset(&sel
, 0, sizeof(sel
));
2532 /* set source address info of selector */
2533 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2534 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2535 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2536 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2537 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2539 sel
.sport_mask
= htons(0xffff);
2541 /* set destination address info of selector */
2542 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
2543 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2544 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2545 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2546 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2548 sel
.dport_mask
= htons(0xffff);
2550 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2552 /* extract ipsecrequests */
2554 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2556 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2557 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2562 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2568 if (!i
|| len
> 0) {
2573 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2574 kma
? &k
: NULL
, net
);
2580 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2581 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2583 return -ENOPROTOOPT
;
2588 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2590 struct net
*net
= sock_net(sk
);
2592 int err
= 0, delete;
2593 struct sadb_x_policy
*pol
;
2594 struct xfrm_policy
*xp
;
2597 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2600 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2601 if (dir
>= XFRM_POLICY_MAX
)
2604 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2605 xp
= xfrm_policy_byid(net
, DUMMY_MARK
, XFRM_POLICY_TYPE_MAIN
,
2606 dir
, pol
->sadb_x_policy_id
, delete, &err
);
2611 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2615 c
.seq
= hdr
->sadb_msg_seq
;
2616 c
.portid
= hdr
->sadb_msg_pid
;
2618 c
.event
= XFRM_MSG_DELPOLICY
;
2619 km_policy_notify(xp
, dir
, &c
);
2621 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2626 if (delete && err
== 0)
2627 xfrm_garbage_collect(net
);
2631 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2633 struct pfkey_sock
*pfk
= ptr
;
2634 struct sk_buff
*out_skb
;
2635 struct sadb_msg
*out_hdr
;
2638 if (!pfkey_can_dump(&pfk
->sk
))
2641 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2642 if (IS_ERR(out_skb
))
2643 return PTR_ERR(out_skb
);
2645 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2649 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2650 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2651 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2652 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2653 out_hdr
->sadb_msg_errno
= 0;
2654 out_hdr
->sadb_msg_seq
= count
+ 1;
2655 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
2658 pfkey_broadcast(pfk
->dump
.skb
, BROADCAST_ONE
,
2659 &pfk
->sk
, sock_net(&pfk
->sk
));
2660 pfk
->dump
.skb
= out_skb
;
2665 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2667 struct net
*net
= sock_net(&pfk
->sk
);
2668 return xfrm_policy_walk(net
, &pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2671 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2673 struct net
*net
= sock_net((struct sock
*)pfk
);
2675 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
, net
);
2678 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2680 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2682 if (pfk
->dump
.dump
!= NULL
)
2685 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2686 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
2687 pfk
->dump
.dump
= pfkey_dump_sp
;
2688 pfk
->dump
.done
= pfkey_dump_sp_done
;
2689 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2691 return pfkey_do_dump(pfk
);
2694 static int key_notify_policy_flush(const struct km_event
*c
)
2696 struct sk_buff
*skb_out
;
2697 struct sadb_msg
*hdr
;
2699 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2702 hdr
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2703 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2704 hdr
->sadb_msg_seq
= c
->seq
;
2705 hdr
->sadb_msg_pid
= c
->portid
;
2706 hdr
->sadb_msg_version
= PF_KEY_V2
;
2707 hdr
->sadb_msg_errno
= (uint8_t) 0;
2708 hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2709 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2710 hdr
->sadb_msg_reserved
= 0;
2711 pfkey_broadcast(skb_out
, BROADCAST_ALL
, NULL
, c
->net
);
2716 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2718 struct net
*net
= sock_net(sk
);
2722 err
= xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, true);
2723 err2
= unicast_flush_resp(sk
, hdr
);
2725 if (err
== -ESRCH
) /* empty table - old silent behavior */
2730 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2731 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2732 c
.portid
= hdr
->sadb_msg_pid
;
2733 c
.seq
= hdr
->sadb_msg_seq
;
2735 km_policy_notify(NULL
, 0, &c
);
2740 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2741 const struct sadb_msg
*hdr
, void * const *ext_hdrs
);
2742 static const pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2743 [SADB_RESERVED
] = pfkey_reserved
,
2744 [SADB_GETSPI
] = pfkey_getspi
,
2745 [SADB_UPDATE
] = pfkey_add
,
2746 [SADB_ADD
] = pfkey_add
,
2747 [SADB_DELETE
] = pfkey_delete
,
2748 [SADB_GET
] = pfkey_get
,
2749 [SADB_ACQUIRE
] = pfkey_acquire
,
2750 [SADB_REGISTER
] = pfkey_register
,
2751 [SADB_EXPIRE
] = NULL
,
2752 [SADB_FLUSH
] = pfkey_flush
,
2753 [SADB_DUMP
] = pfkey_dump
,
2754 [SADB_X_PROMISC
] = pfkey_promisc
,
2755 [SADB_X_PCHANGE
] = NULL
,
2756 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2757 [SADB_X_SPDADD
] = pfkey_spdadd
,
2758 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2759 [SADB_X_SPDGET
] = pfkey_spdget
,
2760 [SADB_X_SPDACQUIRE
] = NULL
,
2761 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2762 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2763 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2764 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2765 [SADB_X_MIGRATE
] = pfkey_migrate
,
2768 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
)
2770 void *ext_hdrs
[SADB_EXT_MAX
];
2773 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
),
2774 BROADCAST_PROMISC_ONLY
, NULL
, sock_net(sk
));
2776 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2777 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2780 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2781 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2786 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2788 struct sadb_msg
*hdr
= NULL
;
2790 if (skb
->len
< sizeof(*hdr
)) {
2793 hdr
= (struct sadb_msg
*) skb
->data
;
2794 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2795 hdr
->sadb_msg_reserved
!= 0 ||
2796 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2797 hdr
->sadb_msg_type
> SADB_MAX
)) {
2800 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2801 sizeof(uint64_t)) ||
2802 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2803 sizeof(uint64_t))) {
2813 static inline int aalg_tmpl_set(const struct xfrm_tmpl
*t
,
2814 const struct xfrm_algo_desc
*d
)
2816 unsigned int id
= d
->desc
.sadb_alg_id
;
2818 if (id
>= sizeof(t
->aalgos
) * 8)
2821 return (t
->aalgos
>> id
) & 1;
2824 static inline int ealg_tmpl_set(const struct xfrm_tmpl
*t
,
2825 const struct xfrm_algo_desc
*d
)
2827 unsigned int id
= d
->desc
.sadb_alg_id
;
2829 if (id
>= sizeof(t
->ealgos
) * 8)
2832 return (t
->ealgos
>> id
) & 1;
2835 static int count_ah_combs(const struct xfrm_tmpl
*t
)
2839 for (i
= 0; ; i
++) {
2840 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2843 if (!aalg
->pfkey_supported
)
2845 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2846 sz
+= sizeof(struct sadb_comb
);
2848 return sz
+ sizeof(struct sadb_prop
);
2851 static int count_esp_combs(const struct xfrm_tmpl
*t
)
2855 for (i
= 0; ; i
++) {
2856 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2860 if (!ealg
->pfkey_supported
)
2863 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2866 for (k
= 1; ; k
++) {
2867 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2871 if (!aalg
->pfkey_supported
)
2874 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2875 sz
+= sizeof(struct sadb_comb
);
2878 return sz
+ sizeof(struct sadb_prop
);
2881 static void dump_ah_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2883 struct sadb_prop
*p
;
2886 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2887 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2888 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2889 p
->sadb_prop_replay
= 32;
2890 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2892 for (i
= 0; ; i
++) {
2893 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2897 if (!aalg
->pfkey_supported
)
2900 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2901 struct sadb_comb
*c
;
2902 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2903 memset(c
, 0, sizeof(*c
));
2904 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2905 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2906 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2907 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2908 c
->sadb_comb_hard_addtime
= 24*60*60;
2909 c
->sadb_comb_soft_addtime
= 20*60*60;
2910 c
->sadb_comb_hard_usetime
= 8*60*60;
2911 c
->sadb_comb_soft_usetime
= 7*60*60;
2916 static void dump_esp_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2918 struct sadb_prop
*p
;
2921 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2922 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2923 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2924 p
->sadb_prop_replay
= 32;
2925 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2928 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2932 if (!ealg
->pfkey_supported
)
2935 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2938 for (k
= 1; ; k
++) {
2939 struct sadb_comb
*c
;
2940 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2943 if (!aalg
->pfkey_supported
)
2945 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2947 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2948 memset(c
, 0, sizeof(*c
));
2949 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2950 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2951 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2952 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2953 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
2954 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
2955 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
2956 c
->sadb_comb_hard_addtime
= 24*60*60;
2957 c
->sadb_comb_soft_addtime
= 20*60*60;
2958 c
->sadb_comb_hard_usetime
= 8*60*60;
2959 c
->sadb_comb_soft_usetime
= 7*60*60;
2964 static int key_notify_policy_expire(struct xfrm_policy
*xp
, const struct km_event
*c
)
2969 static int key_notify_sa_expire(struct xfrm_state
*x
, const struct km_event
*c
)
2971 struct sk_buff
*out_skb
;
2972 struct sadb_msg
*out_hdr
;
2976 hard
= c
->data
.hard
;
2982 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
2983 if (IS_ERR(out_skb
))
2984 return PTR_ERR(out_skb
);
2986 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2987 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2988 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
2989 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
2990 out_hdr
->sadb_msg_errno
= 0;
2991 out_hdr
->sadb_msg_reserved
= 0;
2992 out_hdr
->sadb_msg_seq
= 0;
2993 out_hdr
->sadb_msg_pid
= 0;
2995 pfkey_broadcast(out_skb
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
2999 static int pfkey_send_notify(struct xfrm_state
*x
, const struct km_event
*c
)
3001 struct net
*net
= x
? xs_net(x
) : c
->net
;
3002 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3004 if (atomic_read(&net_pfkey
->socks_nr
) == 0)
3008 case XFRM_MSG_EXPIRE
:
3009 return key_notify_sa_expire(x
, c
);
3010 case XFRM_MSG_DELSA
:
3011 case XFRM_MSG_NEWSA
:
3012 case XFRM_MSG_UPDSA
:
3013 return key_notify_sa(x
, c
);
3014 case XFRM_MSG_FLUSHSA
:
3015 return key_notify_sa_flush(c
);
3016 case XFRM_MSG_NEWAE
: /* not yet supported */
3019 pr_err("pfkey: Unknown SA event %d\n", c
->event
);
3026 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3028 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
3032 case XFRM_MSG_POLEXPIRE
:
3033 return key_notify_policy_expire(xp
, c
);
3034 case XFRM_MSG_DELPOLICY
:
3035 case XFRM_MSG_NEWPOLICY
:
3036 case XFRM_MSG_UPDPOLICY
:
3037 return key_notify_policy(xp
, dir
, c
);
3038 case XFRM_MSG_FLUSHPOLICY
:
3039 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
3041 return key_notify_policy_flush(c
);
3043 pr_err("pfkey: Unknown policy event %d\n", c
->event
);
3050 static u32
get_acqseq(void)
3053 static atomic_t acqseq
;
3056 res
= atomic_inc_return(&acqseq
);
3061 static bool pfkey_is_alive(const struct km_event
*c
)
3063 struct netns_pfkey
*net_pfkey
= net_generic(c
->net
, pfkey_net_id
);
3065 bool is_alive
= false;
3068 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
3069 if (pfkey_sk(sk
)->registered
) {
3079 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
)
3081 struct sk_buff
*skb
;
3082 struct sadb_msg
*hdr
;
3083 struct sadb_address
*addr
;
3084 struct sadb_x_policy
*pol
;
3087 struct sadb_x_sec_ctx
*sec_ctx
;
3088 struct xfrm_sec_ctx
*xfrm_ctx
;
3091 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3095 size
= sizeof(struct sadb_msg
) +
3096 (sizeof(struct sadb_address
) * 2) +
3097 (sockaddr_size
* 2) +
3098 sizeof(struct sadb_x_policy
);
3100 if (x
->id
.proto
== IPPROTO_AH
)
3101 size
+= count_ah_combs(t
);
3102 else if (x
->id
.proto
== IPPROTO_ESP
)
3103 size
+= count_esp_combs(t
);
3105 if ((xfrm_ctx
= x
->security
)) {
3106 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3107 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3110 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3114 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3115 hdr
->sadb_msg_version
= PF_KEY_V2
;
3116 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3117 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3118 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3119 hdr
->sadb_msg_errno
= 0;
3120 hdr
->sadb_msg_reserved
= 0;
3121 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3122 hdr
->sadb_msg_pid
= 0;
3125 addr
= (struct sadb_address
*) skb_put(skb
,
3126 sizeof(struct sadb_address
)+sockaddr_size
);
3127 addr
->sadb_address_len
=
3128 (sizeof(struct sadb_address
)+sockaddr_size
)/
3130 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3131 addr
->sadb_address_proto
= 0;
3132 addr
->sadb_address_reserved
= 0;
3133 addr
->sadb_address_prefixlen
=
3134 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3135 (struct sockaddr
*) (addr
+ 1),
3137 if (!addr
->sadb_address_prefixlen
)
3141 addr
= (struct sadb_address
*) skb_put(skb
,
3142 sizeof(struct sadb_address
)+sockaddr_size
);
3143 addr
->sadb_address_len
=
3144 (sizeof(struct sadb_address
)+sockaddr_size
)/
3146 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3147 addr
->sadb_address_proto
= 0;
3148 addr
->sadb_address_reserved
= 0;
3149 addr
->sadb_address_prefixlen
=
3150 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3151 (struct sockaddr
*) (addr
+ 1),
3153 if (!addr
->sadb_address_prefixlen
)
3156 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
3157 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3158 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3159 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3160 pol
->sadb_x_policy_dir
= XFRM_POLICY_OUT
+ 1;
3161 pol
->sadb_x_policy_reserved
= 0;
3162 pol
->sadb_x_policy_id
= xp
->index
;
3163 pol
->sadb_x_policy_priority
= xp
->priority
;
3165 /* Set sadb_comb's. */
3166 if (x
->id
.proto
== IPPROTO_AH
)
3167 dump_ah_combs(skb
, t
);
3168 else if (x
->id
.proto
== IPPROTO_ESP
)
3169 dump_esp_combs(skb
, t
);
3171 /* security context */
3173 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
3174 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3175 sec_ctx
->sadb_x_sec_len
=
3176 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3177 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3178 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3179 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3180 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3181 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3185 return pfkey_broadcast(skb
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
3188 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3189 u8
*data
, int len
, int *dir
)
3191 struct net
*net
= sock_net(sk
);
3192 struct xfrm_policy
*xp
;
3193 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3194 struct sadb_x_sec_ctx
*sec_ctx
;
3196 switch (sk
->sk_family
) {
3198 if (opt
!= IP_IPSEC_POLICY
) {
3203 #if IS_ENABLED(CONFIG_IPV6)
3205 if (opt
!= IPV6_IPSEC_POLICY
) {
3218 if (len
< sizeof(struct sadb_x_policy
) ||
3219 pol
->sadb_x_policy_len
*8 > len
||
3220 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3221 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3224 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
3230 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3231 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3233 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3234 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3235 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3236 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3237 xp
->family
= sk
->sk_family
;
3240 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3241 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3244 /* security context too */
3245 if (len
>= (pol
->sadb_x_policy_len
*8 +
3246 sizeof(struct sadb_x_sec_ctx
))) {
3247 char *p
= (char *)pol
;
3248 struct xfrm_user_sec_ctx
*uctx
;
3250 p
+= pol
->sadb_x_policy_len
*8;
3251 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3252 if (len
< pol
->sadb_x_policy_len
*8 +
3253 sec_ctx
->sadb_x_sec_len
) {
3257 if ((*dir
= verify_sec_ctx_len(p
)))
3259 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_ATOMIC
);
3260 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_ATOMIC
);
3267 *dir
= pol
->sadb_x_policy_dir
-1;
3272 xfrm_policy_destroy(xp
);
3276 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3278 struct sk_buff
*skb
;
3279 struct sadb_msg
*hdr
;
3281 struct sadb_address
*addr
;
3282 struct sadb_x_nat_t_port
*n_port
;
3285 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3286 struct xfrm_encap_tmpl
*natt
= NULL
;
3288 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3300 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3302 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3303 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3306 size
= sizeof(struct sadb_msg
) +
3307 sizeof(struct sadb_sa
) +
3308 (sizeof(struct sadb_address
) * 2) +
3309 (sockaddr_size
* 2) +
3310 (sizeof(struct sadb_x_nat_t_port
) * 2);
3312 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3316 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3317 hdr
->sadb_msg_version
= PF_KEY_V2
;
3318 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3319 hdr
->sadb_msg_satype
= satype
;
3320 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3321 hdr
->sadb_msg_errno
= 0;
3322 hdr
->sadb_msg_reserved
= 0;
3323 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3324 hdr
->sadb_msg_pid
= 0;
3327 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
3328 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3329 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3330 sa
->sadb_sa_spi
= x
->id
.spi
;
3331 sa
->sadb_sa_replay
= 0;
3332 sa
->sadb_sa_state
= 0;
3333 sa
->sadb_sa_auth
= 0;
3334 sa
->sadb_sa_encrypt
= 0;
3335 sa
->sadb_sa_flags
= 0;
3337 /* ADDRESS_SRC (old addr) */
3338 addr
= (struct sadb_address
*)
3339 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3340 addr
->sadb_address_len
=
3341 (sizeof(struct sadb_address
)+sockaddr_size
)/
3343 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3344 addr
->sadb_address_proto
= 0;
3345 addr
->sadb_address_reserved
= 0;
3346 addr
->sadb_address_prefixlen
=
3347 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3348 (struct sockaddr
*) (addr
+ 1),
3350 if (!addr
->sadb_address_prefixlen
)
3353 /* NAT_T_SPORT (old port) */
3354 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3355 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3356 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3357 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3358 n_port
->sadb_x_nat_t_port_reserved
= 0;
3360 /* ADDRESS_DST (new addr) */
3361 addr
= (struct sadb_address
*)
3362 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3363 addr
->sadb_address_len
=
3364 (sizeof(struct sadb_address
)+sockaddr_size
)/
3366 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3367 addr
->sadb_address_proto
= 0;
3368 addr
->sadb_address_reserved
= 0;
3369 addr
->sadb_address_prefixlen
=
3370 pfkey_sockaddr_fill(ipaddr
, 0,
3371 (struct sockaddr
*) (addr
+ 1),
3373 if (!addr
->sadb_address_prefixlen
)
3376 /* NAT_T_DPORT (new port) */
3377 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3378 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3379 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3380 n_port
->sadb_x_nat_t_port_port
= sport
;
3381 n_port
->sadb_x_nat_t_port_reserved
= 0;
3383 return pfkey_broadcast(skb
, BROADCAST_REGISTERED
, NULL
, xs_net(x
));
3386 #ifdef CONFIG_NET_KEY_MIGRATE
3387 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3388 const struct xfrm_selector
*sel
)
3390 struct sadb_address
*addr
;
3391 addr
= (struct sadb_address
*)skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3392 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3393 addr
->sadb_address_exttype
= type
;
3394 addr
->sadb_address_proto
= sel
->proto
;
3395 addr
->sadb_address_reserved
= 0;
3398 case SADB_EXT_ADDRESS_SRC
:
3399 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3400 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3401 (struct sockaddr
*)(addr
+ 1),
3404 case SADB_EXT_ADDRESS_DST
:
3405 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3406 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3407 (struct sockaddr
*)(addr
+ 1),
3418 static int set_sadb_kmaddress(struct sk_buff
*skb
, const struct xfrm_kmaddress
*k
)
3420 struct sadb_x_kmaddress
*kma
;
3422 int family
= k
->family
;
3423 int socklen
= pfkey_sockaddr_len(family
);
3426 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3427 pfkey_sockaddr_pair_size(family
));
3429 kma
= (struct sadb_x_kmaddress
*)skb_put(skb
, size_req
);
3430 memset(kma
, 0, size_req
);
3431 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3432 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3433 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3435 sa
= (u8
*)(kma
+ 1);
3436 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3437 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3443 static int set_ipsecrequest(struct sk_buff
*skb
,
3444 uint8_t proto
, uint8_t mode
, int level
,
3445 uint32_t reqid
, uint8_t family
,
3446 const xfrm_address_t
*src
, const xfrm_address_t
*dst
)
3448 struct sadb_x_ipsecrequest
*rq
;
3450 int socklen
= pfkey_sockaddr_len(family
);
3453 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3454 pfkey_sockaddr_pair_size(family
);
3456 rq
= (struct sadb_x_ipsecrequest
*)skb_put(skb
, size_req
);
3457 memset(rq
, 0, size_req
);
3458 rq
->sadb_x_ipsecrequest_len
= size_req
;
3459 rq
->sadb_x_ipsecrequest_proto
= proto
;
3460 rq
->sadb_x_ipsecrequest_mode
= mode
;
3461 rq
->sadb_x_ipsecrequest_level
= level
;
3462 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3464 sa
= (u8
*) (rq
+ 1);
3465 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3466 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3473 #ifdef CONFIG_NET_KEY_MIGRATE
3474 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3475 const struct xfrm_migrate
*m
, int num_bundles
,
3476 const struct xfrm_kmaddress
*k
)
3482 struct sk_buff
*skb
;
3483 struct sadb_msg
*hdr
;
3484 struct sadb_x_policy
*pol
;
3485 const struct xfrm_migrate
*mp
;
3487 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3490 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3494 /* addresses for KM */
3495 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3496 pfkey_sockaddr_pair_size(k
->family
));
3500 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3503 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3506 size_pol
+= sizeof(struct sadb_x_policy
);
3509 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3510 /* old locator pair */
3511 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3512 pfkey_sockaddr_pair_size(mp
->old_family
);
3513 /* new locator pair */
3514 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3515 pfkey_sockaddr_pair_size(mp
->new_family
);
3518 size
+= sizeof(struct sadb_msg
) + size_pol
;
3521 skb
= alloc_skb(size
, GFP_ATOMIC
);
3525 hdr
= (struct sadb_msg
*)skb_put(skb
, sizeof(struct sadb_msg
));
3526 hdr
->sadb_msg_version
= PF_KEY_V2
;
3527 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3528 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3529 hdr
->sadb_msg_len
= size
/ 8;
3530 hdr
->sadb_msg_errno
= 0;
3531 hdr
->sadb_msg_reserved
= 0;
3532 hdr
->sadb_msg_seq
= 0;
3533 hdr
->sadb_msg_pid
= 0;
3535 /* Addresses to be used by KM for negotiation, if ext is available */
3536 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3540 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3543 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3545 /* policy information */
3546 pol
= (struct sadb_x_policy
*)skb_put(skb
, sizeof(struct sadb_x_policy
));
3547 pol
->sadb_x_policy_len
= size_pol
/ 8;
3548 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3549 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3550 pol
->sadb_x_policy_dir
= dir
+ 1;
3551 pol
->sadb_x_policy_reserved
= 0;
3552 pol
->sadb_x_policy_id
= 0;
3553 pol
->sadb_x_policy_priority
= 0;
3555 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3556 /* old ipsecrequest */
3557 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3560 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3561 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3562 mp
->reqid
, mp
->old_family
,
3563 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3566 /* new ipsecrequest */
3567 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3568 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3569 mp
->reqid
, mp
->new_family
,
3570 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3574 /* broadcast migrate message to sockets */
3575 pfkey_broadcast(skb
, BROADCAST_ALL
, NULL
, &init_net
);
3584 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3585 const struct xfrm_migrate
*m
, int num_bundles
,
3586 const struct xfrm_kmaddress
*k
)
3588 return -ENOPROTOOPT
;
3592 static int pfkey_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3594 struct sock
*sk
= sock
->sk
;
3595 struct sk_buff
*skb
= NULL
;
3596 struct sadb_msg
*hdr
= NULL
;
3598 struct net
*net
= sock_net(sk
);
3601 if (msg
->msg_flags
& MSG_OOB
)
3605 if ((unsigned int)len
> sk
->sk_sndbuf
- 32)
3609 skb
= alloc_skb(len
, GFP_KERNEL
);
3614 if (memcpy_from_msg(skb_put(skb
,len
), msg
, len
))
3617 hdr
= pfkey_get_base_msg(skb
, &err
);
3621 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
3622 err
= pfkey_process(sk
, skb
, hdr
);
3623 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
3626 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3633 static int pfkey_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3636 struct sock
*sk
= sock
->sk
;
3637 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3638 struct sk_buff
*skb
;
3642 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3645 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3651 msg
->msg_flags
|= MSG_TRUNC
;
3655 skb_reset_transport_header(skb
);
3656 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
3660 sock_recv_ts_and_drops(msg
, sk
, skb
);
3662 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3664 if (pfk
->dump
.dump
!= NULL
&&
3665 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3669 skb_free_datagram(sk
, skb
);
3674 static const struct proto_ops pfkey_ops
= {
3676 .owner
= THIS_MODULE
,
3677 /* Operations that make no sense on pfkey sockets. */
3678 .bind
= sock_no_bind
,
3679 .connect
= sock_no_connect
,
3680 .socketpair
= sock_no_socketpair
,
3681 .accept
= sock_no_accept
,
3682 .getname
= sock_no_getname
,
3683 .ioctl
= sock_no_ioctl
,
3684 .listen
= sock_no_listen
,
3685 .shutdown
= sock_no_shutdown
,
3686 .setsockopt
= sock_no_setsockopt
,
3687 .getsockopt
= sock_no_getsockopt
,
3688 .mmap
= sock_no_mmap
,
3689 .sendpage
= sock_no_sendpage
,
3691 /* Now the operations that really occur. */
3692 .release
= pfkey_release
,
3693 .poll
= datagram_poll
,
3694 .sendmsg
= pfkey_sendmsg
,
3695 .recvmsg
= pfkey_recvmsg
,
3698 static const struct net_proto_family pfkey_family_ops
= {
3700 .create
= pfkey_create
,
3701 .owner
= THIS_MODULE
,
3704 #ifdef CONFIG_PROC_FS
3705 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3707 struct sock
*s
= sk_entry(v
);
3709 if (v
== SEQ_START_TOKEN
)
3710 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3712 seq_printf(f
, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3714 atomic_read(&s
->sk_refcnt
),
3715 sk_rmem_alloc_get(s
),
3716 sk_wmem_alloc_get(s
),
3717 from_kuid_munged(seq_user_ns(f
), sock_i_uid(s
)),
3723 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3726 struct net
*net
= seq_file_net(f
);
3727 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3730 return seq_hlist_start_head_rcu(&net_pfkey
->table
, *ppos
);
3733 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3735 struct net
*net
= seq_file_net(f
);
3736 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3738 return seq_hlist_next_rcu(v
, &net_pfkey
->table
, ppos
);
3741 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3747 static const struct seq_operations pfkey_seq_ops
= {
3748 .start
= pfkey_seq_start
,
3749 .next
= pfkey_seq_next
,
3750 .stop
= pfkey_seq_stop
,
3751 .show
= pfkey_seq_show
,
3754 static int pfkey_seq_open(struct inode
*inode
, struct file
*file
)
3756 return seq_open_net(inode
, file
, &pfkey_seq_ops
,
3757 sizeof(struct seq_net_private
));
3760 static const struct file_operations pfkey_proc_ops
= {
3761 .open
= pfkey_seq_open
,
3763 .llseek
= seq_lseek
,
3764 .release
= seq_release_net
,
3767 static int __net_init
pfkey_init_proc(struct net
*net
)
3769 struct proc_dir_entry
*e
;
3771 e
= proc_create("pfkey", 0, net
->proc_net
, &pfkey_proc_ops
);
3778 static void __net_exit
pfkey_exit_proc(struct net
*net
)
3780 remove_proc_entry("pfkey", net
->proc_net
);
3783 static inline int pfkey_init_proc(struct net
*net
)
3788 static inline void pfkey_exit_proc(struct net
*net
)
3793 static struct xfrm_mgr pfkeyv2_mgr
=
3796 .notify
= pfkey_send_notify
,
3797 .acquire
= pfkey_send_acquire
,
3798 .compile_policy
= pfkey_compile_policy
,
3799 .new_mapping
= pfkey_send_new_mapping
,
3800 .notify_policy
= pfkey_send_policy_notify
,
3801 .migrate
= pfkey_send_migrate
,
3802 .is_alive
= pfkey_is_alive
,
3805 static int __net_init
pfkey_net_init(struct net
*net
)
3807 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3810 INIT_HLIST_HEAD(&net_pfkey
->table
);
3811 atomic_set(&net_pfkey
->socks_nr
, 0);
3813 rv
= pfkey_init_proc(net
);
3818 static void __net_exit
pfkey_net_exit(struct net
*net
)
3820 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3822 pfkey_exit_proc(net
);
3823 BUG_ON(!hlist_empty(&net_pfkey
->table
));
3826 static struct pernet_operations pfkey_net_ops
= {
3827 .init
= pfkey_net_init
,
3828 .exit
= pfkey_net_exit
,
3829 .id
= &pfkey_net_id
,
3830 .size
= sizeof(struct netns_pfkey
),
3833 static void __exit
ipsec_pfkey_exit(void)
3835 xfrm_unregister_km(&pfkeyv2_mgr
);
3836 sock_unregister(PF_KEY
);
3837 unregister_pernet_subsys(&pfkey_net_ops
);
3838 proto_unregister(&key_proto
);
3841 static int __init
ipsec_pfkey_init(void)
3843 int err
= proto_register(&key_proto
, 0);
3848 err
= register_pernet_subsys(&pfkey_net_ops
);
3850 goto out_unregister_key_proto
;
3851 err
= sock_register(&pfkey_family_ops
);
3853 goto out_unregister_pernet
;
3854 err
= xfrm_register_km(&pfkeyv2_mgr
);
3856 goto out_sock_unregister
;
3860 out_sock_unregister
:
3861 sock_unregister(PF_KEY
);
3862 out_unregister_pernet
:
3863 unregister_pernet_subsys(&pfkey_net_ops
);
3864 out_unregister_key_proto
:
3865 proto_unregister(&key_proto
);
3869 module_init(ipsec_pfkey_init
);
3870 module_exit(ipsec_pfkey_exit
);
3871 MODULE_LICENSE("GPL");
3872 MODULE_ALIAS_NETPROTO(PF_KEY
);