xfrm_user.c 74.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9
/* xfrm_user.c: User interface to configure xfrm engine.
 *
 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
 *
 * Changes:
 *	Mitsuru KANDA @USAGI
 * 	Kazunori MIYAZAWA @USAGI
 * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
 * 		IPv6 support
10
 *
Linus Torvalds's avatar
Linus Torvalds committed
11 12
 */

13
#include <linux/crypto.h>
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/socket.h>
#include <linux/string.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/pfkeyv2.h>
#include <linux/ipsec.h>
#include <linux/init.h>
#include <linux/security.h>
#include <net/sock.h>
#include <net/xfrm.h>
28
#include <net/netlink.h>
29
#include <net/ah.h>
Linus Torvalds's avatar
Linus Torvalds committed
30
#include <asm/uaccess.h>
31
#if IS_ENABLED(CONFIG_IPV6)
32 33
#include <linux/in6.h>
#endif
34
#include <asm/unaligned.h>
Linus Torvalds's avatar
Linus Torvalds committed
35

36
static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds's avatar
Linus Torvalds committed
37
{
38
	struct nlattr *rt = attrs[type];
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43
	struct xfrm_algo *algp;

	if (!rt)
		return 0;

44
	algp = nla_data(rt);
45
	if (nla_len(rt) < xfrm_alg_len(algp))
46 47
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51 52 53 54 55
	switch (type) {
	case XFRMA_ALG_AUTH:
	case XFRMA_ALG_CRYPT:
	case XFRMA_ALG_COMP:
		break;

	default:
		return -EINVAL;
56
	}
Linus Torvalds's avatar
Linus Torvalds committed
57 58 59 60 61

	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
	return 0;
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static int verify_auth_trunc(struct nlattr **attrs)
{
	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
	struct xfrm_algo_auth *algp;

	if (!rt)
		return 0;

	algp = nla_data(rt);
	if (nla_len(rt) < xfrm_alg_auth_len(algp))
		return -EINVAL;

	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
	return 0;
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static int verify_aead(struct nlattr **attrs)
{
	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
	struct xfrm_algo_aead *algp;

	if (!rt)
		return 0;

	algp = nla_data(rt);
	if (nla_len(rt) < aead_len(algp))
		return -EINVAL;

	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
	return 0;
}

94
static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95 96
			   xfrm_address_t **addrp)
{
97
	struct nlattr *rt = attrs[type];
98

99
	if (rt && addrp)
100
		*addrp = nla_data(rt);
101
}
102

103
static inline int verify_sec_ctx_len(struct nlattr **attrs)
104
{
105
	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106 107 108 109 110
	struct xfrm_user_sec_ctx *uctx;

	if (!rt)
		return 0;

111
	uctx = nla_data(rt);
112
	if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
113 114 115 116 117
		return -EINVAL;

	return 0;
}

118 119 120 121
static inline int verify_replay(struct xfrm_usersa_info *p,
				struct nlattr **attrs)
{
	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122
	struct xfrm_replay_state_esn *rs;
123

124 125
	if (!rt)
		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
126

127
	rs = nla_data(rt);
128

129 130
	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
		return -EINVAL;
131

132 133 134
	if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
	    nla_len(rt) != sizeof(*rs))
		return -EINVAL;
135

136 137
	/* As only ESP and AH support ESN feature. */
	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
138 139
		return -EINVAL;

140 141 142 143 144
	if (p->replay_window != 0)
		return -EINVAL;

	return 0;
}
145

