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