iwlwifi: mvm: use LIST_HEAD() macro
[linux-2.6/btrfs-unstable.git] / net / xfrm / xfrm_user.c
blobcb65d916a345d1e45547cd81c0d86b2068c58bce
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 <asm/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[CRYPTO_MAX_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[CRYPTO_MAX_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[CRYPTO_MAX_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 (p->flags & XFRM_STATE_ESN) {
125 if (!rt)
126 return -EINVAL;
128 rs = nla_data(rt);
130 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
131 return -EINVAL;
133 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
134 nla_len(rt) != sizeof(*rs))
135 return -EINVAL;
138 if (!rt)
139 return 0;
141 /* As only ESP and AH support ESN feature. */
142 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
143 return -EINVAL;
145 if (p->replay_window != 0)
146 return -EINVAL;
148 return 0;
151 static int verify_newsa_info(struct xfrm_usersa_info *p,
152 struct nlattr **attrs)
154 int err;
156 err = -EINVAL;
157 switch (p->family) {
158 case AF_INET:
159 break;
161 case AF_INET6:
162 #if IS_ENABLED(CONFIG_IPV6)
163 break;
164 #else
165 err = -EAFNOSUPPORT;
166 goto out;
167 #endif
169 default:
170 goto out;
173 err = -EINVAL;
174 switch (p->id.proto) {
175 case IPPROTO_AH:
176 if ((!attrs[XFRMA_ALG_AUTH] &&
177 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
178 attrs[XFRMA_ALG_AEAD] ||
179 attrs[XFRMA_ALG_CRYPT] ||
180 attrs[XFRMA_ALG_COMP] ||
181 attrs[XFRMA_TFCPAD])
182 goto out;
183 break;
185 case IPPROTO_ESP:
186 if (attrs[XFRMA_ALG_COMP])
187 goto out;
188 if (!attrs[XFRMA_ALG_AUTH] &&
189 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
190 !attrs[XFRMA_ALG_CRYPT] &&
191 !attrs[XFRMA_ALG_AEAD])
192 goto out;
193 if ((attrs[XFRMA_ALG_AUTH] ||
194 attrs[XFRMA_ALG_AUTH_TRUNC] ||
195 attrs[XFRMA_ALG_CRYPT]) &&
196 attrs[XFRMA_ALG_AEAD])
197 goto out;
198 if (attrs[XFRMA_TFCPAD] &&
199 p->mode != XFRM_MODE_TUNNEL)
200 goto out;
201 break;
203 case IPPROTO_COMP:
204 if (!attrs[XFRMA_ALG_COMP] ||
205 attrs[XFRMA_ALG_AEAD] ||
206 attrs[XFRMA_ALG_AUTH] ||
207 attrs[XFRMA_ALG_AUTH_TRUNC] ||
208 attrs[XFRMA_ALG_CRYPT] ||
209 attrs[XFRMA_TFCPAD] ||
210 (ntohl(p->id.spi) >= 0x10000))
211 goto out;
212 break;
214 #if IS_ENABLED(CONFIG_IPV6)
215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
220 attrs[XFRMA_ALG_AEAD] ||
221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
224 attrs[XFRMA_TFCPAD] ||
225 !attrs[XFRMA_COADDR])
226 goto out;
227 break;
228 #endif
230 default:
231 goto out;
234 if ((err = verify_aead(attrs)))
235 goto out;
236 if ((err = verify_auth_trunc(attrs)))
237 goto out;
238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
239 goto out;
240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
241 goto out;
242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
243 goto out;
244 if ((err = verify_sec_ctx_len(attrs)))
245 goto out;
246 if ((err = verify_replay(p, attrs)))
247 goto out;
249 err = -EINVAL;
250 switch (p->mode) {
251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
253 case XFRM_MODE_ROUTEOPTIMIZATION:
254 case XFRM_MODE_BEET:
255 break;
257 default:
258 goto out;
261 err = 0;
263 out:
264 return err;
267 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
269 struct nlattr *rta)
271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
274 if (!rta)
275 return 0;
277 ualg = nla_data(rta);
279 algo = get_byname(ualg->alg_name, 1);
280 if (!algo)
281 return -ENOSYS;
282 *props = algo->desc.sadb_alg_id;
284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
285 if (!p)
286 return -ENOMEM;
288 strcpy(p->alg_name, algo->name);
289 *algpp = p;
290 return 0;
293 static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
295 struct xfrm_algo *p, *ualg;
296 struct xfrm_algo_desc *algo;
298 if (!rta)
299 return 0;
301 ualg = nla_data(rta);
303 algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
304 if (!algo)
305 return -ENOSYS;
306 x->props.ealgo = algo->desc.sadb_alg_id;
308 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
309 if (!p)
310 return -ENOMEM;
312 strcpy(p->alg_name, algo->name);
313 x->ealg = p;
314 x->geniv = algo->uinfo.encr.geniv;
315 return 0;
318 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
319 struct nlattr *rta)
321 struct xfrm_algo *ualg;
322 struct xfrm_algo_auth *p;
323 struct xfrm_algo_desc *algo;
325 if (!rta)
326 return 0;
328 ualg = nla_data(rta);
330 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
331 if (!algo)
332 return -ENOSYS;
333 *props = algo->desc.sadb_alg_id;
335 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
336 if (!p)
337 return -ENOMEM;
339 strcpy(p->alg_name, algo->name);
340 p->alg_key_len = ualg->alg_key_len;
341 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
342 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
344 *algpp = p;
345 return 0;
348 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
349 struct nlattr *rta)
351 struct xfrm_algo_auth *p, *ualg;
352 struct xfrm_algo_desc *algo;
354 if (!rta)
355 return 0;
357 ualg = nla_data(rta);
359 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
360 if (!algo)
361 return -ENOSYS;
362 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
363 return -EINVAL;
364 *props = algo->desc.sadb_alg_id;
366 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
367 if (!p)
368 return -ENOMEM;
370 strcpy(p->alg_name, algo->name);
371 if (!p->alg_trunc_len)
372 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
374 *algpp = p;
375 return 0;
378 static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
380 struct xfrm_algo_aead *p, *ualg;
381 struct xfrm_algo_desc *algo;
383 if (!rta)
384 return 0;
386 ualg = nla_data(rta);
388 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
389 if (!algo)
390 return -ENOSYS;
391 x->props.ealgo = algo->desc.sadb_alg_id;
393 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
394 if (!p)
395 return -ENOMEM;
397 strcpy(p->alg_name, algo->name);
398 x->aead = p;
399 x->geniv = algo->uinfo.aead.geniv;
400 return 0;
403 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
404 struct nlattr *rp)
406 struct xfrm_replay_state_esn *up;
407 int ulen;
409 if (!replay_esn || !rp)
410 return 0;
412 up = nla_data(rp);
413 ulen = xfrm_replay_state_esn_len(up);
415 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
416 return -EINVAL;
418 return 0;
421 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
422 struct xfrm_replay_state_esn **preplay_esn,
423 struct nlattr *rta)
425 struct xfrm_replay_state_esn *p, *pp, *up;
426 int klen, ulen;
428 if (!rta)
429 return 0;
431 up = nla_data(rta);
432 klen = xfrm_replay_state_esn_len(up);
433 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
435 p = kzalloc(klen, GFP_KERNEL);
436 if (!p)
437 return -ENOMEM;
439 pp = kzalloc(klen, GFP_KERNEL);
440 if (!pp) {
441 kfree(p);
442 return -ENOMEM;
445 memcpy(p, up, ulen);
446 memcpy(pp, up, ulen);
448 *replay_esn = p;
449 *preplay_esn = pp;
451 return 0;
454 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
456 int len = 0;
458 if (xfrm_ctx) {
459 len += sizeof(struct xfrm_user_sec_ctx);
460 len += xfrm_ctx->ctx_len;
462 return len;
465 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
467 memcpy(&x->id, &p->id, sizeof(x->id));
468 memcpy(&x->sel, &p->sel, sizeof(x->sel));
469 memcpy(&x->lft, &p->lft, sizeof(x->lft));
470 x->props.mode = p->mode;
471 x->props.replay_window = min_t(unsigned int, p->replay_window,
472 sizeof(x->replay.bitmap) * 8);
473 x->props.reqid = p->reqid;
474 x->props.family = p->family;
475 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
476 x->props.flags = p->flags;
478 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
479 x->sel.family = p->family;
483 * someday when pfkey also has support, we could have the code
484 * somehow made shareable and move it to xfrm_state.c - JHS
487 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
488 int update_esn)
490 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
491 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
492 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
493 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
494 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
496 if (re) {
497 struct xfrm_replay_state_esn *replay_esn;
498 replay_esn = nla_data(re);
499 memcpy(x->replay_esn, replay_esn,
500 xfrm_replay_state_esn_len(replay_esn));
501 memcpy(x->preplay_esn, replay_esn,
502 xfrm_replay_state_esn_len(replay_esn));
505 if (rp) {
506 struct xfrm_replay_state *replay;
507 replay = nla_data(rp);
508 memcpy(&x->replay, replay, sizeof(*replay));
509 memcpy(&x->preplay, replay, sizeof(*replay));
512 if (lt) {
513 struct xfrm_lifetime_cur *ltime;
514 ltime = nla_data(lt);
515 x->curlft.bytes = ltime->bytes;
516 x->curlft.packets = ltime->packets;
517 x->curlft.add_time = ltime->add_time;
518 x->curlft.use_time = ltime->use_time;
521 if (et)
522 x->replay_maxage = nla_get_u32(et);
524 if (rt)
525 x->replay_maxdiff = nla_get_u32(rt);
528 static struct xfrm_state *xfrm_state_construct(struct net *net,
529 struct xfrm_usersa_info *p,
530 struct nlattr **attrs,
531 int *errp)
533 struct xfrm_state *x = xfrm_state_alloc(net);
534 int err = -ENOMEM;
536 if (!x)
537 goto error_no_put;
539 copy_from_user_state(x, p);
541 if (attrs[XFRMA_SA_EXTRA_FLAGS])
542 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
544 if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
545 goto error;
546 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
547 attrs[XFRMA_ALG_AUTH_TRUNC])))
548 goto error;
549 if (!x->props.aalgo) {
550 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
551 attrs[XFRMA_ALG_AUTH])))
552 goto error;
554 if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
555 goto error;
556 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
557 xfrm_calg_get_byname,
558 attrs[XFRMA_ALG_COMP])))
559 goto error;
561 if (attrs[XFRMA_ENCAP]) {
562 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
563 sizeof(*x->encap), GFP_KERNEL);
564 if (x->encap == NULL)
565 goto error;
568 if (attrs[XFRMA_TFCPAD])
569 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
571 if (attrs[XFRMA_COADDR]) {
572 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
573 sizeof(*x->coaddr), GFP_KERNEL);
574 if (x->coaddr == NULL)
575 goto error;
578 xfrm_mark_get(attrs, &x->mark);
580 err = __xfrm_init_state(x, false);
581 if (err)
582 goto error;
584 if (attrs[XFRMA_SEC_CTX] &&
585 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
586 goto error;
588 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
589 attrs[XFRMA_REPLAY_ESN_VAL])))
590 goto error;
592 x->km.seq = p->seq;
593 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
594 /* sysctl_xfrm_aevent_etime is in 100ms units */
595 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
597 if ((err = xfrm_init_replay(x)))
598 goto error;
600 /* override default values from above */
601 xfrm_update_ae_params(x, attrs, 0);
603 return x;
605 error:
606 x->km.state = XFRM_STATE_DEAD;
607 xfrm_state_put(x);
608 error_no_put:
609 *errp = err;
610 return NULL;
613 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
614 struct nlattr **attrs)
616 struct net *net = sock_net(skb->sk);
617 struct xfrm_usersa_info *p = nlmsg_data(nlh);
618 struct xfrm_state *x;
619 int err;
620 struct km_event c;
622 err = verify_newsa_info(p, attrs);
623 if (err)
624 return err;
626 x = xfrm_state_construct(net, p, attrs, &err);
627 if (!x)
628 return err;
630 xfrm_state_hold(x);
631 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
632 err = xfrm_state_add(x);
633 else
634 err = xfrm_state_update(x);
636 xfrm_audit_state_add(x, err ? 0 : 1, true);
638 if (err < 0) {
639 x->km.state = XFRM_STATE_DEAD;
640 __xfrm_state_put(x);
641 goto out;
644 c.seq = nlh->nlmsg_seq;
645 c.portid = nlh->nlmsg_pid;
646 c.event = nlh->nlmsg_type;
648 km_state_notify(x, &c);
649 out:
650 xfrm_state_put(x);
651 return err;
654 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
655 struct xfrm_usersa_id *p,
656 struct nlattr **attrs,
657 int *errp)
659 struct xfrm_state *x = NULL;
660 struct xfrm_mark m;
661 int err;
662 u32 mark = xfrm_mark_get(attrs, &m);
664 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
665 err = -ESRCH;
666 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
667 } else {
668 xfrm_address_t *saddr = NULL;
670 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
671 if (!saddr) {
672 err = -EINVAL;
673 goto out;
676 err = -ESRCH;
677 x = xfrm_state_lookup_byaddr(net, mark,
678 &p->daddr, saddr,
679 p->proto, p->family);
682 out:
683 if (!x && errp)
684 *errp = err;
685 return x;
688 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
689 struct nlattr **attrs)
691 struct net *net = sock_net(skb->sk);
692 struct xfrm_state *x;
693 int err = -ESRCH;
694 struct km_event c;
695 struct xfrm_usersa_id *p = nlmsg_data(nlh);
697 x = xfrm_user_state_lookup(net, p, attrs, &err);
698 if (x == NULL)
699 return err;
701 if ((err = security_xfrm_state_delete(x)) != 0)
702 goto out;
704 if (xfrm_state_kern(x)) {
705 err = -EPERM;
706 goto out;
709 err = xfrm_state_delete(x);
711 if (err < 0)
712 goto out;
714 c.seq = nlh->nlmsg_seq;
715 c.portid = nlh->nlmsg_pid;
716 c.event = nlh->nlmsg_type;
717 km_state_notify(x, &c);
719 out:
720 xfrm_audit_state_delete(x, err ? 0 : 1, true);
721 xfrm_state_put(x);
722 return err;
725 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
727 memset(p, 0, sizeof(*p));
728 memcpy(&p->id, &x->id, sizeof(p->id));
729 memcpy(&p->sel, &x->sel, sizeof(p->sel));
730 memcpy(&p->lft, &x->lft, sizeof(p->lft));
731 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
732 put_unaligned(x->stats.replay_window, &p->stats.replay_window);
733 put_unaligned(x->stats.replay, &p->stats.replay);
734 put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
735 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
736 p->mode = x->props.mode;
737 p->replay_window = x->props.replay_window;
738 p->reqid = x->props.reqid;
739 p->family = x->props.family;
740 p->flags = x->props.flags;
741 p->seq = x->km.seq;
744 struct xfrm_dump_info {
745 struct sk_buff *in_skb;
746 struct sk_buff *out_skb;
747 u32 nlmsg_seq;
748 u16 nlmsg_flags;
751 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
753 struct xfrm_user_sec_ctx *uctx;
754 struct nlattr *attr;
755 int ctx_size = sizeof(*uctx) + s->ctx_len;
757 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
758 if (attr == NULL)
759 return -EMSGSIZE;
761 uctx = nla_data(attr);
762 uctx->exttype = XFRMA_SEC_CTX;
763 uctx->len = ctx_size;
764 uctx->ctx_doi = s->ctx_doi;
765 uctx->ctx_alg = s->ctx_alg;
766 uctx->ctx_len = s->ctx_len;
767 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
769 return 0;
772 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
774 struct xfrm_algo *algo;
775 struct nlattr *nla;
777 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
778 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
779 if (!nla)
780 return -EMSGSIZE;
782 algo = nla_data(nla);
783 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
784 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
785 algo->alg_key_len = auth->alg_key_len;
787 return 0;
790 /* Don't change this without updating xfrm_sa_len! */
791 static int copy_to_user_state_extra(struct xfrm_state *x,
792 struct xfrm_usersa_info *p,
793 struct sk_buff *skb)
795 int ret = 0;
797 copy_to_user_state(x, p);
799 if (x->props.extra_flags) {
800 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
801 x->props.extra_flags);
802 if (ret)
803 goto out;
806 if (x->coaddr) {
807 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
808 if (ret)
809 goto out;
811 if (x->lastused) {
812 ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
813 XFRMA_PAD);
814 if (ret)
815 goto out;
817 if (x->aead) {
818 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
819 if (ret)
820 goto out;
822 if (x->aalg) {
823 ret = copy_to_user_auth(x->aalg, skb);
824 if (!ret)
825 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
826 xfrm_alg_auth_len(x->aalg), x->aalg);
827 if (ret)
828 goto out;
830 if (x->ealg) {
831 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
832 if (ret)
833 goto out;
835 if (x->calg) {
836 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
837 if (ret)
838 goto out;
840 if (x->encap) {
841 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
842 if (ret)
843 goto out;
845 if (x->tfcpad) {
846 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
847 if (ret)
848 goto out;
850 ret = xfrm_mark_put(skb, &x->mark);
851 if (ret)
852 goto out;
853 if (x->replay_esn)
854 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
855 xfrm_replay_state_esn_len(x->replay_esn),
856 x->replay_esn);
857 else
858 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
859 &x->replay);
860 if (ret)
861 goto out;
862 if (x->security)
863 ret = copy_sec_ctx(x->security, skb);
864 out:
865 return ret;
868 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
870 struct xfrm_dump_info *sp = ptr;
871 struct sk_buff *in_skb = sp->in_skb;
872 struct sk_buff *skb = sp->out_skb;
873 struct xfrm_usersa_info *p;
874 struct nlmsghdr *nlh;
875 int err;
877 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
878 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
879 if (nlh == NULL)
880 return -EMSGSIZE;
882 p = nlmsg_data(nlh);
884 err = copy_to_user_state_extra(x, p, skb);
885 if (err) {
886 nlmsg_cancel(skb, nlh);
887 return err;
889 nlmsg_end(skb, nlh);
890 return 0;
893 static int xfrm_dump_sa_done(struct netlink_callback *cb)
895 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
896 struct sock *sk = cb->skb->sk;
897 struct net *net = sock_net(sk);
899 if (cb->args[0])
900 xfrm_state_walk_done(walk, net);
901 return 0;
904 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
905 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
907 struct net *net = sock_net(skb->sk);
908 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
909 struct xfrm_dump_info info;
911 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
912 sizeof(cb->args) - sizeof(cb->args[0]));
914 info.in_skb = cb->skb;
915 info.out_skb = skb;
916 info.nlmsg_seq = cb->nlh->nlmsg_seq;
917 info.nlmsg_flags = NLM_F_MULTI;
919 if (!cb->args[0]) {
920 struct nlattr *attrs[XFRMA_MAX+1];
921 struct xfrm_address_filter *filter = NULL;
922 u8 proto = 0;
923 int err;
925 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
926 xfrma_policy);
927 if (err < 0)
928 return err;
930 if (attrs[XFRMA_ADDRESS_FILTER]) {
931 filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
932 sizeof(*filter), GFP_KERNEL);
933 if (filter == NULL)
934 return -ENOMEM;
937 if (attrs[XFRMA_PROTO])
938 proto = nla_get_u8(attrs[XFRMA_PROTO]);
940 xfrm_state_walk_init(walk, proto, filter);
941 cb->args[0] = 1;
944 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
946 return skb->len;
949 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
950 struct xfrm_state *x, u32 seq)
952 struct xfrm_dump_info info;
953 struct sk_buff *skb;
954 int err;
956 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
957 if (!skb)
958 return ERR_PTR(-ENOMEM);
960 info.in_skb = in_skb;
961 info.out_skb = skb;
962 info.nlmsg_seq = seq;
963 info.nlmsg_flags = 0;
965 err = dump_one_state(x, 0, &info);
966 if (err) {
967 kfree_skb(skb);
968 return ERR_PTR(err);
971 return skb;
974 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
975 * Must be called with RCU read lock.
977 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
978 u32 pid, unsigned int group)
980 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
982 if (nlsk)
983 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
984 else
985 return -1;
988 static inline size_t xfrm_spdinfo_msgsize(void)
990 return NLMSG_ALIGN(4)
991 + nla_total_size(sizeof(struct xfrmu_spdinfo))
992 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
993 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
994 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
997 static int build_spdinfo(struct sk_buff *skb, struct net *net,
998 u32 portid, u32 seq, u32 flags)
1000 struct xfrmk_spdinfo si;
1001 struct xfrmu_spdinfo spc;
1002 struct xfrmu_spdhinfo sph;
1003 struct xfrmu_spdhthresh spt4, spt6;
1004 struct nlmsghdr *nlh;
1005 int err;
1006 u32 *f;
1007 unsigned lseq;
1009 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1010 if (nlh == NULL) /* shouldn't really happen ... */
1011 return -EMSGSIZE;
1013 f = nlmsg_data(nlh);
1014 *f = flags;
1015 xfrm_spd_getinfo(net, &si);
1016 spc.incnt = si.incnt;
1017 spc.outcnt = si.outcnt;
1018 spc.fwdcnt = si.fwdcnt;
1019 spc.inscnt = si.inscnt;
1020 spc.outscnt = si.outscnt;
1021 spc.fwdscnt = si.fwdscnt;
1022 sph.spdhcnt = si.spdhcnt;
1023 sph.spdhmcnt = si.spdhmcnt;
1025 do {
1026 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1028 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1029 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1030 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1031 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1032 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1034 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1035 if (!err)
1036 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1037 if (!err)
1038 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1039 if (!err)
1040 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1041 if (err) {
1042 nlmsg_cancel(skb, nlh);
1043 return err;
1046 nlmsg_end(skb, nlh);
1047 return 0;
1050 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1051 struct nlattr **attrs)
1053 struct net *net = sock_net(skb->sk);
1054 struct xfrmu_spdhthresh *thresh4 = NULL;
1055 struct xfrmu_spdhthresh *thresh6 = NULL;
1057 /* selector prefixlen thresholds to hash policies */
1058 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1059 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1061 if (nla_len(rta) < sizeof(*thresh4))
1062 return -EINVAL;
1063 thresh4 = nla_data(rta);
1064 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1065 return -EINVAL;
1067 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1068 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1070 if (nla_len(rta) < sizeof(*thresh6))
1071 return -EINVAL;
1072 thresh6 = nla_data(rta);
1073 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1074 return -EINVAL;
1077 if (thresh4 || thresh6) {
1078 write_seqlock(&net->xfrm.policy_hthresh.lock);
1079 if (thresh4) {
1080 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1081 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1083 if (thresh6) {
1084 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1085 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1087 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1089 xfrm_policy_hash_rebuild(net);
1092 return 0;
1095 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1096 struct nlattr **attrs)
1098 struct net *net = sock_net(skb->sk);
1099 struct sk_buff *r_skb;
1100 u32 *flags = nlmsg_data(nlh);
1101 u32 sportid = NETLINK_CB(skb).portid;
1102 u32 seq = nlh->nlmsg_seq;
1104 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1105 if (r_skb == NULL)
1106 return -ENOMEM;
1108 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1109 BUG();
1111 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1114 static inline size_t xfrm_sadinfo_msgsize(void)
1116 return NLMSG_ALIGN(4)
1117 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1118 + nla_total_size(4); /* XFRMA_SAD_CNT */
1121 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1122 u32 portid, u32 seq, u32 flags)
1124 struct xfrmk_sadinfo si;
1125 struct xfrmu_sadhinfo sh;
1126 struct nlmsghdr *nlh;
1127 int err;
1128 u32 *f;
1130 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1131 if (nlh == NULL) /* shouldn't really happen ... */
1132 return -EMSGSIZE;
1134 f = nlmsg_data(nlh);
1135 *f = flags;
1136 xfrm_sad_getinfo(net, &si);
1138 sh.sadhmcnt = si.sadhmcnt;
1139 sh.sadhcnt = si.sadhcnt;
1141 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1142 if (!err)
1143 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1144 if (err) {
1145 nlmsg_cancel(skb, nlh);
1146 return err;
1149 nlmsg_end(skb, nlh);
1150 return 0;
1153 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1154 struct nlattr **attrs)
1156 struct net *net = sock_net(skb->sk);
1157 struct sk_buff *r_skb;
1158 u32 *flags = nlmsg_data(nlh);
1159 u32 sportid = NETLINK_CB(skb).portid;
1160 u32 seq = nlh->nlmsg_seq;
1162 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1163 if (r_skb == NULL)
1164 return -ENOMEM;
1166 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1167 BUG();
1169 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1172 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1173 struct nlattr **attrs)
1175 struct net *net = sock_net(skb->sk);
1176 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1177 struct xfrm_state *x;
1178 struct sk_buff *resp_skb;
1179 int err = -ESRCH;
1181 x = xfrm_user_state_lookup(net, p, attrs, &err);
1182 if (x == NULL)
1183 goto out_noput;
1185 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1186 if (IS_ERR(resp_skb)) {
1187 err = PTR_ERR(resp_skb);
1188 } else {
1189 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1191 xfrm_state_put(x);
1192 out_noput:
1193 return err;
1196 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1197 struct nlattr **attrs)
1199 struct net *net = sock_net(skb->sk);
1200 struct xfrm_state *x;
1201 struct xfrm_userspi_info *p;
1202 struct sk_buff *resp_skb;
1203 xfrm_address_t *daddr;
1204 int family;
1205 int err;
1206 u32 mark;
1207 struct xfrm_mark m;
1209 p = nlmsg_data(nlh);
1210 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1211 if (err)
1212 goto out_noput;
1214 family = p->info.family;
1215 daddr = &p->info.id.daddr;
1217 x = NULL;
1219 mark = xfrm_mark_get(attrs, &m);
1220 if (p->info.seq) {
1221 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1222 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1223 xfrm_state_put(x);
1224 x = NULL;
1228 if (!x)
1229 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1230 p->info.id.proto, daddr,
1231 &p->info.saddr, 1,
1232 family);
1233 err = -ENOENT;
1234 if (x == NULL)
1235 goto out_noput;
1237 err = xfrm_alloc_spi(x, p->min, p->max);
1238 if (err)
1239 goto out;
1241 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1242 if (IS_ERR(resp_skb)) {
1243 err = PTR_ERR(resp_skb);
1244 goto out;
1247 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1249 out:
1250 xfrm_state_put(x);
1251 out_noput:
1252 return err;
1255 static int verify_policy_dir(u8 dir)
1257 switch (dir) {
1258 case XFRM_POLICY_IN:
1259 case XFRM_POLICY_OUT:
1260 case XFRM_POLICY_FWD:
1261 break;
1263 default:
1264 return -EINVAL;
1267 return 0;
1270 static int verify_policy_type(u8 type)
1272 switch (type) {
1273 case XFRM_POLICY_TYPE_MAIN:
1274 #ifdef CONFIG_XFRM_SUB_POLICY
1275 case XFRM_POLICY_TYPE_SUB:
1276 #endif
1277 break;
1279 default:
1280 return -EINVAL;
1283 return 0;
1286 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1288 int ret;
1290 switch (p->share) {
1291 case XFRM_SHARE_ANY:
1292 case XFRM_SHARE_SESSION:
1293 case XFRM_SHARE_USER:
1294 case XFRM_SHARE_UNIQUE:
1295 break;
1297 default:
1298 return -EINVAL;
1301 switch (p->action) {
1302 case XFRM_POLICY_ALLOW:
1303 case XFRM_POLICY_BLOCK:
1304 break;
1306 default:
1307 return -EINVAL;
1310 switch (p->sel.family) {
1311 case AF_INET:
1312 break;
1314 case AF_INET6:
1315 #if IS_ENABLED(CONFIG_IPV6)
1316 break;
1317 #else
1318 return -EAFNOSUPPORT;
1319 #endif
1321 default:
1322 return -EINVAL;
1325 ret = verify_policy_dir(p->dir);
1326 if (ret)
1327 return ret;
1328 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1329 return -EINVAL;
1331 return 0;
1334 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1336 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1337 struct xfrm_user_sec_ctx *uctx;
1339 if (!rt)
1340 return 0;
1342 uctx = nla_data(rt);
1343 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1346 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1347 int nr)
1349 int i;
1351 xp->xfrm_nr = nr;
1352 for (i = 0; i < nr; i++, ut++) {
1353 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1355 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1356 memcpy(&t->saddr, &ut->saddr,
1357 sizeof(xfrm_address_t));
1358 t->reqid = ut->reqid;
1359 t->mode = ut->mode;
1360 t->share = ut->share;
1361 t->optional = ut->optional;
1362 t->aalgos = ut->aalgos;
1363 t->ealgos = ut->ealgos;
1364 t->calgos = ut->calgos;
1365 /* If all masks are ~0, then we allow all algorithms. */
1366 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1367 t->encap_family = ut->family;
1371 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1373 int i;
1375 if (nr > XFRM_MAX_DEPTH)
1376 return -EINVAL;
1378 for (i = 0; i < nr; i++) {
1379 /* We never validated the ut->family value, so many
1380 * applications simply leave it at zero. The check was
1381 * never made and ut->family was ignored because all
1382 * templates could be assumed to have the same family as
1383 * the policy itself. Now that we will have ipv4-in-ipv6
1384 * and ipv6-in-ipv4 tunnels, this is no longer true.
1386 if (!ut[i].family)
1387 ut[i].family = family;
1389 switch (ut[i].family) {
1390 case AF_INET:
1391 break;
1392 #if IS_ENABLED(CONFIG_IPV6)
1393 case AF_INET6:
1394 break;
1395 #endif
1396 default:
1397 return -EINVAL;
1401 return 0;
1404 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1406 struct nlattr *rt = attrs[XFRMA_TMPL];
1408 if (!rt) {
1409 pol->xfrm_nr = 0;
1410 } else {
1411 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1412 int nr = nla_len(rt) / sizeof(*utmpl);
1413 int err;
1415 err = validate_tmpl(nr, utmpl, pol->family);
1416 if (err)
1417 return err;
1419 copy_templates(pol, utmpl, nr);
1421 return 0;
1424 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1426 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1427 struct xfrm_userpolicy_type *upt;
1428 u8 type = XFRM_POLICY_TYPE_MAIN;
1429 int err;
1431 if (rt) {
1432 upt = nla_data(rt);
1433 type = upt->type;
1436 err = verify_policy_type(type);
1437 if (err)
1438 return err;
1440 *tp = type;
1441 return 0;
1444 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1446 xp->priority = p->priority;
1447 xp->index = p->index;
1448 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1449 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1450 xp->action = p->action;
1451 xp->flags = p->flags;
1452 xp->family = p->sel.family;
1453 /* XXX xp->share = p->share; */
1456 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1458 memset(p, 0, sizeof(*p));
1459 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1460 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1461 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1462 p->priority = xp->priority;
1463 p->index = xp->index;
1464 p->sel.family = xp->family;
1465 p->dir = dir;
1466 p->action = xp->action;
1467 p->flags = xp->flags;
1468 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1471 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1473 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1474 int err;
1476 if (!xp) {
1477 *errp = -ENOMEM;
1478 return NULL;
1481 copy_from_user_policy(xp, p);
1483 err = copy_from_user_policy_type(&xp->type, attrs);
1484 if (err)
1485 goto error;
1487 if (!(err = copy_from_user_tmpl(xp, attrs)))
1488 err = copy_from_user_sec_ctx(xp, attrs);
1489 if (err)
1490 goto error;
1492 xfrm_mark_get(attrs, &xp->mark);
1494 return xp;
1495 error:
1496 *errp = err;
1497 xp->walk.dead = 1;
1498 xfrm_policy_destroy(xp);
1499 return NULL;
1502 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1503 struct nlattr **attrs)
1505 struct net *net = sock_net(skb->sk);
1506 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1507 struct xfrm_policy *xp;
1508 struct km_event c;
1509 int err;
1510 int excl;
1512 err = verify_newpolicy_info(p);
1513 if (err)
1514 return err;
1515 err = verify_sec_ctx_len(attrs);
1516 if (err)
1517 return err;
1519 xp = xfrm_policy_construct(net, p, attrs, &err);
1520 if (!xp)
1521 return err;
1523 /* shouldn't excl be based on nlh flags??
1524 * Aha! this is anti-netlink really i.e more pfkey derived
1525 * in netlink excl is a flag and you wouldnt need
1526 * a type XFRM_MSG_UPDPOLICY - JHS */
1527 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1528 err = xfrm_policy_insert(p->dir, xp, excl);
1529 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1531 if (err) {
1532 security_xfrm_policy_free(xp->security);
1533 kfree(xp);
1534 return err;
1537 c.event = nlh->nlmsg_type;
1538 c.seq = nlh->nlmsg_seq;
1539 c.portid = nlh->nlmsg_pid;
1540 km_policy_notify(xp, p->dir, &c);
1542 xfrm_pol_put(xp);
1544 return 0;
1547 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1549 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1550 int i;
1552 if (xp->xfrm_nr == 0)
1553 return 0;
1555 for (i = 0; i < xp->xfrm_nr; i++) {
1556 struct xfrm_user_tmpl *up = &vec[i];
1557 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1559 memset(up, 0, sizeof(*up));
1560 memcpy(&up->id, &kp->id, sizeof(up->id));
1561 up->family = kp->encap_family;
1562 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1563 up->reqid = kp->reqid;
1564 up->mode = kp->mode;
1565 up->share = kp->share;
1566 up->optional = kp->optional;
1567 up->aalgos = kp->aalgos;
1568 up->ealgos = kp->ealgos;
1569 up->calgos = kp->calgos;
1572 return nla_put(skb, XFRMA_TMPL,
1573 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1576 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1578 if (x->security) {
1579 return copy_sec_ctx(x->security, skb);
1581 return 0;
1584 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1586 if (xp->security)
1587 return copy_sec_ctx(xp->security, skb);
1588 return 0;
1590 static inline size_t userpolicy_type_attrsize(void)
1592 #ifdef CONFIG_XFRM_SUB_POLICY
1593 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1594 #else
1595 return 0;
1596 #endif
1599 #ifdef CONFIG_XFRM_SUB_POLICY
1600 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1602 struct xfrm_userpolicy_type upt = {
1603 .type = type,
1606 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1609 #else
1610 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1612 return 0;
1614 #endif
1616 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1618 struct xfrm_dump_info *sp = ptr;
1619 struct xfrm_userpolicy_info *p;
1620 struct sk_buff *in_skb = sp->in_skb;
1621 struct sk_buff *skb = sp->out_skb;
1622 struct nlmsghdr *nlh;
1623 int err;
1625 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1626 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1627 if (nlh == NULL)
1628 return -EMSGSIZE;
1630 p = nlmsg_data(nlh);
1631 copy_to_user_policy(xp, p, dir);
1632 err = copy_to_user_tmpl(xp, skb);
1633 if (!err)
1634 err = copy_to_user_sec_ctx(xp, skb);
1635 if (!err)
1636 err = copy_to_user_policy_type(xp->type, skb);
1637 if (!err)
1638 err = xfrm_mark_put(skb, &xp->mark);
1639 if (err) {
1640 nlmsg_cancel(skb, nlh);
1641 return err;
1643 nlmsg_end(skb, nlh);
1644 return 0;
1647 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1649 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1650 struct net *net = sock_net(cb->skb->sk);
1652 xfrm_policy_walk_done(walk, net);
1653 return 0;
1656 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1658 struct net *net = sock_net(skb->sk);
1659 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1660 struct xfrm_dump_info info;
1662 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1663 sizeof(cb->args) - sizeof(cb->args[0]));
1665 info.in_skb = cb->skb;
1666 info.out_skb = skb;
1667 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1668 info.nlmsg_flags = NLM_F_MULTI;
1670 if (!cb->args[0]) {
1671 cb->args[0] = 1;
1672 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1675 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1677 return skb->len;
1680 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1681 struct xfrm_policy *xp,
1682 int dir, u32 seq)
1684 struct xfrm_dump_info info;
1685 struct sk_buff *skb;
1686 int err;
1688 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1689 if (!skb)
1690 return ERR_PTR(-ENOMEM);
1692 info.in_skb = in_skb;
1693 info.out_skb = skb;
1694 info.nlmsg_seq = seq;
1695 info.nlmsg_flags = 0;
1697 err = dump_one_policy(xp, dir, 0, &info);
1698 if (err) {
1699 kfree_skb(skb);
1700 return ERR_PTR(err);
1703 return skb;
1706 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1707 struct nlattr **attrs)
1709 struct net *net = sock_net(skb->sk);
1710 struct xfrm_policy *xp;
1711 struct xfrm_userpolicy_id *p;
1712 u8 type = XFRM_POLICY_TYPE_MAIN;
1713 int err;
1714 struct km_event c;
1715 int delete;
1716 struct xfrm_mark m;
1717 u32 mark = xfrm_mark_get(attrs, &m);
1719 p = nlmsg_data(nlh);
1720 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1722 err = copy_from_user_policy_type(&type, attrs);
1723 if (err)
1724 return err;
1726 err = verify_policy_dir(p->dir);
1727 if (err)
1728 return err;
1730 if (p->index)
1731 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1732 else {
1733 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1734 struct xfrm_sec_ctx *ctx;
1736 err = verify_sec_ctx_len(attrs);
1737 if (err)
1738 return err;
1740 ctx = NULL;
1741 if (rt) {
1742 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1744 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1745 if (err)
1746 return err;
1748 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1749 ctx, delete, &err);
1750 security_xfrm_policy_free(ctx);
1752 if (xp == NULL)
1753 return -ENOENT;
1755 if (!delete) {
1756 struct sk_buff *resp_skb;
1758 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1759 if (IS_ERR(resp_skb)) {
1760 err = PTR_ERR(resp_skb);
1761 } else {
1762 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1763 NETLINK_CB(skb).portid);
1765 } else {
1766 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1768 if (err != 0)
1769 goto out;
1771 c.data.byid = p->index;
1772 c.event = nlh->nlmsg_type;
1773 c.seq = nlh->nlmsg_seq;
1774 c.portid = nlh->nlmsg_pid;
1775 km_policy_notify(xp, p->dir, &c);
1778 out:
1779 xfrm_pol_put(xp);
1780 if (delete && err == 0)
1781 xfrm_garbage_collect(net);
1782 return err;
1785 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1786 struct nlattr **attrs)
1788 struct net *net = sock_net(skb->sk);
1789 struct km_event c;
1790 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1791 int err;
1793 err = xfrm_state_flush(net, p->proto, true);
1794 if (err) {
1795 if (err == -ESRCH) /* empty table */
1796 return 0;
1797 return err;
1799 c.data.proto = p->proto;
1800 c.event = nlh->nlmsg_type;
1801 c.seq = nlh->nlmsg_seq;
1802 c.portid = nlh->nlmsg_pid;
1803 c.net = net;
1804 km_state_notify(NULL, &c);
1806 return 0;
1809 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1811 size_t replay_size = x->replay_esn ?
1812 xfrm_replay_state_esn_len(x->replay_esn) :
1813 sizeof(struct xfrm_replay_state);
1815 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1816 + nla_total_size(replay_size)
1817 + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1818 + nla_total_size(sizeof(struct xfrm_mark))
1819 + nla_total_size(4) /* XFRM_AE_RTHR */
1820 + nla_total_size(4); /* XFRM_AE_ETHR */
1823 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1825 struct xfrm_aevent_id *id;
1826 struct nlmsghdr *nlh;
1827 int err;
1829 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1830 if (nlh == NULL)
1831 return -EMSGSIZE;
1833 id = nlmsg_data(nlh);
1834 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1835 id->sa_id.spi = x->id.spi;
1836 id->sa_id.family = x->props.family;
1837 id->sa_id.proto = x->id.proto;
1838 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1839 id->reqid = x->props.reqid;
1840 id->flags = c->data.aevent;
1842 if (x->replay_esn) {
1843 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1844 xfrm_replay_state_esn_len(x->replay_esn),
1845 x->replay_esn);
1846 } else {
1847 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1848 &x->replay);
1850 if (err)
1851 goto out_cancel;
1852 err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1853 XFRMA_PAD);
1854 if (err)
1855 goto out_cancel;
1857 if (id->flags & XFRM_AE_RTHR) {
1858 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1859 if (err)
1860 goto out_cancel;
1862 if (id->flags & XFRM_AE_ETHR) {
1863 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1864 x->replay_maxage * 10 / HZ);
1865 if (err)
1866 goto out_cancel;
1868 err = xfrm_mark_put(skb, &x->mark);
1869 if (err)
1870 goto out_cancel;
1872 nlmsg_end(skb, nlh);
1873 return 0;
1875 out_cancel:
1876 nlmsg_cancel(skb, nlh);
1877 return err;
1880 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1881 struct nlattr **attrs)
1883 struct net *net = sock_net(skb->sk);
1884 struct xfrm_state *x;
1885 struct sk_buff *r_skb;
1886 int err;
1887 struct km_event c;
1888 u32 mark;
1889 struct xfrm_mark m;
1890 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1891 struct xfrm_usersa_id *id = &p->sa_id;
1893 mark = xfrm_mark_get(attrs, &m);
1895 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1896 if (x == NULL)
1897 return -ESRCH;
1899 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1900 if (r_skb == NULL) {
1901 xfrm_state_put(x);
1902 return -ENOMEM;
1906 * XXX: is this lock really needed - none of the other
1907 * gets lock (the concern is things getting updated
1908 * while we are still reading) - jhs
1910 spin_lock_bh(&x->lock);
1911 c.data.aevent = p->flags;
1912 c.seq = nlh->nlmsg_seq;
1913 c.portid = nlh->nlmsg_pid;
1915 if (build_aevent(r_skb, x, &c) < 0)
1916 BUG();
1917 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1918 spin_unlock_bh(&x->lock);
1919 xfrm_state_put(x);
1920 return err;
1923 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1924 struct nlattr **attrs)
1926 struct net *net = sock_net(skb->sk);
1927 struct xfrm_state *x;
1928 struct km_event c;
1929 int err = -EINVAL;
1930 u32 mark = 0;
1931 struct xfrm_mark m;
1932 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1933 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1934 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1935 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1936 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
1937 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
1939 if (!lt && !rp && !re && !et && !rt)
1940 return err;
1942 /* pedantic mode - thou shalt sayeth replaceth */
1943 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1944 return err;
1946 mark = xfrm_mark_get(attrs, &m);
1948 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1949 if (x == NULL)
1950 return -ESRCH;
1952 if (x->km.state != XFRM_STATE_VALID)
1953 goto out;
1955 err = xfrm_replay_verify_len(x->replay_esn, re);
1956 if (err)
1957 goto out;
1959 spin_lock_bh(&x->lock);
1960 xfrm_update_ae_params(x, attrs, 1);
1961 spin_unlock_bh(&x->lock);
1963 c.event = nlh->nlmsg_type;
1964 c.seq = nlh->nlmsg_seq;
1965 c.portid = nlh->nlmsg_pid;
1966 c.data.aevent = XFRM_AE_CU;
1967 km_state_notify(x, &c);
1968 err = 0;
1969 out:
1970 xfrm_state_put(x);
1971 return err;
1974 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1975 struct nlattr **attrs)
1977 struct net *net = sock_net(skb->sk);
1978 struct km_event c;
1979 u8 type = XFRM_POLICY_TYPE_MAIN;
1980 int err;
1982 err = copy_from_user_policy_type(&type, attrs);
1983 if (err)
1984 return err;
1986 err = xfrm_policy_flush(net, type, true);
1987 if (err) {
1988 if (err == -ESRCH) /* empty table */
1989 return 0;
1990 return err;
1993 c.data.type = type;
1994 c.event = nlh->nlmsg_type;
1995 c.seq = nlh->nlmsg_seq;
1996 c.portid = nlh->nlmsg_pid;
1997 c.net = net;
1998 km_policy_notify(NULL, 0, &c);
1999 return 0;
2002 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2003 struct nlattr **attrs)
2005 struct net *net = sock_net(skb->sk);
2006 struct xfrm_policy *xp;
2007 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2008 struct xfrm_userpolicy_info *p = &up->pol;
2009 u8 type = XFRM_POLICY_TYPE_MAIN;
2010 int err = -ENOENT;
2011 struct xfrm_mark m;
2012 u32 mark = xfrm_mark_get(attrs, &m);
2014 err = copy_from_user_policy_type(&type, attrs);
2015 if (err)
2016 return err;
2018 err = verify_policy_dir(p->dir);
2019 if (err)
2020 return err;
2022 if (p->index)
2023 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
2024 else {
2025 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2026 struct xfrm_sec_ctx *ctx;
2028 err = verify_sec_ctx_len(attrs);
2029 if (err)
2030 return err;
2032 ctx = NULL;
2033 if (rt) {
2034 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2036 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2037 if (err)
2038 return err;
2040 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2041 &p->sel, ctx, 0, &err);
2042 security_xfrm_policy_free(ctx);
2044 if (xp == NULL)
2045 return -ENOENT;
2047 if (unlikely(xp->walk.dead))
2048 goto out;
2050 err = 0;
2051 if (up->hard) {
2052 xfrm_policy_delete(xp, p->dir);
2053 xfrm_audit_policy_delete(xp, 1, true);
2055 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2057 out:
2058 xfrm_pol_put(xp);
2059 return err;
2062 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2063 struct nlattr **attrs)
2065 struct net *net = sock_net(skb->sk);
2066 struct xfrm_state *x;
2067 int err;
2068 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2069 struct xfrm_usersa_info *p = &ue->state;
2070 struct xfrm_mark m;
2071 u32 mark = xfrm_mark_get(attrs, &m);
2073 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2075 err = -ENOENT;
2076 if (x == NULL)
2077 return err;
2079 spin_lock_bh(&x->lock);
2080 err = -EINVAL;
2081 if (x->km.state != XFRM_STATE_VALID)
2082 goto out;
2083 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2085 if (ue->hard) {
2086 __xfrm_state_delete(x);
2087 xfrm_audit_state_delete(x, 1, true);
2089 err = 0;
2090 out:
2091 spin_unlock_bh(&x->lock);
2092 xfrm_state_put(x);
2093 return err;
2096 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2097 struct nlattr **attrs)
2099 struct net *net = sock_net(skb->sk);
2100 struct xfrm_policy *xp;
2101 struct xfrm_user_tmpl *ut;
2102 int i;
2103 struct nlattr *rt = attrs[XFRMA_TMPL];
2104 struct xfrm_mark mark;
2106 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2107 struct xfrm_state *x = xfrm_state_alloc(net);
2108 int err = -ENOMEM;
2110 if (!x)
2111 goto nomem;
2113 xfrm_mark_get(attrs, &mark);
2115 err = verify_newpolicy_info(&ua->policy);
2116 if (err)
2117 goto free_state;
2119 /* build an XP */
2120 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2121 if (!xp)
2122 goto free_state;
2124 memcpy(&x->id, &ua->id, sizeof(ua->id));
2125 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2126 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2127 xp->mark.m = x->mark.m = mark.m;
2128 xp->mark.v = x->mark.v = mark.v;
2129 ut = nla_data(rt);
2130 /* extract the templates and for each call km_key */
2131 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2132 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2133 memcpy(&x->id, &t->id, sizeof(x->id));
2134 x->props.mode = t->mode;
2135 x->props.reqid = t->reqid;
2136 x->props.family = ut->family;
2137 t->aalgos = ua->aalgos;
2138 t->ealgos = ua->ealgos;
2139 t->calgos = ua->calgos;
2140 err = km_query(x, t, xp);
2144 kfree(x);
2145 kfree(xp);
2147 return 0;
2149 free_state:
2150 kfree(x);
2151 nomem:
2152 return err;
2155 #ifdef CONFIG_XFRM_MIGRATE
2156 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2157 struct xfrm_kmaddress *k,
2158 struct nlattr **attrs, int *num)
2160 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2161 struct xfrm_user_migrate *um;
2162 int i, num_migrate;
2164 if (k != NULL) {
2165 struct xfrm_user_kmaddress *uk;
2167 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2168 memcpy(&k->local, &uk->local, sizeof(k->local));
2169 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2170 k->family = uk->family;
2171 k->reserved = uk->reserved;
2174 um = nla_data(rt);
2175 num_migrate = nla_len(rt) / sizeof(*um);
2177 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2178 return -EINVAL;
2180 for (i = 0; i < num_migrate; i++, um++, ma++) {
2181 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2182 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2183 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2184 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2186 ma->proto = um->proto;
2187 ma->mode = um->mode;
2188 ma->reqid = um->reqid;
2190 ma->old_family = um->old_family;
2191 ma->new_family = um->new_family;
2194 *num = i;
2195 return 0;
2198 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2199 struct nlattr **attrs)
2201 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2202 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2203 struct xfrm_kmaddress km, *kmp;
2204 u8 type;
2205 int err;
2206 int n = 0;
2207 struct net *net = sock_net(skb->sk);
2209 if (attrs[XFRMA_MIGRATE] == NULL)
2210 return -EINVAL;
2212 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2214 err = copy_from_user_policy_type(&type, attrs);
2215 if (err)
2216 return err;
2218 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2219 if (err)
2220 return err;
2222 if (!n)
2223 return 0;
2225 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2227 return 0;
2229 #else
2230 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2231 struct nlattr **attrs)
2233 return -ENOPROTOOPT;
2235 #endif
2237 #ifdef CONFIG_XFRM_MIGRATE
2238 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2240 struct xfrm_user_migrate um;
2242 memset(&um, 0, sizeof(um));
2243 um.proto = m->proto;
2244 um.mode = m->mode;
2245 um.reqid = m->reqid;
2246 um.old_family = m->old_family;
2247 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2248 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2249 um.new_family = m->new_family;
2250 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2251 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2253 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2256 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2258 struct xfrm_user_kmaddress uk;
2260 memset(&uk, 0, sizeof(uk));
2261 uk.family = k->family;
2262 uk.reserved = k->reserved;
2263 memcpy(&uk.local, &k->local, sizeof(uk.local));
2264 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2266 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2269 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2271 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2272 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2273 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2274 + userpolicy_type_attrsize();
2277 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2278 int num_migrate, const struct xfrm_kmaddress *k,
2279 const struct xfrm_selector *sel, u8 dir, u8 type)
2281 const struct xfrm_migrate *mp;
2282 struct xfrm_userpolicy_id *pol_id;
2283 struct nlmsghdr *nlh;
2284 int i, err;
2286 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2287 if (nlh == NULL)
2288 return -EMSGSIZE;
2290 pol_id = nlmsg_data(nlh);
2291 /* copy data from selector, dir, and type to the pol_id */
2292 memset(pol_id, 0, sizeof(*pol_id));
2293 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2294 pol_id->dir = dir;
2296 if (k != NULL) {
2297 err = copy_to_user_kmaddress(k, skb);
2298 if (err)
2299 goto out_cancel;
2301 err = copy_to_user_policy_type(type, skb);
2302 if (err)
2303 goto out_cancel;
2304 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2305 err = copy_to_user_migrate(mp, skb);
2306 if (err)
2307 goto out_cancel;
2310 nlmsg_end(skb, nlh);
2311 return 0;
2313 out_cancel:
2314 nlmsg_cancel(skb, nlh);
2315 return err;
2318 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2319 const struct xfrm_migrate *m, int num_migrate,
2320 const struct xfrm_kmaddress *k)
2322 struct net *net = &init_net;
2323 struct sk_buff *skb;
2325 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2326 if (skb == NULL)
2327 return -ENOMEM;
2329 /* build migrate */
2330 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2331 BUG();
2333 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2335 #else
2336 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2337 const struct xfrm_migrate *m, int num_migrate,
2338 const struct xfrm_kmaddress *k)
2340 return -ENOPROTOOPT;
2342 #endif
2344 #define XMSGSIZE(type) sizeof(struct type)
2346 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2347 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2348 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2349 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2350 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2351 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2352 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2353 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2354 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2355 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2356 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2357 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2358 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2359 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2360 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2361 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2362 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2363 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2364 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2365 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2366 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2367 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2370 #undef XMSGSIZE
2372 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2373 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2374 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2375 [XFRMA_LASTUSED] = { .type = NLA_U64},
2376 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2377 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2378 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2379 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2380 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2381 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2382 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2383 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2384 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2385 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2386 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2387 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2388 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2389 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2390 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2391 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2392 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2393 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2394 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2395 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2396 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2397 [XFRMA_PROTO] = { .type = NLA_U8 },
2398 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2401 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2402 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2403 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2406 static const struct xfrm_link {
2407 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2408 int (*dump)(struct sk_buff *, struct netlink_callback *);
2409 int (*done)(struct netlink_callback *);
2410 const struct nla_policy *nla_pol;
2411 int nla_max;
2412 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2413 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2414 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2415 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2416 .dump = xfrm_dump_sa,
2417 .done = xfrm_dump_sa_done },
2418 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2419 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2420 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2421 .dump = xfrm_dump_policy,
2422 .done = xfrm_dump_policy_done },
2423 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2424 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2425 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2426 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2427 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2428 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2429 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2430 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2431 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2432 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2433 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2434 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2435 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2436 .nla_pol = xfrma_spd_policy,
2437 .nla_max = XFRMA_SPD_MAX },
2438 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2441 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2443 struct net *net = sock_net(skb->sk);
2444 struct nlattr *attrs[XFRMA_MAX+1];
2445 const struct xfrm_link *link;
2446 int type, err;
2448 #ifdef CONFIG_COMPAT
2449 if (in_compat_syscall())
2450 return -ENOTSUPP;
2451 #endif
2453 type = nlh->nlmsg_type;
2454 if (type > XFRM_MSG_MAX)
2455 return -EINVAL;
2457 type -= XFRM_MSG_BASE;
2458 link = &xfrm_dispatch[type];
2460 /* All operations require privileges, even GET */
2461 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2462 return -EPERM;
2464 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2465 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2466 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2467 if (link->dump == NULL)
2468 return -EINVAL;
2471 struct netlink_dump_control c = {
2472 .dump = link->dump,
2473 .done = link->done,
2475 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2479 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2480 link->nla_max ? : XFRMA_MAX,
2481 link->nla_pol ? : xfrma_policy);
2482 if (err < 0)
2483 return err;
2485 if (link->doit == NULL)
2486 return -EINVAL;
2488 return link->doit(skb, nlh, attrs);
2491 static void xfrm_netlink_rcv(struct sk_buff *skb)
2493 struct net *net = sock_net(skb->sk);
2495 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2496 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2497 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2500 static inline size_t xfrm_expire_msgsize(void)
2502 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2503 + nla_total_size(sizeof(struct xfrm_mark));
2506 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2508 struct xfrm_user_expire *ue;
2509 struct nlmsghdr *nlh;
2510 int err;
2512 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2513 if (nlh == NULL)
2514 return -EMSGSIZE;
2516 ue = nlmsg_data(nlh);
2517 copy_to_user_state(x, &ue->state);
2518 ue->hard = (c->data.hard != 0) ? 1 : 0;
2520 err = xfrm_mark_put(skb, &x->mark);
2521 if (err)
2522 return err;
2524 nlmsg_end(skb, nlh);
2525 return 0;
2528 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2530 struct net *net = xs_net(x);
2531 struct sk_buff *skb;
2533 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2534 if (skb == NULL)
2535 return -ENOMEM;
2537 if (build_expire(skb, x, c) < 0) {
2538 kfree_skb(skb);
2539 return -EMSGSIZE;
2542 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2545 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2547 struct net *net = xs_net(x);
2548 struct sk_buff *skb;
2550 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2551 if (skb == NULL)
2552 return -ENOMEM;
2554 if (build_aevent(skb, x, c) < 0)
2555 BUG();
2557 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2560 static int xfrm_notify_sa_flush(const struct km_event *c)
2562 struct net *net = c->net;
2563 struct xfrm_usersa_flush *p;
2564 struct nlmsghdr *nlh;
2565 struct sk_buff *skb;
2566 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2568 skb = nlmsg_new(len, GFP_ATOMIC);
2569 if (skb == NULL)
2570 return -ENOMEM;
2572 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2573 if (nlh == NULL) {
2574 kfree_skb(skb);
2575 return -EMSGSIZE;
2578 p = nlmsg_data(nlh);
2579 p->proto = c->data.proto;
2581 nlmsg_end(skb, nlh);
2583 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2586 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2588 size_t l = 0;
2589 if (x->aead)
2590 l += nla_total_size(aead_len(x->aead));
2591 if (x->aalg) {
2592 l += nla_total_size(sizeof(struct xfrm_algo) +
2593 (x->aalg->alg_key_len + 7) / 8);
2594 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2596 if (x->ealg)
2597 l += nla_total_size(xfrm_alg_len(x->ealg));
2598 if (x->calg)
2599 l += nla_total_size(sizeof(*x->calg));
2600 if (x->encap)
2601 l += nla_total_size(sizeof(*x->encap));
2602 if (x->tfcpad)
2603 l += nla_total_size(sizeof(x->tfcpad));
2604 if (x->replay_esn)
2605 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2606 else
2607 l += nla_total_size(sizeof(struct xfrm_replay_state));
2608 if (x->security)
2609 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2610 x->security->ctx_len);
2611 if (x->coaddr)
2612 l += nla_total_size(sizeof(*x->coaddr));
2613 if (x->props.extra_flags)
2614 l += nla_total_size(sizeof(x->props.extra_flags));
2616 /* Must count x->lastused as it may become non-zero behind our back. */
2617 l += nla_total_size_64bit(sizeof(u64));
2619 return l;
2622 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2624 struct net *net = xs_net(x);
2625 struct xfrm_usersa_info *p;
2626 struct xfrm_usersa_id *id;
2627 struct nlmsghdr *nlh;
2628 struct sk_buff *skb;
2629 int len = xfrm_sa_len(x);
2630 int headlen, err;
2632 headlen = sizeof(*p);
2633 if (c->event == XFRM_MSG_DELSA) {
2634 len += nla_total_size(headlen);
2635 headlen = sizeof(*id);
2636 len += nla_total_size(sizeof(struct xfrm_mark));
2638 len += NLMSG_ALIGN(headlen);
2640 skb = nlmsg_new(len, GFP_ATOMIC);
2641 if (skb == NULL)
2642 return -ENOMEM;
2644 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2645 err = -EMSGSIZE;
2646 if (nlh == NULL)
2647 goto out_free_skb;
2649 p = nlmsg_data(nlh);
2650 if (c->event == XFRM_MSG_DELSA) {
2651 struct nlattr *attr;
2653 id = nlmsg_data(nlh);
2654 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2655 id->spi = x->id.spi;
2656 id->family = x->props.family;
2657 id->proto = x->id.proto;
2659 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2660 err = -EMSGSIZE;
2661 if (attr == NULL)
2662 goto out_free_skb;
2664 p = nla_data(attr);
2666 err = copy_to_user_state_extra(x, p, skb);
2667 if (err)
2668 goto out_free_skb;
2670 nlmsg_end(skb, nlh);
2672 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2674 out_free_skb:
2675 kfree_skb(skb);
2676 return err;
2679 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2682 switch (c->event) {
2683 case XFRM_MSG_EXPIRE:
2684 return xfrm_exp_state_notify(x, c);
2685 case XFRM_MSG_NEWAE:
2686 return xfrm_aevent_state_notify(x, c);
2687 case XFRM_MSG_DELSA:
2688 case XFRM_MSG_UPDSA:
2689 case XFRM_MSG_NEWSA:
2690 return xfrm_notify_sa(x, c);
2691 case XFRM_MSG_FLUSHSA:
2692 return xfrm_notify_sa_flush(c);
2693 default:
2694 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2695 c->event);
2696 break;
2699 return 0;
2703 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2704 struct xfrm_policy *xp)
2706 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2707 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2708 + nla_total_size(sizeof(struct xfrm_mark))
2709 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2710 + userpolicy_type_attrsize();
2713 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2714 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2716 __u32 seq = xfrm_get_acqseq();
2717 struct xfrm_user_acquire *ua;
2718 struct nlmsghdr *nlh;
2719 int err;
2721 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2722 if (nlh == NULL)
2723 return -EMSGSIZE;
2725 ua = nlmsg_data(nlh);
2726 memcpy(&ua->id, &x->id, sizeof(ua->id));
2727 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2728 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2729 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2730 ua->aalgos = xt->aalgos;
2731 ua->ealgos = xt->ealgos;
2732 ua->calgos = xt->calgos;
2733 ua->seq = x->km.seq = seq;
2735 err = copy_to_user_tmpl(xp, skb);
2736 if (!err)
2737 err = copy_to_user_state_sec_ctx(x, skb);
2738 if (!err)
2739 err = copy_to_user_policy_type(xp->type, skb);
2740 if (!err)
2741 err = xfrm_mark_put(skb, &xp->mark);
2742 if (err) {
2743 nlmsg_cancel(skb, nlh);
2744 return err;
2747 nlmsg_end(skb, nlh);
2748 return 0;
2751 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2752 struct xfrm_policy *xp)
2754 struct net *net = xs_net(x);
2755 struct sk_buff *skb;
2757 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2758 if (skb == NULL)
2759 return -ENOMEM;
2761 if (build_acquire(skb, x, xt, xp) < 0)
2762 BUG();
2764 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2767 /* User gives us xfrm_user_policy_info followed by an array of 0
2768 * or more templates.
2770 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2771 u8 *data, int len, int *dir)
2773 struct net *net = sock_net(sk);
2774 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2775 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2776 struct xfrm_policy *xp;
2777 int nr;
2779 switch (sk->sk_family) {
2780 case AF_INET:
2781 if (opt != IP_XFRM_POLICY) {
2782 *dir = -EOPNOTSUPP;
2783 return NULL;
2785 break;
2786 #if IS_ENABLED(CONFIG_IPV6)
2787 case AF_INET6:
2788 if (opt != IPV6_XFRM_POLICY) {
2789 *dir = -EOPNOTSUPP;
2790 return NULL;
2792 break;
2793 #endif
2794 default:
2795 *dir = -EINVAL;
2796 return NULL;
2799 *dir = -EINVAL;
2801 if (len < sizeof(*p) ||
2802 verify_newpolicy_info(p))
2803 return NULL;
2805 nr = ((len - sizeof(*p)) / sizeof(*ut));
2806 if (validate_tmpl(nr, ut, p->sel.family))
2807 return NULL;
2809 if (p->dir > XFRM_POLICY_OUT)
2810 return NULL;
2812 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2813 if (xp == NULL) {
2814 *dir = -ENOBUFS;
2815 return NULL;
2818 copy_from_user_policy(xp, p);
2819 xp->type = XFRM_POLICY_TYPE_MAIN;
2820 copy_templates(xp, ut, nr);
2822 *dir = p->dir;
2824 return xp;
2827 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2829 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2830 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2831 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2832 + nla_total_size(sizeof(struct xfrm_mark))
2833 + userpolicy_type_attrsize();
2836 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2837 int dir, const struct km_event *c)
2839 struct xfrm_user_polexpire *upe;
2840 int hard = c->data.hard;
2841 struct nlmsghdr *nlh;
2842 int err;
2844 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2845 if (nlh == NULL)
2846 return -EMSGSIZE;
2848 upe = nlmsg_data(nlh);
2849 copy_to_user_policy(xp, &upe->pol, dir);
2850 err = copy_to_user_tmpl(xp, skb);
2851 if (!err)
2852 err = copy_to_user_sec_ctx(xp, skb);
2853 if (!err)
2854 err = copy_to_user_policy_type(xp->type, skb);
2855 if (!err)
2856 err = xfrm_mark_put(skb, &xp->mark);
2857 if (err) {
2858 nlmsg_cancel(skb, nlh);
2859 return err;
2861 upe->hard = !!hard;
2863 nlmsg_end(skb, nlh);
2864 return 0;
2867 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2869 struct net *net = xp_net(xp);
2870 struct sk_buff *skb;
2872 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2873 if (skb == NULL)
2874 return -ENOMEM;
2876 if (build_polexpire(skb, xp, dir, c) < 0)
2877 BUG();
2879 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2882 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2884 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2885 struct net *net = xp_net(xp);
2886 struct xfrm_userpolicy_info *p;
2887 struct xfrm_userpolicy_id *id;
2888 struct nlmsghdr *nlh;
2889 struct sk_buff *skb;
2890 int headlen, err;
2892 headlen = sizeof(*p);
2893 if (c->event == XFRM_MSG_DELPOLICY) {
2894 len += nla_total_size(headlen);
2895 headlen = sizeof(*id);
2897 len += userpolicy_type_attrsize();
2898 len += nla_total_size(sizeof(struct xfrm_mark));
2899 len += NLMSG_ALIGN(headlen);
2901 skb = nlmsg_new(len, GFP_ATOMIC);
2902 if (skb == NULL)
2903 return -ENOMEM;
2905 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2906 err = -EMSGSIZE;
2907 if (nlh == NULL)
2908 goto out_free_skb;
2910 p = nlmsg_data(nlh);
2911 if (c->event == XFRM_MSG_DELPOLICY) {
2912 struct nlattr *attr;
2914 id = nlmsg_data(nlh);
2915 memset(id, 0, sizeof(*id));
2916 id->dir = dir;
2917 if (c->data.byid)
2918 id->index = xp->index;
2919 else
2920 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2922 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2923 err = -EMSGSIZE;
2924 if (attr == NULL)
2925 goto out_free_skb;
2927 p = nla_data(attr);
2930 copy_to_user_policy(xp, p, dir);
2931 err = copy_to_user_tmpl(xp, skb);
2932 if (!err)
2933 err = copy_to_user_policy_type(xp->type, skb);
2934 if (!err)
2935 err = xfrm_mark_put(skb, &xp->mark);
2936 if (err)
2937 goto out_free_skb;
2939 nlmsg_end(skb, nlh);
2941 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2943 out_free_skb:
2944 kfree_skb(skb);
2945 return err;
2948 static int xfrm_notify_policy_flush(const struct km_event *c)
2950 struct net *net = c->net;
2951 struct nlmsghdr *nlh;
2952 struct sk_buff *skb;
2953 int err;
2955 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2956 if (skb == NULL)
2957 return -ENOMEM;
2959 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2960 err = -EMSGSIZE;
2961 if (nlh == NULL)
2962 goto out_free_skb;
2963 err = copy_to_user_policy_type(c->data.type, skb);
2964 if (err)
2965 goto out_free_skb;
2967 nlmsg_end(skb, nlh);
2969 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2971 out_free_skb:
2972 kfree_skb(skb);
2973 return err;
2976 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2979 switch (c->event) {
2980 case XFRM_MSG_NEWPOLICY:
2981 case XFRM_MSG_UPDPOLICY:
2982 case XFRM_MSG_DELPOLICY:
2983 return xfrm_notify_policy(xp, dir, c);
2984 case XFRM_MSG_FLUSHPOLICY:
2985 return xfrm_notify_policy_flush(c);
2986 case XFRM_MSG_POLEXPIRE:
2987 return xfrm_exp_policy_notify(xp, dir, c);
2988 default:
2989 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2990 c->event);
2993 return 0;
2997 static inline size_t xfrm_report_msgsize(void)
2999 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3002 static int build_report(struct sk_buff *skb, u8 proto,
3003 struct xfrm_selector *sel, xfrm_address_t *addr)
3005 struct xfrm_user_report *ur;
3006 struct nlmsghdr *nlh;
3008 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3009 if (nlh == NULL)
3010 return -EMSGSIZE;
3012 ur = nlmsg_data(nlh);
3013 ur->proto = proto;
3014 memcpy(&ur->sel, sel, sizeof(ur->sel));
3016 if (addr) {
3017 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3018 if (err) {
3019 nlmsg_cancel(skb, nlh);
3020 return err;
3023 nlmsg_end(skb, nlh);
3024 return 0;
3027 static int xfrm_send_report(struct net *net, u8 proto,
3028 struct xfrm_selector *sel, xfrm_address_t *addr)
3030 struct sk_buff *skb;
3032 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3033 if (skb == NULL)
3034 return -ENOMEM;
3036 if (build_report(skb, proto, sel, addr) < 0)
3037 BUG();
3039 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3042 static inline size_t xfrm_mapping_msgsize(void)
3044 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3047 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3048 xfrm_address_t *new_saddr, __be16 new_sport)
3050 struct xfrm_user_mapping *um;
3051 struct nlmsghdr *nlh;
3053 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3054 if (nlh == NULL)
3055 return -EMSGSIZE;
3057 um = nlmsg_data(nlh);
3059 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3060 um->id.spi = x->id.spi;
3061 um->id.family = x->props.family;
3062 um->id.proto = x->id.proto;
3063 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3064 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3065 um->new_sport = new_sport;
3066 um->old_sport = x->encap->encap_sport;
3067 um->reqid = x->props.reqid;
3069 nlmsg_end(skb, nlh);
3070 return 0;
3073 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3074 __be16 sport)
3076 struct net *net = xs_net(x);
3077 struct sk_buff *skb;
3079 if (x->id.proto != IPPROTO_ESP)
3080 return -EINVAL;
3082 if (!x->encap)
3083 return -EINVAL;
3085 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3086 if (skb == NULL)
3087 return -ENOMEM;
3089 if (build_mapping(skb, x, ipaddr, sport) < 0)
3090 BUG();
3092 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3095 static bool xfrm_is_alive(const struct km_event *c)
3097 return (bool)xfrm_acquire_is_on(c->net);
3100 static struct xfrm_mgr netlink_mgr = {
3101 .id = "netlink",
3102 .notify = xfrm_send_state_notify,
3103 .acquire = xfrm_send_acquire,
3104 .compile_policy = xfrm_compile_policy,
3105 .notify_policy = xfrm_send_policy_notify,
3106 .report = xfrm_send_report,
3107 .migrate = xfrm_send_migrate,
3108 .new_mapping = xfrm_send_mapping,
3109 .is_alive = xfrm_is_alive,
3112 static int __net_init xfrm_user_net_init(struct net *net)
3114 struct sock *nlsk;
3115 struct netlink_kernel_cfg cfg = {
3116 .groups = XFRMNLGRP_MAX,
3117 .input = xfrm_netlink_rcv,
3120 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3121 if (nlsk == NULL)
3122 return -ENOMEM;
3123 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3124 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3125 return 0;
3128 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3130 struct net *net;
3131 list_for_each_entry(net, net_exit_list, exit_list)
3132 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3133 synchronize_net();
3134 list_for_each_entry(net, net_exit_list, exit_list)
3135 netlink_kernel_release(net->xfrm.nlsk_stash);
3138 static struct pernet_operations xfrm_user_net_ops = {
3139 .init = xfrm_user_net_init,
3140 .exit_batch = xfrm_user_net_exit,
3143 static int __init xfrm_user_init(void)
3145 int rv;
3147 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3149 rv = register_pernet_subsys(&xfrm_user_net_ops);
3150 if (rv < 0)
3151 return rv;
3152 rv = xfrm_register_km(&netlink_mgr);
3153 if (rv < 0)
3154 unregister_pernet_subsys(&xfrm_user_net_ops);
3155 return rv;
3158 static void __exit xfrm_user_exit(void)
3160 xfrm_unregister_km(&netlink_mgr);
3161 unregister_pernet_subsys(&xfrm_user_net_ops);
3164 module_init(xfrm_user_init);
3165 module_exit(xfrm_user_exit);
3166 MODULE_LICENSE("GPL");
3167 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);