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