Linus Torvalds's avatar
Linus Torvalds committed
146
static int verify_newsa_info(struct xfrm_usersa_info *p,
147
			     struct nlattr **attrs)
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152 153
{
	int err;

	err = -EINVAL;
	switch (p->family) {
	case AF_INET:
154 155 156
		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
			goto out;

Linus Torvalds's avatar
Linus Torvalds committed
157 158 159
		break;

	case AF_INET6:
160
#if IS_ENABLED(CONFIG_IPV6)
161 162 163
		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
			goto out;

Linus Torvalds's avatar
Linus Torvalds committed
164 165 166 167 168 169 170 171
		break;
#else
		err = -EAFNOSUPPORT;
		goto out;
#endif

	default:
		goto out;
172
	}
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175 176

	err = -EINVAL;
	switch (p->id.proto) {
	case IPPROTO_AH:
177 178
		if ((!attrs[XFRMA_ALG_AUTH]	&&
		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
179
		    attrs[XFRMA_ALG_AEAD]	||
180
		    attrs[XFRMA_ALG_CRYPT]	||
181
		    attrs[XFRMA_ALG_COMP]	||
182
		    attrs[XFRMA_TFCPAD])
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186
			goto out;
		break;

	case IPPROTO_ESP:
187 188 189
		if (attrs[XFRMA_ALG_COMP])
			goto out;
		if (!attrs[XFRMA_ALG_AUTH] &&
190
		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
191 192 193 194
		    !attrs[XFRMA_ALG_CRYPT] &&
		    !attrs[XFRMA_ALG_AEAD])
			goto out;
		if ((attrs[XFRMA_ALG_AUTH] ||
195
		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
196 197
		     attrs[XFRMA_ALG_CRYPT]) &&
		    attrs[XFRMA_ALG_AEAD])
Linus Torvalds's avatar
Linus Torvalds committed
198
			goto out;
199 200 201
		if (attrs[XFRMA_TFCPAD] &&
		    p->mode != XFRM_MODE_TUNNEL)
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
202 203 204
		break;

	case IPPROTO_COMP:
205
		if (!attrs[XFRMA_ALG_COMP]	||
206
		    attrs[XFRMA_ALG_AEAD]	||
207
		    attrs[XFRMA_ALG_AUTH]	||
208
		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
209
		    attrs[XFRMA_ALG_CRYPT]	||
210 211
		    attrs[XFRMA_TFCPAD]		||
		    (ntohl(p->id.spi) >= 0x10000))
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214
			goto out;
		break;

215
#if IS_ENABLED(CONFIG_IPV6)
216 217
	case IPPROTO_DSTOPTS:
	case IPPROTO_ROUTING:
218 219
		if (attrs[XFRMA_ALG_COMP]	||
		    attrs[XFRMA_ALG_AUTH]	||
220
		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
221
		    attrs[XFRMA_ALG_AEAD]	||
222 223 224
		    attrs[XFRMA_ALG_CRYPT]	||
		    attrs[XFRMA_ENCAP]		||
		    attrs[XFRMA_SEC_CTX]	||
225
		    attrs[XFRMA_TFCPAD]		||
226
		    !attrs[XFRMA_COADDR])
227 228 229 230
			goto out;
		break;
#endif

Linus Torvalds's avatar
Linus Torvalds committed
231 232
	default:
		goto out;
233
	}
Linus Torvalds's avatar
Linus Torvalds committed
234

235 236
	if ((err = verify_aead(attrs)))
		goto out;
237 238
	if ((err = verify_auth_trunc(attrs)))
		goto out;
239
	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds's avatar
Linus Torvalds committed
240
		goto out;
241
	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds's avatar
Linus Torvalds committed
242
		goto out;
243
	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds's avatar
Linus Torvalds committed
244
		goto out;
245
	if ((err = verify_sec_ctx_len(attrs)))
246
		goto out;
247 248
	if ((err = verify_replay(p, attrs)))
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251

	err = -EINVAL;
	switch (p->mode) {
252 253
	case XFRM_MODE_TRANSPORT:
	case XFRM_MODE_TUNNEL:
254
	case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami's avatar
Diego Beltrami committed
255
	case XFRM_MODE_BEET:
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259
		break;

	default:
		goto out;
260
	}
Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266 267 268

	err = 0;

out:
	return err;
}

static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
269
			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
270
			   struct nlattr *rta)
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274 275 276 277
{
	struct xfrm_algo *p, *ualg;
	struct xfrm_algo_desc *algo;

	if (!rta)
		return 0;

278
	ualg = nla_data(rta);
Linus Torvalds's avatar
Linus Torvalds committed
279 280 281 282 283 284

	algo = get_byname(ualg->alg_name, 1);
	if (!algo)
		return -ENOSYS;
	*props = algo->desc.sadb_alg_id;

285
	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
286 287 288
	if (!p)
		return -ENOMEM;

289
	strcpy(p->alg_name, algo->name);
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293
	*algpp = p;
	return 0;
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
{
	struct xfrm_algo *p, *ualg;
	struct xfrm_algo_desc *algo;

	if (!rta)
		return 0;

	ualg = nla_data(rta);

	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
	if (!algo)
		return -ENOSYS;
	x->props.ealgo = algo->desc.sadb_alg_id;

	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
	if (!p)
		return -ENOMEM;

	strcpy(p->alg_name, algo->name);
	x->ealg = p;
	x->geniv = algo->uinfo.encr.geniv;
	return 0;
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
		       struct nlattr *rta)
{
	struct xfrm_algo *ualg;
	struct xfrm_algo_auth *p;
	struct xfrm_algo_desc *algo;

	if (!rta)
		return 0;

	ualg = nla_data(rta);

	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
	if (!algo)
		return -ENOSYS;
	*props = algo->desc.sadb_alg_id;

	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
	if (!p)
		return -ENOMEM;

	strcpy(p->alg_name, algo->name);
	p->alg_key_len = ualg->alg_key_len;
	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);

	*algpp = p;
	return 0;
}

static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
			     struct nlattr *rta)
{
	struct xfrm_algo_auth *p, *ualg;
	struct xfrm_algo_desc *algo;

	if (!rta)
		return 0;

