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