af_key.c 77.6 KB
Newer Older
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1
/*
2
 * net/key/af_key.c	An implementation of PF_KEYv2 sockets.
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
3 4 5 6 7 8 9 10 11
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Maxim Giryaev	<gem@asplinux.ru>
 *		David S. Miller	<davem@redhat.com>
 *		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>
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/socket.h>
#include <linux/pfkeyv2.h>
#include <linux/ipsec.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <net/xfrm.h>

#include <net/sock.h>

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
33 34 35
#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))

36

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
37
/* List of all pfkey sockets. */
38
HLIST_HEAD(pfkey_table);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
39 40 41 42 43 44 45 46 47 48
static DECLARE_WAIT_QUEUE_HEAD(pfkey_table_wait);
static rwlock_t pfkey_table_lock = RW_LOCK_UNLOCKED;
static atomic_t pfkey_table_users = ATOMIC_INIT(0);

static atomic_t pfkey_socks_nr = ATOMIC_INIT(0);

struct pfkey_opt {
	int	registered;
	int	promisc;
};
49
#define pfkey_sk(__sk) ((struct pfkey_opt *)(__sk)->sk_protinfo)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
50 51 52

static void pfkey_sock_destruct(struct sock *sk)
{
53
	skb_queue_purge(&sk->sk_receive_queue);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
54

55
	if (!sock_flag(sk, SOCK_DEAD)) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
56 57 58 59
		printk("Attempt to release alive pfkey socket: %p\n", sk);
		return;
	}

60 61
	BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
	BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	kfree(pfkey_sk(sk));

	atomic_dec(&pfkey_socks_nr);
}

static void pfkey_table_grab(void)
{
	write_lock_bh(&pfkey_table_lock);

	if (atomic_read(&pfkey_table_users)) {
		DECLARE_WAITQUEUE(wait, current);

		add_wait_queue_exclusive(&pfkey_table_wait, &wait);
		for(;;) {
			set_current_state(TASK_UNINTERRUPTIBLE);
			if (atomic_read(&pfkey_table_users) == 0)
				break;
			write_unlock_bh(&pfkey_table_lock);
			schedule();
			write_lock_bh(&pfkey_table_lock);
		}

		__set_current_state(TASK_RUNNING);
		remove_wait_queue(&pfkey_table_wait, &wait);
	}
}

static __inline__ void pfkey_table_ungrab(void)
{
	write_unlock_bh(&pfkey_table_lock);
	wake_up(&pfkey_table_wait);
}

static __inline__ void pfkey_lock_table(void)
{
	/* read_lock() synchronizes us to pfkey_table_grab */

	read_lock(&pfkey_table_lock);
	atomic_inc(&pfkey_table_users);
	read_unlock(&pfkey_table_lock);
}

static __inline__ void pfkey_unlock_table(void)
{
	if (atomic_dec_and_test(&pfkey_table_users))
		wake_up(&pfkey_table_wait);
}


static struct proto_ops pfkey_ops;

static void pfkey_insert(struct sock *sk)
{
	pfkey_table_grab();
117
	sk_add_node(sk, &pfkey_table);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
118 119 120 121 122 123
	pfkey_table_ungrab();
}

static void pfkey_remove(struct sock *sk)
{
	pfkey_table_grab();
124
	sk_del_node_init(sk);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	pfkey_table_ungrab();
}

static int pfkey_create(struct socket *sock, int protocol)
{
	struct sock *sk;
	struct pfkey_opt *pfk;
	int err;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
	if (sock->type != SOCK_RAW)
		return -ESOCKTNOSUPPORT;
	if (protocol != PF_KEY_V2)
		return -EPROTONOSUPPORT;

	err = -ENOMEM;
	sk = sk_alloc(PF_KEY, GFP_KERNEL, 1, NULL);
	if (sk == NULL)
		goto out;
145
	
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
146 147
	sock->ops = &pfkey_ops;
	sock_init_data(sock, sk);
148
	sk_set_owner(sk, THIS_MODULE);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
149 150

	err = -ENOMEM;
151
	pfk = sk->sk_protinfo = kmalloc(sizeof(*pfk), GFP_KERNEL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
152 153 154 155 156 157
	if (!pfk) {
		sk_free(sk);
		goto out;
	}
	memset(pfk, 0, sizeof(*pfk));

158 159
	sk->sk_family = PF_KEY;
	sk->sk_destruct = pfkey_sock_destruct;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

	atomic_inc(&pfkey_socks_nr);

	pfkey_insert(sk);

	return 0;
out:
	return err;
}

static int pfkey_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (!sk)
		return 0;

	pfkey_remove(sk);

	sock_orphan(sk);
	sock->sk = NULL;
181
	skb_queue_purge(&sk->sk_write_queue);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
182 183 184 185 186
	sock_put(sk);

	return 0;
}

187 188
static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
			       int allocation, struct sock *sk)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
189
{
190 191
	int err = -ENOBUFS;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
192 193 194 195 196 197 198 199 200 201
	sock_hold(sk);
	if (*skb2 == NULL) {
		if (atomic_read(&skb->users) != 1) {
			*skb2 = skb_clone(skb, allocation);
		} else {
			*skb2 = skb;
			atomic_inc(&skb->users);
		}
	}
	if (*skb2 != NULL) {
202
		if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
203 204
			skb_orphan(*skb2);
			skb_set_owner_r(*skb2, sk);
205 206
			skb_queue_tail(&sk->sk_receive_queue, *skb2);
			sk->sk_data_ready(sk, (*skb2)->len);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
207
			*skb2 = NULL;
208
			err = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
209 210 211
		}
	}
	sock_put(sk);
212
	return err;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
213 214 215 216 217 218 219
}

/* Send SKB to all pfkey sockets matching selected criteria.  */
#define BROADCAST_ALL		0
#define BROADCAST_ONE		1
#define BROADCAST_REGISTERED	2
#define BROADCAST_PROMISC_ONLY	4
220 221
static int pfkey_broadcast(struct sk_buff *skb, int allocation,
			   int broadcast_flags, struct sock *one_sk)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
222 223
{
	struct sock *sk;
224
	struct hlist_node *node;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
225
	struct sk_buff *skb2 = NULL;
226
	int err = -ESRCH;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
227 228 229 230 231

	/* XXX Do we need something like netlink_overrun?  I think
	 * XXX PF_KEY socket apps will not mind current behavior.
	 */
	if (!skb)
232
		return -ENOMEM;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
233 234

	pfkey_lock_table();
235
	sk_for_each(sk, node, &pfkey_table) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
236
		struct pfkey_opt *pfk = pfkey_sk(sk);
237
		int err2;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

		/* Yes, it means that if you are meant to receive this
		 * pfkey message you receive it twice as promiscuous
		 * socket.
		 */
		if (pfk->promisc)
			pfkey_broadcast_one(skb, &skb2, allocation, sk);

		/* the exact target will be processed later */
		if (sk == one_sk)
			continue;
		if (broadcast_flags != BROADCAST_ALL) {
			if (broadcast_flags & BROADCAST_PROMISC_ONLY)
				continue;
			if ((broadcast_flags & BROADCAST_REGISTERED) &&
			    !pfk->registered)
				continue;
			if (broadcast_flags & BROADCAST_ONE)
				continue;
		}

259 260 261 262 263 264
		err2 = pfkey_broadcast_one(skb, &skb2, allocation, sk);

		/* Error is cleare after succecful sending to at least one
		 * registered KM */
		if ((broadcast_flags & BROADCAST_REGISTERED) && err)
			err = err2;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
265 266 267 268
	}
	pfkey_unlock_table();

	if (one_sk != NULL)
269
		err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
270 271 272 273

	if (skb2)
		kfree_skb(skb2);
	kfree_skb(skb);
274
	return err;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
275 276 277 278 279 280 281
}

static inline void pfkey_hdr_dup(struct sadb_msg *new, struct sadb_msg *orig)
{
	*new = *orig;
}

282
static int pfkey_error(struct sadb_msg *orig, int err, struct sock *sk)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
283 284 285 286 287
{
	struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
	struct sadb_msg *hdr;

	if (!skb)
288
		return -ENOBUFS;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

	/* Woe be to the platform trying to support PFKEY yet
	 * having normal errnos outside the 1-255 range, inclusive.
	 */
	err = -err;
	if (err == ERESTARTSYS ||
	    err == ERESTARTNOHAND ||
	    err == ERESTARTNOINTR)
		err = EINTR;
	if (err >= 512)
		err = EINVAL;
	if (err <= 0 || err >= 256)
		BUG();

	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	pfkey_hdr_dup(hdr, orig);
	hdr->sadb_msg_errno = (uint8_t) err;
	hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
			     sizeof(uint64_t));

309 310 311
	pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk);

	return 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
}

static u8 sadb_ext_min_len[] = {
	[SADB_EXT_RESERVED]		= (u8) 0,
	[SADB_EXT_SA]			= (u8) sizeof(struct sadb_sa),
	[SADB_EXT_LIFETIME_CURRENT]	= (u8) sizeof(struct sadb_lifetime),
	[SADB_EXT_LIFETIME_HARD]	= (u8) sizeof(struct sadb_lifetime),
	[SADB_EXT_LIFETIME_SOFT]	= (u8) sizeof(struct sadb_lifetime),
	[SADB_EXT_ADDRESS_SRC]		= (u8) sizeof(struct sadb_address),
	[SADB_EXT_ADDRESS_DST]		= (u8) sizeof(struct sadb_address),
	[SADB_EXT_ADDRESS_PROXY]	= (u8) sizeof(struct sadb_address),
	[SADB_EXT_KEY_AUTH]		= (u8) sizeof(struct sadb_key),
	[SADB_EXT_KEY_ENCRYPT]		= (u8) sizeof(struct sadb_key),
	[SADB_EXT_IDENTITY_SRC]		= (u8) sizeof(struct sadb_ident),
	[SADB_EXT_IDENTITY_DST]		= (u8) sizeof(struct sadb_ident),
	[SADB_EXT_SENSITIVITY]		= (u8) sizeof(struct sadb_sens),
	[SADB_EXT_PROPOSAL]		= (u8) sizeof(struct sadb_prop),
	[SADB_EXT_SUPPORTED_AUTH]	= (u8) sizeof(struct sadb_supported),
	[SADB_EXT_SUPPORTED_ENCRYPT]	= (u8) sizeof(struct sadb_supported),
	[SADB_EXT_SPIRANGE]		= (u8) sizeof(struct sadb_spirange),
	[SADB_X_EXT_KMPRIVATE]		= (u8) sizeof(struct sadb_x_kmprivate),
	[SADB_X_EXT_POLICY]		= (u8) sizeof(struct sadb_x_policy),
	[SADB_X_EXT_SA2]		= (u8) sizeof(struct sadb_x_sa2),
335 336 337 338
	[SADB_X_EXT_NAT_T_TYPE]		= (u8) sizeof(struct sadb_x_nat_t_type),
	[SADB_X_EXT_NAT_T_SPORT]	= (u8) sizeof(struct sadb_x_nat_t_port),
	[SADB_X_EXT_NAT_T_DPORT]	= (u8) sizeof(struct sadb_x_nat_t_port),
	[SADB_X_EXT_NAT_T_OA]		= (u8) sizeof(struct sadb_address),
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
339 340 341 342 343 344 345 346
};