	ualg = nla_data(rta);

	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
	if (!algo)
		return -ENOSYS;
363
	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		return -EINVAL;
	*props = algo->desc.sadb_alg_id;

	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
	if (!p)
		return -ENOMEM;

	strcpy(p->alg_name, algo->name);
	if (!p->alg_trunc_len)
		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;

	*algpp = p;
	return 0;
}

379
static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
380 381 382 383 384 385 386 387 388 389 390 391
{
	struct xfrm_algo_aead *p, *ualg;
	struct xfrm_algo_desc *algo;

	if (!rta)
		return 0;

	ualg = nla_data(rta);

	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
	if (!algo)
		return -ENOSYS;
392
	x->props.ealgo = algo->desc.sadb_alg_id;
393 394 395 396 397 398

	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
	if (!p)
		return -ENOMEM;

	strcpy(p->alg_name, algo->name);
399 400
	x->aead = p;
	x->geniv = algo->uinfo.aead.geniv;
401 402 403
	return 0;
}

404 405 406 407
static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
					 struct nlattr *rp)
{
	struct xfrm_replay_state_esn *up;
408
	int ulen;
409 410 411 412 413

	if (!replay_esn || !rp)
		return 0;

	up = nla_data(rp);
414
	ulen = xfrm_replay_state_esn_len(up);
415

416 417 418 419 420
	/* Check the overall length and the internal bitmap length to avoid
	 * potential overflow. */
	if (nla_len(rp) < ulen ||
	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
	    replay_esn->bmp_len != up->bmp_len)
421 422
		return -EINVAL;

423 424 425
	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
		return -EINVAL;

426 427 428
	return 0;
}

429 430 431 432 433
static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
				       struct xfrm_replay_state_esn **preplay_esn,
				       struct nlattr *rta)
{
	struct xfrm_replay_state_esn *p, *pp, *up;
434
	int klen, ulen;
435 436 437 438 439

	if (!rta)
		return 0;

	up = nla_data(rta);
440 441
	klen = xfrm_replay_state_esn_len(up);
	ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
442

443
	p = kzalloc(klen, GFP_KERNEL);
444 445 446
	if (!p)
		return -ENOMEM;

447
	pp = kzalloc(klen, GFP_KERNEL);
448 449 450 451 452
	if (!pp) {
		kfree(p);
		return -ENOMEM;
	}

453 454 455
	memcpy(p, up, ulen);
	memcpy(pp, up, ulen);

456 457 458 459 460 461
	*replay_esn = p;
	*preplay_esn = pp;

	return 0;
}

462
static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
463 464 465 466 467 468 469 470 471 472
{
	int len = 0;

	if (xfrm_ctx) {
		len += sizeof(struct xfrm_user_sec_ctx);
		len += xfrm_ctx->ctx_len;
	}
	return len;
}

Linus Torvalds's avatar
Linus Torvalds committed
473 474 475 476 477 478
static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
{
	memcpy(&x->id, &p->id, sizeof(x->id));
	memcpy(&x->sel, &p->sel, sizeof(x->sel));
	memcpy(&x->lft, &p->lft, sizeof(x->lft));
	x->props.mode = p->mode;
479 480
	x->props.replay_window = min_t(unsigned int, p->replay_window,
					sizeof(x->replay.bitmap) * 8);
Linus Torvalds's avatar
Linus Torvalds committed
481 482
	x->props.reqid = p->reqid;
	x->props.family = p->family;
483
	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds's avatar
Linus Torvalds committed
484
	x->props.flags = p->flags;
485

486
	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
487
		x->sel.family = p->family;
Linus Torvalds's avatar
Linus Torvalds committed
488 489
}

490 491 492 493 494
/*
 * someday when pfkey also has support, we could have the code
 * somehow made shareable and move it to xfrm_state.c - JHS
 *
*/
495 496
static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
				  int update_esn)
497
{
498
	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
499
	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
500 501 502
	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
503

504 505 506 507 508 509 510 511 512
	if (re) {
		struct xfrm_replay_state_esn *replay_esn;
		replay_esn = nla_data(re);
		memcpy(x->replay_esn, replay_esn,
		       xfrm_replay_state_esn_len(replay_esn));
		memcpy(x->preplay_esn, replay_esn,
		       xfrm_replay_state_esn_len(replay_esn));
	}

513 514
	if (rp) {
		struct xfrm_replay_state *replay;
515
		replay = nla_data(rp);
516 517 518 519 520 521
		memcpy(&x->replay, replay, sizeof(*replay));
		memcpy(&x->preplay, replay, sizeof(*replay));
	}

	if (lt) {
		struct xfrm_lifetime_cur *ltime;
522
		ltime = nla_data(lt);
523 524 525 526 527 528
		x->curlft.bytes = ltime->bytes;
		x->curlft.packets = ltime->packets;
		x->curlft.add_time = ltime->add_time;
		x->curlft.use_time = ltime->use_time;
	}

529
	if (et)
530
		x->replay_maxage = nla_get_u32(et);
531

532
	if (rt)
533
		x->replay_maxdiff = nla_get_u32(rt);
534 535
}

