af_key.c 102.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * net/key/af_key.c	An implementation of PF_KEYv2 sockets.
 *
 *		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>
 *		Kunihiro Ishiguro <kunihiro@ipinfusion.com>
 *		Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
 *		Derek Atkins <derek@ihtfp.com>
 */

17
#include <linux/capability.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28
#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>
29
#include <linux/slab.h>
30
#include <net/net_namespace.h>
A
Alexey Dobriyan 已提交
31
#include <net/netns/generic.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38
#include <net/xfrm.h>

#include <net/sock.h>

#define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
#define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))

39
static int pfkey_net_id __read_mostly;
A
Alexey Dobriyan 已提交
40 41 42 43 44
struct netns_pfkey {
	/* List of all pfkey sockets. */
	struct hlist_head table;
	atomic_t socks_nr;
};
S
stephen hemminger 已提交
45
static DEFINE_MUTEX(pfkey_mutex);
L
Linus Torvalds 已提交
46

47
#define DUMMY_MARK 0
48
static const struct xfrm_mark dummy_mark = {0, 0};
L
Linus Torvalds 已提交
49 50 51 52 53
struct pfkey_sock {
	/* struct sock must be the first member of struct pfkey_sock */
	struct sock	sk;
	int		registered;
	int		promisc;
54 55 56

	struct {
		uint8_t		msg_version;
57
		uint32_t	msg_portid;
58 59 60 61 62 63
		int		(*dump)(struct pfkey_sock *sk);
		void		(*done)(struct pfkey_sock *sk);
		union {
			struct xfrm_policy_walk	policy;
			struct xfrm_state_walk	state;
		} u;
H
Herbert Xu 已提交
64
		struct sk_buff	*skb;
65
	} dump;
L
Linus Torvalds 已提交
66 67 68 69 70 71 72
};

static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
{
	return (struct pfkey_sock *)sk;
}

73
static int pfkey_can_dump(const struct sock *sk)
74 75 76 77 78 79
{
	if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
		return 1;
	return 0;
}

80
static void pfkey_terminate_dump(struct pfkey_sock *pfk)
81
{
82
	if (pfk->dump.dump) {
H
Herbert Xu 已提交
83 84 85 86
		if (pfk->dump.skb) {
			kfree_skb(pfk->dump.skb);
			pfk->dump.skb = NULL;
		}
87 88 89 90
		pfk->dump.done(pfk);
		pfk->dump.dump = NULL;
		pfk->dump.done = NULL;
	}
91 92
}

L
Linus Torvalds 已提交
93 94
static void pfkey_sock_destruct(struct sock *sk)
{
A
Alexey Dobriyan 已提交
95 96 97
	struct net *net = sock_net(sk);
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);

98
	pfkey_terminate_dump(pfkey_sk(sk));
L
Linus Torvalds 已提交
99 100 101
	skb_queue_purge(&sk->sk_receive_queue);

	if (!sock_flag(sk, SOCK_DEAD)) {
102
		pr_err("Attempt to release alive pfkey socket: %p\n", sk);
L
Linus Torvalds 已提交
103 104 105
		return;
	}

106 107
	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
	WARN_ON(atomic_read(&sk->sk_wmem_alloc));
L
Linus Torvalds 已提交
108

A
Alexey Dobriyan 已提交
109
	atomic_dec(&net_pfkey->socks_nr);
L
Linus Torvalds 已提交
110 111
}

112
static const struct proto_ops pfkey_ops;
L
Linus Torvalds 已提交
113 114 115

static void pfkey_insert(struct sock *sk)
{
A
Alexey Dobriyan 已提交
116 117 118
	struct net *net = sock_net(sk);
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);

S
stephen hemminger 已提交
119 120 121
	mutex_lock(&pfkey_mutex);
	sk_add_node_rcu(sk, &net_pfkey->table);
	mutex_unlock(&pfkey_mutex);
L
Linus Torvalds 已提交
122 123 124 125
}

static void pfkey_remove(struct sock *sk)
{
S
stephen hemminger 已提交
126 127 128
	mutex_lock(&pfkey_mutex);
	sk_del_node_init_rcu(sk);
	mutex_unlock(&pfkey_mutex);
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136
}

static struct proto key_proto = {
	.name	  = "KEY",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct pfkey_sock),
};

137 138
static int pfkey_create(struct net *net, struct socket *sock, int protocol,
			int kern)
L
Linus Torvalds 已提交
139
{
A
Alexey Dobriyan 已提交
140
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
L
Linus Torvalds 已提交
141 142 143
	struct sock *sk;
	int err;

144
	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
		return -EPERM;
	if (sock->type != SOCK_RAW)
		return -ESOCKTNOSUPPORT;
	if (protocol != PF_KEY_V2)
		return -EPROTONOSUPPORT;

	err = -ENOMEM;
152
	sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto);
L
Linus Torvalds 已提交
153 154
	if (sk == NULL)
		goto out;
155

L
Linus Torvalds 已提交
156 157 158 159 160 161
	sock->ops = &pfkey_ops;
	sock_init_data(sock, sk);

	sk->sk_family = PF_KEY;
	sk->sk_destruct = pfkey_sock_destruct;

A
Alexey Dobriyan 已提交
162
	atomic_inc(&net_pfkey->socks_nr);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	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;
	skb_queue_purge(&sk->sk_write_queue);
S
stephen hemminger 已提交
183 184

	synchronize_rcu();
L
Linus Torvalds 已提交
185 186 187 188 189 190
	sock_put(sk);

	return 0;
}

static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
A
Al Viro 已提交
191
			       gfp_t allocation, struct sock *sk)
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
	int err = -ENOBUFS;

	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) {
		if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
			skb_set_owner_r(*skb2, sk);
			skb_queue_tail(&sk->sk_receive_queue, *skb2);
			sk->sk_data_ready(sk, (*skb2)->len);
			*skb2 = NULL;
			err = 0;
		}
	}
	sock_put(sk);
	return err;
}

/* 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
A
Al Viro 已提交
222
static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
A
Alexey Dobriyan 已提交
223 224
			   int broadcast_flags, struct sock *one_sk,
			   struct net *net)
L
Linus Torvalds 已提交
225
{
A
Alexey Dobriyan 已提交
226
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236
	struct sock *sk;
	struct sk_buff *skb2 = NULL;
	int err = -ESRCH;

	/* XXX Do we need something like netlink_overrun?  I think
	 * XXX PF_KEY socket apps will not mind current behavior.
	 */
	if (!skb)
		return -ENOMEM;

S
stephen hemminger 已提交
237
	rcu_read_lock();
238
	sk_for_each_rcu(sk, &net_pfkey->table) {
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
		struct pfkey_sock *pfk = pfkey_sk(sk);
		int err2;

		/* 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;
		}

		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;
	}
S
stephen hemminger 已提交
269
	rcu_read_unlock();
L
Linus Torvalds 已提交
270 271 272 273

	if (one_sk != NULL)
		err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);

274
	kfree_skb(skb2);
L
Linus Torvalds 已提交
275 276 277 278
	kfree_skb(skb);
	return err;
}

279 280
static int pfkey_do_dump(struct pfkey_sock *pfk)
{
H
Herbert Xu 已提交
281
	struct sadb_msg *hdr;
282 283 284 285 286 287
	int rc;

	rc = pfk->dump.dump(pfk);
	if (rc == -ENOBUFS)
		return 0;

H
Herbert Xu 已提交
288 289 290 291 292 293 294 295
	if (pfk->dump.skb) {
		if (!pfkey_can_dump(&pfk->sk))
			return 0;

		hdr = (struct sadb_msg *) pfk->dump.skb->data;
		hdr->sadb_msg_seq = 0;
		hdr->sadb_msg_errno = rc;
		pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
A
Alexey Dobriyan 已提交
296
				&pfk->sk, sock_net(&pfk->sk));
H
Herbert Xu 已提交
297 298 299
		pfk->dump.skb = NULL;
	}

300 301 302 303
	pfkey_terminate_dump(pfk);
	return rc;
}

304 305
static inline void pfkey_hdr_dup(struct sadb_msg *new,
				 const struct sadb_msg *orig)
L
Linus Torvalds 已提交
306 307 308 309
{
	*new = *orig;
}

310
static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
{
	struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
	struct sadb_msg *hdr;

	if (!skb)
		return -ENOBUFS;

	/* 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;
328
	BUG_ON(err <= 0 || err >= 256);
L
Linus Torvalds 已提交
329 330 331 332 333 334 335

	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));

A
Alexey Dobriyan 已提交
336
	pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
L
Linus Torvalds 已提交
337 338 339 340

	return 0;
}

M
Mathias Krause 已提交
341
static const u8 sadb_ext_min_len[] = {
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	[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),
	[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),
366
	[SADB_X_EXT_SEC_CTX]		= (u8) sizeof(struct sadb_x_sec_ctx),
367
	[SADB_X_EXT_KMADDRESS]		= (u8) sizeof(struct sadb_x_kmaddress),
368
	[SADB_X_EXT_FILTER]		= (u8) sizeof(struct sadb_x_filter),
L
Linus Torvalds 已提交
369 370 371
};

/* Verify sadb_address_{len,prefixlen} against sa_family.  */
372
static int verify_address_len(const void *p)
L
Linus Torvalds 已提交
373
{
374 375 376
	const struct sadb_address *sp = p;
	const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
	const struct sockaddr_in *sin;
E
Eric Dumazet 已提交
377
#if IS_ENABLED(CONFIG_IPV6)
378
	const struct sockaddr_in6 *sin6;
L
Linus Torvalds 已提交
379 380 381 382 383
#endif
	int len;

	switch (addr->sa_family) {
	case AF_INET:
I
Ilpo Järvinen 已提交
384
		len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
L
Linus Torvalds 已提交
385 386 387 388
		if (sp->sadb_address_len != len ||
		    sp->sadb_address_prefixlen > 32)
			return -EINVAL;
		break;
E
Eric Dumazet 已提交
389
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
390
	case AF_INET6:
I
Ilpo Järvinen 已提交
391
		len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		if (sp->sadb_address_len != len ||
		    sp->sadb_address_prefixlen > 128)
			return -EINVAL;
		break;
#endif
	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;
409
	}
L
Linus Torvalds 已提交
410 411 412 413

	return 0;
}

414
static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
415
{
I
Ilpo Järvinen 已提交
416 417 418
	return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
			    sec_ctx->sadb_x_ctx_len,
			    sizeof(uint64_t));
419 420
}

421
static inline int verify_sec_ctx_len(const void *p)
422
{
423
	const struct sadb_x_sec_ctx *sec_ctx = p;
424
	int len = sec_ctx->sadb_x_ctx_len;
425

426
	if (len > PAGE_SIZE)
427 428 429 430 431 432 433 434 435 436
		return -EINVAL;

	len = pfkey_sec_ctx_len(sec_ctx);

	if (sec_ctx->sadb_x_sec_len != len)
		return -EINVAL;

	return 0;
}

437
static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
{
	struct xfrm_user_sec_ctx *uctx = NULL;
	int ctx_size = sec_ctx->sadb_x_ctx_len;

	uctx = kmalloc((sizeof(*uctx)+ctx_size), GFP_KERNEL);

	if (!uctx)
		return NULL;

	uctx->len = pfkey_sec_ctx_len(sec_ctx);
	uctx->exttype = sec_ctx->sadb_x_sec_exttype;
	uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
	uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
	uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
	memcpy(uctx + 1, sec_ctx + 1,
	       uctx->ctx_len);

	return uctx;
}

458 459
static int present_and_same_family(const struct sadb_address *src,
				   const struct sadb_address *dst)
L
Linus Torvalds 已提交
460
{
461
	const struct sockaddr *s_addr, *d_addr;
L
Linus Torvalds 已提交
462 463 464 465

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

466 467
	s_addr = (const struct sockaddr *)(src + 1);
	d_addr = (const struct sockaddr *)(dst + 1);
L
Linus Torvalds 已提交
468 469 470
	if (s_addr->sa_family != d_addr->sa_family)
		return 0;
	if (s_addr->sa_family != AF_INET
E
Eric Dumazet 已提交
471
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479
	    && s_addr->sa_family != AF_INET6
#endif
		)
		return 0;

	return 1;
}

480
static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
L
Linus Torvalds 已提交
481
{
482
	const char *p = (char *) hdr;
L
Linus Torvalds 已提交
483 484 485 486 487
	int len = skb->len;

	len -= sizeof(*hdr);
	p += sizeof(*hdr);
	while (len > 0) {
488
		const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
		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 ||
			    ext_type == SADB_EXT_ADDRESS_PROXY ||
			    ext_type == SADB_X_EXT_NAT_T_OA) {
				if (verify_address_len(p))
					return -EINVAL;
512
			}
513 514 515 516
			if (ext_type == SADB_X_EXT_SEC_CTX) {
				if (verify_sec_ctx_len(p))
					return -EINVAL;
			}
517
			ext_hdrs[ext_type-1] = (void *) p;
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
		}
		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 */
}