/* Verify sadb_address_{len,prefixlen} against sa_family.  */
static int verify_address_len(void *p)
{
	struct sadb_address *sp = p;
	struct sockaddr *addr = (struct sockaddr *)(sp + 1);
	struct sockaddr_in *sin;
347
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
348
	struct sockaddr_in6 *sin6;
349
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
350 351 352 353 354 355 356 357 358 359
	int len;

	switch (addr->sa_family) {
	case AF_INET:
		len  = sizeof(*sp) + sizeof(*sin) + (sizeof(uint64_t) - 1);
		len /= sizeof(uint64_t);
		if (sp->sadb_address_len != len ||
		    sp->sadb_address_prefixlen > 32)
			return -EINVAL;
		break;
360
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
361 362 363 364 365 366 367
	case AF_INET6:
		len  = sizeof(*sp) + sizeof(*sin6) + (sizeof(uint64_t) - 1);
		len /= sizeof(uint64_t);
		if (sp->sadb_address_len != len ||
		    sp->sadb_address_prefixlen > 128)
			return -EINVAL;
		break;
368
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	default:
		/* It is user using kernel to keep track of security
		 * associations for another protocol, such as
		 * OSPF/RSVP/RIPV2/MIP.  It is user's job to verify
		 * lengths.
		 *
		 * XXX Actually, association/policy database is not yet
		 * XXX able to cope with arbitrary sockaddr families.
		 * XXX When it can, remove this -EINVAL.  -DaveM
		 */
		return -EINVAL;
		break;
	};

	return 0;
}

static int present_and_same_family(struct sadb_address *src,
				   struct sadb_address *dst)
{
	struct sockaddr *s_addr, *d_addr;

	if (!src || !dst)
		return 0;

	s_addr = (struct sockaddr *)(src + 1);
	d_addr = (struct sockaddr *)(dst + 1);
	if (s_addr->sa_family != d_addr->sa_family)
		return 0;
398 399 400 401 402
	if (s_addr->sa_family != AF_INET
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	    && s_addr->sa_family != AF_INET6
#endif
		)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		return 0;

	return 1;
}

static int parse_exthdrs(struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	char *p = (char *) hdr;
	int len = skb->len;

	len -= sizeof(*hdr);
	p += sizeof(*hdr);
	while (len > 0) {
		struct sadb_ext *ehdr = (struct sadb_ext *) p;
		uint16_t ext_type;
		int ext_len;

		ext_len  = ehdr->sadb_ext_len;
		ext_len *= sizeof(uint64_t);
		ext_type = ehdr->sadb_ext_type;
		if (ext_len < sizeof(uint64_t) ||
		    ext_len > len ||
		    ext_type == SADB_EXT_RESERVED)
			return -EINVAL;

		if (ext_type <= SADB_EXT_MAX) {
			int min = (int) sadb_ext_min_len[ext_type];
			if (ext_len < min)
				return -EINVAL;
			if (ext_hdrs[ext_type-1] != NULL)
				return -EINVAL;
			if (ext_type == SADB_EXT_ADDRESS_SRC ||
			    ext_type == SADB_EXT_ADDRESS_DST ||
436 437
			    ext_type == SADB_EXT_ADDRESS_PROXY ||
			    ext_type == SADB_X_EXT_NAT_T_OA) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
				if (verify_address_len(p))
					return -EINVAL;
			}				
			ext_hdrs[ext_type-1] = p;
		}
		p   += ext_len;
		len -= ext_len;
	}

	return 0;
}

static uint16_t
pfkey_satype2proto(uint8_t satype)
{
	switch (satype) {
	case SADB_SATYPE_UNSPEC:
		return IPSEC_PROTO_ANY;
	case SADB_SATYPE_AH:
		return IPPROTO_AH;
	case SADB_SATYPE_ESP:
		return IPPROTO_ESP;
	case SADB_X_SATYPE_IPCOMP:
		return IPPROTO_COMP;
		break;
	default:
		return 0;
	}
	/* NOTREACHED */
}

static uint8_t
pfkey_proto2satype(uint16_t proto)
{
	switch (proto) {
	case IPPROTO_AH:
		return SADB_SATYPE_AH;
	case IPPROTO_ESP:
		return SADB_SATYPE_ESP;
	case IPPROTO_COMP:
		return SADB_X_SATYPE_IPCOMP;
		break;
	default:
		return 0;
	}
	/* NOTREACHED */
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499
/* BTW, this scheme means that there is no way with PFKEY2 sockets to
 * say specifically 'just raw sockets' as we encode them as 255.
 */

static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
{
	return (proto == IPSEC_PROTO_ANY ? 0 : proto);
}

static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
{
	return (proto ? proto : IPSEC_PROTO_ANY);
}

500 501
static int pfkey_sadb_addr2xfrm_addr(struct sadb_address *addr,
				     xfrm_address_t *xaddr)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
502 503 504
{
	switch (((struct sockaddr*)(addr + 1))->sa_family) {
	case AF_INET:
505
		xaddr->a4 = 
506 507 508
			((struct sockaddr_in *)(addr + 1))->sin_addr.s_addr;
		return AF_INET;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
509 510
	case AF_INET6:
		memcpy(xaddr->a6, 
511 512 513 514
		       &((struct sockaddr_in6 *)(addr + 1))->sin6_addr,
		       sizeof(struct in6_addr));
		return AF_INET6;
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
515
	default:
516
		return 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
517
	}
518
	/* NOTREACHED */
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
519 520 521 522 523 524 525
}

static struct  xfrm_state *pfkey_xfrm_state_lookup(struct sadb_msg *hdr, void **ext_hdrs)
{
	struct sadb_sa *sa;
	struct sadb_address *addr;
	uint16_t proto;
526 527
	unsigned short family;
	xfrm_address_t *xaddr;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541

	sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
	if (sa == NULL)
		return NULL;

	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
	if (proto == 0)
		return NULL;

	/* sadb_address_len should be checked by caller */
	addr = (struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1];
	if (addr == NULL)
		return NULL;

542 543
	family = ((struct sockaddr *)(addr + 1))->sa_family;
	switch (family) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
544
	case AF_INET:
545
		xaddr = (xfrm_address_t *)&((struct sockaddr_in *)(addr + 1))->sin_addr;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
546
		break;
547
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
548
	case AF_INET6:
549
		xaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(addr + 1))->sin6_addr;
550 551
		break;
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
552
	default:
553
		xaddr = NULL;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
554 555
	}

556 557 558 559
	if (!xaddr)
		return NULL;

	return xfrm_state_lookup(xaddr, sa->sadb_sa_spi, proto, family);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
560 561 562
}

#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static int
pfkey_sockaddr_size(sa_family_t family)
{
	switch (family) {
	case AF_INET:
		return PFKEY_ALIGN8(sizeof(struct sockaddr_in));
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	case AF_INET6:
		return PFKEY_ALIGN8(sizeof(struct sockaddr_in6));
#endif
	default:
		return 0;
	}
	/* NOTREACHED */
}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
579 580 581 582 583 584 585 586 587
static struct sk_buff * pfkey_xfrm_state2msg(struct xfrm_state *x, int add_keys, int hsc)
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_sa *sa;
	struct sadb_lifetime *lifetime;
	struct sadb_address *addr;
	struct sadb_key *key;
	struct sadb_x_sa2 *sa2;
588
	struct sockaddr_in *sin;
589 590 591
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct sockaddr_in6 *sin6;
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
592 593 594
	int size;
	int auth_key_size = 0;
	int encrypt_key_size = 0;
595
	int sockaddr_size;
596
	struct xfrm_encap_tmpl *natt = NULL;
