1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
5 * Authors: Maxim Giryaev <gem@asplinux.ru>
6 * David S. Miller <davem@redhat.com>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
10 * Derek Atkins <derek@ihtfp.com>
13 #include <linux/capability.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/socket.h>
17 #include <linux/pfkeyv2.h>
18 #include <linux/ipsec.h>
19 #include <linux/skbuff.h>
20 #include <linux/rtnetlink.h>
22 #include <linux/in6.h>
23 #include <linux/proc_fs.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <net/net_namespace.h>
27 #include <net/netns/generic.h>
32 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
33 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
35 static unsigned int pfkey_net_id __read_mostly
;
37 /* List of all pfkey sockets. */
38 struct hlist_head table
;
41 static DEFINE_MUTEX(pfkey_mutex
);
44 static const struct xfrm_mark dummy_mark
= {0, 0};
46 /* struct sock must be the first member of struct pfkey_sock */
54 int (*dump
)(struct pfkey_sock
*sk
);
55 void (*done
)(struct pfkey_sock
*sk
);
57 struct xfrm_policy_walk policy
;
58 struct xfrm_state_walk state
;
62 struct mutex dump_lock
;
65 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
66 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
69 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
71 return (struct pfkey_sock
*)sk
;
74 static int pfkey_can_dump(const struct sock
*sk
)
76 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
81 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
85 kfree_skb(pfk
->dump
.skb
);
89 pfk
->dump
.dump
= NULL
;
90 pfk
->dump
.done
= NULL
;
94 static void pfkey_sock_destruct(struct sock
*sk
)
96 struct net
*net
= sock_net(sk
);
97 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
99 pfkey_terminate_dump(pfkey_sk(sk
));
100 skb_queue_purge(&sk
->sk_receive_queue
);
102 if (!sock_flag(sk
, SOCK_DEAD
)) {
103 pr_err("Attempt to release alive pfkey socket: %p\n", sk
);
107 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
108 WARN_ON(refcount_read(&sk
->sk_wmem_alloc
));
110 atomic_dec(&net_pfkey
->socks_nr
);
113 static const struct proto_ops pfkey_ops
;
115 static void pfkey_insert(struct sock
*sk
)
117 struct net
*net
= sock_net(sk
);
118 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
120 mutex_lock(&pfkey_mutex
);
121 sk_add_node_rcu(sk
, &net_pfkey
->table
);
122 mutex_unlock(&pfkey_mutex
);
125 static void pfkey_remove(struct sock
*sk
)
127 mutex_lock(&pfkey_mutex
);
128 sk_del_node_init_rcu(sk
);
129 mutex_unlock(&pfkey_mutex
);
132 static struct proto key_proto
= {
134 .owner
= THIS_MODULE
,
135 .obj_size
= sizeof(struct pfkey_sock
),
138 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
,
141 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
143 struct pfkey_sock
*pfk
;
146 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
148 if (sock
->type
!= SOCK_RAW
)
149 return -ESOCKTNOSUPPORT
;
150 if (protocol
!= PF_KEY_V2
)
151 return -EPROTONOSUPPORT
;
154 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
, kern
);
159 mutex_init(&pfk
->dump_lock
);
161 sock
->ops
= &pfkey_ops
;
162 sock_init_data(sock
, sk
);
164 sk
->sk_family
= PF_KEY
;
165 sk
->sk_destruct
= pfkey_sock_destruct
;
167 atomic_inc(&net_pfkey
->socks_nr
);
176 static int pfkey_release(struct socket
*sock
)
178 struct sock
*sk
= sock
->sk
;
187 skb_queue_purge(&sk
->sk_write_queue
);
195 static int pfkey_broadcast_one(struct sk_buff
*skb
, gfp_t allocation
,
200 if (atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
)
203 skb
= skb_clone(skb
, allocation
);
206 skb_set_owner_r(skb
, sk
);
207 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
208 sk
->sk_data_ready(sk
);
214 /* Send SKB to all pfkey sockets matching selected criteria. */
215 #define BROADCAST_ALL 0
216 #define BROADCAST_ONE 1
217 #define BROADCAST_REGISTERED 2
218 #define BROADCAST_PROMISC_ONLY 4
219 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
220 int broadcast_flags
, struct sock
*one_sk
,
223 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
227 /* XXX Do we need something like netlink_overrun? I think
228 * XXX PF_KEY socket apps will not mind current behavior.
234 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
235 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
238 /* Yes, it means that if you are meant to receive this
239 * pfkey message you receive it twice as promiscuous
243 pfkey_broadcast_one(skb
, GFP_ATOMIC
, sk
);
245 /* the exact target will be processed later */
248 if (broadcast_flags
!= BROADCAST_ALL
) {
249 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
251 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
254 if (broadcast_flags
& BROADCAST_ONE
)
258 err2
= pfkey_broadcast_one(skb
, GFP_ATOMIC
, sk
);
260 /* Error is cleared after successful sending to at least one
262 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
268 err
= pfkey_broadcast_one(skb
, allocation
, one_sk
);
274 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
276 struct sadb_msg
*hdr
;
279 mutex_lock(&pfk
->dump_lock
);
280 if (!pfk
->dump
.dump
) {
285 rc
= pfk
->dump
.dump(pfk
);
286 if (rc
== -ENOBUFS
) {
292 if (!pfkey_can_dump(&pfk
->sk
)) {
297 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
298 hdr
->sadb_msg_seq
= 0;
299 hdr
->sadb_msg_errno
= rc
;
300 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
301 &pfk
->sk
, sock_net(&pfk
->sk
));
302 pfk
->dump
.skb
= NULL
;
305 pfkey_terminate_dump(pfk
);
308 mutex_unlock(&pfk
->dump_lock
);
312 static inline void pfkey_hdr_dup(struct sadb_msg
*new,
313 const struct sadb_msg
*orig
)
318 static int pfkey_error(const struct sadb_msg
*orig
, int err
, struct sock
*sk
)
320 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
321 struct sadb_msg
*hdr
;
326 /* Woe be to the platform trying to support PFKEY yet
327 * having normal errnos outside the 1-255 range, inclusive.
330 if (err
== ERESTARTSYS
||
331 err
== ERESTARTNOHAND
||
332 err
== ERESTARTNOINTR
)
336 BUG_ON(err
<= 0 || err
>= 256);
338 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
339 pfkey_hdr_dup(hdr
, orig
);
340 hdr
->sadb_msg_errno
= (uint8_t) err
;
341 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
344 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, sock_net(sk
));
349 static const u8 sadb_ext_min_len
[] = {
350 [SADB_EXT_RESERVED
] = (u8
) 0,
351 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
352 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
353 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
354 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
355 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
356 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
357 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
358 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
359 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
360 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
361 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
362 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
363 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
364 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
365 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
366 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
367 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
368 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
369 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
370 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
371 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
372 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
373 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
374 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
375 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
376 [SADB_X_EXT_FILTER
] = (u8
) sizeof(struct sadb_x_filter
),
379 /* Verify sadb_address_{len,prefixlen} against sa_family. */
380 static int verify_address_len(const void *p
)
382 const struct sadb_address
*sp
= p
;
383 const struct sockaddr
*addr
= (const struct sockaddr
*)(sp
+ 1);
384 const struct sockaddr_in
*sin
;
385 #if IS_ENABLED(CONFIG_IPV6)
386 const struct sockaddr_in6
*sin6
;
390 if (sp
->sadb_address_len
<
391 DIV_ROUND_UP(sizeof(*sp
) + offsetofend(typeof(*addr
), sa_family
),
395 switch (addr
->sa_family
) {
397 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
398 if (sp
->sadb_address_len
!= len
||
399 sp
->sadb_address_prefixlen
> 32)
402 #if IS_ENABLED(CONFIG_IPV6)
404 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
405 if (sp
->sadb_address_len
!= len
||
406 sp
->sadb_address_prefixlen
> 128)
411 /* It is user using kernel to keep track of security
412 * associations for another protocol, such as
413 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
416 * XXX Actually, association/policy database is not yet
417 * XXX able to cope with arbitrary sockaddr families.
418 * XXX When it can, remove this -EINVAL. -DaveM
426 static inline int sadb_key_len(const struct sadb_key
*key
)
428 int key_bytes
= DIV_ROUND_UP(key
->sadb_key_bits
, 8);
430 return DIV_ROUND_UP(sizeof(struct sadb_key
) + key_bytes
,
434 static int verify_key_len(const void *p
)
436 const struct sadb_key
*key
= p
;
438 if (sadb_key_len(key
) > key
->sadb_key_len
)
444 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx
*sec_ctx
)
446 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
447 sec_ctx
->sadb_x_ctx_len
,
451 static inline int verify_sec_ctx_len(const void *p
)
453 const struct sadb_x_sec_ctx
*sec_ctx
= p
;
454 int len
= sec_ctx
->sadb_x_ctx_len
;
459 len
= pfkey_sec_ctx_len(sec_ctx
);
461 if (sec_ctx
->sadb_x_sec_len
!= len
)
467 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx
*sec_ctx
,
470 struct xfrm_user_sec_ctx
*uctx
= NULL
;
471 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
473 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), gfp
);
478 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
479 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
480 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
481 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
482 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
483 memcpy(uctx
+ 1, sec_ctx
+ 1,
489 static int present_and_same_family(const struct sadb_address
*src
,
490 const struct sadb_address
*dst
)
492 const struct sockaddr
*s_addr
, *d_addr
;
497 s_addr
= (const struct sockaddr
*)(src
+ 1);
498 d_addr
= (const struct sockaddr
*)(dst
+ 1);
499 if (s_addr
->sa_family
!= d_addr
->sa_family
)
501 if (s_addr
->sa_family
!= AF_INET
502 #if IS_ENABLED(CONFIG_IPV6)
503 && s_addr
->sa_family
!= AF_INET6
511 static int parse_exthdrs(struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void **ext_hdrs
)
513 const char *p
= (char *) hdr
;
519 const struct sadb_ext
*ehdr
= (const struct sadb_ext
*) p
;
523 if (len
< sizeof(*ehdr
))
526 ext_len
= ehdr
->sadb_ext_len
;
527 ext_len
*= sizeof(uint64_t);
528 ext_type
= ehdr
->sadb_ext_type
;
529 if (ext_len
< sizeof(uint64_t) ||
531 ext_type
== SADB_EXT_RESERVED
)
534 if (ext_type
<= SADB_EXT_MAX
) {
535 int min
= (int) sadb_ext_min_len
[ext_type
];
538 if (ext_hdrs
[ext_type
-1] != NULL
)
541 case SADB_EXT_ADDRESS_SRC
:
542 case SADB_EXT_ADDRESS_DST
:
543 case SADB_EXT_ADDRESS_PROXY
:
544 case SADB_X_EXT_NAT_T_OA
:
545 if (verify_address_len(p
))
548 case SADB_X_EXT_SEC_CTX
:
549 if (verify_sec_ctx_len(p
))
552 case SADB_EXT_KEY_AUTH
:
553 case SADB_EXT_KEY_ENCRYPT
:
554 if (verify_key_len(p
))
560 ext_hdrs
[ext_type
-1] = (void *) p
;
570 pfkey_satype2proto(uint8_t satype
)
573 case SADB_SATYPE_UNSPEC
:
574 return IPSEC_PROTO_ANY
;
577 case SADB_SATYPE_ESP
:
579 case SADB_X_SATYPE_IPCOMP
:
588 pfkey_proto2satype(uint16_t proto
)
592 return SADB_SATYPE_AH
;
594 return SADB_SATYPE_ESP
;
596 return SADB_X_SATYPE_IPCOMP
;
603 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
604 * say specifically 'just raw sockets' as we encode them as 255.
607 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
609 return proto
== IPSEC_PROTO_ANY
? 0 : proto
;
612 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
614 return proto
? proto
: IPSEC_PROTO_ANY
;
617 static inline int pfkey_sockaddr_len(sa_family_t family
)
621 return sizeof(struct sockaddr_in
);
622 #if IS_ENABLED(CONFIG_IPV6)
624 return sizeof(struct sockaddr_in6
);
631 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
633 switch (sa
->sa_family
) {
636 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
638 #if IS_ENABLED(CONFIG_IPV6)
641 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
642 sizeof(struct in6_addr
));
650 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
652 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
656 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct net
*net
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
658 const struct sadb_sa
*sa
;
659 const struct sadb_address
*addr
;
661 unsigned short family
;
662 xfrm_address_t
*xaddr
;
664 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
668 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
672 /* sadb_address_len should be checked by caller */
673 addr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
677 family
= ((const struct sockaddr
*)(addr
+ 1))->sa_family
;
680 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
682 #if IS_ENABLED(CONFIG_IPV6)
684 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
694 return xfrm_state_lookup(net
, DUMMY_MARK
, xaddr
, sa
->sadb_sa_spi
, proto
, family
);
697 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
700 pfkey_sockaddr_size(sa_family_t family
)
702 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
705 static inline int pfkey_mode_from_xfrm(int mode
)
708 case XFRM_MODE_TRANSPORT
:
709 return IPSEC_MODE_TRANSPORT
;
710 case XFRM_MODE_TUNNEL
:
711 return IPSEC_MODE_TUNNEL
;
713 return IPSEC_MODE_BEET
;
719 static inline int pfkey_mode_to_xfrm(int mode
)
722 case IPSEC_MODE_ANY
: /*XXX*/
723 case IPSEC_MODE_TRANSPORT
:
724 return XFRM_MODE_TRANSPORT
;
725 case IPSEC_MODE_TUNNEL
:
726 return XFRM_MODE_TUNNEL
;
727 case IPSEC_MODE_BEET
:
728 return XFRM_MODE_BEET
;
734 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t
*xaddr
, __be16 port
,
736 unsigned short family
)
741 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
742 sin
->sin_family
= AF_INET
;
743 sin
->sin_port
= port
;
744 sin
->sin_addr
.s_addr
= xaddr
->a4
;
745 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
748 #if IS_ENABLED(CONFIG_IPV6)
751 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
752 sin6
->sin6_family
= AF_INET6
;
753 sin6
->sin6_port
= port
;
754 sin6
->sin6_flowinfo
= 0;
755 sin6
->sin6_addr
= xaddr
->in6
;
756 sin6
->sin6_scope_id
= 0;
764 static struct sk_buff
*__pfkey_xfrm_state2msg(const struct xfrm_state
*x
,
765 int add_keys
, int hsc
)
768 struct sadb_msg
*hdr
;
770 struct sadb_lifetime
*lifetime
;
771 struct sadb_address
*addr
;
772 struct sadb_key
*key
;
773 struct sadb_x_sa2
*sa2
;
774 struct sadb_x_sec_ctx
*sec_ctx
;
775 struct xfrm_sec_ctx
*xfrm_ctx
;
778 int auth_key_size
= 0;
779 int encrypt_key_size
= 0;
781 struct xfrm_encap_tmpl
*natt
= NULL
;
784 /* address family check */
785 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
787 return ERR_PTR(-EINVAL
);
789 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
790 key(AE), (identity(SD),) (sensitivity)> */
791 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
792 sizeof(struct sadb_lifetime
) +
793 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
794 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
795 sizeof(struct sadb_address
)*2 +
797 sizeof(struct sadb_x_sa2
);
799 if ((xfrm_ctx
= x
->security
)) {
800 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
801 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
804 /* identity & sensitivity */
805 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
806 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
809 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
811 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
812 size
+= sizeof(struct sadb_key
) + auth_key_size
;
814 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
816 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
817 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
823 if (natt
&& natt
->encap_type
) {
824 size
+= sizeof(struct sadb_x_nat_t_type
);
825 size
+= sizeof(struct sadb_x_nat_t_port
);
826 size
+= sizeof(struct sadb_x_nat_t_port
);
829 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
831 return ERR_PTR(-ENOBUFS
);
833 /* call should fill header later */
834 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
835 memset(hdr
, 0, size
); /* XXX do we need this ? */
836 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
839 sa
= skb_put(skb
, sizeof(struct sadb_sa
));
840 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
841 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
842 sa
->sadb_sa_spi
= x
->id
.spi
;
843 sa
->sadb_sa_replay
= x
->props
.replay_window
;
844 switch (x
->km
.state
) {
845 case XFRM_STATE_VALID
:
846 sa
->sadb_sa_state
= x
->km
.dying
?
847 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
850 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
853 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
856 sa
->sadb_sa_auth
= 0;
858 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
859 sa
->sadb_sa_auth
= (a
&& a
->pfkey_supported
) ?
860 a
->desc
.sadb_alg_id
: 0;
862 sa
->sadb_sa_encrypt
= 0;
863 BUG_ON(x
->ealg
&& x
->calg
);
865 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
866 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
867 a
->desc
.sadb_alg_id
: 0;
869 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
871 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
872 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
873 a
->desc
.sadb_alg_id
: 0;
876 sa
->sadb_sa_flags
= 0;
877 if (x
->props
.flags
& XFRM_STATE_NOECN
)
878 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
879 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
880 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
881 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
882 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
886 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
887 lifetime
->sadb_lifetime_len
=
888 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
889 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
890 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
891 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
892 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
893 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
897 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
898 lifetime
->sadb_lifetime_len
=
899 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
900 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
901 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
902 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
903 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
904 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
907 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
908 lifetime
->sadb_lifetime_len
=
909 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
910 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
911 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
912 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
913 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
914 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
916 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
917 addr
->sadb_address_len
=
918 (sizeof(struct sadb_address
)+sockaddr_size
)/
920 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
921 /* "if the ports are non-zero, then the sadb_address_proto field,
922 normally zero, MUST be filled in with the transport
923 protocol's number." - RFC2367 */
924 addr
->sadb_address_proto
= 0;
925 addr
->sadb_address_reserved
= 0;
927 addr
->sadb_address_prefixlen
=
928 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
929 (struct sockaddr
*) (addr
+ 1),
931 BUG_ON(!addr
->sadb_address_prefixlen
);
934 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
935 addr
->sadb_address_len
=
936 (sizeof(struct sadb_address
)+sockaddr_size
)/
938 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
939 addr
->sadb_address_proto
= 0;
940 addr
->sadb_address_reserved
= 0;
942 addr
->sadb_address_prefixlen
=
943 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
944 (struct sockaddr
*) (addr
+ 1),
946 BUG_ON(!addr
->sadb_address_prefixlen
);
948 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
,
951 sizeof(struct sadb_address
) + sockaddr_size
);
952 addr
->sadb_address_len
=
953 (sizeof(struct sadb_address
)+sockaddr_size
)/
955 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
956 addr
->sadb_address_proto
=
957 pfkey_proto_from_xfrm(x
->sel
.proto
);
958 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
959 addr
->sadb_address_reserved
= 0;
961 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
962 (struct sockaddr
*) (addr
+ 1),
967 if (add_keys
&& auth_key_size
) {
968 key
= skb_put(skb
, sizeof(struct sadb_key
) + auth_key_size
);
969 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
971 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
972 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
973 key
->sadb_key_reserved
= 0;
974 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
977 if (add_keys
&& encrypt_key_size
) {
978 key
= skb_put(skb
, sizeof(struct sadb_key
) + encrypt_key_size
);
979 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
980 encrypt_key_size
) / sizeof(uint64_t);
981 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
982 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
983 key
->sadb_key_reserved
= 0;
984 memcpy(key
+ 1, x
->ealg
->alg_key
,
985 (x
->ealg
->alg_key_len
+7)/8);
989 sa2
= skb_put(skb
, sizeof(struct sadb_x_sa2
));
990 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
991 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
992 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
994 return ERR_PTR(-EINVAL
);
996 sa2
->sadb_x_sa2_mode
= mode
;
997 sa2
->sadb_x_sa2_reserved1
= 0;
998 sa2
->sadb_x_sa2_reserved2
= 0;
999 sa2
->sadb_x_sa2_sequence
= 0;
1000 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
1002 if (natt
&& natt
->encap_type
) {
1003 struct sadb_x_nat_t_type
*n_type
;
1004 struct sadb_x_nat_t_port
*n_port
;
1007 n_type
= skb_put(skb
, sizeof(*n_type
));
1008 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
1009 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1010 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
1011 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
1012 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
1013 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
1016 n_port
= skb_put(skb
, sizeof(*n_port
));
1017 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1018 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1019 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
1020 n_port
->sadb_x_nat_t_port_reserved
= 0;
1023 n_port
= skb_put(skb
, sizeof(*n_port
));
1024 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1025 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
1026 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
1027 n_port
->sadb_x_nat_t_port_reserved
= 0;
1030 /* security context */
1032 sec_ctx
= skb_put(skb
,
1033 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1034 sec_ctx
->sadb_x_sec_len
=
1035 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1036 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1037 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1038 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1039 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1040 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1048 static inline struct sk_buff
*pfkey_xfrm_state2msg(const struct xfrm_state
*x
)
1050 struct sk_buff
*skb
;
1052 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1057 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(const struct xfrm_state
*x
,
1060 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1063 static struct xfrm_state
* pfkey_msg2xfrm_state(struct net
*net
,
1064 const struct sadb_msg
*hdr
,
1065 void * const *ext_hdrs
)
1067 struct xfrm_state
*x
;
1068 const struct sadb_lifetime
*lifetime
;
1069 const struct sadb_sa
*sa
;
1070 const struct sadb_key
*key
;
1071 const struct sadb_x_sec_ctx
*sec_ctx
;
1076 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
1078 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1079 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1080 return ERR_PTR(-EINVAL
);
1081 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1082 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1083 return ERR_PTR(-EINVAL
);
1084 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1085 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1086 return ERR_PTR(-EINVAL
);
1087 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1088 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1089 return ERR_PTR(-EINVAL
);
1091 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1093 return ERR_PTR(-EINVAL
);
1095 /* default error is no buffer space */
1100 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1101 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1102 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1103 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1104 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1107 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1108 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1110 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1111 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1112 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1113 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1114 return ERR_PTR(-EINVAL
);
1115 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1117 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1118 key
->sadb_key_bits
== 0)
1119 return ERR_PTR(-EINVAL
);
1120 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1122 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1123 key
->sadb_key_bits
== 0)
1124 return ERR_PTR(-EINVAL
);
1126 x
= xfrm_state_alloc(net
);
1128 return ERR_PTR(-ENOBUFS
);
1130 x
->id
.proto
= proto
;
1131 x
->id
.spi
= sa
->sadb_sa_spi
;
1132 x
->props
.replay_window
= min_t(unsigned int, sa
->sadb_sa_replay
,
1133 (sizeof(x
->replay
.bitmap
) * 8));
1134 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1135 x
->props
.flags
|= XFRM_STATE_NOECN
;
1136 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1137 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1138 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1139 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1141 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
- 1];
1142 if (lifetime
!= NULL
) {
1143 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1144 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1145 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1146 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1148 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
- 1];
1149 if (lifetime
!= NULL
) {
1150 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1151 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1152 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1153 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1156 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
1157 if (sec_ctx
!= NULL
) {
1158 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
1163 err
= security_xfrm_state_alloc(x
, uctx
);
1171 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1172 if (sa
->sadb_sa_auth
) {
1174 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1175 if (!a
|| !a
->pfkey_supported
) {
1180 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1181 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1186 strcpy(x
->aalg
->alg_name
, a
->name
);
1187 x
->aalg
->alg_key_len
= 0;
1189 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1190 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1192 x
->aalg
->alg_trunc_len
= a
->uinfo
.auth
.icv_truncbits
;
1193 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1194 /* x->algo.flags = sa->sadb_sa_flags; */
1196 if (sa
->sadb_sa_encrypt
) {
1197 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1198 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1199 if (!a
|| !a
->pfkey_supported
) {
1203 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1208 strcpy(x
->calg
->alg_name
, a
->name
);
1209 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1212 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1213 if (!a
|| !a
->pfkey_supported
) {
1217 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1219 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1220 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1225 strcpy(x
->ealg
->alg_name
, a
->name
);
1226 x
->ealg
->alg_key_len
= 0;
1228 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1229 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1231 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1232 x
->geniv
= a
->uinfo
.encr
.geniv
;
1235 /* x->algo.flags = sa->sadb_sa_flags; */
1237 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1239 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1242 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1243 const struct sadb_x_sa2
*sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1];
1244 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1249 x
->props
.mode
= mode
;
1250 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1253 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1254 const struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1256 /* Nobody uses this, but we try. */
1257 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1258 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1262 x
->sel
.family
= x
->props
.family
;
1264 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1265 const struct sadb_x_nat_t_type
* n_type
;
1266 struct xfrm_encap_tmpl
*natt
;
1268 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1275 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1276 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1278 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1279 const struct sadb_x_nat_t_port
*n_port
=
1280 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1281 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1283 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1284 const struct sadb_x_nat_t_port
*n_port
=
1285 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1286 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1288 memset(&natt
->encap_oa
, 0, sizeof(natt
->encap_oa
));
1291 err
= xfrm_init_state(x
);
1295 x
->km
.seq
= hdr
->sadb_msg_seq
;
1299 x
->km
.state
= XFRM_STATE_DEAD
;
1301 return ERR_PTR(err
);
1304 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1309 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1311 struct net
*net
= sock_net(sk
);
1312 struct sk_buff
*resp_skb
;
1313 struct sadb_x_sa2
*sa2
;
1314 struct sadb_address
*saddr
, *daddr
;
1315 struct sadb_msg
*out_hdr
;
1316 struct sadb_spirange
*range
;
1317 struct xfrm_state
*x
= NULL
;
1320 u32 min_spi
, max_spi
;
1323 unsigned short family
;
1324 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1326 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1327 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1330 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1334 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1335 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1338 reqid
= sa2
->sadb_x_sa2_reqid
;
1344 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1345 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1347 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1350 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1351 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1353 #if IS_ENABLED(CONFIG_IPV6)
1355 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1356 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1361 if (hdr
->sadb_msg_seq
) {
1362 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1363 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, xdaddr
, family
)) {
1370 x
= xfrm_find_acq(net
, &dummy_mark
, mode
, reqid
, 0, proto
, xdaddr
, xsaddr
, 1, family
);
1376 max_spi
= 0x0fffffff;
1378 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1380 min_spi
= range
->sadb_spirange_min
;
1381 max_spi
= range
->sadb_spirange_max
;
1384 err
= verify_spi_info(x
->id
.proto
, min_spi
, max_spi
);
1390 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1391 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1393 if (IS_ERR(resp_skb
)) {
1395 return PTR_ERR(resp_skb
);
1398 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1399 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1400 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1401 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1402 out_hdr
->sadb_msg_errno
= 0;
1403 out_hdr
->sadb_msg_reserved
= 0;
1404 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1405 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1409 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, net
);
1414 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1416 struct net
*net
= sock_net(sk
);
1417 struct xfrm_state
*x
;
1419 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1422 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1425 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1429 spin_lock_bh(&x
->lock
);
1430 if (x
->km
.state
== XFRM_STATE_ACQ
)
1431 x
->km
.state
= XFRM_STATE_ERROR
;
1433 spin_unlock_bh(&x
->lock
);
1438 static inline int event2poltype(int event
)
1441 case XFRM_MSG_DELPOLICY
:
1442 return SADB_X_SPDDELETE
;
1443 case XFRM_MSG_NEWPOLICY
:
1444 return SADB_X_SPDADD
;
1445 case XFRM_MSG_UPDPOLICY
:
1446 return SADB_X_SPDUPDATE
;
1447 case XFRM_MSG_POLEXPIRE
:
1448 // return SADB_X_SPDEXPIRE;
1450 pr_err("pfkey: Unknown policy event %d\n", event
);
1457 static inline int event2keytype(int event
)
1460 case XFRM_MSG_DELSA
:
1462 case XFRM_MSG_NEWSA
:
1464 case XFRM_MSG_UPDSA
:
1466 case XFRM_MSG_EXPIRE
:
1469 pr_err("pfkey: Unknown SA event %d\n", event
);
1477 static int key_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
1479 struct sk_buff
*skb
;
1480 struct sadb_msg
*hdr
;
1482 skb
= pfkey_xfrm_state2msg(x
);
1485 return PTR_ERR(skb
);
1487 hdr
= (struct sadb_msg
*) skb
->data
;
1488 hdr
->sadb_msg_version
= PF_KEY_V2
;
1489 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1490 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1491 hdr
->sadb_msg_errno
= 0;
1492 hdr
->sadb_msg_reserved
= 0;
1493 hdr
->sadb_msg_seq
= c
->seq
;
1494 hdr
->sadb_msg_pid
= c
->portid
;
1496 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xs_net(x
));
1501 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1503 struct net
*net
= sock_net(sk
);
1504 struct xfrm_state
*x
;
1508 x
= pfkey_msg2xfrm_state(net
, hdr
, ext_hdrs
);
1513 if (hdr
->sadb_msg_type
== SADB_ADD
)
1514 err
= xfrm_state_add(x
);
1516 err
= xfrm_state_update(x
);
1518 xfrm_audit_state_add(x
, err
? 0 : 1, true);
1521 x
->km
.state
= XFRM_STATE_DEAD
;
1522 __xfrm_state_put(x
);
1526 if (hdr
->sadb_msg_type
== SADB_ADD
)
1527 c
.event
= XFRM_MSG_NEWSA
;
1529 c
.event
= XFRM_MSG_UPDSA
;
1530 c
.seq
= hdr
->sadb_msg_seq
;
1531 c
.portid
= hdr
->sadb_msg_pid
;
1532 km_state_notify(x
, &c
);
1538 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1540 struct net
*net
= sock_net(sk
);
1541 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 if ((err
= security_xfrm_state_delete(x
)))
1557 if (xfrm_state_kern(x
)) {
1562 err
= xfrm_state_delete(x
);
1567 c
.seq
= hdr
->sadb_msg_seq
;
1568 c
.portid
= hdr
->sadb_msg_pid
;
1569 c
.event
= XFRM_MSG_DELSA
;
1570 km_state_notify(x
, &c
);
1572 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
1578 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1580 struct net
*net
= sock_net(sk
);
1582 struct sk_buff
*out_skb
;
1583 struct sadb_msg
*out_hdr
;
1584 struct xfrm_state
*x
;
1586 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1587 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1588 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1591 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1595 out_skb
= pfkey_xfrm_state2msg(x
);
1596 proto
= x
->id
.proto
;
1598 if (IS_ERR(out_skb
))
1599 return PTR_ERR(out_skb
);
1601 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1602 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1603 out_hdr
->sadb_msg_type
= SADB_GET
;
1604 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1605 out_hdr
->sadb_msg_errno
= 0;
1606 out_hdr
->sadb_msg_reserved
= 0;
1607 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1608 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1609 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, sock_net(sk
));
1614 static struct sk_buff
*compose_sadb_supported(const struct sadb_msg
*orig
,
1617 struct sk_buff
*skb
;
1618 struct sadb_msg
*hdr
;
1619 int len
, auth_len
, enc_len
, i
;
1621 auth_len
= xfrm_count_pfkey_auth_supported();
1623 auth_len
*= sizeof(struct sadb_alg
);
1624 auth_len
+= sizeof(struct sadb_supported
);
1627 enc_len
= xfrm_count_pfkey_enc_supported();
1629 enc_len
*= sizeof(struct sadb_alg
);
1630 enc_len
+= sizeof(struct sadb_supported
);
1633 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1635 skb
= alloc_skb(len
+ 16, allocation
);
1639 hdr
= skb_put(skb
, sizeof(*hdr
));
1640 pfkey_hdr_dup(hdr
, orig
);
1641 hdr
->sadb_msg_errno
= 0;
1642 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1645 struct sadb_supported
*sp
;
1646 struct sadb_alg
*ap
;
1648 sp
= skb_put(skb
, auth_len
);
1649 ap
= (struct sadb_alg
*) (sp
+ 1);
1651 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1652 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1654 for (i
= 0; ; i
++) {
1655 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1658 if (!aalg
->pfkey_supported
)
1660 if (aalg
->available
)
1666 struct sadb_supported
*sp
;
1667 struct sadb_alg
*ap
;
1669 sp
= skb_put(skb
, enc_len
);
1670 ap
= (struct sadb_alg
*) (sp
+ 1);
1672 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1673 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1675 for (i
= 0; ; i
++) {
1676 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1679 if (!ealg
->pfkey_supported
)
1681 if (ealg
->available
)
1690 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1692 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1693 struct sk_buff
*supp_skb
;
1695 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1698 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1699 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1701 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1706 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1708 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1709 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1714 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
,
1719 static int unicast_flush_resp(struct sock
*sk
, const struct sadb_msg
*ihdr
)
1721 struct sk_buff
*skb
;
1722 struct sadb_msg
*hdr
;
1724 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1728 hdr
= skb_put_data(skb
, ihdr
, sizeof(struct sadb_msg
));
1729 hdr
->sadb_msg_errno
= (uint8_t) 0;
1730 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1732 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
,
1736 static int key_notify_sa_flush(const struct km_event
*c
)
1738 struct sk_buff
*skb
;
1739 struct sadb_msg
*hdr
;
1741 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1744 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
1745 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1746 hdr
->sadb_msg_type
= SADB_FLUSH
;
1747 hdr
->sadb_msg_seq
= c
->seq
;
1748 hdr
->sadb_msg_pid
= c
->portid
;
1749 hdr
->sadb_msg_version
= PF_KEY_V2
;
1750 hdr
->sadb_msg_errno
= (uint8_t) 0;
1751 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1752 hdr
->sadb_msg_reserved
= 0;
1754 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
1759 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1761 struct net
*net
= sock_net(sk
);
1766 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1770 err
= xfrm_state_flush(net
, proto
, true, false);
1771 err2
= unicast_flush_resp(sk
, hdr
);
1773 if (err
== -ESRCH
) /* empty table - go quietly */
1775 return err
? err
: err2
;
1778 c
.data
.proto
= proto
;
1779 c
.seq
= hdr
->sadb_msg_seq
;
1780 c
.portid
= hdr
->sadb_msg_pid
;
1781 c
.event
= XFRM_MSG_FLUSHSA
;
1783 km_state_notify(NULL
, &c
);
1788 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1790 struct pfkey_sock
*pfk
= ptr
;
1791 struct sk_buff
*out_skb
;
1792 struct sadb_msg
*out_hdr
;
1794 if (!pfkey_can_dump(&pfk
->sk
))
1797 out_skb
= pfkey_xfrm_state2msg(x
);
1798 if (IS_ERR(out_skb
))
1799 return PTR_ERR(out_skb
);
1801 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1802 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1803 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1804 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1805 out_hdr
->sadb_msg_errno
= 0;
1806 out_hdr
->sadb_msg_reserved
= 0;
1807 out_hdr
->sadb_msg_seq
= count
+ 1;
1808 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
1811 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
1812 &pfk
->sk
, sock_net(&pfk
->sk
));
1813 pfk
->dump
.skb
= out_skb
;
1818 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1820 struct net
*net
= sock_net(&pfk
->sk
);
1821 return xfrm_state_walk(net
, &pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1824 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1826 struct net
*net
= sock_net(&pfk
->sk
);
1828 xfrm_state_walk_done(&pfk
->dump
.u
.state
, net
);
1831 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1834 struct xfrm_address_filter
*filter
= NULL
;
1835 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1837 mutex_lock(&pfk
->dump_lock
);
1838 if (pfk
->dump
.dump
!= NULL
) {
1839 mutex_unlock(&pfk
->dump_lock
);
1843 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1845 mutex_unlock(&pfk
->dump_lock
);
1849 if (ext_hdrs
[SADB_X_EXT_FILTER
- 1]) {
1850 struct sadb_x_filter
*xfilter
= ext_hdrs
[SADB_X_EXT_FILTER
- 1];
1852 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
1853 if (filter
== NULL
) {
1854 mutex_unlock(&pfk
->dump_lock
);
1858 memcpy(&filter
->saddr
, &xfilter
->sadb_x_filter_saddr
,
1859 sizeof(xfrm_address_t
));
1860 memcpy(&filter
->daddr
, &xfilter
->sadb_x_filter_daddr
,
1861 sizeof(xfrm_address_t
));
1862 filter
->family
= xfilter
->sadb_x_filter_family
;
1863 filter
->splen
= xfilter
->sadb_x_filter_splen
;
1864 filter
->dplen
= xfilter
->sadb_x_filter_dplen
;
1867 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1868 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
1869 pfk
->dump
.dump
= pfkey_dump_sa
;
1870 pfk
->dump
.done
= pfkey_dump_sa_done
;
1871 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
, filter
);
1872 mutex_unlock(&pfk
->dump_lock
);
1874 return pfkey_do_dump(pfk
);
1877 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1879 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1880 int satype
= hdr
->sadb_msg_satype
;
1881 bool reset_errno
= false;
1883 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1885 if (satype
!= 0 && satype
!= 1)
1887 pfk
->promisc
= satype
;
1889 if (reset_errno
&& skb_cloned(skb
))
1890 skb
= skb_copy(skb
, GFP_KERNEL
);
1892 skb
= skb_clone(skb
, GFP_KERNEL
);
1894 if (reset_errno
&& skb
) {
1895 struct sadb_msg
*new_hdr
= (struct sadb_msg
*) skb
->data
;
1896 new_hdr
->sadb_msg_errno
= 0;
1899 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ALL
, NULL
, sock_net(sk
));
1903 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1906 u32 reqid
= *(u32
*)ptr
;
1908 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1909 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1915 static u32
gen_reqid(struct net
*net
)
1917 struct xfrm_policy_walk walk
;
1920 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1926 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1927 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1928 rc
= xfrm_policy_walk(net
, &walk
, check_reqid
, (void*)&reqid
);
1929 xfrm_policy_walk_done(&walk
, net
);
1932 } while (reqid
!= start
);
1937 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1939 struct net
*net
= xp_net(xp
);
1940 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1943 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1946 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1948 if (!xfrm_id_proto_valid(rq
->sadb_x_ipsecrequest_proto
))
1951 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
;
1952 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1955 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1957 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1958 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1959 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1961 if (!t
->reqid
&& !(t
->reqid
= gen_reqid(net
)))
1965 /* addresses present only in tunnel mode */
1966 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1969 err
= parse_sockaddr_pair(
1970 (struct sockaddr
*)(rq
+ 1),
1971 rq
->sadb_x_ipsecrequest_len
- sizeof(*rq
),
1972 &t
->saddr
, &t
->id
.daddr
, &t
->encap_family
);
1976 t
->encap_family
= xp
->family
;
1978 /* No way to set this via kame pfkey */
1985 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
1988 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
1989 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
1991 if (pol
->sadb_x_policy_len
* 8 < sizeof(struct sadb_x_policy
))
1994 while (len
>= sizeof(*rq
)) {
1995 if (len
< rq
->sadb_x_ipsecrequest_len
||
1996 rq
->sadb_x_ipsecrequest_len
< sizeof(*rq
))
1999 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
2001 len
-= rq
->sadb_x_ipsecrequest_len
;
2002 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
2007 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy
*xp
)
2009 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
2012 int len
= sizeof(struct sadb_x_sec_ctx
);
2013 len
+= xfrm_ctx
->ctx_len
;
2014 return PFKEY_ALIGN8(len
);
2019 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy
*xp
)
2021 const struct xfrm_tmpl
*t
;
2022 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2026 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2027 t
= xp
->xfrm_vec
+ i
;
2028 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
2031 return sizeof(struct sadb_msg
) +
2032 (sizeof(struct sadb_lifetime
) * 3) +
2033 (sizeof(struct sadb_address
) * 2) +
2034 (sockaddr_size
* 2) +
2035 sizeof(struct sadb_x_policy
) +
2036 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
2038 pfkey_xfrm_policy2sec_ctx_size(xp
);
2041 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(const struct xfrm_policy
*xp
)
2043 struct sk_buff
*skb
;
2046 size
= pfkey_xfrm_policy2msg_size(xp
);
2048 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2050 return ERR_PTR(-ENOBUFS
);
2055 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, const struct xfrm_policy
*xp
, int dir
)
2057 struct sadb_msg
*hdr
;
2058 struct sadb_address
*addr
;
2059 struct sadb_lifetime
*lifetime
;
2060 struct sadb_x_policy
*pol
;
2061 struct sadb_x_sec_ctx
*sec_ctx
;
2062 struct xfrm_sec_ctx
*xfrm_ctx
;
2065 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2066 int socklen
= pfkey_sockaddr_len(xp
->family
);
2068 size
= pfkey_xfrm_policy2msg_size(xp
);
2070 /* call should fill header later */
2071 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
2072 memset(hdr
, 0, size
); /* XXX do we need this ? */
2075 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
2076 addr
->sadb_address_len
=
2077 (sizeof(struct sadb_address
)+sockaddr_size
)/
2079 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2080 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2081 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
2082 addr
->sadb_address_reserved
= 0;
2083 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2085 (struct sockaddr
*) (addr
+ 1),
2090 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
2091 addr
->sadb_address_len
=
2092 (sizeof(struct sadb_address
)+sockaddr_size
)/
2094 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2095 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2096 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2097 addr
->sadb_address_reserved
= 0;
2099 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2100 (struct sockaddr
*) (addr
+ 1),
2104 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2105 lifetime
->sadb_lifetime_len
=
2106 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2107 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2108 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2109 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2110 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2111 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2113 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2114 lifetime
->sadb_lifetime_len
=
2115 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2116 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2117 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2118 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2119 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2120 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2122 lifetime
= skb_put(skb
, sizeof(struct sadb_lifetime
));
2123 lifetime
->sadb_lifetime_len
=
2124 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2125 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2126 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2127 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2128 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2129 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2131 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
2132 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2133 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2134 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2135 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2137 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2139 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2141 pol
->sadb_x_policy_dir
= dir
+1;
2142 pol
->sadb_x_policy_reserved
= 0;
2143 pol
->sadb_x_policy_id
= xp
->index
;
2144 pol
->sadb_x_policy_priority
= xp
->priority
;
2146 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2147 const struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2148 struct sadb_x_ipsecrequest
*rq
;
2152 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2153 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2154 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2155 req_size
+= socklen
* 2;
2159 rq
= skb_put(skb
, req_size
);
2160 pol
->sadb_x_policy_len
+= req_size
/8;
2161 memset(rq
, 0, sizeof(*rq
));
2162 rq
->sadb_x_ipsecrequest_len
= req_size
;
2163 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2164 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2166 rq
->sadb_x_ipsecrequest_mode
= mode
;
2167 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2169 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2171 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2172 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2174 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2175 u8
*sa
= (void *)(rq
+ 1);
2176 pfkey_sockaddr_fill(&t
->saddr
, 0,
2177 (struct sockaddr
*)sa
,
2179 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2180 (struct sockaddr
*) (sa
+ socklen
),
2185 /* security context */
2186 if ((xfrm_ctx
= xp
->security
)) {
2187 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2189 sec_ctx
= skb_put(skb
, ctx_size
);
2190 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2191 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2192 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2193 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2194 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2195 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2199 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2200 hdr
->sadb_msg_reserved
= refcount_read(&xp
->refcnt
);
2205 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2207 struct sk_buff
*out_skb
;
2208 struct sadb_msg
*out_hdr
;
2211 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2212 if (IS_ERR(out_skb
))
2213 return PTR_ERR(out_skb
);
2215 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2221 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2222 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2224 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2225 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2227 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2228 out_hdr
->sadb_msg_errno
= 0;
2229 out_hdr
->sadb_msg_seq
= c
->seq
;
2230 out_hdr
->sadb_msg_pid
= c
->portid
;
2231 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xp_net(xp
));
2236 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2238 struct net
*net
= sock_net(sk
);
2240 struct sadb_lifetime
*lifetime
;
2241 struct sadb_address
*sa
;
2242 struct sadb_x_policy
*pol
;
2243 struct xfrm_policy
*xp
;
2245 struct sadb_x_sec_ctx
*sec_ctx
;
2247 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2248 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2249 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2252 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2253 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2255 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2258 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2262 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2263 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2264 xp
->priority
= pol
->sadb_x_policy_priority
;
2266 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2267 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2268 xp
->selector
.family
= xp
->family
;
2269 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2270 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2271 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2272 if (xp
->selector
.sport
)
2273 xp
->selector
.sport_mask
= htons(0xffff);
2275 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2276 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2277 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2279 /* Amusing, we set this twice. KAME apps appear to set same value
2280 * in both addresses.
2282 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2284 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2285 if (xp
->selector
.dport
)
2286 xp
->selector
.dport_mask
= htons(0xffff);
2288 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2289 if (sec_ctx
!= NULL
) {
2290 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2297 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_KERNEL
);
2304 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2305 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2306 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2307 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2308 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2309 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2310 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2311 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2312 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2314 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2315 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2316 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2317 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2318 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2321 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2322 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2325 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2326 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2328 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
2333 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2334 c
.event
= XFRM_MSG_UPDPOLICY
;
2336 c
.event
= XFRM_MSG_NEWPOLICY
;
2338 c
.seq
= hdr
->sadb_msg_seq
;
2339 c
.portid
= hdr
->sadb_msg_pid
;
2341 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2347 xfrm_policy_destroy(xp
);
2351 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2353 struct net
*net
= sock_net(sk
);
2355 struct sadb_address
*sa
;
2356 struct sadb_x_policy
*pol
;
2357 struct xfrm_policy
*xp
;
2358 struct xfrm_selector sel
;
2360 struct sadb_x_sec_ctx
*sec_ctx
;
2361 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2363 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2364 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2365 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2368 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2369 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2372 memset(&sel
, 0, sizeof(sel
));
2374 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2375 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2376 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2377 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2378 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2380 sel
.sport_mask
= htons(0xffff);
2382 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2383 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2384 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2385 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2386 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2388 sel
.dport_mask
= htons(0xffff);
2390 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2391 if (sec_ctx
!= NULL
) {
2392 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2397 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
, GFP_KERNEL
);
2403 xp
= xfrm_policy_bysel_ctx(net
, DUMMY_MARK
, 0, XFRM_POLICY_TYPE_MAIN
,
2404 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2406 security_xfrm_policy_free(pol_ctx
);
2410 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2415 c
.seq
= hdr
->sadb_msg_seq
;
2416 c
.portid
= hdr
->sadb_msg_pid
;
2418 c
.event
= XFRM_MSG_DELPOLICY
;
2419 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2426 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, const struct sadb_msg
*hdr
, int dir
)
2429 struct sk_buff
*out_skb
;
2430 struct sadb_msg
*out_hdr
;
2433 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2434 if (IS_ERR(out_skb
)) {
2435 err
= PTR_ERR(out_skb
);
2438 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2444 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2445 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2446 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2447 out_hdr
->sadb_msg_satype
= 0;
2448 out_hdr
->sadb_msg_errno
= 0;
2449 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2450 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2451 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, xp_net(xp
));
2458 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2460 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2463 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2464 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2469 if (ext_len
< 2 || ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2472 af
= pfkey_sockaddr_extract(sa
, saddr
);
2476 socklen
= pfkey_sockaddr_len(af
);
2477 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2485 #ifdef CONFIG_NET_KEY_MIGRATE
2486 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2487 struct xfrm_migrate
*m
)
2490 struct sadb_x_ipsecrequest
*rq2
;
2493 if (len
< sizeof(*rq1
) ||
2494 len
< rq1
->sadb_x_ipsecrequest_len
||
2495 rq1
->sadb_x_ipsecrequest_len
< sizeof(*rq1
))
2499 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2500 rq1
->sadb_x_ipsecrequest_len
- sizeof(*rq1
),
2501 &m
->old_saddr
, &m
->old_daddr
,
2506 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2507 len
-= rq1
->sadb_x_ipsecrequest_len
;
2509 if (len
<= sizeof(*rq2
) ||
2510 len
< rq2
->sadb_x_ipsecrequest_len
||
2511 rq2
->sadb_x_ipsecrequest_len
< sizeof(*rq2
))
2515 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2516 rq2
->sadb_x_ipsecrequest_len
- sizeof(*rq2
),
2517 &m
->new_saddr
, &m
->new_daddr
,
2522 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2523 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2524 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2527 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2528 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2531 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2533 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2534 rq2
->sadb_x_ipsecrequest_len
));
2537 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2538 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2540 int i
, len
, ret
, err
= -EINVAL
;
2542 struct sadb_address
*sa
;
2543 struct sadb_x_kmaddress
*kma
;
2544 struct sadb_x_policy
*pol
;
2545 struct sadb_x_ipsecrequest
*rq
;
2546 struct xfrm_selector sel
;
2547 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2548 struct xfrm_kmaddress k
;
2549 struct net
*net
= sock_net(sk
);
2551 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2552 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2553 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2558 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2559 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2561 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2567 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2568 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2569 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2570 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2571 &k
.local
, &k
.remote
, &k
.family
);
2578 dir
= pol
->sadb_x_policy_dir
- 1;
2579 memset(&sel
, 0, sizeof(sel
));
2581 /* set source address info of selector */
2582 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2583 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2584 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2585 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2586 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2588 sel
.sport_mask
= htons(0xffff);
2590 /* set destination address info of selector */
2591 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
2592 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2593 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2594 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2595 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2597 sel
.dport_mask
= htons(0xffff);
2599 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2601 /* extract ipsecrequests */
2603 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2605 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2606 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2611 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2617 if (!i
|| len
> 0) {
2622 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2623 kma
? &k
: NULL
, net
, NULL
);
2629 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2630 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2632 return -ENOPROTOOPT
;
2637 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2639 struct net
*net
= sock_net(sk
);
2641 int err
= 0, delete;
2642 struct sadb_x_policy
*pol
;
2643 struct xfrm_policy
*xp
;
2646 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2649 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2650 if (dir
>= XFRM_POLICY_MAX
)
2653 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2654 xp
= xfrm_policy_byid(net
, DUMMY_MARK
, 0, XFRM_POLICY_TYPE_MAIN
,
2655 dir
, pol
->sadb_x_policy_id
, delete, &err
);
2660 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2664 c
.seq
= hdr
->sadb_msg_seq
;
2665 c
.portid
= hdr
->sadb_msg_pid
;
2667 c
.event
= XFRM_MSG_DELPOLICY
;
2668 km_policy_notify(xp
, dir
, &c
);
2670 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2678 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2680 struct pfkey_sock
*pfk
= ptr
;
2681 struct sk_buff
*out_skb
;
2682 struct sadb_msg
*out_hdr
;
2685 if (!pfkey_can_dump(&pfk
->sk
))
2688 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2689 if (IS_ERR(out_skb
))
2690 return PTR_ERR(out_skb
);
2692 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2698 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2699 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2700 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2701 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2702 out_hdr
->sadb_msg_errno
= 0;
2703 out_hdr
->sadb_msg_seq
= count
+ 1;
2704 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
2707 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
2708 &pfk
->sk
, sock_net(&pfk
->sk
));
2709 pfk
->dump
.skb
= out_skb
;
2714 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2716 struct net
*net
= sock_net(&pfk
->sk
);
2717 return xfrm_policy_walk(net
, &pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2720 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2722 struct net
*net
= sock_net((struct sock
*)pfk
);
2724 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
, net
);
2727 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2729 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2731 mutex_lock(&pfk
->dump_lock
);
2732 if (pfk
->dump
.dump
!= NULL
) {
2733 mutex_unlock(&pfk
->dump_lock
);
2737 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2738 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
2739 pfk
->dump
.dump
= pfkey_dump_sp
;
2740 pfk
->dump
.done
= pfkey_dump_sp_done
;
2741 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2742 mutex_unlock(&pfk
->dump_lock
);
2744 return pfkey_do_dump(pfk
);
2747 static int key_notify_policy_flush(const struct km_event
*c
)
2749 struct sk_buff
*skb_out
;
2750 struct sadb_msg
*hdr
;
2752 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2755 hdr
= skb_put(skb_out
, sizeof(struct sadb_msg
));
2756 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2757 hdr
->sadb_msg_seq
= c
->seq
;
2758 hdr
->sadb_msg_pid
= c
->portid
;
2759 hdr
->sadb_msg_version
= PF_KEY_V2
;
2760 hdr
->sadb_msg_errno
= (uint8_t) 0;
2761 hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2762 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2763 hdr
->sadb_msg_reserved
= 0;
2764 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
2769 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2771 struct net
*net
= sock_net(sk
);
2775 err
= xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, true);
2776 err2
= unicast_flush_resp(sk
, hdr
);
2778 if (err
== -ESRCH
) /* empty table - old silent behavior */
2783 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2784 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2785 c
.portid
= hdr
->sadb_msg_pid
;
2786 c
.seq
= hdr
->sadb_msg_seq
;
2788 km_policy_notify(NULL
, 0, &c
);
2793 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2794 const struct sadb_msg
*hdr
, void * const *ext_hdrs
);
2795 static const pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2796 [SADB_RESERVED
] = pfkey_reserved
,
2797 [SADB_GETSPI
] = pfkey_getspi
,
2798 [SADB_UPDATE
] = pfkey_add
,
2799 [SADB_ADD
] = pfkey_add
,
2800 [SADB_DELETE
] = pfkey_delete
,
2801 [SADB_GET
] = pfkey_get
,
2802 [SADB_ACQUIRE
] = pfkey_acquire
,
2803 [SADB_REGISTER
] = pfkey_register
,
2804 [SADB_EXPIRE
] = NULL
,
2805 [SADB_FLUSH
] = pfkey_flush
,
2806 [SADB_DUMP
] = pfkey_dump
,
2807 [SADB_X_PROMISC
] = pfkey_promisc
,
2808 [SADB_X_PCHANGE
] = NULL
,
2809 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2810 [SADB_X_SPDADD
] = pfkey_spdadd
,
2811 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2812 [SADB_X_SPDGET
] = pfkey_spdget
,
2813 [SADB_X_SPDACQUIRE
] = NULL
,
2814 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2815 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2816 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2817 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2818 [SADB_X_MIGRATE
] = pfkey_migrate
,
2821 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
)
2823 void *ext_hdrs
[SADB_EXT_MAX
];
2826 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2827 BROADCAST_PROMISC_ONLY
, NULL
, sock_net(sk
));
2829 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2830 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2833 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2834 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2839 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2841 struct sadb_msg
*hdr
= NULL
;
2843 if (skb
->len
< sizeof(*hdr
)) {
2846 hdr
= (struct sadb_msg
*) skb
->data
;
2847 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2848 hdr
->sadb_msg_reserved
!= 0 ||
2849 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2850 hdr
->sadb_msg_type
> SADB_MAX
)) {
2853 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2854 sizeof(uint64_t)) ||
2855 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2856 sizeof(uint64_t))) {
2866 static inline int aalg_tmpl_set(const struct xfrm_tmpl
*t
,
2867 const struct xfrm_algo_desc
*d
)
2869 unsigned int id
= d
->desc
.sadb_alg_id
;
2871 if (id
>= sizeof(t
->aalgos
) * 8)
2874 return (t
->aalgos
>> id
) & 1;
2877 static inline int ealg_tmpl_set(const struct xfrm_tmpl
*t
,
2878 const struct xfrm_algo_desc
*d
)
2880 unsigned int id
= d
->desc
.sadb_alg_id
;
2882 if (id
>= sizeof(t
->ealgos
) * 8)
2885 return (t
->ealgos
>> id
) & 1;
2888 static int count_ah_combs(const struct xfrm_tmpl
*t
)
2892 for (i
= 0; ; i
++) {
2893 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2896 if (!aalg
->pfkey_supported
)
2898 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2899 sz
+= sizeof(struct sadb_comb
);
2901 return sz
+ sizeof(struct sadb_prop
);
2904 static int count_esp_combs(const struct xfrm_tmpl
*t
)
2908 for (i
= 0; ; i
++) {
2909 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2913 if (!ealg
->pfkey_supported
)
2916 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2919 for (k
= 1; ; k
++) {
2920 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2924 if (!aalg
->pfkey_supported
)
2927 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2928 sz
+= sizeof(struct sadb_comb
);
2931 return sz
+ sizeof(struct sadb_prop
);
2934 static void dump_ah_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2936 struct sadb_prop
*p
;
2939 p
= skb_put(skb
, sizeof(struct sadb_prop
));
2940 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2941 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2942 p
->sadb_prop_replay
= 32;
2943 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2945 for (i
= 0; ; i
++) {
2946 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2950 if (!aalg
->pfkey_supported
)
2953 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2954 struct sadb_comb
*c
;
2955 c
= skb_put_zero(skb
, sizeof(struct sadb_comb
));
2956 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2957 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2958 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2959 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2960 c
->sadb_comb_hard_addtime
= 24*60*60;
2961 c
->sadb_comb_soft_addtime
= 20*60*60;
2962 c
->sadb_comb_hard_usetime
= 8*60*60;
2963 c
->sadb_comb_soft_usetime
= 7*60*60;
2968 static void dump_esp_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2970 struct sadb_prop
*p
;
2973 p
= skb_put(skb
, sizeof(struct sadb_prop
));
2974 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2975 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2976 p
->sadb_prop_replay
= 32;
2977 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2980 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2984 if (!ealg
->pfkey_supported
)
2987 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2990 for (k
= 1; ; k
++) {
2991 struct sadb_comb
*c
;
2992 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2995 if (!aalg
->pfkey_supported
)
2997 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
2999 c
= skb_put(skb
, sizeof(struct sadb_comb
));
3000 memset(c
, 0, sizeof(*c
));
3001 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
3002 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
3003 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
3004 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
3005 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
3006 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
3007 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
3008 c
->sadb_comb_hard_addtime
= 24*60*60;
3009 c
->sadb_comb_soft_addtime
= 20*60*60;
3010 c
->sadb_comb_hard_usetime
= 8*60*60;
3011 c
->sadb_comb_soft_usetime
= 7*60*60;
3016 static int key_notify_policy_expire(struct xfrm_policy
*xp
, const struct km_event
*c
)
3021 static int key_notify_sa_expire(struct xfrm_state
*x
, const struct km_event
*c
)
3023 struct sk_buff
*out_skb
;
3024 struct sadb_msg
*out_hdr
;
3028 hard
= c
->data
.hard
;
3034 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
3035 if (IS_ERR(out_skb
))
3036 return PTR_ERR(out_skb
);
3038 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
3039 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
3040 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
3041 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3042 out_hdr
->sadb_msg_errno
= 0;
3043 out_hdr
->sadb_msg_reserved
= 0;
3044 out_hdr
->sadb_msg_seq
= 0;
3045 out_hdr
->sadb_msg_pid
= 0;
3047 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3052 static int pfkey_send_notify(struct xfrm_state
*x
, const struct km_event
*c
)
3054 struct net
*net
= x
? xs_net(x
) : c
->net
;
3055 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3057 if (atomic_read(&net_pfkey
->socks_nr
) == 0)
3061 case XFRM_MSG_EXPIRE
:
3062 return key_notify_sa_expire(x
, c
);
3063 case XFRM_MSG_DELSA
:
3064 case XFRM_MSG_NEWSA
:
3065 case XFRM_MSG_UPDSA
:
3066 return key_notify_sa(x
, c
);
3067 case XFRM_MSG_FLUSHSA
:
3068 return key_notify_sa_flush(c
);
3069 case XFRM_MSG_NEWAE
: /* not yet supported */
3072 pr_err("pfkey: Unknown SA event %d\n", c
->event
);
3079 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3081 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
3085 case XFRM_MSG_POLEXPIRE
:
3086 return key_notify_policy_expire(xp
, c
);
3087 case XFRM_MSG_DELPOLICY
:
3088 case XFRM_MSG_NEWPOLICY
:
3089 case XFRM_MSG_UPDPOLICY
:
3090 return key_notify_policy(xp
, dir
, c
);
3091 case XFRM_MSG_FLUSHPOLICY
:
3092 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
3094 return key_notify_policy_flush(c
);
3096 pr_err("pfkey: Unknown policy event %d\n", c
->event
);
3103 static u32
get_acqseq(void)
3106 static atomic_t acqseq
;
3109 res
= atomic_inc_return(&acqseq
);
3114 static bool pfkey_is_alive(const struct km_event
*c
)
3116 struct netns_pfkey
*net_pfkey
= net_generic(c
->net
, pfkey_net_id
);
3118 bool is_alive
= false;
3121 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
3122 if (pfkey_sk(sk
)->registered
) {
3132 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
)
3134 struct sk_buff
*skb
;
3135 struct sadb_msg
*hdr
;
3136 struct sadb_address
*addr
;
3137 struct sadb_x_policy
*pol
;
3140 struct sadb_x_sec_ctx
*sec_ctx
;
3141 struct xfrm_sec_ctx
*xfrm_ctx
;
3144 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3148 size
= sizeof(struct sadb_msg
) +
3149 (sizeof(struct sadb_address
) * 2) +
3150 (sockaddr_size
* 2) +
3151 sizeof(struct sadb_x_policy
);
3153 if (x
->id
.proto
== IPPROTO_AH
)
3154 size
+= count_ah_combs(t
);
3155 else if (x
->id
.proto
== IPPROTO_ESP
)
3156 size
+= count_esp_combs(t
);
3158 if ((xfrm_ctx
= x
->security
)) {
3159 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3160 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3163 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3167 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3168 hdr
->sadb_msg_version
= PF_KEY_V2
;
3169 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3170 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3171 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3172 hdr
->sadb_msg_errno
= 0;
3173 hdr
->sadb_msg_reserved
= 0;
3174 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3175 hdr
->sadb_msg_pid
= 0;
3178 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3179 addr
->sadb_address_len
=
3180 (sizeof(struct sadb_address
)+sockaddr_size
)/
3182 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3183 addr
->sadb_address_proto
= 0;
3184 addr
->sadb_address_reserved
= 0;
3185 addr
->sadb_address_prefixlen
=
3186 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3187 (struct sockaddr
*) (addr
+ 1),
3189 if (!addr
->sadb_address_prefixlen
)
3193 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3194 addr
->sadb_address_len
=
3195 (sizeof(struct sadb_address
)+sockaddr_size
)/
3197 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3198 addr
->sadb_address_proto
= 0;
3199 addr
->sadb_address_reserved
= 0;
3200 addr
->sadb_address_prefixlen
=
3201 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3202 (struct sockaddr
*) (addr
+ 1),
3204 if (!addr
->sadb_address_prefixlen
)
3207 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
3208 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3209 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3210 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3211 pol
->sadb_x_policy_dir
= XFRM_POLICY_OUT
+ 1;
3212 pol
->sadb_x_policy_reserved
= 0;
3213 pol
->sadb_x_policy_id
= xp
->index
;
3214 pol
->sadb_x_policy_priority
= xp
->priority
;
3216 /* Set sadb_comb's. */
3217 if (x
->id
.proto
== IPPROTO_AH
)
3218 dump_ah_combs(skb
, t
);
3219 else if (x
->id
.proto
== IPPROTO_ESP
)
3220 dump_esp_combs(skb
, t
);
3222 /* security context */
3224 sec_ctx
= skb_put(skb
,
3225 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3226 sec_ctx
->sadb_x_sec_len
=
3227 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3228 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3229 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3230 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3231 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3232 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3236 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3240 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3241 u8
*data
, int len
, int *dir
)
3243 struct net
*net
= sock_net(sk
);
3244 struct xfrm_policy
*xp
;
3245 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3246 struct sadb_x_sec_ctx
*sec_ctx
;
3248 switch (sk
->sk_family
) {
3250 if (opt
!= IP_IPSEC_POLICY
) {
3255 #if IS_ENABLED(CONFIG_IPV6)
3257 if (opt
!= IPV6_IPSEC_POLICY
) {
3270 if (len
< sizeof(struct sadb_x_policy
) ||
3271 pol
->sadb_x_policy_len
*8 > len
||
3272 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3273 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3276 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
3282 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3283 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3285 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3286 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3287 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3288 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3289 xp
->family
= sk
->sk_family
;
3292 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3293 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3296 /* security context too */
3297 if (len
>= (pol
->sadb_x_policy_len
*8 +
3298 sizeof(struct sadb_x_sec_ctx
))) {
3299 char *p
= (char *)pol
;
3300 struct xfrm_user_sec_ctx
*uctx
;
3302 p
+= pol
->sadb_x_policy_len
*8;
3303 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3304 if (len
< pol
->sadb_x_policy_len
*8 +
3305 sec_ctx
->sadb_x_sec_len
*8) {
3309 if ((*dir
= verify_sec_ctx_len(p
)))
3311 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_ATOMIC
);
3312 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_ATOMIC
);
3319 *dir
= pol
->sadb_x_policy_dir
-1;
3324 xfrm_policy_destroy(xp
);
3328 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3330 struct sk_buff
*skb
;
3331 struct sadb_msg
*hdr
;
3333 struct sadb_address
*addr
;
3334 struct sadb_x_nat_t_port
*n_port
;
3337 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3338 struct xfrm_encap_tmpl
*natt
= NULL
;
3340 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3352 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3354 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3355 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3358 size
= sizeof(struct sadb_msg
) +
3359 sizeof(struct sadb_sa
) +
3360 (sizeof(struct sadb_address
) * 2) +
3361 (sockaddr_size
* 2) +
3362 (sizeof(struct sadb_x_nat_t_port
) * 2);
3364 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3368 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3369 hdr
->sadb_msg_version
= PF_KEY_V2
;
3370 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3371 hdr
->sadb_msg_satype
= satype
;
3372 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3373 hdr
->sadb_msg_errno
= 0;
3374 hdr
->sadb_msg_reserved
= 0;
3375 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3376 hdr
->sadb_msg_pid
= 0;
3379 sa
= skb_put(skb
, sizeof(struct sadb_sa
));
3380 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3381 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3382 sa
->sadb_sa_spi
= x
->id
.spi
;
3383 sa
->sadb_sa_replay
= 0;
3384 sa
->sadb_sa_state
= 0;
3385 sa
->sadb_sa_auth
= 0;
3386 sa
->sadb_sa_encrypt
= 0;
3387 sa
->sadb_sa_flags
= 0;
3389 /* ADDRESS_SRC (old addr) */
3390 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3391 addr
->sadb_address_len
=
3392 (sizeof(struct sadb_address
)+sockaddr_size
)/
3394 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3395 addr
->sadb_address_proto
= 0;
3396 addr
->sadb_address_reserved
= 0;
3397 addr
->sadb_address_prefixlen
=
3398 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3399 (struct sockaddr
*) (addr
+ 1),
3401 if (!addr
->sadb_address_prefixlen
)
3404 /* NAT_T_SPORT (old port) */
3405 n_port
= skb_put(skb
, sizeof(*n_port
));
3406 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3407 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3408 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3409 n_port
->sadb_x_nat_t_port_reserved
= 0;
3411 /* ADDRESS_DST (new addr) */
3412 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sockaddr_size
);
3413 addr
->sadb_address_len
=
3414 (sizeof(struct sadb_address
)+sockaddr_size
)/
3416 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3417 addr
->sadb_address_proto
= 0;
3418 addr
->sadb_address_reserved
= 0;
3419 addr
->sadb_address_prefixlen
=
3420 pfkey_sockaddr_fill(ipaddr
, 0,
3421 (struct sockaddr
*) (addr
+ 1),
3423 if (!addr
->sadb_address_prefixlen
)
3426 /* NAT_T_DPORT (new port) */
3427 n_port
= skb_put(skb
, sizeof(*n_port
));
3428 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3429 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3430 n_port
->sadb_x_nat_t_port_port
= sport
;
3431 n_port
->sadb_x_nat_t_port_reserved
= 0;
3433 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3437 #ifdef CONFIG_NET_KEY_MIGRATE
3438 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3439 const struct xfrm_selector
*sel
)
3441 struct sadb_address
*addr
;
3442 addr
= skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3443 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3444 addr
->sadb_address_exttype
= type
;
3445 addr
->sadb_address_proto
= sel
->proto
;
3446 addr
->sadb_address_reserved
= 0;
3449 case SADB_EXT_ADDRESS_SRC
:
3450 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3451 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3452 (struct sockaddr
*)(addr
+ 1),
3455 case SADB_EXT_ADDRESS_DST
:
3456 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3457 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3458 (struct sockaddr
*)(addr
+ 1),
3469 static int set_sadb_kmaddress(struct sk_buff
*skb
, const struct xfrm_kmaddress
*k
)
3471 struct sadb_x_kmaddress
*kma
;
3473 int family
= k
->family
;
3474 int socklen
= pfkey_sockaddr_len(family
);
3477 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3478 pfkey_sockaddr_pair_size(family
));
3480 kma
= skb_put_zero(skb
, size_req
);
3481 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3482 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3483 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3485 sa
= (u8
*)(kma
+ 1);
3486 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3487 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3493 static int set_ipsecrequest(struct sk_buff
*skb
,
3494 uint8_t proto
, uint8_t mode
, int level
,
3495 uint32_t reqid
, uint8_t family
,
3496 const xfrm_address_t
*src
, const xfrm_address_t
*dst
)
3498 struct sadb_x_ipsecrequest
*rq
;
3500 int socklen
= pfkey_sockaddr_len(family
);
3503 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3504 pfkey_sockaddr_pair_size(family
);
3506 rq
= skb_put_zero(skb
, size_req
);
3507 rq
->sadb_x_ipsecrequest_len
= size_req
;
3508 rq
->sadb_x_ipsecrequest_proto
= proto
;
3509 rq
->sadb_x_ipsecrequest_mode
= mode
;
3510 rq
->sadb_x_ipsecrequest_level
= level
;
3511 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3513 sa
= (u8
*) (rq
+ 1);
3514 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3515 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3522 #ifdef CONFIG_NET_KEY_MIGRATE
3523 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3524 const struct xfrm_migrate
*m
, int num_bundles
,
3525 const struct xfrm_kmaddress
*k
,
3526 const struct xfrm_encap_tmpl
*encap
)
3532 struct sk_buff
*skb
;
3533 struct sadb_msg
*hdr
;
3534 struct sadb_x_policy
*pol
;
3535 const struct xfrm_migrate
*mp
;
3537 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3540 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3544 /* addresses for KM */
3545 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3546 pfkey_sockaddr_pair_size(k
->family
));
3550 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3553 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3556 size_pol
+= sizeof(struct sadb_x_policy
);
3559 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3560 /* old locator pair */
3561 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3562 pfkey_sockaddr_pair_size(mp
->old_family
);
3563 /* new locator pair */
3564 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3565 pfkey_sockaddr_pair_size(mp
->new_family
);
3568 size
+= sizeof(struct sadb_msg
) + size_pol
;
3571 skb
= alloc_skb(size
, GFP_ATOMIC
);
3575 hdr
= skb_put(skb
, sizeof(struct sadb_msg
));
3576 hdr
->sadb_msg_version
= PF_KEY_V2
;
3577 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3578 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3579 hdr
->sadb_msg_len
= size
/ 8;
3580 hdr
->sadb_msg_errno
= 0;
3581 hdr
->sadb_msg_reserved
= 0;
3582 hdr
->sadb_msg_seq
= 0;
3583 hdr
->sadb_msg_pid
= 0;
3585 /* Addresses to be used by KM for negotiation, if ext is available */
3586 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3590 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3593 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3595 /* policy information */
3596 pol
= skb_put(skb
, sizeof(struct sadb_x_policy
));
3597 pol
->sadb_x_policy_len
= size_pol
/ 8;
3598 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3599 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3600 pol
->sadb_x_policy_dir
= dir
+ 1;
3601 pol
->sadb_x_policy_reserved
= 0;
3602 pol
->sadb_x_policy_id
= 0;
3603 pol
->sadb_x_policy_priority
= 0;
3605 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3606 /* old ipsecrequest */
3607 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3610 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3611 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3612 mp
->reqid
, mp
->old_family
,
3613 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3616 /* new ipsecrequest */
3617 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3618 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3619 mp
->reqid
, mp
->new_family
,
3620 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3624 /* broadcast migrate message to sockets */
3625 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, &init_net
);
3634 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3635 const struct xfrm_migrate
*m
, int num_bundles
,
3636 const struct xfrm_kmaddress
*k
,
3637 const struct xfrm_encap_tmpl
*encap
)
3639 return -ENOPROTOOPT
;
3643 static int pfkey_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3645 struct sock
*sk
= sock
->sk
;
3646 struct sk_buff
*skb
= NULL
;
3647 struct sadb_msg
*hdr
= NULL
;
3649 struct net
*net
= sock_net(sk
);
3652 if (msg
->msg_flags
& MSG_OOB
)
3656 if ((unsigned int)len
> sk
->sk_sndbuf
- 32)
3660 skb
= alloc_skb(len
, GFP_KERNEL
);
3665 if (memcpy_from_msg(skb_put(skb
,len
), msg
, len
))
3668 hdr
= pfkey_get_base_msg(skb
, &err
);
3672 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
3673 err
= pfkey_process(sk
, skb
, hdr
);
3674 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
3677 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3684 static int pfkey_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3687 struct sock
*sk
= sock
->sk
;
3688 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3689 struct sk_buff
*skb
;
3693 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3696 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3702 msg
->msg_flags
|= MSG_TRUNC
;
3706 skb_reset_transport_header(skb
);
3707 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
3711 sock_recv_ts_and_drops(msg
, sk
, skb
);
3713 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3715 if (pfk
->dump
.dump
!= NULL
&&
3716 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3720 skb_free_datagram(sk
, skb
);
3725 static const struct proto_ops pfkey_ops
= {
3727 .owner
= THIS_MODULE
,
3728 /* Operations that make no sense on pfkey sockets. */
3729 .bind
= sock_no_bind
,
3730 .connect
= sock_no_connect
,
3731 .socketpair
= sock_no_socketpair
,
3732 .accept
= sock_no_accept
,
3733 .getname
= sock_no_getname
,
3734 .ioctl
= sock_no_ioctl
,
3735 .listen
= sock_no_listen
,
3736 .shutdown
= sock_no_shutdown
,
3737 .setsockopt
= sock_no_setsockopt
,
3738 .getsockopt
= sock_no_getsockopt
,
3739 .mmap
= sock_no_mmap
,
3740 .sendpage
= sock_no_sendpage
,
3742 /* Now the operations that really occur. */
3743 .release
= pfkey_release
,
3744 .poll
= datagram_poll
,
3745 .sendmsg
= pfkey_sendmsg
,
3746 .recvmsg
= pfkey_recvmsg
,
3749 static const struct net_proto_family pfkey_family_ops
= {
3751 .create
= pfkey_create
,
3752 .owner
= THIS_MODULE
,
3755 #ifdef CONFIG_PROC_FS
3756 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3758 struct sock
*s
= sk_entry(v
);
3760 if (v
== SEQ_START_TOKEN
)
3761 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3763 seq_printf(f
, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3765 refcount_read(&s
->sk_refcnt
),
3766 sk_rmem_alloc_get(s
),
3767 sk_wmem_alloc_get(s
),
3768 from_kuid_munged(seq_user_ns(f
), sock_i_uid(s
)),
3774 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3777 struct net
*net
= seq_file_net(f
);
3778 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3781 return seq_hlist_start_head_rcu(&net_pfkey
->table
, *ppos
);
3784 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3786 struct net
*net
= seq_file_net(f
);
3787 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3789 return seq_hlist_next_rcu(v
, &net_pfkey
->table
, ppos
);
3792 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3798 static const struct seq_operations pfkey_seq_ops
= {
3799 .start
= pfkey_seq_start
,
3800 .next
= pfkey_seq_next
,
3801 .stop
= pfkey_seq_stop
,
3802 .show
= pfkey_seq_show
,
3805 static int __net_init
pfkey_init_proc(struct net
*net
)
3807 struct proc_dir_entry
*e
;
3809 e
= proc_create_net("pfkey", 0, net
->proc_net
, &pfkey_seq_ops
,
3810 sizeof(struct seq_net_private
));
3817 static void __net_exit
pfkey_exit_proc(struct net
*net
)
3819 remove_proc_entry("pfkey", net
->proc_net
);
3822 static inline int pfkey_init_proc(struct net
*net
)
3827 static inline void pfkey_exit_proc(struct net
*net
)
3832 static struct xfrm_mgr pfkeyv2_mgr
=
3834 .notify
= pfkey_send_notify
,
3835 .acquire
= pfkey_send_acquire
,
3836 .compile_policy
= pfkey_compile_policy
,
3837 .new_mapping
= pfkey_send_new_mapping
,
3838 .notify_policy
= pfkey_send_policy_notify
,
3839 .migrate
= pfkey_send_migrate
,
3840 .is_alive
= pfkey_is_alive
,
3843 static int __net_init
pfkey_net_init(struct net
*net
)
3845 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3848 INIT_HLIST_HEAD(&net_pfkey
->table
);
3849 atomic_set(&net_pfkey
->socks_nr
, 0);
3851 rv
= pfkey_init_proc(net
);
3856 static void __net_exit
pfkey_net_exit(struct net
*net
)
3858 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3860 pfkey_exit_proc(net
);
3861 WARN_ON(!hlist_empty(&net_pfkey
->table
));
3864 static struct pernet_operations pfkey_net_ops
= {
3865 .init
= pfkey_net_init
,
3866 .exit
= pfkey_net_exit
,
3867 .id
= &pfkey_net_id
,
3868 .size
= sizeof(struct netns_pfkey
),
3871 static void __exit
ipsec_pfkey_exit(void)
3873 xfrm_unregister_km(&pfkeyv2_mgr
);
3874 sock_unregister(PF_KEY
);
3875 unregister_pernet_subsys(&pfkey_net_ops
);
3876 proto_unregister(&key_proto
);
3879 static int __init
ipsec_pfkey_init(void)
3881 int err
= proto_register(&key_proto
, 0);
3886 err
= register_pernet_subsys(&pfkey_net_ops
);
3888 goto out_unregister_key_proto
;
3889 err
= sock_register(&pfkey_family_ops
);
3891 goto out_unregister_pernet
;
3892 err
= xfrm_register_km(&pfkeyv2_mgr
);
3894 goto out_sock_unregister
;
3898 out_sock_unregister
:
3899 sock_unregister(PF_KEY
);
3900 out_unregister_pernet
:
3901 unregister_pernet_subsys(&pfkey_net_ops
);
3902 out_unregister_key_proto
:
3903 proto_unregister(&key_proto
);
3907 module_init(ipsec_pfkey_init
);
3908 module_exit(ipsec_pfkey_exit
);
3909 MODULE_LICENSE("GPL");
3910 MODULE_ALIAS_NETPROTO(PF_KEY
);