536 537
static struct xfrm_state *xfrm_state_construct(struct net *net,
					       struct xfrm_usersa_info *p,
538
					       struct nlattr **attrs,
Linus Torvalds's avatar
Linus Torvalds committed
539 540
					       int *errp)
{
541
	struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds's avatar
Linus Torvalds committed
542 543 544 545 546 547 548
	int err = -ENOMEM;

	if (!x)
		goto error_no_put;

	copy_from_user_state(x, p);

549 550 551
	if (attrs[XFRMA_SA_EXTRA_FLAGS])
		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);

552
	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
553
		goto error;
554 555
	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
				     attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds's avatar
Linus Torvalds committed
556
		goto error;
557 558 559 560 561
	if (!x->props.aalgo) {
		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
				       attrs[XFRMA_ALG_AUTH])))
			goto error;
	}
562
	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565
		goto error;
	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
				   xfrm_calg_get_byname,
566
				   attrs[XFRMA_ALG_COMP])))
Linus Torvalds's avatar
Linus Torvalds committed
567
		goto error;
568 569 570 571 572 573 574 575

	if (attrs[XFRMA_ENCAP]) {
		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
				   sizeof(*x->encap), GFP_KERNEL);
		if (x->encap == NULL)
			goto error;
	}

576 577 578
	if (attrs[XFRMA_TFCPAD])
		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);

579 580 581 582 583 584 585
	if (attrs[XFRMA_COADDR]) {
		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
				    sizeof(*x->coaddr), GFP_KERNEL);
		if (x->coaddr == NULL)
			goto error;
	}

586 587
	xfrm_mark_get(attrs, &x->mark);

588
	err = __xfrm_init_state(x, false);
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591
	if (err)
		goto error;

592 593
	if (attrs[XFRMA_SEC_CTX] &&
	    security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
594 595
		goto error;

596 597 598 599
	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
					       attrs[XFRMA_REPLAY_ESN_VAL])))
		goto error;

Linus Torvalds's avatar
Linus Torvalds committed
600
	x->km.seq = p->seq;
601
	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
602
	/* sysctl_xfrm_aevent_etime is in 100ms units */
603
	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
604

605 606
	if ((err = xfrm_init_replay(x)))
		goto error;
607

608
	/* override default values from above */
609
	xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds's avatar
Linus Torvalds committed
610 611 612 613 614 615 616 617 618 619 620

	return x;

error:
	x->km.state = XFRM_STATE_DEAD;
	xfrm_state_put(x);
error_no_put:
	*errp = err;
	return NULL;
}

621
static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
622
		struct nlattr **attrs)
Linus Torvalds's avatar
Linus Torvalds committed
623
{
624
	struct net *net = sock_net(skb->sk);
625
	struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds's avatar
Linus Torvalds committed
626 627
	struct xfrm_state *x;
	int err;
628
	struct km_event c;
Linus Torvalds's avatar
Linus Torvalds committed
629

630
	err = verify_newsa_info(p, attrs);
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633
	if (err)
		return err;

634
	x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds's avatar
Linus Torvalds committed
635 636 637
	if (!x)
		return err;

638
	xfrm_state_hold(x);
Linus Torvalds's avatar
Linus Torvalds committed
639 640 641 642 643
	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
		err = xfrm_state_add(x);
	else
		err = xfrm_state_update(x);

644
	xfrm_audit_state_add(x, err ? 0 : 1, true);
645

Linus Torvalds's avatar
Linus Torvalds committed
646 647
	if (err < 0) {
		x->km.state = XFRM_STATE_DEAD;
648
		__xfrm_state_put(x);
649
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
650 651
	}

652
	c.seq = nlh->nlmsg_seq;
653
	c.portid = nlh->nlmsg_pid;
654
	c.event = nlh->nlmsg_type;
655 656

	km_state_notify(x, &c);
657
out:
658
	xfrm_state_put(x);
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661
	return err;
}

662 663
static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
						 struct xfrm_usersa_id *p,
664
						 struct nlattr **attrs,
665 666 667
						 int *errp)
{
	struct xfrm_state *x = NULL;
668
	struct xfrm_mark m;
669
	int err;
670
	u32 mark = xfrm_mark_get(attrs, &m);
671 672 673

	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
		err = -ESRCH;
674
		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
675 676 677
	} else {
		xfrm_address_t *saddr = NULL;

678
		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
679 680 681 682 683
		if (!saddr) {
			err = -EINVAL;
			goto out;
		}

684
		err = -ESRCH;
685 686
		x = xfrm_state_lookup_byaddr(net, mark,
					     &p->daddr, saddr,
687
					     p->proto, p->family);
688 689 690 691 692 693 694 695
	}

 out:
	if (!x && errp)
		*errp = err;
	return x;
}