597 598 599 600 601

	/* address family check */
	sockaddr_size = pfkey_sockaddr_size(x->props.family);
	if (!sockaddr_size)
		ERR_PTR(-EINVAL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
602 603 604 605 606 607 608 609

	/* base, SA, (lifetime (HSC),) address(SD), (address(P),)
	   key(AE), (identity(SD),) (sensitivity)> */
	size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) + 
		sizeof(struct sadb_lifetime) +
		((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
		((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
			sizeof(struct sadb_address)*2 + 
610
				sockaddr_size*2 +
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
611
					sizeof(struct sadb_x_sa2);
612
	/* identity & sensitivity */
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
613

614
	if ((x->props.family == AF_INET &&
615
	     x->sel.saddr.a4 != x->props.saddr.a4)
616 617 618 619 620 621
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	    || (x->props.family == AF_INET6 &&
		memcmp (x->sel.saddr.a6, x->props.saddr.a6, sizeof (struct in6_addr)))
#endif
		)
		size += sizeof(struct sadb_address) + sockaddr_size;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
622 623 624 625 626 627 628 629 630 631 632 633 634

	if (add_keys) {
		if (x->aalg && x->aalg->alg_key_len) {
			auth_key_size = 
				PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8); 
			size += sizeof(struct sadb_key) + auth_key_size;
		}
		if (x->ealg && x->ealg->alg_key_len) {
			encrypt_key_size = 
				PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8); 
			size += sizeof(struct sadb_key) + encrypt_key_size;
		}
	}
635 636
	if (x->encap)
		natt = x->encap;
637 638 639 640 641 642 643

	if (natt && natt->encap_type) {
		size += sizeof(struct sadb_x_nat_t_type);
		size += sizeof(struct sadb_x_nat_t_port);
		size += sizeof(struct sadb_x_nat_t_port);
	}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return ERR_PTR(-ENOBUFS);

	/* call should fill header later */
	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	memset(hdr, 0, size);	/* XXX do we need this ? */
	hdr->sadb_msg_len = size / sizeof(uint64_t);

	/* sa */
	sa = (struct sadb_sa *)  skb_put(skb, sizeof(struct sadb_sa));
	sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
	sa->sadb_sa_exttype = SADB_EXT_SA;
	sa->sadb_sa_spi = x->id.spi;
	sa->sadb_sa_replay = x->props.replay_window;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
659 660 661 662 663 664 665
	sa->sadb_sa_state = SADB_SASTATE_DYING;
	if (x->km.state == XFRM_STATE_VALID && !x->km.dying)
		sa->sadb_sa_state = SADB_SASTATE_MATURE;
	else if (x->km.state == XFRM_STATE_ACQ)
		sa->sadb_sa_state = SADB_SASTATE_LARVAL;
	else if (x->km.state == XFRM_STATE_EXPIRED)
		sa->sadb_sa_state = SADB_SASTATE_DEAD;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
666 667
	sa->sadb_sa_auth = 0;
	if (x->aalg) {
668
		struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
669 670 671
		sa->sadb_sa_auth = a ? a->desc.sadb_alg_id : 0;
	}
	sa->sadb_sa_encrypt = 0;
672
	BUG_ON(x->ealg && x->calg);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
673
	if (x->ealg) {
674
		struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
675 676
		sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
	}
677 678 679 680 681 682
	/* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
	if (x->calg) {
		struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name);
		sa->sadb_sa_encrypt = a ? a->desc.sadb_alg_id : 0;
	}

683
	sa->sadb_sa_flags = 0;
684 685
	if (x->props.flags & XFRM_STATE_NOECN)
		sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
686 687 688 689 690 691 692 693

	/* hard time */
	if (hsc & 2) {
		lifetime = (struct sadb_lifetime *)  skb_put(skb, 
							     sizeof(struct sadb_lifetime));
		lifetime->sadb_lifetime_len =
			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
694 695
		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.hard_packet_limit);
		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
696 697 698 699 700 701 702 703 704 705
		lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
		lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
	}
	/* soft time */
	if (hsc & 1) {
		lifetime = (struct sadb_lifetime *)  skb_put(skb, 
							     sizeof(struct sadb_lifetime));
		lifetime->sadb_lifetime_len =
			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
706 707
		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.soft_packet_limit);
		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
708 709 710 711
		lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
		lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
	}
	/* current time */
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
712
	lifetime = (struct sadb_lifetime *)  skb_put(skb,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
713 714 715 716 717 718 719 720 721 722
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
	lifetime->sadb_lifetime_allocations = x->curlft.packets;
	lifetime->sadb_lifetime_bytes = x->curlft.bytes;
	lifetime->sadb_lifetime_addtime = x->curlft.add_time;
	lifetime->sadb_lifetime_usetime = x->curlft.use_time;
	/* src address */
	addr = (struct sadb_address*) skb_put(skb, 
723
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
724
	addr->sadb_address_len = 
725
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
726 727
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
728 729 730 731
	/* "if the ports are non-zero, then the sadb_address_proto field, 
	   normally zero, MUST be filled in with the transport 
	   protocol's number." - RFC2367 */
	addr->sadb_address_proto = 0; 
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
732
	addr->sadb_address_reserved = 0;
733 734 735 736 737
	if (x->props.family == AF_INET) {
		addr->sadb_address_prefixlen = 32;

		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
738
		sin->sin_addr.s_addr = x->props.saddr.a4;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		sin->sin_port = 0;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
 		addr->sadb_address_prefixlen = 128;

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
 		memcpy(&sin6->sin6_addr, x->props.saddr.a6,
		       sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
 	}
#endif
	else
		BUG();

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
758 759
	/* dst address */
	addr = (struct sadb_address*) skb_put(skb, 
760
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
761
	addr->sadb_address_len = 
762
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
763 764
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
765
	addr->sadb_address_proto = 0; 
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
766 767
	addr->sadb_address_prefixlen = 32; /* XXX */ 
	addr->sadb_address_reserved = 0;
768 769
	if (x->props.family == AF_INET) {
		sin = (struct sockaddr_in *) (addr + 1);
770
		sin->sin_family = AF_INET;
771
		sin->sin_addr.s_addr = x->id.daddr.a4;
772
		sin->sin_port = 0;
773
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
774

775
		if (x->sel.saddr.a4 != x->props.saddr.a4) {
776 777 778 779 780 781 782 783 784 785 786 787 788
			addr = (struct sadb_address*) skb_put(skb, 
				sizeof(struct sadb_address)+sockaddr_size);
			addr->sadb_address_len = 
				(sizeof(struct sadb_address)+sockaddr_size)/
				sizeof(uint64_t);
			addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
			addr->sadb_address_proto =
				pfkey_proto_from_xfrm(x->sel.proto);
			addr->sadb_address_prefixlen = x->sel.prefixlen_s;
			addr->sadb_address_reserved = 0;

			sin = (struct sockaddr_in *) (addr + 1);
			sin->sin_family = AF_INET;
789
			sin->sin_addr.s_addr = x->sel.saddr.a4;
790 791 792
			sin->sin_port = x->sel.sport;
			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
		}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
793
	}
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
		addr->sadb_address_prefixlen = 128;

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr, x->id.daddr.a6, sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;

		if (memcmp (x->sel.saddr.a6, x->props.saddr.a6,
			    sizeof(struct in6_addr))) {
			addr = (struct sadb_address *) skb_put(skb, 
				sizeof(struct sadb_address)+sockaddr_size);
			addr->sadb_address_len = 
				(sizeof(struct sadb_address)+sockaddr_size)/
				sizeof(uint64_t);
			addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
			addr->sadb_address_proto =
				pfkey_proto_from_xfrm(x->sel.proto);
			addr->sadb_address_prefixlen = x->sel.prefixlen_s;
			addr->sadb_address_reserved = 0;

			sin6 = (struct sockaddr_in6 *) (addr + 1);
			sin6->sin6_family = AF_INET6;
			sin6->sin6_port = x->sel.sport;
			sin6->sin6_flowinfo = 0;
			memcpy(&sin6->sin6_addr, x->sel.saddr.a6,
			       sizeof(struct in6_addr));
			sin6->sin6_scope_id = 0;
		}
	}
#endif
	else
		BUG();
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

	/* auth key */
	if (add_keys && auth_key_size) {
		key = (struct sadb_key *) skb_put(skb, 
						  sizeof(struct sadb_key)+auth_key_size);
		key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
			sizeof(uint64_t);
		key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
		key->sadb_key_bits = x->aalg->alg_key_len;
		key->sadb_key_reserved = 0;
		memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
	}
	/* encrypt key */
	if (add_keys && encrypt_key_size) {
		key = (struct sadb_key *) skb_put(skb, 
						  sizeof(struct sadb_key)+encrypt_key_size);
		key->sadb_key_len = (sizeof(struct sadb_key) + 
				     encrypt_key_size) / sizeof(uint64_t);
		key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
		key->sadb_key_bits = x->ealg->alg_key_len;
		key->sadb_key_reserved = 0;
		memcpy(key + 1, x->ealg->alg_key, 
		       (x->ealg->alg_key_len+7)/8);
	}

	/* sa */
	sa2 = (struct sadb_x_sa2 *)  skb_put(skb, sizeof(struct sadb_x_sa2));
	sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
	sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
	sa2->sadb_x_sa2_mode = x->props.mode + 1;
	sa2->sadb_x_sa2_reserved1 = 0;
	sa2->sadb_x_sa2_reserved2 = 0;
	sa2->sadb_x_sa2_sequence = 0;
	sa2->sadb_x_sa2_reqid = x->props.reqid;

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	if (natt && natt->encap_type) {
		struct sadb_x_nat_t_type *n_type;
		struct sadb_x_nat_t_port *n_port;

		/* type */
		n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
		n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
		n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
		n_type->sadb_x_nat_t_type_type = natt->encap_type;
		n_type->sadb_x_nat_t_type_reserved[0] = 0;
		n_type->sadb_x_nat_t_type_reserved[1] = 0;
		n_type->sadb_x_nat_t_type_reserved[2] = 0;

		/* source port */
		n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
		n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
		n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
		n_port->sadb_x_nat_t_port_port = natt->encap_sport;
		n_port->sadb_x_nat_t_port_reserved = 0;

		/* dest port */
		n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
		n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
		n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
		n_port->sadb_x_nat_t_port_port = natt->encap_dport;
		n_port->sadb_x_nat_t_port_reserved = 0;
	}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
893 894 895 896 897 898 899 900 901 902 903
	return skb;
}

static struct xfrm_state * pfkey_msg2xfrm_state(struct sadb_msg *hdr, 
						void **ext_hdrs)
{
	struct xfrm_state *x; 
	struct sadb_lifetime *lifetime;
	struct sadb_sa *sa;
	struct sadb_key *key;
	uint16_t proto;
904
	int err;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
	

	sa = (struct sadb_sa *) ext_hdrs[SADB_EXT_SA-1];
	if (!sa ||
	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
		return ERR_PTR(-EINVAL);
	if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
	    !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
		return ERR_PTR(-EINVAL);
	if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
	    !ext_hdrs[SADB_EXT_KEY_AUTH-1])
		return ERR_PTR(-EINVAL);
	if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
	    !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
		return ERR_PTR(-EINVAL);

	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
	if (proto == 0)
		return ERR_PTR(-EINVAL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
925

926 927 928
	/* default error is no buffer space */
	err = -ENOBUFS;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
929 930 931 932 933 934 935 936 937 938 939 940
	/* RFC2367:

   Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
   SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
   sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
   Therefore, the sadb_sa_state field of all submitted SAs MUST be
   SADB_SASTATE_MATURE and the kernel MUST return an error if this is
   not true.

           However, KAME setkey always uses SADB_SASTATE_LARVAL.
	   Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
	 */
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
941
	if (sa->sadb_sa_auth > SADB_AALG_MAX ||
942 943
	    (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
	     sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
944 945 946 947
	    sa->sadb_sa_encrypt > SADB_EALG_MAX)
		return ERR_PTR(-EINVAL);
	key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
	if (key != NULL &&
948
	    sa->sadb_sa_auth != SADB_X_AALG_NULL &&
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
949 950 951 952 953
	    ((key->sadb_key_bits+7) / 8 == 0 ||
	     (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
		return ERR_PTR(-EINVAL);
	key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
	if (key != NULL &&
954
	    sa->sadb_sa_encrypt != SADB_EALG_NULL &&
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
955 956 957 958 959 960 961 962 963 964 965
	    ((key->sadb_key_bits+7) / 8 == 0 ||
	     (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
		return ERR_PTR(-EINVAL);

	x = xfrm_state_alloc();
	if (x == NULL)
		return ERR_PTR(-ENOBUFS);

	x->id.proto = proto;
	x->id.spi = sa->sadb_sa_spi;
	x->props.replay_window = sa->sadb_sa_replay;
966 967
	if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
		x->props.flags |= XFRM_STATE_NOECN;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
968 969 970

	lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_HARD-1];
	if (lifetime != NULL) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
971 972
		x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
973 974 975 976 977
		x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
	lifetime = (struct sadb_lifetime*) ext_hdrs[SADB_EXT_LIFETIME_SOFT-1];
	if (lifetime != NULL) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
978 979
		x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
980 981 982 983 984 985
		x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
	key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_AUTH-1];
	if (sa->sadb_sa_auth) {
		int keysize = 0;
986
		struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
987 988
		if (!a) {
			err = -ENOSYS;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
989
			goto out;
990
		}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
991 992 993 994 995
		if (key)
			keysize = (key->sadb_key_bits + 7) / 8;
		x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
		if (!x->aalg)
			goto out;
996
		strcpy(x->aalg->alg_name, a->name);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
997 998 999 1000 1001 1002 1003 1004 1005
		x->aalg->alg_key_len = 0;
		if (key) {
			x->aalg->alg_key_len = key->sadb_key_bits;
			memcpy(x->aalg->alg_key, key+1, keysize);
		}
		x->props.aalgo = sa->sadb_sa_auth;
		/* x->algo.flags = sa->sadb_sa_flags; */
	}
	if (sa->sadb_sa_encrypt) {
1006 1007
		if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
			struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1008 1009
			if (!a) {
				err = -ENOSYS;
1010
				goto out;
1011
			}
1012 1013 1014 1015 1016 1017 1018 1019
			x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
			if (!x->calg)
				goto out;
			strcpy(x->calg->alg_name, a->name);
			x->props.calgo = sa->sadb_sa_encrypt;
		} else {
			int keysize = 0;
			struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
1020 1021
			if (!a) {
				err = -ENOSYS;
1022
				goto out;
1023
			}
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
			key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
			if (key)
				keysize = (key->sadb_key_bits + 7) / 8;
			x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
			if (!x->ealg)
				goto out;
			strcpy(x->ealg->alg_name, a->name);
			x->ealg->alg_key_len = 0;
			if (key) {
				x->ealg->alg_key_len = key->sadb_key_bits;
				memcpy(x->ealg->alg_key, key+1, keysize);
			}
			x->props.ealgo = sa->sadb_sa_encrypt;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1037 1038 1039 1040
		}
	}
	/* x->algo.flags = sa->sadb_sa_flags; */

1041 1042
	x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
						    &x->props.saddr);
1043 1044
	if (!x->props.family) {
		err = -EAFNOSUPPORT;
1045
		goto out;
1046
	}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1047
	pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
				  &x->id.daddr);

	if (ext_hdrs[SADB_X_EXT_SA2-1]) {
		struct sadb_x_sa2 *sa2 = (void*)ext_hdrs[SADB_X_EXT_SA2-1];
		x->props.mode = sa2->sadb_x_sa2_mode;
		if (x->props.mode)
			x->props.mode--;
		x->props.reqid = sa2->sadb_x_sa2_reqid;
	}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1058 1059 1060 1061 1062 1063 1064 1065
	if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
		struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];

		/* Nobody uses this, but we try. */
		pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
		x->sel.prefixlen_s = addr->sadb_address_prefixlen;
	}

1066 1067 1068 1069
	if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
		struct sadb_x_nat_t_type* n_type;
		struct xfrm_encap_tmpl *natt;

1070 1071
		x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
		if (!x->encap)
1072 1073
			goto out;

1074
		natt = x->encap;
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
		n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
		natt->encap_type = n_type->sadb_x_nat_t_type_type;

		if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
			struct sadb_x_nat_t_port* n_port =
				ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
			natt->encap_sport = n_port->sadb_x_nat_t_port_port;
		}
		if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
			struct sadb_x_nat_t_port* n_port =
				ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
			natt->encap_dport = n_port->sadb_x_nat_t_port_port;
		}
	}

1090
	x->type = xfrm_get_type(proto, x->props.family);
1091 1092
	if (x->type == NULL) {
		err = -ENOPROTOOPT;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1093
		goto out;
1094 1095 1096
	}
	if (x->type->init_state(x, NULL)) {
		err = -EINVAL;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1097
		goto out;
1098
	}
1099
	x->km.seq = hdr->sadb_msg_seq;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1100 1101 1102
	x->km.state = XFRM_STATE_VALID;
	return x;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1103
out:
1104
	x->km.state = XFRM_STATE_DEAD;
1105
	xfrm_state_put(x);
1106
	return ERR_PTR(err);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
}

