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