696
static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
697
		struct nlattr **attrs)
Linus Torvalds's avatar
Linus Torvalds committed
698
{
699
	struct net *net = sock_net(skb->sk);
Linus Torvalds's avatar
Linus Torvalds committed
700
	struct xfrm_state *x;
701
	int err = -ESRCH;
702
	struct km_event c;
703
	struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds's avatar
Linus Torvalds committed
704

705
	x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds's avatar
Linus Torvalds committed
706
	if (x == NULL)
707
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
708

709
	if ((err = security_xfrm_state_delete(x)) != 0)
710 711
		goto out;

Linus Torvalds's avatar
Linus Torvalds committed
712
	if (xfrm_state_kern(x)) {
713 714
		err = -EPERM;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
715 716
	}

717
	err = xfrm_state_delete(x);
718

719 720
	if (err < 0)
		goto out;
721 722

	c.seq = nlh->nlmsg_seq;
723
	c.portid = nlh->nlmsg_pid;
724
	c.event = nlh->nlmsg_type;
725
	km_state_notify(x, &c);
Linus Torvalds's avatar
Linus Torvalds committed
726

727
out:
728
	xfrm_audit_state_delete(x, err ? 0 : 1, true);
729
	xfrm_state_put(x);
730
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
731 732 733 734
}

static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
{
735
	memset(p, 0, sizeof(*p));
Linus Torvalds's avatar
Linus Torvalds committed
736 737 738 739
	memcpy(&p->id, &x->id, sizeof(p->id));
	memcpy(&p->sel, &x->sel, sizeof(p->sel));
	memcpy(&p->lft, &x->lft, sizeof(p->lft));
	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
740 741 742
	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
	put_unaligned(x->stats.replay, &p->stats.replay);
	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
743
	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	p->mode = x->props.mode;
	p->replay_window = x->props.replay_window;
	p->reqid = x->props.reqid;
	p->family = x->props.family;
	p->flags = x->props.flags;
	p->seq = x->km.seq;
}

struct xfrm_dump_info {
	struct sk_buff *in_skb;
	struct sk_buff *out_skb;
	u32 nlmsg_seq;
	u16 nlmsg_flags;
};

759 760 761 762
static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
{
	struct xfrm_user_sec_ctx *uctx;
	struct nlattr *attr;
763
	int ctx_size = sizeof(*uctx) + s->ctx_len;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
	if (attr == NULL)
		return -EMSGSIZE;

	uctx = nla_data(attr);
	uctx->exttype = XFRMA_SEC_CTX;
	uctx->len = ctx_size;
	uctx->ctx_doi = s->ctx_doi;
	uctx->ctx_alg = s->ctx_alg;
	uctx->ctx_len = s->ctx_len;
	memcpy(uctx + 1, s->ctx_str, s->ctx_len);

	return 0;
}

780 781 782 783 784 785 786 787 788 789 790
static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
{
	struct xfrm_algo *algo;
	struct nlattr *nla;

	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
	if (!nla)
		return -EMSGSIZE;

	algo = nla_data(nla);
791
	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
792 793 794 795 796 797
	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
	algo->alg_key_len = auth->alg_key_len;

	return 0;
}

798 799 800 801
/* Don't change this without updating xfrm_sa_len! */
static int copy_to_user_state_extra(struct xfrm_state *x,
				    struct xfrm_usersa_info *p,
				    struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
802
{
803
	int ret = 0;
804

805
	copy_to_user_state(x, p);
806

807 808 809 810 811 812 813
	if (x->props.extra_flags) {
		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
				  x->props.extra_flags);
		if (ret)
			goto out;
	}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	if (x->coaddr) {
		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
		if (ret)
			goto out;
	}
	if (x->lastused) {
		ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
		if (ret)
			goto out;
	}
	if (x->aead) {
		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
		if (ret)
			goto out;
	}
	if (x->aalg) {
		ret = copy_to_user_auth(x->aalg, skb);
		if (!ret)
			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
				      xfrm_alg_auth_len(x->aalg), x->aalg);
		if (ret)
			goto out;
	}
	if (x->ealg) {
		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
		if (ret)
			goto out;
	}
	if (x->calg) {
		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
		if (ret)
			goto out;
	}
	if (x->encap) {
		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
		if (ret)
			goto out;
	}
	if (x->tfcpad) {
		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
		if (ret)
			goto out;
	}
	ret = xfrm_mark_put(skb, &x->mark);
	if (ret)
		goto out;
860
	if (x->replay_esn)
861 862 863
		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
			      xfrm_replay_state_esn_len(x->replay_esn),
			      x->replay_esn);
864 865 866 867 868
	else
		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
			      &x->replay);
	if (ret)
		goto out;
869 870 871 872
	if (x->security)
		ret = copy_sec_ctx(x->security, skb);
out:
	return ret;
873 874 875 876 877 878 879 880 881 882 883
}