static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	return -EOPNOTSUPP;
}

static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct sk_buff *resp_skb;
	struct sadb_x_sa2 *sa2;
1118
	struct sadb_address *saddr, *daddr;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1119
	struct sadb_msg *out_hdr;
1120
	struct xfrm_state *x = NULL;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1121
	u8 mode;
Herbert Xu's avatar
Herbert Xu committed
1122
	u32 reqid;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1123
	u8 proto;
1124 1125
	unsigned short family;
	xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1126 1127

	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
1128
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1129 1130 1131 1132 1133 1134
		return -EINVAL;

	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
	if (proto == 0)
		return -EINVAL;

1135
	if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1136 1137 1138 1139 1140 1141 1142
		mode = sa2->sadb_x_sa2_mode - 1;
		reqid = sa2->sadb_x_sa2_reqid;
	} else {
		mode = 0;
		reqid = 0;
	}

1143 1144 1145
	saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
	daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];

1146 1147
	family = ((struct sockaddr *)(saddr + 1))->sa_family;
	switch (family) {
1148
	case AF_INET:
1149 1150
		xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
		xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
1151 1152 1153
		break;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	case AF_INET6:
1154 1155
		xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
		xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
1156 1157 1158
		break;
#endif
	}
1159 1160
	if (xdaddr)
		x = xfrm_find_acq(mode, reqid, proto, xdaddr, xsaddr, 1, family);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1161 1162 1163 1164

	if (x == NULL)
		return -ENOENT;

1165
	resp_skb = ERR_PTR(-ENOENT);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1166 1167 1168

	spin_lock_bh(&x->lock);
	if (x->km.state != XFRM_STATE_DEAD) {
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
		struct sadb_spirange *range = ext_hdrs[SADB_EXT_SPIRANGE-1];
		u32 min_spi, max_spi;

		if (range != NULL) {
			min_spi = range->sadb_spirange_min;
			max_spi = range->sadb_spirange_max;
		} else {
			min_spi = htonl(0x100);
			max_spi = htonl(0x0fffffff);
		}
1179
		xfrm_alloc_spi(x, min_spi, max_spi);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		if (x->id.spi)
			resp_skb = pfkey_xfrm_state2msg(x, 0, 3);
	}
	spin_unlock_bh(&x->lock);

	if (IS_ERR(resp_skb)) {
		xfrm_state_put(x);
		return  PTR_ERR(resp_skb);
	}

	out_hdr = (struct sadb_msg *) resp_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = SADB_GETSPI;
	out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_reserved = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;

	xfrm_state_put(x);

	pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk);

	return 0;
}

static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct xfrm_state *x;

	if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
		return -EOPNOTSUPP;

	if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
		return 0;

	x = xfrm_find_acq_byseq(hdr->sadb_msg_seq);
	if (x == NULL)
		return 0;

1220 1221 1222 1223 1224 1225
	spin_lock_bh(&x->lock);
	if (x->km.state == XFRM_STATE_ACQ) {
		x->km.state = XFRM_STATE_ERROR;
		wake_up(&km_waitq);
	}
	spin_unlock_bh(&x->lock);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	xfrm_state_put(x);
	return 0;
}


static int pfkey_add(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	struct xfrm_state *x;
1236
	int err;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1237

1238 1239
	xfrm_probe_algs();
	
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1240 1241 1242 1243
	x = pfkey_msg2xfrm_state(hdr, ext_hdrs);
	if (IS_ERR(x))
		return PTR_ERR(x);

1244 1245 1246 1247 1248
	if (hdr->sadb_msg_type == SADB_ADD)
		err = xfrm_state_add(x);
	else
		err = xfrm_state_update(x);

1249
	if (err < 0) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1250 1251
		x->km.state = XFRM_STATE_DEAD;
		xfrm_state_put(x);
1252
		return err;
1253 1254
	}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1255 1256 1257 1258 1259 1260
	out_skb = pfkey_xfrm_state2msg(x, 0, 3);
	if (IS_ERR(out_skb))
		return  PTR_ERR(out_skb); /* XXX Should we return 0 here ? */

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
1261
	out_hdr->sadb_msg_type = hdr->sadb_msg_type;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_reserved = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;

	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);

	return 0;
}

static int pfkey_delete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct xfrm_state *x;

	if (!ext_hdrs[SADB_EXT_SA-1] ||
	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
		return -EINVAL;

	x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
	if (x == NULL)
		return -ESRCH;

1286 1287 1288 1289 1290
	if (xfrm_state_kern(x)) {
		xfrm_state_put(x);
		return -EPERM;
	}
	
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	xfrm_state_delete(x);
	xfrm_state_put(x);

	pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, 
			BROADCAST_ALL, sk);

	return 0;
}

static int pfkey_get(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
1302
	__u8 proto;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	struct xfrm_state *x;

	if (!ext_hdrs[SADB_EXT_SA-1] ||
	    !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
		return -EINVAL;

	x = pfkey_xfrm_state_lookup(hdr, ext_hdrs);
	if (x == NULL)
		return -ESRCH;

	out_skb = pfkey_xfrm_state2msg(x, 1, 3);
1317
	proto = x->id.proto;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1318 1319 1320 1321 1322 1323 1324
	xfrm_state_put(x);
	if (IS_ERR(out_skb))
		return  PTR_ERR(out_skb);

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = SADB_DUMP;
1325
	out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_reserved = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk);

	return 0;
}

static struct sk_buff *compose_sadb_supported(struct sadb_msg *orig, int allocation)
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	int len, auth_len, enc_len, i;

	auth_len = xfrm_count_auth_supported();
	if (auth_len) {
		auth_len *= sizeof(struct sadb_alg);
		auth_len += sizeof(struct sadb_supported);
	}
	
	enc_len = xfrm_count_enc_supported();
	if (enc_len) {
		enc_len *= sizeof(struct sadb_alg);
		enc_len += sizeof(struct sadb_supported);
	}
	
	len = enc_len + auth_len + sizeof(struct sadb_msg);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1354

