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