static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
{
	struct xfrm_dump_info *sp = ptr;
	struct sk_buff *in_skb = sp->in_skb;
	struct sk_buff *skb = sp->out_skb;
	struct xfrm_usersa_info *p;
	struct nlmsghdr *nlh;
	int err;

884
	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
885 886 887 888 889 890 891
			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
	if (nlh == NULL)
		return -EMSGSIZE;

	p = nlmsg_data(nlh);

	err = copy_to_user_state_extra(x, p, skb);
892 893 894 895
	if (err) {
		nlmsg_cancel(skb, nlh);
		return err;
	}
896
	nlmsg_end(skb, nlh);
Linus Torvalds's avatar
Linus Torvalds committed
897 898 899
	return 0;
}

900 901 902
static int xfrm_dump_sa_done(struct netlink_callback *cb)
{
	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
903 904 905 906
	struct sock *sk = cb->skb->sk;
	struct net *net = sock_net(sk);

	xfrm_state_walk_done(walk, net);
907 908 909
	return 0;
}

910
static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds's avatar
Linus Torvalds committed
911 912
static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
{
913
	struct net *net = sock_net(skb->sk);
914
	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds's avatar
Linus Torvalds committed
915 916
	struct xfrm_dump_info info;

917 918 919
	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
		     sizeof(cb->args) - sizeof(cb->args[0]));

Linus Torvalds's avatar
Linus Torvalds committed
920 921 922 923
	info.in_skb = cb->skb;
	info.out_skb = skb;
	info.nlmsg_seq = cb->nlh->nlmsg_seq;
	info.nlmsg_flags = NLM_F_MULTI;
924 925

	if (!cb->args[0]) {
926
		struct nlattr *attrs[XFRMA_MAX+1];
927
		struct xfrm_address_filter *filter = NULL;
928 929 930
		u8 proto = 0;
		int err;

931
		cb->args[0] = 1;
932 933 934 935 936 937

		err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
				  xfrma_policy);
		if (err < 0)
			return err;

938
		if (attrs[XFRMA_ADDRESS_FILTER]) {
939 940
			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
					 sizeof(*filter), GFP_KERNEL);
941 942 943 944 945 946 947 948
			if (filter == NULL)
				return -ENOMEM;
		}

		if (attrs[XFRMA_PROTO])
			proto = nla_get_u8(attrs[XFRMA_PROTO]);

		xfrm_state_walk_init(walk, proto, filter);
949 950
	}

951
	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956 957 958 959 960

	return skb->len;
}

static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
					  struct xfrm_state *x, u32 seq)
{
	struct xfrm_dump_info info;
	struct sk_buff *skb;
961
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
962

963
	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
964 965 966 967 968 969 970 971
	if (!skb)
		return ERR_PTR(-ENOMEM);

	info.in_skb = in_skb;
	info.out_skb = skb;
	info.nlmsg_seq = seq;
	info.nlmsg_flags = 0;

972 973
	err = dump_one_state(x, 0, &info);
	if (err) {
Linus Torvalds's avatar
Linus Torvalds committed
974
		kfree_skb(skb);
975
		return ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
976 977 978 979 980
	}

	return skb;
}

981 982 983 984 985 986 987 988
/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
 * Must be called with RCU read lock.
 */
static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
				       u32 pid, unsigned int group)
{
	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);

989 990 991 992 993 994
	if (!nlsk) {
		kfree_skb(skb);
		return -EPIPE;
	}

	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
995 996
}

997 998 999 1000
static inline size_t xfrm_spdinfo_msgsize(void)
{
	return NLMSG_ALIGN(4)
	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1001 1002 1003
	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1004 1005
}

1006
static int build_spdinfo(struct sk_buff *skb, struct net *net,
1007
			 u32 portid, u32 seq, u32 flags)
1008
{
1009 1010 1011
	struct xfrmk_spdinfo si;
	struct xfrmu_spdinfo spc;
	struct xfrmu_spdhinfo sph;
1012
	struct xfrmu_spdhthresh spt4, spt6;
1013
	struct nlmsghdr *nlh;
1014
	int err;
1015
	u32 *f;
1016
	unsigned lseq;
1017

1018
	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1019
	if (nlh == NULL) /* shouldn't really happen ... */
1020 1021 1022 1023
		return -EMSGSIZE;

	f = nlmsg_data(nlh);
	*f = flags;
1024
	xfrm_spd_getinfo(net, &si);
1025 1026 1027 1028 1029 1030 1031 1032 1033
	spc.incnt = si.incnt;
	spc.outcnt = si.outcnt;
	spc.fwdcnt = si.fwdcnt;
	spc.inscnt = si.inscnt;
	spc.outscnt = si.outscnt;
	spc.fwdscnt = si.fwdscnt;
	sph.spdhcnt = si.spdhcnt;
	sph.spdhmcnt = si.spdhmcnt;

1034 1035 1036 1037 1038 1039 1040 1041 1042
	do {
		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);

		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));