James Morris's avatar
James Morris committed
1355
	skb = alloc_skb(len + 16, allocation);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1356 1357 1358 1359 1360 1361 1362 1363
	if (!skb)
		goto out_put_algs;

	hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
	pfkey_hdr_dup(hdr, orig);
	hdr->sadb_msg_errno = 0;
	hdr->sadb_msg_len = len / sizeof(uint64_t);

1364
	if (auth_len) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1365 1366 1367
		struct sadb_supported *sp;
		struct sadb_alg *ap;

1368
		sp = (struct sadb_supported *) skb_put(skb, auth_len);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1369 1370
		ap = (struct sadb_alg *) (sp + 1);

1371
		sp->sadb_supported_len = auth_len / sizeof(uint64_t);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1372 1373
		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;

1374 1375 1376 1377 1378 1379 1380
		for (i = 0; ; i++) {
			struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
			if (!aalg)
				break;
			if (aalg->available)
				*ap++ = aalg->desc;
		}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1381 1382
	}

1383
	if (enc_len) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1384 1385 1386
		struct sadb_supported *sp;
		struct sadb_alg *ap;

1387
		sp = (struct sadb_supported *) skb_put(skb, enc_len);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1388 1389
		ap = (struct sadb_alg *) (sp + 1);

1390
		sp->sadb_supported_len = enc_len / sizeof(uint64_t);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1391 1392
		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;

1393 1394 1395 1396 1397 1398 1399
		for (i = 0; ; i++) {
			struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
			if (!ealg)
				break;
			if (ealg->available)
				*ap++ = ealg->desc;
		}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1400 1401
	}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1402
out_put_algs:
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	return skb;
}

static int pfkey_register(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct pfkey_opt *pfk = pfkey_sk(sk);
	struct sk_buff *supp_skb;

	if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
		return -EINVAL;

	if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
		if (pfk->registered&(1<<hdr->sadb_msg_satype))
			return -EEXIST;
		pfk->registered |= (1<<hdr->sadb_msg_satype);
	}

1420 1421
	xfrm_probe_algs();
	
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
	if (!supp_skb) {
		if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
			pfk->registered &= ~(1<<hdr->sadb_msg_satype);

		return -ENOBUFS;
	}

	pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk);

	return 0;
}

static int pfkey_flush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	unsigned proto;
	struct sk_buff *skb_out;
	struct sadb_msg *hdr_out;

	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
	if (proto == 0)
		return -EINVAL;

	skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
	if (!skb_out)
		return -ENOBUFS;

	xfrm_state_flush(proto);

	hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
	pfkey_hdr_dup(hdr_out, hdr);
	hdr_out->sadb_msg_errno = (uint8_t) 0;
	hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));

	pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);

	return 0;
}

struct pfkey_dump_data
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sock *sk;
};

static int dump_sa(struct xfrm_state *x, int count, void *ptr)
{
	struct pfkey_dump_data *data = ptr;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;

	out_skb = pfkey_xfrm_state2msg(x, 1, 3);
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = SADB_DUMP;
	out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_reserved = 0;
	out_hdr->sadb_msg_seq = count;
	out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
	return 0;
}

static int pfkey_dump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	u8 proto;
	struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };

	proto = pfkey_satype2proto(hdr->sadb_msg_satype);
	if (proto == 0)
		return -EINVAL;

	return xfrm_state_walk(proto, dump_sa, &data);
}

static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct pfkey_opt *pfk = pfkey_sk(sk);
	int satype = hdr->sadb_msg_satype;

	if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
		/* XXX we mangle packet... */
		hdr->sadb_msg_errno = 0;
		if (satype != 0 && satype != 1)
			return -EINVAL;
		pfk->promisc = satype;
	}
	pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL, BROADCAST_ALL, NULL);
	return 0;
}

1518 1519 1520
static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
{
	int i;
Herbert Xu's avatar
Herbert Xu committed
1521
	u32 reqid = *(u32*)ptr;
1522 1523 1524 1525 1526 1527 1528 1529

	for (i=0; i<xp->xfrm_nr; i++) {
		if (xp->xfrm_vec[i].reqid == reqid)
			return -EEXIST;
	}
	return 0;
}

Herbert Xu's avatar
Herbert Xu committed
1530
static u32 gen_reqid(void)
1531
{
Herbert Xu's avatar
Herbert Xu committed
1532 1533
	u32 start;
	static u32 reqid = IPSEC_MANUAL_REQID_MAX;
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

	start = reqid;
	do {
		++reqid;
		if (reqid == 0)
			reqid = IPSEC_MANUAL_REQID_MAX+1;
		if (xfrm_policy_walk(check_reqid, (void*)&reqid) != -EEXIST)
			return reqid;
	} while (reqid != start);
	return 0;
}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1546 1547 1548 1549
static int
parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
{
	struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1550 1551 1552 1553
	struct sockaddr_in *sin;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct sockaddr_in6 *sin6;
#endif
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1554 1555

	if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
1556
		return -ELOOP;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1557 1558 1559 1560

	if (rq->sadb_x_ipsecrequest_mode == 0)
		return -EINVAL;

1561
	t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1562
	t->mode = rq->sadb_x_ipsecrequest_mode-1;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1563 1564
	if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
		t->optional = 1;
1565 1566 1567 1568 1569 1570 1571 1572
	else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
		t->reqid = rq->sadb_x_ipsecrequest_reqid;
		if (t->reqid > IPSEC_MANUAL_REQID_MAX)
			t->reqid = 0;
		if (!t->reqid && !(t->reqid = gen_reqid()))
			return -ENOBUFS;
	}

1573 1574
	/* addresses present only in tunnel mode */
	if (t->mode) {
1575 1576 1577 1578 1579
		switch (xp->family) {
		case AF_INET:
			sin = (void*)(rq+1);
			if (sin->sin_family != AF_INET)
				return -EINVAL;
1580
			t->saddr.a4 = sin->sin_addr.s_addr;
1581 1582 1583
			sin++;
			if (sin->sin_family != AF_INET)
				return -EINVAL;
1584
			t->id.daddr.a4 = sin->sin_addr.s_addr;
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
			break;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
		case AF_INET6:
			sin6 = (void *)(rq+1);
			if (sin6->sin6_family != AF_INET6)
				return -EINVAL;
			memcpy(t->saddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
			sin6++;
			if (sin6->sin6_family != AF_INET6)
				return -EINVAL;
			memcpy(t->id.daddr.a6, &sin6->sin6_addr, sizeof(struct in6_addr));
			break;
#endif
		default:
			return -EINVAL;
		}
1601
	}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	/* No way to set this via kame pfkey */
	t->aalgos = t->ealgos = t->calgos = ~0;
	xp->xfrm_nr++;
	return 0;
}

static int
parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
{
	int err;
	int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
	struct sadb_x_ipsecrequest *rq = (void*)(pol+1);

1615
	while (len >= sizeof(struct sadb_x_ipsecrequest)) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1616 1617 1618 1619 1620 1621 1622 1623
		if ((err = parse_ipsecrequest(xp, rq)) < 0)
			return err;
		len -= rq->sadb_x_ipsecrequest_len;
		rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
	}
	return 0;
}

1624 1625
static int pfkey_xfrm_policy2msg_size(struct xfrm_policy *xp)
{
1626 1627 1628 1629 1630
	int sockaddr_size = pfkey_sockaddr_size(xp->family);
	int socklen = (xp->family == AF_INET ?
		       sizeof(struct sockaddr_in) :
		       sizeof(struct sockaddr_in6));

1631 1632 1633
	return sizeof(struct sadb_msg) +
		(sizeof(struct sadb_lifetime) * 3) +
		(sizeof(struct sadb_address) * 2) + 
1634
		(sockaddr_size * 2) +
1635 1636
		sizeof(struct sadb_x_policy) +
		(xp->xfrm_nr * (sizeof(struct sadb_x_ipsecrequest) +
1637
				(socklen * 2)));
1638 1639 1640
}

