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