/* 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)
{
E
Eric Dumazet 已提交
568
	return proto == IPSEC_PROTO_ANY ? 0 : proto;
L
Linus Torvalds 已提交
569 570 571 572
}

static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
{
E
Eric Dumazet 已提交
573
	return proto ? proto : IPSEC_PROTO_ANY;
L
Linus Torvalds 已提交
574 575
}

576 577 578 579 580
static inline int pfkey_sockaddr_len(sa_family_t family)
{
	switch (family) {
	case AF_INET:
		return sizeof(struct sockaddr_in);
E
Eric Dumazet 已提交
581
#if IS_ENABLED(CONFIG_IPV6)
582 583 584 585 586 587 588
	case AF_INET6:
		return sizeof(struct sockaddr_in6);
#endif
	}
	return 0;
}

589 590
static
int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
L
Linus Torvalds 已提交
591
{
592
	switch (sa->sa_family) {
L
Linus Torvalds 已提交
593
	case AF_INET:
594
		xaddr->a4 =
595
			((struct sockaddr_in *)sa)->sin_addr.s_addr;
L
Linus Torvalds 已提交
596
		return AF_INET;
E
Eric Dumazet 已提交
597
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
598
	case AF_INET6:
599
		memcpy(xaddr->a6,
600
		       &((struct sockaddr_in6 *)sa)->sin6_addr,
L
Linus Torvalds 已提交
601 602 603 604
		       sizeof(struct in6_addr));
		return AF_INET6;
#endif
	}
605 606 607 608
	return 0;
}

static
609
int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
610 611 612
{
	return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
				      xaddr);
L
Linus Torvalds 已提交
613 614
}

615
static struct  xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
616
{
617 618
	const struct sadb_sa *sa;
	const struct sadb_address *addr;
L
Linus Torvalds 已提交
619 620 621 622
	uint16_t proto;
	unsigned short family;
	xfrm_address_t *xaddr;

J
Joe Perches 已提交
623
	sa = ext_hdrs[SADB_EXT_SA - 1];
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631
	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 */
J
Joe Perches 已提交
632
	addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
L
Linus Torvalds 已提交
633 634 635
	if (addr == NULL)
		return NULL;

636
	family = ((const struct sockaddr *)(addr + 1))->sa_family;
L
Linus Torvalds 已提交
637 638
	switch (family) {
	case AF_INET:
639
		xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
L
Linus Torvalds 已提交
640
		break;
E
Eric Dumazet 已提交
641
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
642
	case AF_INET6:
643
		xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651 652
		break;
#endif
	default:
		xaddr = NULL;
	}

	if (!xaddr)
		return NULL;

653
	return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
L
Linus Torvalds 已提交
654 655 656
}

#define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
657

L
Linus Torvalds 已提交
658 659 660
static int
pfkey_sockaddr_size(sa_family_t family)
{
661
	return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
L
Linus Torvalds 已提交
662 663
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static inline int pfkey_mode_from_xfrm(int mode)
{
	switch(mode) {
	case XFRM_MODE_TRANSPORT:
		return IPSEC_MODE_TRANSPORT;
	case XFRM_MODE_TUNNEL:
		return IPSEC_MODE_TUNNEL;
	case XFRM_MODE_BEET:
		return IPSEC_MODE_BEET;
	default:
		return -1;
	}
}

static inline int pfkey_mode_to_xfrm(int mode)
{
	switch(mode) {
	case IPSEC_MODE_ANY:	/*XXX*/
	case IPSEC_MODE_TRANSPORT:
		return XFRM_MODE_TRANSPORT;
	case IPSEC_MODE_TUNNEL:
		return XFRM_MODE_TUNNEL;
	case IPSEC_MODE_BEET:
		return XFRM_MODE_BEET;
	default:
		return -1;
	}
}

693
static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
694 695
					struct sockaddr *sa,
					unsigned short family)
696 697 698 699 700 701 702 703 704 705 706
{
	switch (family) {
	case AF_INET:
	    {
		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
		sin->sin_family = AF_INET;
		sin->sin_port = port;
		sin->sin_addr.s_addr = xaddr->a4;
		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
		return 32;
	    }
E
Eric Dumazet 已提交
707
#if IS_ENABLED(CONFIG_IPV6)
708 709 710 711 712 713
	case AF_INET6:
	    {
		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = port;
		sin6->sin6_flowinfo = 0;
A
Alexey Dobriyan 已提交
714
		sin6->sin6_addr = *(struct in6_addr *)xaddr->a6;
715 716 717 718 719 720 721 722
		sin6->sin6_scope_id = 0;
		return 128;
	    }
#endif
	}
	return 0;
}

723
static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
724
					      int add_keys, int hsc)
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732
{
	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;
733 734 735
	struct sadb_x_sec_ctx *sec_ctx;
	struct xfrm_sec_ctx *xfrm_ctx;
	int ctx_size = 0;
L
Linus Torvalds 已提交
736 737 738 739 740
	int size;
	int auth_key_size = 0;
	int encrypt_key_size = 0;
	int sockaddr_size;
	struct xfrm_encap_tmpl *natt = NULL;
741
	int mode;
L
Linus Torvalds 已提交
742 743 744 745 746 747 748 749

	/* address family check */
	sockaddr_size = pfkey_sockaddr_size(x->props.family);
	if (!sockaddr_size)
		return ERR_PTR(-EINVAL);

	/* base, SA, (lifetime (HSC),) address(SD), (address(P),)
	   key(AE), (identity(SD),) (sensitivity)> */
750
	size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
L
Linus Torvalds 已提交
751 752 753
		sizeof(struct sadb_lifetime) +
		((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
		((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
754
			sizeof(struct sadb_address)*2 +
L
Linus Torvalds 已提交
755 756
				sockaddr_size*2 +
					sizeof(struct sadb_x_sa2);
757 758 759 760 761 762

	if ((xfrm_ctx = x->security)) {
		ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
		size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
	}

L
Linus Torvalds 已提交
763
	/* identity & sensitivity */
764
	if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
L
Linus Torvalds 已提交
765 766 767 768
		size += sizeof(struct sadb_address) + sockaddr_size;

	if (add_keys) {
		if (x->aalg && x->aalg->alg_key_len) {
769 770
			auth_key_size =
				PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
L
Linus Torvalds 已提交
771 772 773
			size += sizeof(struct sadb_key) + auth_key_size;
		}
		if (x->ealg && x->ealg->alg_key_len) {
774 775
			encrypt_key_size =
				PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
			size += sizeof(struct sadb_key) + encrypt_key_size;
		}
	}
	if (x->encap)
		natt = x->encap;

	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);
	}

	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;
803 804 805 806 807 808
	switch (x->km.state) {
	case XFRM_STATE_VALID:
		sa->sadb_sa_state = x->km.dying ?
			SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
		break;
	case XFRM_STATE_ACQ:
L
Linus Torvalds 已提交
809
		sa->sadb_sa_state = SADB_SASTATE_LARVAL;
810 811
		break;
	default:
L
Linus Torvalds 已提交
812
		sa->sadb_sa_state = SADB_SASTATE_DEAD;
813 814
		break;
	}
L
Linus Torvalds 已提交
815 816 817
	sa->sadb_sa_auth = 0;
	if (x->aalg) {
		struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
818 819
		sa->sadb_sa_auth = (a && a->pfkey_supported) ?
					a->desc.sadb_alg_id : 0;
L
Linus Torvalds 已提交
820 821 822 823 824
	}
	sa->sadb_sa_encrypt = 0;
	BUG_ON(x->ealg && x->calg);
	if (x->ealg) {
		struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
825 826
		sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
					a->desc.sadb_alg_id : 0;
L
Linus Torvalds 已提交
827 828 829 830
	}
	/* 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, 0);
831 832
		sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
					a->desc.sadb_alg_id : 0;
L
Linus Torvalds 已提交
833 834 835 836 837 838 839
	}

	sa->sadb_sa_flags = 0;
	if (x->props.flags & XFRM_STATE_NOECN)
		sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
	if (x->props.flags & XFRM_STATE_DECAP_DSCP)
		sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
840 841
	if (x->props.flags & XFRM_STATE_NOPMTUDISC)
		sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
L
Linus Torvalds 已提交
842 843 844

	/* hard time */
	if (hsc & 2) {
845
		lifetime = (struct sadb_lifetime *)  skb_put(skb,
L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854 855 856
							     sizeof(struct sadb_lifetime));
		lifetime->sadb_lifetime_len =
			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.hard_packet_limit);
		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
		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) {
857
		lifetime = (struct sadb_lifetime *)  skb_put(skb,
L
Linus Torvalds 已提交
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
							     sizeof(struct sadb_lifetime));
		lifetime->sadb_lifetime_len =
			sizeof(struct sadb_lifetime)/sizeof(uint64_t);
		lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
		lifetime->sadb_lifetime_allocations =  _X2KEY(x->lft.soft_packet_limit);
		lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
		lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
		lifetime->sadb_lifetime_usetime = x->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 = 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 */
878
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
879
					      sizeof(struct sadb_address)+sockaddr_size);
880
	addr->sadb_address_len =
L
Linus Torvalds 已提交
881 882 883
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
884 885
	/* "if the ports are non-zero, then the sadb_address_proto field,
	   normally zero, MUST be filled in with the transport
L
Linus Torvalds 已提交
886
	   protocol's number." - RFC2367 */
887
	addr->sadb_address_proto = 0;
L
Linus Torvalds 已提交
888 889
	addr->sadb_address_reserved = 0;

890 891 892 893 894
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(&x->props.saddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
L
Linus Torvalds 已提交
895 896 897
		BUG();

	/* dst address */
898
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
899
					      sizeof(struct sadb_address)+sockaddr_size);
900
	addr->sadb_address_len =
L
Linus Torvalds 已提交
901 902 903
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
904
	addr->sadb_address_proto = 0;
L
Linus Torvalds 已提交
905 906
	addr->sadb_address_reserved = 0;

907 908 909 910 911 912
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(&x->id.daddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
		BUG();
L
Linus Torvalds 已提交
913

914 915
	if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
			     x->props.family)) {
916 917 918 919 920 921 922 923 924 925
		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;
L
Linus Torvalds 已提交
926

927 928 929
		pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
L
Linus Torvalds 已提交
930 931 932 933
	}

	/* auth key */
	if (add_keys && auth_key_size) {
934
		key = (struct sadb_key *) skb_put(skb,
L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944
						  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) {
945
		key = (struct sadb_key *) skb_put(skb,
L
Linus Torvalds 已提交
946
						  sizeof(struct sadb_key)+encrypt_key_size);
947
		key->sadb_key_len = (sizeof(struct sadb_key) +
L
Linus Torvalds 已提交
948 949 950 951
				     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;
952
		memcpy(key + 1, x->ealg->alg_key,
L
Linus Torvalds 已提交
953 954 955 956 957 958 959
		       (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;
960 961 962 963 964
	if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
		kfree_skb(skb);
		return ERR_PTR(-EINVAL);
	}
	sa2->sadb_x_sa2_mode = mode;
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	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;

	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;
	}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	/* security context */
	if (xfrm_ctx) {
		sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
				sizeof(struct sadb_x_sec_ctx) + ctx_size);
		sec_ctx->sadb_x_sec_len =
		  (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
		       xfrm_ctx->ctx_len);
	}

L
Linus Torvalds 已提交
1012 1013 1014
	return skb;
}

1015

1016
static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
1017 1018 1019 1020 1021 1022 1023 1024
{
	struct sk_buff *skb;

	skb = __pfkey_xfrm_state2msg(x, 1, 3);

	return skb;
}

1025
static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
1026 1027 1028 1029 1030
							  int hsc)
{
	return __pfkey_xfrm_state2msg(x, 0, hsc);
}

A
Alexey Dobriyan 已提交
1031
static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
1032 1033
						const struct sadb_msg *hdr,
						void * const *ext_hdrs)
L
Linus Torvalds 已提交
1034
{
1035
	struct xfrm_state *x;
1036 1037 1038 1039
	const struct sadb_lifetime *lifetime;
	const struct sadb_sa *sa;
	const struct sadb_key *key;
	const struct sadb_x_sec_ctx *sec_ctx;
L
Linus Torvalds 已提交
1040 1041
	uint16_t proto;
	int err;
1042

L
Linus Torvalds 已提交
1043

J
Joe Perches 已提交
1044
	sa = ext_hdrs[SADB_EXT_SA - 1];
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	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);

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

	/* 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.

1075
	   However, KAME setkey always uses SADB_SASTATE_LARVAL.
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082
	   Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
	 */
	if (sa->sadb_sa_auth > SADB_AALG_MAX ||
	    (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
	     sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
	    sa->sadb_sa_encrypt > SADB_EALG_MAX)
		return ERR_PTR(-EINVAL);
J
Joe Perches 已提交
1083
	key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	if (key != NULL &&
	    sa->sadb_sa_auth != SADB_X_AALG_NULL &&
	    ((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 &&
	    sa->sadb_sa_encrypt != SADB_EALG_NULL &&
	    ((key->sadb_key_bits+7) / 8 == 0 ||
	     (key->sadb_key_bits+7) / 8 > key->sadb_key_len * sizeof(uint64_t)))
		return ERR_PTR(-EINVAL);

A
Alexey Dobriyan 已提交
1096
	x = xfrm_state_alloc(net);
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101
	if (x == NULL)
		return ERR_PTR(-ENOBUFS);

	x->id.proto = proto;
	x->id.spi = sa->sadb_sa_spi;
1102 1103
	x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
					(sizeof(x->replay.bitmap) * 8));
L
Linus Torvalds 已提交
1104 1105 1106 1107
	if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
		x->props.flags |= XFRM_STATE_NOECN;
	if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
		x->props.flags |= XFRM_STATE_DECAP_DSCP;
1108 1109
	if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
		x->props.flags |= XFRM_STATE_NOPMTUDISC;
L
Linus Torvalds 已提交
1110

J
Joe Perches 已提交
1111
	lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117
	if (lifetime != NULL) {
		x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
		x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
J
Joe Perches 已提交
1118
	lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
L
Linus Torvalds 已提交
1119 1120 1121 1122 1123 1124
	if (lifetime != NULL) {
		x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
		x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
		x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
	}
1125

J
Joe Perches 已提交
1126
	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	if (sec_ctx != NULL) {
		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);

		if (!uctx)
			goto out;

		err = security_xfrm_state_alloc(x, uctx);
		kfree(uctx);

		if (err)
			goto out;
	}

J
Joe Perches 已提交
1140
	key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
L
Linus Torvalds 已提交
1141 1142 1143
	if (sa->sadb_sa_auth) {
		int keysize = 0;
		struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
1144
		if (!a || !a->pfkey_supported) {
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
			err = -ENOSYS;
			goto out;
		}
		if (key)
			keysize = (key->sadb_key_bits + 7) / 8;
		x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
		if (!x->aalg)
			goto out;
		strcpy(x->aalg->alg_name, a->name);
		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);
		}
1159
		x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165
		x->props.aalgo = sa->sadb_sa_auth;
		/* x->algo.flags = sa->sadb_sa_flags; */
	}
	if (sa->sadb_sa_encrypt) {
		if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
			struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
1166
			if (!a || !a->pfkey_supported) {
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
				err = -ENOSYS;
				goto out;
			}
			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);
