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