static struct sk_buff * pfkey_xfrm_policy2msg_prep(struct xfrm_policy *xp)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1641 1642 1643 1644
{
	struct sk_buff *skb;
	int size;

1645
	size = pfkey_xfrm_policy2msg_size(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1646 1647 1648 1649 1650

	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return ERR_PTR(-ENOBUFS);

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
	return skb;
}

static void pfkey_xfrm_policy2msg(struct sk_buff *skb, struct xfrm_policy *xp, int dir)
{
	struct sadb_msg *hdr;
	struct sadb_address *addr;
	struct sadb_lifetime *lifetime;
	struct sadb_x_policy *pol;
	struct sockaddr_in   *sin;
1661 1662 1663
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct sockaddr_in6  *sin6;
#endif
1664 1665
	int i;
	int size;
1666 1667 1668 1669
	int sockaddr_size = pfkey_sockaddr_size(xp->family);
	int socklen = (xp->family == AF_INET ?
		       sizeof(struct sockaddr_in) :
		       sizeof(struct sockaddr_in6));
1670 1671 1672

	size = pfkey_xfrm_policy2msg_size(xp);

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1673 1674 1675 1676 1677 1678
	/* call should fill header later */
	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	memset(hdr, 0, size);	/* XXX do we need this ? */

	/* src address */
	addr = (struct sadb_address*) skb_put(skb, 
1679
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1680
	addr->sadb_address_len = 
1681
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1682 1683
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
1684
	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1685 1686
	addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
	addr->sadb_address_reserved = 0;
1687
	/* src address */
1688 1689 1690
	if (xp->family == AF_INET) {
		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
1691
		sin->sin_addr.s_addr = xp->selector.saddr.a4;
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
		sin->sin_port = xp->selector.sport;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (xp->family == AF_INET6) {
		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = xp->selector.sport;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr, xp->selector.saddr.a6,
1702
		       sizeof(struct in6_addr));
1703 1704 1705 1706 1707 1708
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1709 1710
	/* dst address */
	addr = (struct sadb_address*) skb_put(skb, 
1711
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1712
	addr->sadb_address_len =
1713
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1714 1715
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
1716
	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1717 1718
	addr->sadb_address_prefixlen = xp->selector.prefixlen_d; 
	addr->sadb_address_reserved = 0;
1719 1720 1721
	if (xp->family == AF_INET) {
		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
1722
		sin->sin_addr.s_addr = xp->selector.daddr.a4;
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
		sin->sin_port = xp->selector.dport;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (xp->family == AF_INET6) {
		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = xp->selector.dport;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr, xp->selector.daddr.a6,
		       sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1739 1740 1741 1742 1743 1744 1745

	/* hard time */
	lifetime = (struct sadb_lifetime *)  skb_put(skb, 
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1746 1747
	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.hard_packet_limit);
	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1748 1749 1750 1751 1752 1753 1754 1755
	lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
	lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
	/* soft time */
	lifetime = (struct sadb_lifetime *)  skb_put(skb, 
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1756 1757
	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.soft_packet_limit);
	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
	lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
	lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
	/* current time */
	lifetime = (struct sadb_lifetime *)  skb_put(skb, 
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
	lifetime->sadb_lifetime_allocations = xp->curlft.packets;
	lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
	lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
	lifetime->sadb_lifetime_usetime = xp->curlft.use_time;

	pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
	pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
	pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
	if (xp->action == XFRM_POLICY_ALLOW) {
		if (xp->xfrm_nr)
			pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
		else
			pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
	}
	pol->sadb_x_policy_dir = dir+1;
	pol->sadb_x_policy_id = xp->index;
1783
	pol->sadb_x_policy_priority = xp->priority;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1784 1785 1786 1787

	for (i=0; i<xp->xfrm_nr; i++) {
		struct sadb_x_ipsecrequest *rq;
		struct xfrm_tmpl *t = xp->xfrm_vec + i;
1788 1789 1790 1791
		int req_size;

		req_size = sizeof(struct sadb_x_ipsecrequest);
		if (t->mode)
1792 1793 1794
			req_size += 2*socklen;
		else
			size -= 2*socklen;
1795 1796
		rq = (void*)skb_put(skb, req_size);
		pol->sadb_x_policy_len += req_size/8;
Herbert Xu's avatar
Herbert Xu committed
1797
		memset(rq, 0, sizeof(*rq));
1798
		rq->sadb_x_ipsecrequest_len = req_size;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1799 1800
		rq->sadb_x_ipsecrequest_proto = t->id.proto;
		rq->sadb_x_ipsecrequest_mode = t->mode+1;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1801
		rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
1802 1803
		if (t->reqid)
			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1804 1805
		if (t->optional)
			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1806
		rq->sadb_x_ipsecrequest_reqid = t->reqid;
1807
		if (t->mode) {
1808 1809 1810 1811
			switch (xp->family) {
			case AF_INET:
				sin = (void*)(rq+1);
				sin->sin_family = AF_INET;
1812
				sin->sin_addr.s_addr = t->saddr.a4;
1813 1814 1815 1816
				sin->sin_port = 0;
				memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
				sin++;
				sin->sin_family = AF_INET;
1817
				sin->sin_addr.s_addr = t->id.daddr.a4;
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
				sin->sin_port = 0;
				memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
				break;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
			case AF_INET6:
				sin6 = (void*)(rq+1);
				sin6->sin6_family = AF_INET6;
				sin6->sin6_port = 0;
				sin6->sin6_flowinfo = 0;
				memcpy(&sin6->sin6_addr, t->saddr.a6,
				       sizeof(struct in6_addr));
				sin6->sin6_scope_id = 0;

				sin6++;
				sin6->sin6_family = AF_INET6;
				sin6->sin6_port = 0;
				sin6->sin6_flowinfo = 0;
				memcpy(&sin6->sin6_addr, t->id.daddr.a6,
				       sizeof(struct in6_addr));
				sin6->sin6_scope_id = 0;
				break;
#endif
			default:
				break;
			}
1843
		}
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1844
	}
1845
	hdr->sadb_msg_len = size / sizeof(uint64_t);
1846
	hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
}

static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	int err;
	struct sadb_lifetime *lifetime;
	struct sadb_address *sa;
	struct sadb_x_policy *pol;
	struct xfrm_policy *xp;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;

	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
	    !ext_hdrs[SADB_X_EXT_POLICY-1])
		return -EINVAL;

	pol = ext_hdrs[SADB_X_EXT_POLICY-1];
	if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
		return -EINVAL;
	if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
		return -EINVAL;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1870
	xp = xfrm_policy_alloc(GFP_KERNEL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1871 1872 1873 1874 1875
	if (xp == NULL)
		return -ENOBUFS;

	xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
		      XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
1876
	xp->priority = pol->sadb_x_policy_priority;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1877 1878

	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
1879 1880 1881 1882 1883
	xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
	if (!xp->family) {
		err = -EINVAL;
		goto out;
	}
1884
	xp->selector.family = xp->family;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1885
	xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
1886
	xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1887
	xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1888 1889 1890 1891 1892 1893
	if (xp->selector.sport)
		xp->selector.sport_mask = ~0;

	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
	pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
	xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
1894 1895 1896 1897 1898 1899

	/* Amusing, we set this twice.  KAME apps appear to set same value
	 * in both addresses.
	 */
	xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);

1900
	xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1901 1902 1903
	if (xp->selector.dport)
		xp->selector.dport_mask = ~0;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1904 1905 1906 1907
	xp->lft.soft_byte_limit = XFRM_INF;
	xp->lft.hard_byte_limit = XFRM_INF;
	xp->lft.soft_packet_limit = XFRM_INF;
	xp->lft.hard_packet_limit = XFRM_INF;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1908
	if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1909 1910
		xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1911 1912 1913 1914
		xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
	if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1915 1916
		xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1917 1918 1919 1920 1921 1922 1923 1924
		xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
	xp->xfrm_nr = 0;
	if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
	    (err = parse_ipsecrequests(xp, pol)) < 0)
		goto out;

1925
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	if (IS_ERR(out_skb)) {
		err =  PTR_ERR(out_skb);
		goto out;
	}

	err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
				 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
	if (err) {
		kfree_skb(out_skb);
		goto out;
	}

1938 1939
	pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
	xfrm_pol_put(xp);

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = hdr->sadb_msg_type;
	out_hdr->sadb_msg_satype = 0;
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
	return 0;

1952
out:
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
	kfree(xp);
	return err;
}

static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	int err;
	struct sadb_address *sa;
	struct sadb_x_policy *pol;
	struct xfrm_policy *xp;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	struct xfrm_selector sel;

	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
				     ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
	    !ext_hdrs[SADB_X_EXT_POLICY-1])
		return -EINVAL;

	pol = ext_hdrs[SADB_X_EXT_POLICY-1];
	if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
		return -EINVAL;

	memset(&sel, 0, sizeof(sel));

	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1], 
	pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
	sel.prefixlen_s = sa->sadb_address_prefixlen;
1981
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1982
	sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1983 1984 1985 1986 1987 1988
	if (sel.sport)
		sel.sport_mask = ~0;

	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1], 
	pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
	sel.prefixlen_d = sa->sadb_address_prefixlen;
1989
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
1990
	sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1991 1992 1993
	if (sel.dport)
		sel.dport_mask = ~0;

1994
	xp = xfrm_policy_bysel(pol->sadb_x_policy_dir-1, &sel, 1);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
1995 1996 1997 1998 1999
	if (xp == NULL)
		return -ENOENT;

	err = 0;

2000
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2001 2002 2003 2004
	if (IS_ERR(out_skb)) {
		err =  PTR_ERR(out_skb);
		goto out;
	}
2005
	pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = SADB_X_SPDDELETE;
	out_hdr->sadb_msg_satype = 0;
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
	err = 0;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2017
out:
Herbert Xu's avatar
Herbert Xu committed
2018
	xfrm_pol_put(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	return err;
}

static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	int err;
	struct sadb_x_policy *pol;
	struct xfrm_policy *xp;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;

2030
	if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2031 2032
		return -EINVAL;

2033
	xp = xfrm_policy_byid(0, pol->sadb_x_policy_id,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2034 2035 2036 2037 2038 2039
			      hdr->sadb_msg_type == SADB_X_SPDDELETE2);
	if (xp == NULL)
		return -ENOENT;

	err = 0;

2040
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2041 2042 2043 2044
	if (IS_ERR(out_skb)) {
		err =  PTR_ERR(out_skb);
		goto out;
	}
2045
	pfkey_xfrm_policy2msg(out_skb, xp, pol->sadb_x_policy_dir-1);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = hdr->sadb_msg_type;
	out_hdr->sadb_msg_satype = 0;
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
	out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, sk);
	err = 0;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2057
out:
Herbert Xu's avatar
Herbert Xu committed
2058
	xfrm_pol_put(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2059 2060 2061 2062 2063 2064 2065 2066 2067
	return err;
}

static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
{
	struct pfkey_dump_data *data = ptr;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;

2068
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2069 2070 2071
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

2072 2073
	pfkey_xfrm_policy2msg(out_skb, xp, dir);

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = data->hdr->sadb_msg_version;
	out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
	out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_seq = count;
	out_hdr->sadb_msg_pid = data->hdr->sadb_msg_pid;
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, data->sk);
	return 0;
}

static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct pfkey_dump_data data = { .skb = skb, .hdr = hdr, .sk = sk };

	return xfrm_policy_walk(dump_sp, &data);
}

static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr, void **ext_hdrs)
{
	struct sk_buff *skb_out;
	struct sadb_msg *hdr_out;

	skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
	if (!skb_out)
		return -ENOBUFS;

	xfrm_policy_flush();

	hdr_out = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
	pfkey_hdr_dup(hdr_out, hdr);
	hdr_out->sadb_msg_errno = (uint8_t) 0;
	hdr_out->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
	pfkey_broadcast(skb_out, GFP_KERNEL, BROADCAST_ALL, NULL);

	return 0;
}

typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
			     struct sadb_msg *hdr, void **ext_hdrs);
static pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
	[SADB_RESERVED]		= pfkey_reserved,
	[SADB_GETSPI]		= pfkey_getspi,