1178
			if (!a || !a->pfkey_supported) {
L
Linus Torvalds 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
				err = -ENOSYS;
				goto out;
			}
			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;
		}
	}
	/* x->algo.flags = sa->sadb_sa_flags; */

1199
	x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
L
Linus Torvalds 已提交
1200
						    &x->props.saddr);
1201
	pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
L
Linus Torvalds 已提交
1202 1203 1204
				  &x->id.daddr);

	if (ext_hdrs[SADB_X_EXT_SA2-1]) {
1205
		const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
1206 1207 1208 1209 1210 1211
		int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
		if (mode < 0) {
			err = -EINVAL;
			goto out;
		}
		x->props.mode = mode;
L
Linus Torvalds 已提交
1212 1213 1214 1215
		x->props.reqid = sa2->sadb_x_sa2_reqid;
	}

	if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
1216
		const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
L
Linus Torvalds 已提交
1217 1218 1219 1220 1221 1222

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

1223
	if (!x->sel.family)
1224 1225
		x->sel.family = x->props.family;

L
Linus Torvalds 已提交
1226
	if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
1227
		const struct sadb_x_nat_t_type* n_type;
L
Linus Torvalds 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
		struct xfrm_encap_tmpl *natt;

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

		natt = x->encap;
		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]) {
1239
			const struct sadb_x_nat_t_port *n_port =
L
Linus Torvalds 已提交
1240 1241 1242 1243
				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]) {
1244
			const struct sadb_x_nat_t_port *n_port =
L
Linus Torvalds 已提交
1245 1246 1247
				ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
			natt->encap_dport = n_port->sadb_x_nat_t_port_port;
		}
T
Timo Teras 已提交
1248
		memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
L
Linus Torvalds 已提交
1249 1250
	}

H
Herbert Xu 已提交
1251 1252
	err = xfrm_init_state(x);
	if (err)
L
Linus Torvalds 已提交
1253
		goto out;
H
Herbert Xu 已提交
1254

L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263
	x->km.seq = hdr->sadb_msg_seq;
	return x;

out:
	x->km.state = XFRM_STATE_DEAD;
	xfrm_state_put(x);
	return ERR_PTR(err);
}

1264
static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1265 1266 1267 1268
{
	return -EOPNOTSUPP;
}

1269
static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1270
{
A
Alexey Dobriyan 已提交
1271
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
1272 1273 1274 1275
	struct sk_buff *resp_skb;
	struct sadb_x_sa2 *sa2;
	struct sadb_address *saddr, *daddr;
	struct sadb_msg *out_hdr;
1276
	struct sadb_spirange *range;
L
Linus Torvalds 已提交
1277
	struct xfrm_state *x = NULL;
1278
	int mode;
1279 1280
	int err;
	u32 min_spi, max_spi;
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	u32 reqid;
	u8 proto;
	unsigned short family;
	xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;

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

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

	if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
1295 1296 1297
		mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
		if (mode < 0)
			return -EINVAL;
L
Linus Torvalds 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
		reqid = sa2->sadb_x_sa2_reqid;
	} else {
		mode = 0;
		reqid = 0;
	}

	saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
	daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];

	family = ((struct sockaddr *)(saddr + 1))->sa_family;
	switch (family) {
	case AF_INET:
		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;
		break;
E
Eric Dumazet 已提交
1313
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319 1320 1321
	case AF_INET6:
		xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
		xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
		break;
#endif
	}

	if (hdr->sadb_msg_seq) {
1322
		x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
1323
		if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329
			xfrm_state_put(x);
			x = NULL;
		}
	}

	if (!x)
1330
		x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
L
Linus Torvalds 已提交
1331 1332 1333 1334

	if (x == NULL)
		return -ENOENT;

1335 1336
	min_spi = 0x100;
	max_spi = 0x0fffffff;
L
Linus Torvalds 已提交
1337

1338 1339 1340 1341
	range = ext_hdrs[SADB_EXT_SPIRANGE-1];
	if (range) {
		min_spi = range->sadb_spirange_min;
		max_spi = range->sadb_spirange_max;
L
Linus Torvalds 已提交
1342
	}
1343

1344 1345 1346 1347 1348 1349
	err = verify_spi_info(x->id.proto, min_spi, max_spi);
	if (err) {
		xfrm_state_put(x);
		return err;
	}

1350
	err = xfrm_alloc_spi(x, min_spi, max_spi);
1351
	resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368

	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);

A
Alexey Dobriyan 已提交
1369
	pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
L
Linus Torvalds 已提交
1370 1371 1372 1373

	return 0;
}

1374
static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1375
{
A
Alexey Dobriyan 已提交
1376
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382 1383 1384
	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;

1385
	x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
L
Linus Torvalds 已提交
1386 1387 1388 1389
	if (x == NULL)
		return 0;

	spin_lock_bh(&x->lock);
1390
	if (x->km.state == XFRM_STATE_ACQ)
L
Linus Torvalds 已提交
1391
		x->km.state = XFRM_STATE_ERROR;
1392

L
Linus Torvalds 已提交
1393 1394 1395 1396 1397
	spin_unlock_bh(&x->lock);
	xfrm_state_put(x);
	return 0;
}

1398 1399 1400
static inline int event2poltype(int event)
{
	switch (event) {
1401
	case XFRM_MSG_DELPOLICY:
1402
		return SADB_X_SPDDELETE;
1403
	case XFRM_MSG_NEWPOLICY:
1404
		return SADB_X_SPDADD;
1405
	case XFRM_MSG_UPDPOLICY:
1406
		return SADB_X_SPDUPDATE;
1407
	case XFRM_MSG_POLEXPIRE:
1408 1409
	//	return SADB_X_SPDEXPIRE;
	default:
1410
		pr_err("pfkey: Unknown policy event %d\n", event);
1411 1412 1413 1414 1415 1416 1417 1418 1419
		break;
	}

	return 0;
}

static inline int event2keytype(int event)
{
	switch (event) {
1420
	case XFRM_MSG_DELSA:
1421
		return SADB_DELETE;
1422
	case XFRM_MSG_NEWSA:
1423
		return SADB_ADD;
1424
	case XFRM_MSG_UPDSA:
1425
		return SADB_UPDATE;
1426
	case XFRM_MSG_EXPIRE:
1427 1428
		return SADB_EXPIRE;
	default:
1429
		pr_err("pfkey: Unknown SA event %d\n", event);
1430 1431 1432 1433 1434 1435 1436
		break;
	}

	return 0;
}

/* ADD/UPD/DEL */
1437
static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
1438 1439 1440 1441
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;

1442
	skb = pfkey_xfrm_state2msg(x);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

	if (IS_ERR(skb))
		return PTR_ERR(skb);

	hdr = (struct sadb_msg *) skb->data;
	hdr->sadb_msg_version = PF_KEY_V2;
	hdr->sadb_msg_type = event2keytype(c->event);
	hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
	hdr->sadb_msg_errno = 0;
	hdr->sadb_msg_reserved = 0;
	hdr->sadb_msg_seq = c->seq;
1454
	hdr->sadb_msg_pid = c->portid;
1455

A
Alexey Dobriyan 已提交
1456
	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
1457 1458 1459

	return 0;
}
L
Linus Torvalds 已提交
1460

1461
static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1462
{
A
Alexey Dobriyan 已提交
1463
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
1464 1465
	struct xfrm_state *x;
	int err;
1466
	struct km_event c;
L
Linus Torvalds 已提交
1467

A
Alexey Dobriyan 已提交
1468
	x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
L
Linus Torvalds 已提交
1469 1470 1471
	if (IS_ERR(x))
		return PTR_ERR(x);

1472
	xfrm_state_hold(x);
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477
	if (hdr->sadb_msg_type == SADB_ADD)
		err = xfrm_state_add(x);
	else
		err = xfrm_state_update(x);

J
Joy Latten 已提交
1478
	xfrm_audit_state_add(x, err ? 0 : 1,
1479 1480
			     audit_get_loginuid(current),
			     audit_get_sessionid(current), 0);
J
Joy Latten 已提交
1481

L
Linus Torvalds 已提交
1482 1483
	if (err < 0) {
		x->km.state = XFRM_STATE_DEAD;
1484
		__xfrm_state_put(x);
1485
		goto out;
L
Linus Torvalds 已提交
1486 1487
	}

1488
	if (hdr->sadb_msg_type == SADB_ADD)
1489
		c.event = XFRM_MSG_NEWSA;
1490
	else
1491
		c.event = XFRM_MSG_UPDSA;
1492
	c.seq = hdr->sadb_msg_seq;
1493
	c.portid = hdr->sadb_msg_pid;
1494
	km_state_notify(x, &c);
1495
out:
1496 1497
	xfrm_state_put(x);
	return err;
L
Linus Torvalds 已提交
1498 1499
}

1500
static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1501
{
A
Alexey Dobriyan 已提交
1502
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
1503
	struct xfrm_state *x;
1504 1505
	struct km_event c;
	int err;
L
Linus Torvalds 已提交
1506 1507 1508 1509 1510 1511

	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;

A
Alexey Dobriyan 已提交
1512
	x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
L
Linus Torvalds 已提交
1513 1514 1515
	if (x == NULL)
		return -ESRCH;

C
Catherine Zhang 已提交
1516 1517 1518
	if ((err = security_xfrm_state_delete(x)))
		goto out;

L
Linus Torvalds 已提交
1519
	if (xfrm_state_kern(x)) {
C
Catherine Zhang 已提交
1520 1521
		err = -EPERM;
		goto out;
L
Linus Torvalds 已提交
1522
	}
J
Joy Latten 已提交
1523

1524
	err = xfrm_state_delete(x);
J
Joy Latten 已提交
1525

C
Catherine Zhang 已提交
1526 1527
	if (err < 0)
		goto out;
L
Linus Torvalds 已提交
1528

1529
	c.seq = hdr->sadb_msg_seq;
1530
	c.portid = hdr->sadb_msg_pid;
1531
	c.event = XFRM_MSG_DELSA;
1532
	km_state_notify(x, &c);
C
Catherine Zhang 已提交
1533
out:
J
Joy Latten 已提交
1534
	xfrm_audit_state_delete(x, err ? 0 : 1,
1535 1536
				audit_get_loginuid(current),
				audit_get_sessionid(current), 0);
1537
	xfrm_state_put(x);
L
Linus Torvalds 已提交
1538

1539
	return err;
L
Linus Torvalds 已提交
1540 1541
}

1542
static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1543
{
A
Alexey Dobriyan 已提交
1544
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	__u8 proto;
	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;

A
Alexey Dobriyan 已提交
1555
	x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
L
Linus Torvalds 已提交
1556 1557 1558
	if (x == NULL)
		return -ESRCH;

1559
	out_skb = pfkey_xfrm_state2msg(x);
L
Linus Torvalds 已提交
1560 1561 1562 1563 1564 1565 1566
	proto = x->id.proto;
	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;
1567
	out_hdr->sadb_msg_type = SADB_GET;
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572
	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;
A
Alexey Dobriyan 已提交
1573
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
L
Linus Torvalds 已提交
1574 1575 1576 1577

	return 0;
}

1578
static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
A
Al Viro 已提交
1579
					      gfp_t allocation)
L
Linus Torvalds 已提交
1580 1581 1582 1583 1584
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	int len, auth_len, enc_len, i;

1585
	auth_len = xfrm_count_pfkey_auth_supported();
L
Linus Torvalds 已提交
1586 1587 1588 1589
	if (auth_len) {
		auth_len *= sizeof(struct sadb_alg);
		auth_len += sizeof(struct sadb_supported);
	}
1590

1591
	enc_len = xfrm_count_pfkey_enc_supported();
