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