2117
	[SADB_UPDATE]		= pfkey_add,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
	[SADB_ADD]		= pfkey_add,
	[SADB_DELETE]		= pfkey_delete,
	[SADB_GET]		= pfkey_get,
	[SADB_ACQUIRE]		= pfkey_acquire,
	[SADB_REGISTER]		= pfkey_register,
	[SADB_EXPIRE]		= NULL,
	[SADB_FLUSH]		= pfkey_flush,
	[SADB_DUMP]		= pfkey_dump,
	[SADB_X_PROMISC]	= pfkey_promisc,
	[SADB_X_PCHANGE]	= NULL,
	[SADB_X_SPDUPDATE]	= pfkey_spdadd,
	[SADB_X_SPDADD]		= pfkey_spdadd,
	[SADB_X_SPDDELETE]	= pfkey_spddelete,
	[SADB_X_SPDGET]		= pfkey_spdget,
	[SADB_X_SPDACQUIRE]	= NULL,
	[SADB_X_SPDDUMP]	= pfkey_spddump,
	[SADB_X_SPDFLUSH]	= pfkey_spdflush,
	[SADB_X_SPDSETIDX]	= pfkey_spdadd,
	[SADB_X_SPDDELETE2]	= pfkey_spdget,
};

static int pfkey_process(struct sock *sk, struct sk_buff *skb, struct sadb_msg *hdr)
{
	void *ext_hdrs[SADB_EXT_MAX];
	int err;

	pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
			BROADCAST_PROMISC_ONLY, NULL);

	memset(ext_hdrs, 0, sizeof(ext_hdrs));
	err = parse_exthdrs(skb, hdr, ext_hdrs);
	if (!err) {
		err = -EOPNOTSUPP;
		if (pfkey_funcs[hdr->sadb_msg_type])
			err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
	}
	return err;
}

static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
{
	struct sadb_msg *hdr = NULL;

	if (skb->len < sizeof(*hdr)) {
		*errp = -EMSGSIZE;
	} else {
		hdr = (struct sadb_msg *) skb->data;
		if (hdr->sadb_msg_version != PF_KEY_V2 ||
		    hdr->sadb_msg_reserved != 0 ||
		    (hdr->sadb_msg_type <= SADB_RESERVED ||
		     hdr->sadb_msg_type > SADB_MAX)) {
			hdr = NULL;
			*errp = -EINVAL;
		} else if (hdr->sadb_msg_len != (skb->len /
						 sizeof(uint64_t)) ||
			   hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
						sizeof(uint64_t))) {
			hdr = NULL;
			*errp = -EMSGSIZE;
		} else {
			*errp = 0;
		}
	}
	return hdr;
}

2184
static inline int aalg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2185
{
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
	return t->aalgos & (1 << d->desc.sadb_alg_id);
}

static inline int ealg_tmpl_set(struct xfrm_tmpl *t, struct xfrm_algo_desc *d)
{
	return t->ealgos & (1 << d->desc.sadb_alg_id);
}

static int count_ah_combs(struct xfrm_tmpl *t)
{
	int i, sz = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2197

2198 2199 2200 2201 2202
	for (i = 0; ; i++) {
		struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
		if (!aalg)
			break;
		if (aalg_tmpl_set(t, aalg) && aalg->available)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2203 2204 2205 2206 2207
			sz += sizeof(struct sadb_comb);
	}
	return sz + sizeof(struct sadb_prop);
}

2208
static int count_esp_combs(struct xfrm_tmpl *t)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2209
{
2210
	int i, k, sz = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2211

2212 2213 2214 2215 2216 2217
	for (i = 0; ; i++) {
		struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
		if (!ealg)
			break;
			
		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2218
			continue;
2219 2220 2221 2222 2223 2224 2225
			
		for (k = 1; ; k++) {
			struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
			if (!aalg)
				break;
				
			if (aalg_tmpl_set(t, aalg) && aalg->available)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2226 2227 2228 2229 2230 2231
				sz += sizeof(struct sadb_comb);
		}
	}
	return sz + sizeof(struct sadb_prop);
}

2232
static void dump_ah_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2233 2234 2235 2236 2237 2238 2239 2240
{
	struct sadb_prop *p;
	int i;

	p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
	p->sadb_prop_len = sizeof(struct sadb_prop)/8;
	p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
	p->sadb_prop_replay = 32;
2241
	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2242

2243 2244 2245 2246 2247 2248
	for (i = 0; ; i++) {
		struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
		if (!aalg)
			break;

		if (aalg_tmpl_set(t, aalg) && aalg->available) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2249 2250 2251 2252
			struct sadb_comb *c;
			c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
			memset(c, 0, sizeof(*c));
			p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2253 2254 2255
			c->sadb_comb_auth = aalg->desc.sadb_alg_id;
			c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
			c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2256 2257 2258 2259 2260 2261 2262 2263
			c->sadb_comb_hard_addtime = 24*60*60;
			c->sadb_comb_soft_addtime = 20*60*60;
			c->sadb_comb_hard_usetime = 8*60*60;
			c->sadb_comb_soft_usetime = 7*60*60;
		}
	}
}

2264
static void dump_esp_combs(struct sk_buff *skb, struct xfrm_tmpl *t)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2265 2266 2267 2268 2269 2270 2271 2272
{
	struct sadb_prop *p;
	int i, k;

	p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
	p->sadb_prop_len = sizeof(struct sadb_prop)/8;
	p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
	p->sadb_prop_replay = 32;
2273
	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2274

2275 2276 2277 2278 2279 2280
	for (i=0; ; i++) {
		struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
		if (!ealg)
			break;
	
		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2281
			continue;
2282 2283
			
		for (k = 1; ; k++) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2284
			struct sadb_comb *c;
2285 2286 2287 2288
			struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
			if (!aalg)
				break;
			if (!(aalg_tmpl_set(t, aalg) && aalg->available))
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2289 2290 2291 2292
				continue;
			c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
			memset(c, 0, sizeof(*c));
			p->sadb_prop_len += sizeof(struct sadb_comb)/8;
2293 2294 2295 2296 2297 2298
			c->sadb_comb_auth = aalg->desc.sadb_alg_id;
			c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
			c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
			c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
			c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
			c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
			c->sadb_comb_hard_addtime = 24*60*60;
			c->sadb_comb_soft_addtime = 20*60*60;
			c->sadb_comb_hard_usetime = 8*60*60;
			c->sadb_comb_soft_usetime = 7*60*60;
		}
	}
}

static int pfkey_send_notify(struct xfrm_state *x, int hard)
{
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	int hsc = (hard ? 2 : 1);

	out_skb = pfkey_xfrm_state2msg(x, 0, hsc);
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = PF_KEY_V2;
	out_hdr->sadb_msg_type = SADB_EXPIRE;
	out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_reserved = 0;
	out_hdr->sadb_msg_seq = 0;
	out_hdr->sadb_msg_pid = 0;

2326
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2327 2328 2329
	return 0;
}

2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
static u32 get_acqseq(void)
{
	u32 res;
	static u32 acqseq;
	static spinlock_t acqseq_lock = SPIN_LOCK_UNLOCKED;

	spin_lock_bh(&acqseq_lock);
	res = (++acqseq ? : ++acqseq);
	spin_unlock_bh(&acqseq_lock);
	return res;
}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2342 2343 2344 2345 2346 2347
static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp, int dir)
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_address *addr;
	struct sadb_x_policy *pol;
2348
	struct sockaddr_in *sin;
2349 2350 2351 2352
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct sockaddr_in6 *sin6;
#endif
	int sockaddr_size;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2353 2354
	int size;
	
2355 2356 2357 2358
	sockaddr_size = pfkey_sockaddr_size(x->props.family);
	if (!sockaddr_size)
		return -EINVAL;

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2359
	size = sizeof(struct sadb_msg) +
2360 2361 2362
		(sizeof(struct sadb_address) * 2) +
		(sockaddr_size * 2) +
		sizeof(struct sadb_x_policy);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
	
	if (x->id.proto == IPPROTO_AH)
		size += count_ah_combs(t);
	else if (x->id.proto == IPPROTO_ESP)
		size += count_esp_combs(t);

	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return -ENOMEM;
	
	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	hdr->sadb_msg_version = PF_KEY_V2;
	hdr->sadb_msg_type = SADB_ACQUIRE;
	hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
	hdr->sadb_msg_len = size / sizeof(uint64_t);
	hdr->sadb_msg_errno = 0;
	hdr->sadb_msg_reserved = 0;
2380
	hdr->sadb_msg_seq = x->km.seq = get_acqseq();
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2381
	hdr->sadb_msg_pid = 0;