L
Linus Torvalds 已提交
1592 1593 1594 1595
	if (enc_len) {
		enc_len *= sizeof(struct sadb_alg);
		enc_len += sizeof(struct sadb_supported);
	}
1596

L
Linus Torvalds 已提交
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	len = enc_len + auth_len + sizeof(struct sadb_msg);

	skb = alloc_skb(len + 16, allocation);
	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);

	if (auth_len) {
		struct sadb_supported *sp;
		struct sadb_alg *ap;

		sp = (struct sadb_supported *) skb_put(skb, auth_len);
		ap = (struct sadb_alg *) (sp + 1);

		sp->sadb_supported_len = auth_len / sizeof(uint64_t);
		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;

		for (i = 0; ; i++) {
			struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
			if (!aalg)
				break;
1622 1623
			if (!aalg->pfkey_supported)
				continue;
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
			if (aalg->available)
				*ap++ = aalg->desc;
		}
	}

	if (enc_len) {
		struct sadb_supported *sp;
		struct sadb_alg *ap;

		sp = (struct sadb_supported *) skb_put(skb, enc_len);
		ap = (struct sadb_alg *) (sp + 1);

		sp->sadb_supported_len = enc_len / sizeof(uint64_t);
		sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;

		for (i = 0; ; i++) {
			struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
			if (!ealg)
				break;
1643 1644
			if (!ealg->pfkey_supported)
				continue;
L
Linus Torvalds 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653
			if (ealg->available)
				*ap++ = ealg->desc;
		}
	}

out_put_algs:
	return skb;
}

1654
static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
{
	struct pfkey_sock *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);
	}

	xfrm_probe_algs();
1669

L
Linus Torvalds 已提交
1670 1671 1672 1673 1674 1675 1676 1677
	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;
	}

A
Alexey Dobriyan 已提交
1678
	pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk, sock_net(sk));
L
Linus Torvalds 已提交
1679 1680 1681 1682

	return 0;
}

1683
static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;

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

	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
	memcpy(hdr, ihdr, sizeof(struct sadb_msg));
	hdr->sadb_msg_errno = (uint8_t) 0;
	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));

	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
}

1700
static int key_notify_sa_flush(const struct km_event *c)
1701 1702 1703 1704 1705 1706 1707 1708
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;

	skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
	if (!skb)
		return -ENOBUFS;
	hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
1709
	hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
J
Jerome Borsboom 已提交
1710
	hdr->sadb_msg_type = SADB_FLUSH;
1711
	hdr->sadb_msg_seq = c->seq;
1712
	hdr->sadb_msg_pid = c->portid;
1713 1714 1715
	hdr->sadb_msg_version = PF_KEY_V2;
	hdr->sadb_msg_errno = (uint8_t) 0;
	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
1716
	hdr->sadb_msg_reserved = 0;
1717

A
Alexey Dobriyan 已提交
1718
	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
1719 1720 1721 1722

	return 0;
}

1723
static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1724
{
A
Alexey Dobriyan 已提交
1725
	struct net *net = sock_net(sk);
1726
	unsigned int proto;
1727
	struct km_event c;
J
Joy Latten 已提交
1728
	struct xfrm_audit audit_info;
1729
	int err, err2;
L
Linus Torvalds 已提交
1730 1731 1732 1733 1734

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

1735
	audit_info.loginuid = audit_get_loginuid(current);
1736
	audit_info.sessionid = audit_get_sessionid(current);
J
Joy Latten 已提交
1737
	audit_info.secid = 0;
A
Alexey Dobriyan 已提交
1738
	err = xfrm_state_flush(net, proto, &audit_info);
1739
	err2 = unicast_flush_resp(sk, hdr);
1740 1741 1742
	if (err || err2) {
		if (err == -ESRCH) /* empty table - go quietly */
			err = 0;
1743
		return err ? err : err2;
1744
	}
1745

1746
	c.data.proto = proto;
1747
	c.seq = hdr->sadb_msg_seq;
1748
	c.portid = hdr->sadb_msg_pid;
1749
	c.event = XFRM_MSG_FLUSHSA;
A
Alexey Dobriyan 已提交
1750
	c.net = net;
1751
	km_state_notify(NULL, &c);
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756 1757

	return 0;
}

static int dump_sa(struct xfrm_state *x, int count, void *ptr)
{
1758
	struct pfkey_sock *pfk = ptr;
L
Linus Torvalds 已提交
1759 1760 1761
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;

1762 1763 1764
	if (!pfkey_can_dump(&pfk->sk))
		return -ENOBUFS;

1765
	out_skb = pfkey_xfrm_state2msg(x);
L
Linus Torvalds 已提交
1766 1767 1768 1769
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

	out_hdr = (struct sadb_msg *) out_skb->data;
1770
	out_hdr->sadb_msg_version = pfk->dump.msg_version;
L
Linus Torvalds 已提交
1771 1772 1773 1774
	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;
H
Herbert Xu 已提交
1775
	out_hdr->sadb_msg_seq = count + 1;
1776
	out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
H
Herbert Xu 已提交
1777 1778 1779

	if (pfk->dump.skb)
		pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
A
Alexey Dobriyan 已提交
1780
				&pfk->sk, sock_net(&pfk->sk));
H
Herbert Xu 已提交
1781 1782
	pfk->dump.skb = out_skb;

L
Linus Torvalds 已提交
1783 1784 1785
	return 0;
}

1786 1787
static int pfkey_dump_sa(struct pfkey_sock *pfk)
{
A
Alexey Dobriyan 已提交
1788 1789
	struct net *net = sock_net(&pfk->sk);
	return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
1790 1791 1792 1793
}

static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
{
F
Fan Du 已提交
1794 1795 1796
	struct net *net = sock_net(&pfk->sk);

	xfrm_state_walk_done(&pfk->dump.u.state, net);
1797 1798
}

1799
static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1800 1801
{
	u8 proto;
1802
	struct xfrm_filter *filter = NULL;
1803 1804 1805 1806
	struct pfkey_sock *pfk = pfkey_sk(sk);

	if (pfk->dump.dump != NULL)
		return -EBUSY;
L
Linus Torvalds 已提交
1807 1808 1809 1810 1811

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

1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
	if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
		struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];

		filter = kmalloc(sizeof(*filter), GFP_KERNEL);
		if (filter == NULL)
			return -ENOMEM;

		memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
		       sizeof(xfrm_address_t));
		memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
		       sizeof(xfrm_address_t));
		filter->family = xfilter->sadb_x_filter_family;
		filter->splen = xfilter->sadb_x_filter_splen;
		filter->dplen = xfilter->sadb_x_filter_dplen;
	}

1828
	pfk->dump.msg_version = hdr->sadb_msg_version;
1829
	pfk->dump.msg_portid = hdr->sadb_msg_pid;
1830 1831
	pfk->dump.dump = pfkey_dump_sa;
	pfk->dump.done = pfkey_dump_sa_done;
1832
	xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
1833

1834
	return pfkey_do_dump(pfk);
L
Linus Torvalds 已提交
1835 1836
}

1837
static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
1838 1839 1840
{
	struct pfkey_sock *pfk = pfkey_sk(sk);
	int satype = hdr->sadb_msg_satype;
1841
	bool reset_errno = false;
L
Linus Torvalds 已提交
1842 1843

	if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
1844
		reset_errno = true;
L
Linus Torvalds 已提交
1845 1846 1847 1848
		if (satype != 0 && satype != 1)
			return -EINVAL;
		pfk->promisc = satype;
	}
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	if (reset_errno && skb_cloned(skb))
		skb = skb_copy(skb, GFP_KERNEL);
	else
		skb = skb_clone(skb, GFP_KERNEL);

	if (reset_errno && skb) {
		struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
		new_hdr->sadb_msg_errno = 0;
	}

	pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
L
Linus Torvalds 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	return 0;
}

static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
{
	int i;
	u32 reqid = *(u32*)ptr;

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

A
Alexey Dobriyan 已提交
1875
static u32 gen_reqid(struct net *net)
L
Linus Torvalds 已提交
1876
{
1877
	struct xfrm_policy_walk walk;
L
Linus Torvalds 已提交
1878
	u32 start;
1879
	int rc;
L
Linus Torvalds 已提交
1880 1881 1882 1883 1884 1885 1886
	static u32 reqid = IPSEC_MANUAL_REQID_MAX;

	start = reqid;
	do {
		++reqid;
		if (reqid == 0)
			reqid = IPSEC_MANUAL_REQID_MAX+1;
1887
		xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
A
Alexey Dobriyan 已提交
1888
		rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
F
Fan Du 已提交
1889
		xfrm_policy_walk_done(&walk, net);
1890
		if (rc != -EEXIST)
L
Linus Torvalds 已提交
1891 1892 1893 1894 1895 1896 1897 1898
			return reqid;
	} while (reqid != start);
	return 0;
}

static int
parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
{
A
Alexey Dobriyan 已提交
1899
	struct net *net = xp_net(xp);
L
Linus Torvalds 已提交
1900
	struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
1901
	int mode;
L
Linus Torvalds 已提交
1902 1903 1904 1905 1906 1907 1908 1909

	if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
		return -ELOOP;

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

	t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
1910 1911 1912
	if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
		return -EINVAL;
	t->mode = mode;
L
Linus Torvalds 已提交
1913 1914 1915 1916 1917 1918
	if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
		t->optional = 1;
	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;
A
Alexey Dobriyan 已提交
1919
		if (!t->reqid && !(t->reqid = gen_reqid(net)))
L
Linus Torvalds 已提交
1920 1921 1922 1923
			return -ENOBUFS;
	}

	/* addresses present only in tunnel mode */
1924
	if (t->mode == XFRM_MODE_TUNNEL) {
1925 1926 1927 1928 1929 1930
		u8 *sa = (u8 *) (rq + 1);
		int family, socklen;

		family = pfkey_sockaddr_extract((struct sockaddr *)sa,
						&t->saddr);
		if (!family)
L
Linus Torvalds 已提交
1931
			return -EINVAL;
1932 1933 1934 1935 1936 1937

		socklen = pfkey_sockaddr_len(family);
		if (pfkey_sockaddr_extract((struct sockaddr *)(sa + socklen),
					   &t->id.daddr) != family)
			return -EINVAL;
		t->encap_family = family;
1938 1939 1940
	} else
		t->encap_family = xp->family;

L
Linus Torvalds 已提交
1941
	/* No way to set this via kame pfkey */
1942
	t->allalgs = 1;
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
	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);

1954 1955 1956
	if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
		return -EINVAL;

L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965
	while (len >= sizeof(struct sadb_x_ipsecrequest)) {
		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;
}

1966
static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
{
  struct xfrm_sec_ctx *xfrm_ctx = xp->security;

	if (xfrm_ctx) {
		int len = sizeof(struct sadb_x_sec_ctx);
		len += xfrm_ctx->ctx_len;
		return PFKEY_ALIGN8(len);
	}
	return 0;
}

1978
static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
L
Linus Torvalds 已提交
1979
{
1980
	const struct xfrm_tmpl *t;
L
Linus Torvalds 已提交
1981
	int sockaddr_size = pfkey_sockaddr_size(xp->family);
1982 1983 1984 1985 1986
	int socklen = 0;
	int i;

	for (i=0; i<xp->xfrm_nr; i++) {
		t = xp->xfrm_vec + i;
1987
		socklen += pfkey_sockaddr_len(t->encap_family);
1988
	}
L
Linus Torvalds 已提交
1989 1990 1991

	return sizeof(struct sadb_msg) +
		(sizeof(struct sadb_lifetime) * 3) +
1992
		(sizeof(struct sadb_address) * 2) +
L
Linus Torvalds 已提交
1993 1994
		(sockaddr_size * 2) +
		sizeof(struct sadb_x_policy) +
1995 1996
		(xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
		(socklen * 2) +
1997
		pfkey_xfrm_policy2sec_ctx_size(xp);
L
Linus Torvalds 已提交
1998 1999
}

2000
static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
L
Linus Torvalds 已提交
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
{
	struct sk_buff *skb;
	int size;

	size = pfkey_xfrm_policy2msg_size(xp);

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

	return skb;
}

2014
static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
L
Linus Torvalds 已提交
2015 2016 2017 2018 2019
{
	struct sadb_msg *hdr;
	struct sadb_address *addr;
	struct sadb_lifetime *lifetime;
	struct sadb_x_policy *pol;
2020 2021
	struct sadb_x_sec_ctx *sec_ctx;
	struct xfrm_sec_ctx *xfrm_ctx;
L
Linus Torvalds 已提交
2022 2023 2024
	int i;
	int size;
	int sockaddr_size = pfkey_sockaddr_size(xp->family);
2025
	int socklen = pfkey_sockaddr_len(xp->family);
L
Linus Torvalds 已提交
2026 2027 2028 2029 2030 2031 2032 2033

	size = pfkey_xfrm_policy2msg_size(xp);

	/* 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 */
2034
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
2035
					      sizeof(struct sadb_address)+sockaddr_size);
2036
	addr->sadb_address_len =
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041 2042
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
	addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
	addr->sadb_address_reserved = 0;
2043 2044 2045 2046
	if (!pfkey_sockaddr_fill(&xp->selector.saddr,
				 xp->selector.sport,
				 (struct sockaddr *) (addr + 1),
				 xp->family))
L
Linus Torvalds 已提交
2047 2048 2049
		BUG();

	/* dst address */
2050
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
2051 2052 2053 2054 2055 2056
					      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_DST;
	addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
2057
	addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
L
Linus Torvalds 已提交
2058
	addr->sadb_address_reserved = 0;
2059 2060 2061 2062

	pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
			    (struct sockaddr *) (addr + 1),
			    xp->family);
