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