xref: /openbmc/linux/net/key/af_key.c (revision 60772e48)
1 /*
2  * net/key/af_key.c	An implementation of PF_KEYv2 sockets.
3  *
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.
8  *
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>
15  */
16 
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>
25 #include <linux/in.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>
32 #include <net/xfrm.h>
33 
34 #include <net/sock.h>
35 
36 #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
37 #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
38 
39 static unsigned int pfkey_net_id __read_mostly;
40 struct netns_pfkey {
41 	/* List of all pfkey sockets. */
42 	struct hlist_head table;
43 	atomic_t socks_nr;
44 };
45 static DEFINE_MUTEX(pfkey_mutex);
46 
47 #define DUMMY_MARK 0
48 static const struct xfrm_mark dummy_mark = {0, 0};
49 struct pfkey_sock {
50 	/* struct sock must be the first member of struct pfkey_sock */
51 	struct sock	sk;
52 	int		registered;
53 	int		promisc;
54 
55 	struct {
56 		uint8_t		msg_version;
57 		uint32_t	msg_portid;
58 		int		(*dump)(struct pfkey_sock *sk);
59 		void		(*done)(struct pfkey_sock *sk);
60 		union {
61 			struct xfrm_policy_walk	policy;
62 			struct xfrm_state_walk	state;
63 		} u;
64 		struct sk_buff	*skb;
65 	} dump;
66 	struct mutex dump_lock;
67 };
68 
69 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
70 			       xfrm_address_t *saddr, xfrm_address_t *daddr,
71 			       u16 *family);
72 
73 static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
74 {
75 	return (struct pfkey_sock *)sk;
76 }
77 
78 static int pfkey_can_dump(const struct sock *sk)
79 {
80 	if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
81 		return 1;
82 	return 0;
83 }
84 
85 static void pfkey_terminate_dump(struct pfkey_sock *pfk)
86 {
87 	if (pfk->dump.dump) {
88 		if (pfk->dump.skb) {
89 			kfree_skb(pfk->dump.skb);
90 			pfk->dump.skb = NULL;
91 		}
92 		pfk->dump.done(pfk);
93 		pfk->dump.dump = NULL;
94 		pfk->dump.done = NULL;
95 	}
96 }
97 
98 static void pfkey_sock_destruct(struct sock *sk)
99 {
100 	struct net *net = sock_net(sk);
101 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
102 
103 	pfkey_terminate_dump(pfkey_sk(sk));
104 	skb_queue_purge(&sk->sk_receive_queue);
105 
106 	if (!sock_flag(sk, SOCK_DEAD)) {
107 		pr_err("Attempt to release alive pfkey socket: %p\n", sk);
108 		return;
109 	}
110 
111 	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
112 	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
113 
114 	atomic_dec(&net_pfkey->socks_nr);
115 }
116 
117 static const struct proto_ops pfkey_ops;
118 
119 static void pfkey_insert(struct sock *sk)
120 {
121 	struct net *net = sock_net(sk);
122 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
123 
124 	mutex_lock(&pfkey_mutex);
125 	sk_add_node_rcu(sk, &net_pfkey->table);
126 	mutex_unlock(&pfkey_mutex);
127 }
128 
129 static void pfkey_remove(struct sock *sk)
130 {
131 	mutex_lock(&pfkey_mutex);
132 	sk_del_node_init_rcu(sk);
133 	mutex_unlock(&pfkey_mutex);
134 }
135 
136 static struct proto key_proto = {
137 	.name	  = "KEY",
138 	.owner	  = THIS_MODULE,
139 	.obj_size = sizeof(struct pfkey_sock),
140 };
141 
142 static int pfkey_create(struct net *net, struct socket *sock, int protocol,
143 			int kern)
144 {
145 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
146 	struct sock *sk;
147 	struct pfkey_sock *pfk;
148 	int err;
149 
150 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
151 		return -EPERM;
152 	if (sock->type != SOCK_RAW)
153 		return -ESOCKTNOSUPPORT;
154 	if (protocol != PF_KEY_V2)
155 		return -EPROTONOSUPPORT;
156 
157 	err = -ENOMEM;
158 	sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
159 	if (sk == NULL)
160 		goto out;
161 
162 	pfk = pfkey_sk(sk);
163 	mutex_init(&pfk->dump_lock);
164 
165 	sock->ops = &pfkey_ops;
166 	sock_init_data(sock, sk);
167 
168 	sk->sk_family = PF_KEY;
169 	sk->sk_destruct = pfkey_sock_destruct;
170 
171 	atomic_inc(&net_pfkey->socks_nr);
172 
173 	pfkey_insert(sk);
174 
175 	return 0;
176 out:
177 	return err;
178 }
179 
180 static int pfkey_release(struct socket *sock)
181 {
182 	struct sock *sk = sock->sk;
183 
184 	if (!sk)
185 		return 0;
186 
187 	pfkey_remove(sk);
188 
189 	sock_orphan(sk);
190 	sock->sk = NULL;
191 	skb_queue_purge(&sk->sk_write_queue);
192 
193 	synchronize_rcu();
194 	sock_put(sk);
195 
196 	return 0;
197 }
198 
199 static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
200 			       gfp_t allocation, struct sock *sk)
201 {
202 	int err = -ENOBUFS;
203 
204 	sock_hold(sk);
205 	if (*skb2 == NULL) {
206 		if (refcount_read(&skb->users) != 1) {
207 			*skb2 = skb_clone(skb, allocation);
208 		} else {
209 			*skb2 = skb;
210 			refcount_inc(&skb->users);
211 		}
212 	}
213 	if (*skb2 != NULL) {
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);
218 			*skb2 = NULL;
219 			err = 0;
220 		}
221 	}
222 	sock_put(sk);
223 	return err;
224 }
225 
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,
233 			   struct net *net)
234 {
235 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
236 	struct sock *sk;
237 	struct sk_buff *skb2 = NULL;
238 	int err = -ESRCH;
239 
240 	/* XXX Do we need something like netlink_overrun?  I think
241 	 * XXX PF_KEY socket apps will not mind current behavior.
242 	 */
243 	if (!skb)
244 		return -ENOMEM;
245 
246 	rcu_read_lock();
247 	sk_for_each_rcu(sk, &net_pfkey->table) {
248 		struct pfkey_sock *pfk = pfkey_sk(sk);
249 		int err2;
250 
251 		/* Yes, it means that if you are meant to receive this
252 		 * pfkey message you receive it twice as promiscuous
253 		 * socket.
254 		 */
255 		if (pfk->promisc)
256 			pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
257 
258 		/* the exact target will be processed later */
259 		if (sk == one_sk)
260 			continue;
261 		if (broadcast_flags != BROADCAST_ALL) {
262 			if (broadcast_flags & BROADCAST_PROMISC_ONLY)
263 				continue;
264 			if ((broadcast_flags & BROADCAST_REGISTERED) &&
265 			    !pfk->registered)
266 				continue;
267 			if (broadcast_flags & BROADCAST_ONE)
268 				continue;
269 		}
270 
271 		err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
272 
273 		/* Error is cleared after successful sending to at least one
274 		 * registered KM */
275 		if ((broadcast_flags & BROADCAST_REGISTERED) && err)
276 			err = err2;
277 	}
278 	rcu_read_unlock();
279 
280 	if (one_sk != NULL)
281 		err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
282 
283 	kfree_skb(skb2);
284 	kfree_skb(skb);
285 	return err;
286 }
287 
288 static int pfkey_do_dump(struct pfkey_sock *pfk)
289 {
290 	struct sadb_msg *hdr;
291 	int rc;
292 
293 	mutex_lock(&pfk->dump_lock);
294 	if (!pfk->dump.dump) {
295 		rc = 0;
296 		goto out;
297 	}
298 
299 	rc = pfk->dump.dump(pfk);
300 	if (rc == -ENOBUFS) {
301 		rc = 0;
302 		goto out;
303 	}
304 
305 	if (pfk->dump.skb) {
306 		if (!pfkey_can_dump(&pfk->sk)) {
307 			rc = 0;
308 			goto out;
309 		}
310 
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;
317 	}
318 
319 	pfkey_terminate_dump(pfk);
320 
321 out:
322 	mutex_unlock(&pfk->dump_lock);
323 	return rc;
324 }
325 
326 static inline void pfkey_hdr_dup(struct sadb_msg *new,
327 				 const struct sadb_msg *orig)
328 {
329 	*new = *orig;
330 }
331 
332 static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
333 {
334 	struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
335 	struct sadb_msg *hdr;
336 
337 	if (!skb)
338 		return -ENOBUFS;
339 
340 	/* Woe be to the platform trying to support PFKEY yet
341 	 * having normal errnos outside the 1-255 range, inclusive.
342 	 */
343 	err = -err;
344 	if (err == ERESTARTSYS ||
345 	    err == ERESTARTNOHAND ||
346 	    err == ERESTARTNOINTR)
347 		err = EINTR;
348 	if (err >= 512)
349 		err = EINVAL;
350 	BUG_ON(err <= 0 || err >= 256);
351 
352 	hdr = 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) /
356 			     sizeof(uint64_t));
357 
358 	pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
359 
360 	return 0;
361 }
362 
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),
391 };
392 
393 /* Verify sadb_address_{len,prefixlen} against sa_family.  */
394 static int verify_address_len(const void *p)
395 {
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;
401 #endif
402 	int len;
403 
404 	if (sp->sadb_address_len <
405 	    DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family),
406 			 sizeof(uint64_t)))
407 		return -EINVAL;
408 
409 	switch (addr->sa_family) {
410 	case AF_INET:
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)
414 			return -EINVAL;
415 		break;
416 #if IS_ENABLED(CONFIG_IPV6)
417 	case AF_INET6:
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)
421 			return -EINVAL;
422 		break;
423 #endif
424 	default:
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
428 		 * lengths.
429 		 *
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
433 		 */
434 		return -EINVAL;
435 	}
436 
437 	return 0;
438 }
439 
440 static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
441 {
442 	return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
443 			    sec_ctx->sadb_x_ctx_len,
444 			    sizeof(uint64_t));
445 }
446 
447 static inline int verify_sec_ctx_len(const void *p)
448 {
449 	const struct sadb_x_sec_ctx *sec_ctx = p;
450 	int len = sec_ctx->sadb_x_ctx_len;
451 
452 	if (len > PAGE_SIZE)
453 		return -EINVAL;
454 
455 	len = pfkey_sec_ctx_len(sec_ctx);
456 
457 	if (sec_ctx->sadb_x_sec_len != len)
458 		return -EINVAL;
459 
460 	return 0;
461 }
462 
463 static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
464 								     gfp_t gfp)
465 {
466 	struct xfrm_user_sec_ctx *uctx = NULL;
467 	int ctx_size = sec_ctx->sadb_x_ctx_len;
468 
469 	uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
470 
471 	if (!uctx)
472 		return NULL;
473 
474 	uctx->len = pfkey_sec_ctx_len(sec_ctx);
475 	uctx->exttype = sec_ctx->sadb_x_sec_exttype;
476 	uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
477 	uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
478 	uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
479 	memcpy(uctx + 1, sec_ctx + 1,
480 	       uctx->ctx_len);
481 
482 	return uctx;
483 }
484 
485 static int present_and_same_family(const struct sadb_address *src,
486 				   const struct sadb_address *dst)
487 {
488 	const struct sockaddr *s_addr, *d_addr;
489 
490 	if (!src || !dst)
491 		return 0;
492 
493 	s_addr = (const struct sockaddr *)(src + 1);
494 	d_addr = (const struct sockaddr *)(dst + 1);
495 	if (s_addr->sa_family != d_addr->sa_family)
496 		return 0;
497 	if (s_addr->sa_family != AF_INET
498 #if IS_ENABLED(CONFIG_IPV6)
499 	    && s_addr->sa_family != AF_INET6
500 #endif
501 		)
502 		return 0;
503 
504 	return 1;
505 }
506 
507 static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
508 {
509 	const char *p = (char *) hdr;
510 	int len = skb->len;
511 
512 	len -= sizeof(*hdr);
513 	p += sizeof(*hdr);
514 	while (len > 0) {
515 		const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
516 		uint16_t ext_type;
517 		int ext_len;
518 
519 		if (len < sizeof(*ehdr))
520 			return -EINVAL;
521 
522 		ext_len  = ehdr->sadb_ext_len;
523 		ext_len *= sizeof(uint64_t);
524 		ext_type = ehdr->sadb_ext_type;
525 		if (ext_len < sizeof(uint64_t) ||
526 		    ext_len > len ||
527 		    ext_type == SADB_EXT_RESERVED)
528 			return -EINVAL;
529 
530 		if (ext_type <= SADB_EXT_MAX) {
531 			int min = (int) sadb_ext_min_len[ext_type];
532 			if (ext_len < min)
533 				return -EINVAL;
534 			if (ext_hdrs[ext_type-1] != NULL)
535 				return -EINVAL;
536 			if (ext_type == SADB_EXT_ADDRESS_SRC ||
537 			    ext_type == SADB_EXT_ADDRESS_DST ||
538 			    ext_type == SADB_EXT_ADDRESS_PROXY ||
539 			    ext_type == SADB_X_EXT_NAT_T_OA) {
540 				if (verify_address_len(p))
541 					return -EINVAL;
542 			}
543 			if (ext_type == SADB_X_EXT_SEC_CTX) {
544 				if (verify_sec_ctx_len(p))
545 					return -EINVAL;
546 			}
547 			ext_hdrs[ext_type-1] = (void *) p;
548 		}
549 		p   += ext_len;
550 		len -= ext_len;
551 	}
552 
553 	return 0;
554 }
555 
556 static uint16_t
557 pfkey_satype2proto(uint8_t satype)
558 {
559 	switch (satype) {
560 	case SADB_SATYPE_UNSPEC:
561 		return IPSEC_PROTO_ANY;
562 	case SADB_SATYPE_AH:
563 		return IPPROTO_AH;
564 	case SADB_SATYPE_ESP:
565 		return IPPROTO_ESP;
566 	case SADB_X_SATYPE_IPCOMP:
567 		return IPPROTO_COMP;
568 	default:
569 		return 0;
570 	}
571 	/* NOTREACHED */
572 }
573 
574 static uint8_t
575 pfkey_proto2satype(uint16_t proto)
576 {
577 	switch (proto) {
578 	case IPPROTO_AH:
579 		return SADB_SATYPE_AH;
580 	case IPPROTO_ESP:
581 		return SADB_SATYPE_ESP;
582 	case IPPROTO_COMP:
583 		return SADB_X_SATYPE_IPCOMP;
584 	default:
585 		return 0;
586 	}
587 	/* NOTREACHED */
588 }
589 
590 /* BTW, this scheme means that there is no way with PFKEY2 sockets to
591  * say specifically 'just raw sockets' as we encode them as 255.
592  */
593 
594 static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
595 {
596 	return proto == IPSEC_PROTO_ANY ? 0 : proto;
597 }
598 
599 static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
600 {
601 	return proto ? proto : IPSEC_PROTO_ANY;
602 }
603 
604 static inline int pfkey_sockaddr_len(sa_family_t family)
605 {
606 	switch (family) {
607 	case AF_INET:
608 		return sizeof(struct sockaddr_in);
609 #if IS_ENABLED(CONFIG_IPV6)
610 	case AF_INET6:
611 		return sizeof(struct sockaddr_in6);
612 #endif
613 	}
614 	return 0;
615 }
616 
617 static
618 int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
619 {
620 	switch (sa->sa_family) {
621 	case AF_INET:
622 		xaddr->a4 =
623 			((struct sockaddr_in *)sa)->sin_addr.s_addr;
624 		return AF_INET;
625 #if IS_ENABLED(CONFIG_IPV6)
626 	case AF_INET6:
627 		memcpy(xaddr->a6,
628 		       &((struct sockaddr_in6 *)sa)->sin6_addr,
629 		       sizeof(struct in6_addr));
630 		return AF_INET6;
631 #endif
632 	}
633 	return 0;
634 }
635 
636 static
637 int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
638 {
639 	return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
640 				      xaddr);
641 }
642 
643 static struct  xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
644 {
645 	const struct sadb_sa *sa;
646 	const struct sadb_address *addr;
647 	uint16_t proto;
648 	unsigned short family;
649 	xfrm_address_t *xaddr;
650 
651 	sa = ext_hdrs[SADB_EXT_SA - 1];
652 	if (sa == NULL)
653 		return NULL;
654 
655 	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
656 	if (proto == 0)
657 		return NULL;
658 
659 	/* sadb_address_len should be checked by caller */
660 	addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
661 	if (addr == NULL)
662 		return NULL;
663 
664 	family = ((const struct sockaddr *)(addr + 1))->sa_family;
665 	switch (family) {
666 	case AF_INET:
667 		xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
668 		break;
669 #if IS_ENABLED(CONFIG_IPV6)
670 	case AF_INET6:
671 		xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
672 		break;
673 #endif
674 	default:
675 		xaddr = NULL;
676 	}
677 
678 	if (!xaddr)
679 		return NULL;
680 
681 	return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
682 }
683 
684 #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
685 
686 static int
687 pfkey_sockaddr_size(sa_family_t family)
688 {
689 	return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
690 }
691 
692 static inline int pfkey_mode_from_xfrm(int mode)
693 {
694 	switch(mode) {
695 	case XFRM_MODE_TRANSPORT:
696 		return IPSEC_MODE_TRANSPORT;
697 	case XFRM_MODE_TUNNEL:
698 		return IPSEC_MODE_TUNNEL;
699 	case XFRM_MODE_BEET:
700 		return IPSEC_MODE_BEET;
701 	default:
702 		return -1;
703 	}
704 }
705 
706 static inline int pfkey_mode_to_xfrm(int mode)
707 {
708 	switch(mode) {
709 	case IPSEC_MODE_ANY:	/*XXX*/
710 	case IPSEC_MODE_TRANSPORT:
711 		return XFRM_MODE_TRANSPORT;
712 	case IPSEC_MODE_TUNNEL:
713 		return XFRM_MODE_TUNNEL;
714 	case IPSEC_MODE_BEET:
715 		return XFRM_MODE_BEET;
716 	default:
717 		return -1;
718 	}
719 }
720 
721 static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
722 					struct sockaddr *sa,
723 					unsigned short family)
724 {
725 	switch (family) {
726 	case AF_INET:
727 	    {
728 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
729 		sin->sin_family = AF_INET;
730 		sin->sin_port = port;
731 		sin->sin_addr.s_addr = xaddr->a4;
732 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
733 		return 32;
734 	    }
735 #if IS_ENABLED(CONFIG_IPV6)
736 	case AF_INET6:
737 	    {
738 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
739 		sin6->sin6_family = AF_INET6;
740 		sin6->sin6_port = port;
741 		sin6->sin6_flowinfo = 0;
742 		sin6->sin6_addr = xaddr->in6;
743 		sin6->sin6_scope_id = 0;
744 		return 128;
745 	    }
746 #endif
747 	}
748 	return 0;
749 }
750 
751 static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
752 					      int add_keys, int hsc)
753 {
754 	struct sk_buff *skb;
755 	struct sadb_msg *hdr;
756 	struct sadb_sa *sa;
757 	struct sadb_lifetime *lifetime;
758 	struct sadb_address *addr;
759 	struct sadb_key *key;
760 	struct sadb_x_sa2 *sa2;
761 	struct sadb_x_sec_ctx *sec_ctx;
762 	struct xfrm_sec_ctx *xfrm_ctx;
763 	int ctx_size = 0;
764 	int size;
765 	int auth_key_size = 0;
766 	int encrypt_key_size = 0;
767 	int sockaddr_size;
768 	struct xfrm_encap_tmpl *natt = NULL;
769 	int mode;
770 
771 	/* address family check */
772 	sockaddr_size = pfkey_sockaddr_size(x->props.family);
773 	if (!sockaddr_size)
774 		return ERR_PTR(-EINVAL);
775 
776 	/* base, SA, (lifetime (HSC),) address(SD), (address(P),)
777 	   key(AE), (identity(SD),) (sensitivity)> */
778 	size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
779 		sizeof(struct sadb_lifetime) +
780 		((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
781 		((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
782 			sizeof(struct sadb_address)*2 +
783 				sockaddr_size*2 +
784 					sizeof(struct sadb_x_sa2);
785 
786 	if ((xfrm_ctx = x->security)) {
787 		ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
788 		size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
789 	}
790 
791 	/* identity & sensitivity */
792 	if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
793 		size += sizeof(struct sadb_address) + sockaddr_size;
794 
795 	if (add_keys) {
796 		if (x->aalg && x->aalg->alg_key_len) {
797 			auth_key_size =
798 				PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
799 			size += sizeof(struct sadb_key) + auth_key_size;
800 		}
801 		if (x->ealg && x->ealg->alg_key_len) {
802 			encrypt_key_size =
803 				PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
804 			size += sizeof(struct sadb_key) + encrypt_key_size;
805 		}
806 	}
807 	if (x->encap)
808 		natt = x->encap;
809 
810 	if (natt && natt->encap_type) {
811 		size += sizeof(struct sadb_x_nat_t_type);
812 		size += sizeof(struct sadb_x_nat_t_port);
813 		size += sizeof(struct sadb_x_nat_t_port);
814 	}
815 
816 	skb =  alloc_skb(size + 16, GFP_ATOMIC);
817 	if (skb == NULL)
818 		return ERR_PTR(-ENOBUFS);
819 
820 	/* call should fill header later */
821 	hdr = skb_put(skb, sizeof(struct sadb_msg));
822 	memset(hdr, 0, size);	/* XXX do we need this ? */
823 	hdr->sadb_msg_len = size / sizeof(uint64_t);
824 
825 	/* sa */
826 	sa = skb_put(skb, sizeof(struct sadb_sa));
827 	sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
828 	sa->sadb_sa_exttype = SADB_EXT_SA;
829 	sa->sadb_sa_spi = x->id.spi;
830 	sa->sadb_sa_replay = x->props.replay_window;
831 	switch (x->km.state) {
832 	case XFRM_STATE_VALID:
833 		sa->sadb_sa_state = x->km.dying ?
834 			SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
835 		break;
836 	case XFRM_STATE_ACQ:
837 		sa->sadb_sa_state = SADB_SASTATE_LARVAL;
838 		break;
839 	default:
840 		sa->sadb_sa_state = SADB_SASTATE_DEAD;
841 		break;
842 	}
843 	sa->sadb_sa_auth = 0;
844 	if (x->aalg) {
845 		struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
846 		sa->sadb_sa_auth = (a && a->pfkey_supported) ?
847 					a->desc.sadb_alg_id : 0;
848 	}
849 	sa->sadb_sa_encrypt = 0;
850 	BUG_ON(x->ealg && x->calg);
851 	if (x->ealg) {
852 		struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
853 		sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
854 					a->desc.sadb_alg_id : 0;
855 	}
856 	/* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
857 	if (x->calg) {
858 		struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
859 		sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
860 					a->desc.sadb_alg_id : 0;
861 	}
862 
863 	sa->sadb_sa_flags = 0;
864 	if (x->props.flags & XFRM_STATE_NOECN)
865 		sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
866 	if (x->props.flags & XFRM_STATE_DECAP_DSCP)
867 		sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
868 	if (x->props.flags & XFRM_STATE_NOPMTUDISC)
869 		sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
870 
871 	/* hard time */
872 	if (hsc & 2) {
873 		lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
874 		lifetime->sadb_lifetime_len =
875 			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
876 		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
877 		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.hard_packet_limit);
878 		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
879 		lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
880 		lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
881 	}
882 	/* soft time */
883 	if (hsc & 1) {
884 		lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
885 		lifetime->sadb_lifetime_len =
886 			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
887 		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
888 		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.soft_packet_limit);
889 		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
890 		lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
891 		lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
892 	}
893 	/* current time */
894 	lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
895 	lifetime->sadb_lifetime_len =
896 		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
897 	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
898 	lifetime->sadb_lifetime_allocations = x->curlft.packets;
899 	lifetime->sadb_lifetime_bytes = x->curlft.bytes;
900 	lifetime->sadb_lifetime_addtime = x->curlft.add_time;
901 	lifetime->sadb_lifetime_usetime = x->curlft.use_time;
902 	/* src address */
903 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
904 	addr->sadb_address_len =
905 		(sizeof(struct sadb_address)+sockaddr_size)/
906 			sizeof(uint64_t);
907 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
908 	/* "if the ports are non-zero, then the sadb_address_proto field,
909 	   normally zero, MUST be filled in with the transport
910 	   protocol's number." - RFC2367 */
911 	addr->sadb_address_proto = 0;
912 	addr->sadb_address_reserved = 0;
913 
914 	addr->sadb_address_prefixlen =
915 		pfkey_sockaddr_fill(&x->props.saddr, 0,
916 				    (struct sockaddr *) (addr + 1),
917 				    x->props.family);
918 	if (!addr->sadb_address_prefixlen)
919 		BUG();
920 
921 	/* dst address */
922 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
923 	addr->sadb_address_len =
924 		(sizeof(struct sadb_address)+sockaddr_size)/
925 			sizeof(uint64_t);
926 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
927 	addr->sadb_address_proto = 0;
928 	addr->sadb_address_reserved = 0;
929 
930 	addr->sadb_address_prefixlen =
931 		pfkey_sockaddr_fill(&x->id.daddr, 0,
932 				    (struct sockaddr *) (addr + 1),
933 				    x->props.family);
934 	if (!addr->sadb_address_prefixlen)
935 		BUG();
936 
937 	if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
938 			     x->props.family)) {
939 		addr = skb_put(skb,
940 			       sizeof(struct sadb_address) + sockaddr_size);
941 		addr->sadb_address_len =
942 			(sizeof(struct sadb_address)+sockaddr_size)/
943 			sizeof(uint64_t);
944 		addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
945 		addr->sadb_address_proto =
946 			pfkey_proto_from_xfrm(x->sel.proto);
947 		addr->sadb_address_prefixlen = x->sel.prefixlen_s;
948 		addr->sadb_address_reserved = 0;
949 
950 		pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
951 				    (struct sockaddr *) (addr + 1),
952 				    x->props.family);
953 	}
954 
955 	/* auth key */
956 	if (add_keys && auth_key_size) {
957 		key = skb_put(skb, sizeof(struct sadb_key) + auth_key_size);
958 		key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
959 			sizeof(uint64_t);
960 		key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
961 		key->sadb_key_bits = x->aalg->alg_key_len;
962 		key->sadb_key_reserved = 0;
963 		memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
964 	}
965 	/* encrypt key */
966 	if (add_keys && encrypt_key_size) {
967 		key = skb_put(skb, sizeof(struct sadb_key) + encrypt_key_size);
968 		key->sadb_key_len = (sizeof(struct sadb_key) +
969 				     encrypt_key_size) / sizeof(uint64_t);
970 		key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
971 		key->sadb_key_bits = x->ealg->alg_key_len;
972 		key->sadb_key_reserved = 0;
973 		memcpy(key + 1, x->ealg->alg_key,
974 		       (x->ealg->alg_key_len+7)/8);
975 	}
976 
977 	/* sa */
978 	sa2 = skb_put(skb, sizeof(struct sadb_x_sa2));
979 	sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
980 	sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
981 	if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
982 		kfree_skb(skb);
983 		return ERR_PTR(-EINVAL);
984 	}
985 	sa2->sadb_x_sa2_mode = mode;
986 	sa2->sadb_x_sa2_reserved1 = 0;
987 	sa2->sadb_x_sa2_reserved2 = 0;
988 	sa2->sadb_x_sa2_sequence = 0;
989 	sa2->sadb_x_sa2_reqid = x->props.reqid;
990 
991 	if (natt && natt->encap_type) {
992 		struct sadb_x_nat_t_type *n_type;
993 		struct sadb_x_nat_t_port *n_port;
994 
995 		/* type */
996 		n_type = skb_put(skb, sizeof(*n_type));
997 		n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
998 		n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
999 		n_type->sadb_x_nat_t_type_type = natt->encap_type;
1000 		n_type->sadb_x_nat_t_type_reserved[0] = 0;
1001 		n_type->sadb_x_nat_t_type_reserved[1] = 0;
1002 		n_type->sadb_x_nat_t_type_reserved[2] = 0;
1003 
1004 		/* source port */
1005 		n_port = skb_put(skb, sizeof(*n_port));
1006 		n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1007 		n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
1008 		n_port->sadb_x_nat_t_port_port = natt->encap_sport;
1009 		n_port->sadb_x_nat_t_port_reserved = 0;
1010 
1011 		/* dest port */
1012 		n_port = skb_put(skb, sizeof(*n_port));
1013 		n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
1014 		n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
1015 		n_port->sadb_x_nat_t_port_port = natt->encap_dport;
1016 		n_port->sadb_x_nat_t_port_reserved = 0;
1017 	}
1018 
1019 	/* security context */
1020 	if (xfrm_ctx) {
1021 		sec_ctx = skb_put(skb,
1022 				  sizeof(struct sadb_x_sec_ctx) + ctx_size);
1023 		sec_ctx->sadb_x_sec_len =
1024 		  (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
1025 		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
1026 		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
1027 		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
1028 		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
1029 		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
1030 		       xfrm_ctx->ctx_len);
1031 	}
1032 
1033 	return skb;
1034 }
1035 
1036 
1037 static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1038 {
1039 	struct sk_buff *skb;
1040 
1041 	skb = __pfkey_xfrm_state2msg(x, 1, 3);
1042 
1043 	return skb;
1044 }
1045 
1046 static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1047 							  int hsc)
1048 {
1049 	return __pfkey_xfrm_state2msg(x, 0, hsc);
1050 }
1051 
1052 static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1053 						const struct sadb_msg *hdr,
1054 						void * const *ext_hdrs)
1055 {
1056 	struct xfrm_state *x;
1057 	const struct sadb_lifetime *lifetime;
1058 	const struct sadb_sa *sa;
1059 	const struct sadb_key *key;
1060 	const struct sadb_x_sec_ctx *sec_ctx;
1061 	uint16_t proto;
1062 	int err;
1063 
1064 
1065 	sa = ext_hdrs[SADB_EXT_SA - 1];
1066 	if (!sa ||
1067 	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1068 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1069 		return ERR_PTR(-EINVAL);
1070 	if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
1071 	    !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
1072 		return ERR_PTR(-EINVAL);
1073 	if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
1074 	    !ext_hdrs[SADB_EXT_KEY_AUTH-1])
1075 		return ERR_PTR(-EINVAL);
1076 	if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
1077 	    !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
1078 		return ERR_PTR(-EINVAL);
1079 
1080 	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1081 	if (proto == 0)
1082 		return ERR_PTR(-EINVAL);
1083 
1084 	/* default error is no buffer space */
1085 	err = -ENOBUFS;
1086 
1087 	/* RFC2367:
1088 
1089    Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
1090    SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
1091    sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
1092    Therefore, the sadb_sa_state field of all submitted SAs MUST be
1093    SADB_SASTATE_MATURE and the kernel MUST return an error if this is
1094    not true.
1095 
1096 	   However, KAME setkey always uses SADB_SASTATE_LARVAL.
1097 	   Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
1098 	 */
1099 	if (sa->sadb_sa_auth > SADB_AALG_MAX ||
1100 	    (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
1101 	     sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
1102 	    sa->sadb_sa_encrypt > SADB_EALG_MAX)
1103 		return ERR_PTR(-EINVAL);
1104 	key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1105 	if (key != NULL &&
1106 	    sa->sadb_sa_auth != SADB_X_AALG_NULL &&
1107 	    ((key->sadb_key_bits+7) / 8 == 0 ||
1108 	     (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1109 		return ERR_PTR(-EINVAL);
1110 	key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1111 	if (key != NULL &&
1112 	    sa->sadb_sa_encrypt != SADB_EALG_NULL &&
1113 	    ((key->sadb_key_bits+7) / 8 == 0 ||
1114 	     (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
1115 		return ERR_PTR(-EINVAL);
1116 
1117 	x = xfrm_state_alloc(net);
1118 	if (x == NULL)
1119 		return ERR_PTR(-ENOBUFS);
1120 
1121 	x->id.proto = proto;
1122 	x->id.spi = sa->sadb_sa_spi;
1123 	x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
1124 					(sizeof(x->replay.bitmap) * 8));
1125 	if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
1126 		x->props.flags |= XFRM_STATE_NOECN;
1127 	if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
1128 		x->props.flags |= XFRM_STATE_DECAP_DSCP;
1129 	if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
1130 		x->props.flags |= XFRM_STATE_NOPMTUDISC;
1131 
1132 	lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
1133 	if (lifetime != NULL) {
1134 		x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1135 		x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1136 		x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1137 		x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1138 	}
1139 	lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
1140 	if (lifetime != NULL) {
1141 		x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
1142 		x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
1143 		x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
1144 		x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
1145 	}
1146 
1147 	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
1148 	if (sec_ctx != NULL) {
1149 		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
1150 
1151 		if (!uctx)
1152 			goto out;
1153 
1154 		err = security_xfrm_state_alloc(x, uctx);
1155 		kfree(uctx);
1156 
1157 		if (err)
1158 			goto out;
1159 	}
1160 
1161 	err = -ENOBUFS;
1162 	key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
1163 	if (sa->sadb_sa_auth) {
1164 		int keysize = 0;
1165 		struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1166 		if (!a || !a->pfkey_supported) {
1167 			err = -ENOSYS;
1168 			goto out;
1169 		}
1170 		if (key)
1171 			keysize = (key->sadb_key_bits + 7) / 8;
1172 		x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
1173 		if (!x->aalg) {
1174 			err = -ENOMEM;
1175 			goto out;
1176 		}
1177 		strcpy(x->aalg->alg_name, a->name);
1178 		x->aalg->alg_key_len = 0;
1179 		if (key) {
1180 			x->aalg->alg_key_len = key->sadb_key_bits;
1181 			memcpy(x->aalg->alg_key, key+1, keysize);
1182 		}
1183 		x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
1184 		x->props.aalgo = sa->sadb_sa_auth;
1185 		/* x->algo.flags = sa->sadb_sa_flags; */
1186 	}
1187 	if (sa->sadb_sa_encrypt) {
1188 		if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
1189 			struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1190 			if (!a || !a->pfkey_supported) {
1191 				err = -ENOSYS;
1192 				goto out;
1193 			}
1194 			x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
1195 			if (!x->calg) {
1196 				err = -ENOMEM;
1197 				goto out;
1198 			}
1199 			strcpy(x->calg->alg_name, a->name);
1200 			x->props.calgo = sa->sadb_sa_encrypt;
1201 		} else {
1202 			int keysize = 0;
1203 			struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1204 			if (!a || !a->pfkey_supported) {
1205 				err = -ENOSYS;
1206 				goto out;
1207 			}
1208 			key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
1209 			if (key)
1210 				keysize = (key->sadb_key_bits + 7) / 8;
1211 			x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
1212 			if (!x->ealg) {
1213 				err = -ENOMEM;
1214 				goto out;
1215 			}
1216 			strcpy(x->ealg->alg_name, a->name);
1217 			x->ealg->alg_key_len = 0;
1218 			if (key) {
1219 				x->ealg->alg_key_len = key->sadb_key_bits;
1220 				memcpy(x->ealg->alg_key, key+1, keysize);
1221 			}
1222 			x->props.ealgo = sa->sadb_sa_encrypt;
1223 			x->geniv = a->uinfo.encr.geniv;
1224 		}
1225 	}
1226 	/* x->algo.flags = sa->sadb_sa_flags; */
1227 
1228 	x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1229 						    &x->props.saddr);
1230 	pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
1231 				  &x->id.daddr);
1232 
1233 	if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1234 		const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1235 		int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1236 		if (mode < 0) {
1237 			err = -EINVAL;
1238 			goto out;
1239 		}
1240 		x->props.mode = mode;
1241 		x->props.reqid = sa2->sadb_x_sa2_reqid;
1242 	}
1243 
1244 	if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1245 		const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
1246 
1247 		/* Nobody uses this, but we try. */
1248 		x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
1249 		x->sel.prefixlen_s = addr->sadb_address_prefixlen;
1250 	}
1251 
1252 	if (!x->sel.family)
1253 		x->sel.family = x->props.family;
1254 
1255 	if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1256 		const struct sadb_x_nat_t_type* n_type;
1257 		struct xfrm_encap_tmpl *natt;
1258 
1259 		x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
1260 		if (!x->encap) {
1261 			err = -ENOMEM;
1262 			goto out;
1263 		}
1264 
1265 		natt = x->encap;
1266 		n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
1267 		natt->encap_type = n_type->sadb_x_nat_t_type_type;
1268 
1269 		if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
1270 			const struct sadb_x_nat_t_port *n_port =
1271 				ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
1272 			natt->encap_sport = n_port->sadb_x_nat_t_port_port;
1273 		}
1274 		if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
1275 			const struct sadb_x_nat_t_port *n_port =
1276 				ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
1277 			natt->encap_dport = n_port->sadb_x_nat_t_port_port;
1278 		}
1279 		memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
1280 	}
1281 
1282 	err = xfrm_init_state(x);
1283 	if (err)
1284 		goto out;
1285 
1286 	x->km.seq = hdr->sadb_msg_seq;
1287 	return x;
1288 
1289 out:
1290 	x->km.state = XFRM_STATE_DEAD;
1291 	xfrm_state_put(x);
1292 	return ERR_PTR(err);
1293 }
1294 
1295 static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1296 {
1297 	return -EOPNOTSUPP;
1298 }
1299 
1300 static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1301 {
1302 	struct net *net = sock_net(sk);
1303 	struct sk_buff *resp_skb;
1304 	struct sadb_x_sa2 *sa2;
1305 	struct sadb_address *saddr, *daddr;
1306 	struct sadb_msg *out_hdr;
1307 	struct sadb_spirange *range;
1308 	struct xfrm_state *x = NULL;
1309 	int mode;
1310 	int err;
1311 	u32 min_spi, max_spi;
1312 	u32 reqid;
1313 	u8 proto;
1314 	unsigned short family;
1315 	xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
1316 
1317 	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1318 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1319 		return -EINVAL;
1320 
1321 	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1322 	if (proto == 0)
1323 		return -EINVAL;
1324 
1325 	if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1326 		mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
1327 		if (mode < 0)
1328 			return -EINVAL;
1329 		reqid = sa2->sadb_x_sa2_reqid;
1330 	} else {
1331 		mode = 0;
1332 		reqid = 0;
1333 	}
1334 
1335 	saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
1336 	daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
1337 
1338 	family = ((struct sockaddr *)(saddr + 1))->sa_family;
1339 	switch (family) {
1340 	case AF_INET:
1341 		xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
1342 		xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1343 		break;
1344 #if IS_ENABLED(CONFIG_IPV6)
1345 	case AF_INET6:
1346 		xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
1347 		xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1348 		break;
1349 #endif
1350 	}
1351 
1352 	if (hdr->sadb_msg_seq) {
1353 		x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1354 		if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
1355 			xfrm_state_put(x);
1356 			x = NULL;
1357 		}
1358 	}
1359 
1360 	if (!x)
1361 		x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
1362 
1363 	if (x == NULL)
1364 		return -ENOENT;
1365 
1366 	min_spi = 0x100;
1367 	max_spi = 0x0fffffff;
1368 
1369 	range = ext_hdrs[SADB_EXT_SPIRANGE-1];
1370 	if (range) {
1371 		min_spi = range->sadb_spirange_min;
1372 		max_spi = range->sadb_spirange_max;
1373 	}
1374 
1375 	err = verify_spi_info(x->id.proto, min_spi, max_spi);
1376 	if (err) {
1377 		xfrm_state_put(x);
1378 		return err;
1379 	}
1380 
1381 	err = xfrm_alloc_spi(x, min_spi, max_spi);
1382 	resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
1383 
1384 	if (IS_ERR(resp_skb)) {
1385 		xfrm_state_put(x);
1386 		return  PTR_ERR(resp_skb);
1387 	}
1388 
1389 	out_hdr = (struct sadb_msg *) resp_skb->data;
1390 	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1391 	out_hdr->sadb_msg_type = SADB_GETSPI;
1392 	out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1393 	out_hdr->sadb_msg_errno = 0;
1394 	out_hdr->sadb_msg_reserved = 0;
1395 	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1396 	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1397 
1398 	xfrm_state_put(x);
1399 
1400 	pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
1401 
1402 	return 0;
1403 }
1404 
1405 static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1406 {
1407 	struct net *net = sock_net(sk);
1408 	struct xfrm_state *x;
1409 
1410 	if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
1411 		return -EOPNOTSUPP;
1412 
1413 	if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
1414 		return 0;
1415 
1416 	x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1417 	if (x == NULL)
1418 		return 0;
1419 
1420 	spin_lock_bh(&x->lock);
1421 	if (x->km.state == XFRM_STATE_ACQ)
1422 		x->km.state = XFRM_STATE_ERROR;
1423 
1424 	spin_unlock_bh(&x->lock);
1425 	xfrm_state_put(x);
1426 	return 0;
1427 }
1428 
1429 static inline int event2poltype(int event)
1430 {
1431 	switch (event) {
1432 	case XFRM_MSG_DELPOLICY:
1433 		return SADB_X_SPDDELETE;
1434 	case XFRM_MSG_NEWPOLICY:
1435 		return SADB_X_SPDADD;
1436 	case XFRM_MSG_UPDPOLICY:
1437 		return SADB_X_SPDUPDATE;
1438 	case XFRM_MSG_POLEXPIRE:
1439 	//	return SADB_X_SPDEXPIRE;
1440 	default:
1441 		pr_err("pfkey: Unknown policy event %d\n", event);
1442 		break;
1443 	}
1444 
1445 	return 0;
1446 }
1447 
1448 static inline int event2keytype(int event)
1449 {
1450 	switch (event) {
1451 	case XFRM_MSG_DELSA:
1452 		return SADB_DELETE;
1453 	case XFRM_MSG_NEWSA:
1454 		return SADB_ADD;
1455 	case XFRM_MSG_UPDSA:
1456 		return SADB_UPDATE;
1457 	case XFRM_MSG_EXPIRE:
1458 		return SADB_EXPIRE;
1459 	default:
1460 		pr_err("pfkey: Unknown SA event %d\n", event);
1461 		break;
1462 	}
1463 
1464 	return 0;
1465 }
1466 
1467 /* ADD/UPD/DEL */
1468 static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1469 {
1470 	struct sk_buff *skb;
1471 	struct sadb_msg *hdr;
1472 
1473 	skb = pfkey_xfrm_state2msg(x);
1474 
1475 	if (IS_ERR(skb))
1476 		return PTR_ERR(skb);
1477 
1478 	hdr = (struct sadb_msg *) skb->data;
1479 	hdr->sadb_msg_version = PF_KEY_V2;
1480 	hdr->sadb_msg_type = event2keytype(c->event);
1481 	hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1482 	hdr->sadb_msg_errno = 0;
1483 	hdr->sadb_msg_reserved = 0;
1484 	hdr->sadb_msg_seq = c->seq;
1485 	hdr->sadb_msg_pid = c->portid;
1486 
1487 	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
1488 
1489 	return 0;
1490 }
1491 
1492 static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1493 {
1494 	struct net *net = sock_net(sk);
1495 	struct xfrm_state *x;
1496 	int err;
1497 	struct km_event c;
1498 
1499 	x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
1500 	if (IS_ERR(x))
1501 		return PTR_ERR(x);
1502 
1503 	xfrm_state_hold(x);
1504 	if (hdr->sadb_msg_type == SADB_ADD)
1505 		err = xfrm_state_add(x);
1506 	else
1507 		err = xfrm_state_update(x);
1508 
1509 	xfrm_audit_state_add(x, err ? 0 : 1, true);
1510 
1511 	if (err < 0) {
1512 		x->km.state = XFRM_STATE_DEAD;
1513 		__xfrm_state_put(x);
1514 		goto out;
1515 	}
1516 
1517 	if (hdr->sadb_msg_type == SADB_ADD)
1518 		c.event = XFRM_MSG_NEWSA;
1519 	else
1520 		c.event = XFRM_MSG_UPDSA;
1521 	c.seq = hdr->sadb_msg_seq;
1522 	c.portid = hdr->sadb_msg_pid;
1523 	km_state_notify(x, &c);
1524 out:
1525 	xfrm_state_put(x);
1526 	return err;
1527 }
1528 
1529 static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1530 {
1531 	struct net *net = sock_net(sk);
1532 	struct xfrm_state *x;
1533 	struct km_event c;
1534 	int err;
1535 
1536 	if (!ext_hdrs[SADB_EXT_SA-1] ||
1537 	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1538 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1539 		return -EINVAL;
1540 
1541 	x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1542 	if (x == NULL)
1543 		return -ESRCH;
1544 
1545 	if ((err = security_xfrm_state_delete(x)))
1546 		goto out;
1547 
1548 	if (xfrm_state_kern(x)) {
1549 		err = -EPERM;
1550 		goto out;
1551 	}
1552 
1553 	err = xfrm_state_delete(x);
1554 
1555 	if (err < 0)
1556 		goto out;
1557 
1558 	c.seq = hdr->sadb_msg_seq;
1559 	c.portid = hdr->sadb_msg_pid;
1560 	c.event = XFRM_MSG_DELSA;
1561 	km_state_notify(x, &c);
1562 out:
1563 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
1564 	xfrm_state_put(x);
1565 
1566 	return err;
1567 }
1568 
1569 static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1570 {
1571 	struct net *net = sock_net(sk);
1572 	__u8 proto;
1573 	struct sk_buff *out_skb;
1574 	struct sadb_msg *out_hdr;
1575 	struct xfrm_state *x;
1576 
1577 	if (!ext_hdrs[SADB_EXT_SA-1] ||
1578 	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1579 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
1580 		return -EINVAL;
1581 
1582 	x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
1583 	if (x == NULL)
1584 		return -ESRCH;
1585 
1586 	out_skb = pfkey_xfrm_state2msg(x);
1587 	proto = x->id.proto;
1588 	xfrm_state_put(x);
1589 	if (IS_ERR(out_skb))
1590 		return  PTR_ERR(out_skb);
1591 
1592 	out_hdr = (struct sadb_msg *) out_skb->data;
1593 	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1594 	out_hdr->sadb_msg_type = SADB_GET;
1595 	out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
1596 	out_hdr->sadb_msg_errno = 0;
1597 	out_hdr->sadb_msg_reserved = 0;
1598 	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
1599 	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
1600 	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
1601 
1602 	return 0;
1603 }
1604 
1605 static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
1606 					      gfp_t allocation)
1607 {
1608 	struct sk_buff *skb;
1609 	struct sadb_msg *hdr;
1610 	int len, auth_len, enc_len, i;
1611 
1612 	auth_len = xfrm_count_pfkey_auth_supported();
1613 	if (auth_len) {
1614 		auth_len *= sizeof(struct sadb_alg);
1615 		auth_len += sizeof(struct sadb_supported);
1616 	}
1617 
1618 	enc_len = xfrm_count_pfkey_enc_supported();
1619 	if (enc_len) {
1620 		enc_len *= sizeof(struct sadb_alg);
1621 		enc_len += sizeof(struct sadb_supported);
1622 	}
1623 
1624 	len = enc_len + auth_len + sizeof(struct sadb_msg);
1625 
1626 	skb = alloc_skb(len + 16, allocation);
1627 	if (!skb)
1628 		goto out_put_algs;
1629 
1630 	hdr = skb_put(skb, sizeof(*hdr));
1631 	pfkey_hdr_dup(hdr, orig);
1632 	hdr->sadb_msg_errno = 0;
1633 	hdr->sadb_msg_len = len / sizeof(uint64_t);
1634 
1635 	if (auth_len) {
1636 		struct sadb_supported *sp;
1637 		struct sadb_alg *ap;
1638 
1639 		sp = skb_put(skb, auth_len);
1640 		ap = (struct sadb_alg *) (sp + 1);
1641 
1642 		sp->sadb_supported_len = auth_len / sizeof(uint64_t);
1643 		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
1644 
1645 		for (i = 0; ; i++) {
1646 			struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
1647 			if (!aalg)
1648 				break;
1649 			if (!aalg->pfkey_supported)
1650 				continue;
1651 			if (aalg->available)
1652 				*ap++ = aalg->desc;
1653 		}
1654 	}
1655 
1656 	if (enc_len) {
1657 		struct sadb_supported *sp;
1658 		struct sadb_alg *ap;
1659 
1660 		sp = skb_put(skb, enc_len);
1661 		ap = (struct sadb_alg *) (sp + 1);
1662 
1663 		sp->sadb_supported_len = enc_len / sizeof(uint64_t);
1664 		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
1665 
1666 		for (i = 0; ; i++) {
1667 			struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
1668 			if (!ealg)
1669 				break;
1670 			if (!ealg->pfkey_supported)
1671 				continue;
1672 			if (ealg->available)
1673 				*ap++ = ealg->desc;
1674 		}
1675 	}
1676 
1677 out_put_algs:
1678 	return skb;
1679 }
1680 
1681 static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1682 {
1683 	struct pfkey_sock *pfk = pfkey_sk(sk);
1684 	struct sk_buff *supp_skb;
1685 
1686 	if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
1687 		return -EINVAL;
1688 
1689 	if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
1690 		if (pfk->registered&(1<<hdr->sadb_msg_satype))
1691 			return -EEXIST;
1692 		pfk->registered |= (1<<hdr->sadb_msg_satype);
1693 	}
1694 
1695 	xfrm_probe_algs();
1696 
1697 	supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
1698 	if (!supp_skb) {
1699 		if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
1700 			pfk->registered &= ~(1<<hdr->sadb_msg_satype);
1701 
1702 		return -ENOBUFS;
1703 	}
1704 
1705 	pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
1706 			sock_net(sk));
1707 	return 0;
1708 }
1709 
1710 static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1711 {
1712 	struct sk_buff *skb;
1713 	struct sadb_msg *hdr;
1714 
1715 	skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1716 	if (!skb)
1717 		return -ENOBUFS;
1718 
1719 	hdr = skb_put_data(skb, ihdr, sizeof(struct sadb_msg));
1720 	hdr->sadb_msg_errno = (uint8_t) 0;
1721 	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1722 
1723 	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
1724 			       sock_net(sk));
1725 }
1726 
1727 static int key_notify_sa_flush(const struct km_event *c)
1728 {
1729 	struct sk_buff *skb;
1730 	struct sadb_msg *hdr;
1731 
1732 	skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
1733 	if (!skb)
1734 		return -ENOBUFS;
1735 	hdr = skb_put(skb, sizeof(struct sadb_msg));
1736 	hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
1737 	hdr->sadb_msg_type = SADB_FLUSH;
1738 	hdr->sadb_msg_seq = c->seq;
1739 	hdr->sadb_msg_pid = c->portid;
1740 	hdr->sadb_msg_version = PF_KEY_V2;
1741 	hdr->sadb_msg_errno = (uint8_t) 0;
1742 	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1743 	hdr->sadb_msg_reserved = 0;
1744 
1745 	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
1746 
1747 	return 0;
1748 }
1749 
1750 static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1751 {
1752 	struct net *net = sock_net(sk);
1753 	unsigned int proto;
1754 	struct km_event c;
1755 	int err, err2;
1756 
1757 	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1758 	if (proto == 0)
1759 		return -EINVAL;
1760 
1761 	err = xfrm_state_flush(net, proto, true);
1762 	err2 = unicast_flush_resp(sk, hdr);
1763 	if (err || err2) {
1764 		if (err == -ESRCH) /* empty table - go quietly */
1765 			err = 0;
1766 		return err ? err : err2;
1767 	}
1768 
1769 	c.data.proto = proto;
1770 	c.seq = hdr->sadb_msg_seq;
1771 	c.portid = hdr->sadb_msg_pid;
1772 	c.event = XFRM_MSG_FLUSHSA;
1773 	c.net = net;
1774 	km_state_notify(NULL, &c);
1775 
1776 	return 0;
1777 }
1778 
1779 static int dump_sa(struct xfrm_state *x, int count, void *ptr)
1780 {
1781 	struct pfkey_sock *pfk = ptr;
1782 	struct sk_buff *out_skb;
1783 	struct sadb_msg *out_hdr;
1784 
1785 	if (!pfkey_can_dump(&pfk->sk))
1786 		return -ENOBUFS;
1787 
1788 	out_skb = pfkey_xfrm_state2msg(x);
1789 	if (IS_ERR(out_skb))
1790 		return PTR_ERR(out_skb);
1791 
1792 	out_hdr = (struct sadb_msg *) out_skb->data;
1793 	out_hdr->sadb_msg_version = pfk->dump.msg_version;
1794 	out_hdr->sadb_msg_type = SADB_DUMP;
1795 	out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
1796 	out_hdr->sadb_msg_errno = 0;
1797 	out_hdr->sadb_msg_reserved = 0;
1798 	out_hdr->sadb_msg_seq = count + 1;
1799 	out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
1800 
1801 	if (pfk->dump.skb)
1802 		pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
1803 				&pfk->sk, sock_net(&pfk->sk));
1804 	pfk->dump.skb = out_skb;
1805 
1806 	return 0;
1807 }
1808 
1809 static int pfkey_dump_sa(struct pfkey_sock *pfk)
1810 {
1811 	struct net *net = sock_net(&pfk->sk);
1812 	return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1813 }
1814 
1815 static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
1816 {
1817 	struct net *net = sock_net(&pfk->sk);
1818 
1819 	xfrm_state_walk_done(&pfk->dump.u.state, net);
1820 }
1821 
1822 static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1823 {
1824 	u8 proto;
1825 	struct xfrm_address_filter *filter = NULL;
1826 	struct pfkey_sock *pfk = pfkey_sk(sk);
1827 
1828 	mutex_lock(&pfk->dump_lock);
1829 	if (pfk->dump.dump != NULL) {
1830 		mutex_unlock(&pfk->dump_lock);
1831 		return -EBUSY;
1832 	}
1833 
1834 	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
1835 	if (proto == 0) {
1836 		mutex_unlock(&pfk->dump_lock);
1837 		return -EINVAL;
1838 	}
1839 
1840 	if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
1841 		struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
1842 
1843 		filter = kmalloc(sizeof(*filter), GFP_KERNEL);
1844 		if (filter == NULL) {
1845 			mutex_unlock(&pfk->dump_lock);
1846 			return -ENOMEM;
1847 		}
1848 
1849 		memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
1850 		       sizeof(xfrm_address_t));
1851 		memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
1852 		       sizeof(xfrm_address_t));
1853 		filter->family = xfilter->sadb_x_filter_family;
1854 		filter->splen = xfilter->sadb_x_filter_splen;
1855 		filter->dplen = xfilter->sadb_x_filter_dplen;
1856 	}
1857 
1858 	pfk->dump.msg_version = hdr->sadb_msg_version;
1859 	pfk->dump.msg_portid = hdr->sadb_msg_pid;
1860 	pfk->dump.dump = pfkey_dump_sa;
1861 	pfk->dump.done = pfkey_dump_sa_done;
1862 	xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
1863 	mutex_unlock(&pfk->dump_lock);
1864 
1865 	return pfkey_do_dump(pfk);
1866 }
1867 
1868 static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
1869 {
1870 	struct pfkey_sock *pfk = pfkey_sk(sk);
1871 	int satype = hdr->sadb_msg_satype;
1872 	bool reset_errno = false;
1873 
1874 	if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1875 		reset_errno = true;
1876 		if (satype != 0 && satype != 1)
1877 			return -EINVAL;
1878 		pfk->promisc = satype;
1879 	}
1880 	if (reset_errno && skb_cloned(skb))
1881 		skb = skb_copy(skb, GFP_KERNEL);
1882 	else
1883 		skb = skb_clone(skb, GFP_KERNEL);
1884 
1885 	if (reset_errno && skb) {
1886 		struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
1887 		new_hdr->sadb_msg_errno = 0;
1888 	}
1889 
1890 	pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
1891 	return 0;
1892 }
1893 
1894 static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
1895 {
1896 	int i;
1897 	u32 reqid = *(u32*)ptr;
1898 
1899 	for (i=0; i<xp->xfrm_nr; i++) {
1900 		if (xp->xfrm_vec[i].reqid == reqid)
1901 			return -EEXIST;
1902 	}
1903 	return 0;
1904 }
1905 
1906 static u32 gen_reqid(struct net *net)
1907 {
1908 	struct xfrm_policy_walk walk;
1909 	u32 start;
1910 	int rc;
1911 	static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1912 
1913 	start = reqid;
1914 	do {
1915 		++reqid;
1916 		if (reqid == 0)
1917 			reqid = IPSEC_MANUAL_REQID_MAX+1;
1918 		xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
1919 		rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
1920 		xfrm_policy_walk_done(&walk, net);
1921 		if (rc != -EEXIST)
1922 			return reqid;
1923 	} while (reqid != start);
1924 	return 0;
1925 }
1926 
1927 static int
1928 parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
1929 {
1930 	struct net *net = xp_net(xp);
1931 	struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1932 	int mode;
1933 
1934 	if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1935 		return -ELOOP;
1936 
1937 	if (rq->sadb_x_ipsecrequest_mode == 0)
1938 		return -EINVAL;
1939 
1940 	t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1941 	if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
1942 		return -EINVAL;
1943 	t->mode = mode;
1944 	if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
1945 		t->optional = 1;
1946 	else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
1947 		t->reqid = rq->sadb_x_ipsecrequest_reqid;
1948 		if (t->reqid > IPSEC_MANUAL_REQID_MAX)
1949 			t->reqid = 0;
1950 		if (!t->reqid && !(t->reqid = gen_reqid(net)))
1951 			return -ENOBUFS;
1952 	}
1953 
1954 	/* addresses present only in tunnel mode */
1955 	if (t->mode == XFRM_MODE_TUNNEL) {
1956 		int err;
1957 
1958 		err = parse_sockaddr_pair(
1959 			(struct sockaddr *)(rq + 1),
1960 			rq->sadb_x_ipsecrequest_len - sizeof(*rq),
1961 			&t->saddr, &t->id.daddr, &t->encap_family);
1962 		if (err)
1963 			return err;
1964 	} else
1965 		t->encap_family = xp->family;
1966 
1967 	/* No way to set this via kame pfkey */
1968 	t->allalgs = 1;
1969 	xp->xfrm_nr++;
1970 	return 0;
1971 }
1972 
1973 static int
1974 parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
1975 {
1976 	int err;
1977 	int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
1978 	struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
1979 
1980 	if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
1981 		return -EINVAL;
1982 
1983 	while (len >= sizeof(*rq)) {
1984 		if (len < rq->sadb_x_ipsecrequest_len ||
1985 		    rq->sadb_x_ipsecrequest_len < sizeof(*rq))
1986 			return -EINVAL;
1987 
1988 		if ((err = parse_ipsecrequest(xp, rq)) < 0)
1989 			return err;
1990 		len -= rq->sadb_x_ipsecrequest_len;
1991 		rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
1992 	}
1993 	return 0;
1994 }
1995 
1996 static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
1997 {
1998   struct xfrm_sec_ctx *xfrm_ctx = xp->security;
1999 
2000 	if (xfrm_ctx) {
2001 		int len = sizeof(struct sadb_x_sec_ctx);
2002 		len += xfrm_ctx->ctx_len;
2003 		return PFKEY_ALIGN8(len);
2004 	}
2005 	return 0;
2006 }
2007 
2008 static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
2009 {
2010 	const struct xfrm_tmpl *t;
2011 	int sockaddr_size = pfkey_sockaddr_size(xp->family);
2012 	int socklen = 0;
2013 	int i;
2014 
2015 	for (i=0; i<xp->xfrm_nr; i++) {
2016 		t = xp->xfrm_vec + i;
2017 		socklen += pfkey_sockaddr_len(t->encap_family);
2018 	}
2019 
2020 	return sizeof(struct sadb_msg) +
2021 		(sizeof(struct sadb_lifetime) * 3) +
2022 		(sizeof(struct sadb_address) * 2) +
2023 		(sockaddr_size * 2) +
2024 		sizeof(struct sadb_x_policy) +
2025 		(xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
2026 		(socklen * 2) +
2027 		pfkey_xfrm_policy2sec_ctx_size(xp);
2028 }
2029 
2030 static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
2031 {
2032 	struct sk_buff *skb;
2033 	int size;
2034 
2035 	size = pfkey_xfrm_policy2msg_size(xp);
2036 
2037 	skb =  alloc_skb(size + 16, GFP_ATOMIC);
2038 	if (skb == NULL)
2039 		return ERR_PTR(-ENOBUFS);
2040 
2041 	return skb;
2042 }
2043 
2044 static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
2045 {
2046 	struct sadb_msg *hdr;
2047 	struct sadb_address *addr;
2048 	struct sadb_lifetime *lifetime;
2049 	struct sadb_x_policy *pol;
2050 	struct sadb_x_sec_ctx *sec_ctx;
2051 	struct xfrm_sec_ctx *xfrm_ctx;
2052 	int i;
2053 	int size;
2054 	int sockaddr_size = pfkey_sockaddr_size(xp->family);
2055 	int socklen = pfkey_sockaddr_len(xp->family);
2056 
2057 	size = pfkey_xfrm_policy2msg_size(xp);
2058 
2059 	/* call should fill header later */
2060 	hdr = skb_put(skb, sizeof(struct sadb_msg));
2061 	memset(hdr, 0, size);	/* XXX do we need this ? */
2062 
2063 	/* src address */
2064 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
2065 	addr->sadb_address_len =
2066 		(sizeof(struct sadb_address)+sockaddr_size)/
2067 			sizeof(uint64_t);
2068 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2069 	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2070 	addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
2071 	addr->sadb_address_reserved = 0;
2072 	if (!pfkey_sockaddr_fill(&xp->selector.saddr,
2073 				 xp->selector.sport,
2074 				 (struct sockaddr *) (addr + 1),
2075 				 xp->family))
2076 		BUG();
2077 
2078 	/* dst address */
2079 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
2080 	addr->sadb_address_len =
2081 		(sizeof(struct sadb_address)+sockaddr_size)/
2082 			sizeof(uint64_t);
2083 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2084 	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2085 	addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
2086 	addr->sadb_address_reserved = 0;
2087 
2088 	pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
2089 			    (struct sockaddr *) (addr + 1),
2090 			    xp->family);
2091 
2092 	/* hard time */
2093 	lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2094 	lifetime->sadb_lifetime_len =
2095 		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2096 	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
2097 	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.hard_packet_limit);
2098 	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
2099 	lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
2100 	lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
2101 	/* soft time */
2102 	lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2103 	lifetime->sadb_lifetime_len =
2104 		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2105 	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
2106 	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.soft_packet_limit);
2107 	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
2108 	lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
2109 	lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
2110 	/* current time */
2111 	lifetime = skb_put(skb, sizeof(struct sadb_lifetime));
2112 	lifetime->sadb_lifetime_len =
2113 		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
2114 	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
2115 	lifetime->sadb_lifetime_allocations = xp->curlft.packets;
2116 	lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
2117 	lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
2118 	lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
2119 
2120 	pol = skb_put(skb, sizeof(struct sadb_x_policy));
2121 	pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
2122 	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
2123 	pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
2124 	if (xp->action == XFRM_POLICY_ALLOW) {
2125 		if (xp->xfrm_nr)
2126 			pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
2127 		else
2128 			pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
2129 	}
2130 	pol->sadb_x_policy_dir = dir+1;
2131 	pol->sadb_x_policy_reserved = 0;
2132 	pol->sadb_x_policy_id = xp->index;
2133 	pol->sadb_x_policy_priority = xp->priority;
2134 
2135 	for (i=0; i<xp->xfrm_nr; i++) {
2136 		const struct xfrm_tmpl *t = xp->xfrm_vec + i;
2137 		struct sadb_x_ipsecrequest *rq;
2138 		int req_size;
2139 		int mode;
2140 
2141 		req_size = sizeof(struct sadb_x_ipsecrequest);
2142 		if (t->mode == XFRM_MODE_TUNNEL) {
2143 			socklen = pfkey_sockaddr_len(t->encap_family);
2144 			req_size += socklen * 2;
2145 		} else {
2146 			size -= 2*socklen;
2147 		}
2148 		rq = skb_put(skb, req_size);
2149 		pol->sadb_x_policy_len += req_size/8;
2150 		memset(rq, 0, sizeof(*rq));
2151 		rq->sadb_x_ipsecrequest_len = req_size;
2152 		rq->sadb_x_ipsecrequest_proto = t->id.proto;
2153 		if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
2154 			return -EINVAL;
2155 		rq->sadb_x_ipsecrequest_mode = mode;
2156 		rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
2157 		if (t->reqid)
2158 			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
2159 		if (t->optional)
2160 			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
2161 		rq->sadb_x_ipsecrequest_reqid = t->reqid;
2162 
2163 		if (t->mode == XFRM_MODE_TUNNEL) {
2164 			u8 *sa = (void *)(rq + 1);
2165 			pfkey_sockaddr_fill(&t->saddr, 0,
2166 					    (struct sockaddr *)sa,
2167 					    t->encap_family);
2168 			pfkey_sockaddr_fill(&t->id.daddr, 0,
2169 					    (struct sockaddr *) (sa + socklen),
2170 					    t->encap_family);
2171 		}
2172 	}
2173 
2174 	/* security context */
2175 	if ((xfrm_ctx = xp->security)) {
2176 		int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
2177 
2178 		sec_ctx = skb_put(skb, ctx_size);
2179 		sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
2180 		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
2181 		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
2182 		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
2183 		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
2184 		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
2185 		       xfrm_ctx->ctx_len);
2186 	}
2187 
2188 	hdr->sadb_msg_len = size / sizeof(uint64_t);
2189 	hdr->sadb_msg_reserved = refcount_read(&xp->refcnt);
2190 
2191 	return 0;
2192 }
2193 
2194 static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2195 {
2196 	struct sk_buff *out_skb;
2197 	struct sadb_msg *out_hdr;
2198 	int err;
2199 
2200 	out_skb = pfkey_xfrm_policy2msg_prep(xp);
2201 	if (IS_ERR(out_skb))
2202 		return PTR_ERR(out_skb);
2203 
2204 	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2205 	if (err < 0) {
2206 		kfree_skb(out_skb);
2207 		return err;
2208 	}
2209 
2210 	out_hdr = (struct sadb_msg *) out_skb->data;
2211 	out_hdr->sadb_msg_version = PF_KEY_V2;
2212 
2213 	if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2214 		out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
2215 	else
2216 		out_hdr->sadb_msg_type = event2poltype(c->event);
2217 	out_hdr->sadb_msg_errno = 0;
2218 	out_hdr->sadb_msg_seq = c->seq;
2219 	out_hdr->sadb_msg_pid = c->portid;
2220 	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
2221 	return 0;
2222 
2223 }
2224 
2225 static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2226 {
2227 	struct net *net = sock_net(sk);
2228 	int err = 0;
2229 	struct sadb_lifetime *lifetime;
2230 	struct sadb_address *sa;
2231 	struct sadb_x_policy *pol;
2232 	struct xfrm_policy *xp;
2233 	struct km_event c;
2234 	struct sadb_x_sec_ctx *sec_ctx;
2235 
2236 	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2237 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2238 	    !ext_hdrs[SADB_X_EXT_POLICY-1])
2239 		return -EINVAL;
2240 
2241 	pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2242 	if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
2243 		return -EINVAL;
2244 	if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2245 		return -EINVAL;
2246 
2247 	xp = xfrm_policy_alloc(net, GFP_KERNEL);
2248 	if (xp == NULL)
2249 		return -ENOBUFS;
2250 
2251 	xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
2252 		      XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
2253 	xp->priority = pol->sadb_x_policy_priority;
2254 
2255 	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2256 	xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
2257 	xp->selector.family = xp->family;
2258 	xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
2259 	xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2260 	xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2261 	if (xp->selector.sport)
2262 		xp->selector.sport_mask = htons(0xffff);
2263 
2264 	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2265 	pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
2266 	xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
2267 
2268 	/* Amusing, we set this twice.  KAME apps appear to set same value
2269 	 * in both addresses.
2270 	 */
2271 	xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2272 
2273 	xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2274 	if (xp->selector.dport)
2275 		xp->selector.dport_mask = htons(0xffff);
2276 
2277 	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2278 	if (sec_ctx != NULL) {
2279 		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2280 
2281 		if (!uctx) {
2282 			err = -ENOBUFS;
2283 			goto out;
2284 		}
2285 
2286 		err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
2287 		kfree(uctx);
2288 
2289 		if (err)
2290 			goto out;
2291 	}
2292 
2293 	xp->lft.soft_byte_limit = XFRM_INF;
2294 	xp->lft.hard_byte_limit = XFRM_INF;
2295 	xp->lft.soft_packet_limit = XFRM_INF;
2296 	xp->lft.hard_packet_limit = XFRM_INF;
2297 	if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
2298 		xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2299 		xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2300 		xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2301 		xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2302 	}
2303 	if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
2304 		xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
2305 		xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
2306 		xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
2307 		xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
2308 	}
2309 	xp->xfrm_nr = 0;
2310 	if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
2311 	    (err = parse_ipsecrequests(xp, pol)) < 0)
2312 		goto out;
2313 
2314 	err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
2315 				 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2316 
2317 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
2318 
2319 	if (err)
2320 		goto out;
2321 
2322 	if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2323 		c.event = XFRM_MSG_UPDPOLICY;
2324 	else
2325 		c.event = XFRM_MSG_NEWPOLICY;
2326 
2327 	c.seq = hdr->sadb_msg_seq;
2328 	c.portid = hdr->sadb_msg_pid;
2329 
2330 	km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2331 	xfrm_pol_put(xp);
2332 	return 0;
2333 
2334 out:
2335 	xp->walk.dead = 1;
2336 	xfrm_policy_destroy(xp);
2337 	return err;
2338 }
2339 
2340 static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2341 {
2342 	struct net *net = sock_net(sk);
2343 	int err;
2344 	struct sadb_address *sa;
2345 	struct sadb_x_policy *pol;
2346 	struct xfrm_policy *xp;
2347 	struct xfrm_selector sel;
2348 	struct km_event c;
2349 	struct sadb_x_sec_ctx *sec_ctx;
2350 	struct xfrm_sec_ctx *pol_ctx = NULL;
2351 
2352 	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
2353 				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
2354 	    !ext_hdrs[SADB_X_EXT_POLICY-1])
2355 		return -EINVAL;
2356 
2357 	pol = ext_hdrs[SADB_X_EXT_POLICY-1];
2358 	if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
2359 		return -EINVAL;
2360 
2361 	memset(&sel, 0, sizeof(sel));
2362 
2363 	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
2364 	sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2365 	sel.prefixlen_s = sa->sadb_address_prefixlen;
2366 	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2367 	sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
2368 	if (sel.sport)
2369 		sel.sport_mask = htons(0xffff);
2370 
2371 	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
2372 	pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2373 	sel.prefixlen_d = sa->sadb_address_prefixlen;
2374 	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2375 	sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
2376 	if (sel.dport)
2377 		sel.dport_mask = htons(0xffff);
2378 
2379 	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2380 	if (sec_ctx != NULL) {
2381 		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
2382 
2383 		if (!uctx)
2384 			return -ENOMEM;
2385 
2386 		err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
2387 		kfree(uctx);
2388 		if (err)
2389 			return err;
2390 	}
2391 
2392 	xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2393 				   pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
2394 				   1, &err);
2395 	security_xfrm_policy_free(pol_ctx);
2396 	if (xp == NULL)
2397 		return -ENOENT;
2398 
2399 	xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2400 
2401 	if (err)
2402 		goto out;
2403 
2404 	c.seq = hdr->sadb_msg_seq;
2405 	c.portid = hdr->sadb_msg_pid;
2406 	c.data.byid = 0;
2407 	c.event = XFRM_MSG_DELPOLICY;
2408 	km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
2409 
2410 out:
2411 	xfrm_pol_put(xp);
2412 	return err;
2413 }
2414 
2415 static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2416 {
2417 	int err;
2418 	struct sk_buff *out_skb;
2419 	struct sadb_msg *out_hdr;
2420 	err = 0;
2421 
2422 	out_skb = pfkey_xfrm_policy2msg_prep(xp);
2423 	if (IS_ERR(out_skb)) {
2424 		err =  PTR_ERR(out_skb);
2425 		goto out;
2426 	}
2427 	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2428 	if (err < 0)
2429 		goto out;
2430 
2431 	out_hdr = (struct sadb_msg *) out_skb->data;
2432 	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2433 	out_hdr->sadb_msg_type = hdr->sadb_msg_type;
2434 	out_hdr->sadb_msg_satype = 0;
2435 	out_hdr->sadb_msg_errno = 0;
2436 	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
2437 	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
2438 	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
2439 	err = 0;
2440 
2441 out:
2442 	return err;
2443 }
2444 
2445 static int pfkey_sockaddr_pair_size(sa_family_t family)
2446 {
2447 	return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2448 }
2449 
2450 static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2451 			       xfrm_address_t *saddr, xfrm_address_t *daddr,
2452 			       u16 *family)
2453 {
2454 	int af, socklen;
2455 
2456 	if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2457 		return -EINVAL;
2458 
2459 	af = pfkey_sockaddr_extract(sa, saddr);
2460 	if (!af)
2461 		return -EINVAL;
2462 
2463 	socklen = pfkey_sockaddr_len(af);
2464 	if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2465 				   daddr) != af)
2466 		return -EINVAL;
2467 
2468 	*family = af;
2469 	return 0;
2470 }
2471 
2472 #ifdef CONFIG_NET_KEY_MIGRATE
2473 static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
2474 				    struct xfrm_migrate *m)
2475 {
2476 	int err;
2477 	struct sadb_x_ipsecrequest *rq2;
2478 	int mode;
2479 
2480 	if (len < sizeof(*rq1) ||
2481 	    len < rq1->sadb_x_ipsecrequest_len ||
2482 	    rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
2483 		return -EINVAL;
2484 
2485 	/* old endoints */
2486 	err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
2487 				  rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
2488 				  &m->old_saddr, &m->old_daddr,
2489 				  &m->old_family);
2490 	if (err)
2491 		return err;
2492 
2493 	rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
2494 	len -= rq1->sadb_x_ipsecrequest_len;
2495 
2496 	if (len <= sizeof(*rq2) ||
2497 	    len < rq2->sadb_x_ipsecrequest_len ||
2498 	    rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
2499 		return -EINVAL;
2500 
2501 	/* new endpoints */
2502 	err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
2503 				  rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
2504 				  &m->new_saddr, &m->new_daddr,
2505 				  &m->new_family);
2506 	if (err)
2507 		return err;
2508 
2509 	if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
2510 	    rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
2511 	    rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
2512 		return -EINVAL;
2513 
2514 	m->proto = rq1->sadb_x_ipsecrequest_proto;
2515 	if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
2516 		return -EINVAL;
2517 	m->mode = mode;
2518 	m->reqid = rq1->sadb_x_ipsecrequest_reqid;
2519 
2520 	return ((int)(rq1->sadb_x_ipsecrequest_len +
2521 		      rq2->sadb_x_ipsecrequest_len));
2522 }
2523 
2524 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2525 			 const struct sadb_msg *hdr, void * const *ext_hdrs)
2526 {
2527 	int i, len, ret, err = -EINVAL;
2528 	u8 dir;
2529 	struct sadb_address *sa;
2530 	struct sadb_x_kmaddress *kma;
2531 	struct sadb_x_policy *pol;
2532 	struct sadb_x_ipsecrequest *rq;
2533 	struct xfrm_selector sel;
2534 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
2535 	struct xfrm_kmaddress k;
2536 	struct net *net = sock_net(sk);
2537 
2538 	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2539 				     ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2540 	    !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
2541 		err = -EINVAL;
2542 		goto out;
2543 	}
2544 
2545 	kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2546 	pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
2547 
2548 	if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
2549 		err = -EINVAL;
2550 		goto out;
2551 	}
2552 
2553 	if (kma) {
2554 		/* convert sadb_x_kmaddress to xfrm_kmaddress */
2555 		k.reserved = kma->sadb_x_kmaddress_reserved;
2556 		ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
2557 					  8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
2558 					  &k.local, &k.remote, &k.family);
2559 		if (ret < 0) {
2560 			err = ret;
2561 			goto out;
2562 		}
2563 	}
2564 
2565 	dir = pol->sadb_x_policy_dir - 1;
2566 	memset(&sel, 0, sizeof(sel));
2567 
2568 	/* set source address info of selector */
2569 	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
2570 	sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
2571 	sel.prefixlen_s = sa->sadb_address_prefixlen;
2572 	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2573 	sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2574 	if (sel.sport)
2575 		sel.sport_mask = htons(0xffff);
2576 
2577 	/* set destination address info of selector */
2578 	sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
2579 	pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
2580 	sel.prefixlen_d = sa->sadb_address_prefixlen;
2581 	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
2582 	sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
2583 	if (sel.dport)
2584 		sel.dport_mask = htons(0xffff);
2585 
2586 	rq = (struct sadb_x_ipsecrequest *)(pol + 1);
2587 
2588 	/* extract ipsecrequests */
2589 	i = 0;
2590 	len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
2591 
2592 	while (len > 0 && i < XFRM_MAX_DEPTH) {
2593 		ret = ipsecrequests_to_migrate(rq, len, &m[i]);
2594 		if (ret < 0) {
2595 			err = ret;
2596 			goto out;
2597 		} else {
2598 			rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
2599 			len -= ret;
2600 			i++;
2601 		}
2602 	}
2603 
2604 	if (!i || len > 0) {
2605 		err = -EINVAL;
2606 		goto out;
2607 	}
2608 
2609 	return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2610 			    kma ? &k : NULL, net, NULL);
2611 
2612  out:
2613 	return err;
2614 }
2615 #else
2616 static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2617 			 const struct sadb_msg *hdr, void * const *ext_hdrs)
2618 {
2619 	return -ENOPROTOOPT;
2620 }
2621 #endif
2622 
2623 
2624 static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2625 {
2626 	struct net *net = sock_net(sk);
2627 	unsigned int dir;
2628 	int err = 0, delete;
2629 	struct sadb_x_policy *pol;
2630 	struct xfrm_policy *xp;
2631 	struct km_event c;
2632 
2633 	if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
2634 		return -EINVAL;
2635 
2636 	dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
2637 	if (dir >= XFRM_POLICY_MAX)
2638 		return -EINVAL;
2639 
2640 	delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2641 	xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2642 			      dir, pol->sadb_x_policy_id, delete, &err);
2643 	if (xp == NULL)
2644 		return -ENOENT;
2645 
2646 	if (delete) {
2647 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
2648 
2649 		if (err)
2650 			goto out;
2651 		c.seq = hdr->sadb_msg_seq;
2652 		c.portid = hdr->sadb_msg_pid;
2653 		c.data.byid = 1;
2654 		c.event = XFRM_MSG_DELPOLICY;
2655 		km_policy_notify(xp, dir, &c);
2656 	} else {
2657 		err = key_pol_get_resp(sk, xp, hdr, dir);
2658 	}
2659 
2660 out:
2661 	xfrm_pol_put(xp);
2662 	return err;
2663 }
2664 
2665 static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
2666 {
2667 	struct pfkey_sock *pfk = ptr;
2668 	struct sk_buff *out_skb;
2669 	struct sadb_msg *out_hdr;
2670 	int err;
2671 
2672 	if (!pfkey_can_dump(&pfk->sk))
2673 		return -ENOBUFS;
2674 
2675 	out_skb = pfkey_xfrm_policy2msg_prep(xp);
2676 	if (IS_ERR(out_skb))
2677 		return PTR_ERR(out_skb);
2678 
2679 	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
2680 	if (err < 0)
2681 		return err;
2682 
2683 	out_hdr = (struct sadb_msg *) out_skb->data;
2684 	out_hdr->sadb_msg_version = pfk->dump.msg_version;
2685 	out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
2686 	out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2687 	out_hdr->sadb_msg_errno = 0;
2688 	out_hdr->sadb_msg_seq = count + 1;
2689 	out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
2690 
2691 	if (pfk->dump.skb)
2692 		pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
2693 				&pfk->sk, sock_net(&pfk->sk));
2694 	pfk->dump.skb = out_skb;
2695 
2696 	return 0;
2697 }
2698 
2699 static int pfkey_dump_sp(struct pfkey_sock *pfk)
2700 {
2701 	struct net *net = sock_net(&pfk->sk);
2702 	return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2703 }
2704 
2705 static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
2706 {
2707 	struct net *net = sock_net((struct sock *)pfk);
2708 
2709 	xfrm_policy_walk_done(&pfk->dump.u.policy, net);
2710 }
2711 
2712 static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2713 {
2714 	struct pfkey_sock *pfk = pfkey_sk(sk);
2715 
2716 	mutex_lock(&pfk->dump_lock);
2717 	if (pfk->dump.dump != NULL) {
2718 		mutex_unlock(&pfk->dump_lock);
2719 		return -EBUSY;
2720 	}
2721 
2722 	pfk->dump.msg_version = hdr->sadb_msg_version;
2723 	pfk->dump.msg_portid = hdr->sadb_msg_pid;
2724 	pfk->dump.dump = pfkey_dump_sp;
2725 	pfk->dump.done = pfkey_dump_sp_done;
2726 	xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
2727 	mutex_unlock(&pfk->dump_lock);
2728 
2729 	return pfkey_do_dump(pfk);
2730 }
2731 
2732 static int key_notify_policy_flush(const struct km_event *c)
2733 {
2734 	struct sk_buff *skb_out;
2735 	struct sadb_msg *hdr;
2736 
2737 	skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
2738 	if (!skb_out)
2739 		return -ENOBUFS;
2740 	hdr = skb_put(skb_out, sizeof(struct sadb_msg));
2741 	hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2742 	hdr->sadb_msg_seq = c->seq;
2743 	hdr->sadb_msg_pid = c->portid;
2744 	hdr->sadb_msg_version = PF_KEY_V2;
2745 	hdr->sadb_msg_errno = (uint8_t) 0;
2746 	hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2747 	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2748 	hdr->sadb_msg_reserved = 0;
2749 	pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
2750 	return 0;
2751 
2752 }
2753 
2754 static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2755 {
2756 	struct net *net = sock_net(sk);
2757 	struct km_event c;
2758 	int err, err2;
2759 
2760 	err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
2761 	err2 = unicast_flush_resp(sk, hdr);
2762 	if (err || err2) {
2763 		if (err == -ESRCH) /* empty table - old silent behavior */
2764 			return 0;
2765 		return err;
2766 	}
2767 
2768 	c.data.type = XFRM_POLICY_TYPE_MAIN;
2769 	c.event = XFRM_MSG_FLUSHPOLICY;
2770 	c.portid = hdr->sadb_msg_pid;
2771 	c.seq = hdr->sadb_msg_seq;
2772 	c.net = net;
2773 	km_policy_notify(NULL, 0, &c);
2774 
2775 	return 0;
2776 }
2777 
2778 typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2779 			     const struct sadb_msg *hdr, void * const *ext_hdrs);
2780 static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
2781 	[SADB_RESERVED]		= pfkey_reserved,
2782 	[SADB_GETSPI]		= pfkey_getspi,
2783 	[SADB_UPDATE]		= pfkey_add,
2784 	[SADB_ADD]		= pfkey_add,
2785 	[SADB_DELETE]		= pfkey_delete,
2786 	[SADB_GET]		= pfkey_get,
2787 	[SADB_ACQUIRE]		= pfkey_acquire,
2788 	[SADB_REGISTER]		= pfkey_register,
2789 	[SADB_EXPIRE]		= NULL,
2790 	[SADB_FLUSH]		= pfkey_flush,
2791 	[SADB_DUMP]		= pfkey_dump,
2792 	[SADB_X_PROMISC]	= pfkey_promisc,
2793 	[SADB_X_PCHANGE]	= NULL,
2794 	[SADB_X_SPDUPDATE]	= pfkey_spdadd,
2795 	[SADB_X_SPDADD]		= pfkey_spdadd,
2796 	[SADB_X_SPDDELETE]	= pfkey_spddelete,
2797 	[SADB_X_SPDGET]		= pfkey_spdget,
2798 	[SADB_X_SPDACQUIRE]	= NULL,
2799 	[SADB_X_SPDDUMP]	= pfkey_spddump,
2800 	[SADB_X_SPDFLUSH]	= pfkey_spdflush,
2801 	[SADB_X_SPDSETIDX]	= pfkey_spdadd,
2802 	[SADB_X_SPDDELETE2]	= pfkey_spdget,
2803 	[SADB_X_MIGRATE]	= pfkey_migrate,
2804 };
2805 
2806 static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
2807 {
2808 	void *ext_hdrs[SADB_EXT_MAX];
2809 	int err;
2810 
2811 	pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
2812 			BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
2813 
2814 	memset(ext_hdrs, 0, sizeof(ext_hdrs));
2815 	err = parse_exthdrs(skb, hdr, ext_hdrs);
2816 	if (!err) {
2817 		err = -EOPNOTSUPP;
2818 		if (pfkey_funcs[hdr->sadb_msg_type])
2819 			err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
2820 	}
2821 	return err;
2822 }
2823 
2824 static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
2825 {
2826 	struct sadb_msg *hdr = NULL;
2827 
2828 	if (skb->len < sizeof(*hdr)) {
2829 		*errp = -EMSGSIZE;
2830 	} else {
2831 		hdr = (struct sadb_msg *) skb->data;
2832 		if (hdr->sadb_msg_version != PF_KEY_V2 ||
2833 		    hdr->sadb_msg_reserved != 0 ||
2834 		    (hdr->sadb_msg_type <= SADB_RESERVED ||
2835 		     hdr->sadb_msg_type > SADB_MAX)) {
2836 			hdr = NULL;
2837 			*errp = -EINVAL;
2838 		} else if (hdr->sadb_msg_len != (skb->len /
2839 						 sizeof(uint64_t)) ||
2840 			   hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
2841 						sizeof(uint64_t))) {
2842 			hdr = NULL;
2843 			*errp = -EMSGSIZE;
2844 		} else {
2845 			*errp = 0;
2846 		}
2847 	}
2848 	return hdr;
2849 }
2850 
2851 static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
2852 				const struct xfrm_algo_desc *d)
2853 {
2854 	unsigned int id = d->desc.sadb_alg_id;
2855 
2856 	if (id >= sizeof(t->aalgos) * 8)
2857 		return 0;
2858 
2859 	return (t->aalgos >> id) & 1;
2860 }
2861 
2862 static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
2863 				const struct xfrm_algo_desc *d)
2864 {
2865 	unsigned int id = d->desc.sadb_alg_id;
2866 
2867 	if (id >= sizeof(t->ealgos) * 8)
2868 		return 0;
2869 
2870 	return (t->ealgos >> id) & 1;
2871 }
2872 
2873 static int count_ah_combs(const struct xfrm_tmpl *t)
2874 {
2875 	int i, sz = 0;
2876 
2877 	for (i = 0; ; i++) {
2878 		const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2879 		if (!aalg)
2880 			break;
2881 		if (!aalg->pfkey_supported)
2882 			continue;
2883 		if (aalg_tmpl_set(t, aalg) && aalg->available)
2884 			sz += sizeof(struct sadb_comb);
2885 	}
2886 	return sz + sizeof(struct sadb_prop);
2887 }
2888 
2889 static int count_esp_combs(const struct xfrm_tmpl *t)
2890 {
2891 	int i, k, sz = 0;
2892 
2893 	for (i = 0; ; i++) {
2894 		const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2895 		if (!ealg)
2896 			break;
2897 
2898 		if (!ealg->pfkey_supported)
2899 			continue;
2900 
2901 		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2902 			continue;
2903 
2904 		for (k = 1; ; k++) {
2905 			const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2906 			if (!aalg)
2907 				break;
2908 
2909 			if (!aalg->pfkey_supported)
2910 				continue;
2911 
2912 			if (aalg_tmpl_set(t, aalg) && aalg->available)
2913 				sz += sizeof(struct sadb_comb);
2914 		}
2915 	}
2916 	return sz + sizeof(struct sadb_prop);
2917 }
2918 
2919 static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2920 {
2921 	struct sadb_prop *p;
2922 	int i;
2923 
2924 	p = skb_put(skb, sizeof(struct sadb_prop));
2925 	p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2926 	p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2927 	p->sadb_prop_replay = 32;
2928 	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2929 
2930 	for (i = 0; ; i++) {
2931 		const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
2932 		if (!aalg)
2933 			break;
2934 
2935 		if (!aalg->pfkey_supported)
2936 			continue;
2937 
2938 		if (aalg_tmpl_set(t, aalg) && aalg->available) {
2939 			struct sadb_comb *c;
2940 			c = skb_put_zero(skb, sizeof(struct sadb_comb));
2941 			p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2942 			c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2943 			c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2944 			c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2945 			c->sadb_comb_hard_addtime = 24*60*60;
2946 			c->sadb_comb_soft_addtime = 20*60*60;
2947 			c->sadb_comb_hard_usetime = 8*60*60;
2948 			c->sadb_comb_soft_usetime = 7*60*60;
2949 		}
2950 	}
2951 }
2952 
2953 static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
2954 {
2955 	struct sadb_prop *p;
2956 	int i, k;
2957 
2958 	p = skb_put(skb, sizeof(struct sadb_prop));
2959 	p->sadb_prop_len = sizeof(struct sadb_prop)/8;
2960 	p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
2961 	p->sadb_prop_replay = 32;
2962 	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
2963 
2964 	for (i=0; ; i++) {
2965 		const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
2966 		if (!ealg)
2967 			break;
2968 
2969 		if (!ealg->pfkey_supported)
2970 			continue;
2971 
2972 		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
2973 			continue;
2974 
2975 		for (k = 1; ; k++) {
2976 			struct sadb_comb *c;
2977 			const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
2978 			if (!aalg)
2979 				break;
2980 			if (!aalg->pfkey_supported)
2981 				continue;
2982 			if (!(aalg_tmpl_set(t, aalg) && aalg->available))
2983 				continue;
2984 			c = skb_put(skb, sizeof(struct sadb_comb));
2985 			memset(c, 0, sizeof(*c));
2986 			p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2987 			c->sadb_comb_auth = aalg->desc.sadb_alg_id;
2988 			c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
2989 			c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
2990 			c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
2991 			c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
2992 			c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
2993 			c->sadb_comb_hard_addtime = 24*60*60;
2994 			c->sadb_comb_soft_addtime = 20*60*60;
2995 			c->sadb_comb_hard_usetime = 8*60*60;
2996 			c->sadb_comb_soft_usetime = 7*60*60;
2997 		}
2998 	}
2999 }
3000 
3001 static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
3002 {
3003 	return 0;
3004 }
3005 
3006 static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
3007 {
3008 	struct sk_buff *out_skb;
3009 	struct sadb_msg *out_hdr;
3010 	int hard;
3011 	int hsc;
3012 
3013 	hard = c->data.hard;
3014 	if (hard)
3015 		hsc = 2;
3016 	else
3017 		hsc = 1;
3018 
3019 	out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
3020 	if (IS_ERR(out_skb))
3021 		return PTR_ERR(out_skb);
3022 
3023 	out_hdr = (struct sadb_msg *) out_skb->data;
3024 	out_hdr->sadb_msg_version = PF_KEY_V2;
3025 	out_hdr->sadb_msg_type = SADB_EXPIRE;
3026 	out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3027 	out_hdr->sadb_msg_errno = 0;
3028 	out_hdr->sadb_msg_reserved = 0;
3029 	out_hdr->sadb_msg_seq = 0;
3030 	out_hdr->sadb_msg_pid = 0;
3031 
3032 	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3033 			xs_net(x));
3034 	return 0;
3035 }
3036 
3037 static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
3038 {
3039 	struct net *net = x ? xs_net(x) : c->net;
3040 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3041 
3042 	if (atomic_read(&net_pfkey->socks_nr) == 0)
3043 		return 0;
3044 
3045 	switch (c->event) {
3046 	case XFRM_MSG_EXPIRE:
3047 		return key_notify_sa_expire(x, c);
3048 	case XFRM_MSG_DELSA:
3049 	case XFRM_MSG_NEWSA:
3050 	case XFRM_MSG_UPDSA:
3051 		return key_notify_sa(x, c);
3052 	case XFRM_MSG_FLUSHSA:
3053 		return key_notify_sa_flush(c);
3054 	case XFRM_MSG_NEWAE: /* not yet supported */
3055 		break;
3056 	default:
3057 		pr_err("pfkey: Unknown SA event %d\n", c->event);
3058 		break;
3059 	}
3060 
3061 	return 0;
3062 }
3063 
3064 static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3065 {
3066 	if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
3067 		return 0;
3068 
3069 	switch (c->event) {
3070 	case XFRM_MSG_POLEXPIRE:
3071 		return key_notify_policy_expire(xp, c);
3072 	case XFRM_MSG_DELPOLICY:
3073 	case XFRM_MSG_NEWPOLICY:
3074 	case XFRM_MSG_UPDPOLICY:
3075 		return key_notify_policy(xp, dir, c);
3076 	case XFRM_MSG_FLUSHPOLICY:
3077 		if (c->data.type != XFRM_POLICY_TYPE_MAIN)
3078 			break;
3079 		return key_notify_policy_flush(c);
3080 	default:
3081 		pr_err("pfkey: Unknown policy event %d\n", c->event);
3082 		break;
3083 	}
3084 
3085 	return 0;
3086 }
3087 
3088 static u32 get_acqseq(void)
3089 {
3090 	u32 res;
3091 	static atomic_t acqseq;
3092 
3093 	do {
3094 		res = atomic_inc_return(&acqseq);
3095 	} while (!res);
3096 	return res;
3097 }
3098 
3099 static bool pfkey_is_alive(const struct km_event *c)
3100 {
3101 	struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
3102 	struct sock *sk;
3103 	bool is_alive = false;
3104 
3105 	rcu_read_lock();
3106 	sk_for_each_rcu(sk, &net_pfkey->table) {
3107 		if (pfkey_sk(sk)->registered) {
3108 			is_alive = true;
3109 			break;
3110 		}
3111 	}
3112 	rcu_read_unlock();
3113 
3114 	return is_alive;
3115 }
3116 
3117 static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
3118 {
3119 	struct sk_buff *skb;
3120 	struct sadb_msg *hdr;
3121 	struct sadb_address *addr;
3122 	struct sadb_x_policy *pol;
3123 	int sockaddr_size;
3124 	int size;
3125 	struct sadb_x_sec_ctx *sec_ctx;
3126 	struct xfrm_sec_ctx *xfrm_ctx;
3127 	int ctx_size = 0;
3128 
3129 	sockaddr_size = pfkey_sockaddr_size(x->props.family);
3130 	if (!sockaddr_size)
3131 		return -EINVAL;
3132 
3133 	size = sizeof(struct sadb_msg) +
3134 		(sizeof(struct sadb_address) * 2) +
3135 		(sockaddr_size * 2) +
3136 		sizeof(struct sadb_x_policy);
3137 
3138 	if (x->id.proto == IPPROTO_AH)
3139 		size += count_ah_combs(t);
3140 	else if (x->id.proto == IPPROTO_ESP)
3141 		size += count_esp_combs(t);
3142 
3143 	if ((xfrm_ctx = x->security)) {
3144 		ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
3145 		size +=  sizeof(struct sadb_x_sec_ctx) + ctx_size;
3146 	}
3147 
3148 	skb =  alloc_skb(size + 16, GFP_ATOMIC);
3149 	if (skb == NULL)
3150 		return -ENOMEM;
3151 
3152 	hdr = skb_put(skb, sizeof(struct sadb_msg));
3153 	hdr->sadb_msg_version = PF_KEY_V2;
3154 	hdr->sadb_msg_type = SADB_ACQUIRE;
3155 	hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
3156 	hdr->sadb_msg_len = size / sizeof(uint64_t);
3157 	hdr->sadb_msg_errno = 0;
3158 	hdr->sadb_msg_reserved = 0;
3159 	hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3160 	hdr->sadb_msg_pid = 0;
3161 
3162 	/* src address */
3163 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3164 	addr->sadb_address_len =
3165 		(sizeof(struct sadb_address)+sockaddr_size)/
3166 			sizeof(uint64_t);
3167 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3168 	addr->sadb_address_proto = 0;
3169 	addr->sadb_address_reserved = 0;
3170 	addr->sadb_address_prefixlen =
3171 		pfkey_sockaddr_fill(&x->props.saddr, 0,
3172 				    (struct sockaddr *) (addr + 1),
3173 				    x->props.family);
3174 	if (!addr->sadb_address_prefixlen)
3175 		BUG();
3176 
3177 	/* dst address */
3178 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3179 	addr->sadb_address_len =
3180 		(sizeof(struct sadb_address)+sockaddr_size)/
3181 			sizeof(uint64_t);
3182 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3183 	addr->sadb_address_proto = 0;
3184 	addr->sadb_address_reserved = 0;
3185 	addr->sadb_address_prefixlen =
3186 		pfkey_sockaddr_fill(&x->id.daddr, 0,
3187 				    (struct sockaddr *) (addr + 1),
3188 				    x->props.family);
3189 	if (!addr->sadb_address_prefixlen)
3190 		BUG();
3191 
3192 	pol = skb_put(skb, sizeof(struct sadb_x_policy));
3193 	pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
3194 	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3195 	pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3196 	pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
3197 	pol->sadb_x_policy_reserved = 0;
3198 	pol->sadb_x_policy_id = xp->index;
3199 	pol->sadb_x_policy_priority = xp->priority;
3200 
3201 	/* Set sadb_comb's. */
3202 	if (x->id.proto == IPPROTO_AH)
3203 		dump_ah_combs(skb, t);
3204 	else if (x->id.proto == IPPROTO_ESP)
3205 		dump_esp_combs(skb, t);
3206 
3207 	/* security context */
3208 	if (xfrm_ctx) {
3209 		sec_ctx = skb_put(skb,
3210 				  sizeof(struct sadb_x_sec_ctx) + ctx_size);
3211 		sec_ctx->sadb_x_sec_len =
3212 		  (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
3213 		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
3214 		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
3215 		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
3216 		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
3217 		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
3218 		       xfrm_ctx->ctx_len);
3219 	}
3220 
3221 	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3222 			       xs_net(x));
3223 }
3224 
3225 static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3226 						u8 *data, int len, int *dir)
3227 {
3228 	struct net *net = sock_net(sk);
3229 	struct xfrm_policy *xp;
3230 	struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3231 	struct sadb_x_sec_ctx *sec_ctx;
3232 
3233 	switch (sk->sk_family) {
3234 	case AF_INET:
3235 		if (opt != IP_IPSEC_POLICY) {
3236 			*dir = -EOPNOTSUPP;
3237 			return NULL;
3238 		}
3239 		break;
3240 #if IS_ENABLED(CONFIG_IPV6)
3241 	case AF_INET6:
3242 		if (opt != IPV6_IPSEC_POLICY) {
3243 			*dir = -EOPNOTSUPP;
3244 			return NULL;
3245 		}
3246 		break;
3247 #endif
3248 	default:
3249 		*dir = -EINVAL;
3250 		return NULL;
3251 	}
3252 
3253 	*dir = -EINVAL;
3254 
3255 	if (len < sizeof(struct sadb_x_policy) ||
3256 	    pol->sadb_x_policy_len*8 > len ||
3257 	    pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
3258 	    (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
3259 		return NULL;
3260 
3261 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3262 	if (xp == NULL) {
3263 		*dir = -ENOBUFS;
3264 		return NULL;
3265 	}
3266 
3267 	xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
3268 		      XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
3269 
3270 	xp->lft.soft_byte_limit = XFRM_INF;
3271 	xp->lft.hard_byte_limit = XFRM_INF;
3272 	xp->lft.soft_packet_limit = XFRM_INF;
3273 	xp->lft.hard_packet_limit = XFRM_INF;
3274 	xp->family = sk->sk_family;
3275 
3276 	xp->xfrm_nr = 0;
3277 	if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
3278 	    (*dir = parse_ipsecrequests(xp, pol)) < 0)
3279 		goto out;
3280 
3281 	/* security context too */
3282 	if (len >= (pol->sadb_x_policy_len*8 +
3283 	    sizeof(struct sadb_x_sec_ctx))) {
3284 		char *p = (char *)pol;
3285 		struct xfrm_user_sec_ctx *uctx;
3286 
3287 		p += pol->sadb_x_policy_len*8;
3288 		sec_ctx = (struct sadb_x_sec_ctx *)p;
3289 		if (len < pol->sadb_x_policy_len*8 +
3290 		    sec_ctx->sadb_x_sec_len*8) {
3291 			*dir = -EINVAL;
3292 			goto out;
3293 		}
3294 		if ((*dir = verify_sec_ctx_len(p)))
3295 			goto out;
3296 		uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
3297 		*dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
3298 		kfree(uctx);
3299 
3300 		if (*dir)
3301 			goto out;
3302 	}
3303 
3304 	*dir = pol->sadb_x_policy_dir-1;
3305 	return xp;
3306 
3307 out:
3308 	xp->walk.dead = 1;
3309 	xfrm_policy_destroy(xp);
3310 	return NULL;
3311 }
3312 
3313 static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
3314 {
3315 	struct sk_buff *skb;
3316 	struct sadb_msg *hdr;
3317 	struct sadb_sa *sa;
3318 	struct sadb_address *addr;
3319 	struct sadb_x_nat_t_port *n_port;
3320 	int sockaddr_size;
3321 	int size;
3322 	__u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
3323 	struct xfrm_encap_tmpl *natt = NULL;
3324 
3325 	sockaddr_size = pfkey_sockaddr_size(x->props.family);
3326 	if (!sockaddr_size)
3327 		return -EINVAL;
3328 
3329 	if (!satype)
3330 		return -EINVAL;
3331 
3332 	if (!x->encap)
3333 		return -EINVAL;
3334 
3335 	natt = x->encap;
3336 
3337 	/* Build an SADB_X_NAT_T_NEW_MAPPING message:
3338 	 *
3339 	 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
3340 	 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
3341 	 */
3342 
3343 	size = sizeof(struct sadb_msg) +
3344 		sizeof(struct sadb_sa) +
3345 		(sizeof(struct sadb_address) * 2) +
3346 		(sockaddr_size * 2) +
3347 		(sizeof(struct sadb_x_nat_t_port) * 2);
3348 
3349 	skb =  alloc_skb(size + 16, GFP_ATOMIC);
3350 	if (skb == NULL)
3351 		return -ENOMEM;
3352 
3353 	hdr = skb_put(skb, sizeof(struct sadb_msg));
3354 	hdr->sadb_msg_version = PF_KEY_V2;
3355 	hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
3356 	hdr->sadb_msg_satype = satype;
3357 	hdr->sadb_msg_len = size / sizeof(uint64_t);
3358 	hdr->sadb_msg_errno = 0;
3359 	hdr->sadb_msg_reserved = 0;
3360 	hdr->sadb_msg_seq = x->km.seq = get_acqseq();
3361 	hdr->sadb_msg_pid = 0;
3362 
3363 	/* SA */
3364 	sa = skb_put(skb, sizeof(struct sadb_sa));
3365 	sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
3366 	sa->sadb_sa_exttype = SADB_EXT_SA;
3367 	sa->sadb_sa_spi = x->id.spi;
3368 	sa->sadb_sa_replay = 0;
3369 	sa->sadb_sa_state = 0;
3370 	sa->sadb_sa_auth = 0;
3371 	sa->sadb_sa_encrypt = 0;
3372 	sa->sadb_sa_flags = 0;
3373 
3374 	/* ADDRESS_SRC (old addr) */
3375 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3376 	addr->sadb_address_len =
3377 		(sizeof(struct sadb_address)+sockaddr_size)/
3378 			sizeof(uint64_t);
3379 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
3380 	addr->sadb_address_proto = 0;
3381 	addr->sadb_address_reserved = 0;
3382 	addr->sadb_address_prefixlen =
3383 		pfkey_sockaddr_fill(&x->props.saddr, 0,
3384 				    (struct sockaddr *) (addr + 1),
3385 				    x->props.family);
3386 	if (!addr->sadb_address_prefixlen)
3387 		BUG();
3388 
3389 	/* NAT_T_SPORT (old port) */
3390 	n_port = skb_put(skb, sizeof(*n_port));
3391 	n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3392 	n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
3393 	n_port->sadb_x_nat_t_port_port = natt->encap_sport;
3394 	n_port->sadb_x_nat_t_port_reserved = 0;
3395 
3396 	/* ADDRESS_DST (new addr) */
3397 	addr = skb_put(skb, sizeof(struct sadb_address) + sockaddr_size);
3398 	addr->sadb_address_len =
3399 		(sizeof(struct sadb_address)+sockaddr_size)/
3400 			sizeof(uint64_t);
3401 	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
3402 	addr->sadb_address_proto = 0;
3403 	addr->sadb_address_reserved = 0;
3404 	addr->sadb_address_prefixlen =
3405 		pfkey_sockaddr_fill(ipaddr, 0,
3406 				    (struct sockaddr *) (addr + 1),
3407 				    x->props.family);
3408 	if (!addr->sadb_address_prefixlen)
3409 		BUG();
3410 
3411 	/* NAT_T_DPORT (new port) */
3412 	n_port = skb_put(skb, sizeof(*n_port));
3413 	n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
3414 	n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
3415 	n_port->sadb_x_nat_t_port_port = sport;
3416 	n_port->sadb_x_nat_t_port_reserved = 0;
3417 
3418 	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
3419 			       xs_net(x));
3420 }
3421 
3422 #ifdef CONFIG_NET_KEY_MIGRATE
3423 static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3424 			    const struct xfrm_selector *sel)
3425 {
3426 	struct sadb_address *addr;
3427 	addr = skb_put(skb, sizeof(struct sadb_address) + sasize);
3428 	addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
3429 	addr->sadb_address_exttype = type;
3430 	addr->sadb_address_proto = sel->proto;
3431 	addr->sadb_address_reserved = 0;
3432 
3433 	switch (type) {
3434 	case SADB_EXT_ADDRESS_SRC:
3435 		addr->sadb_address_prefixlen = sel->prefixlen_s;
3436 		pfkey_sockaddr_fill(&sel->saddr, 0,
3437 				    (struct sockaddr *)(addr + 1),
3438 				    sel->family);
3439 		break;
3440 	case SADB_EXT_ADDRESS_DST:
3441 		addr->sadb_address_prefixlen = sel->prefixlen_d;
3442 		pfkey_sockaddr_fill(&sel->daddr, 0,
3443 				    (struct sockaddr *)(addr + 1),
3444 				    sel->family);
3445 		break;
3446 	default:
3447 		return -EINVAL;
3448 	}
3449 
3450 	return 0;
3451 }
3452 
3453 
3454 static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3455 {
3456 	struct sadb_x_kmaddress *kma;
3457 	u8 *sa;
3458 	int family = k->family;
3459 	int socklen = pfkey_sockaddr_len(family);
3460 	int size_req;
3461 
3462 	size_req = (sizeof(struct sadb_x_kmaddress) +
3463 		    pfkey_sockaddr_pair_size(family));
3464 
3465 	kma = skb_put_zero(skb, size_req);
3466 	kma->sadb_x_kmaddress_len = size_req / 8;
3467 	kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
3468 	kma->sadb_x_kmaddress_reserved = k->reserved;
3469 
3470 	sa = (u8 *)(kma + 1);
3471 	if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
3472 	    !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
3473 		return -EINVAL;
3474 
3475 	return 0;
3476 }
3477 
3478 static int set_ipsecrequest(struct sk_buff *skb,
3479 			    uint8_t proto, uint8_t mode, int level,
3480 			    uint32_t reqid, uint8_t family,
3481 			    const xfrm_address_t *src, const xfrm_address_t *dst)
3482 {
3483 	struct sadb_x_ipsecrequest *rq;
3484 	u8 *sa;
3485 	int socklen = pfkey_sockaddr_len(family);
3486 	int size_req;
3487 
3488 	size_req = sizeof(struct sadb_x_ipsecrequest) +
3489 		   pfkey_sockaddr_pair_size(family);
3490 
3491 	rq = skb_put_zero(skb, size_req);
3492 	rq->sadb_x_ipsecrequest_len = size_req;
3493 	rq->sadb_x_ipsecrequest_proto = proto;
3494 	rq->sadb_x_ipsecrequest_mode = mode;
3495 	rq->sadb_x_ipsecrequest_level = level;
3496 	rq->sadb_x_ipsecrequest_reqid = reqid;
3497 
3498 	sa = (u8 *) (rq + 1);
3499 	if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
3500 	    !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3501 		return -EINVAL;
3502 
3503 	return 0;
3504 }
3505 #endif
3506 
3507 #ifdef CONFIG_NET_KEY_MIGRATE
3508 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3509 			      const struct xfrm_migrate *m, int num_bundles,
3510 			      const struct xfrm_kmaddress *k,
3511 			      const struct xfrm_encap_tmpl *encap)
3512 {
3513 	int i;
3514 	int sasize_sel;
3515 	int size = 0;
3516 	int size_pol = 0;
3517 	struct sk_buff *skb;
3518 	struct sadb_msg *hdr;
3519 	struct sadb_x_policy *pol;
3520 	const struct xfrm_migrate *mp;
3521 
3522 	if (type != XFRM_POLICY_TYPE_MAIN)
3523 		return 0;
3524 
3525 	if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
3526 		return -EINVAL;
3527 
3528 	if (k != NULL) {
3529 		/* addresses for KM */
3530 		size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
3531 				     pfkey_sockaddr_pair_size(k->family));
3532 	}
3533 
3534 	/* selector */
3535 	sasize_sel = pfkey_sockaddr_size(sel->family);
3536 	if (!sasize_sel)
3537 		return -EINVAL;
3538 	size += (sizeof(struct sadb_address) + sasize_sel) * 2;
3539 
3540 	/* policy info */
3541 	size_pol += sizeof(struct sadb_x_policy);
3542 
3543 	/* ipsecrequests */
3544 	for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3545 		/* old locator pair */
3546 		size_pol += sizeof(struct sadb_x_ipsecrequest) +
3547 			    pfkey_sockaddr_pair_size(mp->old_family);
3548 		/* new locator pair */
3549 		size_pol += sizeof(struct sadb_x_ipsecrequest) +
3550 			    pfkey_sockaddr_pair_size(mp->new_family);
3551 	}
3552 
3553 	size += sizeof(struct sadb_msg) + size_pol;
3554 
3555 	/* alloc buffer */
3556 	skb = alloc_skb(size, GFP_ATOMIC);
3557 	if (skb == NULL)
3558 		return -ENOMEM;
3559 
3560 	hdr = skb_put(skb, sizeof(struct sadb_msg));
3561 	hdr->sadb_msg_version = PF_KEY_V2;
3562 	hdr->sadb_msg_type = SADB_X_MIGRATE;
3563 	hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
3564 	hdr->sadb_msg_len = size / 8;
3565 	hdr->sadb_msg_errno = 0;
3566 	hdr->sadb_msg_reserved = 0;
3567 	hdr->sadb_msg_seq = 0;
3568 	hdr->sadb_msg_pid = 0;
3569 
3570 	/* Addresses to be used by KM for negotiation, if ext is available */
3571 	if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3572 		goto err;
3573 
3574 	/* selector src */
3575 	set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
3576 
3577 	/* selector dst */
3578 	set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
3579 
3580 	/* policy information */
3581 	pol = skb_put(skb, sizeof(struct sadb_x_policy));
3582 	pol->sadb_x_policy_len = size_pol / 8;
3583 	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
3584 	pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
3585 	pol->sadb_x_policy_dir = dir + 1;
3586 	pol->sadb_x_policy_reserved = 0;
3587 	pol->sadb_x_policy_id = 0;
3588 	pol->sadb_x_policy_priority = 0;
3589 
3590 	for (i = 0, mp = m; i < num_bundles; i++, mp++) {
3591 		/* old ipsecrequest */
3592 		int mode = pfkey_mode_from_xfrm(mp->mode);
3593 		if (mode < 0)
3594 			goto err;
3595 		if (set_ipsecrequest(skb, mp->proto, mode,
3596 				     (mp->reqid ?  IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3597 				     mp->reqid, mp->old_family,
3598 				     &mp->old_saddr, &mp->old_daddr) < 0)
3599 			goto err;
3600 
3601 		/* new ipsecrequest */
3602 		if (set_ipsecrequest(skb, mp->proto, mode,
3603 				     (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
3604 				     mp->reqid, mp->new_family,
3605 				     &mp->new_saddr, &mp->new_daddr) < 0)
3606 			goto err;
3607 	}
3608 
3609 	/* broadcast migrate message to sockets */
3610 	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
3611 
3612 	return 0;
3613 
3614 err:
3615 	kfree_skb(skb);
3616 	return -EINVAL;
3617 }
3618 #else
3619 static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3620 			      const struct xfrm_migrate *m, int num_bundles,
3621 			      const struct xfrm_kmaddress *k,
3622 			      const struct xfrm_encap_tmpl *encap)
3623 {
3624 	return -ENOPROTOOPT;
3625 }
3626 #endif
3627 
3628 static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
3629 {
3630 	struct sock *sk = sock->sk;
3631 	struct sk_buff *skb = NULL;
3632 	struct sadb_msg *hdr = NULL;
3633 	int err;
3634 	struct net *net = sock_net(sk);
3635 
3636 	err = -EOPNOTSUPP;
3637 	if (msg->msg_flags & MSG_OOB)
3638 		goto out;
3639 
3640 	err = -EMSGSIZE;
3641 	if ((unsigned int)len > sk->sk_sndbuf - 32)
3642 		goto out;
3643 
3644 	err = -ENOBUFS;
3645 	skb = alloc_skb(len, GFP_KERNEL);
3646 	if (skb == NULL)
3647 		goto out;
3648 
3649 	err = -EFAULT;
3650 	if (memcpy_from_msg(skb_put(skb,len), msg, len))
3651 		goto out;
3652 
3653 	hdr = pfkey_get_base_msg(skb, &err);
3654 	if (!hdr)
3655 		goto out;
3656 
3657 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
3658 	err = pfkey_process(sk, skb, hdr);
3659 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
3660 
3661 out:
3662 	if (err && hdr && pfkey_error(hdr, err, sk) == 0)
3663 		err = 0;
3664 	kfree_skb(skb);
3665 
3666 	return err ? : len;
3667 }
3668 
3669 static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3670 			 int flags)
3671 {
3672 	struct sock *sk = sock->sk;
3673 	struct pfkey_sock *pfk = pfkey_sk(sk);
3674 	struct sk_buff *skb;
3675 	int copied, err;
3676 
3677 	err = -EINVAL;
3678 	if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3679 		goto out;
3680 
3681 	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3682 	if (skb == NULL)
3683 		goto out;
3684 
3685 	copied = skb->len;
3686 	if (copied > len) {
3687 		msg->msg_flags |= MSG_TRUNC;
3688 		copied = len;
3689 	}
3690 
3691 	skb_reset_transport_header(skb);
3692 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
3693 	if (err)
3694 		goto out_free;
3695 
3696 	sock_recv_ts_and_drops(msg, sk, skb);
3697 
3698 	err = (flags & MSG_TRUNC) ? skb->len : copied;
3699 
3700 	if (pfk->dump.dump != NULL &&
3701 	    3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
3702 		pfkey_do_dump(pfk);
3703 
3704 out_free:
3705 	skb_free_datagram(sk, skb);
3706 out:
3707 	return err;
3708 }
3709 
3710 static const struct proto_ops pfkey_ops = {
3711 	.family		=	PF_KEY,
3712 	.owner		=	THIS_MODULE,
3713 	/* Operations that make no sense on pfkey sockets. */
3714 	.bind		=	sock_no_bind,
3715 	.connect	=	sock_no_connect,
3716 	.socketpair	=	sock_no_socketpair,
3717 	.accept		=	sock_no_accept,
3718 	.getname	=	sock_no_getname,
3719 	.ioctl		=	sock_no_ioctl,
3720 	.listen		=	sock_no_listen,
3721 	.shutdown	=	sock_no_shutdown,
3722 	.setsockopt	=	sock_no_setsockopt,
3723 	.getsockopt	=	sock_no_getsockopt,
3724 	.mmap		=	sock_no_mmap,
3725 	.sendpage	=	sock_no_sendpage,
3726 
3727 	/* Now the operations that really occur. */
3728 	.release	=	pfkey_release,
3729 	.poll		=	datagram_poll,
3730 	.sendmsg	=	pfkey_sendmsg,
3731 	.recvmsg	=	pfkey_recvmsg,
3732 };
3733 
3734 static const struct net_proto_family pfkey_family_ops = {
3735 	.family	=	PF_KEY,
3736 	.create	=	pfkey_create,
3737 	.owner	=	THIS_MODULE,
3738 };
3739 
3740 #ifdef CONFIG_PROC_FS
3741 static int pfkey_seq_show(struct seq_file *f, void *v)
3742 {
3743 	struct sock *s = sk_entry(v);
3744 
3745 	if (v == SEQ_START_TOKEN)
3746 		seq_printf(f ,"sk       RefCnt Rmem   Wmem   User   Inode\n");
3747 	else
3748 		seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
3749 			       s,
3750 			       refcount_read(&s->sk_refcnt),
3751 			       sk_rmem_alloc_get(s),
3752 			       sk_wmem_alloc_get(s),
3753 			       from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
3754 			       sock_i_ino(s)
3755 			       );
3756 	return 0;
3757 }
3758 
3759 static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3760 	__acquires(rcu)
3761 {
3762 	struct net *net = seq_file_net(f);
3763 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3764 
3765 	rcu_read_lock();
3766 	return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3767 }
3768 
3769 static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
3770 {
3771 	struct net *net = seq_file_net(f);
3772 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3773 
3774 	return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3775 }
3776 
3777 static void pfkey_seq_stop(struct seq_file *f, void *v)
3778 	__releases(rcu)
3779 {
3780 	rcu_read_unlock();
3781 }
3782 
3783 static const struct seq_operations pfkey_seq_ops = {
3784 	.start	= pfkey_seq_start,
3785 	.next	= pfkey_seq_next,
3786 	.stop	= pfkey_seq_stop,
3787 	.show	= pfkey_seq_show,
3788 };
3789 
3790 static int pfkey_seq_open(struct inode *inode, struct file *file)
3791 {
3792 	return seq_open_net(inode, file, &pfkey_seq_ops,
3793 			    sizeof(struct seq_net_private));
3794 }
3795 
3796 static const struct file_operations pfkey_proc_ops = {
3797 	.open	 = pfkey_seq_open,
3798 	.read	 = seq_read,
3799 	.llseek	 = seq_lseek,
3800 	.release = seq_release_net,
3801 };
3802 
3803 static int __net_init pfkey_init_proc(struct net *net)
3804 {
3805 	struct proc_dir_entry *e;
3806 
3807 	e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
3808 	if (e == NULL)
3809 		return -ENOMEM;
3810 
3811 	return 0;
3812 }
3813 
3814 static void __net_exit pfkey_exit_proc(struct net *net)
3815 {
3816 	remove_proc_entry("pfkey", net->proc_net);
3817 }
3818 #else
3819 static inline int pfkey_init_proc(struct net *net)
3820 {
3821 	return 0;
3822 }
3823 
3824 static inline void pfkey_exit_proc(struct net *net)
3825 {
3826 }
3827 #endif
3828 
3829 static struct xfrm_mgr pfkeyv2_mgr =
3830 {
3831 	.notify		= pfkey_send_notify,
3832 	.acquire	= pfkey_send_acquire,
3833 	.compile_policy	= pfkey_compile_policy,
3834 	.new_mapping	= pfkey_send_new_mapping,
3835 	.notify_policy	= pfkey_send_policy_notify,
3836 	.migrate	= pfkey_send_migrate,
3837 	.is_alive	= pfkey_is_alive,
3838 };
3839 
3840 static int __net_init pfkey_net_init(struct net *net)
3841 {
3842 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3843 	int rv;
3844 
3845 	INIT_HLIST_HEAD(&net_pfkey->table);
3846 	atomic_set(&net_pfkey->socks_nr, 0);
3847 
3848 	rv = pfkey_init_proc(net);
3849 
3850 	return rv;
3851 }
3852 
3853 static void __net_exit pfkey_net_exit(struct net *net)
3854 {
3855 	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
3856 
3857 	pfkey_exit_proc(net);
3858 	WARN_ON(!hlist_empty(&net_pfkey->table));
3859 }
3860 
3861 static struct pernet_operations pfkey_net_ops = {
3862 	.init = pfkey_net_init,
3863 	.exit = pfkey_net_exit,
3864 	.id   = &pfkey_net_id,
3865 	.size = sizeof(struct netns_pfkey),
3866 };
3867 
3868 static void __exit ipsec_pfkey_exit(void)
3869 {
3870 	xfrm_unregister_km(&pfkeyv2_mgr);
3871 	sock_unregister(PF_KEY);
3872 	unregister_pernet_subsys(&pfkey_net_ops);
3873 	proto_unregister(&key_proto);
3874 }
3875 
3876 static int __init ipsec_pfkey_init(void)
3877 {
3878 	int err = proto_register(&key_proto, 0);
3879 
3880 	if (err != 0)
3881 		goto out;
3882 
3883 	err = register_pernet_subsys(&pfkey_net_ops);
3884 	if (err != 0)
3885 		goto out_unregister_key_proto;
3886 	err = sock_register(&pfkey_family_ops);
3887 	if (err != 0)
3888 		goto out_unregister_pernet;
3889 	err = xfrm_register_km(&pfkeyv2_mgr);
3890 	if (err != 0)
3891 		goto out_sock_unregister;
3892 out:
3893 	return err;
3894 
3895 out_sock_unregister:
3896 	sock_unregister(PF_KEY);
3897 out_unregister_pernet:
3898 	unregister_pernet_subsys(&pfkey_net_ops);
3899 out_unregister_key_proto:
3900 	proto_unregister(&key_proto);
3901 	goto out;
3902 }
3903 
3904 module_init(ipsec_pfkey_init);
3905 module_exit(ipsec_pfkey_exit);
3906 MODULE_LICENSE("GPL");
3907 MODULE_ALIAS_NETPROTO(PF_KEY);
3908