L
Linus Torvalds 已提交
2063 2064

	/* hard time */
2065
	lifetime = (struct sadb_lifetime *)  skb_put(skb,
L
Linus Torvalds 已提交
2066 2067 2068 2069 2070 2071 2072 2073 2074
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.hard_packet_limit);
	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
	lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
	lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
	/* soft time */
2075
	lifetime = (struct sadb_lifetime *)  skb_put(skb,
L
Linus Torvalds 已提交
2076 2077 2078 2079 2080 2081 2082 2083 2084
						     sizeof(struct sadb_lifetime));
	lifetime->sadb_lifetime_len =
		sizeof(struct sadb_lifetime)/sizeof(uint64_t);
	lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
	lifetime->sadb_lifetime_allocations =  _X2KEY(xp->lft.soft_packet_limit);
	lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
	lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
	lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
	/* current time */
2085
	lifetime = (struct sadb_lifetime *)  skb_put(skb,
L
Linus Torvalds 已提交
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
						     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;
2106
	pol->sadb_x_policy_reserved = 0;
L
Linus Torvalds 已提交
2107 2108 2109 2110
	pol->sadb_x_policy_id = xp->index;
	pol->sadb_x_policy_priority = xp->priority;

	for (i=0; i<xp->xfrm_nr; i++) {
2111
		const struct xfrm_tmpl *t = xp->xfrm_vec + i;
L
Linus Torvalds 已提交
2112 2113
		struct sadb_x_ipsecrequest *rq;
		int req_size;
2114
		int mode;
L
Linus Torvalds 已提交
2115 2116

		req_size = sizeof(struct sadb_x_ipsecrequest);
2117 2118 2119 2120
		if (t->mode == XFRM_MODE_TUNNEL) {
			socklen = pfkey_sockaddr_len(t->encap_family);
			req_size += socklen * 2;
		} else {
L
Linus Torvalds 已提交
2121
			size -= 2*socklen;
2122
		}
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127
		rq = (void*)skb_put(skb, req_size);
		pol->sadb_x_policy_len += req_size/8;
		memset(rq, 0, sizeof(*rq));
		rq->sadb_x_ipsecrequest_len = req_size;
		rq->sadb_x_ipsecrequest_proto = t->id.proto;
2128 2129
		if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
			return -EINVAL;
2130
		rq->sadb_x_ipsecrequest_mode = mode;
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136
		rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
		if (t->reqid)
			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
		if (t->optional)
			rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
		rq->sadb_x_ipsecrequest_reqid = t->reqid;
2137

2138
		if (t->mode == XFRM_MODE_TUNNEL) {
2139 2140 2141 2142 2143 2144 2145
			u8 *sa = (void *)(rq + 1);
			pfkey_sockaddr_fill(&t->saddr, 0,
					    (struct sockaddr *)sa,
					    t->encap_family);
			pfkey_sockaddr_fill(&t->id.daddr, 0,
					    (struct sockaddr *) (sa + socklen),
					    t->encap_family);
L
Linus Torvalds 已提交
2146 2147
		}
	}
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162

	/* security context */
	if ((xfrm_ctx = xp->security)) {
		int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);

		sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
		sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
		       xfrm_ctx->ctx_len);
	}

L
Linus Torvalds 已提交
2163 2164
	hdr->sadb_msg_len = size / sizeof(uint64_t);
	hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
2165 2166

	return 0;
L
Linus Torvalds 已提交
2167 2168
}

2169
static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2170 2171 2172 2173 2174 2175
{
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	int err;

	out_skb = pfkey_xfrm_policy2msg_prep(xp);
2176 2177 2178
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

2179 2180 2181
	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
	if (err < 0)
		return err;
2182 2183 2184 2185

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = PF_KEY_V2;

2186
	if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
2187 2188 2189 2190 2191
		out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
	else
		out_hdr->sadb_msg_type = event2poltype(c->event);
	out_hdr->sadb_msg_errno = 0;
	out_hdr->sadb_msg_seq = c->seq;
2192
	out_hdr->sadb_msg_pid = c->portid;
A
Alexey Dobriyan 已提交
2193
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
2194 2195 2196 2197
	return 0;

}

2198
static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
2199
{
A
Alexey Dobriyan 已提交
2200
	struct net *net = sock_net(sk);
2201
	int err = 0;
L
Linus Torvalds 已提交
2202 2203 2204 2205
	struct sadb_lifetime *lifetime;
	struct sadb_address *sa;
	struct sadb_x_policy *pol;
	struct xfrm_policy *xp;
2206
	struct km_event c;
2207
	struct sadb_x_sec_ctx *sec_ctx;
L
Linus Torvalds 已提交
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219

	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;

A
Alexey Dobriyan 已提交
2220
	xp = xfrm_policy_alloc(net, GFP_KERNEL);
L
Linus Torvalds 已提交
2221 2222 2223 2224 2225 2226 2227
	if (xp == NULL)
		return -ENOBUFS;

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

J
Junwei Zhang 已提交
2228
	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
L
Linus Torvalds 已提交
2229 2230 2231 2232 2233 2234
	xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
	xp->selector.family = xp->family;
	xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
	xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
	xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
	if (xp->selector.sport)
2235
		xp->selector.sport_mask = htons(0xffff);
L
Linus Torvalds 已提交
2236

J
Junwei Zhang 已提交
2237
	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
L
Linus Torvalds 已提交
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247
	pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
	xp->selector.prefixlen_d = sa->sadb_address_prefixlen;

	/* 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);

	xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
	if (xp->selector.dport)
2248
		xp->selector.dport_mask = htons(0xffff);
L
Linus Torvalds 已提交
2249

J
Joe Perches 已提交
2250
	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2251 2252 2253 2254 2255 2256 2257 2258
	if (sec_ctx != NULL) {
		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);

		if (!uctx) {
			err = -ENOBUFS;
			goto out;
		}

2259
		err = security_xfrm_policy_alloc(&xp->security, uctx);
2260 2261 2262 2263 2264 2265
		kfree(uctx);

		if (err)
			goto out;
	}

L
Linus Torvalds 已提交
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
	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;
	if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
		xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
		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) {
		xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
		xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
		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;

	err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
				 hdr->sadb_msg_type != SADB_X_SPDUPDATE);
2289

J
Joy Latten 已提交
2290
	xfrm_audit_policy_add(xp, err ? 0 : 1,
2291 2292
			      audit_get_loginuid(current),
			      audit_get_sessionid(current), 0);
J
Joy Latten 已提交
2293

2294 2295
	if (err)
		goto out;
L
Linus Torvalds 已提交
2296

2297
	if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
2298
		c.event = XFRM_MSG_UPDPOLICY;
2299
	else
2300
		c.event = XFRM_MSG_NEWPOLICY;
L
Linus Torvalds 已提交
2301

2302
	c.seq = hdr->sadb_msg_seq;
2303
	c.portid = hdr->sadb_msg_pid;
L
Linus Torvalds 已提交
2304

2305 2306
	km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
	xfrm_pol_put(xp);
L
Linus Torvalds 已提交
2307 2308 2309
	return 0;

out:
H
Herbert Xu 已提交
2310
	xp->walk.dead = 1;
2311
	xfrm_policy_destroy(xp);
L
Linus Torvalds 已提交
2312 2313 2314
	return err;
}

2315
static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
2316
{
A
Alexey Dobriyan 已提交
2317
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
2318 2319 2320
	int err;
	struct sadb_address *sa;
	struct sadb_x_policy *pol;
2321
	struct xfrm_policy *xp;
L
Linus Torvalds 已提交
2322
	struct xfrm_selector sel;
2323
	struct km_event c;
2324
	struct sadb_x_sec_ctx *sec_ctx;
2325
	struct xfrm_sec_ctx *pol_ctx = NULL;
L
Linus Torvalds 已提交
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337

	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));

J
Junwei Zhang 已提交
2338
	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
L
Linus Torvalds 已提交
2339 2340 2341 2342 2343
	sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
	sel.prefixlen_s = sa->sadb_address_prefixlen;
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
	sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
	if (sel.sport)
2344
		sel.sport_mask = htons(0xffff);
L
Linus Torvalds 已提交
2345

J
Junwei Zhang 已提交
2346
	sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
L
Linus Torvalds 已提交
2347 2348 2349 2350 2351
	pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
	sel.prefixlen_d = sa->sadb_address_prefixlen;
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
	sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
	if (sel.dport)
2352
		sel.dport_mask = htons(0xffff);
L
Linus Torvalds 已提交
2353

J
Joe Perches 已提交
2354
	sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
2355 2356 2357 2358 2359 2360
	if (sec_ctx != NULL) {
		struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);

		if (!uctx)
			return -ENOMEM;

2361
		err = security_xfrm_policy_alloc(&pol_ctx, uctx);
2362 2363 2364
		kfree(uctx);
		if (err)
			return err;
2365
	}
J
Joy Latten 已提交
2366

2367
	xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2368 2369 2370
				   pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
				   1, &err);
	security_xfrm_policy_free(pol_ctx);
L
Linus Torvalds 已提交
2371 2372 2373
	if (xp == NULL)
		return -ENOENT;

J
Joy Latten 已提交
2374
	xfrm_audit_policy_delete(xp, err ? 0 : 1,
2375 2376
				 audit_get_loginuid(current),
				 audit_get_sessionid(current), 0);
2377 2378

	if (err)
C
Catherine Zhang 已提交
2379
		goto out;
2380

2381
	c.seq = hdr->sadb_msg_seq;
2382
	c.portid = hdr->sadb_msg_pid;
2383
	c.data.byid = 0;
2384
	c.event = XFRM_MSG_DELPOLICY;
2385 2386
	km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);

C
Catherine Zhang 已提交
2387
out:
2388
	xfrm_pol_put(xp);
2389 2390
	if (err == 0)
		xfrm_garbage_collect(net);
2391 2392 2393
	return err;
}

2394
static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
2395 2396 2397 2398 2399 2400
{
	int err;
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
	err = 0;

L
Linus Torvalds 已提交
2401 2402 2403 2404 2405
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
	if (IS_ERR(out_skb)) {
		err =  PTR_ERR(out_skb);
		goto out;
	}
2406 2407 2408
	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
	if (err < 0)
		goto out;
L
Linus Torvalds 已提交
2409 2410 2411

	out_hdr = (struct sadb_msg *) out_skb->data;
	out_hdr->sadb_msg_version = hdr->sadb_msg_version;
2412
	out_hdr->sadb_msg_type = hdr->sadb_msg_type;
L
Linus Torvalds 已提交
2413 2414 2415 2416
	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;
A
Alexey Dobriyan 已提交
2417
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
L
Linus Torvalds 已提交
2418 2419 2420 2421 2422 2423
	err = 0;

out:
	return err;
}

2424 2425 2426
#ifdef CONFIG_NET_KEY_MIGRATE
static int pfkey_sockaddr_pair_size(sa_family_t family)
{
2427
	return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
2428 2429
}

2430
static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
2431 2432 2433
			       xfrm_address_t *saddr, xfrm_address_t *daddr,
			       u16 *family)
{
2434 2435
	int af, socklen;

2436
	if (ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
2437 2438
		return -EINVAL;

2439
	af = pfkey_sockaddr_extract(sa, saddr);
2440 2441 2442 2443
	if (!af)
		return -EINVAL;

	socklen = pfkey_sockaddr_len(af);
2444
	if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
2445
				   daddr) != af)
2446 2447
		return -EINVAL;

2448
	*family = af;
2449 2450 2451 2452 2453 2454 2455 2456
	return 0;
}

static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
				    struct xfrm_migrate *m)
{
	int err;
	struct sadb_x_ipsecrequest *rq2;
2457
	int mode;
2458 2459 2460 2461 2462 2463

	if (len <= sizeof(struct sadb_x_ipsecrequest) ||
	    len < rq1->sadb_x_ipsecrequest_len)
		return -EINVAL;

	/* old endoints */
2464 2465 2466
	err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
				  rq1->sadb_x_ipsecrequest_len,
				  &m->old_saddr, &m->old_daddr,
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
				  &m->old_family);
	if (err)
		return err;

	rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
	len -= rq1->sadb_x_ipsecrequest_len;

	if (len <= sizeof(struct sadb_x_ipsecrequest) ||
	    len < rq2->sadb_x_ipsecrequest_len)
		return -EINVAL;

	/* new endpoints */
2479 2480 2481
	err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
				  rq2->sadb_x_ipsecrequest_len,
				  &m->new_saddr, &m->new_daddr,
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
				  &m->new_family);
	if (err)
		return err;

	if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
	    rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
	    rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
		return -EINVAL;

	m->proto = rq1->sadb_x_ipsecrequest_proto;
2492 2493 2494
	if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
		return -EINVAL;
	m->mode = mode;
2495 2496 2497 2498 2499 2500 2501
	m->reqid = rq1->sadb_x_ipsecrequest_reqid;

	return ((int)(rq1->sadb_x_ipsecrequest_len +
		      rq2->sadb_x_ipsecrequest_len));
}

