xref: /openbmc/linux/net/xfrm/xfrm_user.c (revision 2208f39c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* xfrm_user.c: User interface to configure xfrm engine.
3  *
4  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5  *
6  * Changes:
7  *	Mitsuru KANDA @USAGI
8  * 	Kazunori MIYAZAWA @USAGI
9  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
10  * 		IPv6 support
11  *
12  */
13 
14 #include <linux/crypto.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/socket.h>
20 #include <linux/string.h>
21 #include <linux/net.h>
22 #include <linux/skbuff.h>
23 #include <linux/pfkeyv2.h>
24 #include <linux/ipsec.h>
25 #include <linux/init.h>
26 #include <linux/security.h>
27 #include <net/sock.h>
28 #include <net/xfrm.h>
29 #include <net/netlink.h>
30 #include <net/ah.h>
31 #include <linux/uaccess.h>
32 #if IS_ENABLED(CONFIG_IPV6)
33 #include <linux/in6.h>
34 #endif
35 #include <asm/unaligned.h>
36 
37 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
38 {
39 	struct nlattr *rt = attrs[type];
40 	struct xfrm_algo *algp;
41 
42 	if (!rt)
43 		return 0;
44 
45 	algp = nla_data(rt);
46 	if (nla_len(rt) < (int)xfrm_alg_len(algp))
47 		return -EINVAL;
48 
49 	switch (type) {
50 	case XFRMA_ALG_AUTH:
51 	case XFRMA_ALG_CRYPT:
52 	case XFRMA_ALG_COMP:
53 		break;
54 
55 	default:
56 		return -EINVAL;
57 	}
58 
59 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
60 	return 0;
61 }
62 
63 static int verify_auth_trunc(struct nlattr **attrs)
64 {
65 	struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
66 	struct xfrm_algo_auth *algp;
67 
68 	if (!rt)
69 		return 0;
70 
71 	algp = nla_data(rt);
72 	if (nla_len(rt) < (int)xfrm_alg_auth_len(algp))
73 		return -EINVAL;
74 
75 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
76 	return 0;
77 }
78 
79 static int verify_aead(struct nlattr **attrs)
80 {
81 	struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
82 	struct xfrm_algo_aead *algp;
83 
84 	if (!rt)
85 		return 0;
86 
87 	algp = nla_data(rt);
88 	if (nla_len(rt) < (int)aead_len(algp))
89 		return -EINVAL;
90 
91 	algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
92 	return 0;
93 }
94 
95 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
96 			   xfrm_address_t **addrp)
97 {
98 	struct nlattr *rt = attrs[type];
99 
100 	if (rt && addrp)
101 		*addrp = nla_data(rt);
102 }
103 
104 static inline int verify_sec_ctx_len(struct nlattr **attrs)
105 {
106 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
107 	struct xfrm_user_sec_ctx *uctx;
108 
109 	if (!rt)
110 		return 0;
111 
112 	uctx = nla_data(rt);
113 	if (uctx->len > nla_len(rt) ||
114 	    uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
115 		return -EINVAL;
116 
117 	return 0;
118 }
119 
120 static inline int verify_replay(struct xfrm_usersa_info *p,
121 				struct nlattr **attrs)
122 {
123 	struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
124 	struct xfrm_replay_state_esn *rs;
125 
126 	if (!rt)
127 		return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
128 
129 	rs = nla_data(rt);
130 
131 	if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
132 		return -EINVAL;
133 
134 	if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
135 	    nla_len(rt) != sizeof(*rs))
136 		return -EINVAL;
137 
138 	/* As only ESP and AH support ESN feature. */
139 	if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
140 		return -EINVAL;
141 
142 	if (p->replay_window != 0)
143 		return -EINVAL;
144 
145 	return 0;
146 }
147 
148 static int verify_newsa_info(struct xfrm_usersa_info *p,
149 			     struct nlattr **attrs)
150 {
151 	int err;
152 
153 	err = -EINVAL;
154 	switch (p->family) {
155 	case AF_INET:
156 		break;
157 
158 	case AF_INET6:
159 #if IS_ENABLED(CONFIG_IPV6)
160 		break;
161 #else
162 		err = -EAFNOSUPPORT;
163 		goto out;
164 #endif
165 
166 	default:
167 		goto out;
168 	}
169 
170 	switch (p->sel.family) {
171 	case AF_UNSPEC:
172 		break;
173 
174 	case AF_INET:
175 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
176 			goto out;
177 
178 		break;
179 
180 	case AF_INET6:
181 #if IS_ENABLED(CONFIG_IPV6)
182 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
183 			goto out;
184 
185 		break;
186 #else
187 		err = -EAFNOSUPPORT;
188 		goto out;
189 #endif
190 
191 	default:
192 		goto out;
193 	}
194 
195 	err = -EINVAL;
196 	switch (p->id.proto) {
197 	case IPPROTO_AH:
198 		if ((!attrs[XFRMA_ALG_AUTH]	&&
199 		     !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
200 		    attrs[XFRMA_ALG_AEAD]	||
201 		    attrs[XFRMA_ALG_CRYPT]	||
202 		    attrs[XFRMA_ALG_COMP]	||
203 		    attrs[XFRMA_TFCPAD])
204 			goto out;
205 		break;
206 
207 	case IPPROTO_ESP:
208 		if (attrs[XFRMA_ALG_COMP])
209 			goto out;
210 		if (!attrs[XFRMA_ALG_AUTH] &&
211 		    !attrs[XFRMA_ALG_AUTH_TRUNC] &&
212 		    !attrs[XFRMA_ALG_CRYPT] &&
213 		    !attrs[XFRMA_ALG_AEAD])
214 			goto out;
215 		if ((attrs[XFRMA_ALG_AUTH] ||
216 		     attrs[XFRMA_ALG_AUTH_TRUNC] ||
217 		     attrs[XFRMA_ALG_CRYPT]) &&
218 		    attrs[XFRMA_ALG_AEAD])
219 			goto out;
220 		if (attrs[XFRMA_TFCPAD] &&
221 		    p->mode != XFRM_MODE_TUNNEL)
222 			goto out;
223 		break;
224 
225 	case IPPROTO_COMP:
226 		if (!attrs[XFRMA_ALG_COMP]	||
227 		    attrs[XFRMA_ALG_AEAD]	||
228 		    attrs[XFRMA_ALG_AUTH]	||
229 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
230 		    attrs[XFRMA_ALG_CRYPT]	||
231 		    attrs[XFRMA_TFCPAD]		||
232 		    (ntohl(p->id.spi) >= 0x10000))
233 			goto out;
234 		break;
235 
236 #if IS_ENABLED(CONFIG_IPV6)
237 	case IPPROTO_DSTOPTS:
238 	case IPPROTO_ROUTING:
239 		if (attrs[XFRMA_ALG_COMP]	||
240 		    attrs[XFRMA_ALG_AUTH]	||
241 		    attrs[XFRMA_ALG_AUTH_TRUNC]	||
242 		    attrs[XFRMA_ALG_AEAD]	||
243 		    attrs[XFRMA_ALG_CRYPT]	||
244 		    attrs[XFRMA_ENCAP]		||
245 		    attrs[XFRMA_SEC_CTX]	||
246 		    attrs[XFRMA_TFCPAD]		||
247 		    !attrs[XFRMA_COADDR])
248 			goto out;
249 		break;
250 #endif
251 
252 	default:
253 		goto out;
254 	}
255 
256 	if ((err = verify_aead(attrs)))
257 		goto out;
258 	if ((err = verify_auth_trunc(attrs)))
259 		goto out;
260 	if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
261 		goto out;
262 	if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
263 		goto out;
264 	if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
265 		goto out;
266 	if ((err = verify_sec_ctx_len(attrs)))
267 		goto out;
268 	if ((err = verify_replay(p, attrs)))
269 		goto out;
270 
271 	err = -EINVAL;
272 	switch (p->mode) {
273 	case XFRM_MODE_TRANSPORT:
274 	case XFRM_MODE_TUNNEL:
275 	case XFRM_MODE_ROUTEOPTIMIZATION:
276 	case XFRM_MODE_BEET:
277 		break;
278 
279 	default:
280 		goto out;
281 	}
282 
283 	err = 0;
284 
285 out:
286 	return err;
287 }
288 
289 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
290 			   struct xfrm_algo_desc *(*get_byname)(const char *, int),
291 			   struct nlattr *rta)
292 {
293 	struct xfrm_algo *p, *ualg;
294 	struct xfrm_algo_desc *algo;
295 
296 	if (!rta)
297 		return 0;
298 
299 	ualg = nla_data(rta);
300 
301 	algo = get_byname(ualg->alg_name, 1);
302 	if (!algo)
303 		return -ENOSYS;
304 	*props = algo->desc.sadb_alg_id;
305 
306 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
307 	if (!p)
308 		return -ENOMEM;
309 
310 	strcpy(p->alg_name, algo->name);
311 	*algpp = p;
312 	return 0;
313 }
314 
315 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
316 {
317 	struct xfrm_algo *p, *ualg;
318 	struct xfrm_algo_desc *algo;
319 
320 	if (!rta)
321 		return 0;
322 
323 	ualg = nla_data(rta);
324 
325 	algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
326 	if (!algo)
327 		return -ENOSYS;
328 	x->props.ealgo = algo->desc.sadb_alg_id;
329 
330 	p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
331 	if (!p)
332 		return -ENOMEM;
333 
334 	strcpy(p->alg_name, algo->name);
335 	x->ealg = p;
336 	x->geniv = algo->uinfo.encr.geniv;
337 	return 0;
338 }
339 
340 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
341 		       struct nlattr *rta)
342 {
343 	struct xfrm_algo *ualg;
344 	struct xfrm_algo_auth *p;
345 	struct xfrm_algo_desc *algo;
346 
347 	if (!rta)
348 		return 0;
349 
350 	ualg = nla_data(rta);
351 
352 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
353 	if (!algo)
354 		return -ENOSYS;
355 	*props = algo->desc.sadb_alg_id;
356 
357 	p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
358 	if (!p)
359 		return -ENOMEM;
360 
361 	strcpy(p->alg_name, algo->name);
362 	p->alg_key_len = ualg->alg_key_len;
363 	p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
364 	memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
365 
366 	*algpp = p;
367 	return 0;
368 }
369 
370 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
371 			     struct nlattr *rta)
372 {
373 	struct xfrm_algo_auth *p, *ualg;
374 	struct xfrm_algo_desc *algo;
375 
376 	if (!rta)
377 		return 0;
378 
379 	ualg = nla_data(rta);
380 
381 	algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
382 	if (!algo)
383 		return -ENOSYS;
384 	if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
385 		return -EINVAL;
386 	*props = algo->desc.sadb_alg_id;
387 
388 	p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
389 	if (!p)
390 		return -ENOMEM;
391 
392 	strcpy(p->alg_name, algo->name);
393 	if (!p->alg_trunc_len)
394 		p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
395 
396 	*algpp = p;
397 	return 0;
398 }
399 
400 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
401 {
402 	struct xfrm_algo_aead *p, *ualg;
403 	struct xfrm_algo_desc *algo;
404 
405 	if (!rta)
406 		return 0;
407 
408 	ualg = nla_data(rta);
409 
410 	algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
411 	if (!algo)
412 		return -ENOSYS;
413 	x->props.ealgo = algo->desc.sadb_alg_id;
414 
415 	p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
416 	if (!p)
417 		return -ENOMEM;
418 
419 	strcpy(p->alg_name, algo->name);
420 	x->aead = p;
421 	x->geniv = algo->uinfo.aead.geniv;
422 	return 0;
423 }
424 
425 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
426 					 struct nlattr *rp)
427 {
428 	struct xfrm_replay_state_esn *up;
429 	unsigned int ulen;
430 
431 	if (!replay_esn || !rp)
432 		return 0;
433 
434 	up = nla_data(rp);
435 	ulen = xfrm_replay_state_esn_len(up);
436 
437 	/* Check the overall length and the internal bitmap length to avoid
438 	 * potential overflow. */
439 	if (nla_len(rp) < (int)ulen ||
440 	    xfrm_replay_state_esn_len(replay_esn) != ulen ||
441 	    replay_esn->bmp_len != up->bmp_len)
442 		return -EINVAL;
443 
444 	if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
445 		return -EINVAL;
446 
447 	return 0;
448 }
449 
450 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
451 				       struct xfrm_replay_state_esn **preplay_esn,
452 				       struct nlattr *rta)
453 {
454 	struct xfrm_replay_state_esn *p, *pp, *up;
455 	unsigned int klen, ulen;
456 
457 	if (!rta)
458 		return 0;
459 
460 	up = nla_data(rta);
461 	klen = xfrm_replay_state_esn_len(up);
462 	ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
463 
464 	p = kzalloc(klen, GFP_KERNEL);
465 	if (!p)
466 		return -ENOMEM;
467 
468 	pp = kzalloc(klen, GFP_KERNEL);
469 	if (!pp) {
470 		kfree(p);
471 		return -ENOMEM;
472 	}
473 
474 	memcpy(p, up, ulen);
475 	memcpy(pp, up, ulen);
476 
477 	*replay_esn = p;
478 	*preplay_esn = pp;
479 
480 	return 0;
481 }
482 
483 static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
484 {
485 	unsigned int len = 0;
486 
487 	if (xfrm_ctx) {
488 		len += sizeof(struct xfrm_user_sec_ctx);
489 		len += xfrm_ctx->ctx_len;
490 	}
491 	return len;
492 }
493 
494 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
495 {
496 	memcpy(&x->id, &p->id, sizeof(x->id));
497 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
498 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
499 	x->props.mode = p->mode;
500 	x->props.replay_window = min_t(unsigned int, p->replay_window,
501 					sizeof(x->replay.bitmap) * 8);
502 	x->props.reqid = p->reqid;
503 	x->props.family = p->family;
504 	memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
505 	x->props.flags = p->flags;
506 
507 	if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
508 		x->sel.family = p->family;
509 }
510 
511 /*
512  * someday when pfkey also has support, we could have the code
513  * somehow made shareable and move it to xfrm_state.c - JHS
514  *
515 */
516 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
517 				  int update_esn)
518 {
519 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
520 	struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
521 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
522 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
523 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
524 
525 	if (re) {
526 		struct xfrm_replay_state_esn *replay_esn;
527 		replay_esn = nla_data(re);
528 		memcpy(x->replay_esn, replay_esn,
529 		       xfrm_replay_state_esn_len(replay_esn));
530 		memcpy(x->preplay_esn, replay_esn,
531 		       xfrm_replay_state_esn_len(replay_esn));
532 	}
533 
534 	if (rp) {
535 		struct xfrm_replay_state *replay;
536 		replay = nla_data(rp);
537 		memcpy(&x->replay, replay, sizeof(*replay));
538 		memcpy(&x->preplay, replay, sizeof(*replay));
539 	}
540 
541 	if (lt) {
542 		struct xfrm_lifetime_cur *ltime;
543 		ltime = nla_data(lt);
544 		x->curlft.bytes = ltime->bytes;
545 		x->curlft.packets = ltime->packets;
546 		x->curlft.add_time = ltime->add_time;
547 		x->curlft.use_time = ltime->use_time;
548 	}
549 
550 	if (et)
551 		x->replay_maxage = nla_get_u32(et);
552 
553 	if (rt)
554 		x->replay_maxdiff = nla_get_u32(rt);
555 }
556 
557 static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
558 {
559 	if (attrs[XFRMA_SET_MARK]) {
560 		m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
561 		if (attrs[XFRMA_SET_MARK_MASK])
562 			m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
563 		else
564 			m->m = 0xffffffff;
565 	} else {
566 		m->v = m->m = 0;
567 	}
568 }
569 
570 static struct xfrm_state *xfrm_state_construct(struct net *net,
571 					       struct xfrm_usersa_info *p,
572 					       struct nlattr **attrs,
573 					       int *errp)
574 {
575 	struct xfrm_state *x = xfrm_state_alloc(net);
576 	int err = -ENOMEM;
577 
578 	if (!x)
579 		goto error_no_put;
580 
581 	copy_from_user_state(x, p);
582 
583 	if (attrs[XFRMA_SA_EXTRA_FLAGS])
584 		x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
585 
586 	if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
587 		goto error;
588 	if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
589 				     attrs[XFRMA_ALG_AUTH_TRUNC])))
590 		goto error;
591 	if (!x->props.aalgo) {
592 		if ((err = attach_auth(&x->aalg, &x->props.aalgo,
593 				       attrs[XFRMA_ALG_AUTH])))
594 			goto error;
595 	}
596 	if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
597 		goto error;
598 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
599 				   xfrm_calg_get_byname,
600 				   attrs[XFRMA_ALG_COMP])))
601 		goto error;
602 
603 	if (attrs[XFRMA_ENCAP]) {
604 		x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
605 				   sizeof(*x->encap), GFP_KERNEL);
606 		if (x->encap == NULL)
607 			goto error;
608 	}
609 
610 	if (attrs[XFRMA_TFCPAD])
611 		x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
612 
613 	if (attrs[XFRMA_COADDR]) {
614 		x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
615 				    sizeof(*x->coaddr), GFP_KERNEL);
616 		if (x->coaddr == NULL)
617 			goto error;
618 	}
619 
620 	xfrm_mark_get(attrs, &x->mark);
621 
622 	xfrm_smark_init(attrs, &x->props.smark);
623 
624 	if (attrs[XFRMA_IF_ID])
625 		x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
626 
627 	err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
628 	if (err)
629 		goto error;
630 
631 	if (attrs[XFRMA_SEC_CTX]) {
632 		err = security_xfrm_state_alloc(x,
633 						nla_data(attrs[XFRMA_SEC_CTX]));
634 		if (err)
635 			goto error;
636 	}
637 
638 	if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
639 					       attrs[XFRMA_REPLAY_ESN_VAL])))
640 		goto error;
641 
642 	x->km.seq = p->seq;
643 	x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
644 	/* sysctl_xfrm_aevent_etime is in 100ms units */
645 	x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
646 
647 	if ((err = xfrm_init_replay(x)))
648 		goto error;
649 
650 	/* override default values from above */
651 	xfrm_update_ae_params(x, attrs, 0);
652 
653 	/* configure the hardware if offload is requested */
654 	if (attrs[XFRMA_OFFLOAD_DEV]) {
655 		err = xfrm_dev_state_add(net, x,
656 					 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
657 		if (err)
658 			goto error;
659 	}
660 
661 	return x;
662 
663 error:
664 	x->km.state = XFRM_STATE_DEAD;
665 	xfrm_state_put(x);
666 error_no_put:
667 	*errp = err;
668 	return NULL;
669 }
670 
671 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
672 		struct nlattr **attrs)
673 {
674 	struct net *net = sock_net(skb->sk);
675 	struct xfrm_usersa_info *p = nlmsg_data(nlh);
676 	struct xfrm_state *x;
677 	int err;
678 	struct km_event c;
679 
680 	err = verify_newsa_info(p, attrs);
681 	if (err)
682 		return err;
683 
684 	x = xfrm_state_construct(net, p, attrs, &err);
685 	if (!x)
686 		return err;
687 
688 	xfrm_state_hold(x);
689 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
690 		err = xfrm_state_add(x);
691 	else
692 		err = xfrm_state_update(x);
693 
694 	xfrm_audit_state_add(x, err ? 0 : 1, true);
695 
696 	if (err < 0) {
697 		x->km.state = XFRM_STATE_DEAD;
698 		xfrm_dev_state_delete(x);
699 		__xfrm_state_put(x);
700 		goto out;
701 	}
702 
703 	if (x->km.state == XFRM_STATE_VOID)
704 		x->km.state = XFRM_STATE_VALID;
705 
706 	c.seq = nlh->nlmsg_seq;
707 	c.portid = nlh->nlmsg_pid;
708 	c.event = nlh->nlmsg_type;
709 
710 	km_state_notify(x, &c);
711 out:
712 	xfrm_state_put(x);
713 	return err;
714 }
715 
716 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
717 						 struct xfrm_usersa_id *p,
718 						 struct nlattr **attrs,
719 						 int *errp)
720 {
721 	struct xfrm_state *x = NULL;
722 	struct xfrm_mark m;
723 	int err;
724 	u32 mark = xfrm_mark_get(attrs, &m);
725 
726 	if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
727 		err = -ESRCH;
728 		x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
729 	} else {
730 		xfrm_address_t *saddr = NULL;
731 
732 		verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
733 		if (!saddr) {
734 			err = -EINVAL;
735 			goto out;
736 		}
737 
738 		err = -ESRCH;
739 		x = xfrm_state_lookup_byaddr(net, mark,
740 					     &p->daddr, saddr,
741 					     p->proto, p->family);
742 	}
743 
744  out:
745 	if (!x && errp)
746 		*errp = err;
747 	return x;
748 }
749 
750 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
751 		struct nlattr **attrs)
752 {
753 	struct net *net = sock_net(skb->sk);
754 	struct xfrm_state *x;
755 	int err = -ESRCH;
756 	struct km_event c;
757 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
758 
759 	x = xfrm_user_state_lookup(net, p, attrs, &err);
760 	if (x == NULL)
761 		return err;
762 
763 	if ((err = security_xfrm_state_delete(x)) != 0)
764 		goto out;
765 
766 	if (xfrm_state_kern(x)) {
767 		err = -EPERM;
768 		goto out;
769 	}
770 
771 	err = xfrm_state_delete(x);
772 
773 	if (err < 0)
774 		goto out;
775 
776 	c.seq = nlh->nlmsg_seq;
777 	c.portid = nlh->nlmsg_pid;
778 	c.event = nlh->nlmsg_type;
779 	km_state_notify(x, &c);
780 
781 out:
782 	xfrm_audit_state_delete(x, err ? 0 : 1, true);
783 	xfrm_state_put(x);
784 	return err;
785 }
786 
787 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
788 {
789 	memset(p, 0, sizeof(*p));
790 	memcpy(&p->id, &x->id, sizeof(p->id));
791 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
792 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
793 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
794 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
795 	put_unaligned(x->stats.replay, &p->stats.replay);
796 	put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
797 	memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
798 	p->mode = x->props.mode;
799 	p->replay_window = x->props.replay_window;
800 	p->reqid = x->props.reqid;
801 	p->family = x->props.family;
802 	p->flags = x->props.flags;
803 	p->seq = x->km.seq;
804 }
805 
806 struct xfrm_dump_info {
807 	struct sk_buff *in_skb;
808 	struct sk_buff *out_skb;
809 	u32 nlmsg_seq;
810 	u16 nlmsg_flags;
811 };
812 
813 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
814 {
815 	struct xfrm_user_sec_ctx *uctx;
816 	struct nlattr *attr;
817 	int ctx_size = sizeof(*uctx) + s->ctx_len;
818 
819 	attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
820 	if (attr == NULL)
821 		return -EMSGSIZE;
822 
823 	uctx = nla_data(attr);
824 	uctx->exttype = XFRMA_SEC_CTX;
825 	uctx->len = ctx_size;
826 	uctx->ctx_doi = s->ctx_doi;
827 	uctx->ctx_alg = s->ctx_alg;
828 	uctx->ctx_len = s->ctx_len;
829 	memcpy(uctx + 1, s->ctx_str, s->ctx_len);
830 
831 	return 0;
832 }
833 
834 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
835 {
836 	struct xfrm_user_offload *xuo;
837 	struct nlattr *attr;
838 
839 	attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
840 	if (attr == NULL)
841 		return -EMSGSIZE;
842 
843 	xuo = nla_data(attr);
844 	memset(xuo, 0, sizeof(*xuo));
845 	xuo->ifindex = xso->dev->ifindex;
846 	xuo->flags = xso->flags;
847 
848 	return 0;
849 }
850 
851 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
852 {
853 	struct xfrm_algo *algo;
854 	struct nlattr *nla;
855 
856 	nla = nla_reserve(skb, XFRMA_ALG_AUTH,
857 			  sizeof(*algo) + (auth->alg_key_len + 7) / 8);
858 	if (!nla)
859 		return -EMSGSIZE;
860 
861 	algo = nla_data(nla);
862 	strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
863 	memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
864 	algo->alg_key_len = auth->alg_key_len;
865 
866 	return 0;
867 }
868 
869 static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
870 {
871 	int ret = 0;
872 
873 	if (m->v | m->m) {
874 		ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
875 		if (!ret)
876 			ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
877 	}
878 	return ret;
879 }
880 
881 /* Don't change this without updating xfrm_sa_len! */
882 static int copy_to_user_state_extra(struct xfrm_state *x,
883 				    struct xfrm_usersa_info *p,
884 				    struct sk_buff *skb)
885 {
886 	int ret = 0;
887 
888 	copy_to_user_state(x, p);
889 
890 	if (x->props.extra_flags) {
891 		ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
892 				  x->props.extra_flags);
893 		if (ret)
894 			goto out;
895 	}
896 
897 	if (x->coaddr) {
898 		ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
899 		if (ret)
900 			goto out;
901 	}
902 	if (x->lastused) {
903 		ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
904 					XFRMA_PAD);
905 		if (ret)
906 			goto out;
907 	}
908 	if (x->aead) {
909 		ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
910 		if (ret)
911 			goto out;
912 	}
913 	if (x->aalg) {
914 		ret = copy_to_user_auth(x->aalg, skb);
915 		if (!ret)
916 			ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
917 				      xfrm_alg_auth_len(x->aalg), x->aalg);
918 		if (ret)
919 			goto out;
920 	}
921 	if (x->ealg) {
922 		ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
923 		if (ret)
924 			goto out;
925 	}
926 	if (x->calg) {
927 		ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
928 		if (ret)
929 			goto out;
930 	}
931 	if (x->encap) {
932 		ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
933 		if (ret)
934 			goto out;
935 	}
936 	if (x->tfcpad) {
937 		ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
938 		if (ret)
939 			goto out;
940 	}
941 	ret = xfrm_mark_put(skb, &x->mark);
942 	if (ret)
943 		goto out;
944 
945 	ret = xfrm_smark_put(skb, &x->props.smark);
946 	if (ret)
947 		goto out;
948 
949 	if (x->replay_esn)
950 		ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
951 			      xfrm_replay_state_esn_len(x->replay_esn),
952 			      x->replay_esn);
953 	else
954 		ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
955 			      &x->replay);
956 	if (ret)
957 		goto out;
958 	if(x->xso.dev)
959 		ret = copy_user_offload(&x->xso, skb);
960 	if (ret)
961 		goto out;
962 	if (x->if_id) {
963 		ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
964 		if (ret)
965 			goto out;
966 	}
967 	if (x->security)
968 		ret = copy_sec_ctx(x->security, skb);
969 out:
970 	return ret;
971 }
972 
973 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
974 {
975 	struct xfrm_dump_info *sp = ptr;
976 	struct sk_buff *in_skb = sp->in_skb;
977 	struct sk_buff *skb = sp->out_skb;
978 	struct xfrm_translator *xtr;
979 	struct xfrm_usersa_info *p;
980 	struct nlmsghdr *nlh;
981 	int err;
982 
983 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
984 			XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
985 	if (nlh == NULL)
986 		return -EMSGSIZE;
987 
988 	p = nlmsg_data(nlh);
989 
990 	err = copy_to_user_state_extra(x, p, skb);
991 	if (err) {
992 		nlmsg_cancel(skb, nlh);
993 		return err;
994 	}
995 	nlmsg_end(skb, nlh);
996 
997 	xtr = xfrm_get_translator();
998 	if (xtr) {
999 		err = xtr->alloc_compat(skb, nlh);
1000 
1001 		xfrm_put_translator(xtr);
1002 		if (err) {
1003 			nlmsg_cancel(skb, nlh);
1004 			return err;
1005 		}
1006 	}
1007 
1008 	return 0;
1009 }
1010 
1011 static int xfrm_dump_sa_done(struct netlink_callback *cb)
1012 {
1013 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1014 	struct sock *sk = cb->skb->sk;
1015 	struct net *net = sock_net(sk);
1016 
1017 	if (cb->args[0])
1018 		xfrm_state_walk_done(walk, net);
1019 	return 0;
1020 }
1021 
1022 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
1023 {
1024 	struct net *net = sock_net(skb->sk);
1025 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
1026 	struct xfrm_dump_info info;
1027 
1028 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
1029 		     sizeof(cb->args) - sizeof(cb->args[0]));
1030 
1031 	info.in_skb = cb->skb;
1032 	info.out_skb = skb;
1033 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
1034 	info.nlmsg_flags = NLM_F_MULTI;
1035 
1036 	if (!cb->args[0]) {
1037 		struct nlattr *attrs[XFRMA_MAX+1];
1038 		struct xfrm_address_filter *filter = NULL;
1039 		u8 proto = 0;
1040 		int err;
1041 
1042 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
1043 					     xfrma_policy, cb->extack);
1044 		if (err < 0)
1045 			return err;
1046 
1047 		if (attrs[XFRMA_ADDRESS_FILTER]) {
1048 			filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
1049 					 sizeof(*filter), GFP_KERNEL);
1050 			if (filter == NULL)
1051 				return -ENOMEM;
1052 		}
1053 
1054 		if (attrs[XFRMA_PROTO])
1055 			proto = nla_get_u8(attrs[XFRMA_PROTO]);
1056 
1057 		xfrm_state_walk_init(walk, proto, filter);
1058 		cb->args[0] = 1;
1059 	}
1060 
1061 	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
1062 
1063 	return skb->len;
1064 }
1065 
1066 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1067 					  struct xfrm_state *x, u32 seq)
1068 {
1069 	struct xfrm_dump_info info;
1070 	struct sk_buff *skb;
1071 	int err;
1072 
1073 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1074 	if (!skb)
1075 		return ERR_PTR(-ENOMEM);
1076 
1077 	info.in_skb = in_skb;
1078 	info.out_skb = skb;
1079 	info.nlmsg_seq = seq;
1080 	info.nlmsg_flags = 0;
1081 
1082 	err = dump_one_state(x, 0, &info);
1083 	if (err) {
1084 		kfree_skb(skb);
1085 		return ERR_PTR(err);
1086 	}
1087 
1088 	return skb;
1089 }
1090 
1091 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1092  * Must be called with RCU read lock.
1093  */
1094 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1095 				       u32 pid, unsigned int group)
1096 {
1097 	struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1098 	struct xfrm_translator *xtr;
1099 
1100 	if (!nlsk) {
1101 		kfree_skb(skb);
1102 		return -EPIPE;
1103 	}
1104 
1105 	xtr = xfrm_get_translator();
1106 	if (xtr) {
1107 		int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1108 
1109 		xfrm_put_translator(xtr);
1110 		if (err) {
1111 			kfree_skb(skb);
1112 			return err;
1113 		}
1114 	}
1115 
1116 	return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1117 }
1118 
1119 static inline unsigned int xfrm_spdinfo_msgsize(void)
1120 {
1121 	return NLMSG_ALIGN(4)
1122 	       + nla_total_size(sizeof(struct xfrmu_spdinfo))
1123 	       + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1124 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1125 	       + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1126 }
1127 
1128 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1129 			 u32 portid, u32 seq, u32 flags)
1130 {
1131 	struct xfrmk_spdinfo si;
1132 	struct xfrmu_spdinfo spc;
1133 	struct xfrmu_spdhinfo sph;
1134 	struct xfrmu_spdhthresh spt4, spt6;
1135 	struct nlmsghdr *nlh;
1136 	int err;
1137 	u32 *f;
1138 	unsigned lseq;
1139 
1140 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1141 	if (nlh == NULL) /* shouldn't really happen ... */
1142 		return -EMSGSIZE;
1143 
1144 	f = nlmsg_data(nlh);
1145 	*f = flags;
1146 	xfrm_spd_getinfo(net, &si);
1147 	spc.incnt = si.incnt;
1148 	spc.outcnt = si.outcnt;
1149 	spc.fwdcnt = si.fwdcnt;
1150 	spc.inscnt = si.inscnt;
1151 	spc.outscnt = si.outscnt;
1152 	spc.fwdscnt = si.fwdscnt;
1153 	sph.spdhcnt = si.spdhcnt;
1154 	sph.spdhmcnt = si.spdhmcnt;
1155 
1156 	do {
1157 		lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1158 
1159 		spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1160 		spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1161 		spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1162 		spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1163 	} while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1164 
1165 	err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1166 	if (!err)
1167 		err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1168 	if (!err)
1169 		err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1170 	if (!err)
1171 		err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1172 	if (err) {
1173 		nlmsg_cancel(skb, nlh);
1174 		return err;
1175 	}
1176 
1177 	nlmsg_end(skb, nlh);
1178 	return 0;
1179 }
1180 
1181 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1182 			    struct nlattr **attrs)
1183 {
1184 	struct net *net = sock_net(skb->sk);
1185 	struct xfrmu_spdhthresh *thresh4 = NULL;
1186 	struct xfrmu_spdhthresh *thresh6 = NULL;
1187 
1188 	/* selector prefixlen thresholds to hash policies */
1189 	if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1190 		struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1191 
1192 		if (nla_len(rta) < sizeof(*thresh4))
1193 			return -EINVAL;
1194 		thresh4 = nla_data(rta);
1195 		if (thresh4->lbits > 32 || thresh4->rbits > 32)
1196 			return -EINVAL;
1197 	}
1198 	if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1199 		struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1200 
1201 		if (nla_len(rta) < sizeof(*thresh6))
1202 			return -EINVAL;
1203 		thresh6 = nla_data(rta);
1204 		if (thresh6->lbits > 128 || thresh6->rbits > 128)
1205 			return -EINVAL;
1206 	}
1207 
1208 	if (thresh4 || thresh6) {
1209 		write_seqlock(&net->xfrm.policy_hthresh.lock);
1210 		if (thresh4) {
1211 			net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1212 			net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1213 		}
1214 		if (thresh6) {
1215 			net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1216 			net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1217 		}
1218 		write_sequnlock(&net->xfrm.policy_hthresh.lock);
1219 
1220 		xfrm_policy_hash_rebuild(net);
1221 	}
1222 
1223 	return 0;
1224 }
1225 
1226 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1227 		struct nlattr **attrs)
1228 {
1229 	struct net *net = sock_net(skb->sk);
1230 	struct sk_buff *r_skb;
1231 	u32 *flags = nlmsg_data(nlh);
1232 	u32 sportid = NETLINK_CB(skb).portid;
1233 	u32 seq = nlh->nlmsg_seq;
1234 	int err;
1235 
1236 	r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1237 	if (r_skb == NULL)
1238 		return -ENOMEM;
1239 
1240 	err = build_spdinfo(r_skb, net, sportid, seq, *flags);
1241 	BUG_ON(err < 0);
1242 
1243 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1244 }
1245 
1246 static inline unsigned int xfrm_sadinfo_msgsize(void)
1247 {
1248 	return NLMSG_ALIGN(4)
1249 	       + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1250 	       + nla_total_size(4); /* XFRMA_SAD_CNT */
1251 }
1252 
1253 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1254 			 u32 portid, u32 seq, u32 flags)
1255 {
1256 	struct xfrmk_sadinfo si;
1257 	struct xfrmu_sadhinfo sh;
1258 	struct nlmsghdr *nlh;
1259 	int err;
1260 	u32 *f;
1261 
1262 	nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1263 	if (nlh == NULL) /* shouldn't really happen ... */
1264 		return -EMSGSIZE;
1265 
1266 	f = nlmsg_data(nlh);
1267 	*f = flags;
1268 	xfrm_sad_getinfo(net, &si);
1269 
1270 	sh.sadhmcnt = si.sadhmcnt;
1271 	sh.sadhcnt = si.sadhcnt;
1272 
1273 	err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1274 	if (!err)
1275 		err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1276 	if (err) {
1277 		nlmsg_cancel(skb, nlh);
1278 		return err;
1279 	}
1280 
1281 	nlmsg_end(skb, nlh);
1282 	return 0;
1283 }
1284 
1285 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1286 		struct nlattr **attrs)
1287 {
1288 	struct net *net = sock_net(skb->sk);
1289 	struct sk_buff *r_skb;
1290 	u32 *flags = nlmsg_data(nlh);
1291 	u32 sportid = NETLINK_CB(skb).portid;
1292 	u32 seq = nlh->nlmsg_seq;
1293 	int err;
1294 
1295 	r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1296 	if (r_skb == NULL)
1297 		return -ENOMEM;
1298 
1299 	err = build_sadinfo(r_skb, net, sportid, seq, *flags);
1300 	BUG_ON(err < 0);
1301 
1302 	return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1303 }
1304 
1305 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1306 		struct nlattr **attrs)
1307 {
1308 	struct net *net = sock_net(skb->sk);
1309 	struct xfrm_usersa_id *p = nlmsg_data(nlh);
1310 	struct xfrm_state *x;
1311 	struct sk_buff *resp_skb;
1312 	int err = -ESRCH;
1313 
1314 	x = xfrm_user_state_lookup(net, p, attrs, &err);
1315 	if (x == NULL)
1316 		goto out_noput;
1317 
1318 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1319 	if (IS_ERR(resp_skb)) {
1320 		err = PTR_ERR(resp_skb);
1321 	} else {
1322 		err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1323 	}
1324 	xfrm_state_put(x);
1325 out_noput:
1326 	return err;
1327 }
1328 
1329 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1330 		struct nlattr **attrs)
1331 {
1332 	struct net *net = sock_net(skb->sk);
1333 	struct xfrm_state *x;
1334 	struct xfrm_userspi_info *p;
1335 	struct xfrm_translator *xtr;
1336 	struct sk_buff *resp_skb;
1337 	xfrm_address_t *daddr;
1338 	int family;
1339 	int err;
1340 	u32 mark;
1341 	struct xfrm_mark m;
1342 	u32 if_id = 0;
1343 
1344 	p = nlmsg_data(nlh);
1345 	err = verify_spi_info(p->info.id.proto, p->min, p->max);
1346 	if (err)
1347 		goto out_noput;
1348 
1349 	family = p->info.family;
1350 	daddr = &p->info.id.daddr;
1351 
1352 	x = NULL;
1353 
1354 	mark = xfrm_mark_get(attrs, &m);
1355 
1356 	if (attrs[XFRMA_IF_ID])
1357 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1358 
1359 	if (p->info.seq) {
1360 		x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1361 		if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1362 			xfrm_state_put(x);
1363 			x = NULL;
1364 		}
1365 	}
1366 
1367 	if (!x)
1368 		x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1369 				  if_id, p->info.id.proto, daddr,
1370 				  &p->info.saddr, 1,
1371 				  family);
1372 	err = -ENOENT;
1373 	if (x == NULL)
1374 		goto out_noput;
1375 
1376 	err = xfrm_alloc_spi(x, p->min, p->max);
1377 	if (err)
1378 		goto out;
1379 
1380 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1381 	if (IS_ERR(resp_skb)) {
1382 		err = PTR_ERR(resp_skb);
1383 		goto out;
1384 	}
1385 
1386 	xtr = xfrm_get_translator();
1387 	if (xtr) {
1388 		err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
1389 
1390 		xfrm_put_translator(xtr);
1391 		if (err) {
1392 			kfree_skb(resp_skb);
1393 			goto out;
1394 		}
1395 	}
1396 
1397 	err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1398 
1399 out:
1400 	xfrm_state_put(x);
1401 out_noput:
1402 	return err;
1403 }
1404 
1405 static int verify_policy_dir(u8 dir)
1406 {
1407 	switch (dir) {
1408 	case XFRM_POLICY_IN:
1409 	case XFRM_POLICY_OUT:
1410 	case XFRM_POLICY_FWD:
1411 		break;
1412 
1413 	default:
1414 		return -EINVAL;
1415 	}
1416 
1417 	return 0;
1418 }
1419 
1420 static int verify_policy_type(u8 type)
1421 {
1422 	switch (type) {
1423 	case XFRM_POLICY_TYPE_MAIN:
1424 #ifdef CONFIG_XFRM_SUB_POLICY
1425 	case XFRM_POLICY_TYPE_SUB:
1426 #endif
1427 		break;
1428 
1429 	default:
1430 		return -EINVAL;
1431 	}
1432 
1433 	return 0;
1434 }
1435 
1436 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1437 {
1438 	int ret;
1439 
1440 	switch (p->share) {
1441 	case XFRM_SHARE_ANY:
1442 	case XFRM_SHARE_SESSION:
1443 	case XFRM_SHARE_USER:
1444 	case XFRM_SHARE_UNIQUE:
1445 		break;
1446 
1447 	default:
1448 		return -EINVAL;
1449 	}
1450 
1451 	switch (p->action) {
1452 	case XFRM_POLICY_ALLOW:
1453 	case XFRM_POLICY_BLOCK:
1454 		break;
1455 
1456 	default:
1457 		return -EINVAL;
1458 	}
1459 
1460 	switch (p->sel.family) {
1461 	case AF_INET:
1462 		if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1463 			return -EINVAL;
1464 
1465 		break;
1466 
1467 	case AF_INET6:
1468 #if IS_ENABLED(CONFIG_IPV6)
1469 		if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1470 			return -EINVAL;
1471 
1472 		break;
1473 #else
1474 		return  -EAFNOSUPPORT;
1475 #endif
1476 
1477 	default:
1478 		return -EINVAL;
1479 	}
1480 
1481 	ret = verify_policy_dir(p->dir);
1482 	if (ret)
1483 		return ret;
1484 	if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1485 		return -EINVAL;
1486 
1487 	return 0;
1488 }
1489 
1490 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1491 {
1492 	struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1493 	struct xfrm_user_sec_ctx *uctx;
1494 
1495 	if (!rt)
1496 		return 0;
1497 
1498 	uctx = nla_data(rt);
1499 	return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1500 }
1501 
1502 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1503 			   int nr)
1504 {
1505 	int i;
1506 
1507 	xp->xfrm_nr = nr;
1508 	for (i = 0; i < nr; i++, ut++) {
1509 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1510 
1511 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1512 		memcpy(&t->saddr, &ut->saddr,
1513 		       sizeof(xfrm_address_t));
1514 		t->reqid = ut->reqid;
1515 		t->mode = ut->mode;
1516 		t->share = ut->share;
1517 		t->optional = ut->optional;
1518 		t->aalgos = ut->aalgos;
1519 		t->ealgos = ut->ealgos;
1520 		t->calgos = ut->calgos;
1521 		/* If all masks are ~0, then we allow all algorithms. */
1522 		t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1523 		t->encap_family = ut->family;
1524 	}
1525 }
1526 
1527 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1528 {
1529 	u16 prev_family;
1530 	int i;
1531 
1532 	if (nr > XFRM_MAX_DEPTH)
1533 		return -EINVAL;
1534 
1535 	prev_family = family;
1536 
1537 	for (i = 0; i < nr; i++) {
1538 		/* We never validated the ut->family value, so many
1539 		 * applications simply leave it at zero.  The check was
1540 		 * never made and ut->family was ignored because all
1541 		 * templates could be assumed to have the same family as
1542 		 * the policy itself.  Now that we will have ipv4-in-ipv6
1543 		 * and ipv6-in-ipv4 tunnels, this is no longer true.
1544 		 */
1545 		if (!ut[i].family)
1546 			ut[i].family = family;
1547 
1548 		switch (ut[i].mode) {
1549 		case XFRM_MODE_TUNNEL:
1550 		case XFRM_MODE_BEET:
1551 			break;
1552 		default:
1553 			if (ut[i].family != prev_family)
1554 				return -EINVAL;
1555 			break;
1556 		}
1557 		if (ut[i].mode >= XFRM_MODE_MAX)
1558 			return -EINVAL;
1559 
1560 		prev_family = ut[i].family;
1561 
1562 		switch (ut[i].family) {
1563 		case AF_INET:
1564 			break;
1565 #if IS_ENABLED(CONFIG_IPV6)
1566 		case AF_INET6:
1567 			break;
1568 #endif
1569 		default:
1570 			return -EINVAL;
1571 		}
1572 
1573 		if (!xfrm_id_proto_valid(ut[i].id.proto))
1574 			return -EINVAL;
1575 	}
1576 
1577 	return 0;
1578 }
1579 
1580 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1581 {
1582 	struct nlattr *rt = attrs[XFRMA_TMPL];
1583 
1584 	if (!rt) {
1585 		pol->xfrm_nr = 0;
1586 	} else {
1587 		struct xfrm_user_tmpl *utmpl = nla_data(rt);
1588 		int nr = nla_len(rt) / sizeof(*utmpl);
1589 		int err;
1590 
1591 		err = validate_tmpl(nr, utmpl, pol->family);
1592 		if (err)
1593 			return err;
1594 
1595 		copy_templates(pol, utmpl, nr);
1596 	}
1597 	return 0;
1598 }
1599 
1600 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1601 {
1602 	struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1603 	struct xfrm_userpolicy_type *upt;
1604 	u8 type = XFRM_POLICY_TYPE_MAIN;
1605 	int err;
1606 
1607 	if (rt) {
1608 		upt = nla_data(rt);
1609 		type = upt->type;
1610 	}
1611 
1612 	err = verify_policy_type(type);
1613 	if (err)
1614 		return err;
1615 
1616 	*tp = type;
1617 	return 0;
1618 }
1619 
1620 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1621 {
1622 	xp->priority = p->priority;
1623 	xp->index = p->index;
1624 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1625 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1626 	xp->action = p->action;
1627 	xp->flags = p->flags;
1628 	xp->family = p->sel.family;
1629 	/* XXX xp->share = p->share; */
1630 }
1631 
1632 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1633 {
1634 	memset(p, 0, sizeof(*p));
1635 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1636 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1637 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1638 	p->priority = xp->priority;
1639 	p->index = xp->index;
1640 	p->sel.family = xp->family;
1641 	p->dir = dir;
1642 	p->action = xp->action;
1643 	p->flags = xp->flags;
1644 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1645 }
1646 
1647 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1648 {
1649 	struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1650 	int err;
1651 
1652 	if (!xp) {
1653 		*errp = -ENOMEM;
1654 		return NULL;
1655 	}
1656 
1657 	copy_from_user_policy(xp, p);
1658 
1659 	err = copy_from_user_policy_type(&xp->type, attrs);
1660 	if (err)
1661 		goto error;
1662 
1663 	if (!(err = copy_from_user_tmpl(xp, attrs)))
1664 		err = copy_from_user_sec_ctx(xp, attrs);
1665 	if (err)
1666 		goto error;
1667 
1668 	xfrm_mark_get(attrs, &xp->mark);
1669 
1670 	if (attrs[XFRMA_IF_ID])
1671 		xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1672 
1673 	return xp;
1674  error:
1675 	*errp = err;
1676 	xp->walk.dead = 1;
1677 	xfrm_policy_destroy(xp);
1678 	return NULL;
1679 }
1680 
1681 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1682 		struct nlattr **attrs)
1683 {
1684 	struct net *net = sock_net(skb->sk);
1685 	struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1686 	struct xfrm_policy *xp;
1687 	struct km_event c;
1688 	int err;
1689 	int excl;
1690 
1691 	err = verify_newpolicy_info(p);
1692 	if (err)
1693 		return err;
1694 	err = verify_sec_ctx_len(attrs);
1695 	if (err)
1696 		return err;
1697 
1698 	xp = xfrm_policy_construct(net, p, attrs, &err);
1699 	if (!xp)
1700 		return err;
1701 
1702 	/* shouldn't excl be based on nlh flags??
1703 	 * Aha! this is anti-netlink really i.e  more pfkey derived
1704 	 * in netlink excl is a flag and you wouldnt need
1705 	 * a type XFRM_MSG_UPDPOLICY - JHS */
1706 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1707 	err = xfrm_policy_insert(p->dir, xp, excl);
1708 	xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1709 
1710 	if (err) {
1711 		security_xfrm_policy_free(xp->security);
1712 		kfree(xp);
1713 		return err;
1714 	}
1715 
1716 	c.event = nlh->nlmsg_type;
1717 	c.seq = nlh->nlmsg_seq;
1718 	c.portid = nlh->nlmsg_pid;
1719 	km_policy_notify(xp, p->dir, &c);
1720 
1721 	xfrm_pol_put(xp);
1722 
1723 	return 0;
1724 }
1725 
1726 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1727 {
1728 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1729 	int i;
1730 
1731 	if (xp->xfrm_nr == 0)
1732 		return 0;
1733 
1734 	for (i = 0; i < xp->xfrm_nr; i++) {
1735 		struct xfrm_user_tmpl *up = &vec[i];
1736 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1737 
1738 		memset(up, 0, sizeof(*up));
1739 		memcpy(&up->id, &kp->id, sizeof(up->id));
1740 		up->family = kp->encap_family;
1741 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1742 		up->reqid = kp->reqid;
1743 		up->mode = kp->mode;
1744 		up->share = kp->share;
1745 		up->optional = kp->optional;
1746 		up->aalgos = kp->aalgos;
1747 		up->ealgos = kp->ealgos;
1748 		up->calgos = kp->calgos;
1749 	}
1750 
1751 	return nla_put(skb, XFRMA_TMPL,
1752 		       sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1753 }
1754 
1755 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1756 {
1757 	if (x->security) {
1758 		return copy_sec_ctx(x->security, skb);
1759 	}
1760 	return 0;
1761 }
1762 
1763 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1764 {
1765 	if (xp->security)
1766 		return copy_sec_ctx(xp->security, skb);
1767 	return 0;
1768 }
1769 static inline unsigned int userpolicy_type_attrsize(void)
1770 {
1771 #ifdef CONFIG_XFRM_SUB_POLICY
1772 	return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1773 #else
1774 	return 0;
1775 #endif
1776 }
1777 
1778 #ifdef CONFIG_XFRM_SUB_POLICY
1779 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1780 {
1781 	struct xfrm_userpolicy_type upt;
1782 
1783 	/* Sadly there are two holes in struct xfrm_userpolicy_type */
1784 	memset(&upt, 0, sizeof(upt));
1785 	upt.type = type;
1786 
1787 	return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1788 }
1789 
1790 #else
1791 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1792 {
1793 	return 0;
1794 }
1795 #endif
1796 
1797 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1798 {
1799 	struct xfrm_dump_info *sp = ptr;
1800 	struct xfrm_userpolicy_info *p;
1801 	struct sk_buff *in_skb = sp->in_skb;
1802 	struct sk_buff *skb = sp->out_skb;
1803 	struct xfrm_translator *xtr;
1804 	struct nlmsghdr *nlh;
1805 	int err;
1806 
1807 	nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1808 			XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1809 	if (nlh == NULL)
1810 		return -EMSGSIZE;
1811 
1812 	p = nlmsg_data(nlh);
1813 	copy_to_user_policy(xp, p, dir);
1814 	err = copy_to_user_tmpl(xp, skb);
1815 	if (!err)
1816 		err = copy_to_user_sec_ctx(xp, skb);
1817 	if (!err)
1818 		err = copy_to_user_policy_type(xp->type, skb);
1819 	if (!err)
1820 		err = xfrm_mark_put(skb, &xp->mark);
1821 	if (!err)
1822 		err = xfrm_if_id_put(skb, xp->if_id);
1823 	if (err) {
1824 		nlmsg_cancel(skb, nlh);
1825 		return err;
1826 	}
1827 	nlmsg_end(skb, nlh);
1828 
1829 	xtr = xfrm_get_translator();
1830 	if (xtr) {
1831 		err = xtr->alloc_compat(skb, nlh);
1832 
1833 		xfrm_put_translator(xtr);
1834 		if (err) {
1835 			nlmsg_cancel(skb, nlh);
1836 			return err;
1837 		}
1838 	}
1839 
1840 	return 0;
1841 }
1842 
1843 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1844 {
1845 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1846 	struct net *net = sock_net(cb->skb->sk);
1847 
1848 	xfrm_policy_walk_done(walk, net);
1849 	return 0;
1850 }
1851 
1852 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1853 {
1854 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1855 
1856 	BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1857 
1858 	xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1859 	return 0;
1860 }
1861 
1862 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1863 {
1864 	struct net *net = sock_net(skb->sk);
1865 	struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1866 	struct xfrm_dump_info info;
1867 
1868 	info.in_skb = cb->skb;
1869 	info.out_skb = skb;
1870 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
1871 	info.nlmsg_flags = NLM_F_MULTI;
1872 
1873 	(void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1874 
1875 	return skb->len;
1876 }
1877 
1878 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1879 					  struct xfrm_policy *xp,
1880 					  int dir, u32 seq)
1881 {
1882 	struct xfrm_dump_info info;
1883 	struct sk_buff *skb;
1884 	int err;
1885 
1886 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1887 	if (!skb)
1888 		return ERR_PTR(-ENOMEM);
1889 
1890 	info.in_skb = in_skb;
1891 	info.out_skb = skb;
1892 	info.nlmsg_seq = seq;
1893 	info.nlmsg_flags = 0;
1894 
1895 	err = dump_one_policy(xp, dir, 0, &info);
1896 	if (err) {
1897 		kfree_skb(skb);
1898 		return ERR_PTR(err);
1899 	}
1900 
1901 	return skb;
1902 }
1903 
1904 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1905 		struct nlattr **attrs)
1906 {
1907 	struct net *net = sock_net(skb->sk);
1908 	struct xfrm_policy *xp;
1909 	struct xfrm_userpolicy_id *p;
1910 	u8 type = XFRM_POLICY_TYPE_MAIN;
1911 	int err;
1912 	struct km_event c;
1913 	int delete;
1914 	struct xfrm_mark m;
1915 	u32 if_id = 0;
1916 
1917 	p = nlmsg_data(nlh);
1918 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1919 
1920 	err = copy_from_user_policy_type(&type, attrs);
1921 	if (err)
1922 		return err;
1923 
1924 	err = verify_policy_dir(p->dir);
1925 	if (err)
1926 		return err;
1927 
1928 	if (attrs[XFRMA_IF_ID])
1929 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
1930 
1931 	xfrm_mark_get(attrs, &m);
1932 
1933 	if (p->index)
1934 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
1935 				      p->index, delete, &err);
1936 	else {
1937 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1938 		struct xfrm_sec_ctx *ctx;
1939 
1940 		err = verify_sec_ctx_len(attrs);
1941 		if (err)
1942 			return err;
1943 
1944 		ctx = NULL;
1945 		if (rt) {
1946 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1947 
1948 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1949 			if (err)
1950 				return err;
1951 		}
1952 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
1953 					   &p->sel, ctx, delete, &err);
1954 		security_xfrm_policy_free(ctx);
1955 	}
1956 	if (xp == NULL)
1957 		return -ENOENT;
1958 
1959 	if (!delete) {
1960 		struct sk_buff *resp_skb;
1961 
1962 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1963 		if (IS_ERR(resp_skb)) {
1964 			err = PTR_ERR(resp_skb);
1965 		} else {
1966 			err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1967 					    NETLINK_CB(skb).portid);
1968 		}
1969 	} else {
1970 		xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1971 
1972 		if (err != 0)
1973 			goto out;
1974 
1975 		c.data.byid = p->index;
1976 		c.event = nlh->nlmsg_type;
1977 		c.seq = nlh->nlmsg_seq;
1978 		c.portid = nlh->nlmsg_pid;
1979 		km_policy_notify(xp, p->dir, &c);
1980 	}
1981 
1982 out:
1983 	xfrm_pol_put(xp);
1984 	return err;
1985 }
1986 
1987 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1988 		struct nlattr **attrs)
1989 {
1990 	struct net *net = sock_net(skb->sk);
1991 	struct km_event c;
1992 	struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1993 	int err;
1994 
1995 	err = xfrm_state_flush(net, p->proto, true, false);
1996 	if (err) {
1997 		if (err == -ESRCH) /* empty table */
1998 			return 0;
1999 		return err;
2000 	}
2001 	c.data.proto = p->proto;
2002 	c.event = nlh->nlmsg_type;
2003 	c.seq = nlh->nlmsg_seq;
2004 	c.portid = nlh->nlmsg_pid;
2005 	c.net = net;
2006 	km_state_notify(NULL, &c);
2007 
2008 	return 0;
2009 }
2010 
2011 static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
2012 {
2013 	unsigned int replay_size = x->replay_esn ?
2014 			      xfrm_replay_state_esn_len(x->replay_esn) :
2015 			      sizeof(struct xfrm_replay_state);
2016 
2017 	return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
2018 	       + nla_total_size(replay_size)
2019 	       + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
2020 	       + nla_total_size(sizeof(struct xfrm_mark))
2021 	       + nla_total_size(4) /* XFRM_AE_RTHR */
2022 	       + nla_total_size(4); /* XFRM_AE_ETHR */
2023 }
2024 
2025 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2026 {
2027 	struct xfrm_aevent_id *id;
2028 	struct nlmsghdr *nlh;
2029 	int err;
2030 
2031 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
2032 	if (nlh == NULL)
2033 		return -EMSGSIZE;
2034 
2035 	id = nlmsg_data(nlh);
2036 	memset(&id->sa_id, 0, sizeof(id->sa_id));
2037 	memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
2038 	id->sa_id.spi = x->id.spi;
2039 	id->sa_id.family = x->props.family;
2040 	id->sa_id.proto = x->id.proto;
2041 	memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
2042 	id->reqid = x->props.reqid;
2043 	id->flags = c->data.aevent;
2044 
2045 	if (x->replay_esn) {
2046 		err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
2047 			      xfrm_replay_state_esn_len(x->replay_esn),
2048 			      x->replay_esn);
2049 	} else {
2050 		err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
2051 			      &x->replay);
2052 	}
2053 	if (err)
2054 		goto out_cancel;
2055 	err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
2056 			    XFRMA_PAD);
2057 	if (err)
2058 		goto out_cancel;
2059 
2060 	if (id->flags & XFRM_AE_RTHR) {
2061 		err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
2062 		if (err)
2063 			goto out_cancel;
2064 	}
2065 	if (id->flags & XFRM_AE_ETHR) {
2066 		err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
2067 				  x->replay_maxage * 10 / HZ);
2068 		if (err)
2069 			goto out_cancel;
2070 	}
2071 	err = xfrm_mark_put(skb, &x->mark);
2072 	if (err)
2073 		goto out_cancel;
2074 
2075 	err = xfrm_if_id_put(skb, x->if_id);
2076 	if (err)
2077 		goto out_cancel;
2078 
2079 	nlmsg_end(skb, nlh);
2080 	return 0;
2081 
2082 out_cancel:
2083 	nlmsg_cancel(skb, nlh);
2084 	return err;
2085 }
2086 
2087 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2088 		struct nlattr **attrs)
2089 {
2090 	struct net *net = sock_net(skb->sk);
2091 	struct xfrm_state *x;
2092 	struct sk_buff *r_skb;
2093 	int err;
2094 	struct km_event c;
2095 	u32 mark;
2096 	struct xfrm_mark m;
2097 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2098 	struct xfrm_usersa_id *id = &p->sa_id;
2099 
2100 	mark = xfrm_mark_get(attrs, &m);
2101 
2102 	x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
2103 	if (x == NULL)
2104 		return -ESRCH;
2105 
2106 	r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2107 	if (r_skb == NULL) {
2108 		xfrm_state_put(x);
2109 		return -ENOMEM;
2110 	}
2111 
2112 	/*
2113 	 * XXX: is this lock really needed - none of the other
2114 	 * gets lock (the concern is things getting updated
2115 	 * while we are still reading) - jhs
2116 	*/
2117 	spin_lock_bh(&x->lock);
2118 	c.data.aevent = p->flags;
2119 	c.seq = nlh->nlmsg_seq;
2120 	c.portid = nlh->nlmsg_pid;
2121 
2122 	err = build_aevent(r_skb, x, &c);
2123 	BUG_ON(err < 0);
2124 
2125 	err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2126 	spin_unlock_bh(&x->lock);
2127 	xfrm_state_put(x);
2128 	return err;
2129 }
2130 
2131 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2132 		struct nlattr **attrs)
2133 {
2134 	struct net *net = sock_net(skb->sk);
2135 	struct xfrm_state *x;
2136 	struct km_event c;
2137 	int err = -EINVAL;
2138 	u32 mark = 0;
2139 	struct xfrm_mark m;
2140 	struct xfrm_aevent_id *p = nlmsg_data(nlh);
2141 	struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2142 	struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2143 	struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2144 	struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2145 	struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2146 
2147 	if (!lt && !rp && !re && !et && !rt)
2148 		return err;
2149 
2150 	/* pedantic mode - thou shalt sayeth replaceth */
2151 	if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2152 		return err;
2153 
2154 	mark = xfrm_mark_get(attrs, &m);
2155 
2156 	x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2157 	if (x == NULL)
2158 		return -ESRCH;
2159 
2160 	if (x->km.state != XFRM_STATE_VALID)
2161 		goto out;
2162 
2163 	err = xfrm_replay_verify_len(x->replay_esn, re);
2164 	if (err)
2165 		goto out;
2166 
2167 	spin_lock_bh(&x->lock);
2168 	xfrm_update_ae_params(x, attrs, 1);
2169 	spin_unlock_bh(&x->lock);
2170 
2171 	c.event = nlh->nlmsg_type;
2172 	c.seq = nlh->nlmsg_seq;
2173 	c.portid = nlh->nlmsg_pid;
2174 	c.data.aevent = XFRM_AE_CU;
2175 	km_state_notify(x, &c);
2176 	err = 0;
2177 out:
2178 	xfrm_state_put(x);
2179 	return err;
2180 }
2181 
2182 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2183 		struct nlattr **attrs)
2184 {
2185 	struct net *net = sock_net(skb->sk);
2186 	struct km_event c;
2187 	u8 type = XFRM_POLICY_TYPE_MAIN;
2188 	int err;
2189 
2190 	err = copy_from_user_policy_type(&type, attrs);
2191 	if (err)
2192 		return err;
2193 
2194 	err = xfrm_policy_flush(net, type, true);
2195 	if (err) {
2196 		if (err == -ESRCH) /* empty table */
2197 			return 0;
2198 		return err;
2199 	}
2200 
2201 	c.data.type = type;
2202 	c.event = nlh->nlmsg_type;
2203 	c.seq = nlh->nlmsg_seq;
2204 	c.portid = nlh->nlmsg_pid;
2205 	c.net = net;
2206 	km_policy_notify(NULL, 0, &c);
2207 	return 0;
2208 }
2209 
2210 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2211 		struct nlattr **attrs)
2212 {
2213 	struct net *net = sock_net(skb->sk);
2214 	struct xfrm_policy *xp;
2215 	struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2216 	struct xfrm_userpolicy_info *p = &up->pol;
2217 	u8 type = XFRM_POLICY_TYPE_MAIN;
2218 	int err = -ENOENT;
2219 	struct xfrm_mark m;
2220 	u32 if_id = 0;
2221 
2222 	err = copy_from_user_policy_type(&type, attrs);
2223 	if (err)
2224 		return err;
2225 
2226 	err = verify_policy_dir(p->dir);
2227 	if (err)
2228 		return err;
2229 
2230 	if (attrs[XFRMA_IF_ID])
2231 		if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
2232 
2233 	xfrm_mark_get(attrs, &m);
2234 
2235 	if (p->index)
2236 		xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
2237 				      0, &err);
2238 	else {
2239 		struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2240 		struct xfrm_sec_ctx *ctx;
2241 
2242 		err = verify_sec_ctx_len(attrs);
2243 		if (err)
2244 			return err;
2245 
2246 		ctx = NULL;
2247 		if (rt) {
2248 			struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2249 
2250 			err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2251 			if (err)
2252 				return err;
2253 		}
2254 		xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
2255 					   &p->sel, ctx, 0, &err);
2256 		security_xfrm_policy_free(ctx);
2257 	}
2258 	if (xp == NULL)
2259 		return -ENOENT;
2260 
2261 	if (unlikely(xp->walk.dead))
2262 		goto out;
2263 
2264 	err = 0;
2265 	if (up->hard) {
2266 		xfrm_policy_delete(xp, p->dir);
2267 		xfrm_audit_policy_delete(xp, 1, true);
2268 	}
2269 	km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2270 
2271 out:
2272 	xfrm_pol_put(xp);
2273 	return err;
2274 }
2275 
2276 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2277 		struct nlattr **attrs)
2278 {
2279 	struct net *net = sock_net(skb->sk);
2280 	struct xfrm_state *x;
2281 	int err;
2282 	struct xfrm_user_expire *ue = nlmsg_data(nlh);
2283 	struct xfrm_usersa_info *p = &ue->state;
2284 	struct xfrm_mark m;
2285 	u32 mark = xfrm_mark_get(attrs, &m);
2286 
2287 	x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2288 
2289 	err = -ENOENT;
2290 	if (x == NULL)
2291 		return err;
2292 
2293 	spin_lock_bh(&x->lock);
2294 	err = -EINVAL;
2295 	if (x->km.state != XFRM_STATE_VALID)
2296 		goto out;
2297 	km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2298 
2299 	if (ue->hard) {
2300 		__xfrm_state_delete(x);
2301 		xfrm_audit_state_delete(x, 1, true);
2302 	}
2303 	err = 0;
2304 out:
2305 	spin_unlock_bh(&x->lock);
2306 	xfrm_state_put(x);
2307 	return err;
2308 }
2309 
2310 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2311 		struct nlattr **attrs)
2312 {
2313 	struct net *net = sock_net(skb->sk);
2314 	struct xfrm_policy *xp;
2315 	struct xfrm_user_tmpl *ut;
2316 	int i;
2317 	struct nlattr *rt = attrs[XFRMA_TMPL];
2318 	struct xfrm_mark mark;
2319 
2320 	struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2321 	struct xfrm_state *x = xfrm_state_alloc(net);
2322 	int err = -ENOMEM;
2323 
2324 	if (!x)
2325 		goto nomem;
2326 
2327 	xfrm_mark_get(attrs, &mark);
2328 
2329 	err = verify_newpolicy_info(&ua->policy);
2330 	if (err)
2331 		goto free_state;
2332 	err = verify_sec_ctx_len(attrs);
2333 	if (err)
2334 		goto free_state;
2335 
2336 	/*   build an XP */
2337 	xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2338 	if (!xp)
2339 		goto free_state;
2340 
2341 	memcpy(&x->id, &ua->id, sizeof(ua->id));
2342 	memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2343 	memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2344 	xp->mark.m = x->mark.m = mark.m;
2345 	xp->mark.v = x->mark.v = mark.v;
2346 	ut = nla_data(rt);
2347 	/* extract the templates and for each call km_key */
2348 	for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2349 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2350 		memcpy(&x->id, &t->id, sizeof(x->id));
2351 		x->props.mode = t->mode;
2352 		x->props.reqid = t->reqid;
2353 		x->props.family = ut->family;
2354 		t->aalgos = ua->aalgos;
2355 		t->ealgos = ua->ealgos;
2356 		t->calgos = ua->calgos;
2357 		err = km_query(x, t, xp);
2358 
2359 	}
2360 
2361 	xfrm_state_free(x);
2362 	kfree(xp);
2363 
2364 	return 0;
2365 
2366 free_state:
2367 	xfrm_state_free(x);
2368 nomem:
2369 	return err;
2370 }
2371 
2372 #ifdef CONFIG_XFRM_MIGRATE
2373 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2374 				  struct xfrm_kmaddress *k,
2375 				  struct nlattr **attrs, int *num)
2376 {
2377 	struct nlattr *rt = attrs[XFRMA_MIGRATE];
2378 	struct xfrm_user_migrate *um;
2379 	int i, num_migrate;
2380 
2381 	if (k != NULL) {
2382 		struct xfrm_user_kmaddress *uk;
2383 
2384 		uk = nla_data(attrs[XFRMA_KMADDRESS]);
2385 		memcpy(&k->local, &uk->local, sizeof(k->local));
2386 		memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2387 		k->family = uk->family;
2388 		k->reserved = uk->reserved;
2389 	}
2390 
2391 	um = nla_data(rt);
2392 	num_migrate = nla_len(rt) / sizeof(*um);
2393 
2394 	if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2395 		return -EINVAL;
2396 
2397 	for (i = 0; i < num_migrate; i++, um++, ma++) {
2398 		memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2399 		memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2400 		memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2401 		memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2402 
2403 		ma->proto = um->proto;
2404 		ma->mode = um->mode;
2405 		ma->reqid = um->reqid;
2406 
2407 		ma->old_family = um->old_family;
2408 		ma->new_family = um->new_family;
2409 	}
2410 
2411 	*num = i;
2412 	return 0;
2413 }
2414 
2415 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2416 			   struct nlattr **attrs)
2417 {
2418 	struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2419 	struct xfrm_migrate m[XFRM_MAX_DEPTH];
2420 	struct xfrm_kmaddress km, *kmp;
2421 	u8 type;
2422 	int err;
2423 	int n = 0;
2424 	struct net *net = sock_net(skb->sk);
2425 	struct xfrm_encap_tmpl  *encap = NULL;
2426 
2427 	if (attrs[XFRMA_MIGRATE] == NULL)
2428 		return -EINVAL;
2429 
2430 	kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2431 
2432 	err = copy_from_user_policy_type(&type, attrs);
2433 	if (err)
2434 		return err;
2435 
2436 	err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2437 	if (err)
2438 		return err;
2439 
2440 	if (!n)
2441 		return 0;
2442 
2443 	if (attrs[XFRMA_ENCAP]) {
2444 		encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2445 				sizeof(*encap), GFP_KERNEL);
2446 		if (!encap)
2447 			return 0;
2448 	}
2449 
2450 	err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2451 
2452 	kfree(encap);
2453 
2454 	return err;
2455 }
2456 #else
2457 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2458 			   struct nlattr **attrs)
2459 {
2460 	return -ENOPROTOOPT;
2461 }
2462 #endif
2463 
2464 #ifdef CONFIG_XFRM_MIGRATE
2465 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2466 {
2467 	struct xfrm_user_migrate um;
2468 
2469 	memset(&um, 0, sizeof(um));
2470 	um.proto = m->proto;
2471 	um.mode = m->mode;
2472 	um.reqid = m->reqid;
2473 	um.old_family = m->old_family;
2474 	memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2475 	memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2476 	um.new_family = m->new_family;
2477 	memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2478 	memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2479 
2480 	return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2481 }
2482 
2483 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2484 {
2485 	struct xfrm_user_kmaddress uk;
2486 
2487 	memset(&uk, 0, sizeof(uk));
2488 	uk.family = k->family;
2489 	uk.reserved = k->reserved;
2490 	memcpy(&uk.local, &k->local, sizeof(uk.local));
2491 	memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2492 
2493 	return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2494 }
2495 
2496 static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
2497 						int with_encp)
2498 {
2499 	return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2500 	      + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2501 	      + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2502 	      + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2503 	      + userpolicy_type_attrsize();
2504 }
2505 
2506 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2507 			 int num_migrate, const struct xfrm_kmaddress *k,
2508 			 const struct xfrm_selector *sel,
2509 			 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2510 {
2511 	const struct xfrm_migrate *mp;
2512 	struct xfrm_userpolicy_id *pol_id;
2513 	struct nlmsghdr *nlh;
2514 	int i, err;
2515 
2516 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2517 	if (nlh == NULL)
2518 		return -EMSGSIZE;
2519 
2520 	pol_id = nlmsg_data(nlh);
2521 	/* copy data from selector, dir, and type to the pol_id */
2522 	memset(pol_id, 0, sizeof(*pol_id));
2523 	memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2524 	pol_id->dir = dir;
2525 
2526 	if (k != NULL) {
2527 		err = copy_to_user_kmaddress(k, skb);
2528 		if (err)
2529 			goto out_cancel;
2530 	}
2531 	if (encap) {
2532 		err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2533 		if (err)
2534 			goto out_cancel;
2535 	}
2536 	err = copy_to_user_policy_type(type, skb);
2537 	if (err)
2538 		goto out_cancel;
2539 	for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2540 		err = copy_to_user_migrate(mp, skb);
2541 		if (err)
2542 			goto out_cancel;
2543 	}
2544 
2545 	nlmsg_end(skb, nlh);
2546 	return 0;
2547 
2548 out_cancel:
2549 	nlmsg_cancel(skb, nlh);
2550 	return err;
2551 }
2552 
2553 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2554 			     const struct xfrm_migrate *m, int num_migrate,
2555 			     const struct xfrm_kmaddress *k,
2556 			     const struct xfrm_encap_tmpl *encap)
2557 {
2558 	struct net *net = &init_net;
2559 	struct sk_buff *skb;
2560 	int err;
2561 
2562 	skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2563 			GFP_ATOMIC);
2564 	if (skb == NULL)
2565 		return -ENOMEM;
2566 
2567 	/* build migrate */
2568 	err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
2569 	BUG_ON(err < 0);
2570 
2571 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2572 }
2573 #else
2574 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2575 			     const struct xfrm_migrate *m, int num_migrate,
2576 			     const struct xfrm_kmaddress *k,
2577 			     const struct xfrm_encap_tmpl *encap)
2578 {
2579 	return -ENOPROTOOPT;
2580 }
2581 #endif
2582 
2583 #define XMSGSIZE(type) sizeof(struct type)
2584 
2585 const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2586 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2587 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2588 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2589 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2590 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2591 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2592 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2593 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2594 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2595 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2596 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2597 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2598 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2599 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2600 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2601 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2602 	[XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2603 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2604 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2605 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2606 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2607 };
2608 EXPORT_SYMBOL_GPL(xfrm_msg_min);
2609 
2610 #undef XMSGSIZE
2611 
2612 const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2613 	[XFRMA_SA]		= { .len = sizeof(struct xfrm_usersa_info)},
2614 	[XFRMA_POLICY]		= { .len = sizeof(struct xfrm_userpolicy_info)},
2615 	[XFRMA_LASTUSED]	= { .type = NLA_U64},
2616 	[XFRMA_ALG_AUTH_TRUNC]	= { .len = sizeof(struct xfrm_algo_auth)},
2617 	[XFRMA_ALG_AEAD]	= { .len = sizeof(struct xfrm_algo_aead) },
2618 	[XFRMA_ALG_AUTH]	= { .len = sizeof(struct xfrm_algo) },
2619 	[XFRMA_ALG_CRYPT]	= { .len = sizeof(struct xfrm_algo) },
2620 	[XFRMA_ALG_COMP]	= { .len = sizeof(struct xfrm_algo) },
2621 	[XFRMA_ENCAP]		= { .len = sizeof(struct xfrm_encap_tmpl) },
2622 	[XFRMA_TMPL]		= { .len = sizeof(struct xfrm_user_tmpl) },
2623 	[XFRMA_SEC_CTX]		= { .len = sizeof(struct xfrm_sec_ctx) },
2624 	[XFRMA_LTIME_VAL]	= { .len = sizeof(struct xfrm_lifetime_cur) },
2625 	[XFRMA_REPLAY_VAL]	= { .len = sizeof(struct xfrm_replay_state) },
2626 	[XFRMA_REPLAY_THRESH]	= { .type = NLA_U32 },
2627 	[XFRMA_ETIMER_THRESH]	= { .type = NLA_U32 },
2628 	[XFRMA_SRCADDR]		= { .len = sizeof(xfrm_address_t) },
2629 	[XFRMA_COADDR]		= { .len = sizeof(xfrm_address_t) },
2630 	[XFRMA_POLICY_TYPE]	= { .len = sizeof(struct xfrm_userpolicy_type)},
2631 	[XFRMA_MIGRATE]		= { .len = sizeof(struct xfrm_user_migrate) },
2632 	[XFRMA_KMADDRESS]	= { .len = sizeof(struct xfrm_user_kmaddress) },
2633 	[XFRMA_MARK]		= { .len = sizeof(struct xfrm_mark) },
2634 	[XFRMA_TFCPAD]		= { .type = NLA_U32 },
2635 	[XFRMA_REPLAY_ESN_VAL]	= { .len = sizeof(struct xfrm_replay_state_esn) },
2636 	[XFRMA_SA_EXTRA_FLAGS]	= { .type = NLA_U32 },
2637 	[XFRMA_PROTO]		= { .type = NLA_U8 },
2638 	[XFRMA_ADDRESS_FILTER]	= { .len = sizeof(struct xfrm_address_filter) },
2639 	[XFRMA_OFFLOAD_DEV]	= { .len = sizeof(struct xfrm_user_offload) },
2640 	[XFRMA_SET_MARK]	= { .type = NLA_U32 },
2641 	[XFRMA_SET_MARK_MASK]	= { .type = NLA_U32 },
2642 	[XFRMA_IF_ID]		= { .type = NLA_U32 },
2643 };
2644 EXPORT_SYMBOL_GPL(xfrma_policy);
2645 
2646 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2647 	[XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2648 	[XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2649 };
2650 
2651 static const struct xfrm_link {
2652 	int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2653 	int (*start)(struct netlink_callback *);
2654 	int (*dump)(struct sk_buff *, struct netlink_callback *);
2655 	int (*done)(struct netlink_callback *);
2656 	const struct nla_policy *nla_pol;
2657 	int nla_max;
2658 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2659 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2660 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2661 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2662 						   .dump = xfrm_dump_sa,
2663 						   .done = xfrm_dump_sa_done  },
2664 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2665 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2666 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2667 						   .start = xfrm_dump_policy_start,
2668 						   .dump = xfrm_dump_policy,
2669 						   .done = xfrm_dump_policy_done },
2670 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2671 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
2672 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2673 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2674 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2675 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2676 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2677 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2678 	[XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2679 	[XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
2680 	[XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
2681 	[XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2682 	[XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2683 						   .nla_pol = xfrma_spd_policy,
2684 						   .nla_max = XFRMA_SPD_MAX },
2685 	[XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
2686 };
2687 
2688 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2689 			     struct netlink_ext_ack *extack)
2690 {
2691 	struct net *net = sock_net(skb->sk);
2692 	struct nlattr *attrs[XFRMA_MAX+1];
2693 	const struct xfrm_link *link;
2694 	struct nlmsghdr *nlh64 = NULL;
2695 	int type, err;
2696 
2697 	type = nlh->nlmsg_type;
2698 	if (type > XFRM_MSG_MAX)
2699 		return -EINVAL;
2700 
2701 	type -= XFRM_MSG_BASE;
2702 	link = &xfrm_dispatch[type];
2703 
2704 	/* All operations require privileges, even GET */
2705 	if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2706 		return -EPERM;
2707 
2708 	if (in_compat_syscall()) {
2709 		struct xfrm_translator *xtr = xfrm_get_translator();
2710 
2711 		if (!xtr)
2712 			return -EOPNOTSUPP;
2713 
2714 		nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
2715 					    link->nla_pol, extack);
2716 		xfrm_put_translator(xtr);
2717 		if (IS_ERR(nlh64))
2718 			return PTR_ERR(nlh64);
2719 		if (nlh64)
2720 			nlh = nlh64;
2721 	}
2722 
2723 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2724 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2725 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
2726 		struct netlink_dump_control c = {
2727 			.start = link->start,
2728 			.dump = link->dump,
2729 			.done = link->done,
2730 		};
2731 
2732 		if (link->dump == NULL) {
2733 			err = -EINVAL;
2734 			goto err;
2735 		}
2736 
2737 		err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2738 		goto err;
2739 	}
2740 
2741 	err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
2742 				     link->nla_max ? : XFRMA_MAX,
2743 				     link->nla_pol ? : xfrma_policy, extack);
2744 	if (err < 0)
2745 		goto err;
2746 
2747 	if (link->doit == NULL) {
2748 		err = -EINVAL;
2749 		goto err;
2750 	}
2751 
2752 	err = link->doit(skb, nlh, attrs);
2753 
2754 err:
2755 	kvfree(nlh64);
2756 	return err;
2757 }
2758 
2759 static void xfrm_netlink_rcv(struct sk_buff *skb)
2760 {
2761 	struct net *net = sock_net(skb->sk);
2762 
2763 	mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2764 	netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2765 	mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2766 }
2767 
2768 static inline unsigned int xfrm_expire_msgsize(void)
2769 {
2770 	return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2771 	       + nla_total_size(sizeof(struct xfrm_mark));
2772 }
2773 
2774 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2775 {
2776 	struct xfrm_user_expire *ue;
2777 	struct nlmsghdr *nlh;
2778 	int err;
2779 
2780 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2781 	if (nlh == NULL)
2782 		return -EMSGSIZE;
2783 
2784 	ue = nlmsg_data(nlh);
2785 	copy_to_user_state(x, &ue->state);
2786 	ue->hard = (c->data.hard != 0) ? 1 : 0;
2787 	/* clear the padding bytes */
2788 	memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2789 
2790 	err = xfrm_mark_put(skb, &x->mark);
2791 	if (err)
2792 		return err;
2793 
2794 	err = xfrm_if_id_put(skb, x->if_id);
2795 	if (err)
2796 		return err;
2797 
2798 	nlmsg_end(skb, nlh);
2799 	return 0;
2800 }
2801 
2802 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2803 {
2804 	struct net *net = xs_net(x);
2805 	struct sk_buff *skb;
2806 
2807 	skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2808 	if (skb == NULL)
2809 		return -ENOMEM;
2810 
2811 	if (build_expire(skb, x, c) < 0) {
2812 		kfree_skb(skb);
2813 		return -EMSGSIZE;
2814 	}
2815 
2816 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2817 }
2818 
2819 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2820 {
2821 	struct net *net = xs_net(x);
2822 	struct sk_buff *skb;
2823 	int err;
2824 
2825 	skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2826 	if (skb == NULL)
2827 		return -ENOMEM;
2828 
2829 	err = build_aevent(skb, x, c);
2830 	BUG_ON(err < 0);
2831 
2832 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2833 }
2834 
2835 static int xfrm_notify_sa_flush(const struct km_event *c)
2836 {
2837 	struct net *net = c->net;
2838 	struct xfrm_usersa_flush *p;
2839 	struct nlmsghdr *nlh;
2840 	struct sk_buff *skb;
2841 	int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2842 
2843 	skb = nlmsg_new(len, GFP_ATOMIC);
2844 	if (skb == NULL)
2845 		return -ENOMEM;
2846 
2847 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2848 	if (nlh == NULL) {
2849 		kfree_skb(skb);
2850 		return -EMSGSIZE;
2851 	}
2852 
2853 	p = nlmsg_data(nlh);
2854 	p->proto = c->data.proto;
2855 
2856 	nlmsg_end(skb, nlh);
2857 
2858 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2859 }
2860 
2861 static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
2862 {
2863 	unsigned int l = 0;
2864 	if (x->aead)
2865 		l += nla_total_size(aead_len(x->aead));
2866 	if (x->aalg) {
2867 		l += nla_total_size(sizeof(struct xfrm_algo) +
2868 				    (x->aalg->alg_key_len + 7) / 8);
2869 		l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2870 	}
2871 	if (x->ealg)
2872 		l += nla_total_size(xfrm_alg_len(x->ealg));
2873 	if (x->calg)
2874 		l += nla_total_size(sizeof(*x->calg));
2875 	if (x->encap)
2876 		l += nla_total_size(sizeof(*x->encap));
2877 	if (x->tfcpad)
2878 		l += nla_total_size(sizeof(x->tfcpad));
2879 	if (x->replay_esn)
2880 		l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2881 	else
2882 		l += nla_total_size(sizeof(struct xfrm_replay_state));
2883 	if (x->security)
2884 		l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2885 				    x->security->ctx_len);
2886 	if (x->coaddr)
2887 		l += nla_total_size(sizeof(*x->coaddr));
2888 	if (x->props.extra_flags)
2889 		l += nla_total_size(sizeof(x->props.extra_flags));
2890 	if (x->xso.dev)
2891 		 l += nla_total_size(sizeof(x->xso));
2892 	if (x->props.smark.v | x->props.smark.m) {
2893 		l += nla_total_size(sizeof(x->props.smark.v));
2894 		l += nla_total_size(sizeof(x->props.smark.m));
2895 	}
2896 	if (x->if_id)
2897 		l += nla_total_size(sizeof(x->if_id));
2898 
2899 	/* Must count x->lastused as it may become non-zero behind our back. */
2900 	l += nla_total_size_64bit(sizeof(u64));
2901 
2902 	return l;
2903 }
2904 
2905 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2906 {
2907 	struct net *net = xs_net(x);
2908 	struct xfrm_usersa_info *p;
2909 	struct xfrm_usersa_id *id;
2910 	struct nlmsghdr *nlh;
2911 	struct sk_buff *skb;
2912 	unsigned int len = xfrm_sa_len(x);
2913 	unsigned int headlen;
2914 	int err;
2915 
2916 	headlen = sizeof(*p);
2917 	if (c->event == XFRM_MSG_DELSA) {
2918 		len += nla_total_size(headlen);
2919 		headlen = sizeof(*id);
2920 		len += nla_total_size(sizeof(struct xfrm_mark));
2921 	}
2922 	len += NLMSG_ALIGN(headlen);
2923 
2924 	skb = nlmsg_new(len, GFP_ATOMIC);
2925 	if (skb == NULL)
2926 		return -ENOMEM;
2927 
2928 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2929 	err = -EMSGSIZE;
2930 	if (nlh == NULL)
2931 		goto out_free_skb;
2932 
2933 	p = nlmsg_data(nlh);
2934 	if (c->event == XFRM_MSG_DELSA) {
2935 		struct nlattr *attr;
2936 
2937 		id = nlmsg_data(nlh);
2938 		memset(id, 0, sizeof(*id));
2939 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2940 		id->spi = x->id.spi;
2941 		id->family = x->props.family;
2942 		id->proto = x->id.proto;
2943 
2944 		attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2945 		err = -EMSGSIZE;
2946 		if (attr == NULL)
2947 			goto out_free_skb;
2948 
2949 		p = nla_data(attr);
2950 	}
2951 	err = copy_to_user_state_extra(x, p, skb);
2952 	if (err)
2953 		goto out_free_skb;
2954 
2955 	nlmsg_end(skb, nlh);
2956 
2957 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2958 
2959 out_free_skb:
2960 	kfree_skb(skb);
2961 	return err;
2962 }
2963 
2964 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2965 {
2966 
2967 	switch (c->event) {
2968 	case XFRM_MSG_EXPIRE:
2969 		return xfrm_exp_state_notify(x, c);
2970 	case XFRM_MSG_NEWAE:
2971 		return xfrm_aevent_state_notify(x, c);
2972 	case XFRM_MSG_DELSA:
2973 	case XFRM_MSG_UPDSA:
2974 	case XFRM_MSG_NEWSA:
2975 		return xfrm_notify_sa(x, c);
2976 	case XFRM_MSG_FLUSHSA:
2977 		return xfrm_notify_sa_flush(c);
2978 	default:
2979 		printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2980 		       c->event);
2981 		break;
2982 	}
2983 
2984 	return 0;
2985 
2986 }
2987 
2988 static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
2989 						struct xfrm_policy *xp)
2990 {
2991 	return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2992 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2993 	       + nla_total_size(sizeof(struct xfrm_mark))
2994 	       + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2995 	       + userpolicy_type_attrsize();
2996 }
2997 
2998 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2999 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
3000 {
3001 	__u32 seq = xfrm_get_acqseq();
3002 	struct xfrm_user_acquire *ua;
3003 	struct nlmsghdr *nlh;
3004 	int err;
3005 
3006 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
3007 	if (nlh == NULL)
3008 		return -EMSGSIZE;
3009 
3010 	ua = nlmsg_data(nlh);
3011 	memcpy(&ua->id, &x->id, sizeof(ua->id));
3012 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
3013 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
3014 	copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
3015 	ua->aalgos = xt->aalgos;
3016 	ua->ealgos = xt->ealgos;
3017 	ua->calgos = xt->calgos;
3018 	ua->seq = x->km.seq = seq;
3019 
3020 	err = copy_to_user_tmpl(xp, skb);
3021 	if (!err)
3022 		err = copy_to_user_state_sec_ctx(x, skb);
3023 	if (!err)
3024 		err = copy_to_user_policy_type(xp->type, skb);
3025 	if (!err)
3026 		err = xfrm_mark_put(skb, &xp->mark);
3027 	if (!err)
3028 		err = xfrm_if_id_put(skb, xp->if_id);
3029 	if (err) {
3030 		nlmsg_cancel(skb, nlh);
3031 		return err;
3032 	}
3033 
3034 	nlmsg_end(skb, nlh);
3035 	return 0;
3036 }
3037 
3038 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
3039 			     struct xfrm_policy *xp)
3040 {
3041 	struct net *net = xs_net(x);
3042 	struct sk_buff *skb;
3043 	int err;
3044 
3045 	skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
3046 	if (skb == NULL)
3047 		return -ENOMEM;
3048 
3049 	err = build_acquire(skb, x, xt, xp);
3050 	BUG_ON(err < 0);
3051 
3052 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
3053 }
3054 
3055 /* User gives us xfrm_user_policy_info followed by an array of 0
3056  * or more templates.
3057  */
3058 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
3059 					       u8 *data, int len, int *dir)
3060 {
3061 	struct net *net = sock_net(sk);
3062 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
3063 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
3064 	struct xfrm_policy *xp;
3065 	int nr;
3066 
3067 	switch (sk->sk_family) {
3068 	case AF_INET:
3069 		if (opt != IP_XFRM_POLICY) {
3070 			*dir = -EOPNOTSUPP;
3071 			return NULL;
3072 		}
3073 		break;
3074 #if IS_ENABLED(CONFIG_IPV6)
3075 	case AF_INET6:
3076 		if (opt != IPV6_XFRM_POLICY) {
3077 			*dir = -EOPNOTSUPP;
3078 			return NULL;
3079 		}
3080 		break;
3081 #endif
3082 	default:
3083 		*dir = -EINVAL;
3084 		return NULL;
3085 	}
3086 
3087 	*dir = -EINVAL;
3088 
3089 	if (len < sizeof(*p) ||
3090 	    verify_newpolicy_info(p))
3091 		return NULL;
3092 
3093 	nr = ((len - sizeof(*p)) / sizeof(*ut));
3094 	if (validate_tmpl(nr, ut, p->sel.family))
3095 		return NULL;
3096 
3097 	if (p->dir > XFRM_POLICY_OUT)
3098 		return NULL;
3099 
3100 	xp = xfrm_policy_alloc(net, GFP_ATOMIC);
3101 	if (xp == NULL) {
3102 		*dir = -ENOBUFS;
3103 		return NULL;
3104 	}
3105 
3106 	copy_from_user_policy(xp, p);
3107 	xp->type = XFRM_POLICY_TYPE_MAIN;
3108 	copy_templates(xp, ut, nr);
3109 
3110 	*dir = p->dir;
3111 
3112 	return xp;
3113 }
3114 
3115 static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
3116 {
3117 	return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
3118 	       + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
3119 	       + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
3120 	       + nla_total_size(sizeof(struct xfrm_mark))
3121 	       + userpolicy_type_attrsize();
3122 }
3123 
3124 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
3125 			   int dir, const struct km_event *c)
3126 {
3127 	struct xfrm_user_polexpire *upe;
3128 	int hard = c->data.hard;
3129 	struct nlmsghdr *nlh;
3130 	int err;
3131 
3132 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
3133 	if (nlh == NULL)
3134 		return -EMSGSIZE;
3135 
3136 	upe = nlmsg_data(nlh);
3137 	copy_to_user_policy(xp, &upe->pol, dir);
3138 	err = copy_to_user_tmpl(xp, skb);
3139 	if (!err)
3140 		err = copy_to_user_sec_ctx(xp, skb);
3141 	if (!err)
3142 		err = copy_to_user_policy_type(xp->type, skb);
3143 	if (!err)
3144 		err = xfrm_mark_put(skb, &xp->mark);
3145 	if (!err)
3146 		err = xfrm_if_id_put(skb, xp->if_id);
3147 	if (err) {
3148 		nlmsg_cancel(skb, nlh);
3149 		return err;
3150 	}
3151 	upe->hard = !!hard;
3152 
3153 	nlmsg_end(skb, nlh);
3154 	return 0;
3155 }
3156 
3157 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3158 {
3159 	struct net *net = xp_net(xp);
3160 	struct sk_buff *skb;
3161 	int err;
3162 
3163 	skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3164 	if (skb == NULL)
3165 		return -ENOMEM;
3166 
3167 	err = build_polexpire(skb, xp, dir, c);
3168 	BUG_ON(err < 0);
3169 
3170 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3171 }
3172 
3173 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3174 {
3175 	unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3176 	struct net *net = xp_net(xp);
3177 	struct xfrm_userpolicy_info *p;
3178 	struct xfrm_userpolicy_id *id;
3179 	struct nlmsghdr *nlh;
3180 	struct sk_buff *skb;
3181 	unsigned int headlen;
3182 	int err;
3183 
3184 	headlen = sizeof(*p);
3185 	if (c->event == XFRM_MSG_DELPOLICY) {
3186 		len += nla_total_size(headlen);
3187 		headlen = sizeof(*id);
3188 	}
3189 	len += userpolicy_type_attrsize();
3190 	len += nla_total_size(sizeof(struct xfrm_mark));
3191 	len += NLMSG_ALIGN(headlen);
3192 
3193 	skb = nlmsg_new(len, GFP_ATOMIC);
3194 	if (skb == NULL)
3195 		return -ENOMEM;
3196 
3197 	nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3198 	err = -EMSGSIZE;
3199 	if (nlh == NULL)
3200 		goto out_free_skb;
3201 
3202 	p = nlmsg_data(nlh);
3203 	if (c->event == XFRM_MSG_DELPOLICY) {
3204 		struct nlattr *attr;
3205 
3206 		id = nlmsg_data(nlh);
3207 		memset(id, 0, sizeof(*id));
3208 		id->dir = dir;
3209 		if (c->data.byid)
3210 			id->index = xp->index;
3211 		else
3212 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3213 
3214 		attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3215 		err = -EMSGSIZE;
3216 		if (attr == NULL)
3217 			goto out_free_skb;
3218 
3219 		p = nla_data(attr);
3220 	}
3221 
3222 	copy_to_user_policy(xp, p, dir);
3223 	err = copy_to_user_tmpl(xp, skb);
3224 	if (!err)
3225 		err = copy_to_user_policy_type(xp->type, skb);
3226 	if (!err)
3227 		err = xfrm_mark_put(skb, &xp->mark);
3228 	if (!err)
3229 		err = xfrm_if_id_put(skb, xp->if_id);
3230 	if (err)
3231 		goto out_free_skb;
3232 
3233 	nlmsg_end(skb, nlh);
3234 
3235 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3236 
3237 out_free_skb:
3238 	kfree_skb(skb);
3239 	return err;
3240 }
3241 
3242 static int xfrm_notify_policy_flush(const struct km_event *c)
3243 {
3244 	struct net *net = c->net;
3245 	struct nlmsghdr *nlh;
3246 	struct sk_buff *skb;
3247 	int err;
3248 
3249 	skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3250 	if (skb == NULL)
3251 		return -ENOMEM;
3252 
3253 	nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3254 	err = -EMSGSIZE;
3255 	if (nlh == NULL)
3256 		goto out_free_skb;
3257 	err = copy_to_user_policy_type(c->data.type, skb);
3258 	if (err)
3259 		goto out_free_skb;
3260 
3261 	nlmsg_end(skb, nlh);
3262 
3263 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3264 
3265 out_free_skb:
3266 	kfree_skb(skb);
3267 	return err;
3268 }
3269 
3270 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3271 {
3272 
3273 	switch (c->event) {
3274 	case XFRM_MSG_NEWPOLICY:
3275 	case XFRM_MSG_UPDPOLICY:
3276 	case XFRM_MSG_DELPOLICY:
3277 		return xfrm_notify_policy(xp, dir, c);
3278 	case XFRM_MSG_FLUSHPOLICY:
3279 		return xfrm_notify_policy_flush(c);
3280 	case XFRM_MSG_POLEXPIRE:
3281 		return xfrm_exp_policy_notify(xp, dir, c);
3282 	default:
3283 		printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3284 		       c->event);
3285 	}
3286 
3287 	return 0;
3288 
3289 }
3290 
3291 static inline unsigned int xfrm_report_msgsize(void)
3292 {
3293 	return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3294 }
3295 
3296 static int build_report(struct sk_buff *skb, u8 proto,
3297 			struct xfrm_selector *sel, xfrm_address_t *addr)
3298 {
3299 	struct xfrm_user_report *ur;
3300 	struct nlmsghdr *nlh;
3301 
3302 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3303 	if (nlh == NULL)
3304 		return -EMSGSIZE;
3305 
3306 	ur = nlmsg_data(nlh);
3307 	ur->proto = proto;
3308 	memcpy(&ur->sel, sel, sizeof(ur->sel));
3309 
3310 	if (addr) {
3311 		int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3312 		if (err) {
3313 			nlmsg_cancel(skb, nlh);
3314 			return err;
3315 		}
3316 	}
3317 	nlmsg_end(skb, nlh);
3318 	return 0;
3319 }
3320 
3321 static int xfrm_send_report(struct net *net, u8 proto,
3322 			    struct xfrm_selector *sel, xfrm_address_t *addr)
3323 {
3324 	struct sk_buff *skb;
3325 	int err;
3326 
3327 	skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3328 	if (skb == NULL)
3329 		return -ENOMEM;
3330 
3331 	err = build_report(skb, proto, sel, addr);
3332 	BUG_ON(err < 0);
3333 
3334 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3335 }
3336 
3337 static inline unsigned int xfrm_mapping_msgsize(void)
3338 {
3339 	return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3340 }
3341 
3342 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3343 			 xfrm_address_t *new_saddr, __be16 new_sport)
3344 {
3345 	struct xfrm_user_mapping *um;
3346 	struct nlmsghdr *nlh;
3347 
3348 	nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3349 	if (nlh == NULL)
3350 		return -EMSGSIZE;
3351 
3352 	um = nlmsg_data(nlh);
3353 
3354 	memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3355 	um->id.spi = x->id.spi;
3356 	um->id.family = x->props.family;
3357 	um->id.proto = x->id.proto;
3358 	memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3359 	memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3360 	um->new_sport = new_sport;
3361 	um->old_sport = x->encap->encap_sport;
3362 	um->reqid = x->props.reqid;
3363 
3364 	nlmsg_end(skb, nlh);
3365 	return 0;
3366 }
3367 
3368 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3369 			     __be16 sport)
3370 {
3371 	struct net *net = xs_net(x);
3372 	struct sk_buff *skb;
3373 	int err;
3374 
3375 	if (x->id.proto != IPPROTO_ESP)
3376 		return -EINVAL;
3377 
3378 	if (!x->encap)
3379 		return -EINVAL;
3380 
3381 	skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3382 	if (skb == NULL)
3383 		return -ENOMEM;
3384 
3385 	err = build_mapping(skb, x, ipaddr, sport);
3386 	BUG_ON(err < 0);
3387 
3388 	return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3389 }
3390 
3391 static bool xfrm_is_alive(const struct km_event *c)
3392 {
3393 	return (bool)xfrm_acquire_is_on(c->net);
3394 }
3395 
3396 static struct xfrm_mgr netlink_mgr = {
3397 	.notify		= xfrm_send_state_notify,
3398 	.acquire	= xfrm_send_acquire,
3399 	.compile_policy	= xfrm_compile_policy,
3400 	.notify_policy	= xfrm_send_policy_notify,
3401 	.report		= xfrm_send_report,
3402 	.migrate	= xfrm_send_migrate,
3403 	.new_mapping	= xfrm_send_mapping,
3404 	.is_alive	= xfrm_is_alive,
3405 };
3406 
3407 static int __net_init xfrm_user_net_init(struct net *net)
3408 {
3409 	struct sock *nlsk;
3410 	struct netlink_kernel_cfg cfg = {
3411 		.groups	= XFRMNLGRP_MAX,
3412 		.input	= xfrm_netlink_rcv,
3413 	};
3414 
3415 	nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3416 	if (nlsk == NULL)
3417 		return -ENOMEM;
3418 	net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3419 	rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3420 	return 0;
3421 }
3422 
3423 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3424 {
3425 	struct net *net;
3426 	list_for_each_entry(net, net_exit_list, exit_list)
3427 		RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3428 	synchronize_net();
3429 	list_for_each_entry(net, net_exit_list, exit_list)
3430 		netlink_kernel_release(net->xfrm.nlsk_stash);
3431 }
3432 
3433 static struct pernet_operations xfrm_user_net_ops = {
3434 	.init	    = xfrm_user_net_init,
3435 	.exit_batch = xfrm_user_net_exit,
3436 };
3437 
3438 static int __init xfrm_user_init(void)
3439 {
3440 	int rv;
3441 
3442 	printk(KERN_INFO "Initializing XFRM netlink socket\n");
3443 
3444 	rv = register_pernet_subsys(&xfrm_user_net_ops);
3445 	if (rv < 0)
3446 		return rv;
3447 	rv = xfrm_register_km(&netlink_mgr);
3448 	if (rv < 0)
3449 		unregister_pernet_subsys(&xfrm_user_net_ops);
3450 	return rv;
3451 }
3452 
3453 static void __exit xfrm_user_exit(void)
3454 {
3455 	xfrm_unregister_km(&netlink_mgr);
3456 	unregister_pernet_subsys(&xfrm_user_net_ops);
3457 }
3458 
3459 module_init(xfrm_user_init);
3460 module_exit(xfrm_user_exit);
3461 MODULE_LICENSE("GPL");
3462 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3463