1043 1044 1045
	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
	if (!err)
		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1046 1047 1048 1049
	if (!err)
		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
	if (!err)
		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1050 1051 1052 1053
	if (err) {
		nlmsg_cancel(skb, nlh);
		return err;
	}
1054

1055 1056
	nlmsg_end(skb, nlh);
	return 0;
1057 1058
}

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
			    struct nlattr **attrs)
{
	struct net *net = sock_net(skb->sk);
	struct xfrmu_spdhthresh *thresh4 = NULL;
	struct xfrmu_spdhthresh *thresh6 = NULL;

	/* selector prefixlen thresholds to hash policies */
	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];

		if (nla_len(rta) < sizeof(*thresh4))
			return -EINVAL;
		thresh4 = nla_data(rta);
		if (thresh4->lbits > 32 || thresh4->rbits > 32)
			return -EINVAL;
	}
	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];

		if (nla_len(rta) < sizeof(*thresh6))
			return -EINVAL;
		thresh6 = nla_data(rta);
		if (thresh6->lbits > 128 || thresh6->rbits > 128)
			return -EINVAL;
	}

	if (thresh4 || thresh6) {
		write_seqlock(&net->xfrm.policy_hthresh.lock);
		if (thresh4) {
			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
		}
		if (thresh6) {
			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
		}
		write_sequnlock(&net->xfrm.policy_hthresh.lock);

		xfrm_policy_hash_rebuild(net);
	}

	return 0;
}

1104
static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1105
		struct nlattr **attrs)
1106
{
1107
	struct net *net = sock_net(skb->sk);
1108
	struct sk_buff *r_skb;
1109
	u32 *flags = nlmsg_data(nlh);
1110
	u32 sportid = NETLINK_CB(skb).portid;
1111 1112
	u32 seq = nlh->nlmsg_seq;

1113
	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1114 1115 1116
	if (r_skb == NULL)
		return -ENOMEM;

1117
	if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1118 1119
		BUG();

1120
	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1121 1122
}

1123 1124 1125 1126 1127 1128 1129
static inline size_t xfrm_sadinfo_msgsize(void)
{
	return NLMSG_ALIGN(4)
	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
	       + nla_total_size(4); /* XFRMA_SAD_CNT */
}

1130
static int build_sadinfo(struct sk_buff *skb, struct net *net,
1131
			 u32 portid, u32 seq, u32 flags)
1132
{
1133 1134
	struct xfrmk_sadinfo si;
	struct xfrmu_sadhinfo sh;
1135
	struct nlmsghdr *nlh;
1136
	int err;
1137 1138
	u32 *f;

1139
	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1140
	if (nlh == NULL) /* shouldn't really happen ... */
1141 1142 1143 1144
		return -EMSGSIZE;

	f = nlmsg_data(nlh);
	*f = flags;
1145
	xfrm_sad_getinfo(net, &si);
1146

1147 1148 1149
	sh.sadhmcnt = si.sadhmcnt;
	sh.sadhcnt = si.sadhcnt;

1150 1151 1152 1153 1154 1155 1156
	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
	if (!err)
		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
	if (err) {
		nlmsg_cancel(skb, nlh);
		return err;
	}
1157

1158 1159
	nlmsg_end(skb, nlh);
	return 0;
1160 1161 1162
}

static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1163
		struct nlattr **attrs)
1164
{
1165
	struct net *net = sock_net(skb->sk);
1166
	struct sk_buff *r_skb;
1167
	u32 *flags = nlmsg_data(nlh);
1168
	u32 sportid = NETLINK_CB(skb).portid;
1169 1170
	u32 seq = nlh->nlmsg_seq;

1171
	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1172 1173 1174
	if (r_skb == NULL)
		return -ENOMEM;

1175
	if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1176 1177
		BUG();

1178
	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1179 1180
}

1181
static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1182
		struct nlattr **attrs)
Linus Torvalds's avatar
Linus Torvalds committed
1183
{
1184
	struct net *net = sock_net(skb->sk);
1185
	struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds's avatar
Linus Torvalds committed
1186 1187
	struct xfrm_state *x;
	struct sk_buff *resp_skb;
1188
	int err = -ESRCH;
Linus Torvalds's avatar
Linus Torvalds committed
1189

1190
	x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds's avatar
Linus Torvalds committed
1191 1192 1193 1194 1195 1196 1197
	if (x == NULL)
		goto out_noput;

	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
	if (IS_ERR(resp_skb)) {
		err = PTR_ERR(resp_skb);
	} else {
1198
		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds's avatar
Linus Torvalds committed
1199 1200 1201 1202 1203 1204
	}
	xfrm_state_put(x);
out_noput:
	return err;
}

1205
static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1206
		struct nlattr **attrs)