2382

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2383 2384
	/* src address */
	addr = (struct sadb_address*) skb_put(skb, 
2385
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2386
	addr->sadb_address_len = 
2387
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2388 2389
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
2390
	addr->sadb_address_proto = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2391
	addr->sadb_address_reserved = 0;
2392 2393 2394 2395 2396
	if (x->props.family == AF_INET) {
		addr->sadb_address_prefixlen = 32;

		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
2397
		sin->sin_addr.s_addr = x->props.saddr.a4;
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
		sin->sin_port = 0;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
		addr->sadb_address_prefixlen = 128;

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr,
		       x->props.saddr.a6, sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2416 2417 2418
	
	/* dst address */
	addr = (struct sadb_address*) skb_put(skb, 
2419
					      sizeof(struct sadb_address)+sockaddr_size);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2420
	addr->sadb_address_len =
2421
		(sizeof(struct sadb_address)+sockaddr_size)/
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2422 2423
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2424
	addr->sadb_address_proto = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2425
	addr->sadb_address_reserved = 0;
2426 2427 2428 2429 2430
	if (x->props.family == AF_INET) {
		addr->sadb_address_prefixlen = 32; 

		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
2431
		sin->sin_addr.s_addr = x->id.daddr.a4;
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
		sin->sin_port = 0;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
		addr->sadb_address_prefixlen = 128; 

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr,
		       x->id.daddr.a6, sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();
2450

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463
	pol = (struct sadb_x_policy *)  skb_put(skb, sizeof(struct sadb_x_policy));
	pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
	pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
	pol->sadb_x_policy_dir = dir+1;
	pol->sadb_x_policy_id = xp->index;

	/* Set sadb_comb's. */
	if (x->id.proto == IPPROTO_AH)
		dump_ah_combs(skb, t);
	else if (x->id.proto == IPPROTO_ESP)
		dump_esp_combs(skb, t);

2464
	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2465 2466
}

2467 2468
static struct xfrm_policy *pfkey_compile_policy(u16 family, int opt,
                                                u8 *data, int len, int *dir)
2469 2470 2471 2472
{
	struct xfrm_policy *xp;
	struct sadb_x_policy *pol = (struct sadb_x_policy*)data;

2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
	switch (family) {
	case AF_INET:
		if (opt != IP_IPSEC_POLICY) {
			*dir = -EOPNOTSUPP;
			return NULL;
		}
		break;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	case AF_INET6:
		if (opt != IPV6_IPSEC_POLICY) {
			*dir = -EOPNOTSUPP;
			return NULL;
		}
		break;
#endif
	default:
		*dir = -EINVAL;
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
		return NULL;
	}

	*dir = -EINVAL;

	if (len < sizeof(struct sadb_x_policy) ||
	    pol->sadb_x_policy_len*8 > len ||
	    pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
	    (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
		return NULL;

2501
	xp = xfrm_policy_alloc(GFP_ATOMIC);
2502 2503 2504 2505 2506 2507 2508 2509
	if (xp == NULL) {
		*dir = -ENOBUFS;
		return NULL;
	}

	xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
		      XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2510 2511 2512 2513
	xp->lft.soft_byte_limit = XFRM_INF;
	xp->lft.hard_byte_limit = XFRM_INF;
	xp->lft.soft_packet_limit = XFRM_INF;
	xp->lft.hard_packet_limit = XFRM_INF;
2514
	xp->family = family;
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528

	xp->xfrm_nr = 0;
	if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
	    (*dir = parse_ipsecrequests(xp, pol)) < 0)
		goto out;

	*dir = pol->sadb_x_policy_dir-1;
	return xp;

out:
	kfree(xp);
	return NULL;
}

2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, u16 sport)
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_sa *sa;
	struct sadb_address *addr;
	struct sadb_x_nat_t_port *n_port;
	struct sockaddr_in *sin;
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	struct sockaddr_in6 *sin6;
#endif
	int sockaddr_size;
	int size;
	__u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
	struct xfrm_encap_tmpl *natt = NULL;

	sockaddr_size = pfkey_sockaddr_size(x->props.family);
	if (!sockaddr_size)
		return -EINVAL;

	if (!satype)
		return -EINVAL;

2552
	if (!x->encap)
2553 2554
		return -EINVAL;

2555
	natt = x->encap;
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640

	/* Build an SADB_X_NAT_T_NEW_MAPPING message:
	 *
	 * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
	 * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
	 */
	
	size = sizeof(struct sadb_msg) +
		sizeof(struct sadb_sa) +
		(sizeof(struct sadb_address) * 2) +
		(sockaddr_size * 2) +
		(sizeof(struct sadb_x_nat_t_port) * 2);
	
	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return -ENOMEM;
	
	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	hdr->sadb_msg_version = PF_KEY_V2;
	hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
	hdr->sadb_msg_satype = satype;
	hdr->sadb_msg_len = size / sizeof(uint64_t);
	hdr->sadb_msg_errno = 0;
	hdr->sadb_msg_reserved = 0;
	hdr->sadb_msg_seq = x->km.seq = get_acqseq();
	hdr->sadb_msg_pid = 0;

	/* SA */
	sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
	sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
	sa->sadb_sa_exttype = SADB_EXT_SA;
	sa->sadb_sa_spi = x->id.spi;
	sa->sadb_sa_replay = 0;
	sa->sadb_sa_state = 0;
	sa->sadb_sa_auth = 0;
	sa->sadb_sa_encrypt = 0;
	sa->sadb_sa_flags = 0;

	/* ADDRESS_SRC (old addr) */
	addr = (struct sadb_address*)
		skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
	addr->sadb_address_len = 
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
	addr->sadb_address_proto = 0;
	addr->sadb_address_reserved = 0;
	if (x->props.family == AF_INET) {
		addr->sadb_address_prefixlen = 32;

		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = x->props.saddr.a4;
		sin->sin_port = 0;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
		addr->sadb_address_prefixlen = 128;

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr,
		       x->props.saddr.a6, sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();

	/* NAT_T_SPORT (old port) */
	n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
	n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
	n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
	n_port->sadb_x_nat_t_port_port = natt->encap_sport;
	n_port->sadb_x_nat_t_port_reserved = 0;

	/* ADDRESS_DST (new addr) */
	addr = (struct sadb_address*)
		skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
	addr->sadb_address_len = 
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
2641
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
	addr->sadb_address_proto = 0;
	addr->sadb_address_reserved = 0;
	if (x->props.family == AF_INET) {
		addr->sadb_address_prefixlen = 32;

		sin = (struct sockaddr_in *) (addr + 1);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = ipaddr->a4;
		sin->sin_port = 0;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (x->props.family == AF_INET6) {
		addr->sadb_address_prefixlen = 128;

		sin6 = (struct sockaddr_in6 *) (addr + 1);
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = 0;
		sin6->sin6_flowinfo = 0;
		memcpy(&sin6->sin6_addr, &ipaddr->a6, sizeof(struct in6_addr));
		sin6->sin6_scope_id = 0;
	}
#endif
	else
		BUG();

	/* NAT_T_DPORT (new port) */
	n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
	n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
	n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
	n_port->sadb_x_nat_t_port_port = sport;
	n_port->sadb_x_nat_t_port_reserved = 0;

	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL);
}

Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2678
static int pfkey_sendmsg(struct kiocb *kiocb,
2679
			 struct socket *sock, struct msghdr *msg, size_t len)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
{
	struct sock *sk = sock->sk;
	struct sk_buff *skb = NULL;
	struct sadb_msg *hdr = NULL;
	int err;

	err = -EOPNOTSUPP;
	if (msg->msg_flags & MSG_OOB)
		goto out;

	err = -EMSGSIZE;
2691
	if ((unsigned)len > sk->sk_sndbuf - 32)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
		goto out;

	err = -ENOBUFS;
	skb = alloc_skb(len, GFP_KERNEL);
	if (skb == NULL)
		goto out;

	err = -EFAULT;
	if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len))
		goto out;

	hdr = pfkey_get_base_msg(skb, &err);
	if (!hdr)
		goto out;

2707
	down(&xfrm_cfg_sem);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2708
	err = pfkey_process(sk, skb, hdr);
2709
	up(&xfrm_cfg_sem);
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2710 2711

out:
2712 2713
	if (err && hdr && pfkey_error(hdr, err, sk) == 0)
		err = 0;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2714 2715 2716
	if (skb)
		kfree_skb(skb);

2717
	return err ? : len;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2718 2719 2720
}

static int pfkey_recvmsg(struct kiocb *kiocb,
2721
			 struct socket *sock, struct msghdr *msg, size_t len,
2722
			 int flags)
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
{
	struct sock *sk = sock->sk;
	struct sk_buff *skb;
	int copied, err;

	err = -EINVAL;
	if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC))
		goto out;

	msg->msg_namelen = 0;
	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
	if (skb == NULL)
		goto out;

	copied = skb->len;
	if (copied > len) {
		msg->msg_flags |= MSG_TRUNC;
		copied = len;
	}

	skb->h.raw = skb->data;
	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
	if (err)
		goto out_free;

	sock_recv_timestamp(msg, sk, skb);

	err = (flags & MSG_TRUNC) ? skb->len : copied;

out_free:
	skb_free_datagram(sk, skb);
out:
	return err;
}

static struct proto_ops pfkey_ops = {
	.family		=	PF_KEY,
2760
	.owner		=	THIS_MODULE,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
	/* Operations that make no sense on pfkey sockets. */
	.bind		=	sock_no_bind,
	.connect	=	sock_no_connect,
	.socketpair	=	sock_no_socketpair,
	.accept		=	sock_no_accept,
	.getname	=	sock_no_getname,
	.ioctl		=	sock_no_ioctl,
	.listen		=	sock_no_listen,
	.shutdown	=	sock_no_shutdown,
	.setsockopt	=	sock_no_setsockopt,
	.getsockopt	=	sock_no_getsockopt,
	.mmap		=	sock_no_mmap,
	.sendpage	=	sock_no_sendpage,

	/* Now the operations that really occur. */
	.release	=	pfkey_release,
	.poll		=	datagram_poll,
	.sendmsg	=	pfkey_sendmsg,
	.recvmsg	=	pfkey_recvmsg,
};

static struct net_proto_family pfkey_family_ops = {
	.family	=	PF_KEY,
	.create	=	pfkey_create,
2785
	.owner	=	THIS_MODULE,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
};

#ifdef CONFIG_PROC_FS
static int pfkey_read_proc(char *buffer, char **start, off_t offset,
			   int length, int *eof, void *data)
{
	off_t pos = 0;
	off_t begin = 0;
	int len = 0;
	struct sock *s;
2796
	struct hlist_node *node;
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2797 2798 2799 2800 2801

	len += sprintf(buffer,"sk       RefCnt Rmem   Wmem   User   Inode\n");

	read_lock(&pfkey_table_lock);

2802
	sk_for_each(s, node, &pfkey_table) {
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2803 2804
		len += sprintf(buffer+len,"%p %-6d %-6u %-6u %-6u %-6lu",
			       s,
2805 2806 2807
			       atomic_read(&s->sk_refcnt),
			       atomic_read(&s->sk_rmem_alloc),
			       atomic_read(&s->sk_wmem_alloc),
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842
			       sock_i_uid(s),
			       sock_i_ino(s)
			       );

		buffer[len++] = '\n';
		
		pos = begin + len;
		if (pos < offset) {
			len = 0;
			begin = pos;
		}
		if(pos > offset + length)
			goto done;
	}
	*eof = 1;

done:
	read_unlock(&pfkey_table_lock);

	*start = buffer + (offset - begin);
	len -= (offset - begin);

	if (len > length)
		len = length;
	if (len < 0)
		len = 0;

	return len;
}
#endif

static struct xfrm_mgr pfkeyv2_mgr =
{
	.id		= "pfkeyv2",
	.notify		= pfkey_send_notify,
2843 2844
	.acquire	= pfkey_send_acquire,
	.compile_policy	= pfkey_compile_policy,
2845
	.new_mapping	= pfkey_send_new_mapping,
Alexey Kuznetsov's avatar
Alexey Kuznetsov committed
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
};

static void __exit ipsec_pfkey_exit(void)
{
	xfrm_unregister_km(&pfkeyv2_mgr);
	remove_proc_entry("net/pfkey", 0);
	sock_unregister(PF_KEY);
}

static int __init ipsec_pfkey_init(void)
{
	sock_register(&pfkey_family_ops);
#ifdef CONFIG_PROC_FS
	create_proc_read_entry("net/pfkey", 0, 0, pfkey_read_proc, NULL);
#endif
	xfrm_register_km(&pfkeyv2_mgr);
	return 0;
}

module_init(ipsec_pfkey_init);
module_exit(ipsec_pfkey_exit);
MODULE_LICENSE("GPL");
2868
MODULE_ALIAS_NETPROTO(PF_KEY);