static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
2502
			 const struct sadb_msg *hdr, void * const *ext_hdrs)
2503 2504 2505 2506
{
	int i, len, ret, err = -EINVAL;
	u8 dir;
	struct sadb_address *sa;
2507
	struct sadb_x_kmaddress *kma;
2508 2509 2510 2511
	struct sadb_x_policy *pol;
	struct sadb_x_ipsecrequest *rq;
	struct xfrm_selector sel;
	struct xfrm_migrate m[XFRM_MAX_DEPTH];
2512
	struct xfrm_kmaddress k;
2513
	struct net *net = sock_net(sk);
2514 2515

	if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
2516
				     ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
2517 2518 2519 2520 2521
	    !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
		err = -EINVAL;
		goto out;
	}

2522
	kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
2523 2524 2525 2526 2527 2528 2529
	pol = ext_hdrs[SADB_X_EXT_POLICY - 1];

	if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
		err = -EINVAL;
		goto out;
	}

2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
	if (kma) {
		/* convert sadb_x_kmaddress to xfrm_kmaddress */
		k.reserved = kma->sadb_x_kmaddress_reserved;
		ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
					  8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
					  &k.local, &k.remote, &k.family);
		if (ret < 0) {
			err = ret;
			goto out;
		}
	}

2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
	dir = pol->sadb_x_policy_dir - 1;
	memset(&sel, 0, sizeof(sel));

	/* set source address info of selector */
	sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
	sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
	sel.prefixlen_s = sa->sadb_address_prefixlen;
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
	sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
	if (sel.sport)
A
Al Viro 已提交
2552
		sel.sport_mask = htons(0xffff);
2553 2554 2555 2556 2557 2558 2559 2560

	/* set destination address info of selector */
	sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1],
	pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
	sel.prefixlen_d = sa->sadb_address_prefixlen;
	sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
	sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
	if (sel.dport)
A
Al Viro 已提交
2561
		sel.dport_mask = htons(0xffff);
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585

	rq = (struct sadb_x_ipsecrequest *)(pol + 1);

	/* extract ipsecrequests */
	i = 0;
	len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);

	while (len > 0 && i < XFRM_MAX_DEPTH) {
		ret = ipsecrequests_to_migrate(rq, len, &m[i]);
		if (ret < 0) {
			err = ret;
			goto out;
		} else {
			rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
			len -= ret;
			i++;
		}
	}

	if (!i || len > 0) {
		err = -EINVAL;
		goto out;
	}

2586
	return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
2587
			    kma ? &k : NULL, net);
2588 2589 2590 2591 2592 2593

 out:
	return err;
}
#else
static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
S
Stephen Hemminger 已提交
2594
			 const struct sadb_msg *hdr, void * const *ext_hdrs)
2595 2596 2597 2598 2599 2600
{
	return -ENOPROTOOPT;
}
#endif


2601
static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
2602
{
A
Alexey Dobriyan 已提交
2603
	struct net *net = sock_net(sk);
2604
	unsigned int dir;
2605
	int err = 0, delete;
L
Linus Torvalds 已提交
2606 2607
	struct sadb_x_policy *pol;
	struct xfrm_policy *xp;
2608
	struct km_event c;
L
Linus Torvalds 已提交
2609 2610 2611 2612

	if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
		return -EINVAL;

2613 2614 2615 2616
	dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
	if (dir >= XFRM_POLICY_MAX)
		return -EINVAL;

2617
	delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
2618
	xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
2619
			      dir, pol->sadb_x_policy_id, delete, &err);
L
Linus Torvalds 已提交
2620 2621 2622
	if (xp == NULL)
		return -ENOENT;

2623
	if (delete) {
J
Joy Latten 已提交
2624
		xfrm_audit_policy_delete(xp, err ? 0 : 1,
2625 2626
				audit_get_loginuid(current),
				audit_get_sessionid(current), 0);
L
Linus Torvalds 已提交
2627

2628 2629 2630
		if (err)
			goto out;
		c.seq = hdr->sadb_msg_seq;
2631
		c.portid = hdr->sadb_msg_pid;
2632
		c.data.byid = 1;
2633
		c.event = XFRM_MSG_DELPOLICY;
2634
		km_policy_notify(xp, dir, &c);
2635
	} else {
2636
		err = key_pol_get_resp(sk, xp, hdr, dir);
L
Linus Torvalds 已提交
2637 2638
	}

2639
out:
L
Linus Torvalds 已提交
2640
	xfrm_pol_put(xp);
2641 2642
	if (delete && err == 0)
		xfrm_garbage_collect(net);
L
Linus Torvalds 已提交
2643 2644 2645 2646 2647
	return err;
}

static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
{
2648
	struct pfkey_sock *pfk = ptr;
L
Linus Torvalds 已提交
2649 2650
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
2651
	int err;
L
Linus Torvalds 已提交
2652

2653 2654 2655
	if (!pfkey_can_dump(&pfk->sk))
		return -ENOBUFS;

L
Linus Torvalds 已提交
2656 2657 2658 2659
	out_skb = pfkey_xfrm_policy2msg_prep(xp);
	if (IS_ERR(out_skb))
		return PTR_ERR(out_skb);

2660 2661 2662
	err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
2663 2664

	out_hdr = (struct sadb_msg *) out_skb->data;
2665
	out_hdr->sadb_msg_version = pfk->dump.msg_version;
L
Linus Torvalds 已提交
2666 2667 2668
	out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
	out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
	out_hdr->sadb_msg_errno = 0;
H
Herbert Xu 已提交
2669
	out_hdr->sadb_msg_seq = count + 1;
2670
	out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
H
Herbert Xu 已提交
2671 2672 2673

	if (pfk->dump.skb)
		pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
A
Alexey Dobriyan 已提交
2674
				&pfk->sk, sock_net(&pfk->sk));
H
Herbert Xu 已提交
2675 2676
	pfk->dump.skb = out_skb;

L
Linus Torvalds 已提交
2677 2678 2679
	return 0;
}

2680 2681
static int pfkey_dump_sp(struct pfkey_sock *pfk)
{
A
Alexey Dobriyan 已提交
2682 2683
	struct net *net = sock_net(&pfk->sk);
	return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
2684 2685 2686 2687
}

static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
{
F
Fan Du 已提交
2688 2689 2690
	struct net *net = sock_net((struct sock *)pfk);

	xfrm_policy_walk_done(&pfk->dump.u.policy, net);
2691 2692
}

2693
static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
L
Linus Torvalds 已提交
2694
{
2695
	struct pfkey_sock *pfk = pfkey_sk(sk);
2696

2697 2698
	if (pfk->dump.dump != NULL)
		return -EBUSY;
L
Linus Torvalds 已提交
2699

2700
	pfk->dump.msg_version = hdr->sadb_msg_version;
2701
	pfk->dump.msg_portid = hdr->sadb_msg_pid;
2702 2703 2704 2705 2706
	pfk->dump.dump = pfkey_dump_sp;
	pfk->dump.done = pfkey_dump_sp_done;
	xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);

	return pfkey_do_dump(pfk);
L
Linus Torvalds 已提交
2707 2708
}

2709
static int key_notify_policy_flush(const struct km_event *c)
L
Linus Torvalds 已提交
2710 2711
{
	struct sk_buff *skb_out;
2712
	struct sadb_msg *hdr;
L
Linus Torvalds 已提交
2713

2714
	skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
L
Linus Torvalds 已提交
2715 2716
	if (!skb_out)
		return -ENOBUFS;
2717
	hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
J
Jerome Borsboom 已提交
2718
	hdr->sadb_msg_type = SADB_X_SPDFLUSH;
2719
	hdr->sadb_msg_seq = c->seq;
2720
	hdr->sadb_msg_pid = c->portid;
2721 2722
	hdr->sadb_msg_version = PF_KEY_V2;
	hdr->sadb_msg_errno = (uint8_t) 0;
2723
	hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
2724
	hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
2725
	hdr->sadb_msg_reserved = 0;
A
Alexey Dobriyan 已提交
2726
	pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
2727
	return 0;
L
Linus Torvalds 已提交
2728

2729 2730
}

2731
static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
2732
{
A
Alexey Dobriyan 已提交
2733
	struct net *net = sock_net(sk);
2734
	struct km_event c;
J
Joy Latten 已提交
2735
	struct xfrm_audit audit_info;
2736
	int err, err2;
L
Linus Torvalds 已提交
2737

2738
	audit_info.loginuid = audit_get_loginuid(current);
2739
	audit_info.sessionid = audit_get_sessionid(current);
J
Joy Latten 已提交
2740
	audit_info.secid = 0;
A
Alexey Dobriyan 已提交
2741
	err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2742
	err2 = unicast_flush_resp(sk, hdr);
2743 2744 2745 2746 2747
	if (err || err2) {
		if (err == -ESRCH) /* empty table - old silent behavior */
			return 0;
		return err;
	}
2748

2749
	c.data.type = XFRM_POLICY_TYPE_MAIN;
2750
	c.event = XFRM_MSG_FLUSHPOLICY;
2751
	c.portid = hdr->sadb_msg_pid;
2752
	c.seq = hdr->sadb_msg_seq;
A
Alexey Dobriyan 已提交
2753
	c.net = net;
2754
	km_policy_notify(NULL, 0, &c);
L
Linus Torvalds 已提交
2755 2756 2757 2758 2759

	return 0;
}

typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
2760
			     const struct sadb_msg *hdr, void * const *ext_hdrs);
M
Mathias Krause 已提交
2761
static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
L
Linus Torvalds 已提交
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783
	[SADB_RESERVED]		= pfkey_reserved,
	[SADB_GETSPI]		= pfkey_getspi,
	[SADB_UPDATE]		= pfkey_add,
	[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,
2784
	[SADB_X_MIGRATE]	= pfkey_migrate,
L
Linus Torvalds 已提交
2785 2786
};

2787
static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
L
Linus Torvalds 已提交
2788 2789 2790 2791 2792
{
	void *ext_hdrs[SADB_EXT_MAX];
	int err;

	pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
A
Alexey Dobriyan 已提交
2793
			BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
L
Linus Torvalds 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831

	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;
}

2832 2833
static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
				const struct xfrm_algo_desc *d)
L
Linus Torvalds 已提交
2834
{
2835 2836 2837 2838 2839 2840
	unsigned int id = d->desc.sadb_alg_id;

	if (id >= sizeof(t->aalgos) * 8)
		return 0;

	return (t->aalgos >> id) & 1;
L
Linus Torvalds 已提交
2841 2842
}

2843 2844
static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
				const struct xfrm_algo_desc *d)
L
Linus Torvalds 已提交
2845
{
2846 2847 2848 2849 2850 2851
	unsigned int id = d->desc.sadb_alg_id;

	if (id >= sizeof(t->ealgos) * 8)
		return 0;

	return (t->ealgos >> id) & 1;
L
Linus Torvalds 已提交
2852 2853
}

2854
static int count_ah_combs(const struct xfrm_tmpl *t)
L
Linus Torvalds 已提交
2855 2856 2857 2858
{
	int i, sz = 0;

	for (i = 0; ; i++) {
2859
		const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
L
Linus Torvalds 已提交
2860 2861
		if (!aalg)
			break;
2862 2863
		if (!aalg->pfkey_supported)
			continue;
L
Linus Torvalds 已提交
2864 2865 2866 2867 2868 2869
		if (aalg_tmpl_set(t, aalg) && aalg->available)
			sz += sizeof(struct sadb_comb);
	}
	return sz + sizeof(struct sadb_prop);
}

2870
static int count_esp_combs(const struct xfrm_tmpl *t)
L
Linus Torvalds 已提交
2871 2872 2873 2874
{
	int i, k, sz = 0;

	for (i = 0; ; i++) {
2875
		const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
L
Linus Torvalds 已提交
2876 2877
		if (!ealg)
			break;
2878

2879 2880 2881
		if (!ealg->pfkey_supported)
			continue;

L
Linus Torvalds 已提交
2882 2883
		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
			continue;
2884

L
Linus Torvalds 已提交
2885
		for (k = 1; ; k++) {
2886
			const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
L
Linus Torvalds 已提交
2887 2888
			if (!aalg)
				break;
2889

2890 2891 2892
			if (!aalg->pfkey_supported)
				continue;

L
Linus Torvalds 已提交
2893 2894 2895 2896 2897 2898 2899
			if (aalg_tmpl_set(t, aalg) && aalg->available)
				sz += sizeof(struct sadb_comb);
		}
	}
	return sz + sizeof(struct sadb_prop);
}

2900
static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
L
Linus Torvalds 已提交
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
{
	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;
	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));

	for (i = 0; ; i++) {
2912
		const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
L
Linus Torvalds 已提交
2913 2914 2915
		if (!aalg)
			break;

2916 2917 2918
		if (!aalg->pfkey_supported)
			continue;

L
Linus Torvalds 已提交
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
		if (aalg_tmpl_set(t, aalg) && aalg->available) {
			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;
			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_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;
		}
	}
}

