Linux 4.14.168
[linux/fpc-iii.git] / net / xfrm / xfrm_user.c
blob339a070da597879e33dc041f9b71e9fcd16336b6
1 /* xfrm_user.c: User interface to configure xfrm engine.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
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 <linux/uaccess.h>
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <linux/in6.h>
33 #endif
34 #include <asm/unaligned.h>
36 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
38 struct nlattr *rt = attrs[type];
39 struct xfrm_algo *algp;
41 if (!rt)
42 return 0;
44 algp = nla_data(rt);
45 if (nla_len(rt) < xfrm_alg_len(algp))
46 return -EINVAL;
48 switch (type) {
49 case XFRMA_ALG_AUTH:
50 case XFRMA_ALG_CRYPT:
51 case XFRMA_ALG_COMP:
52 break;
54 default:
55 return -EINVAL;
58 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
59 return 0;
62 static int verify_auth_trunc(struct nlattr **attrs)
64 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
65 struct xfrm_algo_auth *algp;
67 if (!rt)
68 return 0;
70 algp = nla_data(rt);
71 if (nla_len(rt) < xfrm_alg_auth_len(algp))
72 return -EINVAL;
74 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
75 return 0;
78 static int verify_aead(struct nlattr **attrs)
80 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
81 struct xfrm_algo_aead *algp;
83 if (!rt)
84 return 0;
86 algp = nla_data(rt);
87 if (nla_len(rt) < aead_len(algp))
88 return -EINVAL;
90 algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
91 return 0;
94 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
95 xfrm_address_t **addrp)
97 struct nlattr *rt = attrs[type];
99 if (rt && addrp)
100 *addrp = nla_data(rt);
103 static inline int verify_sec_ctx_len(struct nlattr **attrs)
105 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
106 struct xfrm_user_sec_ctx *uctx;
108 if (!rt)
109 return 0;
111 uctx = nla_data(rt);
112 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
113 return -EINVAL;
115 return 0;
118 static inline int verify_replay(struct xfrm_usersa_info *p,
119 struct nlattr **attrs)
121 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
122 struct xfrm_replay_state_esn *rs;
124 if (!rt)
125 return (p->flags & XFRM_STATE_ESN) ? -EINVAL : 0;
127 rs = nla_data(rt);
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
136 /* As only ESP and AH support ESN feature. */
137 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
138 return -EINVAL;
140 if (p->replay_window != 0)
141 return -EINVAL;
143 return 0;
146 static int verify_newsa_info(struct xfrm_usersa_info *p,
147 struct nlattr **attrs)
149 int err;
151 err = -EINVAL;
152 switch (p->family) {
153 case AF_INET:
154 break;
156 case AF_INET6:
157 #if IS_ENABLED(CONFIG_IPV6)
158 break;
159 #else
160 err = -EAFNOSUPPORT;
161 goto out;
162 #endif
164 default:
165 goto out;
168 switch (p->sel.family) {
169 case AF_UNSPEC:
170 break;
172 case AF_INET:
173 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
174 goto out;
176 break;
178 case AF_INET6:
179 #if IS_ENABLED(CONFIG_IPV6)
180 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
181 goto out;
183 break;
184 #else
185 err = -EAFNOSUPPORT;
186 goto out;
187 #endif
189 default:
190 goto out;
193 err = -EINVAL;
194 switch (p->id.proto) {
195 case IPPROTO_AH:
196 if ((!attrs[XFRMA_ALG_AUTH] &&
197 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
198 attrs[XFRMA_ALG_AEAD] ||
199 attrs[XFRMA_ALG_CRYPT] ||
200 attrs[XFRMA_ALG_COMP] ||
201 attrs[XFRMA_TFCPAD])
202 goto out;
203 break;
205 case IPPROTO_ESP:
206 if (attrs[XFRMA_ALG_COMP])
207 goto out;
208 if (!attrs[XFRMA_ALG_AUTH] &&
209 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
210 !attrs[XFRMA_ALG_CRYPT] &&
211 !attrs[XFRMA_ALG_AEAD])
212 goto out;
213 if ((attrs[XFRMA_ALG_AUTH] ||
214 attrs[XFRMA_ALG_AUTH_TRUNC] ||
215 attrs[XFRMA_ALG_CRYPT]) &&
216 attrs[XFRMA_ALG_AEAD])
217 goto out;
218 if (attrs[XFRMA_TFCPAD] &&
219 p->mode != XFRM_MODE_TUNNEL)
220 goto out;
221 break;
223 case IPPROTO_COMP:
224 if (!attrs[XFRMA_ALG_COMP] ||
225 attrs[XFRMA_ALG_AEAD] ||
226 attrs[XFRMA_ALG_AUTH] ||
227 attrs[XFRMA_ALG_AUTH_TRUNC] ||
228 attrs[XFRMA_ALG_CRYPT] ||
229 attrs[XFRMA_TFCPAD] ||
230 (ntohl(p->id.spi) >= 0x10000))
231 goto out;
232 break;
234 #if IS_ENABLED(CONFIG_IPV6)
235 case IPPROTO_DSTOPTS:
236 case IPPROTO_ROUTING:
237 if (attrs[XFRMA_ALG_COMP] ||
238 attrs[XFRMA_ALG_AUTH] ||
239 attrs[XFRMA_ALG_AUTH_TRUNC] ||
240 attrs[XFRMA_ALG_AEAD] ||
241 attrs[XFRMA_ALG_CRYPT] ||
242 attrs[XFRMA_ENCAP] ||
243 attrs[XFRMA_SEC_CTX] ||
244 attrs[XFRMA_TFCPAD] ||
245 !attrs[XFRMA_COADDR])
246 goto out;
247 break;
248 #endif
250 default:
251 goto out;
254 if ((err = verify_aead(attrs)))
255 goto out;
256 if ((err = verify_auth_trunc(attrs)))
257 goto out;
258 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
259 goto out;
260 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
261 goto out;
262 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
263 goto out;
264 if ((err = verify_sec_ctx_len(attrs)))
265 goto out;
266 if ((err = verify_replay(p, attrs)))
267 goto out;
269 err = -EINVAL;
270 switch (p->mode) {
271 case XFRM_MODE_TRANSPORT:
272 case XFRM_MODE_TUNNEL:
273 case XFRM_MODE_ROUTEOPTIMIZATION:
274 case XFRM_MODE_BEET:
275 break;
277 default:
278 goto out;
281 err = 0;
283 out:
284 return err;
287 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
288 struct xfrm_algo_desc *(*get_byname)(const char *, int),
289 struct nlattr *rta)
291 struct xfrm_algo *p, *ualg;
292 struct xfrm_algo_desc *algo;
294 if (!rta)
295 return 0;
297 ualg = nla_data(rta);
299 algo = get_byname(ualg->alg_name, 1);
300 if (!algo)
301 return -ENOSYS;
302 *props = algo->desc.sadb_alg_id;
304 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
305 if (!p)
306 return -ENOMEM;
308 strcpy(p->alg_name, algo->name);
309 *algpp = p;
310 return 0;
313 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
315 struct xfrm_algo *p, *ualg;
316 struct xfrm_algo_desc *algo;
318 if (!rta)
319 return 0;
321 ualg = nla_data(rta);
323 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
324 if (!algo)
325 return -ENOSYS;
326 x->props.ealgo = algo->desc.sadb_alg_id;
328 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
329 if (!p)
330 return -ENOMEM;
332 strcpy(p->alg_name, algo->name);
333 x->ealg = p;
334 x->geniv = algo->uinfo.encr.geniv;
335 return 0;
338 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
339 struct nlattr *rta)
341 struct xfrm_algo *ualg;
342 struct xfrm_algo_auth *p;
343 struct xfrm_algo_desc *algo;
345 if (!rta)
346 return 0;
348 ualg = nla_data(rta);
350 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
351 if (!algo)
352 return -ENOSYS;
353 *props = algo->desc.sadb_alg_id;
355 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
356 if (!p)
357 return -ENOMEM;
359 strcpy(p->alg_name, algo->name);
360 p->alg_key_len = ualg->alg_key_len;
361 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
362 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
364 *algpp = p;
365 return 0;
368 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
369 struct nlattr *rta)
371 struct xfrm_algo_auth *p, *ualg;
372 struct xfrm_algo_desc *algo;
374 if (!rta)
375 return 0;
377 ualg = nla_data(rta);
379 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
380 if (!algo)
381 return -ENOSYS;
382 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
383 return -EINVAL;
384 *props = algo->desc.sadb_alg_id;
386 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
387 if (!p)
388 return -ENOMEM;
390 strcpy(p->alg_name, algo->name);
391 if (!p->alg_trunc_len)
392 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
394 *algpp = p;
395 return 0;
398 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
400 struct xfrm_algo_aead *p, *ualg;
401 struct xfrm_algo_desc *algo;
403 if (!rta)
404 return 0;
406 ualg = nla_data(rta);
408 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
409 if (!algo)
410 return -ENOSYS;
411 x->props.ealgo = algo->desc.sadb_alg_id;
413 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
414 if (!p)
415 return -ENOMEM;
417 strcpy(p->alg_name, algo->name);
418 x->aead = p;
419 x->geniv = algo->uinfo.aead.geniv;
420 return 0;
423 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
424 struct nlattr *rp)
426 struct xfrm_replay_state_esn *up;
427 int ulen;
429 if (!replay_esn || !rp)
430 return 0;
432 up = nla_data(rp);
433 ulen = xfrm_replay_state_esn_len(up);
435 /* Check the overall length and the internal bitmap length to avoid
436 * potential overflow. */
437 if (nla_len(rp) < ulen ||
438 xfrm_replay_state_esn_len(replay_esn) != ulen ||
439 replay_esn->bmp_len != up->bmp_len)
440 return -EINVAL;
442 if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
443 return -EINVAL;
445 return 0;
448 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
449 struct xfrm_replay_state_esn **preplay_esn,
450 struct nlattr *rta)
452 struct xfrm_replay_state_esn *p, *pp, *up;
453 int klen, ulen;
455 if (!rta)
456 return 0;
458 up = nla_data(rta);
459 klen = xfrm_replay_state_esn_len(up);
460 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
462 p = kzalloc(klen, GFP_KERNEL);
463 if (!p)
464 return -ENOMEM;
466 pp = kzalloc(klen, GFP_KERNEL);
467 if (!pp) {
468 kfree(p);
469 return -ENOMEM;
472 memcpy(p, up, ulen);
473 memcpy(pp, up, ulen);
475 *replay_esn = p;
476 *preplay_esn = pp;
478 return 0;
481 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
483 int len = 0;
485 if (xfrm_ctx) {
486 len += sizeof(struct xfrm_user_sec_ctx);
487 len += xfrm_ctx->ctx_len;
489 return len;
492 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
494 memcpy(&x->id, &p->id, sizeof(x->id));
495 memcpy(&x->sel, &p->sel, sizeof(x->sel));
496 memcpy(&x->lft, &p->lft, sizeof(x->lft));
497 x->props.mode = p->mode;
498 x->props.replay_window = min_t(unsigned int, p->replay_window,
499 sizeof(x->replay.bitmap) * 8);
500 x->props.reqid = p->reqid;
501 x->props.family = p->family;
502 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
503 x->props.flags = p->flags;
505 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
506 x->sel.family = p->family;
510 * someday when pfkey also has support, we could have the code
511 * somehow made shareable and move it to xfrm_state.c - JHS
514 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
515 int update_esn)
517 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
518 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
519 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
520 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
521 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
523 if (re) {
524 struct xfrm_replay_state_esn *replay_esn;
525 replay_esn = nla_data(re);
526 memcpy(x->replay_esn, replay_esn,
527 xfrm_replay_state_esn_len(replay_esn));
528 memcpy(x->preplay_esn, replay_esn,
529 xfrm_replay_state_esn_len(replay_esn));
532 if (rp) {
533 struct xfrm_replay_state *replay;
534 replay = nla_data(rp);
535 memcpy(&x->replay, replay, sizeof(*replay));
536 memcpy(&x->preplay, replay, sizeof(*replay));
539 if (lt) {
540 struct xfrm_lifetime_cur *ltime;
541 ltime = nla_data(lt);
542 x->curlft.bytes = ltime->bytes;
543 x->curlft.packets = ltime->packets;
544 x->curlft.add_time = ltime->add_time;
545 x->curlft.use_time = ltime->use_time;
548 if (et)
549 x->replay_maxage = nla_get_u32(et);
551 if (rt)
552 x->replay_maxdiff = nla_get_u32(rt);
555 static struct xfrm_state *xfrm_state_construct(struct net *net,
556 struct xfrm_usersa_info *p,
557 struct nlattr **attrs,
558 int *errp)
560 struct xfrm_state *x = xfrm_state_alloc(net);
561 int err = -ENOMEM;
563 if (!x)
564 goto error_no_put;
566 copy_from_user_state(x, p);
568 if (attrs[XFRMA_SA_EXTRA_FLAGS])
569 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
571 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
572 goto error;
573 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
574 attrs[XFRMA_ALG_AUTH_TRUNC])))
575 goto error;
576 if (!x->props.aalgo) {
577 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
578 attrs[XFRMA_ALG_AUTH])))
579 goto error;
581 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
582 goto error;
583 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
584 xfrm_calg_get_byname,
585 attrs[XFRMA_ALG_COMP])))
586 goto error;
588 if (attrs[XFRMA_ENCAP]) {
589 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
590 sizeof(*x->encap), GFP_KERNEL);
591 if (x->encap == NULL)
592 goto error;
595 if (attrs[XFRMA_TFCPAD])
596 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
598 if (attrs[XFRMA_COADDR]) {
599 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
600 sizeof(*x->coaddr), GFP_KERNEL);
601 if (x->coaddr == NULL)
602 goto error;
605 xfrm_mark_get(attrs, &x->mark);
607 if (attrs[XFRMA_OUTPUT_MARK])
608 x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
610 err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
611 if (err)
612 goto error;
614 if (attrs[XFRMA_SEC_CTX]) {
615 err = security_xfrm_state_alloc(x,
616 nla_data(attrs[XFRMA_SEC_CTX]));
617 if (err)
618 goto error;
621 if (attrs[XFRMA_OFFLOAD_DEV]) {
622 err = xfrm_dev_state_add(net, x,
623 nla_data(attrs[XFRMA_OFFLOAD_DEV]));
624 if (err)
625 goto error;
628 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
629 attrs[XFRMA_REPLAY_ESN_VAL])))
630 goto error;
632 x->km.seq = p->seq;
633 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
634 /* sysctl_xfrm_aevent_etime is in 100ms units */
635 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
637 if ((err = xfrm_init_replay(x)))
638 goto error;
640 /* override default values from above */
641 xfrm_update_ae_params(x, attrs, 0);
643 return x;
645 error:
646 x->km.state = XFRM_STATE_DEAD;
647 xfrm_state_put(x);
648 error_no_put:
649 *errp = err;
650 return NULL;
653 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
654 struct nlattr **attrs)
656 struct net *net = sock_net(skb->sk);
657 struct xfrm_usersa_info *p = nlmsg_data(nlh);
658 struct xfrm_state *x;
659 int err;
660 struct km_event c;
662 err = verify_newsa_info(p, attrs);
663 if (err)
664 return err;
666 x = xfrm_state_construct(net, p, attrs, &err);
667 if (!x)
668 return err;
670 xfrm_state_hold(x);
671 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
672 err = xfrm_state_add(x);
673 else
674 err = xfrm_state_update(x);
676 xfrm_audit_state_add(x, err ? 0 : 1, true);
678 if (err < 0) {
679 x->km.state = XFRM_STATE_DEAD;
680 xfrm_dev_state_delete(x);
681 __xfrm_state_put(x);
682 goto out;
685 c.seq = nlh->nlmsg_seq;
686 c.portid = nlh->nlmsg_pid;
687 c.event = nlh->nlmsg_type;
689 km_state_notify(x, &c);
690 out:
691 xfrm_state_put(x);
692 return err;
695 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
696 struct xfrm_usersa_id *p,
697 struct nlattr **attrs,
698 int *errp)
700 struct xfrm_state *x = NULL;
701 struct xfrm_mark m;
702 int err;
703 u32 mark = xfrm_mark_get(attrs, &m);
705 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
706 err = -ESRCH;
707 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
708 } else {
709 xfrm_address_t *saddr = NULL;
711 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
712 if (!saddr) {
713 err = -EINVAL;
714 goto out;
717 err = -ESRCH;
718 x = xfrm_state_lookup_byaddr(net, mark,
719 &p->daddr, saddr,
720 p->proto, p->family);
723 out:
724 if (!x && errp)
725 *errp = err;
726 return x;
729 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
730 struct nlattr **attrs)
732 struct net *net = sock_net(skb->sk);
733 struct xfrm_state *x;
734 int err = -ESRCH;
735 struct km_event c;
736 struct xfrm_usersa_id *p = nlmsg_data(nlh);
738 x = xfrm_user_state_lookup(net, p, attrs, &err);
739 if (x == NULL)
740 return err;
742 if ((err = security_xfrm_state_delete(x)) != 0)
743 goto out;
745 if (xfrm_state_kern(x)) {
746 err = -EPERM;
747 goto out;
750 err = xfrm_state_delete(x);
752 if (err < 0)
753 goto out;
755 c.seq = nlh->nlmsg_seq;
756 c.portid = nlh->nlmsg_pid;
757 c.event = nlh->nlmsg_type;
758 km_state_notify(x, &c);
760 out:
761 xfrm_audit_state_delete(x, err ? 0 : 1, true);
762 xfrm_state_put(x);
763 return err;
766 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
768 memset(p, 0, sizeof(*p));
769 memcpy(&p->id, &x->id, sizeof(p->id));
770 memcpy(&p->sel, &x->sel, sizeof(p->sel));
771 memcpy(&p->lft, &x->lft, sizeof(p->lft));
772 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
773 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
774 put_unaligned(x->stats.replay, &p->stats.replay);
775 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
776 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
777 p->mode = x->props.mode;
778 p->replay_window = x->props.replay_window;
779 p->reqid = x->props.reqid;
780 p->family = x->props.family;
781 p->flags = x->props.flags;
782 p->seq = x->km.seq;
785 struct xfrm_dump_info {
786 struct sk_buff *in_skb;
787 struct sk_buff *out_skb;
788 u32 nlmsg_seq;
789 u16 nlmsg_flags;
792 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
794 struct xfrm_user_sec_ctx *uctx;
795 struct nlattr *attr;
796 int ctx_size = sizeof(*uctx) + s->ctx_len;
798 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
799 if (attr == NULL)
800 return -EMSGSIZE;
802 uctx = nla_data(attr);
803 uctx->exttype = XFRMA_SEC_CTX;
804 uctx->len = ctx_size;
805 uctx->ctx_doi = s->ctx_doi;
806 uctx->ctx_alg = s->ctx_alg;
807 uctx->ctx_len = s->ctx_len;
808 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
810 return 0;
813 static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
815 struct xfrm_user_offload *xuo;
816 struct nlattr *attr;
818 attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
819 if (attr == NULL)
820 return -EMSGSIZE;
822 xuo = nla_data(attr);
823 memset(xuo, 0, sizeof(*xuo));
824 xuo->ifindex = xso->dev->ifindex;
825 xuo->flags = xso->flags;
827 return 0;
830 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
832 struct xfrm_algo *algo;
833 struct nlattr *nla;
835 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
836 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
837 if (!nla)
838 return -EMSGSIZE;
840 algo = nla_data(nla);
841 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
842 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
843 algo->alg_key_len = auth->alg_key_len;
845 return 0;
848 /* Don't change this without updating xfrm_sa_len! */
849 static int copy_to_user_state_extra(struct xfrm_state *x,
850 struct xfrm_usersa_info *p,
851 struct sk_buff *skb)
853 int ret = 0;
855 copy_to_user_state(x, p);
857 if (x->props.extra_flags) {
858 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
859 x->props.extra_flags);
860 if (ret)
861 goto out;
864 if (x->coaddr) {
865 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
866 if (ret)
867 goto out;
869 if (x->lastused) {
870 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
871 XFRMA_PAD);
872 if (ret)
873 goto out;
875 if (x->aead) {
876 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
877 if (ret)
878 goto out;
880 if (x->aalg) {
881 ret = copy_to_user_auth(x->aalg, skb);
882 if (!ret)
883 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
884 xfrm_alg_auth_len(x->aalg), x->aalg);
885 if (ret)
886 goto out;
888 if (x->ealg) {
889 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
890 if (ret)
891 goto out;
893 if (x->calg) {
894 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
895 if (ret)
896 goto out;
898 if (x->encap) {
899 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
900 if (ret)
901 goto out;
903 if (x->tfcpad) {
904 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
905 if (ret)
906 goto out;
908 ret = xfrm_mark_put(skb, &x->mark);
909 if (ret)
910 goto out;
911 if (x->replay_esn)
912 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
913 xfrm_replay_state_esn_len(x->replay_esn),
914 x->replay_esn);
915 else
916 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
917 &x->replay);
918 if (ret)
919 goto out;
920 if(x->xso.dev)
921 ret = copy_user_offload(&x->xso, skb);
922 if (ret)
923 goto out;
924 if (x->props.output_mark) {
925 ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
926 if (ret)
927 goto out;
929 if (x->security)
930 ret = copy_sec_ctx(x->security, skb);
931 out:
932 return ret;
935 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
937 struct xfrm_dump_info *sp = ptr;
938 struct sk_buff *in_skb = sp->in_skb;
939 struct sk_buff *skb = sp->out_skb;
940 struct xfrm_usersa_info *p;
941 struct nlmsghdr *nlh;
942 int err;
944 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
945 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
946 if (nlh == NULL)
947 return -EMSGSIZE;
949 p = nlmsg_data(nlh);
951 err = copy_to_user_state_extra(x, p, skb);
952 if (err) {
953 nlmsg_cancel(skb, nlh);
954 return err;
956 nlmsg_end(skb, nlh);
957 return 0;
960 static int xfrm_dump_sa_done(struct netlink_callback *cb)
962 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
963 struct sock *sk = cb->skb->sk;
964 struct net *net = sock_net(sk);
966 if (cb->args[0])
967 xfrm_state_walk_done(walk, net);
968 return 0;
971 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
972 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
974 struct net *net = sock_net(skb->sk);
975 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
976 struct xfrm_dump_info info;
978 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
979 sizeof(cb->args) - sizeof(cb->args[0]));
981 info.in_skb = cb->skb;
982 info.out_skb = skb;
983 info.nlmsg_seq = cb->nlh->nlmsg_seq;
984 info.nlmsg_flags = NLM_F_MULTI;
986 if (!cb->args[0]) {
987 struct nlattr *attrs[XFRMA_MAX+1];
988 struct xfrm_address_filter *filter = NULL;
989 u8 proto = 0;
990 int err;
992 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
993 NULL);
994 if (err < 0)
995 return err;
997 if (attrs[XFRMA_ADDRESS_FILTER]) {
998 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
999 sizeof(*filter), GFP_KERNEL);
1000 if (filter == NULL)
1001 return -ENOMEM;
1004 if (attrs[XFRMA_PROTO])
1005 proto = nla_get_u8(attrs[XFRMA_PROTO]);
1007 xfrm_state_walk_init(walk, proto, filter);
1008 cb->args[0] = 1;
1011 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1013 return skb->len;
1016 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1017 struct xfrm_state *x, u32 seq)
1019 struct xfrm_dump_info info;
1020 struct sk_buff *skb;
1021 int err;
1023 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1024 if (!skb)
1025 return ERR_PTR(-ENOMEM);
1027 info.in_skb = in_skb;
1028 info.out_skb = skb;
1029 info.nlmsg_seq = seq;
1030 info.nlmsg_flags = 0;
1032 err = dump_one_state(x, 0, &info);
1033 if (err) {
1034 kfree_skb(skb);
1035 return ERR_PTR(err);
1038 return skb;
1041 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1042 * Must be called with RCU read lock.
1044 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1045 u32 pid, unsigned int group)
1047 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1049 if (!nlsk) {
1050 kfree_skb(skb);
1051 return -EPIPE;
1054 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1057 static inline size_t xfrm_spdinfo_msgsize(void)
1059 return NLMSG_ALIGN(4)
1060 + nla_total_size(sizeof(struct xfrmu_spdinfo))
1061 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1062 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1063 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1066 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1067 u32 portid, u32 seq, u32 flags)
1069 struct xfrmk_spdinfo si;
1070 struct xfrmu_spdinfo spc;
1071 struct xfrmu_spdhinfo sph;
1072 struct xfrmu_spdhthresh spt4, spt6;
1073 struct nlmsghdr *nlh;
1074 int err;
1075 u32 *f;
1076 unsigned lseq;
1078 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1079 if (nlh == NULL) /* shouldn't really happen ... */
1080 return -EMSGSIZE;
1082 f = nlmsg_data(nlh);
1083 *f = flags;
1084 xfrm_spd_getinfo(net, &si);
1085 spc.incnt = si.incnt;
1086 spc.outcnt = si.outcnt;
1087 spc.fwdcnt = si.fwdcnt;
1088 spc.inscnt = si.inscnt;
1089 spc.outscnt = si.outscnt;
1090 spc.fwdscnt = si.fwdscnt;
1091 sph.spdhcnt = si.spdhcnt;
1092 sph.spdhmcnt = si.spdhmcnt;
1094 do {
1095 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1097 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1098 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1099 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1100 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1101 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1103 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1104 if (!err)
1105 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1106 if (!err)
1107 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1108 if (!err)
1109 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1110 if (err) {
1111 nlmsg_cancel(skb, nlh);
1112 return err;
1115 nlmsg_end(skb, nlh);
1116 return 0;
1119 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1120 struct nlattr **attrs)
1122 struct net *net = sock_net(skb->sk);
1123 struct xfrmu_spdhthresh *thresh4 = NULL;
1124 struct xfrmu_spdhthresh *thresh6 = NULL;
1126 /* selector prefixlen thresholds to hash policies */
1127 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1128 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1130 if (nla_len(rta) < sizeof(*thresh4))
1131 return -EINVAL;
1132 thresh4 = nla_data(rta);
1133 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1134 return -EINVAL;
1136 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1137 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1139 if (nla_len(rta) < sizeof(*thresh6))
1140 return -EINVAL;
1141 thresh6 = nla_data(rta);
1142 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1143 return -EINVAL;
1146 if (thresh4 || thresh6) {
1147 write_seqlock(&net->xfrm.policy_hthresh.lock);
1148 if (thresh4) {
1149 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1150 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1152 if (thresh6) {
1153 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1154 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1156 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1158 xfrm_policy_hash_rebuild(net);
1161 return 0;
1164 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1165 struct nlattr **attrs)
1167 struct net *net = sock_net(skb->sk);
1168 struct sk_buff *r_skb;
1169 u32 *flags = nlmsg_data(nlh);
1170 u32 sportid = NETLINK_CB(skb).portid;
1171 u32 seq = nlh->nlmsg_seq;
1173 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1174 if (r_skb == NULL)
1175 return -ENOMEM;
1177 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1178 BUG();
1180 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1183 static inline size_t xfrm_sadinfo_msgsize(void)
1185 return NLMSG_ALIGN(4)
1186 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1187 + nla_total_size(4); /* XFRMA_SAD_CNT */
1190 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1191 u32 portid, u32 seq, u32 flags)
1193 struct xfrmk_sadinfo si;
1194 struct xfrmu_sadhinfo sh;
1195 struct nlmsghdr *nlh;
1196 int err;
1197 u32 *f;
1199 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1200 if (nlh == NULL) /* shouldn't really happen ... */
1201 return -EMSGSIZE;
1203 f = nlmsg_data(nlh);
1204 *f = flags;
1205 xfrm_sad_getinfo(net, &si);
1207 sh.sadhmcnt = si.sadhmcnt;
1208 sh.sadhcnt = si.sadhcnt;
1210 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1211 if (!err)
1212 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1213 if (err) {
1214 nlmsg_cancel(skb, nlh);
1215 return err;
1218 nlmsg_end(skb, nlh);
1219 return 0;
1222 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1223 struct nlattr **attrs)
1225 struct net *net = sock_net(skb->sk);
1226 struct sk_buff *r_skb;
1227 u32 *flags = nlmsg_data(nlh);
1228 u32 sportid = NETLINK_CB(skb).portid;
1229 u32 seq = nlh->nlmsg_seq;
1231 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1232 if (r_skb == NULL)
1233 return -ENOMEM;
1235 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1236 BUG();
1238 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1241 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1242 struct nlattr **attrs)
1244 struct net *net = sock_net(skb->sk);
1245 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1246 struct xfrm_state *x;
1247 struct sk_buff *resp_skb;
1248 int err = -ESRCH;
1250 x = xfrm_user_state_lookup(net, p, attrs, &err);
1251 if (x == NULL)
1252 goto out_noput;
1254 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1255 if (IS_ERR(resp_skb)) {
1256 err = PTR_ERR(resp_skb);
1257 } else {
1258 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1260 xfrm_state_put(x);
1261 out_noput:
1262 return err;
1265 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1266 struct nlattr **attrs)
1268 struct net *net = sock_net(skb->sk);
1269 struct xfrm_state *x;
1270 struct xfrm_userspi_info *p;
1271 struct sk_buff *resp_skb;
1272 xfrm_address_t *daddr;
1273 int family;
1274 int err;
1275 u32 mark;
1276 struct xfrm_mark m;
1278 p = nlmsg_data(nlh);
1279 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1280 if (err)
1281 goto out_noput;
1283 family = p->info.family;
1284 daddr = &p->info.id.daddr;
1286 x = NULL;
1288 mark = xfrm_mark_get(attrs, &m);
1289 if (p->info.seq) {
1290 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1291 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1292 xfrm_state_put(x);
1293 x = NULL;
1297 if (!x)
1298 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1299 p->info.id.proto, daddr,
1300 &p->info.saddr, 1,
1301 family);
1302 err = -ENOENT;
1303 if (x == NULL)
1304 goto out_noput;
1306 err = xfrm_alloc_spi(x, p->min, p->max);
1307 if (err)
1308 goto out;
1310 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1311 if (IS_ERR(resp_skb)) {
1312 err = PTR_ERR(resp_skb);
1313 goto out;
1316 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1318 out:
1319 xfrm_state_put(x);
1320 out_noput:
1321 return err;
1324 static int verify_policy_dir(u8 dir)
1326 switch (dir) {
1327 case XFRM_POLICY_IN:
1328 case XFRM_POLICY_OUT:
1329 case XFRM_POLICY_FWD:
1330 break;
1332 default:
1333 return -EINVAL;
1336 return 0;
1339 static int verify_policy_type(u8 type)
1341 switch (type) {
1342 case XFRM_POLICY_TYPE_MAIN:
1343 #ifdef CONFIG_XFRM_SUB_POLICY
1344 case XFRM_POLICY_TYPE_SUB:
1345 #endif
1346 break;
1348 default:
1349 return -EINVAL;
1352 return 0;
1355 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1357 int ret;
1359 switch (p->share) {
1360 case XFRM_SHARE_ANY:
1361 case XFRM_SHARE_SESSION:
1362 case XFRM_SHARE_USER:
1363 case XFRM_SHARE_UNIQUE:
1364 break;
1366 default:
1367 return -EINVAL;
1370 switch (p->action) {
1371 case XFRM_POLICY_ALLOW:
1372 case XFRM_POLICY_BLOCK:
1373 break;
1375 default:
1376 return -EINVAL;
1379 switch (p->sel.family) {
1380 case AF_INET:
1381 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1382 return -EINVAL;
1384 break;
1386 case AF_INET6:
1387 #if IS_ENABLED(CONFIG_IPV6)
1388 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1389 return -EINVAL;
1391 break;
1392 #else
1393 return -EAFNOSUPPORT;
1394 #endif
1396 default:
1397 return -EINVAL;
1400 ret = verify_policy_dir(p->dir);
1401 if (ret)
1402 return ret;
1403 if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1404 return -EINVAL;
1406 return 0;
1409 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1411 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1412 struct xfrm_user_sec_ctx *uctx;
1414 if (!rt)
1415 return 0;
1417 uctx = nla_data(rt);
1418 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1421 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1422 int nr)
1424 int i;
1426 xp->xfrm_nr = nr;
1427 for (i = 0; i < nr; i++, ut++) {
1428 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1430 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1431 memcpy(&t->saddr, &ut->saddr,
1432 sizeof(xfrm_address_t));
1433 t->reqid = ut->reqid;
1434 t->mode = ut->mode;
1435 t->share = ut->share;
1436 t->optional = ut->optional;
1437 t->aalgos = ut->aalgos;
1438 t->ealgos = ut->ealgos;
1439 t->calgos = ut->calgos;
1440 /* If all masks are ~0, then we allow all algorithms. */
1441 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1442 t->encap_family = ut->family;
1446 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1448 u16 prev_family;
1449 int i;
1451 if (nr > XFRM_MAX_DEPTH)
1452 return -EINVAL;
1454 prev_family = family;
1456 for (i = 0; i < nr; i++) {
1457 /* We never validated the ut->family value, so many
1458 * applications simply leave it at zero. The check was
1459 * never made and ut->family was ignored because all
1460 * templates could be assumed to have the same family as
1461 * the policy itself. Now that we will have ipv4-in-ipv6
1462 * and ipv6-in-ipv4 tunnels, this is no longer true.
1464 if (!ut[i].family)
1465 ut[i].family = family;
1467 switch (ut[i].mode) {
1468 case XFRM_MODE_TUNNEL:
1469 case XFRM_MODE_BEET:
1470 break;
1471 default:
1472 if (ut[i].family != prev_family)
1473 return -EINVAL;
1474 break;
1476 if (ut[i].mode >= XFRM_MODE_MAX)
1477 return -EINVAL;
1479 prev_family = ut[i].family;
1481 switch (ut[i].family) {
1482 case AF_INET:
1483 break;
1484 #if IS_ENABLED(CONFIG_IPV6)
1485 case AF_INET6:
1486 break;
1487 #endif
1488 default:
1489 return -EINVAL;
1492 if (!xfrm_id_proto_valid(ut[i].id.proto))
1493 return -EINVAL;
1496 return 0;
1499 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1501 struct nlattr *rt = attrs[XFRMA_TMPL];
1503 if (!rt) {
1504 pol->xfrm_nr = 0;
1505 } else {
1506 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1507 int nr = nla_len(rt) / sizeof(*utmpl);
1508 int err;
1510 err = validate_tmpl(nr, utmpl, pol->family);
1511 if (err)
1512 return err;
1514 copy_templates(pol, utmpl, nr);
1516 return 0;
1519 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1521 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1522 struct xfrm_userpolicy_type *upt;
1523 u8 type = XFRM_POLICY_TYPE_MAIN;
1524 int err;
1526 if (rt) {
1527 upt = nla_data(rt);
1528 type = upt->type;
1531 err = verify_policy_type(type);
1532 if (err)
1533 return err;
1535 *tp = type;
1536 return 0;
1539 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1541 xp->priority = p->priority;
1542 xp->index = p->index;
1543 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1544 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1545 xp->action = p->action;
1546 xp->flags = p->flags;
1547 xp->family = p->sel.family;
1548 /* XXX xp->share = p->share; */
1551 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1553 memset(p, 0, sizeof(*p));
1554 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1555 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1556 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1557 p->priority = xp->priority;
1558 p->index = xp->index;
1559 p->sel.family = xp->family;
1560 p->dir = dir;
1561 p->action = xp->action;
1562 p->flags = xp->flags;
1563 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1566 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1568 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1569 int err;
1571 if (!xp) {
1572 *errp = -ENOMEM;
1573 return NULL;
1576 copy_from_user_policy(xp, p);
1578 err = copy_from_user_policy_type(&xp->type, attrs);
1579 if (err)
1580 goto error;
1582 if (!(err = copy_from_user_tmpl(xp, attrs)))
1583 err = copy_from_user_sec_ctx(xp, attrs);
1584 if (err)
1585 goto error;
1587 xfrm_mark_get(attrs, &xp->mark);
1589 return xp;
1590 error:
1591 *errp = err;
1592 xp->walk.dead = 1;
1593 xfrm_policy_destroy(xp);
1594 return NULL;
1597 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1598 struct nlattr **attrs)
1600 struct net *net = sock_net(skb->sk);
1601 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1602 struct xfrm_policy *xp;
1603 struct km_event c;
1604 int err;
1605 int excl;
1607 err = verify_newpolicy_info(p);
1608 if (err)
1609 return err;
1610 err = verify_sec_ctx_len(attrs);
1611 if (err)
1612 return err;
1614 xp = xfrm_policy_construct(net, p, attrs, &err);
1615 if (!xp)
1616 return err;
1618 /* shouldn't excl be based on nlh flags??
1619 * Aha! this is anti-netlink really i.e more pfkey derived
1620 * in netlink excl is a flag and you wouldnt need
1621 * a type XFRM_MSG_UPDPOLICY - JHS */
1622 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1623 err = xfrm_policy_insert(p->dir, xp, excl);
1624 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1626 if (err) {
1627 security_xfrm_policy_free(xp->security);
1628 kfree(xp);
1629 return err;
1632 c.event = nlh->nlmsg_type;
1633 c.seq = nlh->nlmsg_seq;
1634 c.portid = nlh->nlmsg_pid;
1635 km_policy_notify(xp, p->dir, &c);
1637 xfrm_pol_put(xp);
1639 return 0;
1642 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1644 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1645 int i;
1647 if (xp->xfrm_nr == 0)
1648 return 0;
1650 for (i = 0; i < xp->xfrm_nr; i++) {
1651 struct xfrm_user_tmpl *up = &vec[i];
1652 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1654 memset(up, 0, sizeof(*up));
1655 memcpy(&up->id, &kp->id, sizeof(up->id));
1656 up->family = kp->encap_family;
1657 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1658 up->reqid = kp->reqid;
1659 up->mode = kp->mode;
1660 up->share = kp->share;
1661 up->optional = kp->optional;
1662 up->aalgos = kp->aalgos;
1663 up->ealgos = kp->ealgos;
1664 up->calgos = kp->calgos;
1667 return nla_put(skb, XFRMA_TMPL,
1668 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1671 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1673 if (x->security) {
1674 return copy_sec_ctx(x->security, skb);
1676 return 0;
1679 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1681 if (xp->security)
1682 return copy_sec_ctx(xp->security, skb);
1683 return 0;
1685 static inline size_t userpolicy_type_attrsize(void)
1687 #ifdef CONFIG_XFRM_SUB_POLICY
1688 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1689 #else
1690 return 0;
1691 #endif
1694 #ifdef CONFIG_XFRM_SUB_POLICY
1695 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1697 struct xfrm_userpolicy_type upt;
1699 /* Sadly there are two holes in struct xfrm_userpolicy_type */
1700 memset(&upt, 0, sizeof(upt));
1701 upt.type = type;
1703 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1706 #else
1707 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1709 return 0;
1711 #endif
1713 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1715 struct xfrm_dump_info *sp = ptr;
1716 struct xfrm_userpolicy_info *p;
1717 struct sk_buff *in_skb = sp->in_skb;
1718 struct sk_buff *skb = sp->out_skb;
1719 struct nlmsghdr *nlh;
1720 int err;
1722 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1723 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1724 if (nlh == NULL)
1725 return -EMSGSIZE;
1727 p = nlmsg_data(nlh);
1728 copy_to_user_policy(xp, p, dir);
1729 err = copy_to_user_tmpl(xp, skb);
1730 if (!err)
1731 err = copy_to_user_sec_ctx(xp, skb);
1732 if (!err)
1733 err = copy_to_user_policy_type(xp->type, skb);
1734 if (!err)
1735 err = xfrm_mark_put(skb, &xp->mark);
1736 if (err) {
1737 nlmsg_cancel(skb, nlh);
1738 return err;
1740 nlmsg_end(skb, nlh);
1741 return 0;
1744 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1746 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1747 struct net *net = sock_net(cb->skb->sk);
1749 xfrm_policy_walk_done(walk, net);
1750 return 0;
1753 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1755 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1757 BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1759 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1760 return 0;
1763 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1765 struct net *net = sock_net(skb->sk);
1766 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1767 struct xfrm_dump_info info;
1769 info.in_skb = cb->skb;
1770 info.out_skb = skb;
1771 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1772 info.nlmsg_flags = NLM_F_MULTI;
1774 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1776 return skb->len;
1779 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1780 struct xfrm_policy *xp,
1781 int dir, u32 seq)
1783 struct xfrm_dump_info info;
1784 struct sk_buff *skb;
1785 int err;
1787 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1788 if (!skb)
1789 return ERR_PTR(-ENOMEM);
1791 info.in_skb = in_skb;
1792 info.out_skb = skb;
1793 info.nlmsg_seq = seq;
1794 info.nlmsg_flags = 0;
1796 err = dump_one_policy(xp, dir, 0, &info);
1797 if (err) {
1798 kfree_skb(skb);
1799 return ERR_PTR(err);
1802 return skb;
1805 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1806 struct nlattr **attrs)
1808 struct net *net = sock_net(skb->sk);
1809 struct xfrm_policy *xp;
1810 struct xfrm_userpolicy_id *p;
1811 u8 type = XFRM_POLICY_TYPE_MAIN;
1812 int err;
1813 struct km_event c;
1814 int delete;
1815 struct xfrm_mark m;
1816 u32 mark = xfrm_mark_get(attrs, &m);
1818 p = nlmsg_data(nlh);
1819 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1821 err = copy_from_user_policy_type(&type, attrs);
1822 if (err)
1823 return err;
1825 err = verify_policy_dir(p->dir);
1826 if (err)
1827 return err;
1829 if (p->index)
1830 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1831 else {
1832 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1833 struct xfrm_sec_ctx *ctx;
1835 err = verify_sec_ctx_len(attrs);
1836 if (err)
1837 return err;
1839 ctx = NULL;
1840 if (rt) {
1841 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1843 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1844 if (err)
1845 return err;
1847 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1848 ctx, delete, &err);
1849 security_xfrm_policy_free(ctx);
1851 if (xp == NULL)
1852 return -ENOENT;
1854 if (!delete) {
1855 struct sk_buff *resp_skb;
1857 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1858 if (IS_ERR(resp_skb)) {
1859 err = PTR_ERR(resp_skb);
1860 } else {
1861 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1862 NETLINK_CB(skb).portid);
1864 } else {
1865 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1867 if (err != 0)
1868 goto out;
1870 c.data.byid = p->index;
1871 c.event = nlh->nlmsg_type;
1872 c.seq = nlh->nlmsg_seq;
1873 c.portid = nlh->nlmsg_pid;
1874 km_policy_notify(xp, p->dir, &c);
1877 out:
1878 xfrm_pol_put(xp);
1879 return err;
1882 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1883 struct nlattr **attrs)
1885 struct net *net = sock_net(skb->sk);
1886 struct km_event c;
1887 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1888 int err;
1890 err = xfrm_state_flush(net, p->proto, true);
1891 if (err) {
1892 if (err == -ESRCH) /* empty table */
1893 return 0;
1894 return err;
1896 c.data.proto = p->proto;
1897 c.event = nlh->nlmsg_type;
1898 c.seq = nlh->nlmsg_seq;
1899 c.portid = nlh->nlmsg_pid;
1900 c.net = net;
1901 km_state_notify(NULL, &c);
1903 return 0;
1906 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1908 size_t replay_size = x->replay_esn ?
1909 xfrm_replay_state_esn_len(x->replay_esn) :
1910 sizeof(struct xfrm_replay_state);
1912 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1913 + nla_total_size(replay_size)
1914 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1915 + nla_total_size(sizeof(struct xfrm_mark))
1916 + nla_total_size(4) /* XFRM_AE_RTHR */
1917 + nla_total_size(4); /* XFRM_AE_ETHR */
1920 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1922 struct xfrm_aevent_id *id;
1923 struct nlmsghdr *nlh;
1924 int err;
1926 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1927 if (nlh == NULL)
1928 return -EMSGSIZE;
1930 id = nlmsg_data(nlh);
1931 memset(&id->sa_id, 0, sizeof(id->sa_id));
1932 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1933 id->sa_id.spi = x->id.spi;
1934 id->sa_id.family = x->props.family;
1935 id->sa_id.proto = x->id.proto;
1936 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1937 id->reqid = x->props.reqid;
1938 id->flags = c->data.aevent;
1940 if (x->replay_esn) {
1941 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1942 xfrm_replay_state_esn_len(x->replay_esn),
1943 x->replay_esn);
1944 } else {
1945 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1946 &x->replay);
1948 if (err)
1949 goto out_cancel;
1950 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1951 XFRMA_PAD);
1952 if (err)
1953 goto out_cancel;
1955 if (id->flags & XFRM_AE_RTHR) {
1956 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1957 if (err)
1958 goto out_cancel;
1960 if (id->flags & XFRM_AE_ETHR) {
1961 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1962 x->replay_maxage * 10 / HZ);
1963 if (err)
1964 goto out_cancel;
1966 err = xfrm_mark_put(skb, &x->mark);
1967 if (err)
1968 goto out_cancel;
1970 nlmsg_end(skb, nlh);
1971 return 0;
1973 out_cancel:
1974 nlmsg_cancel(skb, nlh);
1975 return err;
1978 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1979 struct nlattr **attrs)
1981 struct net *net = sock_net(skb->sk);
1982 struct xfrm_state *x;
1983 struct sk_buff *r_skb;
1984 int err;
1985 struct km_event c;
1986 u32 mark;
1987 struct xfrm_mark m;
1988 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1989 struct xfrm_usersa_id *id = &p->sa_id;
1991 mark = xfrm_mark_get(attrs, &m);
1993 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1994 if (x == NULL)
1995 return -ESRCH;
1997 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1998 if (r_skb == NULL) {
1999 xfrm_state_put(x);
2000 return -ENOMEM;
2004 * XXX: is this lock really needed - none of the other
2005 * gets lock (the concern is things getting updated
2006 * while we are still reading) - jhs
2008 spin_lock_bh(&x->lock);
2009 c.data.aevent = p->flags;
2010 c.seq = nlh->nlmsg_seq;
2011 c.portid = nlh->nlmsg_pid;
2013 if (build_aevent(r_skb, x, &c) < 0)
2014 BUG();
2015 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2016 spin_unlock_bh(&x->lock);
2017 xfrm_state_put(x);
2018 return err;
2021 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2022 struct nlattr **attrs)
2024 struct net *net = sock_net(skb->sk);
2025 struct xfrm_state *x;
2026 struct km_event c;
2027 int err = -EINVAL;
2028 u32 mark = 0;
2029 struct xfrm_mark m;
2030 struct xfrm_aevent_id *p = nlmsg_data(nlh);
2031 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2032 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2033 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2034 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2035 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2037 if (!lt && !rp && !re && !et && !rt)
2038 return err;
2040 /* pedantic mode - thou shalt sayeth replaceth */
2041 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2042 return err;
2044 mark = xfrm_mark_get(attrs, &m);
2046 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2047 if (x == NULL)
2048 return -ESRCH;
2050 if (x->km.state != XFRM_STATE_VALID)
2051 goto out;
2053 err = xfrm_replay_verify_len(x->replay_esn, re);
2054 if (err)
2055 goto out;
2057 spin_lock_bh(&x->lock);
2058 xfrm_update_ae_params(x, attrs, 1);
2059 spin_unlock_bh(&x->lock);
2061 c.event = nlh->nlmsg_type;
2062 c.seq = nlh->nlmsg_seq;
2063 c.portid = nlh->nlmsg_pid;
2064 c.data.aevent = XFRM_AE_CU;
2065 km_state_notify(x, &c);
2066 err = 0;
2067 out:
2068 xfrm_state_put(x);
2069 return err;
2072 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2073 struct nlattr **attrs)
2075 struct net *net = sock_net(skb->sk);
2076 struct km_event c;
2077 u8 type = XFRM_POLICY_TYPE_MAIN;
2078 int err;
2080 err = copy_from_user_policy_type(&type, attrs);
2081 if (err)
2082 return err;
2084 err = xfrm_policy_flush(net, type, true);
2085 if (err) {
2086 if (err == -ESRCH) /* empty table */
2087 return 0;
2088 return err;
2091 c.data.type = type;
2092 c.event = nlh->nlmsg_type;
2093 c.seq = nlh->nlmsg_seq;
2094 c.portid = nlh->nlmsg_pid;
2095 c.net = net;
2096 km_policy_notify(NULL, 0, &c);
2097 return 0;
2100 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2101 struct nlattr **attrs)
2103 struct net *net = sock_net(skb->sk);
2104 struct xfrm_policy *xp;
2105 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2106 struct xfrm_userpolicy_info *p = &up->pol;
2107 u8 type = XFRM_POLICY_TYPE_MAIN;
2108 int err = -ENOENT;
2109 struct xfrm_mark m;
2110 u32 mark = xfrm_mark_get(attrs, &m);
2112 err = copy_from_user_policy_type(&type, attrs);
2113 if (err)
2114 return err;
2116 err = verify_policy_dir(p->dir);
2117 if (err)
2118 return err;
2120 if (p->index)
2121 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2122 else {
2123 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2124 struct xfrm_sec_ctx *ctx;
2126 err = verify_sec_ctx_len(attrs);
2127 if (err)
2128 return err;
2130 ctx = NULL;
2131 if (rt) {
2132 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2134 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2135 if (err)
2136 return err;
2138 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2139 &p->sel, ctx, 0, &err);
2140 security_xfrm_policy_free(ctx);
2142 if (xp == NULL)
2143 return -ENOENT;
2145 if (unlikely(xp->walk.dead))
2146 goto out;
2148 err = 0;
2149 if (up->hard) {
2150 xfrm_policy_delete(xp, p->dir);
2151 xfrm_audit_policy_delete(xp, 1, true);
2153 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2155 out:
2156 xfrm_pol_put(xp);
2157 return err;
2160 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2161 struct nlattr **attrs)
2163 struct net *net = sock_net(skb->sk);
2164 struct xfrm_state *x;
2165 int err;
2166 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2167 struct xfrm_usersa_info *p = &ue->state;
2168 struct xfrm_mark m;
2169 u32 mark = xfrm_mark_get(attrs, &m);
2171 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2173 err = -ENOENT;
2174 if (x == NULL)
2175 return err;
2177 spin_lock_bh(&x->lock);
2178 err = -EINVAL;
2179 if (x->km.state != XFRM_STATE_VALID)
2180 goto out;
2181 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2183 if (ue->hard) {
2184 __xfrm_state_delete(x);
2185 xfrm_audit_state_delete(x, 1, true);
2187 err = 0;
2188 out:
2189 spin_unlock_bh(&x->lock);
2190 xfrm_state_put(x);
2191 return err;
2194 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2195 struct nlattr **attrs)
2197 struct net *net = sock_net(skb->sk);
2198 struct xfrm_policy *xp;
2199 struct xfrm_user_tmpl *ut;
2200 int i;
2201 struct nlattr *rt = attrs[XFRMA_TMPL];
2202 struct xfrm_mark mark;
2204 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2205 struct xfrm_state *x = xfrm_state_alloc(net);
2206 int err = -ENOMEM;
2208 if (!x)
2209 goto nomem;
2211 xfrm_mark_get(attrs, &mark);
2213 err = verify_newpolicy_info(&ua->policy);
2214 if (err)
2215 goto free_state;
2217 /* build an XP */
2218 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2219 if (!xp)
2220 goto free_state;
2222 memcpy(&x->id, &ua->id, sizeof(ua->id));
2223 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2224 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2225 xp->mark.m = x->mark.m = mark.m;
2226 xp->mark.v = x->mark.v = mark.v;
2227 ut = nla_data(rt);
2228 /* extract the templates and for each call km_key */
2229 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2230 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2231 memcpy(&x->id, &t->id, sizeof(x->id));
2232 x->props.mode = t->mode;
2233 x->props.reqid = t->reqid;
2234 x->props.family = ut->family;
2235 t->aalgos = ua->aalgos;
2236 t->ealgos = ua->ealgos;
2237 t->calgos = ua->calgos;
2238 err = km_query(x, t, xp);
2242 kfree(x);
2243 kfree(xp);
2245 return 0;
2247 free_state:
2248 kfree(x);
2249 nomem:
2250 return err;
2253 #ifdef CONFIG_XFRM_MIGRATE
2254 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2255 struct xfrm_kmaddress *k,
2256 struct nlattr **attrs, int *num)
2258 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2259 struct xfrm_user_migrate *um;
2260 int i, num_migrate;
2262 if (k != NULL) {
2263 struct xfrm_user_kmaddress *uk;
2265 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2266 memcpy(&k->local, &uk->local, sizeof(k->local));
2267 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2268 k->family = uk->family;
2269 k->reserved = uk->reserved;
2272 um = nla_data(rt);
2273 num_migrate = nla_len(rt) / sizeof(*um);
2275 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2276 return -EINVAL;
2278 for (i = 0; i < num_migrate; i++, um++, ma++) {
2279 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2280 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2281 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2282 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2284 ma->proto = um->proto;
2285 ma->mode = um->mode;
2286 ma->reqid = um->reqid;
2288 ma->old_family = um->old_family;
2289 ma->new_family = um->new_family;
2292 *num = i;
2293 return 0;
2296 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2297 struct nlattr **attrs)
2299 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2300 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2301 struct xfrm_kmaddress km, *kmp;
2302 u8 type;
2303 int err;
2304 int n = 0;
2305 struct net *net = sock_net(skb->sk);
2306 struct xfrm_encap_tmpl *encap = NULL;
2308 if (attrs[XFRMA_MIGRATE] == NULL)
2309 return -EINVAL;
2311 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2313 err = copy_from_user_policy_type(&type, attrs);
2314 if (err)
2315 return err;
2317 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2318 if (err)
2319 return err;
2321 if (!n)
2322 return 0;
2324 if (attrs[XFRMA_ENCAP]) {
2325 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2326 sizeof(*encap), GFP_KERNEL);
2327 if (!encap)
2328 return 0;
2331 err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2333 kfree(encap);
2335 return err;
2337 #else
2338 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2339 struct nlattr **attrs)
2341 return -ENOPROTOOPT;
2343 #endif
2345 #ifdef CONFIG_XFRM_MIGRATE
2346 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2348 struct xfrm_user_migrate um;
2350 memset(&um, 0, sizeof(um));
2351 um.proto = m->proto;
2352 um.mode = m->mode;
2353 um.reqid = m->reqid;
2354 um.old_family = m->old_family;
2355 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2356 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2357 um.new_family = m->new_family;
2358 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2359 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2361 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2364 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2366 struct xfrm_user_kmaddress uk;
2368 memset(&uk, 0, sizeof(uk));
2369 uk.family = k->family;
2370 uk.reserved = k->reserved;
2371 memcpy(&uk.local, &k->local, sizeof(uk.local));
2372 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2374 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2377 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
2378 int with_encp)
2380 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2381 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2382 + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2383 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2384 + userpolicy_type_attrsize();
2387 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2388 int num_migrate, const struct xfrm_kmaddress *k,
2389 const struct xfrm_selector *sel,
2390 const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2392 const struct xfrm_migrate *mp;
2393 struct xfrm_userpolicy_id *pol_id;
2394 struct nlmsghdr *nlh;
2395 int i, err;
2397 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2398 if (nlh == NULL)
2399 return -EMSGSIZE;
2401 pol_id = nlmsg_data(nlh);
2402 /* copy data from selector, dir, and type to the pol_id */
2403 memset(pol_id, 0, sizeof(*pol_id));
2404 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2405 pol_id->dir = dir;
2407 if (k != NULL) {
2408 err = copy_to_user_kmaddress(k, skb);
2409 if (err)
2410 goto out_cancel;
2412 if (encap) {
2413 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2414 if (err)
2415 goto out_cancel;
2417 err = copy_to_user_policy_type(type, skb);
2418 if (err)
2419 goto out_cancel;
2420 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2421 err = copy_to_user_migrate(mp, skb);
2422 if (err)
2423 goto out_cancel;
2426 nlmsg_end(skb, nlh);
2427 return 0;
2429 out_cancel:
2430 nlmsg_cancel(skb, nlh);
2431 return err;
2434 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2435 const struct xfrm_migrate *m, int num_migrate,
2436 const struct xfrm_kmaddress *k,
2437 const struct xfrm_encap_tmpl *encap)
2439 struct net *net = &init_net;
2440 struct sk_buff *skb;
2442 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2443 GFP_ATOMIC);
2444 if (skb == NULL)
2445 return -ENOMEM;
2447 /* build migrate */
2448 if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
2449 BUG();
2451 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2453 #else
2454 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2455 const struct xfrm_migrate *m, int num_migrate,
2456 const struct xfrm_kmaddress *k,
2457 const struct xfrm_encap_tmpl *encap)
2459 return -ENOPROTOOPT;
2461 #endif
2463 #define XMSGSIZE(type) sizeof(struct type)
2465 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2466 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2467 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2468 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2469 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2470 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2471 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2472 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2473 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2474 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2475 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2476 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2477 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2478 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2479 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2480 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2481 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2482 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2483 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2484 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2485 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2486 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2489 #undef XMSGSIZE
2491 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2492 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2493 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2494 [XFRMA_LASTUSED] = { .type = NLA_U64},
2495 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2496 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2497 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2498 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2499 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2500 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2501 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2502 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2503 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2504 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2505 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2506 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2507 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2508 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2509 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2510 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2511 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2512 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2513 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2514 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2515 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2516 [XFRMA_PROTO] = { .type = NLA_U8 },
2517 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2518 [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
2519 [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
2522 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2523 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2524 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2527 static const struct xfrm_link {
2528 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2529 int (*start)(struct netlink_callback *);
2530 int (*dump)(struct sk_buff *, struct netlink_callback *);
2531 int (*done)(struct netlink_callback *);
2532 const struct nla_policy *nla_pol;
2533 int nla_max;
2534 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2535 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2536 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2537 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2538 .dump = xfrm_dump_sa,
2539 .done = xfrm_dump_sa_done },
2540 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2541 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2542 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2543 .start = xfrm_dump_policy_start,
2544 .dump = xfrm_dump_policy,
2545 .done = xfrm_dump_policy_done },
2546 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2547 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2548 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2549 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2550 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2551 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2552 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2553 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2554 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2555 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2556 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2557 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2558 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2559 .nla_pol = xfrma_spd_policy,
2560 .nla_max = XFRMA_SPD_MAX },
2561 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2564 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2565 struct netlink_ext_ack *extack)
2567 struct net *net = sock_net(skb->sk);
2568 struct nlattr *attrs[XFRMA_MAX+1];
2569 const struct xfrm_link *link;
2570 int type, err;
2572 #ifdef CONFIG_COMPAT
2573 if (in_compat_syscall())
2574 return -EOPNOTSUPP;
2575 #endif
2577 type = nlh->nlmsg_type;
2578 if (type > XFRM_MSG_MAX)
2579 return -EINVAL;
2581 type -= XFRM_MSG_BASE;
2582 link = &xfrm_dispatch[type];
2584 /* All operations require privileges, even GET */
2585 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2586 return -EPERM;
2588 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2589 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2590 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2591 if (link->dump == NULL)
2592 return -EINVAL;
2595 struct netlink_dump_control c = {
2596 .start = link->start,
2597 .dump = link->dump,
2598 .done = link->done,
2600 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2604 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2605 link->nla_max ? : XFRMA_MAX,
2606 link->nla_pol ? : xfrma_policy, extack);
2607 if (err < 0)
2608 return err;
2610 if (link->doit == NULL)
2611 return -EINVAL;
2613 return link->doit(skb, nlh, attrs);
2616 static void xfrm_netlink_rcv(struct sk_buff *skb)
2618 struct net *net = sock_net(skb->sk);
2620 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2621 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2622 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2625 static inline size_t xfrm_expire_msgsize(void)
2627 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2628 + nla_total_size(sizeof(struct xfrm_mark));
2631 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2633 struct xfrm_user_expire *ue;
2634 struct nlmsghdr *nlh;
2635 int err;
2637 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2638 if (nlh == NULL)
2639 return -EMSGSIZE;
2641 ue = nlmsg_data(nlh);
2642 copy_to_user_state(x, &ue->state);
2643 ue->hard = (c->data.hard != 0) ? 1 : 0;
2644 /* clear the padding bytes */
2645 memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2647 err = xfrm_mark_put(skb, &x->mark);
2648 if (err)
2649 return err;
2651 nlmsg_end(skb, nlh);
2652 return 0;
2655 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2657 struct net *net = xs_net(x);
2658 struct sk_buff *skb;
2660 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2661 if (skb == NULL)
2662 return -ENOMEM;
2664 if (build_expire(skb, x, c) < 0) {
2665 kfree_skb(skb);
2666 return -EMSGSIZE;
2669 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2672 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2674 struct net *net = xs_net(x);
2675 struct sk_buff *skb;
2677 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2678 if (skb == NULL)
2679 return -ENOMEM;
2681 if (build_aevent(skb, x, c) < 0)
2682 BUG();
2684 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2687 static int xfrm_notify_sa_flush(const struct km_event *c)
2689 struct net *net = c->net;
2690 struct xfrm_usersa_flush *p;
2691 struct nlmsghdr *nlh;
2692 struct sk_buff *skb;
2693 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2695 skb = nlmsg_new(len, GFP_ATOMIC);
2696 if (skb == NULL)
2697 return -ENOMEM;
2699 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2700 if (nlh == NULL) {
2701 kfree_skb(skb);
2702 return -EMSGSIZE;
2705 p = nlmsg_data(nlh);
2706 p->proto = c->data.proto;
2708 nlmsg_end(skb, nlh);
2710 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2713 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2715 size_t l = 0;
2716 if (x->aead)
2717 l += nla_total_size(aead_len(x->aead));
2718 if (x->aalg) {
2719 l += nla_total_size(sizeof(struct xfrm_algo) +
2720 (x->aalg->alg_key_len + 7) / 8);
2721 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2723 if (x->ealg)
2724 l += nla_total_size(xfrm_alg_len(x->ealg));
2725 if (x->calg)
2726 l += nla_total_size(sizeof(*x->calg));
2727 if (x->encap)
2728 l += nla_total_size(sizeof(*x->encap));
2729 if (x->tfcpad)
2730 l += nla_total_size(sizeof(x->tfcpad));
2731 if (x->replay_esn)
2732 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2733 else
2734 l += nla_total_size(sizeof(struct xfrm_replay_state));
2735 if (x->security)
2736 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2737 x->security->ctx_len);
2738 if (x->coaddr)
2739 l += nla_total_size(sizeof(*x->coaddr));
2740 if (x->props.extra_flags)
2741 l += nla_total_size(sizeof(x->props.extra_flags));
2742 if (x->xso.dev)
2743 l += nla_total_size(sizeof(x->xso));
2744 if (x->props.output_mark)
2745 l += nla_total_size(sizeof(x->props.output_mark));
2747 /* Must count x->lastused as it may become non-zero behind our back. */
2748 l += nla_total_size_64bit(sizeof(u64));
2750 return l;
2753 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2755 struct net *net = xs_net(x);
2756 struct xfrm_usersa_info *p;
2757 struct xfrm_usersa_id *id;
2758 struct nlmsghdr *nlh;
2759 struct sk_buff *skb;
2760 int len = xfrm_sa_len(x);
2761 int headlen, err;
2763 headlen = sizeof(*p);
2764 if (c->event == XFRM_MSG_DELSA) {
2765 len += nla_total_size(headlen);
2766 headlen = sizeof(*id);
2767 len += nla_total_size(sizeof(struct xfrm_mark));
2769 len += NLMSG_ALIGN(headlen);
2771 skb = nlmsg_new(len, GFP_ATOMIC);
2772 if (skb == NULL)
2773 return -ENOMEM;
2775 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2776 err = -EMSGSIZE;
2777 if (nlh == NULL)
2778 goto out_free_skb;
2780 p = nlmsg_data(nlh);
2781 if (c->event == XFRM_MSG_DELSA) {
2782 struct nlattr *attr;
2784 id = nlmsg_data(nlh);
2785 memset(id, 0, sizeof(*id));
2786 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2787 id->spi = x->id.spi;
2788 id->family = x->props.family;
2789 id->proto = x->id.proto;
2791 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2792 err = -EMSGSIZE;
2793 if (attr == NULL)
2794 goto out_free_skb;
2796 p = nla_data(attr);
2798 err = copy_to_user_state_extra(x, p, skb);
2799 if (err)
2800 goto out_free_skb;
2802 nlmsg_end(skb, nlh);
2804 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2806 out_free_skb:
2807 kfree_skb(skb);
2808 return err;
2811 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2814 switch (c->event) {
2815 case XFRM_MSG_EXPIRE:
2816 return xfrm_exp_state_notify(x, c);
2817 case XFRM_MSG_NEWAE:
2818 return xfrm_aevent_state_notify(x, c);
2819 case XFRM_MSG_DELSA:
2820 case XFRM_MSG_UPDSA:
2821 case XFRM_MSG_NEWSA:
2822 return xfrm_notify_sa(x, c);
2823 case XFRM_MSG_FLUSHSA:
2824 return xfrm_notify_sa_flush(c);
2825 default:
2826 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2827 c->event);
2828 break;
2831 return 0;
2835 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2836 struct xfrm_policy *xp)
2838 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2839 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2840 + nla_total_size(sizeof(struct xfrm_mark))
2841 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2842 + userpolicy_type_attrsize();
2845 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2846 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2848 __u32 seq = xfrm_get_acqseq();
2849 struct xfrm_user_acquire *ua;
2850 struct nlmsghdr *nlh;
2851 int err;
2853 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2854 if (nlh == NULL)
2855 return -EMSGSIZE;
2857 ua = nlmsg_data(nlh);
2858 memcpy(&ua->id, &x->id, sizeof(ua->id));
2859 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2860 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2861 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2862 ua->aalgos = xt->aalgos;
2863 ua->ealgos = xt->ealgos;
2864 ua->calgos = xt->calgos;
2865 ua->seq = x->km.seq = seq;
2867 err = copy_to_user_tmpl(xp, skb);
2868 if (!err)
2869 err = copy_to_user_state_sec_ctx(x, skb);
2870 if (!err)
2871 err = copy_to_user_policy_type(xp->type, skb);
2872 if (!err)
2873 err = xfrm_mark_put(skb, &xp->mark);
2874 if (err) {
2875 nlmsg_cancel(skb, nlh);
2876 return err;
2879 nlmsg_end(skb, nlh);
2880 return 0;
2883 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2884 struct xfrm_policy *xp)
2886 struct net *net = xs_net(x);
2887 struct sk_buff *skb;
2889 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2890 if (skb == NULL)
2891 return -ENOMEM;
2893 if (build_acquire(skb, x, xt, xp) < 0)
2894 BUG();
2896 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2899 /* User gives us xfrm_user_policy_info followed by an array of 0
2900 * or more templates.
2902 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2903 u8 *data, int len, int *dir)
2905 struct net *net = sock_net(sk);
2906 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2907 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2908 struct xfrm_policy *xp;
2909 int nr;
2911 switch (sk->sk_family) {
2912 case AF_INET:
2913 if (opt != IP_XFRM_POLICY) {
2914 *dir = -EOPNOTSUPP;
2915 return NULL;
2917 break;
2918 #if IS_ENABLED(CONFIG_IPV6)
2919 case AF_INET6:
2920 if (opt != IPV6_XFRM_POLICY) {
2921 *dir = -EOPNOTSUPP;
2922 return NULL;
2924 break;
2925 #endif
2926 default:
2927 *dir = -EINVAL;
2928 return NULL;
2931 *dir = -EINVAL;
2933 if (len < sizeof(*p) ||
2934 verify_newpolicy_info(p))
2935 return NULL;
2937 nr = ((len - sizeof(*p)) / sizeof(*ut));
2938 if (validate_tmpl(nr, ut, p->sel.family))
2939 return NULL;
2941 if (p->dir > XFRM_POLICY_OUT)
2942 return NULL;
2944 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2945 if (xp == NULL) {
2946 *dir = -ENOBUFS;
2947 return NULL;
2950 copy_from_user_policy(xp, p);
2951 xp->type = XFRM_POLICY_TYPE_MAIN;
2952 copy_templates(xp, ut, nr);
2954 *dir = p->dir;
2956 return xp;
2959 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2961 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2962 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2963 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2964 + nla_total_size(sizeof(struct xfrm_mark))
2965 + userpolicy_type_attrsize();
2968 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2969 int dir, const struct km_event *c)
2971 struct xfrm_user_polexpire *upe;
2972 int hard = c->data.hard;
2973 struct nlmsghdr *nlh;
2974 int err;
2976 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2977 if (nlh == NULL)
2978 return -EMSGSIZE;
2980 upe = nlmsg_data(nlh);
2981 copy_to_user_policy(xp, &upe->pol, dir);
2982 err = copy_to_user_tmpl(xp, skb);
2983 if (!err)
2984 err = copy_to_user_sec_ctx(xp, skb);
2985 if (!err)
2986 err = copy_to_user_policy_type(xp->type, skb);
2987 if (!err)
2988 err = xfrm_mark_put(skb, &xp->mark);
2989 if (err) {
2990 nlmsg_cancel(skb, nlh);
2991 return err;
2993 upe->hard = !!hard;
2995 nlmsg_end(skb, nlh);
2996 return 0;
2999 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3001 struct net *net = xp_net(xp);
3002 struct sk_buff *skb;
3004 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3005 if (skb == NULL)
3006 return -ENOMEM;
3008 if (build_polexpire(skb, xp, dir, c) < 0)
3009 BUG();
3011 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3014 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3016 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3017 struct net *net = xp_net(xp);
3018 struct xfrm_userpolicy_info *p;
3019 struct xfrm_userpolicy_id *id;
3020 struct nlmsghdr *nlh;
3021 struct sk_buff *skb;
3022 int headlen, err;
3024 headlen = sizeof(*p);
3025 if (c->event == XFRM_MSG_DELPOLICY) {
3026 len += nla_total_size(headlen);
3027 headlen = sizeof(*id);
3029 len += userpolicy_type_attrsize();
3030 len += nla_total_size(sizeof(struct xfrm_mark));
3031 len += NLMSG_ALIGN(headlen);
3033 skb = nlmsg_new(len, GFP_ATOMIC);
3034 if (skb == NULL)
3035 return -ENOMEM;
3037 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3038 err = -EMSGSIZE;
3039 if (nlh == NULL)
3040 goto out_free_skb;
3042 p = nlmsg_data(nlh);
3043 if (c->event == XFRM_MSG_DELPOLICY) {
3044 struct nlattr *attr;
3046 id = nlmsg_data(nlh);
3047 memset(id, 0, sizeof(*id));
3048 id->dir = dir;
3049 if (c->data.byid)
3050 id->index = xp->index;
3051 else
3052 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3054 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3055 err = -EMSGSIZE;
3056 if (attr == NULL)
3057 goto out_free_skb;
3059 p = nla_data(attr);
3062 copy_to_user_policy(xp, p, dir);
3063 err = copy_to_user_tmpl(xp, skb);
3064 if (!err)
3065 err = copy_to_user_policy_type(xp->type, skb);
3066 if (!err)
3067 err = xfrm_mark_put(skb, &xp->mark);
3068 if (err)
3069 goto out_free_skb;
3071 nlmsg_end(skb, nlh);
3073 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3075 out_free_skb:
3076 kfree_skb(skb);
3077 return err;
3080 static int xfrm_notify_policy_flush(const struct km_event *c)
3082 struct net *net = c->net;
3083 struct nlmsghdr *nlh;
3084 struct sk_buff *skb;
3085 int err;
3087 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3088 if (skb == NULL)
3089 return -ENOMEM;
3091 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3092 err = -EMSGSIZE;
3093 if (nlh == NULL)
3094 goto out_free_skb;
3095 err = copy_to_user_policy_type(c->data.type, skb);
3096 if (err)
3097 goto out_free_skb;
3099 nlmsg_end(skb, nlh);
3101 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3103 out_free_skb:
3104 kfree_skb(skb);
3105 return err;
3108 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3111 switch (c->event) {
3112 case XFRM_MSG_NEWPOLICY:
3113 case XFRM_MSG_UPDPOLICY:
3114 case XFRM_MSG_DELPOLICY:
3115 return xfrm_notify_policy(xp, dir, c);
3116 case XFRM_MSG_FLUSHPOLICY:
3117 return xfrm_notify_policy_flush(c);
3118 case XFRM_MSG_POLEXPIRE:
3119 return xfrm_exp_policy_notify(xp, dir, c);
3120 default:
3121 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3122 c->event);
3125 return 0;
3129 static inline size_t xfrm_report_msgsize(void)
3131 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3134 static int build_report(struct sk_buff *skb, u8 proto,
3135 struct xfrm_selector *sel, xfrm_address_t *addr)
3137 struct xfrm_user_report *ur;
3138 struct nlmsghdr *nlh;
3140 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3141 if (nlh == NULL)
3142 return -EMSGSIZE;
3144 ur = nlmsg_data(nlh);
3145 ur->proto = proto;
3146 memcpy(&ur->sel, sel, sizeof(ur->sel));
3148 if (addr) {
3149 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3150 if (err) {
3151 nlmsg_cancel(skb, nlh);
3152 return err;
3155 nlmsg_end(skb, nlh);
3156 return 0;
3159 static int xfrm_send_report(struct net *net, u8 proto,
3160 struct xfrm_selector *sel, xfrm_address_t *addr)
3162 struct sk_buff *skb;
3164 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3165 if (skb == NULL)
3166 return -ENOMEM;
3168 if (build_report(skb, proto, sel, addr) < 0)
3169 BUG();
3171 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3174 static inline size_t xfrm_mapping_msgsize(void)
3176 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3179 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3180 xfrm_address_t *new_saddr, __be16 new_sport)
3182 struct xfrm_user_mapping *um;
3183 struct nlmsghdr *nlh;
3185 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3186 if (nlh == NULL)
3187 return -EMSGSIZE;
3189 um = nlmsg_data(nlh);
3191 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3192 um->id.spi = x->id.spi;
3193 um->id.family = x->props.family;
3194 um->id.proto = x->id.proto;
3195 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3196 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3197 um->new_sport = new_sport;
3198 um->old_sport = x->encap->encap_sport;
3199 um->reqid = x->props.reqid;
3201 nlmsg_end(skb, nlh);
3202 return 0;
3205 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3206 __be16 sport)
3208 struct net *net = xs_net(x);
3209 struct sk_buff *skb;
3211 if (x->id.proto != IPPROTO_ESP)
3212 return -EINVAL;
3214 if (!x->encap)
3215 return -EINVAL;
3217 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3218 if (skb == NULL)
3219 return -ENOMEM;
3221 if (build_mapping(skb, x, ipaddr, sport) < 0)
3222 BUG();
3224 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3227 static bool xfrm_is_alive(const struct km_event *c)
3229 return (bool)xfrm_acquire_is_on(c->net);
3232 static struct xfrm_mgr netlink_mgr = {
3233 .notify = xfrm_send_state_notify,
3234 .acquire = xfrm_send_acquire,
3235 .compile_policy = xfrm_compile_policy,
3236 .notify_policy = xfrm_send_policy_notify,
3237 .report = xfrm_send_report,
3238 .migrate = xfrm_send_migrate,
3239 .new_mapping = xfrm_send_mapping,
3240 .is_alive = xfrm_is_alive,
3243 static int __net_init xfrm_user_net_init(struct net *net)
3245 struct sock *nlsk;
3246 struct netlink_kernel_cfg cfg = {
3247 .groups = XFRMNLGRP_MAX,
3248 .input = xfrm_netlink_rcv,
3251 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3252 if (nlsk == NULL)
3253 return -ENOMEM;
3254 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3255 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3256 return 0;
3259 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3261 struct net *net;
3262 list_for_each_entry(net, net_exit_list, exit_list)
3263 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3264 synchronize_net();
3265 list_for_each_entry(net, net_exit_list, exit_list)
3266 netlink_kernel_release(net->xfrm.nlsk_stash);
3269 static struct pernet_operations xfrm_user_net_ops = {
3270 .init = xfrm_user_net_init,
3271 .exit_batch = xfrm_user_net_exit,
3274 static int __init xfrm_user_init(void)
3276 int rv;
3278 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3280 rv = register_pernet_subsys(&xfrm_user_net_ops);
3281 if (rv < 0)
3282 return rv;
3283 rv = xfrm_register_km(&netlink_mgr);
3284 if (rv < 0)
3285 unregister_pernet_subsys(&xfrm_user_net_ops);
3286 return rv;
3289 static void __exit xfrm_user_exit(void)
3291 xfrm_unregister_km(&netlink_mgr);
3292 unregister_pernet_subsys(&xfrm_user_net_ops);
3295 module_init(xfrm_user_init);
3296 module_exit(xfrm_user_exit);
3297 MODULE_LICENSE("GPL");
3298 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);