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