Linus Torvalds's avatar
Linus Torvalds committed
1207
{
1208
	struct net *net = sock_net(skb->sk);
Linus Torvalds's avatar
Linus Torvalds committed
1209 1210 1211 1212 1213 1214
	struct xfrm_state *x;
	struct xfrm_userspi_info *p;
	struct sk_buff *resp_skb;
	xfrm_address_t *daddr;
	int family;
	int err;
1215 1216
	u32 mark;
	struct xfrm_mark m;
Linus Torvalds's avatar
Linus Torvalds committed
1217

1218
	p = nlmsg_data(nlh);
1219
	err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds's avatar
Linus Torvalds committed
1220 1221 1222 1223 1224 1225 1226
	if (err)
		goto out_noput;

	family = p->info.family;
	daddr = &p->info.id.daddr;

	x = NULL;
1227 1228

	mark = xfrm_mark_get(attrs, &m);
Linus Torvalds's avatar
Linus Torvalds committed
1229
	if (p->info.seq) {
1230
		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1231
		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds's avatar
Linus Torvalds committed
1232 1233 1234 1235 1236 1237
			xfrm_state_put(x);
			x = NULL;
		}
	}

	if (!x)
1238
		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds's avatar
Linus Torvalds committed
1239 1240 1241 1242 1243 1244 1245
				  p->info.id.proto, daddr,
				  &p->info.saddr, 1,
				  family);
	err = -ENOENT;
	if (x == NULL)
		goto out_noput;

1246 1247 1248
	err = xfrm_alloc_spi(x, p->min, p->max);
	if (err)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1249

1250
	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds's avatar
Linus Torvalds committed
1251 1252 1253 1254 1255
	if (IS_ERR(resp_skb)) {
		err = PTR_ERR(resp_skb);
		goto out;
	}

1256
	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds's avatar
Linus Torvalds committed
1257 1258 1259 1260 1261 1262 1263

out:
	xfrm_state_put(x);
out_noput:
	return err;
}

1264
static int verify_policy_dir(u8 dir)
Linus Torvalds's avatar
Linus Torvalds committed
1265 1266 1267 1268 1269 1270 1271 1272 1273
{
	switch (dir) {
	case XFRM_POLICY_IN:
	case XFRM_POLICY_OUT:
	case XFRM_POLICY_FWD:
		break;

	default:
		return -EINVAL;
1274
	}
Linus Torvalds's avatar
Linus Torvalds committed
1275 1276 1277 1278

	return 0;
}

1279
static int verify_policy_type(u8 type)
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
{
	switch (type) {
	case XFRM_POLICY_TYPE_MAIN:
#ifdef CONFIG_XFRM_SUB_POLICY
	case XFRM_POLICY_TYPE_SUB:
#endif
		break;

	default:
		return -EINVAL;
1290
	}
1291 1292 1293 1294

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1295 1296
static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
{
1297 1298
	int ret;

Linus Torvalds's avatar
Linus Torvalds committed
1299 1300 1301 1302 1303 1304 1305 1306 1307
	switch (p->share) {
	case XFRM_SHARE_ANY:
	case XFRM_SHARE_SESSION:
	case XFRM_SHARE_USER:
	case XFRM_SHARE_UNIQUE:
		break;

	default:
		return -EINVAL;
1308
	}
Linus Torvalds's avatar
Linus Torvalds committed
1309 1310 1311 1312 1313 1314 1315 1316

	switch (p->action) {
	case XFRM_POLICY_ALLOW:
	case XFRM_POLICY_BLOCK:
		break;

	default:
		return -EINVAL;
1317
	}
Linus Torvalds's avatar
Linus Torvalds committed
1318 1319 1320

	switch (p->sel.family) {
	case AF_INET:
1321 1322 1323
		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
			return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
1324 1325 1326
		break;

	case AF_INET6:
1327
#if IS_ENABLED(CONFIG_IPV6)
1328 1329 1330
		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
			return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
1331 1332 1333 1334 1335 1336 1337
		break;
#else
		return  -EAFNOSUPPORT;
#endif

	default:
		return -EINVAL;
1338
	}
Linus Torvalds's avatar
Linus Torvalds committed
1339

1340 1341 1342 1343 1344 1345 1346
	ret = verify_policy_dir(p->dir);
	if (ret)
		return ret;
	if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
		return -EINVAL;

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1347 1348
}

1349
static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1350
{
1351
	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1352 1353 1354 1355 1356
	struct xfrm_user_sec_ctx *uctx;

	if (!rt)
		return 0;

1357
	uctx = nla_data(rt);
1358
	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1359 1360
}

Linus Torvalds's avatar
Linus Torvalds committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
			   int nr)
{
	int i;

	xp->xfrm_nr = nr;
	for (i = 0; i < nr; i++, ut++) {
		struct xfrm_tmpl *t = &xp->xfrm_vec[i];

		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
		memcpy(&t->saddr, &ut->saddr,
		       sizeof(xfrm_address_t));
		t-