2 * net/key/af_key.c An implementation of PF_KEYv2 sockets.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Maxim Giryaev <gem@asplinux.ru>
10 * David S. Miller <davem@redhat.com>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
13 * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
14 * Derek Atkins <derek@ihtfp.com>
17 #include <linux/capability.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/socket.h>
21 #include <linux/pfkeyv2.h>
22 #include <linux/ipsec.h>
23 #include <linux/skbuff.h>
24 #include <linux/rtnetlink.h>
26 #include <linux/in6.h>
27 #include <linux/proc_fs.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
39 static int pfkey_net_id __read_mostly
;
41 /* List of all pfkey sockets. */
42 struct hlist_head table
;
45 static DEFINE_MUTEX(pfkey_mutex
);
48 static const struct xfrm_mark dummy_mark
= {0, 0};
50 /* struct sock must be the first member of struct pfkey_sock */
58 int (*dump
)(struct pfkey_sock
*sk
);
59 void (*done
)(struct pfkey_sock
*sk
);
61 struct xfrm_policy_walk policy
;
62 struct xfrm_state_walk state
;
66 struct mutex dump_lock
;
69 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
70 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
73 static inline struct pfkey_sock
*pfkey_sk(struct sock
*sk
)
75 return (struct pfkey_sock
*)sk
;
78 static int pfkey_can_dump(const struct sock
*sk
)
80 if (3 * atomic_read(&sk
->sk_rmem_alloc
) <= 2 * sk
->sk_rcvbuf
)
85 static void pfkey_terminate_dump(struct pfkey_sock
*pfk
)
89 kfree_skb(pfk
->dump
.skb
);
93 pfk
->dump
.dump
= NULL
;
94 pfk
->dump
.done
= NULL
;
98 static void pfkey_sock_destruct(struct sock
*sk
)
100 struct net
*net
= sock_net(sk
);
101 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
103 pfkey_terminate_dump(pfkey_sk(sk
));
104 skb_queue_purge(&sk
->sk_receive_queue
);
106 if (!sock_flag(sk
, SOCK_DEAD
)) {
107 pr_err("Attempt to release alive pfkey socket: %p\n", sk
);
111 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
112 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
114 atomic_dec(&net_pfkey
->socks_nr
);
117 static const struct proto_ops pfkey_ops
;
119 static void pfkey_insert(struct sock
*sk
)
121 struct net
*net
= sock_net(sk
);
122 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
124 mutex_lock(&pfkey_mutex
);
125 sk_add_node_rcu(sk
, &net_pfkey
->table
);
126 mutex_unlock(&pfkey_mutex
);
129 static void pfkey_remove(struct sock
*sk
)
131 mutex_lock(&pfkey_mutex
);
132 sk_del_node_init_rcu(sk
);
133 mutex_unlock(&pfkey_mutex
);
136 static struct proto key_proto
= {
138 .owner
= THIS_MODULE
,
139 .obj_size
= sizeof(struct pfkey_sock
),
142 static int pfkey_create(struct net
*net
, struct socket
*sock
, int protocol
,
145 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
147 struct pfkey_sock
*pfk
;
150 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
152 if (sock
->type
!= SOCK_RAW
)
153 return -ESOCKTNOSUPPORT
;
154 if (protocol
!= PF_KEY_V2
)
155 return -EPROTONOSUPPORT
;
158 sk
= sk_alloc(net
, PF_KEY
, GFP_KERNEL
, &key_proto
, kern
);
163 mutex_init(&pfk
->dump_lock
);
165 sock
->ops
= &pfkey_ops
;
166 sock_init_data(sock
, sk
);
168 sk
->sk_family
= PF_KEY
;
169 sk
->sk_destruct
= pfkey_sock_destruct
;
171 atomic_inc(&net_pfkey
->socks_nr
);
180 static int pfkey_release(struct socket
*sock
)
182 struct sock
*sk
= sock
->sk
;
191 skb_queue_purge(&sk
->sk_write_queue
);
199 static int pfkey_broadcast_one(struct sk_buff
*skb
, struct sk_buff
**skb2
,
200 gfp_t allocation
, struct sock
*sk
)
206 if (atomic_read(&skb
->users
) != 1) {
207 *skb2
= skb_clone(skb
, allocation
);
210 atomic_inc(&skb
->users
);
214 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
) {
215 skb_set_owner_r(*skb2
, sk
);
216 skb_queue_tail(&sk
->sk_receive_queue
, *skb2
);
217 sk
->sk_data_ready(sk
);
226 /* Send SKB to all pfkey sockets matching selected criteria. */
227 #define BROADCAST_ALL 0
228 #define BROADCAST_ONE 1
229 #define BROADCAST_REGISTERED 2
230 #define BROADCAST_PROMISC_ONLY 4
231 static int pfkey_broadcast(struct sk_buff
*skb
, gfp_t allocation
,
232 int broadcast_flags
, struct sock
*one_sk
,
235 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
237 struct sk_buff
*skb2
= NULL
;
240 /* XXX Do we need something like netlink_overrun? I think
241 * XXX PF_KEY socket apps will not mind current behavior.
247 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
248 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
251 /* Yes, it means that if you are meant to receive this
252 * pfkey message you receive it twice as promiscuous
256 pfkey_broadcast_one(skb
, &skb2
, GFP_ATOMIC
, sk
);
258 /* the exact target will be processed later */
261 if (broadcast_flags
!= BROADCAST_ALL
) {
262 if (broadcast_flags
& BROADCAST_PROMISC_ONLY
)
264 if ((broadcast_flags
& BROADCAST_REGISTERED
) &&
267 if (broadcast_flags
& BROADCAST_ONE
)
271 err2
= pfkey_broadcast_one(skb
, &skb2
, GFP_ATOMIC
, sk
);
273 /* Error is cleared after successful sending to at least one
275 if ((broadcast_flags
& BROADCAST_REGISTERED
) && err
)
281 err
= pfkey_broadcast_one(skb
, &skb2
, allocation
, one_sk
);
288 static int pfkey_do_dump(struct pfkey_sock
*pfk
)
290 struct sadb_msg
*hdr
;
293 mutex_lock(&pfk
->dump_lock
);
294 if (!pfk
->dump
.dump
) {
299 rc
= pfk
->dump
.dump(pfk
);
300 if (rc
== -ENOBUFS
) {
306 if (!pfkey_can_dump(&pfk
->sk
)) {
311 hdr
= (struct sadb_msg
*) pfk
->dump
.skb
->data
;
312 hdr
->sadb_msg_seq
= 0;
313 hdr
->sadb_msg_errno
= rc
;
314 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
315 &pfk
->sk
, sock_net(&pfk
->sk
));
316 pfk
->dump
.skb
= NULL
;
319 pfkey_terminate_dump(pfk
);
322 mutex_unlock(&pfk
->dump_lock
);
326 static inline void pfkey_hdr_dup(struct sadb_msg
*new,
327 const struct sadb_msg
*orig
)
332 static int pfkey_error(const struct sadb_msg
*orig
, int err
, struct sock
*sk
)
334 struct sk_buff
*skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_KERNEL
);
335 struct sadb_msg
*hdr
;
340 /* Woe be to the platform trying to support PFKEY yet
341 * having normal errnos outside the 1-255 range, inclusive.
344 if (err
== ERESTARTSYS
||
345 err
== ERESTARTNOHAND
||
346 err
== ERESTARTNOINTR
)
350 BUG_ON(err
<= 0 || err
>= 256);
352 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
353 pfkey_hdr_dup(hdr
, orig
);
354 hdr
->sadb_msg_errno
= (uint8_t) err
;
355 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) /
358 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, sock_net(sk
));
363 static const u8 sadb_ext_min_len
[] = {
364 [SADB_EXT_RESERVED
] = (u8
) 0,
365 [SADB_EXT_SA
] = (u8
) sizeof(struct sadb_sa
),
366 [SADB_EXT_LIFETIME_CURRENT
] = (u8
) sizeof(struct sadb_lifetime
),
367 [SADB_EXT_LIFETIME_HARD
] = (u8
) sizeof(struct sadb_lifetime
),
368 [SADB_EXT_LIFETIME_SOFT
] = (u8
) sizeof(struct sadb_lifetime
),
369 [SADB_EXT_ADDRESS_SRC
] = (u8
) sizeof(struct sadb_address
),
370 [SADB_EXT_ADDRESS_DST
] = (u8
) sizeof(struct sadb_address
),
371 [SADB_EXT_ADDRESS_PROXY
] = (u8
) sizeof(struct sadb_address
),
372 [SADB_EXT_KEY_AUTH
] = (u8
) sizeof(struct sadb_key
),
373 [SADB_EXT_KEY_ENCRYPT
] = (u8
) sizeof(struct sadb_key
),
374 [SADB_EXT_IDENTITY_SRC
] = (u8
) sizeof(struct sadb_ident
),
375 [SADB_EXT_IDENTITY_DST
] = (u8
) sizeof(struct sadb_ident
),
376 [SADB_EXT_SENSITIVITY
] = (u8
) sizeof(struct sadb_sens
),
377 [SADB_EXT_PROPOSAL
] = (u8
) sizeof(struct sadb_prop
),
378 [SADB_EXT_SUPPORTED_AUTH
] = (u8
) sizeof(struct sadb_supported
),
379 [SADB_EXT_SUPPORTED_ENCRYPT
] = (u8
) sizeof(struct sadb_supported
),
380 [SADB_EXT_SPIRANGE
] = (u8
) sizeof(struct sadb_spirange
),
381 [SADB_X_EXT_KMPRIVATE
] = (u8
) sizeof(struct sadb_x_kmprivate
),
382 [SADB_X_EXT_POLICY
] = (u8
) sizeof(struct sadb_x_policy
),
383 [SADB_X_EXT_SA2
] = (u8
) sizeof(struct sadb_x_sa2
),
384 [SADB_X_EXT_NAT_T_TYPE
] = (u8
) sizeof(struct sadb_x_nat_t_type
),
385 [SADB_X_EXT_NAT_T_SPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
386 [SADB_X_EXT_NAT_T_DPORT
] = (u8
) sizeof(struct sadb_x_nat_t_port
),
387 [SADB_X_EXT_NAT_T_OA
] = (u8
) sizeof(struct sadb_address
),
388 [SADB_X_EXT_SEC_CTX
] = (u8
) sizeof(struct sadb_x_sec_ctx
),
389 [SADB_X_EXT_KMADDRESS
] = (u8
) sizeof(struct sadb_x_kmaddress
),
390 [SADB_X_EXT_FILTER
] = (u8
) sizeof(struct sadb_x_filter
),
393 /* Verify sadb_address_{len,prefixlen} against sa_family. */
394 static int verify_address_len(const void *p
)
396 const struct sadb_address
*sp
= p
;
397 const struct sockaddr
*addr
= (const struct sockaddr
*)(sp
+ 1);
398 const struct sockaddr_in
*sin
;
399 #if IS_ENABLED(CONFIG_IPV6)
400 const struct sockaddr_in6
*sin6
;
404 if (sp
->sadb_address_len
<
405 DIV_ROUND_UP(sizeof(*sp
) + offsetofend(typeof(*addr
), sa_family
),
409 switch (addr
->sa_family
) {
411 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin
), sizeof(uint64_t));
412 if (sp
->sadb_address_len
!= len
||
413 sp
->sadb_address_prefixlen
> 32)
416 #if IS_ENABLED(CONFIG_IPV6)
418 len
= DIV_ROUND_UP(sizeof(*sp
) + sizeof(*sin6
), sizeof(uint64_t));
419 if (sp
->sadb_address_len
!= len
||
420 sp
->sadb_address_prefixlen
> 128)
425 /* It is user using kernel to keep track of security
426 * associations for another protocol, such as
427 * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
430 * XXX Actually, association/policy database is not yet
431 * XXX able to cope with arbitrary sockaddr families.
432 * XXX When it can, remove this -EINVAL. -DaveM
440 static inline int sadb_key_len(const struct sadb_key
*key
)
442 int key_bytes
= DIV_ROUND_UP(key
->sadb_key_bits
, 8);
444 return DIV_ROUND_UP(sizeof(struct sadb_key
) + key_bytes
,
448 static int verify_key_len(const void *p
)
450 const struct sadb_key
*key
= p
;
452 if (sadb_key_len(key
) > key
->sadb_key_len
)
458 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx
*sec_ctx
)
460 return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx
) +
461 sec_ctx
->sadb_x_ctx_len
,
465 static inline int verify_sec_ctx_len(const void *p
)
467 const struct sadb_x_sec_ctx
*sec_ctx
= p
;
468 int len
= sec_ctx
->sadb_x_ctx_len
;
473 len
= pfkey_sec_ctx_len(sec_ctx
);
475 if (sec_ctx
->sadb_x_sec_len
!= len
)
481 static inline struct xfrm_user_sec_ctx
*pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx
*sec_ctx
,
484 struct xfrm_user_sec_ctx
*uctx
= NULL
;
485 int ctx_size
= sec_ctx
->sadb_x_ctx_len
;
487 uctx
= kmalloc((sizeof(*uctx
)+ctx_size
), gfp
);
492 uctx
->len
= pfkey_sec_ctx_len(sec_ctx
);
493 uctx
->exttype
= sec_ctx
->sadb_x_sec_exttype
;
494 uctx
->ctx_doi
= sec_ctx
->sadb_x_ctx_doi
;
495 uctx
->ctx_alg
= sec_ctx
->sadb_x_ctx_alg
;
496 uctx
->ctx_len
= sec_ctx
->sadb_x_ctx_len
;
497 memcpy(uctx
+ 1, sec_ctx
+ 1,
503 static int present_and_same_family(const struct sadb_address
*src
,
504 const struct sadb_address
*dst
)
506 const struct sockaddr
*s_addr
, *d_addr
;
511 s_addr
= (const struct sockaddr
*)(src
+ 1);
512 d_addr
= (const struct sockaddr
*)(dst
+ 1);
513 if (s_addr
->sa_family
!= d_addr
->sa_family
)
515 if (s_addr
->sa_family
!= AF_INET
516 #if IS_ENABLED(CONFIG_IPV6)
517 && s_addr
->sa_family
!= AF_INET6
525 static int parse_exthdrs(struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void **ext_hdrs
)
527 const char *p
= (char *) hdr
;
533 const struct sadb_ext
*ehdr
= (const struct sadb_ext
*) p
;
537 if (len
< sizeof(*ehdr
))
540 ext_len
= ehdr
->sadb_ext_len
;
541 ext_len
*= sizeof(uint64_t);
542 ext_type
= ehdr
->sadb_ext_type
;
543 if (ext_len
< sizeof(uint64_t) ||
545 ext_type
== SADB_EXT_RESERVED
)
548 if (ext_type
<= SADB_EXT_MAX
) {
549 int min
= (int) sadb_ext_min_len
[ext_type
];
552 if (ext_hdrs
[ext_type
-1] != NULL
)
555 case SADB_EXT_ADDRESS_SRC
:
556 case SADB_EXT_ADDRESS_DST
:
557 case SADB_EXT_ADDRESS_PROXY
:
558 case SADB_X_EXT_NAT_T_OA
:
559 if (verify_address_len(p
))
562 case SADB_X_EXT_SEC_CTX
:
563 if (verify_sec_ctx_len(p
))
566 case SADB_EXT_KEY_AUTH
:
567 case SADB_EXT_KEY_ENCRYPT
:
568 if (verify_key_len(p
))
574 ext_hdrs
[ext_type
-1] = (void *) p
;
584 pfkey_satype2proto(uint8_t satype
)
587 case SADB_SATYPE_UNSPEC
:
588 return IPSEC_PROTO_ANY
;
591 case SADB_SATYPE_ESP
:
593 case SADB_X_SATYPE_IPCOMP
:
602 pfkey_proto2satype(uint16_t proto
)
606 return SADB_SATYPE_AH
;
608 return SADB_SATYPE_ESP
;
610 return SADB_X_SATYPE_IPCOMP
;
617 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
618 * say specifically 'just raw sockets' as we encode them as 255.
621 static uint8_t pfkey_proto_to_xfrm(uint8_t proto
)
623 return proto
== IPSEC_PROTO_ANY
? 0 : proto
;
626 static uint8_t pfkey_proto_from_xfrm(uint8_t proto
)
628 return proto
? proto
: IPSEC_PROTO_ANY
;
631 static inline int pfkey_sockaddr_len(sa_family_t family
)
635 return sizeof(struct sockaddr_in
);
636 #if IS_ENABLED(CONFIG_IPV6)
638 return sizeof(struct sockaddr_in6
);
645 int pfkey_sockaddr_extract(const struct sockaddr
*sa
, xfrm_address_t
*xaddr
)
647 switch (sa
->sa_family
) {
650 ((struct sockaddr_in
*)sa
)->sin_addr
.s_addr
;
652 #if IS_ENABLED(CONFIG_IPV6)
655 &((struct sockaddr_in6
*)sa
)->sin6_addr
,
656 sizeof(struct in6_addr
));
664 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address
*addr
, xfrm_address_t
*xaddr
)
666 return pfkey_sockaddr_extract((struct sockaddr
*)(addr
+ 1),
670 static struct xfrm_state
*pfkey_xfrm_state_lookup(struct net
*net
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
672 const struct sadb_sa
*sa
;
673 const struct sadb_address
*addr
;
675 unsigned short family
;
676 xfrm_address_t
*xaddr
;
678 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
682 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
686 /* sadb_address_len should be checked by caller */
687 addr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
691 family
= ((const struct sockaddr
*)(addr
+ 1))->sa_family
;
694 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in
*)(addr
+ 1))->sin_addr
;
696 #if IS_ENABLED(CONFIG_IPV6)
698 xaddr
= (xfrm_address_t
*)&((const struct sockaddr_in6
*)(addr
+ 1))->sin6_addr
;
708 return xfrm_state_lookup(net
, DUMMY_MARK
, xaddr
, sa
->sadb_sa_spi
, proto
, family
);
711 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
714 pfkey_sockaddr_size(sa_family_t family
)
716 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
));
719 static inline int pfkey_mode_from_xfrm(int mode
)
722 case XFRM_MODE_TRANSPORT
:
723 return IPSEC_MODE_TRANSPORT
;
724 case XFRM_MODE_TUNNEL
:
725 return IPSEC_MODE_TUNNEL
;
727 return IPSEC_MODE_BEET
;
733 static inline int pfkey_mode_to_xfrm(int mode
)
736 case IPSEC_MODE_ANY
: /*XXX*/
737 case IPSEC_MODE_TRANSPORT
:
738 return XFRM_MODE_TRANSPORT
;
739 case IPSEC_MODE_TUNNEL
:
740 return XFRM_MODE_TUNNEL
;
741 case IPSEC_MODE_BEET
:
742 return XFRM_MODE_BEET
;
748 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t
*xaddr
, __be16 port
,
750 unsigned short family
)
755 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
756 sin
->sin_family
= AF_INET
;
757 sin
->sin_port
= port
;
758 sin
->sin_addr
.s_addr
= xaddr
->a4
;
759 memset(sin
->sin_zero
, 0, sizeof(sin
->sin_zero
));
762 #if IS_ENABLED(CONFIG_IPV6)
765 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)sa
;
766 sin6
->sin6_family
= AF_INET6
;
767 sin6
->sin6_port
= port
;
768 sin6
->sin6_flowinfo
= 0;
769 sin6
->sin6_addr
= xaddr
->in6
;
770 sin6
->sin6_scope_id
= 0;
778 static struct sk_buff
*__pfkey_xfrm_state2msg(const struct xfrm_state
*x
,
779 int add_keys
, int hsc
)
782 struct sadb_msg
*hdr
;
784 struct sadb_lifetime
*lifetime
;
785 struct sadb_address
*addr
;
786 struct sadb_key
*key
;
787 struct sadb_x_sa2
*sa2
;
788 struct sadb_x_sec_ctx
*sec_ctx
;
789 struct xfrm_sec_ctx
*xfrm_ctx
;
792 int auth_key_size
= 0;
793 int encrypt_key_size
= 0;
795 struct xfrm_encap_tmpl
*natt
= NULL
;
798 /* address family check */
799 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
801 return ERR_PTR(-EINVAL
);
803 /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
804 key(AE), (identity(SD),) (sensitivity)> */
805 size
= sizeof(struct sadb_msg
) +sizeof(struct sadb_sa
) +
806 sizeof(struct sadb_lifetime
) +
807 ((hsc
& 1) ? sizeof(struct sadb_lifetime
) : 0) +
808 ((hsc
& 2) ? sizeof(struct sadb_lifetime
) : 0) +
809 sizeof(struct sadb_address
)*2 +
811 sizeof(struct sadb_x_sa2
);
813 if ((xfrm_ctx
= x
->security
)) {
814 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
815 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
818 /* identity & sensitivity */
819 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
, x
->props
.family
))
820 size
+= sizeof(struct sadb_address
) + sockaddr_size
;
823 if (x
->aalg
&& x
->aalg
->alg_key_len
) {
825 PFKEY_ALIGN8((x
->aalg
->alg_key_len
+ 7) / 8);
826 size
+= sizeof(struct sadb_key
) + auth_key_size
;
828 if (x
->ealg
&& x
->ealg
->alg_key_len
) {
830 PFKEY_ALIGN8((x
->ealg
->alg_key_len
+7) / 8);
831 size
+= sizeof(struct sadb_key
) + encrypt_key_size
;
837 if (natt
&& natt
->encap_type
) {
838 size
+= sizeof(struct sadb_x_nat_t_type
);
839 size
+= sizeof(struct sadb_x_nat_t_port
);
840 size
+= sizeof(struct sadb_x_nat_t_port
);
843 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
845 return ERR_PTR(-ENOBUFS
);
847 /* call should fill header later */
848 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
849 memset(hdr
, 0, size
); /* XXX do we need this ? */
850 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
853 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
854 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
855 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
856 sa
->sadb_sa_spi
= x
->id
.spi
;
857 sa
->sadb_sa_replay
= x
->props
.replay_window
;
858 switch (x
->km
.state
) {
859 case XFRM_STATE_VALID
:
860 sa
->sadb_sa_state
= x
->km
.dying
?
861 SADB_SASTATE_DYING
: SADB_SASTATE_MATURE
;
864 sa
->sadb_sa_state
= SADB_SASTATE_LARVAL
;
867 sa
->sadb_sa_state
= SADB_SASTATE_DEAD
;
870 sa
->sadb_sa_auth
= 0;
872 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
873 sa
->sadb_sa_auth
= (a
&& a
->pfkey_supported
) ?
874 a
->desc
.sadb_alg_id
: 0;
876 sa
->sadb_sa_encrypt
= 0;
877 BUG_ON(x
->ealg
&& x
->calg
);
879 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byname(x
->ealg
->alg_name
, 0);
880 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
881 a
->desc
.sadb_alg_id
: 0;
883 /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
885 struct xfrm_algo_desc
*a
= xfrm_calg_get_byname(x
->calg
->alg_name
, 0);
886 sa
->sadb_sa_encrypt
= (a
&& a
->pfkey_supported
) ?
887 a
->desc
.sadb_alg_id
: 0;
890 sa
->sadb_sa_flags
= 0;
891 if (x
->props
.flags
& XFRM_STATE_NOECN
)
892 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOECN
;
893 if (x
->props
.flags
& XFRM_STATE_DECAP_DSCP
)
894 sa
->sadb_sa_flags
|= SADB_SAFLAGS_DECAP_DSCP
;
895 if (x
->props
.flags
& XFRM_STATE_NOPMTUDISC
)
896 sa
->sadb_sa_flags
|= SADB_SAFLAGS_NOPMTUDISC
;
900 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
901 sizeof(struct sadb_lifetime
));
902 lifetime
->sadb_lifetime_len
=
903 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
904 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
905 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.hard_packet_limit
);
906 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.hard_byte_limit
);
907 lifetime
->sadb_lifetime_addtime
= x
->lft
.hard_add_expires_seconds
;
908 lifetime
->sadb_lifetime_usetime
= x
->lft
.hard_use_expires_seconds
;
912 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
913 sizeof(struct sadb_lifetime
));
914 lifetime
->sadb_lifetime_len
=
915 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
916 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
917 lifetime
->sadb_lifetime_allocations
= _X2KEY(x
->lft
.soft_packet_limit
);
918 lifetime
->sadb_lifetime_bytes
= _X2KEY(x
->lft
.soft_byte_limit
);
919 lifetime
->sadb_lifetime_addtime
= x
->lft
.soft_add_expires_seconds
;
920 lifetime
->sadb_lifetime_usetime
= x
->lft
.soft_use_expires_seconds
;
923 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
924 sizeof(struct sadb_lifetime
));
925 lifetime
->sadb_lifetime_len
=
926 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
927 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
928 lifetime
->sadb_lifetime_allocations
= x
->curlft
.packets
;
929 lifetime
->sadb_lifetime_bytes
= x
->curlft
.bytes
;
930 lifetime
->sadb_lifetime_addtime
= x
->curlft
.add_time
;
931 lifetime
->sadb_lifetime_usetime
= x
->curlft
.use_time
;
933 addr
= (struct sadb_address
*) skb_put(skb
,
934 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_SRC
;
939 /* "if the ports are non-zero, then the sadb_address_proto field,
940 normally zero, MUST be filled in with the transport
941 protocol's number." - RFC2367 */
942 addr
->sadb_address_proto
= 0;
943 addr
->sadb_address_reserved
= 0;
945 addr
->sadb_address_prefixlen
=
946 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
947 (struct sockaddr
*) (addr
+ 1),
949 if (!addr
->sadb_address_prefixlen
)
953 addr
= (struct sadb_address
*) skb_put(skb
,
954 sizeof(struct sadb_address
)+sockaddr_size
);
955 addr
->sadb_address_len
=
956 (sizeof(struct sadb_address
)+sockaddr_size
)/
958 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
959 addr
->sadb_address_proto
= 0;
960 addr
->sadb_address_reserved
= 0;
962 addr
->sadb_address_prefixlen
=
963 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
964 (struct sockaddr
*) (addr
+ 1),
966 if (!addr
->sadb_address_prefixlen
)
969 if (!xfrm_addr_equal(&x
->sel
.saddr
, &x
->props
.saddr
,
971 addr
= (struct sadb_address
*) skb_put(skb
,
972 sizeof(struct sadb_address
)+sockaddr_size
);
973 addr
->sadb_address_len
=
974 (sizeof(struct sadb_address
)+sockaddr_size
)/
976 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_PROXY
;
977 addr
->sadb_address_proto
=
978 pfkey_proto_from_xfrm(x
->sel
.proto
);
979 addr
->sadb_address_prefixlen
= x
->sel
.prefixlen_s
;
980 addr
->sadb_address_reserved
= 0;
982 pfkey_sockaddr_fill(&x
->sel
.saddr
, x
->sel
.sport
,
983 (struct sockaddr
*) (addr
+ 1),
988 if (add_keys
&& auth_key_size
) {
989 key
= (struct sadb_key
*) skb_put(skb
,
990 sizeof(struct sadb_key
)+auth_key_size
);
991 key
->sadb_key_len
= (sizeof(struct sadb_key
) + auth_key_size
) /
993 key
->sadb_key_exttype
= SADB_EXT_KEY_AUTH
;
994 key
->sadb_key_bits
= x
->aalg
->alg_key_len
;
995 key
->sadb_key_reserved
= 0;
996 memcpy(key
+ 1, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+7)/8);
999 if (add_keys
&& encrypt_key_size
) {
1000 key
= (struct sadb_key
*) skb_put(skb
,
1001 sizeof(struct sadb_key
)+encrypt_key_size
);
1002 key
->sadb_key_len
= (sizeof(struct sadb_key
) +
1003 encrypt_key_size
) / sizeof(uint64_t);
1004 key
->sadb_key_exttype
= SADB_EXT_KEY_ENCRYPT
;
1005 key
->sadb_key_bits
= x
->ealg
->alg_key_len
;
1006 key
->sadb_key_reserved
= 0;
1007 memcpy(key
+ 1, x
->ealg
->alg_key
,
1008 (x
->ealg
->alg_key_len
+7)/8);
1012 sa2
= (struct sadb_x_sa2
*) skb_put(skb
, sizeof(struct sadb_x_sa2
));
1013 sa2
->sadb_x_sa2_len
= sizeof(struct sadb_x_sa2
)/sizeof(uint64_t);
1014 sa2
->sadb_x_sa2_exttype
= SADB_X_EXT_SA2
;
1015 if ((mode
= pfkey_mode_from_xfrm(x
->props
.mode
)) < 0) {
1017 return ERR_PTR(-EINVAL
);
1019 sa2
->sadb_x_sa2_mode
= mode
;
1020 sa2
->sadb_x_sa2_reserved1
= 0;
1021 sa2
->sadb_x_sa2_reserved2
= 0;
1022 sa2
->sadb_x_sa2_sequence
= 0;
1023 sa2
->sadb_x_sa2_reqid
= x
->props
.reqid
;
1025 if (natt
&& natt
->encap_type
) {
1026 struct sadb_x_nat_t_type
*n_type
;
1027 struct sadb_x_nat_t_port
*n_port
;
1030 n_type
= (struct sadb_x_nat_t_type
*) skb_put(skb
, sizeof(*n_type
));
1031 n_type
->sadb_x_nat_t_type_len
= sizeof(*n_type
)/sizeof(uint64_t);
1032 n_type
->sadb_x_nat_t_type_exttype
= SADB_X_EXT_NAT_T_TYPE
;
1033 n_type
->sadb_x_nat_t_type_type
= natt
->encap_type
;
1034 n_type
->sadb_x_nat_t_type_reserved
[0] = 0;
1035 n_type
->sadb_x_nat_t_type_reserved
[1] = 0;
1036 n_type
->sadb_x_nat_t_type_reserved
[2] = 0;
1039 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
1040 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1041 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
1042 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
1043 n_port
->sadb_x_nat_t_port_reserved
= 0;
1046 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
1047 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
1048 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
1049 n_port
->sadb_x_nat_t_port_port
= natt
->encap_dport
;
1050 n_port
->sadb_x_nat_t_port_reserved
= 0;
1053 /* security context */
1055 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
1056 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
1057 sec_ctx
->sadb_x_sec_len
=
1058 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
1059 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
1060 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
1061 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
1062 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
1063 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
1071 static inline struct sk_buff
*pfkey_xfrm_state2msg(const struct xfrm_state
*x
)
1073 struct sk_buff
*skb
;
1075 skb
= __pfkey_xfrm_state2msg(x
, 1, 3);
1080 static inline struct sk_buff
*pfkey_xfrm_state2msg_expire(const struct xfrm_state
*x
,
1083 return __pfkey_xfrm_state2msg(x
, 0, hsc
);
1086 static struct xfrm_state
* pfkey_msg2xfrm_state(struct net
*net
,
1087 const struct sadb_msg
*hdr
,
1088 void * const *ext_hdrs
)
1090 struct xfrm_state
*x
;
1091 const struct sadb_lifetime
*lifetime
;
1092 const struct sadb_sa
*sa
;
1093 const struct sadb_key
*key
;
1094 const struct sadb_x_sec_ctx
*sec_ctx
;
1099 sa
= ext_hdrs
[SADB_EXT_SA
- 1];
1101 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1102 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1103 return ERR_PTR(-EINVAL
);
1104 if (hdr
->sadb_msg_satype
== SADB_SATYPE_ESP
&&
1105 !ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1])
1106 return ERR_PTR(-EINVAL
);
1107 if (hdr
->sadb_msg_satype
== SADB_SATYPE_AH
&&
1108 !ext_hdrs
[SADB_EXT_KEY_AUTH
-1])
1109 return ERR_PTR(-EINVAL
);
1110 if (!!ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1] !=
1111 !!ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1])
1112 return ERR_PTR(-EINVAL
);
1114 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1116 return ERR_PTR(-EINVAL
);
1118 /* default error is no buffer space */
1123 Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1124 SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1125 sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1126 Therefore, the sadb_sa_state field of all submitted SAs MUST be
1127 SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1130 However, KAME setkey always uses SADB_SASTATE_LARVAL.
1131 Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1133 if (sa
->sadb_sa_auth
> SADB_AALG_MAX
||
1134 (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
&&
1135 sa
->sadb_sa_encrypt
> SADB_X_CALG_MAX
) ||
1136 sa
->sadb_sa_encrypt
> SADB_EALG_MAX
)
1137 return ERR_PTR(-EINVAL
);
1138 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1140 sa
->sadb_sa_auth
!= SADB_X_AALG_NULL
&&
1141 key
->sadb_key_bits
== 0)
1142 return ERR_PTR(-EINVAL
);
1143 key
= ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1145 sa
->sadb_sa_encrypt
!= SADB_EALG_NULL
&&
1146 key
->sadb_key_bits
== 0)
1147 return ERR_PTR(-EINVAL
);
1149 x
= xfrm_state_alloc(net
);
1151 return ERR_PTR(-ENOBUFS
);
1153 x
->id
.proto
= proto
;
1154 x
->id
.spi
= sa
->sadb_sa_spi
;
1155 x
->props
.replay_window
= min_t(unsigned int, sa
->sadb_sa_replay
,
1156 (sizeof(x
->replay
.bitmap
) * 8));
1157 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOECN
)
1158 x
->props
.flags
|= XFRM_STATE_NOECN
;
1159 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_DECAP_DSCP
)
1160 x
->props
.flags
|= XFRM_STATE_DECAP_DSCP
;
1161 if (sa
->sadb_sa_flags
& SADB_SAFLAGS_NOPMTUDISC
)
1162 x
->props
.flags
|= XFRM_STATE_NOPMTUDISC
;
1164 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
- 1];
1165 if (lifetime
!= NULL
) {
1166 x
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1167 x
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1168 x
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1169 x
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1171 lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
- 1];
1172 if (lifetime
!= NULL
) {
1173 x
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
1174 x
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
1175 x
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
1176 x
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
1179 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
1180 if (sec_ctx
!= NULL
) {
1181 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
1186 err
= security_xfrm_state_alloc(x
, uctx
);
1194 key
= ext_hdrs
[SADB_EXT_KEY_AUTH
- 1];
1195 if (sa
->sadb_sa_auth
) {
1197 struct xfrm_algo_desc
*a
= xfrm_aalg_get_byid(sa
->sadb_sa_auth
);
1198 if (!a
|| !a
->pfkey_supported
) {
1203 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1204 x
->aalg
= kmalloc(sizeof(*x
->aalg
) + keysize
, GFP_KERNEL
);
1209 strcpy(x
->aalg
->alg_name
, a
->name
);
1210 x
->aalg
->alg_key_len
= 0;
1212 x
->aalg
->alg_key_len
= key
->sadb_key_bits
;
1213 memcpy(x
->aalg
->alg_key
, key
+1, keysize
);
1215 x
->aalg
->alg_trunc_len
= a
->uinfo
.auth
.icv_truncbits
;
1216 x
->props
.aalgo
= sa
->sadb_sa_auth
;
1217 /* x->algo.flags = sa->sadb_sa_flags; */
1219 if (sa
->sadb_sa_encrypt
) {
1220 if (hdr
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
1221 struct xfrm_algo_desc
*a
= xfrm_calg_get_byid(sa
->sadb_sa_encrypt
);
1222 if (!a
|| !a
->pfkey_supported
) {
1226 x
->calg
= kmalloc(sizeof(*x
->calg
), GFP_KERNEL
);
1231 strcpy(x
->calg
->alg_name
, a
->name
);
1232 x
->props
.calgo
= sa
->sadb_sa_encrypt
;
1235 struct xfrm_algo_desc
*a
= xfrm_ealg_get_byid(sa
->sadb_sa_encrypt
);
1236 if (!a
|| !a
->pfkey_supported
) {
1240 key
= (struct sadb_key
*) ext_hdrs
[SADB_EXT_KEY_ENCRYPT
-1];
1242 keysize
= (key
->sadb_key_bits
+ 7) / 8;
1243 x
->ealg
= kmalloc(sizeof(*x
->ealg
) + keysize
, GFP_KERNEL
);
1248 strcpy(x
->ealg
->alg_name
, a
->name
);
1249 x
->ealg
->alg_key_len
= 0;
1251 x
->ealg
->alg_key_len
= key
->sadb_key_bits
;
1252 memcpy(x
->ealg
->alg_key
, key
+1, keysize
);
1254 x
->props
.ealgo
= sa
->sadb_sa_encrypt
;
1255 x
->geniv
= a
->uinfo
.encr
.geniv
;
1258 /* x->algo.flags = sa->sadb_sa_flags; */
1260 x
->props
.family
= pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1262 pfkey_sadb_addr2xfrm_addr((struct sadb_address
*) ext_hdrs
[SADB_EXT_ADDRESS_DST
-1],
1265 if (ext_hdrs
[SADB_X_EXT_SA2
-1]) {
1266 const struct sadb_x_sa2
*sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1];
1267 int mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1272 x
->props
.mode
= mode
;
1273 x
->props
.reqid
= sa2
->sadb_x_sa2_reqid
;
1276 if (ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1]) {
1277 const struct sadb_address
*addr
= ext_hdrs
[SADB_EXT_ADDRESS_PROXY
-1];
1279 /* Nobody uses this, but we try. */
1280 x
->sel
.family
= pfkey_sadb_addr2xfrm_addr(addr
, &x
->sel
.saddr
);
1281 x
->sel
.prefixlen_s
= addr
->sadb_address_prefixlen
;
1285 x
->sel
.family
= x
->props
.family
;
1287 if (ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1]) {
1288 const struct sadb_x_nat_t_type
* n_type
;
1289 struct xfrm_encap_tmpl
*natt
;
1291 x
->encap
= kmalloc(sizeof(*x
->encap
), GFP_KERNEL
);
1298 n_type
= ext_hdrs
[SADB_X_EXT_NAT_T_TYPE
-1];
1299 natt
->encap_type
= n_type
->sadb_x_nat_t_type_type
;
1301 if (ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1]) {
1302 const struct sadb_x_nat_t_port
*n_port
=
1303 ext_hdrs
[SADB_X_EXT_NAT_T_SPORT
-1];
1304 natt
->encap_sport
= n_port
->sadb_x_nat_t_port_port
;
1306 if (ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1]) {
1307 const struct sadb_x_nat_t_port
*n_port
=
1308 ext_hdrs
[SADB_X_EXT_NAT_T_DPORT
-1];
1309 natt
->encap_dport
= n_port
->sadb_x_nat_t_port_port
;
1311 memset(&natt
->encap_oa
, 0, sizeof(natt
->encap_oa
));
1314 err
= xfrm_init_state(x
);
1318 x
->km
.seq
= hdr
->sadb_msg_seq
;
1322 x
->km
.state
= XFRM_STATE_DEAD
;
1324 return ERR_PTR(err
);
1327 static int pfkey_reserved(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1332 static int pfkey_getspi(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1334 struct net
*net
= sock_net(sk
);
1335 struct sk_buff
*resp_skb
;
1336 struct sadb_x_sa2
*sa2
;
1337 struct sadb_address
*saddr
, *daddr
;
1338 struct sadb_msg
*out_hdr
;
1339 struct sadb_spirange
*range
;
1340 struct xfrm_state
*x
= NULL
;
1343 u32 min_spi
, max_spi
;
1346 unsigned short family
;
1347 xfrm_address_t
*xsaddr
= NULL
, *xdaddr
= NULL
;
1349 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1350 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1353 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1357 if ((sa2
= ext_hdrs
[SADB_X_EXT_SA2
-1]) != NULL
) {
1358 mode
= pfkey_mode_to_xfrm(sa2
->sadb_x_sa2_mode
);
1361 reqid
= sa2
->sadb_x_sa2_reqid
;
1367 saddr
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
1368 daddr
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
1370 family
= ((struct sockaddr
*)(saddr
+ 1))->sa_family
;
1373 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(daddr
+ 1))->sin_addr
.s_addr
;
1374 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in
*)(saddr
+ 1))->sin_addr
.s_addr
;
1376 #if IS_ENABLED(CONFIG_IPV6)
1378 xdaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(daddr
+ 1))->sin6_addr
;
1379 xsaddr
= (xfrm_address_t
*)&((struct sockaddr_in6
*)(saddr
+ 1))->sin6_addr
;
1384 if (hdr
->sadb_msg_seq
) {
1385 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1386 if (x
&& !xfrm_addr_equal(&x
->id
.daddr
, xdaddr
, family
)) {
1393 x
= xfrm_find_acq(net
, &dummy_mark
, mode
, reqid
, proto
, xdaddr
, xsaddr
, 1, family
);
1399 max_spi
= 0x0fffffff;
1401 range
= ext_hdrs
[SADB_EXT_SPIRANGE
-1];
1403 min_spi
= range
->sadb_spirange_min
;
1404 max_spi
= range
->sadb_spirange_max
;
1407 err
= verify_spi_info(x
->id
.proto
, min_spi
, max_spi
);
1413 err
= xfrm_alloc_spi(x
, min_spi
, max_spi
);
1414 resp_skb
= err
? ERR_PTR(err
) : pfkey_xfrm_state2msg(x
);
1416 if (IS_ERR(resp_skb
)) {
1418 return PTR_ERR(resp_skb
);
1421 out_hdr
= (struct sadb_msg
*) resp_skb
->data
;
1422 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1423 out_hdr
->sadb_msg_type
= SADB_GETSPI
;
1424 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1425 out_hdr
->sadb_msg_errno
= 0;
1426 out_hdr
->sadb_msg_reserved
= 0;
1427 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1428 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1432 pfkey_broadcast(resp_skb
, GFP_KERNEL
, BROADCAST_ONE
, sk
, net
);
1437 static int pfkey_acquire(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1439 struct net
*net
= sock_net(sk
);
1440 struct xfrm_state
*x
;
1442 if (hdr
->sadb_msg_len
!= sizeof(struct sadb_msg
)/8)
1445 if (hdr
->sadb_msg_seq
== 0 || hdr
->sadb_msg_errno
== 0)
1448 x
= xfrm_find_acq_byseq(net
, DUMMY_MARK
, hdr
->sadb_msg_seq
);
1452 spin_lock_bh(&x
->lock
);
1453 if (x
->km
.state
== XFRM_STATE_ACQ
)
1454 x
->km
.state
= XFRM_STATE_ERROR
;
1456 spin_unlock_bh(&x
->lock
);
1461 static inline int event2poltype(int event
)
1464 case XFRM_MSG_DELPOLICY
:
1465 return SADB_X_SPDDELETE
;
1466 case XFRM_MSG_NEWPOLICY
:
1467 return SADB_X_SPDADD
;
1468 case XFRM_MSG_UPDPOLICY
:
1469 return SADB_X_SPDUPDATE
;
1470 case XFRM_MSG_POLEXPIRE
:
1471 // return SADB_X_SPDEXPIRE;
1473 pr_err("pfkey: Unknown policy event %d\n", event
);
1480 static inline int event2keytype(int event
)
1483 case XFRM_MSG_DELSA
:
1485 case XFRM_MSG_NEWSA
:
1487 case XFRM_MSG_UPDSA
:
1489 case XFRM_MSG_EXPIRE
:
1492 pr_err("pfkey: Unknown SA event %d\n", event
);
1500 static int key_notify_sa(struct xfrm_state
*x
, const struct km_event
*c
)
1502 struct sk_buff
*skb
;
1503 struct sadb_msg
*hdr
;
1505 skb
= pfkey_xfrm_state2msg(x
);
1508 return PTR_ERR(skb
);
1510 hdr
= (struct sadb_msg
*) skb
->data
;
1511 hdr
->sadb_msg_version
= PF_KEY_V2
;
1512 hdr
->sadb_msg_type
= event2keytype(c
->event
);
1513 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1514 hdr
->sadb_msg_errno
= 0;
1515 hdr
->sadb_msg_reserved
= 0;
1516 hdr
->sadb_msg_seq
= c
->seq
;
1517 hdr
->sadb_msg_pid
= c
->portid
;
1519 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xs_net(x
));
1524 static int pfkey_add(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1526 struct net
*net
= sock_net(sk
);
1527 struct xfrm_state
*x
;
1531 x
= pfkey_msg2xfrm_state(net
, hdr
, ext_hdrs
);
1536 if (hdr
->sadb_msg_type
== SADB_ADD
)
1537 err
= xfrm_state_add(x
);
1539 err
= xfrm_state_update(x
);
1541 xfrm_audit_state_add(x
, err
? 0 : 1, true);
1544 x
->km
.state
= XFRM_STATE_DEAD
;
1545 __xfrm_state_put(x
);
1549 if (hdr
->sadb_msg_type
== SADB_ADD
)
1550 c
.event
= XFRM_MSG_NEWSA
;
1552 c
.event
= XFRM_MSG_UPDSA
;
1553 c
.seq
= hdr
->sadb_msg_seq
;
1554 c
.portid
= hdr
->sadb_msg_pid
;
1555 km_state_notify(x
, &c
);
1561 static int pfkey_delete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1563 struct net
*net
= sock_net(sk
);
1564 struct xfrm_state
*x
;
1568 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1569 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1570 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1573 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1577 if ((err
= security_xfrm_state_delete(x
)))
1580 if (xfrm_state_kern(x
)) {
1585 err
= xfrm_state_delete(x
);
1590 c
.seq
= hdr
->sadb_msg_seq
;
1591 c
.portid
= hdr
->sadb_msg_pid
;
1592 c
.event
= XFRM_MSG_DELSA
;
1593 km_state_notify(x
, &c
);
1595 xfrm_audit_state_delete(x
, err
? 0 : 1, true);
1601 static int pfkey_get(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1603 struct net
*net
= sock_net(sk
);
1605 struct sk_buff
*out_skb
;
1606 struct sadb_msg
*out_hdr
;
1607 struct xfrm_state
*x
;
1609 if (!ext_hdrs
[SADB_EXT_SA
-1] ||
1610 !present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
1611 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]))
1614 x
= pfkey_xfrm_state_lookup(net
, hdr
, ext_hdrs
);
1618 out_skb
= pfkey_xfrm_state2msg(x
);
1619 proto
= x
->id
.proto
;
1621 if (IS_ERR(out_skb
))
1622 return PTR_ERR(out_skb
);
1624 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1625 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
1626 out_hdr
->sadb_msg_type
= SADB_GET
;
1627 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(proto
);
1628 out_hdr
->sadb_msg_errno
= 0;
1629 out_hdr
->sadb_msg_reserved
= 0;
1630 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
1631 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
1632 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, sock_net(sk
));
1637 static struct sk_buff
*compose_sadb_supported(const struct sadb_msg
*orig
,
1640 struct sk_buff
*skb
;
1641 struct sadb_msg
*hdr
;
1642 int len
, auth_len
, enc_len
, i
;
1644 auth_len
= xfrm_count_pfkey_auth_supported();
1646 auth_len
*= sizeof(struct sadb_alg
);
1647 auth_len
+= sizeof(struct sadb_supported
);
1650 enc_len
= xfrm_count_pfkey_enc_supported();
1652 enc_len
*= sizeof(struct sadb_alg
);
1653 enc_len
+= sizeof(struct sadb_supported
);
1656 len
= enc_len
+ auth_len
+ sizeof(struct sadb_msg
);
1658 skb
= alloc_skb(len
+ 16, allocation
);
1662 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(*hdr
));
1663 pfkey_hdr_dup(hdr
, orig
);
1664 hdr
->sadb_msg_errno
= 0;
1665 hdr
->sadb_msg_len
= len
/ sizeof(uint64_t);
1668 struct sadb_supported
*sp
;
1669 struct sadb_alg
*ap
;
1671 sp
= (struct sadb_supported
*) skb_put(skb
, auth_len
);
1672 ap
= (struct sadb_alg
*) (sp
+ 1);
1674 sp
->sadb_supported_len
= auth_len
/ sizeof(uint64_t);
1675 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_AUTH
;
1677 for (i
= 0; ; i
++) {
1678 struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
1681 if (!aalg
->pfkey_supported
)
1683 if (aalg
->available
)
1689 struct sadb_supported
*sp
;
1690 struct sadb_alg
*ap
;
1692 sp
= (struct sadb_supported
*) skb_put(skb
, enc_len
);
1693 ap
= (struct sadb_alg
*) (sp
+ 1);
1695 sp
->sadb_supported_len
= enc_len
/ sizeof(uint64_t);
1696 sp
->sadb_supported_exttype
= SADB_EXT_SUPPORTED_ENCRYPT
;
1698 for (i
= 0; ; i
++) {
1699 struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
1702 if (!ealg
->pfkey_supported
)
1704 if (ealg
->available
)
1713 static int pfkey_register(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1715 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1716 struct sk_buff
*supp_skb
;
1718 if (hdr
->sadb_msg_satype
> SADB_SATYPE_MAX
)
1721 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
) {
1722 if (pfk
->registered
&(1<<hdr
->sadb_msg_satype
))
1724 pfk
->registered
|= (1<<hdr
->sadb_msg_satype
);
1729 supp_skb
= compose_sadb_supported(hdr
, GFP_KERNEL
);
1731 if (hdr
->sadb_msg_satype
!= SADB_SATYPE_UNSPEC
)
1732 pfk
->registered
&= ~(1<<hdr
->sadb_msg_satype
);
1737 pfkey_broadcast(supp_skb
, GFP_KERNEL
, BROADCAST_REGISTERED
, sk
,
1742 static int unicast_flush_resp(struct sock
*sk
, const struct sadb_msg
*ihdr
)
1744 struct sk_buff
*skb
;
1745 struct sadb_msg
*hdr
;
1747 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1751 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1752 memcpy(hdr
, ihdr
, sizeof(struct sadb_msg
));
1753 hdr
->sadb_msg_errno
= (uint8_t) 0;
1754 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1756 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
,
1760 static int key_notify_sa_flush(const struct km_event
*c
)
1762 struct sk_buff
*skb
;
1763 struct sadb_msg
*hdr
;
1765 skb
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
1768 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
1769 hdr
->sadb_msg_satype
= pfkey_proto2satype(c
->data
.proto
);
1770 hdr
->sadb_msg_type
= SADB_FLUSH
;
1771 hdr
->sadb_msg_seq
= c
->seq
;
1772 hdr
->sadb_msg_pid
= c
->portid
;
1773 hdr
->sadb_msg_version
= PF_KEY_V2
;
1774 hdr
->sadb_msg_errno
= (uint8_t) 0;
1775 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
1776 hdr
->sadb_msg_reserved
= 0;
1778 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
1783 static int pfkey_flush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1785 struct net
*net
= sock_net(sk
);
1790 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1794 err
= xfrm_state_flush(net
, proto
, true);
1795 err2
= unicast_flush_resp(sk
, hdr
);
1797 if (err
== -ESRCH
) /* empty table - go quietly */
1799 return err
? err
: err2
;
1802 c
.data
.proto
= proto
;
1803 c
.seq
= hdr
->sadb_msg_seq
;
1804 c
.portid
= hdr
->sadb_msg_pid
;
1805 c
.event
= XFRM_MSG_FLUSHSA
;
1807 km_state_notify(NULL
, &c
);
1812 static int dump_sa(struct xfrm_state
*x
, int count
, void *ptr
)
1814 struct pfkey_sock
*pfk
= ptr
;
1815 struct sk_buff
*out_skb
;
1816 struct sadb_msg
*out_hdr
;
1818 if (!pfkey_can_dump(&pfk
->sk
))
1821 out_skb
= pfkey_xfrm_state2msg(x
);
1822 if (IS_ERR(out_skb
))
1823 return PTR_ERR(out_skb
);
1825 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
1826 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
1827 out_hdr
->sadb_msg_type
= SADB_DUMP
;
1828 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
1829 out_hdr
->sadb_msg_errno
= 0;
1830 out_hdr
->sadb_msg_reserved
= 0;
1831 out_hdr
->sadb_msg_seq
= count
+ 1;
1832 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
1835 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
1836 &pfk
->sk
, sock_net(&pfk
->sk
));
1837 pfk
->dump
.skb
= out_skb
;
1842 static int pfkey_dump_sa(struct pfkey_sock
*pfk
)
1844 struct net
*net
= sock_net(&pfk
->sk
);
1845 return xfrm_state_walk(net
, &pfk
->dump
.u
.state
, dump_sa
, (void *) pfk
);
1848 static void pfkey_dump_sa_done(struct pfkey_sock
*pfk
)
1850 struct net
*net
= sock_net(&pfk
->sk
);
1852 xfrm_state_walk_done(&pfk
->dump
.u
.state
, net
);
1855 static int pfkey_dump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1858 struct xfrm_address_filter
*filter
= NULL
;
1859 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1861 mutex_lock(&pfk
->dump_lock
);
1862 if (pfk
->dump
.dump
!= NULL
) {
1863 mutex_unlock(&pfk
->dump_lock
);
1867 proto
= pfkey_satype2proto(hdr
->sadb_msg_satype
);
1869 mutex_unlock(&pfk
->dump_lock
);
1873 if (ext_hdrs
[SADB_X_EXT_FILTER
- 1]) {
1874 struct sadb_x_filter
*xfilter
= ext_hdrs
[SADB_X_EXT_FILTER
- 1];
1876 filter
= kmalloc(sizeof(*filter
), GFP_KERNEL
);
1877 if (filter
== NULL
) {
1878 mutex_unlock(&pfk
->dump_lock
);
1882 memcpy(&filter
->saddr
, &xfilter
->sadb_x_filter_saddr
,
1883 sizeof(xfrm_address_t
));
1884 memcpy(&filter
->daddr
, &xfilter
->sadb_x_filter_daddr
,
1885 sizeof(xfrm_address_t
));
1886 filter
->family
= xfilter
->sadb_x_filter_family
;
1887 filter
->splen
= xfilter
->sadb_x_filter_splen
;
1888 filter
->dplen
= xfilter
->sadb_x_filter_dplen
;
1891 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
1892 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
1893 pfk
->dump
.dump
= pfkey_dump_sa
;
1894 pfk
->dump
.done
= pfkey_dump_sa_done
;
1895 xfrm_state_walk_init(&pfk
->dump
.u
.state
, proto
, filter
);
1896 mutex_unlock(&pfk
->dump_lock
);
1898 return pfkey_do_dump(pfk
);
1901 static int pfkey_promisc(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
1903 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
1904 int satype
= hdr
->sadb_msg_satype
;
1905 bool reset_errno
= false;
1907 if (hdr
->sadb_msg_len
== (sizeof(*hdr
) / sizeof(uint64_t))) {
1909 if (satype
!= 0 && satype
!= 1)
1911 pfk
->promisc
= satype
;
1913 if (reset_errno
&& skb_cloned(skb
))
1914 skb
= skb_copy(skb
, GFP_KERNEL
);
1916 skb
= skb_clone(skb
, GFP_KERNEL
);
1918 if (reset_errno
&& skb
) {
1919 struct sadb_msg
*new_hdr
= (struct sadb_msg
*) skb
->data
;
1920 new_hdr
->sadb_msg_errno
= 0;
1923 pfkey_broadcast(skb
, GFP_KERNEL
, BROADCAST_ALL
, NULL
, sock_net(sk
));
1927 static int check_reqid(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
1930 u32 reqid
= *(u32
*)ptr
;
1932 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
1933 if (xp
->xfrm_vec
[i
].reqid
== reqid
)
1939 static u32
gen_reqid(struct net
*net
)
1941 struct xfrm_policy_walk walk
;
1944 static u32 reqid
= IPSEC_MANUAL_REQID_MAX
;
1950 reqid
= IPSEC_MANUAL_REQID_MAX
+1;
1951 xfrm_policy_walk_init(&walk
, XFRM_POLICY_TYPE_MAIN
);
1952 rc
= xfrm_policy_walk(net
, &walk
, check_reqid
, (void*)&reqid
);
1953 xfrm_policy_walk_done(&walk
, net
);
1956 } while (reqid
!= start
);
1961 parse_ipsecrequest(struct xfrm_policy
*xp
, struct sadb_x_ipsecrequest
*rq
)
1963 struct net
*net
= xp_net(xp
);
1964 struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ xp
->xfrm_nr
;
1967 if (xp
->xfrm_nr
>= XFRM_MAX_DEPTH
)
1970 if (rq
->sadb_x_ipsecrequest_mode
== 0)
1973 t
->id
.proto
= rq
->sadb_x_ipsecrequest_proto
; /* XXX check proto */
1974 if ((mode
= pfkey_mode_to_xfrm(rq
->sadb_x_ipsecrequest_mode
)) < 0)
1977 if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_USE
)
1979 else if (rq
->sadb_x_ipsecrequest_level
== IPSEC_LEVEL_UNIQUE
) {
1980 t
->reqid
= rq
->sadb_x_ipsecrequest_reqid
;
1981 if (t
->reqid
> IPSEC_MANUAL_REQID_MAX
)
1983 if (!t
->reqid
&& !(t
->reqid
= gen_reqid(net
)))
1987 /* addresses present only in tunnel mode */
1988 if (t
->mode
== XFRM_MODE_TUNNEL
) {
1991 err
= parse_sockaddr_pair(
1992 (struct sockaddr
*)(rq
+ 1),
1993 rq
->sadb_x_ipsecrequest_len
- sizeof(*rq
),
1994 &t
->saddr
, &t
->id
.daddr
, &t
->encap_family
);
1998 t
->encap_family
= xp
->family
;
2000 /* No way to set this via kame pfkey */
2007 parse_ipsecrequests(struct xfrm_policy
*xp
, struct sadb_x_policy
*pol
)
2010 int len
= pol
->sadb_x_policy_len
*8 - sizeof(struct sadb_x_policy
);
2011 struct sadb_x_ipsecrequest
*rq
= (void*)(pol
+1);
2013 if (pol
->sadb_x_policy_len
* 8 < sizeof(struct sadb_x_policy
))
2016 while (len
>= sizeof(*rq
)) {
2017 if (len
< rq
->sadb_x_ipsecrequest_len
||
2018 rq
->sadb_x_ipsecrequest_len
< sizeof(*rq
))
2021 if ((err
= parse_ipsecrequest(xp
, rq
)) < 0)
2023 len
-= rq
->sadb_x_ipsecrequest_len
;
2024 rq
= (void*)((u8
*)rq
+ rq
->sadb_x_ipsecrequest_len
);
2029 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy
*xp
)
2031 struct xfrm_sec_ctx
*xfrm_ctx
= xp
->security
;
2034 int len
= sizeof(struct sadb_x_sec_ctx
);
2035 len
+= xfrm_ctx
->ctx_len
;
2036 return PFKEY_ALIGN8(len
);
2041 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy
*xp
)
2043 const struct xfrm_tmpl
*t
;
2044 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2048 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2049 t
= xp
->xfrm_vec
+ i
;
2050 socklen
+= pfkey_sockaddr_len(t
->encap_family
);
2053 return sizeof(struct sadb_msg
) +
2054 (sizeof(struct sadb_lifetime
) * 3) +
2055 (sizeof(struct sadb_address
) * 2) +
2056 (sockaddr_size
* 2) +
2057 sizeof(struct sadb_x_policy
) +
2058 (xp
->xfrm_nr
* sizeof(struct sadb_x_ipsecrequest
)) +
2060 pfkey_xfrm_policy2sec_ctx_size(xp
);
2063 static struct sk_buff
* pfkey_xfrm_policy2msg_prep(const struct xfrm_policy
*xp
)
2065 struct sk_buff
*skb
;
2068 size
= pfkey_xfrm_policy2msg_size(xp
);
2070 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
2072 return ERR_PTR(-ENOBUFS
);
2077 static int pfkey_xfrm_policy2msg(struct sk_buff
*skb
, const struct xfrm_policy
*xp
, int dir
)
2079 struct sadb_msg
*hdr
;
2080 struct sadb_address
*addr
;
2081 struct sadb_lifetime
*lifetime
;
2082 struct sadb_x_policy
*pol
;
2083 struct sadb_x_sec_ctx
*sec_ctx
;
2084 struct xfrm_sec_ctx
*xfrm_ctx
;
2087 int sockaddr_size
= pfkey_sockaddr_size(xp
->family
);
2088 int socklen
= pfkey_sockaddr_len(xp
->family
);
2090 size
= pfkey_xfrm_policy2msg_size(xp
);
2092 /* call should fill header later */
2093 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
2094 memset(hdr
, 0, size
); /* XXX do we need this ? */
2097 addr
= (struct sadb_address
*) skb_put(skb
,
2098 sizeof(struct sadb_address
)+sockaddr_size
);
2099 addr
->sadb_address_len
=
2100 (sizeof(struct sadb_address
)+sockaddr_size
)/
2102 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
2103 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2104 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_s
;
2105 addr
->sadb_address_reserved
= 0;
2106 if (!pfkey_sockaddr_fill(&xp
->selector
.saddr
,
2108 (struct sockaddr
*) (addr
+ 1),
2113 addr
= (struct sadb_address
*) skb_put(skb
,
2114 sizeof(struct sadb_address
)+sockaddr_size
);
2115 addr
->sadb_address_len
=
2116 (sizeof(struct sadb_address
)+sockaddr_size
)/
2118 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
2119 addr
->sadb_address_proto
= pfkey_proto_from_xfrm(xp
->selector
.proto
);
2120 addr
->sadb_address_prefixlen
= xp
->selector
.prefixlen_d
;
2121 addr
->sadb_address_reserved
= 0;
2123 pfkey_sockaddr_fill(&xp
->selector
.daddr
, xp
->selector
.dport
,
2124 (struct sockaddr
*) (addr
+ 1),
2128 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2129 sizeof(struct sadb_lifetime
));
2130 lifetime
->sadb_lifetime_len
=
2131 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2132 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_HARD
;
2133 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.hard_packet_limit
);
2134 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.hard_byte_limit
);
2135 lifetime
->sadb_lifetime_addtime
= xp
->lft
.hard_add_expires_seconds
;
2136 lifetime
->sadb_lifetime_usetime
= xp
->lft
.hard_use_expires_seconds
;
2138 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2139 sizeof(struct sadb_lifetime
));
2140 lifetime
->sadb_lifetime_len
=
2141 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2142 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_SOFT
;
2143 lifetime
->sadb_lifetime_allocations
= _X2KEY(xp
->lft
.soft_packet_limit
);
2144 lifetime
->sadb_lifetime_bytes
= _X2KEY(xp
->lft
.soft_byte_limit
);
2145 lifetime
->sadb_lifetime_addtime
= xp
->lft
.soft_add_expires_seconds
;
2146 lifetime
->sadb_lifetime_usetime
= xp
->lft
.soft_use_expires_seconds
;
2148 lifetime
= (struct sadb_lifetime
*) skb_put(skb
,
2149 sizeof(struct sadb_lifetime
));
2150 lifetime
->sadb_lifetime_len
=
2151 sizeof(struct sadb_lifetime
)/sizeof(uint64_t);
2152 lifetime
->sadb_lifetime_exttype
= SADB_EXT_LIFETIME_CURRENT
;
2153 lifetime
->sadb_lifetime_allocations
= xp
->curlft
.packets
;
2154 lifetime
->sadb_lifetime_bytes
= xp
->curlft
.bytes
;
2155 lifetime
->sadb_lifetime_addtime
= xp
->curlft
.add_time
;
2156 lifetime
->sadb_lifetime_usetime
= xp
->curlft
.use_time
;
2158 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
2159 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
2160 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
2161 pol
->sadb_x_policy_type
= IPSEC_POLICY_DISCARD
;
2162 if (xp
->action
== XFRM_POLICY_ALLOW
) {
2164 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
2166 pol
->sadb_x_policy_type
= IPSEC_POLICY_NONE
;
2168 pol
->sadb_x_policy_dir
= dir
+1;
2169 pol
->sadb_x_policy_reserved
= 0;
2170 pol
->sadb_x_policy_id
= xp
->index
;
2171 pol
->sadb_x_policy_priority
= xp
->priority
;
2173 for (i
=0; i
<xp
->xfrm_nr
; i
++) {
2174 const struct xfrm_tmpl
*t
= xp
->xfrm_vec
+ i
;
2175 struct sadb_x_ipsecrequest
*rq
;
2179 req_size
= sizeof(struct sadb_x_ipsecrequest
);
2180 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2181 socklen
= pfkey_sockaddr_len(t
->encap_family
);
2182 req_size
+= socklen
* 2;
2186 rq
= (void*)skb_put(skb
, req_size
);
2187 pol
->sadb_x_policy_len
+= req_size
/8;
2188 memset(rq
, 0, sizeof(*rq
));
2189 rq
->sadb_x_ipsecrequest_len
= req_size
;
2190 rq
->sadb_x_ipsecrequest_proto
= t
->id
.proto
;
2191 if ((mode
= pfkey_mode_from_xfrm(t
->mode
)) < 0)
2193 rq
->sadb_x_ipsecrequest_mode
= mode
;
2194 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_REQUIRE
;
2196 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_UNIQUE
;
2198 rq
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_USE
;
2199 rq
->sadb_x_ipsecrequest_reqid
= t
->reqid
;
2201 if (t
->mode
== XFRM_MODE_TUNNEL
) {
2202 u8
*sa
= (void *)(rq
+ 1);
2203 pfkey_sockaddr_fill(&t
->saddr
, 0,
2204 (struct sockaddr
*)sa
,
2206 pfkey_sockaddr_fill(&t
->id
.daddr
, 0,
2207 (struct sockaddr
*) (sa
+ socklen
),
2212 /* security context */
2213 if ((xfrm_ctx
= xp
->security
)) {
2214 int ctx_size
= pfkey_xfrm_policy2sec_ctx_size(xp
);
2216 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
, ctx_size
);
2217 sec_ctx
->sadb_x_sec_len
= ctx_size
/ sizeof(uint64_t);
2218 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
2219 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
2220 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
2221 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
2222 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
2226 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
2227 hdr
->sadb_msg_reserved
= atomic_read(&xp
->refcnt
);
2232 static int key_notify_policy(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
2234 struct sk_buff
*out_skb
;
2235 struct sadb_msg
*out_hdr
;
2238 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2239 if (IS_ERR(out_skb
))
2240 return PTR_ERR(out_skb
);
2242 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2246 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2247 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
2249 if (c
->data
.byid
&& c
->event
== XFRM_MSG_DELPOLICY
)
2250 out_hdr
->sadb_msg_type
= SADB_X_SPDDELETE2
;
2252 out_hdr
->sadb_msg_type
= event2poltype(c
->event
);
2253 out_hdr
->sadb_msg_errno
= 0;
2254 out_hdr
->sadb_msg_seq
= c
->seq
;
2255 out_hdr
->sadb_msg_pid
= c
->portid
;
2256 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, xp_net(xp
));
2261 static int pfkey_spdadd(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2263 struct net
*net
= sock_net(sk
);
2265 struct sadb_lifetime
*lifetime
;
2266 struct sadb_address
*sa
;
2267 struct sadb_x_policy
*pol
;
2268 struct xfrm_policy
*xp
;
2270 struct sadb_x_sec_ctx
*sec_ctx
;
2272 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2273 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2274 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2277 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2278 if (pol
->sadb_x_policy_type
> IPSEC_POLICY_IPSEC
)
2280 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2283 xp
= xfrm_policy_alloc(net
, GFP_KERNEL
);
2287 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
2288 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
2289 xp
->priority
= pol
->sadb_x_policy_priority
;
2291 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2292 xp
->family
= pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.saddr
);
2293 xp
->selector
.family
= xp
->family
;
2294 xp
->selector
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2295 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2296 xp
->selector
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2297 if (xp
->selector
.sport
)
2298 xp
->selector
.sport_mask
= htons(0xffff);
2300 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2301 pfkey_sadb_addr2xfrm_addr(sa
, &xp
->selector
.daddr
);
2302 xp
->selector
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2304 /* Amusing, we set this twice. KAME apps appear to set same value
2305 * in both addresses.
2307 xp
->selector
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2309 xp
->selector
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2310 if (xp
->selector
.dport
)
2311 xp
->selector
.dport_mask
= htons(0xffff);
2313 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2314 if (sec_ctx
!= NULL
) {
2315 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2322 err
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_KERNEL
);
2329 xp
->lft
.soft_byte_limit
= XFRM_INF
;
2330 xp
->lft
.hard_byte_limit
= XFRM_INF
;
2331 xp
->lft
.soft_packet_limit
= XFRM_INF
;
2332 xp
->lft
.hard_packet_limit
= XFRM_INF
;
2333 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_HARD
-1]) != NULL
) {
2334 xp
->lft
.hard_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2335 xp
->lft
.hard_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2336 xp
->lft
.hard_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2337 xp
->lft
.hard_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2339 if ((lifetime
= ext_hdrs
[SADB_EXT_LIFETIME_SOFT
-1]) != NULL
) {
2340 xp
->lft
.soft_packet_limit
= _KEY2X(lifetime
->sadb_lifetime_allocations
);
2341 xp
->lft
.soft_byte_limit
= _KEY2X(lifetime
->sadb_lifetime_bytes
);
2342 xp
->lft
.soft_add_expires_seconds
= lifetime
->sadb_lifetime_addtime
;
2343 xp
->lft
.soft_use_expires_seconds
= lifetime
->sadb_lifetime_usetime
;
2346 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
2347 (err
= parse_ipsecrequests(xp
, pol
)) < 0)
2350 err
= xfrm_policy_insert(pol
->sadb_x_policy_dir
-1, xp
,
2351 hdr
->sadb_msg_type
!= SADB_X_SPDUPDATE
);
2353 xfrm_audit_policy_add(xp
, err
? 0 : 1, true);
2358 if (hdr
->sadb_msg_type
== SADB_X_SPDUPDATE
)
2359 c
.event
= XFRM_MSG_UPDPOLICY
;
2361 c
.event
= XFRM_MSG_NEWPOLICY
;
2363 c
.seq
= hdr
->sadb_msg_seq
;
2364 c
.portid
= hdr
->sadb_msg_pid
;
2366 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2372 xfrm_policy_destroy(xp
);
2376 static int pfkey_spddelete(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2378 struct net
*net
= sock_net(sk
);
2380 struct sadb_address
*sa
;
2381 struct sadb_x_policy
*pol
;
2382 struct xfrm_policy
*xp
;
2383 struct xfrm_selector sel
;
2385 struct sadb_x_sec_ctx
*sec_ctx
;
2386 struct xfrm_sec_ctx
*pol_ctx
= NULL
;
2388 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1],
2389 ext_hdrs
[SADB_EXT_ADDRESS_DST
-1]) ||
2390 !ext_hdrs
[SADB_X_EXT_POLICY
-1])
2393 pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1];
2394 if (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
)
2397 memset(&sel
, 0, sizeof(sel
));
2399 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
-1];
2400 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2401 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2402 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2403 sel
.sport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2405 sel
.sport_mask
= htons(0xffff);
2407 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
-1];
2408 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2409 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2410 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2411 sel
.dport
= ((struct sockaddr_in
*)(sa
+1))->sin_port
;
2413 sel
.dport_mask
= htons(0xffff);
2415 sec_ctx
= ext_hdrs
[SADB_X_EXT_SEC_CTX
- 1];
2416 if (sec_ctx
!= NULL
) {
2417 struct xfrm_user_sec_ctx
*uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_KERNEL
);
2422 err
= security_xfrm_policy_alloc(&pol_ctx
, uctx
, GFP_KERNEL
);
2428 xp
= xfrm_policy_bysel_ctx(net
, DUMMY_MARK
, XFRM_POLICY_TYPE_MAIN
,
2429 pol
->sadb_x_policy_dir
- 1, &sel
, pol_ctx
,
2431 security_xfrm_policy_free(pol_ctx
);
2435 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2440 c
.seq
= hdr
->sadb_msg_seq
;
2441 c
.portid
= hdr
->sadb_msg_pid
;
2443 c
.event
= XFRM_MSG_DELPOLICY
;
2444 km_policy_notify(xp
, pol
->sadb_x_policy_dir
-1, &c
);
2449 xfrm_garbage_collect(net
);
2453 static int key_pol_get_resp(struct sock
*sk
, struct xfrm_policy
*xp
, const struct sadb_msg
*hdr
, int dir
)
2456 struct sk_buff
*out_skb
;
2457 struct sadb_msg
*out_hdr
;
2460 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2461 if (IS_ERR(out_skb
)) {
2462 err
= PTR_ERR(out_skb
);
2465 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2469 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2470 out_hdr
->sadb_msg_version
= hdr
->sadb_msg_version
;
2471 out_hdr
->sadb_msg_type
= hdr
->sadb_msg_type
;
2472 out_hdr
->sadb_msg_satype
= 0;
2473 out_hdr
->sadb_msg_errno
= 0;
2474 out_hdr
->sadb_msg_seq
= hdr
->sadb_msg_seq
;
2475 out_hdr
->sadb_msg_pid
= hdr
->sadb_msg_pid
;
2476 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_ONE
, sk
, xp_net(xp
));
2483 static int pfkey_sockaddr_pair_size(sa_family_t family
)
2485 return PFKEY_ALIGN8(pfkey_sockaddr_len(family
) * 2);
2488 static int parse_sockaddr_pair(struct sockaddr
*sa
, int ext_len
,
2489 xfrm_address_t
*saddr
, xfrm_address_t
*daddr
,
2494 if (ext_len
< 2 || ext_len
< pfkey_sockaddr_pair_size(sa
->sa_family
))
2497 af
= pfkey_sockaddr_extract(sa
, saddr
);
2501 socklen
= pfkey_sockaddr_len(af
);
2502 if (pfkey_sockaddr_extract((struct sockaddr
*) (((u8
*)sa
) + socklen
),
2510 #ifdef CONFIG_NET_KEY_MIGRATE
2511 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest
*rq1
, int len
,
2512 struct xfrm_migrate
*m
)
2515 struct sadb_x_ipsecrequest
*rq2
;
2518 if (len
< sizeof(*rq1
) ||
2519 len
< rq1
->sadb_x_ipsecrequest_len
||
2520 rq1
->sadb_x_ipsecrequest_len
< sizeof(*rq1
))
2524 err
= parse_sockaddr_pair((struct sockaddr
*)(rq1
+ 1),
2525 rq1
->sadb_x_ipsecrequest_len
- sizeof(*rq1
),
2526 &m
->old_saddr
, &m
->old_daddr
,
2531 rq2
= (struct sadb_x_ipsecrequest
*)((u8
*)rq1
+ rq1
->sadb_x_ipsecrequest_len
);
2532 len
-= rq1
->sadb_x_ipsecrequest_len
;
2534 if (len
<= sizeof(*rq2
) ||
2535 len
< rq2
->sadb_x_ipsecrequest_len
||
2536 rq2
->sadb_x_ipsecrequest_len
< sizeof(*rq2
))
2540 err
= parse_sockaddr_pair((struct sockaddr
*)(rq2
+ 1),
2541 rq2
->sadb_x_ipsecrequest_len
- sizeof(*rq2
),
2542 &m
->new_saddr
, &m
->new_daddr
,
2547 if (rq1
->sadb_x_ipsecrequest_proto
!= rq2
->sadb_x_ipsecrequest_proto
||
2548 rq1
->sadb_x_ipsecrequest_mode
!= rq2
->sadb_x_ipsecrequest_mode
||
2549 rq1
->sadb_x_ipsecrequest_reqid
!= rq2
->sadb_x_ipsecrequest_reqid
)
2552 m
->proto
= rq1
->sadb_x_ipsecrequest_proto
;
2553 if ((mode
= pfkey_mode_to_xfrm(rq1
->sadb_x_ipsecrequest_mode
)) < 0)
2556 m
->reqid
= rq1
->sadb_x_ipsecrequest_reqid
;
2558 return ((int)(rq1
->sadb_x_ipsecrequest_len
+
2559 rq2
->sadb_x_ipsecrequest_len
));
2562 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2563 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2565 int i
, len
, ret
, err
= -EINVAL
;
2567 struct sadb_address
*sa
;
2568 struct sadb_x_kmaddress
*kma
;
2569 struct sadb_x_policy
*pol
;
2570 struct sadb_x_ipsecrequest
*rq
;
2571 struct xfrm_selector sel
;
2572 struct xfrm_migrate m
[XFRM_MAX_DEPTH
];
2573 struct xfrm_kmaddress k
;
2574 struct net
*net
= sock_net(sk
);
2576 if (!present_and_same_family(ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1],
2577 ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1]) ||
2578 !ext_hdrs
[SADB_X_EXT_POLICY
- 1]) {
2583 kma
= ext_hdrs
[SADB_X_EXT_KMADDRESS
- 1];
2584 pol
= ext_hdrs
[SADB_X_EXT_POLICY
- 1];
2586 if (pol
->sadb_x_policy_dir
>= IPSEC_DIR_MAX
) {
2592 /* convert sadb_x_kmaddress to xfrm_kmaddress */
2593 k
.reserved
= kma
->sadb_x_kmaddress_reserved
;
2594 ret
= parse_sockaddr_pair((struct sockaddr
*)(kma
+ 1),
2595 8*(kma
->sadb_x_kmaddress_len
) - sizeof(*kma
),
2596 &k
.local
, &k
.remote
, &k
.family
);
2603 dir
= pol
->sadb_x_policy_dir
- 1;
2604 memset(&sel
, 0, sizeof(sel
));
2606 /* set source address info of selector */
2607 sa
= ext_hdrs
[SADB_EXT_ADDRESS_SRC
- 1];
2608 sel
.family
= pfkey_sadb_addr2xfrm_addr(sa
, &sel
.saddr
);
2609 sel
.prefixlen_s
= sa
->sadb_address_prefixlen
;
2610 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2611 sel
.sport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2613 sel
.sport_mask
= htons(0xffff);
2615 /* set destination address info of selector */
2616 sa
= ext_hdrs
[SADB_EXT_ADDRESS_DST
- 1];
2617 pfkey_sadb_addr2xfrm_addr(sa
, &sel
.daddr
);
2618 sel
.prefixlen_d
= sa
->sadb_address_prefixlen
;
2619 sel
.proto
= pfkey_proto_to_xfrm(sa
->sadb_address_proto
);
2620 sel
.dport
= ((struct sockaddr_in
*)(sa
+ 1))->sin_port
;
2622 sel
.dport_mask
= htons(0xffff);
2624 rq
= (struct sadb_x_ipsecrequest
*)(pol
+ 1);
2626 /* extract ipsecrequests */
2628 len
= pol
->sadb_x_policy_len
* 8 - sizeof(struct sadb_x_policy
);
2630 while (len
> 0 && i
< XFRM_MAX_DEPTH
) {
2631 ret
= ipsecrequests_to_migrate(rq
, len
, &m
[i
]);
2636 rq
= (struct sadb_x_ipsecrequest
*)((u8
*)rq
+ ret
);
2642 if (!i
|| len
> 0) {
2647 return xfrm_migrate(&sel
, dir
, XFRM_POLICY_TYPE_MAIN
, m
, i
,
2648 kma
? &k
: NULL
, net
);
2654 static int pfkey_migrate(struct sock
*sk
, struct sk_buff
*skb
,
2655 const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2657 return -ENOPROTOOPT
;
2662 static int pfkey_spdget(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2664 struct net
*net
= sock_net(sk
);
2666 int err
= 0, delete;
2667 struct sadb_x_policy
*pol
;
2668 struct xfrm_policy
*xp
;
2671 if ((pol
= ext_hdrs
[SADB_X_EXT_POLICY
-1]) == NULL
)
2674 dir
= xfrm_policy_id2dir(pol
->sadb_x_policy_id
);
2675 if (dir
>= XFRM_POLICY_MAX
)
2678 delete = (hdr
->sadb_msg_type
== SADB_X_SPDDELETE2
);
2679 xp
= xfrm_policy_byid(net
, DUMMY_MARK
, XFRM_POLICY_TYPE_MAIN
,
2680 dir
, pol
->sadb_x_policy_id
, delete, &err
);
2685 xfrm_audit_policy_delete(xp
, err
? 0 : 1, true);
2689 c
.seq
= hdr
->sadb_msg_seq
;
2690 c
.portid
= hdr
->sadb_msg_pid
;
2692 c
.event
= XFRM_MSG_DELPOLICY
;
2693 km_policy_notify(xp
, dir
, &c
);
2695 err
= key_pol_get_resp(sk
, xp
, hdr
, dir
);
2700 if (delete && err
== 0)
2701 xfrm_garbage_collect(net
);
2705 static int dump_sp(struct xfrm_policy
*xp
, int dir
, int count
, void *ptr
)
2707 struct pfkey_sock
*pfk
= ptr
;
2708 struct sk_buff
*out_skb
;
2709 struct sadb_msg
*out_hdr
;
2712 if (!pfkey_can_dump(&pfk
->sk
))
2715 out_skb
= pfkey_xfrm_policy2msg_prep(xp
);
2716 if (IS_ERR(out_skb
))
2717 return PTR_ERR(out_skb
);
2719 err
= pfkey_xfrm_policy2msg(out_skb
, xp
, dir
);
2723 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
2724 out_hdr
->sadb_msg_version
= pfk
->dump
.msg_version
;
2725 out_hdr
->sadb_msg_type
= SADB_X_SPDDUMP
;
2726 out_hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2727 out_hdr
->sadb_msg_errno
= 0;
2728 out_hdr
->sadb_msg_seq
= count
+ 1;
2729 out_hdr
->sadb_msg_pid
= pfk
->dump
.msg_portid
;
2732 pfkey_broadcast(pfk
->dump
.skb
, GFP_ATOMIC
, BROADCAST_ONE
,
2733 &pfk
->sk
, sock_net(&pfk
->sk
));
2734 pfk
->dump
.skb
= out_skb
;
2739 static int pfkey_dump_sp(struct pfkey_sock
*pfk
)
2741 struct net
*net
= sock_net(&pfk
->sk
);
2742 return xfrm_policy_walk(net
, &pfk
->dump
.u
.policy
, dump_sp
, (void *) pfk
);
2745 static void pfkey_dump_sp_done(struct pfkey_sock
*pfk
)
2747 struct net
*net
= sock_net((struct sock
*)pfk
);
2749 xfrm_policy_walk_done(&pfk
->dump
.u
.policy
, net
);
2752 static int pfkey_spddump(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2754 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
2756 mutex_lock(&pfk
->dump_lock
);
2757 if (pfk
->dump
.dump
!= NULL
) {
2758 mutex_unlock(&pfk
->dump_lock
);
2762 pfk
->dump
.msg_version
= hdr
->sadb_msg_version
;
2763 pfk
->dump
.msg_portid
= hdr
->sadb_msg_pid
;
2764 pfk
->dump
.dump
= pfkey_dump_sp
;
2765 pfk
->dump
.done
= pfkey_dump_sp_done
;
2766 xfrm_policy_walk_init(&pfk
->dump
.u
.policy
, XFRM_POLICY_TYPE_MAIN
);
2767 mutex_unlock(&pfk
->dump_lock
);
2769 return pfkey_do_dump(pfk
);
2772 static int key_notify_policy_flush(const struct km_event
*c
)
2774 struct sk_buff
*skb_out
;
2775 struct sadb_msg
*hdr
;
2777 skb_out
= alloc_skb(sizeof(struct sadb_msg
) + 16, GFP_ATOMIC
);
2780 hdr
= (struct sadb_msg
*) skb_put(skb_out
, sizeof(struct sadb_msg
));
2781 hdr
->sadb_msg_type
= SADB_X_SPDFLUSH
;
2782 hdr
->sadb_msg_seq
= c
->seq
;
2783 hdr
->sadb_msg_pid
= c
->portid
;
2784 hdr
->sadb_msg_version
= PF_KEY_V2
;
2785 hdr
->sadb_msg_errno
= (uint8_t) 0;
2786 hdr
->sadb_msg_satype
= SADB_SATYPE_UNSPEC
;
2787 hdr
->sadb_msg_len
= (sizeof(struct sadb_msg
) / sizeof(uint64_t));
2788 hdr
->sadb_msg_reserved
= 0;
2789 pfkey_broadcast(skb_out
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, c
->net
);
2794 static int pfkey_spdflush(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
, void * const *ext_hdrs
)
2796 struct net
*net
= sock_net(sk
);
2800 err
= xfrm_policy_flush(net
, XFRM_POLICY_TYPE_MAIN
, true);
2801 err2
= unicast_flush_resp(sk
, hdr
);
2803 if (err
== -ESRCH
) /* empty table - old silent behavior */
2808 c
.data
.type
= XFRM_POLICY_TYPE_MAIN
;
2809 c
.event
= XFRM_MSG_FLUSHPOLICY
;
2810 c
.portid
= hdr
->sadb_msg_pid
;
2811 c
.seq
= hdr
->sadb_msg_seq
;
2813 km_policy_notify(NULL
, 0, &c
);
2818 typedef int (*pfkey_handler
)(struct sock
*sk
, struct sk_buff
*skb
,
2819 const struct sadb_msg
*hdr
, void * const *ext_hdrs
);
2820 static const pfkey_handler pfkey_funcs
[SADB_MAX
+ 1] = {
2821 [SADB_RESERVED
] = pfkey_reserved
,
2822 [SADB_GETSPI
] = pfkey_getspi
,
2823 [SADB_UPDATE
] = pfkey_add
,
2824 [SADB_ADD
] = pfkey_add
,
2825 [SADB_DELETE
] = pfkey_delete
,
2826 [SADB_GET
] = pfkey_get
,
2827 [SADB_ACQUIRE
] = pfkey_acquire
,
2828 [SADB_REGISTER
] = pfkey_register
,
2829 [SADB_EXPIRE
] = NULL
,
2830 [SADB_FLUSH
] = pfkey_flush
,
2831 [SADB_DUMP
] = pfkey_dump
,
2832 [SADB_X_PROMISC
] = pfkey_promisc
,
2833 [SADB_X_PCHANGE
] = NULL
,
2834 [SADB_X_SPDUPDATE
] = pfkey_spdadd
,
2835 [SADB_X_SPDADD
] = pfkey_spdadd
,
2836 [SADB_X_SPDDELETE
] = pfkey_spddelete
,
2837 [SADB_X_SPDGET
] = pfkey_spdget
,
2838 [SADB_X_SPDACQUIRE
] = NULL
,
2839 [SADB_X_SPDDUMP
] = pfkey_spddump
,
2840 [SADB_X_SPDFLUSH
] = pfkey_spdflush
,
2841 [SADB_X_SPDSETIDX
] = pfkey_spdadd
,
2842 [SADB_X_SPDDELETE2
] = pfkey_spdget
,
2843 [SADB_X_MIGRATE
] = pfkey_migrate
,
2846 static int pfkey_process(struct sock
*sk
, struct sk_buff
*skb
, const struct sadb_msg
*hdr
)
2848 void *ext_hdrs
[SADB_EXT_MAX
];
2851 pfkey_broadcast(skb_clone(skb
, GFP_KERNEL
), GFP_KERNEL
,
2852 BROADCAST_PROMISC_ONLY
, NULL
, sock_net(sk
));
2854 memset(ext_hdrs
, 0, sizeof(ext_hdrs
));
2855 err
= parse_exthdrs(skb
, hdr
, ext_hdrs
);
2858 if (pfkey_funcs
[hdr
->sadb_msg_type
])
2859 err
= pfkey_funcs
[hdr
->sadb_msg_type
](sk
, skb
, hdr
, ext_hdrs
);
2864 static struct sadb_msg
*pfkey_get_base_msg(struct sk_buff
*skb
, int *errp
)
2866 struct sadb_msg
*hdr
= NULL
;
2868 if (skb
->len
< sizeof(*hdr
)) {
2871 hdr
= (struct sadb_msg
*) skb
->data
;
2872 if (hdr
->sadb_msg_version
!= PF_KEY_V2
||
2873 hdr
->sadb_msg_reserved
!= 0 ||
2874 (hdr
->sadb_msg_type
<= SADB_RESERVED
||
2875 hdr
->sadb_msg_type
> SADB_MAX
)) {
2878 } else if (hdr
->sadb_msg_len
!= (skb
->len
/
2879 sizeof(uint64_t)) ||
2880 hdr
->sadb_msg_len
< (sizeof(struct sadb_msg
) /
2881 sizeof(uint64_t))) {
2891 static inline int aalg_tmpl_set(const struct xfrm_tmpl
*t
,
2892 const struct xfrm_algo_desc
*d
)
2894 unsigned int id
= d
->desc
.sadb_alg_id
;
2896 if (id
>= sizeof(t
->aalgos
) * 8)
2899 return (t
->aalgos
>> id
) & 1;
2902 static inline int ealg_tmpl_set(const struct xfrm_tmpl
*t
,
2903 const struct xfrm_algo_desc
*d
)
2905 unsigned int id
= d
->desc
.sadb_alg_id
;
2907 if (id
>= sizeof(t
->ealgos
) * 8)
2910 return (t
->ealgos
>> id
) & 1;
2913 static int count_ah_combs(const struct xfrm_tmpl
*t
)
2917 for (i
= 0; ; i
++) {
2918 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2921 if (!aalg
->pfkey_supported
)
2923 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2924 sz
+= sizeof(struct sadb_comb
);
2926 return sz
+ sizeof(struct sadb_prop
);
2929 static int count_esp_combs(const struct xfrm_tmpl
*t
)
2933 for (i
= 0; ; i
++) {
2934 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
2938 if (!ealg
->pfkey_supported
)
2941 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
2944 for (k
= 1; ; k
++) {
2945 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
2949 if (!aalg
->pfkey_supported
)
2952 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
)
2953 sz
+= sizeof(struct sadb_comb
);
2956 return sz
+ sizeof(struct sadb_prop
);
2959 static void dump_ah_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2961 struct sadb_prop
*p
;
2964 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
2965 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
2966 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
2967 p
->sadb_prop_replay
= 32;
2968 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
2970 for (i
= 0; ; i
++) {
2971 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(i
);
2975 if (!aalg
->pfkey_supported
)
2978 if (aalg_tmpl_set(t
, aalg
) && aalg
->available
) {
2979 struct sadb_comb
*c
;
2980 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
2981 memset(c
, 0, sizeof(*c
));
2982 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
2983 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
2984 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
2985 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
2986 c
->sadb_comb_hard_addtime
= 24*60*60;
2987 c
->sadb_comb_soft_addtime
= 20*60*60;
2988 c
->sadb_comb_hard_usetime
= 8*60*60;
2989 c
->sadb_comb_soft_usetime
= 7*60*60;
2994 static void dump_esp_combs(struct sk_buff
*skb
, const struct xfrm_tmpl
*t
)
2996 struct sadb_prop
*p
;
2999 p
= (struct sadb_prop
*)skb_put(skb
, sizeof(struct sadb_prop
));
3000 p
->sadb_prop_len
= sizeof(struct sadb_prop
)/8;
3001 p
->sadb_prop_exttype
= SADB_EXT_PROPOSAL
;
3002 p
->sadb_prop_replay
= 32;
3003 memset(p
->sadb_prop_reserved
, 0, sizeof(p
->sadb_prop_reserved
));
3006 const struct xfrm_algo_desc
*ealg
= xfrm_ealg_get_byidx(i
);
3010 if (!ealg
->pfkey_supported
)
3013 if (!(ealg_tmpl_set(t
, ealg
) && ealg
->available
))
3016 for (k
= 1; ; k
++) {
3017 struct sadb_comb
*c
;
3018 const struct xfrm_algo_desc
*aalg
= xfrm_aalg_get_byidx(k
);
3021 if (!aalg
->pfkey_supported
)
3023 if (!(aalg_tmpl_set(t
, aalg
) && aalg
->available
))
3025 c
= (struct sadb_comb
*)skb_put(skb
, sizeof(struct sadb_comb
));
3026 memset(c
, 0, sizeof(*c
));
3027 p
->sadb_prop_len
+= sizeof(struct sadb_comb
)/8;
3028 c
->sadb_comb_auth
= aalg
->desc
.sadb_alg_id
;
3029 c
->sadb_comb_auth_minbits
= aalg
->desc
.sadb_alg_minbits
;
3030 c
->sadb_comb_auth_maxbits
= aalg
->desc
.sadb_alg_maxbits
;
3031 c
->sadb_comb_encrypt
= ealg
->desc
.sadb_alg_id
;
3032 c
->sadb_comb_encrypt_minbits
= ealg
->desc
.sadb_alg_minbits
;
3033 c
->sadb_comb_encrypt_maxbits
= ealg
->desc
.sadb_alg_maxbits
;
3034 c
->sadb_comb_hard_addtime
= 24*60*60;
3035 c
->sadb_comb_soft_addtime
= 20*60*60;
3036 c
->sadb_comb_hard_usetime
= 8*60*60;
3037 c
->sadb_comb_soft_usetime
= 7*60*60;
3042 static int key_notify_policy_expire(struct xfrm_policy
*xp
, const struct km_event
*c
)
3047 static int key_notify_sa_expire(struct xfrm_state
*x
, const struct km_event
*c
)
3049 struct sk_buff
*out_skb
;
3050 struct sadb_msg
*out_hdr
;
3054 hard
= c
->data
.hard
;
3060 out_skb
= pfkey_xfrm_state2msg_expire(x
, hsc
);
3061 if (IS_ERR(out_skb
))
3062 return PTR_ERR(out_skb
);
3064 out_hdr
= (struct sadb_msg
*) out_skb
->data
;
3065 out_hdr
->sadb_msg_version
= PF_KEY_V2
;
3066 out_hdr
->sadb_msg_type
= SADB_EXPIRE
;
3067 out_hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3068 out_hdr
->sadb_msg_errno
= 0;
3069 out_hdr
->sadb_msg_reserved
= 0;
3070 out_hdr
->sadb_msg_seq
= 0;
3071 out_hdr
->sadb_msg_pid
= 0;
3073 pfkey_broadcast(out_skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3078 static int pfkey_send_notify(struct xfrm_state
*x
, const struct km_event
*c
)
3080 struct net
*net
= x
? xs_net(x
) : c
->net
;
3081 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3083 if (atomic_read(&net_pfkey
->socks_nr
) == 0)
3087 case XFRM_MSG_EXPIRE
:
3088 return key_notify_sa_expire(x
, c
);
3089 case XFRM_MSG_DELSA
:
3090 case XFRM_MSG_NEWSA
:
3091 case XFRM_MSG_UPDSA
:
3092 return key_notify_sa(x
, c
);
3093 case XFRM_MSG_FLUSHSA
:
3094 return key_notify_sa_flush(c
);
3095 case XFRM_MSG_NEWAE
: /* not yet supported */
3098 pr_err("pfkey: Unknown SA event %d\n", c
->event
);
3105 static int pfkey_send_policy_notify(struct xfrm_policy
*xp
, int dir
, const struct km_event
*c
)
3107 if (xp
&& xp
->type
!= XFRM_POLICY_TYPE_MAIN
)
3111 case XFRM_MSG_POLEXPIRE
:
3112 return key_notify_policy_expire(xp
, c
);
3113 case XFRM_MSG_DELPOLICY
:
3114 case XFRM_MSG_NEWPOLICY
:
3115 case XFRM_MSG_UPDPOLICY
:
3116 return key_notify_policy(xp
, dir
, c
);
3117 case XFRM_MSG_FLUSHPOLICY
:
3118 if (c
->data
.type
!= XFRM_POLICY_TYPE_MAIN
)
3120 return key_notify_policy_flush(c
);
3122 pr_err("pfkey: Unknown policy event %d\n", c
->event
);
3129 static u32
get_acqseq(void)
3132 static atomic_t acqseq
;
3135 res
= atomic_inc_return(&acqseq
);
3140 static bool pfkey_is_alive(const struct km_event
*c
)
3142 struct netns_pfkey
*net_pfkey
= net_generic(c
->net
, pfkey_net_id
);
3144 bool is_alive
= false;
3147 sk_for_each_rcu(sk
, &net_pfkey
->table
) {
3148 if (pfkey_sk(sk
)->registered
) {
3158 static int pfkey_send_acquire(struct xfrm_state
*x
, struct xfrm_tmpl
*t
, struct xfrm_policy
*xp
)
3160 struct sk_buff
*skb
;
3161 struct sadb_msg
*hdr
;
3162 struct sadb_address
*addr
;
3163 struct sadb_x_policy
*pol
;
3166 struct sadb_x_sec_ctx
*sec_ctx
;
3167 struct xfrm_sec_ctx
*xfrm_ctx
;
3170 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3174 size
= sizeof(struct sadb_msg
) +
3175 (sizeof(struct sadb_address
) * 2) +
3176 (sockaddr_size
* 2) +
3177 sizeof(struct sadb_x_policy
);
3179 if (x
->id
.proto
== IPPROTO_AH
)
3180 size
+= count_ah_combs(t
);
3181 else if (x
->id
.proto
== IPPROTO_ESP
)
3182 size
+= count_esp_combs(t
);
3184 if ((xfrm_ctx
= x
->security
)) {
3185 ctx_size
= PFKEY_ALIGN8(xfrm_ctx
->ctx_len
);
3186 size
+= sizeof(struct sadb_x_sec_ctx
) + ctx_size
;
3189 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3193 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3194 hdr
->sadb_msg_version
= PF_KEY_V2
;
3195 hdr
->sadb_msg_type
= SADB_ACQUIRE
;
3196 hdr
->sadb_msg_satype
= pfkey_proto2satype(x
->id
.proto
);
3197 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3198 hdr
->sadb_msg_errno
= 0;
3199 hdr
->sadb_msg_reserved
= 0;
3200 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3201 hdr
->sadb_msg_pid
= 0;
3204 addr
= (struct sadb_address
*) skb_put(skb
,
3205 sizeof(struct sadb_address
)+sockaddr_size
);
3206 addr
->sadb_address_len
=
3207 (sizeof(struct sadb_address
)+sockaddr_size
)/
3209 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3210 addr
->sadb_address_proto
= 0;
3211 addr
->sadb_address_reserved
= 0;
3212 addr
->sadb_address_prefixlen
=
3213 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3214 (struct sockaddr
*) (addr
+ 1),
3216 if (!addr
->sadb_address_prefixlen
)
3220 addr
= (struct sadb_address
*) skb_put(skb
,
3221 sizeof(struct sadb_address
)+sockaddr_size
);
3222 addr
->sadb_address_len
=
3223 (sizeof(struct sadb_address
)+sockaddr_size
)/
3225 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3226 addr
->sadb_address_proto
= 0;
3227 addr
->sadb_address_reserved
= 0;
3228 addr
->sadb_address_prefixlen
=
3229 pfkey_sockaddr_fill(&x
->id
.daddr
, 0,
3230 (struct sockaddr
*) (addr
+ 1),
3232 if (!addr
->sadb_address_prefixlen
)
3235 pol
= (struct sadb_x_policy
*) skb_put(skb
, sizeof(struct sadb_x_policy
));
3236 pol
->sadb_x_policy_len
= sizeof(struct sadb_x_policy
)/sizeof(uint64_t);
3237 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3238 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3239 pol
->sadb_x_policy_dir
= XFRM_POLICY_OUT
+ 1;
3240 pol
->sadb_x_policy_reserved
= 0;
3241 pol
->sadb_x_policy_id
= xp
->index
;
3242 pol
->sadb_x_policy_priority
= xp
->priority
;
3244 /* Set sadb_comb's. */
3245 if (x
->id
.proto
== IPPROTO_AH
)
3246 dump_ah_combs(skb
, t
);
3247 else if (x
->id
.proto
== IPPROTO_ESP
)
3248 dump_esp_combs(skb
, t
);
3250 /* security context */
3252 sec_ctx
= (struct sadb_x_sec_ctx
*) skb_put(skb
,
3253 sizeof(struct sadb_x_sec_ctx
) + ctx_size
);
3254 sec_ctx
->sadb_x_sec_len
=
3255 (sizeof(struct sadb_x_sec_ctx
) + ctx_size
) / sizeof(uint64_t);
3256 sec_ctx
->sadb_x_sec_exttype
= SADB_X_EXT_SEC_CTX
;
3257 sec_ctx
->sadb_x_ctx_doi
= xfrm_ctx
->ctx_doi
;
3258 sec_ctx
->sadb_x_ctx_alg
= xfrm_ctx
->ctx_alg
;
3259 sec_ctx
->sadb_x_ctx_len
= xfrm_ctx
->ctx_len
;
3260 memcpy(sec_ctx
+ 1, xfrm_ctx
->ctx_str
,
3264 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3268 static struct xfrm_policy
*pfkey_compile_policy(struct sock
*sk
, int opt
,
3269 u8
*data
, int len
, int *dir
)
3271 struct net
*net
= sock_net(sk
);
3272 struct xfrm_policy
*xp
;
3273 struct sadb_x_policy
*pol
= (struct sadb_x_policy
*)data
;
3274 struct sadb_x_sec_ctx
*sec_ctx
;
3276 switch (sk
->sk_family
) {
3278 if (opt
!= IP_IPSEC_POLICY
) {
3283 #if IS_ENABLED(CONFIG_IPV6)
3285 if (opt
!= IPV6_IPSEC_POLICY
) {
3298 if (len
< sizeof(struct sadb_x_policy
) ||
3299 pol
->sadb_x_policy_len
*8 > len
||
3300 pol
->sadb_x_policy_type
> IPSEC_POLICY_BYPASS
||
3301 (!pol
->sadb_x_policy_dir
|| pol
->sadb_x_policy_dir
> IPSEC_DIR_OUTBOUND
))
3304 xp
= xfrm_policy_alloc(net
, GFP_ATOMIC
);
3310 xp
->action
= (pol
->sadb_x_policy_type
== IPSEC_POLICY_DISCARD
?
3311 XFRM_POLICY_BLOCK
: XFRM_POLICY_ALLOW
);
3313 xp
->lft
.soft_byte_limit
= XFRM_INF
;
3314 xp
->lft
.hard_byte_limit
= XFRM_INF
;
3315 xp
->lft
.soft_packet_limit
= XFRM_INF
;
3316 xp
->lft
.hard_packet_limit
= XFRM_INF
;
3317 xp
->family
= sk
->sk_family
;
3320 if (pol
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
&&
3321 (*dir
= parse_ipsecrequests(xp
, pol
)) < 0)
3324 /* security context too */
3325 if (len
>= (pol
->sadb_x_policy_len
*8 +
3326 sizeof(struct sadb_x_sec_ctx
))) {
3327 char *p
= (char *)pol
;
3328 struct xfrm_user_sec_ctx
*uctx
;
3330 p
+= pol
->sadb_x_policy_len
*8;
3331 sec_ctx
= (struct sadb_x_sec_ctx
*)p
;
3332 if (len
< pol
->sadb_x_policy_len
*8 +
3333 sec_ctx
->sadb_x_sec_len
*8) {
3337 if ((*dir
= verify_sec_ctx_len(p
)))
3339 uctx
= pfkey_sadb2xfrm_user_sec_ctx(sec_ctx
, GFP_ATOMIC
);
3340 *dir
= security_xfrm_policy_alloc(&xp
->security
, uctx
, GFP_ATOMIC
);
3347 *dir
= pol
->sadb_x_policy_dir
-1;
3352 xfrm_policy_destroy(xp
);
3356 static int pfkey_send_new_mapping(struct xfrm_state
*x
, xfrm_address_t
*ipaddr
, __be16 sport
)
3358 struct sk_buff
*skb
;
3359 struct sadb_msg
*hdr
;
3361 struct sadb_address
*addr
;
3362 struct sadb_x_nat_t_port
*n_port
;
3365 __u8 satype
= (x
->id
.proto
== IPPROTO_ESP
? SADB_SATYPE_ESP
: 0);
3366 struct xfrm_encap_tmpl
*natt
= NULL
;
3368 sockaddr_size
= pfkey_sockaddr_size(x
->props
.family
);
3380 /* Build an SADB_X_NAT_T_NEW_MAPPING message:
3382 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3383 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3386 size
= sizeof(struct sadb_msg
) +
3387 sizeof(struct sadb_sa
) +
3388 (sizeof(struct sadb_address
) * 2) +
3389 (sockaddr_size
* 2) +
3390 (sizeof(struct sadb_x_nat_t_port
) * 2);
3392 skb
= alloc_skb(size
+ 16, GFP_ATOMIC
);
3396 hdr
= (struct sadb_msg
*) skb_put(skb
, sizeof(struct sadb_msg
));
3397 hdr
->sadb_msg_version
= PF_KEY_V2
;
3398 hdr
->sadb_msg_type
= SADB_X_NAT_T_NEW_MAPPING
;
3399 hdr
->sadb_msg_satype
= satype
;
3400 hdr
->sadb_msg_len
= size
/ sizeof(uint64_t);
3401 hdr
->sadb_msg_errno
= 0;
3402 hdr
->sadb_msg_reserved
= 0;
3403 hdr
->sadb_msg_seq
= x
->km
.seq
= get_acqseq();
3404 hdr
->sadb_msg_pid
= 0;
3407 sa
= (struct sadb_sa
*) skb_put(skb
, sizeof(struct sadb_sa
));
3408 sa
->sadb_sa_len
= sizeof(struct sadb_sa
)/sizeof(uint64_t);
3409 sa
->sadb_sa_exttype
= SADB_EXT_SA
;
3410 sa
->sadb_sa_spi
= x
->id
.spi
;
3411 sa
->sadb_sa_replay
= 0;
3412 sa
->sadb_sa_state
= 0;
3413 sa
->sadb_sa_auth
= 0;
3414 sa
->sadb_sa_encrypt
= 0;
3415 sa
->sadb_sa_flags
= 0;
3417 /* ADDRESS_SRC (old addr) */
3418 addr
= (struct sadb_address
*)
3419 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3420 addr
->sadb_address_len
=
3421 (sizeof(struct sadb_address
)+sockaddr_size
)/
3423 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_SRC
;
3424 addr
->sadb_address_proto
= 0;
3425 addr
->sadb_address_reserved
= 0;
3426 addr
->sadb_address_prefixlen
=
3427 pfkey_sockaddr_fill(&x
->props
.saddr
, 0,
3428 (struct sockaddr
*) (addr
+ 1),
3430 if (!addr
->sadb_address_prefixlen
)
3433 /* NAT_T_SPORT (old port) */
3434 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3435 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3436 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_SPORT
;
3437 n_port
->sadb_x_nat_t_port_port
= natt
->encap_sport
;
3438 n_port
->sadb_x_nat_t_port_reserved
= 0;
3440 /* ADDRESS_DST (new addr) */
3441 addr
= (struct sadb_address
*)
3442 skb_put(skb
, sizeof(struct sadb_address
)+sockaddr_size
);
3443 addr
->sadb_address_len
=
3444 (sizeof(struct sadb_address
)+sockaddr_size
)/
3446 addr
->sadb_address_exttype
= SADB_EXT_ADDRESS_DST
;
3447 addr
->sadb_address_proto
= 0;
3448 addr
->sadb_address_reserved
= 0;
3449 addr
->sadb_address_prefixlen
=
3450 pfkey_sockaddr_fill(ipaddr
, 0,
3451 (struct sockaddr
*) (addr
+ 1),
3453 if (!addr
->sadb_address_prefixlen
)
3456 /* NAT_T_DPORT (new port) */
3457 n_port
= (struct sadb_x_nat_t_port
*) skb_put(skb
, sizeof (*n_port
));
3458 n_port
->sadb_x_nat_t_port_len
= sizeof(*n_port
)/sizeof(uint64_t);
3459 n_port
->sadb_x_nat_t_port_exttype
= SADB_X_EXT_NAT_T_DPORT
;
3460 n_port
->sadb_x_nat_t_port_port
= sport
;
3461 n_port
->sadb_x_nat_t_port_reserved
= 0;
3463 return pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_REGISTERED
, NULL
,
3467 #ifdef CONFIG_NET_KEY_MIGRATE
3468 static int set_sadb_address(struct sk_buff
*skb
, int sasize
, int type
,
3469 const struct xfrm_selector
*sel
)
3471 struct sadb_address
*addr
;
3472 addr
= (struct sadb_address
*)skb_put(skb
, sizeof(struct sadb_address
) + sasize
);
3473 addr
->sadb_address_len
= (sizeof(struct sadb_address
) + sasize
)/8;
3474 addr
->sadb_address_exttype
= type
;
3475 addr
->sadb_address_proto
= sel
->proto
;
3476 addr
->sadb_address_reserved
= 0;
3479 case SADB_EXT_ADDRESS_SRC
:
3480 addr
->sadb_address_prefixlen
= sel
->prefixlen_s
;
3481 pfkey_sockaddr_fill(&sel
->saddr
, 0,
3482 (struct sockaddr
*)(addr
+ 1),
3485 case SADB_EXT_ADDRESS_DST
:
3486 addr
->sadb_address_prefixlen
= sel
->prefixlen_d
;
3487 pfkey_sockaddr_fill(&sel
->daddr
, 0,
3488 (struct sockaddr
*)(addr
+ 1),
3499 static int set_sadb_kmaddress(struct sk_buff
*skb
, const struct xfrm_kmaddress
*k
)
3501 struct sadb_x_kmaddress
*kma
;
3503 int family
= k
->family
;
3504 int socklen
= pfkey_sockaddr_len(family
);
3507 size_req
= (sizeof(struct sadb_x_kmaddress
) +
3508 pfkey_sockaddr_pair_size(family
));
3510 kma
= (struct sadb_x_kmaddress
*)skb_put(skb
, size_req
);
3511 memset(kma
, 0, size_req
);
3512 kma
->sadb_x_kmaddress_len
= size_req
/ 8;
3513 kma
->sadb_x_kmaddress_exttype
= SADB_X_EXT_KMADDRESS
;
3514 kma
->sadb_x_kmaddress_reserved
= k
->reserved
;
3516 sa
= (u8
*)(kma
+ 1);
3517 if (!pfkey_sockaddr_fill(&k
->local
, 0, (struct sockaddr
*)sa
, family
) ||
3518 !pfkey_sockaddr_fill(&k
->remote
, 0, (struct sockaddr
*)(sa
+socklen
), family
))
3524 static int set_ipsecrequest(struct sk_buff
*skb
,
3525 uint8_t proto
, uint8_t mode
, int level
,
3526 uint32_t reqid
, uint8_t family
,
3527 const xfrm_address_t
*src
, const xfrm_address_t
*dst
)
3529 struct sadb_x_ipsecrequest
*rq
;
3531 int socklen
= pfkey_sockaddr_len(family
);
3534 size_req
= sizeof(struct sadb_x_ipsecrequest
) +
3535 pfkey_sockaddr_pair_size(family
);
3537 rq
= (struct sadb_x_ipsecrequest
*)skb_put(skb
, size_req
);
3538 memset(rq
, 0, size_req
);
3539 rq
->sadb_x_ipsecrequest_len
= size_req
;
3540 rq
->sadb_x_ipsecrequest_proto
= proto
;
3541 rq
->sadb_x_ipsecrequest_mode
= mode
;
3542 rq
->sadb_x_ipsecrequest_level
= level
;
3543 rq
->sadb_x_ipsecrequest_reqid
= reqid
;
3545 sa
= (u8
*) (rq
+ 1);
3546 if (!pfkey_sockaddr_fill(src
, 0, (struct sockaddr
*)sa
, family
) ||
3547 !pfkey_sockaddr_fill(dst
, 0, (struct sockaddr
*)(sa
+ socklen
), family
))
3554 #ifdef CONFIG_NET_KEY_MIGRATE
3555 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3556 const struct xfrm_migrate
*m
, int num_bundles
,
3557 const struct xfrm_kmaddress
*k
)
3563 struct sk_buff
*skb
;
3564 struct sadb_msg
*hdr
;
3565 struct sadb_x_policy
*pol
;
3566 const struct xfrm_migrate
*mp
;
3568 if (type
!= XFRM_POLICY_TYPE_MAIN
)
3571 if (num_bundles
<= 0 || num_bundles
> XFRM_MAX_DEPTH
)
3575 /* addresses for KM */
3576 size
+= PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress
) +
3577 pfkey_sockaddr_pair_size(k
->family
));
3581 sasize_sel
= pfkey_sockaddr_size(sel
->family
);
3584 size
+= (sizeof(struct sadb_address
) + sasize_sel
) * 2;
3587 size_pol
+= sizeof(struct sadb_x_policy
);
3590 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3591 /* old locator pair */
3592 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3593 pfkey_sockaddr_pair_size(mp
->old_family
);
3594 /* new locator pair */
3595 size_pol
+= sizeof(struct sadb_x_ipsecrequest
) +
3596 pfkey_sockaddr_pair_size(mp
->new_family
);
3599 size
+= sizeof(struct sadb_msg
) + size_pol
;
3602 skb
= alloc_skb(size
, GFP_ATOMIC
);
3606 hdr
= (struct sadb_msg
*)skb_put(skb
, sizeof(struct sadb_msg
));
3607 hdr
->sadb_msg_version
= PF_KEY_V2
;
3608 hdr
->sadb_msg_type
= SADB_X_MIGRATE
;
3609 hdr
->sadb_msg_satype
= pfkey_proto2satype(m
->proto
);
3610 hdr
->sadb_msg_len
= size
/ 8;
3611 hdr
->sadb_msg_errno
= 0;
3612 hdr
->sadb_msg_reserved
= 0;
3613 hdr
->sadb_msg_seq
= 0;
3614 hdr
->sadb_msg_pid
= 0;
3616 /* Addresses to be used by KM for negotiation, if ext is available */
3617 if (k
!= NULL
&& (set_sadb_kmaddress(skb
, k
) < 0))
3621 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_SRC
, sel
);
3624 set_sadb_address(skb
, sasize_sel
, SADB_EXT_ADDRESS_DST
, sel
);
3626 /* policy information */
3627 pol
= (struct sadb_x_policy
*)skb_put(skb
, sizeof(struct sadb_x_policy
));
3628 pol
->sadb_x_policy_len
= size_pol
/ 8;
3629 pol
->sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
3630 pol
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
3631 pol
->sadb_x_policy_dir
= dir
+ 1;
3632 pol
->sadb_x_policy_reserved
= 0;
3633 pol
->sadb_x_policy_id
= 0;
3634 pol
->sadb_x_policy_priority
= 0;
3636 for (i
= 0, mp
= m
; i
< num_bundles
; i
++, mp
++) {
3637 /* old ipsecrequest */
3638 int mode
= pfkey_mode_from_xfrm(mp
->mode
);
3641 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3642 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3643 mp
->reqid
, mp
->old_family
,
3644 &mp
->old_saddr
, &mp
->old_daddr
) < 0)
3647 /* new ipsecrequest */
3648 if (set_ipsecrequest(skb
, mp
->proto
, mode
,
3649 (mp
->reqid
? IPSEC_LEVEL_UNIQUE
: IPSEC_LEVEL_REQUIRE
),
3650 mp
->reqid
, mp
->new_family
,
3651 &mp
->new_saddr
, &mp
->new_daddr
) < 0)
3655 /* broadcast migrate message to sockets */
3656 pfkey_broadcast(skb
, GFP_ATOMIC
, BROADCAST_ALL
, NULL
, &init_net
);
3665 static int pfkey_send_migrate(const struct xfrm_selector
*sel
, u8 dir
, u8 type
,
3666 const struct xfrm_migrate
*m
, int num_bundles
,
3667 const struct xfrm_kmaddress
*k
)
3669 return -ENOPROTOOPT
;
3673 static int pfkey_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
3675 struct sock
*sk
= sock
->sk
;
3676 struct sk_buff
*skb
= NULL
;
3677 struct sadb_msg
*hdr
= NULL
;
3679 struct net
*net
= sock_net(sk
);
3682 if (msg
->msg_flags
& MSG_OOB
)
3686 if ((unsigned int)len
> sk
->sk_sndbuf
- 32)
3690 skb
= alloc_skb(len
, GFP_KERNEL
);
3695 if (memcpy_from_msg(skb_put(skb
,len
), msg
, len
))
3698 hdr
= pfkey_get_base_msg(skb
, &err
);
3702 mutex_lock(&net
->xfrm
.xfrm_cfg_mutex
);
3703 err
= pfkey_process(sk
, skb
, hdr
);
3704 mutex_unlock(&net
->xfrm
.xfrm_cfg_mutex
);
3707 if (err
&& hdr
&& pfkey_error(hdr
, err
, sk
) == 0)
3714 static int pfkey_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
3717 struct sock
*sk
= sock
->sk
;
3718 struct pfkey_sock
*pfk
= pfkey_sk(sk
);
3719 struct sk_buff
*skb
;
3723 if (flags
& ~(MSG_PEEK
|MSG_DONTWAIT
|MSG_TRUNC
|MSG_CMSG_COMPAT
))
3726 skb
= skb_recv_datagram(sk
, flags
, flags
& MSG_DONTWAIT
, &err
);
3732 msg
->msg_flags
|= MSG_TRUNC
;
3736 skb_reset_transport_header(skb
);
3737 err
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
3741 sock_recv_ts_and_drops(msg
, sk
, skb
);
3743 err
= (flags
& MSG_TRUNC
) ? skb
->len
: copied
;
3745 if (pfk
->dump
.dump
!= NULL
&&
3746 3 * atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
)
3750 skb_free_datagram(sk
, skb
);
3755 static const struct proto_ops pfkey_ops
= {
3757 .owner
= THIS_MODULE
,
3758 /* Operations that make no sense on pfkey sockets. */
3759 .bind
= sock_no_bind
,
3760 .connect
= sock_no_connect
,
3761 .socketpair
= sock_no_socketpair
,
3762 .accept
= sock_no_accept
,
3763 .getname
= sock_no_getname
,
3764 .ioctl
= sock_no_ioctl
,
3765 .listen
= sock_no_listen
,
3766 .shutdown
= sock_no_shutdown
,
3767 .setsockopt
= sock_no_setsockopt
,
3768 .getsockopt
= sock_no_getsockopt
,
3769 .mmap
= sock_no_mmap
,
3770 .sendpage
= sock_no_sendpage
,
3772 /* Now the operations that really occur. */
3773 .release
= pfkey_release
,
3774 .poll
= datagram_poll
,
3775 .sendmsg
= pfkey_sendmsg
,
3776 .recvmsg
= pfkey_recvmsg
,
3779 static const struct net_proto_family pfkey_family_ops
= {
3781 .create
= pfkey_create
,
3782 .owner
= THIS_MODULE
,
3785 #ifdef CONFIG_PROC_FS
3786 static int pfkey_seq_show(struct seq_file
*f
, void *v
)
3788 struct sock
*s
= sk_entry(v
);
3790 if (v
== SEQ_START_TOKEN
)
3791 seq_printf(f
,"sk RefCnt Rmem Wmem User Inode\n");
3793 seq_printf(f
, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3795 atomic_read(&s
->sk_refcnt
),
3796 sk_rmem_alloc_get(s
),
3797 sk_wmem_alloc_get(s
),
3798 from_kuid_munged(seq_user_ns(f
), sock_i_uid(s
)),
3804 static void *pfkey_seq_start(struct seq_file
*f
, loff_t
*ppos
)
3807 struct net
*net
= seq_file_net(f
);
3808 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3811 return seq_hlist_start_head_rcu(&net_pfkey
->table
, *ppos
);
3814 static void *pfkey_seq_next(struct seq_file
*f
, void *v
, loff_t
*ppos
)
3816 struct net
*net
= seq_file_net(f
);
3817 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3819 return seq_hlist_next_rcu(v
, &net_pfkey
->table
, ppos
);
3822 static void pfkey_seq_stop(struct seq_file
*f
, void *v
)
3828 static const struct seq_operations pfkey_seq_ops
= {
3829 .start
= pfkey_seq_start
,
3830 .next
= pfkey_seq_next
,
3831 .stop
= pfkey_seq_stop
,
3832 .show
= pfkey_seq_show
,
3835 static int pfkey_seq_open(struct inode
*inode
, struct file
*file
)
3837 return seq_open_net(inode
, file
, &pfkey_seq_ops
,
3838 sizeof(struct seq_net_private
));
3841 static const struct file_operations pfkey_proc_ops
= {
3842 .open
= pfkey_seq_open
,
3844 .llseek
= seq_lseek
,
3845 .release
= seq_release_net
,
3848 static int __net_init
pfkey_init_proc(struct net
*net
)
3850 struct proc_dir_entry
*e
;
3852 e
= proc_create("pfkey", 0, net
->proc_net
, &pfkey_proc_ops
);
3859 static void __net_exit
pfkey_exit_proc(struct net
*net
)
3861 remove_proc_entry("pfkey", net
->proc_net
);
3864 static inline int pfkey_init_proc(struct net
*net
)
3869 static inline void pfkey_exit_proc(struct net
*net
)
3874 static struct xfrm_mgr pfkeyv2_mgr
=
3877 .notify
= pfkey_send_notify
,
3878 .acquire
= pfkey_send_acquire
,
3879 .compile_policy
= pfkey_compile_policy
,
3880 .new_mapping
= pfkey_send_new_mapping
,
3881 .notify_policy
= pfkey_send_policy_notify
,
3882 .migrate
= pfkey_send_migrate
,
3883 .is_alive
= pfkey_is_alive
,
3886 static int __net_init
pfkey_net_init(struct net
*net
)
3888 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3891 INIT_HLIST_HEAD(&net_pfkey
->table
);
3892 atomic_set(&net_pfkey
->socks_nr
, 0);
3894 rv
= pfkey_init_proc(net
);
3899 static void __net_exit
pfkey_net_exit(struct net
*net
)
3901 struct netns_pfkey
*net_pfkey
= net_generic(net
, pfkey_net_id
);
3903 pfkey_exit_proc(net
);
3904 BUG_ON(!hlist_empty(&net_pfkey
->table
));
3907 static struct pernet_operations pfkey_net_ops
= {
3908 .init
= pfkey_net_init
,
3909 .exit
= pfkey_net_exit
,
3910 .id
= &pfkey_net_id
,
3911 .size
= sizeof(struct netns_pfkey
),
3914 static void __exit
ipsec_pfkey_exit(void)
3916 xfrm_unregister_km(&pfkeyv2_mgr
);
3917 sock_unregister(PF_KEY
);
3918 unregister_pernet_subsys(&pfkey_net_ops
);
3919 proto_unregister(&key_proto
);
3922 static int __init
ipsec_pfkey_init(void)
3924 int err
= proto_register(&key_proto
, 0);
3929 err
= register_pernet_subsys(&pfkey_net_ops
);
3931 goto out_unregister_key_proto
;
3932 err
= sock_register(&pfkey_family_ops
);
3934 goto out_unregister_pernet
;
3935 err
= xfrm_register_km(&pfkeyv2_mgr
);
3937 goto out_sock_unregister
;
3941 out_sock_unregister
:
3942 sock_unregister(PF_KEY
);
3943 out_unregister_pernet
:
3944 unregister_pernet_subsys(&pfkey_net_ops
);
3945 out_unregister_key_proto
:
3946 proto_unregister(&key_proto
);
3950 module_init(ipsec_pfkey_init
);
3951 module_exit(ipsec_pfkey_exit
);
3952 MODULE_LICENSE("GPL");
3953 MODULE_ALIAS_NETPROTO(PF_KEY
);