2935
static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
L
Linus Torvalds 已提交
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
{
	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;
	memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));

	for (i=0; ; i++) {
2947
		const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
L
Linus Torvalds 已提交
2948 2949
		if (!ealg)
			break;
2950

2951 2952 2953
		if (!ealg->pfkey_supported)
			continue;

L
Linus Torvalds 已提交
2954 2955
		if (!(ealg_tmpl_set(t, ealg) && ealg->available))
			continue;
2956

L
Linus Torvalds 已提交
2957 2958
		for (k = 1; ; k++) {
			struct sadb_comb *c;
2959
			const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
L
Linus Torvalds 已提交
2960 2961
			if (!aalg)
				break;
2962 2963
			if (!aalg->pfkey_supported)
				continue;
L
Linus Torvalds 已提交
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
			if (!(aalg_tmpl_set(t, aalg) && aalg->available))
				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;
			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;
			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;
		}
	}
}

2983
static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
2984 2985 2986 2987
{
	return 0;
}

2988
static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
L
Linus Torvalds 已提交
2989 2990 2991
{
	struct sk_buff *out_skb;
	struct sadb_msg *out_hdr;
2992 2993 2994
	int hard;
	int hsc;

2995
	hard = c->data.hard;
2996 2997 2998 2999
	if (hard)
		hsc = 2;
	else
		hsc = 1;
L
Linus Torvalds 已提交
3000

3001
	out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
L
Linus Torvalds 已提交
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
	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;

A
Alexey Dobriyan 已提交
3014
	pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
L
Linus Torvalds 已提交
3015 3016 3017
	return 0;
}

3018
static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
3019
{
A
Alexey Dobriyan 已提交
3020
	struct net *net = x ? xs_net(x) : c->net;
A
Alexey Dobriyan 已提交
3021 3022 3023
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);

	if (atomic_read(&net_pfkey->socks_nr) == 0)
3024 3025
		return 0;

3026
	switch (c->event) {
3027
	case XFRM_MSG_EXPIRE:
3028
		return key_notify_sa_expire(x, c);
3029 3030 3031
	case XFRM_MSG_DELSA:
	case XFRM_MSG_NEWSA:
	case XFRM_MSG_UPDSA:
3032
		return key_notify_sa(x, c);
3033
	case XFRM_MSG_FLUSHSA:
3034
		return key_notify_sa_flush(c);
J
Jamal Hadi Salim 已提交
3035 3036
	case XFRM_MSG_NEWAE: /* not yet supported */
		break;
3037
	default:
3038
		pr_err("pfkey: Unknown SA event %d\n", c->event);
3039 3040 3041 3042 3043 3044
		break;
	}

	return 0;
}

3045
static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3046
{
3047 3048 3049
	if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
		return 0;

3050
	switch (c->event) {
3051
	case XFRM_MSG_POLEXPIRE:
3052
		return key_notify_policy_expire(xp, c);
3053 3054 3055
	case XFRM_MSG_DELPOLICY:
	case XFRM_MSG_NEWPOLICY:
	case XFRM_MSG_UPDPOLICY:
3056
		return key_notify_policy(xp, dir, c);
3057
	case XFRM_MSG_FLUSHPOLICY:
3058 3059
		if (c->data.type != XFRM_POLICY_TYPE_MAIN)
			break;
3060 3061
		return key_notify_policy_flush(c);
	default:
3062
		pr_err("pfkey: Unknown policy event %d\n", c->event);
3063 3064 3065 3066 3067 3068
		break;
	}

	return 0;
}

L
Linus Torvalds 已提交
3069 3070 3071
static u32 get_acqseq(void)
{
	u32 res;
3072
	static atomic_t acqseq;
L
Linus Torvalds 已提交
3073

3074 3075 3076
	do {
		res = atomic_inc_return(&acqseq);
	} while (!res);
L
Linus Torvalds 已提交
3077 3078 3079
	return res;
}

3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
static bool pfkey_is_alive(const struct km_event *c)
{
	struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
	struct sock *sk;
	bool is_alive = false;

	rcu_read_lock();
	sk_for_each_rcu(sk, &net_pfkey->table) {
		if (pfkey_sk(sk)->registered) {
			is_alive = true;
			break;
		}
	}
	rcu_read_unlock();

	return is_alive;
}

3098
static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
L
Linus Torvalds 已提交
3099 3100 3101 3102 3103 3104 3105
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_address *addr;
	struct sadb_x_policy *pol;
	int sockaddr_size;
	int size;
3106 3107 3108
	struct sadb_x_sec_ctx *sec_ctx;
	struct xfrm_sec_ctx *xfrm_ctx;
	int ctx_size = 0;
3109

L
Linus Torvalds 已提交
3110 3111 3112 3113 3114 3115 3116 3117
	sockaddr_size = pfkey_sockaddr_size(x->props.family);
	if (!sockaddr_size)
		return -EINVAL;

	size = sizeof(struct sadb_msg) +
		(sizeof(struct sadb_address) * 2) +
		(sockaddr_size * 2) +
		sizeof(struct sadb_x_policy);
3118

L
Linus Torvalds 已提交
3119 3120 3121 3122 3123
	if (x->id.proto == IPPROTO_AH)
		size += count_ah_combs(t);
	else if (x->id.proto == IPPROTO_ESP)
		size += count_esp_combs(t);

3124 3125 3126 3127 3128
	if ((xfrm_ctx = x->security)) {
		ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
		size +=  sizeof(struct sadb_x_sec_ctx) + ctx_size;
	}

L
Linus Torvalds 已提交
3129 3130 3131
	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return -ENOMEM;
3132

L
Linus Torvalds 已提交
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
	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;
	hdr->sadb_msg_seq = x->km.seq = get_acqseq();
	hdr->sadb_msg_pid = 0;

	/* src address */
3144
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
3145
					      sizeof(struct sadb_address)+sockaddr_size);
3146
	addr->sadb_address_len =
L
Linus Torvalds 已提交
3147 3148 3149 3150 3151
		(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;
3152 3153 3154 3155 3156
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(&x->props.saddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
L
Linus Torvalds 已提交
3157
		BUG();
3158

L
Linus Torvalds 已提交
3159
	/* dst address */
3160
	addr = (struct sadb_address*) skb_put(skb,
L
Linus Torvalds 已提交
3161 3162 3163 3164 3165 3166 3167
					      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_DST;
	addr->sadb_address_proto = 0;
	addr->sadb_address_reserved = 0;
3168 3169 3170 3171 3172
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(&x->id.daddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
L
Linus Torvalds 已提交
3173 3174 3175 3176 3177 3178
		BUG();

	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;
3179
	pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
3180
	pol->sadb_x_policy_reserved = 0;
L
Linus Torvalds 已提交
3181
	pol->sadb_x_policy_id = xp->index;
3182
	pol->sadb_x_policy_priority = xp->priority;
L
Linus Torvalds 已提交
3183 3184 3185 3186 3187 3188 3189

	/* 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);

3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203
	/* security context */
	if (xfrm_ctx) {
		sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
				sizeof(struct sadb_x_sec_ctx) + ctx_size);
		sec_ctx->sadb_x_sec_len =
		  (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
		sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
		sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
		sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
		sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
		memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
		       xfrm_ctx->ctx_len);
	}

A
Alexey Dobriyan 已提交
3204
	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
L
Linus Torvalds 已提交
3205 3206
}

3207
static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
3208
						u8 *data, int len, int *dir)
L
Linus Torvalds 已提交
3209
{
A
Alexey Dobriyan 已提交
3210
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
3211 3212
	struct xfrm_policy *xp;
	struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
3213
	struct sadb_x_sec_ctx *sec_ctx;
L
Linus Torvalds 已提交
3214

3215
	switch (sk->sk_family) {
L
Linus Torvalds 已提交
3216 3217 3218 3219 3220 3221
	case AF_INET:
		if (opt != IP_IPSEC_POLICY) {
			*dir = -EOPNOTSUPP;
			return NULL;
		}
		break;
E
Eric Dumazet 已提交
3222
#if IS_ENABLED(CONFIG_IPV6)
L
Linus Torvalds 已提交
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
	case AF_INET6:
		if (opt != IPV6_IPSEC_POLICY) {
			*dir = -EOPNOTSUPP;
			return NULL;
		}
		break;
#endif
	default:
		*dir = -EINVAL;
		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;

A
Alexey Dobriyan 已提交
3243
	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
L
Linus Torvalds 已提交
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255
	if (xp == NULL) {
		*dir = -ENOBUFS;
		return NULL;
	}

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

	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;
3256
	xp->family = sk->sk_family;
L
Linus Torvalds 已提交
3257 3258 3259 3260 3261 3262

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

3263 3264 3265 3266 3267 3268 3269 3270 3271
	/* security context too */
	if (len >= (pol->sadb_x_policy_len*8 +
	    sizeof(struct sadb_x_sec_ctx))) {
		char *p = (char *)pol;
		struct xfrm_user_sec_ctx *uctx;

		p += pol->sadb_x_policy_len*8;
		sec_ctx = (struct sadb_x_sec_ctx *)p;
		if (len < pol->sadb_x_policy_len*8 +
3272 3273
		    sec_ctx->sadb_x_sec_len) {
			*dir = -EINVAL;
3274
			goto out;
3275
		}
3276 3277 3278
		if ((*dir = verify_sec_ctx_len(p)))
			goto out;
		uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx);
3279
		*dir = security_xfrm_policy_alloc(&xp->security, uctx);
3280 3281 3282 3283 3284 3285
		kfree(uctx);

		if (*dir)
			goto out;
	}

L
Linus Torvalds 已提交
3286 3287 3288 3289
	*dir = pol->sadb_x_policy_dir-1;
	return xp;

out:
3290
	xp->walk.dead = 1;
3291
	xfrm_policy_destroy(xp);
L
Linus Torvalds 已提交
3292 3293 3294
	return NULL;
}

A
Al Viro 已提交
3295
static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
L
Linus Torvalds 已提交
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
{
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_sa *sa;
	struct sadb_address *addr;
	struct sadb_x_nat_t_port *n_port;
	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;

	if (!x->encap)
		return -EINVAL;

	natt = x->encap;

	/* 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)
	 */
3324

L
Linus Torvalds 已提交
3325 3326 3327 3328 3329
	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);
3330

L
Linus Torvalds 已提交
3331 3332 3333
	skb =  alloc_skb(size + 16, GFP_ATOMIC);
	if (skb == NULL)
		return -ENOMEM;
3334

L
Linus Torvalds 已提交
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
	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);
3359
	addr->sadb_address_len =
L
Linus Torvalds 已提交
3360 3361 3362 3363 3364
		(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;
3365 3366 3367 3368 3369
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(&x->props.saddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
L
Linus Torvalds 已提交
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381
		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);
3382
	addr->sadb_address_len =
L
Linus Torvalds 已提交
3383 3384 3385 3386 3387
		(sizeof(struct sadb_address)+sockaddr_size)/
			sizeof(uint64_t);
	addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
	addr->sadb_address_proto = 0;
	addr->sadb_address_reserved = 0;
3388 3389 3390 3391 3392
	addr->sadb_address_prefixlen =
		pfkey_sockaddr_fill(ipaddr, 0,
				    (struct sockaddr *) (addr + 1),
				    x->props.family);
	if (!addr->sadb_address_prefixlen)
L
Linus Torvalds 已提交
3393 3394 3395 3396 3397 3398 3399 3400 3401
		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;

A
Alexey Dobriyan 已提交
3402
	return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL, xs_net(x));
L
Linus Torvalds 已提交
3403 3404
}

