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