3405 3406
#ifdef CONFIG_NET_KEY_MIGRATE
static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
3407
			    const struct xfrm_selector *sel)
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
{
	struct sadb_address *addr;
	addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
	addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
	addr->sadb_address_exttype = type;
	addr->sadb_address_proto = sel->proto;
	addr->sadb_address_reserved = 0;

	switch (type) {
	case SADB_EXT_ADDRESS_SRC:
3418 3419 3420 3421
		addr->sadb_address_prefixlen = sel->prefixlen_s;
		pfkey_sockaddr_fill(&sel->saddr, 0,
				    (struct sockaddr *)(addr + 1),
				    sel->family);
3422 3423
		break;
	case SADB_EXT_ADDRESS_DST:
3424 3425 3426 3427
		addr->sadb_address_prefixlen = sel->prefixlen_d;
		pfkey_sockaddr_fill(&sel->daddr, 0,
				    (struct sockaddr *)(addr + 1),
				    sel->family);
3428 3429 3430 3431 3432 3433 3434 3435
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

3436

3437
static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461
{
	struct sadb_x_kmaddress *kma;
	u8 *sa;
	int family = k->family;
	int socklen = pfkey_sockaddr_len(family);
	int size_req;

	size_req = (sizeof(struct sadb_x_kmaddress) +
		    pfkey_sockaddr_pair_size(family));

	kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
	memset(kma, 0, size_req);
	kma->sadb_x_kmaddress_len = size_req / 8;
	kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
	kma->sadb_x_kmaddress_reserved = k->reserved;

	sa = (u8 *)(kma + 1);
	if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
	    !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
		return -EINVAL;

	return 0;
}

3462 3463 3464
static int set_ipsecrequest(struct sk_buff *skb,
			    uint8_t proto, uint8_t mode, int level,
			    uint32_t reqid, uint8_t family,
3465
			    const xfrm_address_t *src, const xfrm_address_t *dst)
3466 3467
{
	struct sadb_x_ipsecrequest *rq;
3468 3469
	u8 *sa;
	int socklen = pfkey_sockaddr_len(family);
3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
	int size_req;

	size_req = sizeof(struct sadb_x_ipsecrequest) +
		   pfkey_sockaddr_pair_size(family);

	rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
	memset(rq, 0, size_req);
	rq->sadb_x_ipsecrequest_len = size_req;
	rq->sadb_x_ipsecrequest_proto = proto;
	rq->sadb_x_ipsecrequest_mode = mode;
	rq->sadb_x_ipsecrequest_level = level;
	rq->sadb_x_ipsecrequest_reqid = reqid;

3483 3484 3485
	sa = (u8 *) (rq + 1);
	if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
	    !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
3486 3487 3488 3489 3490 3491 3492
		return -EINVAL;

	return 0;
}
#endif

#ifdef CONFIG_NET_KEY_MIGRATE
3493 3494 3495
static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
			      const struct xfrm_migrate *m, int num_bundles,
			      const struct xfrm_kmaddress *k)
3496 3497 3498 3499 3500 3501 3502 3503
{
	int i;
	int sasize_sel;
	int size = 0;
	int size_pol = 0;
	struct sk_buff *skb;
	struct sadb_msg *hdr;
	struct sadb_x_policy *pol;
3504
	const struct xfrm_migrate *mp;
3505 3506 3507 3508 3509 3510 3511

	if (type != XFRM_POLICY_TYPE_MAIN)
		return 0;

	if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
		return -EINVAL;

3512 3513 3514 3515 3516 3517
	if (k != NULL) {
		/* addresses for KM */
		size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
				     pfkey_sockaddr_pair_size(k->family));
	}

3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
	/* selector */
	sasize_sel = pfkey_sockaddr_size(sel->family);
	if (!sasize_sel)
		return -EINVAL;
	size += (sizeof(struct sadb_address) + sasize_sel) * 2;

	/* policy info */
	size_pol += sizeof(struct sadb_x_policy);

	/* ipsecrequests */
	for (i = 0, mp = m; i < num_bundles; i++, mp++) {
		/* old locator pair */
		size_pol += sizeof(struct sadb_x_ipsecrequest) +
			    pfkey_sockaddr_pair_size(mp->old_family);
		/* new locator pair */
		size_pol += sizeof(struct sadb_x_ipsecrequest) +
			    pfkey_sockaddr_pair_size(mp->new_family);
	}

	size += sizeof(struct sadb_msg) + size_pol;

	/* alloc buffer */
	skb = alloc_skb(size, 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_MIGRATE;
	hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
	hdr->sadb_msg_len = size / 8;
	hdr->sadb_msg_errno = 0;
	hdr->sadb_msg_reserved = 0;
	hdr->sadb_msg_seq = 0;
	hdr->sadb_msg_pid = 0;

3554 3555
	/* Addresses to be used by KM for negotiation, if ext is available */
	if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
3556
		goto err;
3557

3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
	/* selector src */
	set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);

	/* selector dst */
	set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);

	/* policy information */
	pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
	pol->sadb_x_policy_len = size_pol / 8;
	pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
	pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
	pol->sadb_x_policy_dir = dir + 1;
3570
	pol->sadb_x_policy_reserved = 0;
3571 3572 3573 3574 3575
	pol->sadb_x_policy_id = 0;
	pol->sadb_x_policy_priority = 0;

	for (i = 0, mp = m; i < num_bundles; i++, mp++) {
		/* old ipsecrequest */
3576 3577
		int mode = pfkey_mode_from_xfrm(mp->mode);
		if (mode < 0)
3578
			goto err;
3579
		if (set_ipsecrequest(skb, mp->proto, mode,
3580 3581
				     (mp->reqid ?  IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
				     mp->reqid, mp->old_family,
3582 3583
				     &mp->old_saddr, &mp->old_daddr) < 0)
			goto err;
3584 3585

		/* new ipsecrequest */
3586
		if (set_ipsecrequest(skb, mp->proto, mode,
3587 3588
				     (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
				     mp->reqid, mp->new_family,
3589 3590
				     &mp->new_saddr, &mp->new_daddr) < 0)
			goto err;
3591 3592 3593
	}

	/* broadcast migrate message to sockets */
A
Alexey Dobriyan 已提交
3594
	pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
3595 3596

	return 0;
3597 3598 3599 3600

err:
	kfree_skb(skb);
	return -EINVAL;
3601 3602
}
#else
3603 3604 3605
static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
			      const struct xfrm_migrate *m, int num_bundles,
			      const struct xfrm_kmaddress *k)
3606 3607 3608 3609 3610
{
	return -ENOPROTOOPT;
}
#endif

L
Linus Torvalds 已提交
3611 3612 3613 3614 3615 3616 3617
static int pfkey_sendmsg(struct kiocb *kiocb,
			 struct socket *sock, struct msghdr *msg, size_t len)
{
	struct sock *sk = sock->sk;
	struct sk_buff *skb = NULL;
	struct sadb_msg *hdr = NULL;
	int err;
F
Fan Du 已提交
3618
	struct net *net = sock_net(sk);
L
Linus Torvalds 已提交
3619 3620 3621 3622 3623 3624

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

	err = -EMSGSIZE;
3625
	if ((unsigned int)len > sk->sk_sndbuf - 32)
L
Linus Torvalds 已提交
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
		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;

F
Fan Du 已提交
3641
	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
L
Linus Torvalds 已提交
3642
	err = pfkey_process(sk, skb, hdr);
F
Fan Du 已提交
3643
	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
L
Linus Torvalds 已提交
3644 3645 3646 3647

out:
	if (err && hdr && pfkey_error(hdr, err, sk) == 0)
		err = 0;
3648
	kfree_skb(skb);
L
Linus Torvalds 已提交
3649 3650 3651 3652 3653 3654 3655 3656 3657

	return err ? : len;
}

static int pfkey_recvmsg(struct kiocb *kiocb,
			 struct socket *sock, struct msghdr *msg, size_t len,
			 int flags)
{
	struct sock *sk = sock->sk;
3658
	struct pfkey_sock *pfk = pfkey_sk(sk);
L
Linus Torvalds 已提交
3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
	struct sk_buff *skb;
	int copied, err;

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

	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;
	}

3676
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
3677 3678 3679 3680
	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
	if (err)
		goto out_free;

3681
	sock_recv_ts_and_drops(msg, sk, skb);
L
Linus Torvalds 已提交
3682 3683 3684

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

3685 3686 3687 3688
	if (pfk->dump.dump != NULL &&
	    3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
		pfkey_do_dump(pfk);

L
Linus Torvalds 已提交
3689 3690 3691 3692 3693 3694
out_free:
	skb_free_datagram(sk, skb);
out:
	return err;
}

3695
static const struct proto_ops pfkey_ops = {
L
Linus Torvalds 已提交
3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718
	.family		=	PF_KEY,
	.owner		=	THIS_MODULE,
	/* 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,
};

3719
static const struct net_proto_family pfkey_family_ops = {
L
Linus Torvalds 已提交
3720 3721 3722 3723 3724 3725
	.family	=	PF_KEY,
	.create	=	pfkey_create,
	.owner	=	THIS_MODULE,
};

#ifdef CONFIG_PROC_FS
3726
static int pfkey_seq_show(struct seq_file *f, void *v)
L
Linus Torvalds 已提交
3727
{
3728
	struct sock *s = sk_entry(v);
L
Linus Torvalds 已提交
3729

3730 3731 3732
	if (v == SEQ_START_TOKEN)
		seq_printf(f ,"sk       RefCnt Rmem   Wmem   User   Inode\n");
	else
D
Dan Rosenberg 已提交
3733
		seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
L
Linus Torvalds 已提交
3734 3735
			       s,
			       atomic_read(&s->sk_refcnt),
3736 3737
			       sk_rmem_alloc_get(s),
			       sk_wmem_alloc_get(s),
3738
			       from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
L
Linus Torvalds 已提交
3739 3740
			       sock_i_ino(s)
			       );
3741 3742 3743 3744
	return 0;
}

static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
3745
	__acquires(rcu)
3746
{
3747
	struct net *net = seq_file_net(f);
A
Alexey Dobriyan 已提交
3748
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
L
Linus Torvalds 已提交
3749

S
stephen hemminger 已提交
3750 3751
	rcu_read_lock();
	return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
3752 3753 3754 3755
}

static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
{
3756
	struct net *net = seq_file_net(f);
A
Alexey Dobriyan 已提交
3757 3758
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);

S
stephen hemminger 已提交
3759
	return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
3760
}
L
Linus Torvalds 已提交
3761

3762
static void pfkey_seq_stop(struct seq_file *f, void *v)
3763
	__releases(rcu)
3764
{
S
stephen hemminger 已提交
3765
	rcu_read_unlock();
3766
}
L
Linus Torvalds 已提交
3767

3768
static const struct seq_operations pfkey_seq_ops = {
3769 3770 3771 3772 3773
	.start	= pfkey_seq_start,
	.next	= pfkey_seq_next,
	.stop	= pfkey_seq_stop,
	.show	= pfkey_seq_show,
};
L
Linus Torvalds 已提交
3774

3775 3776
static int pfkey_seq_open(struct inode *inode, struct file *file)
{
3777 3778
	return seq_open_net(inode, file, &pfkey_seq_ops,
			    sizeof(struct seq_net_private));
L
Linus Torvalds 已提交
3779
}
3780

3781
static const struct file_operations pfkey_proc_ops = {
3782 3783 3784
	.open	 = pfkey_seq_open,
	.read	 = seq_read,
	.llseek	 = seq_lseek,
3785
	.release = seq_release_net,
3786 3787
};

3788
static int __net_init pfkey_init_proc(struct net *net)
3789
{
3790 3791
	struct proc_dir_entry *e;

3792
	e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
3793
	if (e == NULL)
3794
		return -ENOMEM;
3795 3796

	return 0;
3797 3798
}

3799
static void __net_exit pfkey_exit_proc(struct net *net)
3800
{
3801
	remove_proc_entry("pfkey", net->proc_net);
3802 3803
}
#else
3804
static inline int pfkey_init_proc(struct net *net)
3805 3806 3807 3808
{
	return 0;
}

3809
static inline void pfkey_exit_proc(struct net *net)
3810 3811
{
}
L
Linus Torvalds 已提交
3812 3813 3814 3815 3816 3817 3818 3819 3820
#endif

static struct xfrm_mgr pfkeyv2_mgr =
{
	.id		= "pfkeyv2",
	.notify		= pfkey_send_notify,
	.acquire	= pfkey_send_acquire,
	.compile_policy	= pfkey_compile_policy,
	.new_mapping	= pfkey_send_new_mapping,
3821
	.notify_policy	= pfkey_send_policy_notify,
3822
	.migrate	= pfkey_send_migrate,
3823
	.is_alive	= pfkey_is_alive,
L
Linus Torvalds 已提交
3824 3825
};

A
Alexey Dobriyan 已提交
3826 3827
static int __net_init pfkey_net_init(struct net *net)
{
3828
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
A
Alexey Dobriyan 已提交
3829 3830 3831 3832
	int rv;

	INIT_HLIST_HEAD(&net_pfkey->table);
	atomic_set(&net_pfkey->socks_nr, 0);
3833

3834
	rv = pfkey_init_proc(net);
A
Alexey Dobriyan 已提交
3835 3836 3837 3838 3839 3840 3841 3842

	return rv;
}

static void __net_exit pfkey_net_exit(struct net *net)
{
	struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);

3843
	pfkey_exit_proc(net);
A
Alexey Dobriyan 已提交
3844 3845 3846 3847 3848 3849
	BUG_ON(!hlist_empty(&net_pfkey->table));
}

static struct pernet_operations pfkey_net_ops = {
	.init = pfkey_net_init,
	.exit = pfkey_net_exit,
3850 3851
	.id   = &pfkey_net_id,
	.size = sizeof(struct netns_pfkey),
A
Alexey Dobriyan 已提交
3852 3853
};

L
Linus Torvalds 已提交
3854 3855 3856 3857
static void __exit ipsec_pfkey_exit(void)
{
	xfrm_unregister_km(&pfkeyv2_mgr);
	sock_unregister(PF_KEY);
3858
	unregister_pernet_subsys(&pfkey_net_ops);
L
Linus Torvalds 已提交
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
	proto_unregister(&key_proto);
}

static int __init ipsec_pfkey_init(void)
{
	int err = proto_register(&key_proto, 0);

	if (err != 0)
		goto out;

3869
	err = register_pernet_subsys(&pfkey_net_ops);
L
Linus Torvalds 已提交
3870 3871
	if (err != 0)
		goto out_unregister_key_proto;
3872 3873 3874
	err = sock_register(&pfkey_family_ops);
	if (err != 0)
		goto out_unregister_pernet;
L
Linus Torvalds 已提交
3875 3876
	err = xfrm_register_km(&pfkeyv2_mgr);
	if (err != 0)
3877
		goto out_sock_unregister;
L
Linus Torvalds 已提交
3878 3879
out:
	return err;
3880

L
Linus Torvalds 已提交
3881 3882
out_sock_unregister:
	sock_unregister(PF_KEY);
3883 3884
out_unregister_pernet:
	unregister_pernet_subsys(&pfkey_net_ops);
L
Linus Torvalds 已提交
3885 3886 3887 3888 3889 3890 3891 3892 3893
out_unregister_key_proto:
	proto_unregister(&key_proto);
	goto out;
}

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