Linux 4.1.16
[linux/fpc-iii.git] / net / xfrm / xfrm_user.c
blob2091664295bae1a3a4725287e5d0bf5cfb28357c
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
35 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
37 struct nlattr *rt = attrs[type];
38 struct xfrm_algo *algp;
40 if (!rt)
41 return 0;
43 algp = nla_data(rt);
44 if (nla_len(rt) < xfrm_alg_len(algp))
45 return -EINVAL;
47 switch (type) {
48 case XFRMA_ALG_AUTH:
49 case XFRMA_ALG_CRYPT:
50 case XFRMA_ALG_COMP:
51 break;
53 default:
54 return -EINVAL;
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
61 static int verify_auth_trunc(struct nlattr **attrs)
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
66 if (!rt)
67 return 0;
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
77 static int verify_aead(struct nlattr **attrs)
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
82 if (!rt)
83 return 0;
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
93 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
94 xfrm_address_t **addrp)
96 struct nlattr *rt = attrs[type];
98 if (rt && addrp)
99 *addrp = nla_data(rt);
102 static inline int verify_sec_ctx_len(struct nlattr **attrs)
104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
105 struct xfrm_user_sec_ctx *uctx;
107 if (!rt)
108 return 0;
110 uctx = nla_data(rt);
111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
112 return -EINVAL;
114 return 0;
117 static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
121 struct xfrm_replay_state_esn *rs;
123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
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;
137 if (!rt)
138 return 0;
140 /* As only ESP and AH support ESN feature. */
141 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
142 return -EINVAL;
144 if (p->replay_window != 0)
145 return -EINVAL;
147 return 0;
150 static int verify_newsa_info(struct xfrm_usersa_info *p,
151 struct nlattr **attrs)
153 int err;
155 err = -EINVAL;
156 switch (p->family) {
157 case AF_INET:
158 break;
160 case AF_INET6:
161 #if IS_ENABLED(CONFIG_IPV6)
162 break;
163 #else
164 err = -EAFNOSUPPORT;
165 goto out;
166 #endif
168 default:
169 goto out;
172 err = -EINVAL;
173 switch (p->id.proto) {
174 case IPPROTO_AH:
175 if ((!attrs[XFRMA_ALG_AUTH] &&
176 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
177 attrs[XFRMA_ALG_AEAD] ||
178 attrs[XFRMA_ALG_CRYPT] ||
179 attrs[XFRMA_ALG_COMP] ||
180 attrs[XFRMA_TFCPAD])
181 goto out;
182 break;
184 case IPPROTO_ESP:
185 if (attrs[XFRMA_ALG_COMP])
186 goto out;
187 if (!attrs[XFRMA_ALG_AUTH] &&
188 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
189 !attrs[XFRMA_ALG_CRYPT] &&
190 !attrs[XFRMA_ALG_AEAD])
191 goto out;
192 if ((attrs[XFRMA_ALG_AUTH] ||
193 attrs[XFRMA_ALG_AUTH_TRUNC] ||
194 attrs[XFRMA_ALG_CRYPT]) &&
195 attrs[XFRMA_ALG_AEAD])
196 goto out;
197 if (attrs[XFRMA_TFCPAD] &&
198 p->mode != XFRM_MODE_TUNNEL)
199 goto out;
200 break;
202 case IPPROTO_COMP:
203 if (!attrs[XFRMA_ALG_COMP] ||
204 attrs[XFRMA_ALG_AEAD] ||
205 attrs[XFRMA_ALG_AUTH] ||
206 attrs[XFRMA_ALG_AUTH_TRUNC] ||
207 attrs[XFRMA_ALG_CRYPT] ||
208 attrs[XFRMA_TFCPAD] ||
209 (ntohl(p->id.spi) >= 0x10000))
210 goto out;
211 break;
213 #if IS_ENABLED(CONFIG_IPV6)
214 case IPPROTO_DSTOPTS:
215 case IPPROTO_ROUTING:
216 if (attrs[XFRMA_ALG_COMP] ||
217 attrs[XFRMA_ALG_AUTH] ||
218 attrs[XFRMA_ALG_AUTH_TRUNC] ||
219 attrs[XFRMA_ALG_AEAD] ||
220 attrs[XFRMA_ALG_CRYPT] ||
221 attrs[XFRMA_ENCAP] ||
222 attrs[XFRMA_SEC_CTX] ||
223 attrs[XFRMA_TFCPAD] ||
224 !attrs[XFRMA_COADDR])
225 goto out;
226 break;
227 #endif
229 default:
230 goto out;
233 if ((err = verify_aead(attrs)))
234 goto out;
235 if ((err = verify_auth_trunc(attrs)))
236 goto out;
237 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
238 goto out;
239 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
240 goto out;
241 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
242 goto out;
243 if ((err = verify_sec_ctx_len(attrs)))
244 goto out;
245 if ((err = verify_replay(p, attrs)))
246 goto out;
248 err = -EINVAL;
249 switch (p->mode) {
250 case XFRM_MODE_TRANSPORT:
251 case XFRM_MODE_TUNNEL:
252 case XFRM_MODE_ROUTEOPTIMIZATION:
253 case XFRM_MODE_BEET:
254 break;
256 default:
257 goto out;
260 err = 0;
262 out:
263 return err;
266 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
267 struct xfrm_algo_desc *(*get_byname)(const char *, int),
268 struct nlattr *rta)
270 struct xfrm_algo *p, *ualg;
271 struct xfrm_algo_desc *algo;
273 if (!rta)
274 return 0;
276 ualg = nla_data(rta);
278 algo = get_byname(ualg->alg_name, 1);
279 if (!algo)
280 return -ENOSYS;
281 *props = algo->desc.sadb_alg_id;
283 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
284 if (!p)
285 return -ENOMEM;
287 strcpy(p->alg_name, algo->name);
288 *algpp = p;
289 return 0;
292 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
293 struct nlattr *rta)
295 struct xfrm_algo *ualg;
296 struct xfrm_algo_auth *p;
297 struct xfrm_algo_desc *algo;
299 if (!rta)
300 return 0;
302 ualg = nla_data(rta);
304 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
305 if (!algo)
306 return -ENOSYS;
307 *props = algo->desc.sadb_alg_id;
309 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
310 if (!p)
311 return -ENOMEM;
313 strcpy(p->alg_name, algo->name);
314 p->alg_key_len = ualg->alg_key_len;
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
318 *algpp = p;
319 return 0;
322 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
323 struct nlattr *rta)
325 struct xfrm_algo_auth *p, *ualg;
326 struct xfrm_algo_desc *algo;
328 if (!rta)
329 return 0;
331 ualg = nla_data(rta);
333 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
334 if (!algo)
335 return -ENOSYS;
336 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
337 return -EINVAL;
338 *props = algo->desc.sadb_alg_id;
340 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
341 if (!p)
342 return -ENOMEM;
344 strcpy(p->alg_name, algo->name);
345 if (!p->alg_trunc_len)
346 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
348 *algpp = p;
349 return 0;
352 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
353 struct nlattr *rta)
355 struct xfrm_algo_aead *p, *ualg;
356 struct xfrm_algo_desc *algo;
358 if (!rta)
359 return 0;
361 ualg = nla_data(rta);
363 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
364 if (!algo)
365 return -ENOSYS;
366 *props = algo->desc.sadb_alg_id;
368 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
369 if (!p)
370 return -ENOMEM;
372 strcpy(p->alg_name, algo->name);
373 *algpp = p;
374 return 0;
377 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
378 struct nlattr *rp)
380 struct xfrm_replay_state_esn *up;
381 int ulen;
383 if (!replay_esn || !rp)
384 return 0;
386 up = nla_data(rp);
387 ulen = xfrm_replay_state_esn_len(up);
389 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
390 return -EINVAL;
392 return 0;
395 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
396 struct xfrm_replay_state_esn **preplay_esn,
397 struct nlattr *rta)
399 struct xfrm_replay_state_esn *p, *pp, *up;
400 int klen, ulen;
402 if (!rta)
403 return 0;
405 up = nla_data(rta);
406 klen = xfrm_replay_state_esn_len(up);
407 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
409 p = kzalloc(klen, GFP_KERNEL);
410 if (!p)
411 return -ENOMEM;
413 pp = kzalloc(klen, GFP_KERNEL);
414 if (!pp) {
415 kfree(p);
416 return -ENOMEM;
419 memcpy(p, up, ulen);
420 memcpy(pp, up, ulen);
422 *replay_esn = p;
423 *preplay_esn = pp;
425 return 0;
428 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
430 int len = 0;
432 if (xfrm_ctx) {
433 len += sizeof(struct xfrm_user_sec_ctx);
434 len += xfrm_ctx->ctx_len;
436 return len;
439 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
441 memcpy(&x->id, &p->id, sizeof(x->id));
442 memcpy(&x->sel, &p->sel, sizeof(x->sel));
443 memcpy(&x->lft, &p->lft, sizeof(x->lft));
444 x->props.mode = p->mode;
445 x->props.replay_window = min_t(unsigned int, p->replay_window,
446 sizeof(x->replay.bitmap) * 8);
447 x->props.reqid = p->reqid;
448 x->props.family = p->family;
449 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
450 x->props.flags = p->flags;
452 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
453 x->sel.family = p->family;
457 * someday when pfkey also has support, we could have the code
458 * somehow made shareable and move it to xfrm_state.c - JHS
461 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
462 int update_esn)
464 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
465 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
466 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
467 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
468 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
470 if (re) {
471 struct xfrm_replay_state_esn *replay_esn;
472 replay_esn = nla_data(re);
473 memcpy(x->replay_esn, replay_esn,
474 xfrm_replay_state_esn_len(replay_esn));
475 memcpy(x->preplay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
479 if (rp) {
480 struct xfrm_replay_state *replay;
481 replay = nla_data(rp);
482 memcpy(&x->replay, replay, sizeof(*replay));
483 memcpy(&x->preplay, replay, sizeof(*replay));
486 if (lt) {
487 struct xfrm_lifetime_cur *ltime;
488 ltime = nla_data(lt);
489 x->curlft.bytes = ltime->bytes;
490 x->curlft.packets = ltime->packets;
491 x->curlft.add_time = ltime->add_time;
492 x->curlft.use_time = ltime->use_time;
495 if (et)
496 x->replay_maxage = nla_get_u32(et);
498 if (rt)
499 x->replay_maxdiff = nla_get_u32(rt);
502 static struct xfrm_state *xfrm_state_construct(struct net *net,
503 struct xfrm_usersa_info *p,
504 struct nlattr **attrs,
505 int *errp)
507 struct xfrm_state *x = xfrm_state_alloc(net);
508 int err = -ENOMEM;
510 if (!x)
511 goto error_no_put;
513 copy_from_user_state(x, p);
515 if (attrs[XFRMA_SA_EXTRA_FLAGS])
516 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
518 if ((err = attach_aead(&x->aead, &x->props.ealgo,
519 attrs[XFRMA_ALG_AEAD])))
520 goto error;
521 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
522 attrs[XFRMA_ALG_AUTH_TRUNC])))
523 goto error;
524 if (!x->props.aalgo) {
525 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH])))
527 goto error;
529 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
530 xfrm_ealg_get_byname,
531 attrs[XFRMA_ALG_CRYPT])))
532 goto error;
533 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
534 xfrm_calg_get_byname,
535 attrs[XFRMA_ALG_COMP])))
536 goto error;
538 if (attrs[XFRMA_ENCAP]) {
539 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
540 sizeof(*x->encap), GFP_KERNEL);
541 if (x->encap == NULL)
542 goto error;
545 if (attrs[XFRMA_TFCPAD])
546 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
548 if (attrs[XFRMA_COADDR]) {
549 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
550 sizeof(*x->coaddr), GFP_KERNEL);
551 if (x->coaddr == NULL)
552 goto error;
555 xfrm_mark_get(attrs, &x->mark);
557 err = __xfrm_init_state(x, false);
558 if (err)
559 goto error;
561 if (attrs[XFRMA_SEC_CTX] &&
562 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
563 goto error;
565 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
566 attrs[XFRMA_REPLAY_ESN_VAL])))
567 goto error;
569 x->km.seq = p->seq;
570 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
571 /* sysctl_xfrm_aevent_etime is in 100ms units */
572 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
574 if ((err = xfrm_init_replay(x)))
575 goto error;
577 /* override default values from above */
578 xfrm_update_ae_params(x, attrs, 0);
580 return x;
582 error:
583 x->km.state = XFRM_STATE_DEAD;
584 xfrm_state_put(x);
585 error_no_put:
586 *errp = err;
587 return NULL;
590 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
591 struct nlattr **attrs)
593 struct net *net = sock_net(skb->sk);
594 struct xfrm_usersa_info *p = nlmsg_data(nlh);
595 struct xfrm_state *x;
596 int err;
597 struct km_event c;
599 err = verify_newsa_info(p, attrs);
600 if (err)
601 return err;
603 x = xfrm_state_construct(net, p, attrs, &err);
604 if (!x)
605 return err;
607 xfrm_state_hold(x);
608 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
609 err = xfrm_state_add(x);
610 else
611 err = xfrm_state_update(x);
613 xfrm_audit_state_add(x, err ? 0 : 1, true);
615 if (err < 0) {
616 x->km.state = XFRM_STATE_DEAD;
617 __xfrm_state_put(x);
618 goto out;
621 c.seq = nlh->nlmsg_seq;
622 c.portid = nlh->nlmsg_pid;
623 c.event = nlh->nlmsg_type;
625 km_state_notify(x, &c);
626 out:
627 xfrm_state_put(x);
628 return err;
631 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
632 struct xfrm_usersa_id *p,
633 struct nlattr **attrs,
634 int *errp)
636 struct xfrm_state *x = NULL;
637 struct xfrm_mark m;
638 int err;
639 u32 mark = xfrm_mark_get(attrs, &m);
641 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
642 err = -ESRCH;
643 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
644 } else {
645 xfrm_address_t *saddr = NULL;
647 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
648 if (!saddr) {
649 err = -EINVAL;
650 goto out;
653 err = -ESRCH;
654 x = xfrm_state_lookup_byaddr(net, mark,
655 &p->daddr, saddr,
656 p->proto, p->family);
659 out:
660 if (!x && errp)
661 *errp = err;
662 return x;
665 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
666 struct nlattr **attrs)
668 struct net *net = sock_net(skb->sk);
669 struct xfrm_state *x;
670 int err = -ESRCH;
671 struct km_event c;
672 struct xfrm_usersa_id *p = nlmsg_data(nlh);
674 x = xfrm_user_state_lookup(net, p, attrs, &err);
675 if (x == NULL)
676 return err;
678 if ((err = security_xfrm_state_delete(x)) != 0)
679 goto out;
681 if (xfrm_state_kern(x)) {
682 err = -EPERM;
683 goto out;
686 err = xfrm_state_delete(x);
688 if (err < 0)
689 goto out;
691 c.seq = nlh->nlmsg_seq;
692 c.portid = nlh->nlmsg_pid;
693 c.event = nlh->nlmsg_type;
694 km_state_notify(x, &c);
696 out:
697 xfrm_audit_state_delete(x, err ? 0 : 1, true);
698 xfrm_state_put(x);
699 return err;
702 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
704 memset(p, 0, sizeof(*p));
705 memcpy(&p->id, &x->id, sizeof(p->id));
706 memcpy(&p->sel, &x->sel, sizeof(p->sel));
707 memcpy(&p->lft, &x->lft, sizeof(p->lft));
708 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
709 memcpy(&p->stats, &x->stats, sizeof(p->stats));
710 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
711 p->mode = x->props.mode;
712 p->replay_window = x->props.replay_window;
713 p->reqid = x->props.reqid;
714 p->family = x->props.family;
715 p->flags = x->props.flags;
716 p->seq = x->km.seq;
719 struct xfrm_dump_info {
720 struct sk_buff *in_skb;
721 struct sk_buff *out_skb;
722 u32 nlmsg_seq;
723 u16 nlmsg_flags;
726 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
728 struct xfrm_user_sec_ctx *uctx;
729 struct nlattr *attr;
730 int ctx_size = sizeof(*uctx) + s->ctx_len;
732 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
733 if (attr == NULL)
734 return -EMSGSIZE;
736 uctx = nla_data(attr);
737 uctx->exttype = XFRMA_SEC_CTX;
738 uctx->len = ctx_size;
739 uctx->ctx_doi = s->ctx_doi;
740 uctx->ctx_alg = s->ctx_alg;
741 uctx->ctx_len = s->ctx_len;
742 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
744 return 0;
747 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
749 struct xfrm_algo *algo;
750 struct nlattr *nla;
752 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
753 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
754 if (!nla)
755 return -EMSGSIZE;
757 algo = nla_data(nla);
758 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
759 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
760 algo->alg_key_len = auth->alg_key_len;
762 return 0;
765 /* Don't change this without updating xfrm_sa_len! */
766 static int copy_to_user_state_extra(struct xfrm_state *x,
767 struct xfrm_usersa_info *p,
768 struct sk_buff *skb)
770 int ret = 0;
772 copy_to_user_state(x, p);
774 if (x->props.extra_flags) {
775 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
776 x->props.extra_flags);
777 if (ret)
778 goto out;
781 if (x->coaddr) {
782 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
783 if (ret)
784 goto out;
786 if (x->lastused) {
787 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
788 if (ret)
789 goto out;
791 if (x->aead) {
792 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
793 if (ret)
794 goto out;
796 if (x->aalg) {
797 ret = copy_to_user_auth(x->aalg, skb);
798 if (!ret)
799 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
800 xfrm_alg_auth_len(x->aalg), x->aalg);
801 if (ret)
802 goto out;
804 if (x->ealg) {
805 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
806 if (ret)
807 goto out;
809 if (x->calg) {
810 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
811 if (ret)
812 goto out;
814 if (x->encap) {
815 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
816 if (ret)
817 goto out;
819 if (x->tfcpad) {
820 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
821 if (ret)
822 goto out;
824 ret = xfrm_mark_put(skb, &x->mark);
825 if (ret)
826 goto out;
827 if (x->replay_esn)
828 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
829 xfrm_replay_state_esn_len(x->replay_esn),
830 x->replay_esn);
831 else
832 ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
833 &x->replay);
834 if (ret)
835 goto out;
836 if (x->security)
837 ret = copy_sec_ctx(x->security, skb);
838 out:
839 return ret;
842 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
844 struct xfrm_dump_info *sp = ptr;
845 struct sk_buff *in_skb = sp->in_skb;
846 struct sk_buff *skb = sp->out_skb;
847 struct xfrm_usersa_info *p;
848 struct nlmsghdr *nlh;
849 int err;
851 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
852 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
853 if (nlh == NULL)
854 return -EMSGSIZE;
856 p = nlmsg_data(nlh);
858 err = copy_to_user_state_extra(x, p, skb);
859 if (err) {
860 nlmsg_cancel(skb, nlh);
861 return err;
863 nlmsg_end(skb, nlh);
864 return 0;
867 static int xfrm_dump_sa_done(struct netlink_callback *cb)
869 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
870 struct sock *sk = cb->skb->sk;
871 struct net *net = sock_net(sk);
873 xfrm_state_walk_done(walk, net);
874 return 0;
877 static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
878 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
880 struct net *net = sock_net(skb->sk);
881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
882 struct xfrm_dump_info info;
884 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
885 sizeof(cb->args) - sizeof(cb->args[0]));
887 info.in_skb = cb->skb;
888 info.out_skb = skb;
889 info.nlmsg_seq = cb->nlh->nlmsg_seq;
890 info.nlmsg_flags = NLM_F_MULTI;
892 if (!cb->args[0]) {
893 struct nlattr *attrs[XFRMA_MAX+1];
894 struct xfrm_address_filter *filter = NULL;
895 u8 proto = 0;
896 int err;
898 cb->args[0] = 1;
900 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
901 xfrma_policy);
902 if (err < 0)
903 return err;
905 if (attrs[XFRMA_ADDRESS_FILTER]) {
906 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
907 if (filter == NULL)
908 return -ENOMEM;
910 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
911 sizeof(*filter));
914 if (attrs[XFRMA_PROTO])
915 proto = nla_get_u8(attrs[XFRMA_PROTO]);
917 xfrm_state_walk_init(walk, proto, filter);
920 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
922 return skb->len;
925 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
926 struct xfrm_state *x, u32 seq)
928 struct xfrm_dump_info info;
929 struct sk_buff *skb;
930 int err;
932 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
933 if (!skb)
934 return ERR_PTR(-ENOMEM);
936 info.in_skb = in_skb;
937 info.out_skb = skb;
938 info.nlmsg_seq = seq;
939 info.nlmsg_flags = 0;
941 err = dump_one_state(x, 0, &info);
942 if (err) {
943 kfree_skb(skb);
944 return ERR_PTR(err);
947 return skb;
950 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
951 * Must be called with RCU read lock.
953 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
954 u32 pid, unsigned int group)
956 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
958 if (nlsk)
959 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
960 else
961 return -1;
964 static inline size_t xfrm_spdinfo_msgsize(void)
966 return NLMSG_ALIGN(4)
967 + nla_total_size(sizeof(struct xfrmu_spdinfo))
968 + nla_total_size(sizeof(struct xfrmu_spdhinfo))
969 + nla_total_size(sizeof(struct xfrmu_spdhthresh))
970 + nla_total_size(sizeof(struct xfrmu_spdhthresh));
973 static int build_spdinfo(struct sk_buff *skb, struct net *net,
974 u32 portid, u32 seq, u32 flags)
976 struct xfrmk_spdinfo si;
977 struct xfrmu_spdinfo spc;
978 struct xfrmu_spdhinfo sph;
979 struct xfrmu_spdhthresh spt4, spt6;
980 struct nlmsghdr *nlh;
981 int err;
982 u32 *f;
983 unsigned lseq;
985 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
986 if (nlh == NULL) /* shouldn't really happen ... */
987 return -EMSGSIZE;
989 f = nlmsg_data(nlh);
990 *f = flags;
991 xfrm_spd_getinfo(net, &si);
992 spc.incnt = si.incnt;
993 spc.outcnt = si.outcnt;
994 spc.fwdcnt = si.fwdcnt;
995 spc.inscnt = si.inscnt;
996 spc.outscnt = si.outscnt;
997 spc.fwdscnt = si.fwdscnt;
998 sph.spdhcnt = si.spdhcnt;
999 sph.spdhmcnt = si.spdhmcnt;
1001 do {
1002 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1004 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1005 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1006 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1007 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1008 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1010 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1011 if (!err)
1012 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1013 if (!err)
1014 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1015 if (!err)
1016 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1017 if (err) {
1018 nlmsg_cancel(skb, nlh);
1019 return err;
1022 nlmsg_end(skb, nlh);
1023 return 0;
1026 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1027 struct nlattr **attrs)
1029 struct net *net = sock_net(skb->sk);
1030 struct xfrmu_spdhthresh *thresh4 = NULL;
1031 struct xfrmu_spdhthresh *thresh6 = NULL;
1033 /* selector prefixlen thresholds to hash policies */
1034 if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1035 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1037 if (nla_len(rta) < sizeof(*thresh4))
1038 return -EINVAL;
1039 thresh4 = nla_data(rta);
1040 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1041 return -EINVAL;
1043 if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1044 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1046 if (nla_len(rta) < sizeof(*thresh6))
1047 return -EINVAL;
1048 thresh6 = nla_data(rta);
1049 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1050 return -EINVAL;
1053 if (thresh4 || thresh6) {
1054 write_seqlock(&net->xfrm.policy_hthresh.lock);
1055 if (thresh4) {
1056 net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1057 net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1059 if (thresh6) {
1060 net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1061 net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1063 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1065 xfrm_policy_hash_rebuild(net);
1068 return 0;
1071 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1072 struct nlattr **attrs)
1074 struct net *net = sock_net(skb->sk);
1075 struct sk_buff *r_skb;
1076 u32 *flags = nlmsg_data(nlh);
1077 u32 sportid = NETLINK_CB(skb).portid;
1078 u32 seq = nlh->nlmsg_seq;
1080 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1081 if (r_skb == NULL)
1082 return -ENOMEM;
1084 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1085 BUG();
1087 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1090 static inline size_t xfrm_sadinfo_msgsize(void)
1092 return NLMSG_ALIGN(4)
1093 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1094 + nla_total_size(4); /* XFRMA_SAD_CNT */
1097 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1098 u32 portid, u32 seq, u32 flags)
1100 struct xfrmk_sadinfo si;
1101 struct xfrmu_sadhinfo sh;
1102 struct nlmsghdr *nlh;
1103 int err;
1104 u32 *f;
1106 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1107 if (nlh == NULL) /* shouldn't really happen ... */
1108 return -EMSGSIZE;
1110 f = nlmsg_data(nlh);
1111 *f = flags;
1112 xfrm_sad_getinfo(net, &si);
1114 sh.sadhmcnt = si.sadhmcnt;
1115 sh.sadhcnt = si.sadhcnt;
1117 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1118 if (!err)
1119 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1120 if (err) {
1121 nlmsg_cancel(skb, nlh);
1122 return err;
1125 nlmsg_end(skb, nlh);
1126 return 0;
1129 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1130 struct nlattr **attrs)
1132 struct net *net = sock_net(skb->sk);
1133 struct sk_buff *r_skb;
1134 u32 *flags = nlmsg_data(nlh);
1135 u32 sportid = NETLINK_CB(skb).portid;
1136 u32 seq = nlh->nlmsg_seq;
1138 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1139 if (r_skb == NULL)
1140 return -ENOMEM;
1142 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1143 BUG();
1145 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1148 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1149 struct nlattr **attrs)
1151 struct net *net = sock_net(skb->sk);
1152 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1153 struct xfrm_state *x;
1154 struct sk_buff *resp_skb;
1155 int err = -ESRCH;
1157 x = xfrm_user_state_lookup(net, p, attrs, &err);
1158 if (x == NULL)
1159 goto out_noput;
1161 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1162 if (IS_ERR(resp_skb)) {
1163 err = PTR_ERR(resp_skb);
1164 } else {
1165 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1167 xfrm_state_put(x);
1168 out_noput:
1169 return err;
1172 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1173 struct nlattr **attrs)
1175 struct net *net = sock_net(skb->sk);
1176 struct xfrm_state *x;
1177 struct xfrm_userspi_info *p;
1178 struct sk_buff *resp_skb;
1179 xfrm_address_t *daddr;
1180 int family;
1181 int err;
1182 u32 mark;
1183 struct xfrm_mark m;
1185 p = nlmsg_data(nlh);
1186 err = verify_spi_info(p->info.id.proto, p->min, p->max);
1187 if (err)
1188 goto out_noput;
1190 family = p->info.family;
1191 daddr = &p->info.id.daddr;
1193 x = NULL;
1195 mark = xfrm_mark_get(attrs, &m);
1196 if (p->info.seq) {
1197 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1198 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1199 xfrm_state_put(x);
1200 x = NULL;
1204 if (!x)
1205 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1206 p->info.id.proto, daddr,
1207 &p->info.saddr, 1,
1208 family);
1209 err = -ENOENT;
1210 if (x == NULL)
1211 goto out_noput;
1213 err = xfrm_alloc_spi(x, p->min, p->max);
1214 if (err)
1215 goto out;
1217 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1218 if (IS_ERR(resp_skb)) {
1219 err = PTR_ERR(resp_skb);
1220 goto out;
1223 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1225 out:
1226 xfrm_state_put(x);
1227 out_noput:
1228 return err;
1231 static int verify_policy_dir(u8 dir)
1233 switch (dir) {
1234 case XFRM_POLICY_IN:
1235 case XFRM_POLICY_OUT:
1236 case XFRM_POLICY_FWD:
1237 break;
1239 default:
1240 return -EINVAL;
1243 return 0;
1246 static int verify_policy_type(u8 type)
1248 switch (type) {
1249 case XFRM_POLICY_TYPE_MAIN:
1250 #ifdef CONFIG_XFRM_SUB_POLICY
1251 case XFRM_POLICY_TYPE_SUB:
1252 #endif
1253 break;
1255 default:
1256 return -EINVAL;
1259 return 0;
1262 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1264 int ret;
1266 switch (p->share) {
1267 case XFRM_SHARE_ANY:
1268 case XFRM_SHARE_SESSION:
1269 case XFRM_SHARE_USER:
1270 case XFRM_SHARE_UNIQUE:
1271 break;
1273 default:
1274 return -EINVAL;
1277 switch (p->action) {
1278 case XFRM_POLICY_ALLOW:
1279 case XFRM_POLICY_BLOCK:
1280 break;
1282 default:
1283 return -EINVAL;
1286 switch (p->sel.family) {
1287 case AF_INET:
1288 break;
1290 case AF_INET6:
1291 #if IS_ENABLED(CONFIG_IPV6)
1292 break;
1293 #else
1294 return -EAFNOSUPPORT;
1295 #endif
1297 default:
1298 return -EINVAL;
1301 ret = verify_policy_dir(p->dir);
1302 if (ret)
1303 return ret;
1304 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1305 return -EINVAL;
1307 return 0;
1310 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1312 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1313 struct xfrm_user_sec_ctx *uctx;
1315 if (!rt)
1316 return 0;
1318 uctx = nla_data(rt);
1319 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1322 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1323 int nr)
1325 int i;
1327 xp->xfrm_nr = nr;
1328 for (i = 0; i < nr; i++, ut++) {
1329 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1331 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1332 memcpy(&t->saddr, &ut->saddr,
1333 sizeof(xfrm_address_t));
1334 t->reqid = ut->reqid;
1335 t->mode = ut->mode;
1336 t->share = ut->share;
1337 t->optional = ut->optional;
1338 t->aalgos = ut->aalgos;
1339 t->ealgos = ut->ealgos;
1340 t->calgos = ut->calgos;
1341 /* If all masks are ~0, then we allow all algorithms. */
1342 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1343 t->encap_family = ut->family;
1347 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1349 int i;
1351 if (nr > XFRM_MAX_DEPTH)
1352 return -EINVAL;
1354 for (i = 0; i < nr; i++) {
1355 /* We never validated the ut->family value, so many
1356 * applications simply leave it at zero. The check was
1357 * never made and ut->family was ignored because all
1358 * templates could be assumed to have the same family as
1359 * the policy itself. Now that we will have ipv4-in-ipv6
1360 * and ipv6-in-ipv4 tunnels, this is no longer true.
1362 if (!ut[i].family)
1363 ut[i].family = family;
1365 switch (ut[i].family) {
1366 case AF_INET:
1367 break;
1368 #if IS_ENABLED(CONFIG_IPV6)
1369 case AF_INET6:
1370 break;
1371 #endif
1372 default:
1373 return -EINVAL;
1377 return 0;
1380 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1382 struct nlattr *rt = attrs[XFRMA_TMPL];
1384 if (!rt) {
1385 pol->xfrm_nr = 0;
1386 } else {
1387 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1388 int nr = nla_len(rt) / sizeof(*utmpl);
1389 int err;
1391 err = validate_tmpl(nr, utmpl, pol->family);
1392 if (err)
1393 return err;
1395 copy_templates(pol, utmpl, nr);
1397 return 0;
1400 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1402 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1403 struct xfrm_userpolicy_type *upt;
1404 u8 type = XFRM_POLICY_TYPE_MAIN;
1405 int err;
1407 if (rt) {
1408 upt = nla_data(rt);
1409 type = upt->type;
1412 err = verify_policy_type(type);
1413 if (err)
1414 return err;
1416 *tp = type;
1417 return 0;
1420 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1422 xp->priority = p->priority;
1423 xp->index = p->index;
1424 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1425 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1426 xp->action = p->action;
1427 xp->flags = p->flags;
1428 xp->family = p->sel.family;
1429 /* XXX xp->share = p->share; */
1432 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1434 memset(p, 0, sizeof(*p));
1435 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1436 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1437 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1438 p->priority = xp->priority;
1439 p->index = xp->index;
1440 p->sel.family = xp->family;
1441 p->dir = dir;
1442 p->action = xp->action;
1443 p->flags = xp->flags;
1444 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1447 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1449 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1450 int err;
1452 if (!xp) {
1453 *errp = -ENOMEM;
1454 return NULL;
1457 copy_from_user_policy(xp, p);
1459 err = copy_from_user_policy_type(&xp->type, attrs);
1460 if (err)
1461 goto error;
1463 if (!(err = copy_from_user_tmpl(xp, attrs)))
1464 err = copy_from_user_sec_ctx(xp, attrs);
1465 if (err)
1466 goto error;
1468 xfrm_mark_get(attrs, &xp->mark);
1470 return xp;
1471 error:
1472 *errp = err;
1473 xp->walk.dead = 1;
1474 xfrm_policy_destroy(xp);
1475 return NULL;
1478 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1479 struct nlattr **attrs)
1481 struct net *net = sock_net(skb->sk);
1482 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1483 struct xfrm_policy *xp;
1484 struct km_event c;
1485 int err;
1486 int excl;
1488 err = verify_newpolicy_info(p);
1489 if (err)
1490 return err;
1491 err = verify_sec_ctx_len(attrs);
1492 if (err)
1493 return err;
1495 xp = xfrm_policy_construct(net, p, attrs, &err);
1496 if (!xp)
1497 return err;
1499 /* shouldn't excl be based on nlh flags??
1500 * Aha! this is anti-netlink really i.e more pfkey derived
1501 * in netlink excl is a flag and you wouldnt need
1502 * a type XFRM_MSG_UPDPOLICY - JHS */
1503 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1504 err = xfrm_policy_insert(p->dir, xp, excl);
1505 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1507 if (err) {
1508 security_xfrm_policy_free(xp->security);
1509 kfree(xp);
1510 return err;
1513 c.event = nlh->nlmsg_type;
1514 c.seq = nlh->nlmsg_seq;
1515 c.portid = nlh->nlmsg_pid;
1516 km_policy_notify(xp, p->dir, &c);
1518 xfrm_pol_put(xp);
1520 return 0;
1523 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1525 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1526 int i;
1528 if (xp->xfrm_nr == 0)
1529 return 0;
1531 for (i = 0; i < xp->xfrm_nr; i++) {
1532 struct xfrm_user_tmpl *up = &vec[i];
1533 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1535 memset(up, 0, sizeof(*up));
1536 memcpy(&up->id, &kp->id, sizeof(up->id));
1537 up->family = kp->encap_family;
1538 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1539 up->reqid = kp->reqid;
1540 up->mode = kp->mode;
1541 up->share = kp->share;
1542 up->optional = kp->optional;
1543 up->aalgos = kp->aalgos;
1544 up->ealgos = kp->ealgos;
1545 up->calgos = kp->calgos;
1548 return nla_put(skb, XFRMA_TMPL,
1549 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1552 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1554 if (x->security) {
1555 return copy_sec_ctx(x->security, skb);
1557 return 0;
1560 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1562 if (xp->security)
1563 return copy_sec_ctx(xp->security, skb);
1564 return 0;
1566 static inline size_t userpolicy_type_attrsize(void)
1568 #ifdef CONFIG_XFRM_SUB_POLICY
1569 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1570 #else
1571 return 0;
1572 #endif
1575 #ifdef CONFIG_XFRM_SUB_POLICY
1576 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1578 struct xfrm_userpolicy_type upt = {
1579 .type = type,
1582 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1585 #else
1586 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1588 return 0;
1590 #endif
1592 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1594 struct xfrm_dump_info *sp = ptr;
1595 struct xfrm_userpolicy_info *p;
1596 struct sk_buff *in_skb = sp->in_skb;
1597 struct sk_buff *skb = sp->out_skb;
1598 struct nlmsghdr *nlh;
1599 int err;
1601 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1602 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1603 if (nlh == NULL)
1604 return -EMSGSIZE;
1606 p = nlmsg_data(nlh);
1607 copy_to_user_policy(xp, p, dir);
1608 err = copy_to_user_tmpl(xp, skb);
1609 if (!err)
1610 err = copy_to_user_sec_ctx(xp, skb);
1611 if (!err)
1612 err = copy_to_user_policy_type(xp->type, skb);
1613 if (!err)
1614 err = xfrm_mark_put(skb, &xp->mark);
1615 if (err) {
1616 nlmsg_cancel(skb, nlh);
1617 return err;
1619 nlmsg_end(skb, nlh);
1620 return 0;
1623 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1625 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1626 struct net *net = sock_net(cb->skb->sk);
1628 xfrm_policy_walk_done(walk, net);
1629 return 0;
1632 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1634 struct net *net = sock_net(skb->sk);
1635 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1636 struct xfrm_dump_info info;
1638 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1639 sizeof(cb->args) - sizeof(cb->args[0]));
1641 info.in_skb = cb->skb;
1642 info.out_skb = skb;
1643 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1644 info.nlmsg_flags = NLM_F_MULTI;
1646 if (!cb->args[0]) {
1647 cb->args[0] = 1;
1648 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1651 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1653 return skb->len;
1656 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1657 struct xfrm_policy *xp,
1658 int dir, u32 seq)
1660 struct xfrm_dump_info info;
1661 struct sk_buff *skb;
1662 int err;
1664 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1665 if (!skb)
1666 return ERR_PTR(-ENOMEM);
1668 info.in_skb = in_skb;
1669 info.out_skb = skb;
1670 info.nlmsg_seq = seq;
1671 info.nlmsg_flags = 0;
1673 err = dump_one_policy(xp, dir, 0, &info);
1674 if (err) {
1675 kfree_skb(skb);
1676 return ERR_PTR(err);
1679 return skb;
1682 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1683 struct nlattr **attrs)
1685 struct net *net = sock_net(skb->sk);
1686 struct xfrm_policy *xp;
1687 struct xfrm_userpolicy_id *p;
1688 u8 type = XFRM_POLICY_TYPE_MAIN;
1689 int err;
1690 struct km_event c;
1691 int delete;
1692 struct xfrm_mark m;
1693 u32 mark = xfrm_mark_get(attrs, &m);
1695 p = nlmsg_data(nlh);
1696 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1698 err = copy_from_user_policy_type(&type, attrs);
1699 if (err)
1700 return err;
1702 err = verify_policy_dir(p->dir);
1703 if (err)
1704 return err;
1706 if (p->index)
1707 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1708 else {
1709 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1710 struct xfrm_sec_ctx *ctx;
1712 err = verify_sec_ctx_len(attrs);
1713 if (err)
1714 return err;
1716 ctx = NULL;
1717 if (rt) {
1718 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1720 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1721 if (err)
1722 return err;
1724 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1725 ctx, delete, &err);
1726 security_xfrm_policy_free(ctx);
1728 if (xp == NULL)
1729 return -ENOENT;
1731 if (!delete) {
1732 struct sk_buff *resp_skb;
1734 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1735 if (IS_ERR(resp_skb)) {
1736 err = PTR_ERR(resp_skb);
1737 } else {
1738 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1739 NETLINK_CB(skb).portid);
1741 } else {
1742 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1744 if (err != 0)
1745 goto out;
1747 c.data.byid = p->index;
1748 c.event = nlh->nlmsg_type;
1749 c.seq = nlh->nlmsg_seq;
1750 c.portid = nlh->nlmsg_pid;
1751 km_policy_notify(xp, p->dir, &c);
1754 out:
1755 xfrm_pol_put(xp);
1756 if (delete && err == 0)
1757 xfrm_garbage_collect(net);
1758 return err;
1761 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1762 struct nlattr **attrs)
1764 struct net *net = sock_net(skb->sk);
1765 struct km_event c;
1766 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1767 int err;
1769 err = xfrm_state_flush(net, p->proto, true);
1770 if (err) {
1771 if (err == -ESRCH) /* empty table */
1772 return 0;
1773 return err;
1775 c.data.proto = p->proto;
1776 c.event = nlh->nlmsg_type;
1777 c.seq = nlh->nlmsg_seq;
1778 c.portid = nlh->nlmsg_pid;
1779 c.net = net;
1780 km_state_notify(NULL, &c);
1782 return 0;
1785 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1787 size_t replay_size = x->replay_esn ?
1788 xfrm_replay_state_esn_len(x->replay_esn) :
1789 sizeof(struct xfrm_replay_state);
1791 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1792 + nla_total_size(replay_size)
1793 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1794 + nla_total_size(sizeof(struct xfrm_mark))
1795 + nla_total_size(4) /* XFRM_AE_RTHR */
1796 + nla_total_size(4); /* XFRM_AE_ETHR */
1799 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1801 struct xfrm_aevent_id *id;
1802 struct nlmsghdr *nlh;
1803 int err;
1805 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1806 if (nlh == NULL)
1807 return -EMSGSIZE;
1809 id = nlmsg_data(nlh);
1810 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1811 id->sa_id.spi = x->id.spi;
1812 id->sa_id.family = x->props.family;
1813 id->sa_id.proto = x->id.proto;
1814 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1815 id->reqid = x->props.reqid;
1816 id->flags = c->data.aevent;
1818 if (x->replay_esn) {
1819 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1820 xfrm_replay_state_esn_len(x->replay_esn),
1821 x->replay_esn);
1822 } else {
1823 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1824 &x->replay);
1826 if (err)
1827 goto out_cancel;
1828 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1829 if (err)
1830 goto out_cancel;
1832 if (id->flags & XFRM_AE_RTHR) {
1833 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1834 if (err)
1835 goto out_cancel;
1837 if (id->flags & XFRM_AE_ETHR) {
1838 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1839 x->replay_maxage * 10 / HZ);
1840 if (err)
1841 goto out_cancel;
1843 err = xfrm_mark_put(skb, &x->mark);
1844 if (err)
1845 goto out_cancel;
1847 nlmsg_end(skb, nlh);
1848 return 0;
1850 out_cancel:
1851 nlmsg_cancel(skb, nlh);
1852 return err;
1855 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1856 struct nlattr **attrs)
1858 struct net *net = sock_net(skb->sk);
1859 struct xfrm_state *x;
1860 struct sk_buff *r_skb;
1861 int err;
1862 struct km_event c;
1863 u32 mark;
1864 struct xfrm_mark m;
1865 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1866 struct xfrm_usersa_id *id = &p->sa_id;
1868 mark = xfrm_mark_get(attrs, &m);
1870 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1871 if (x == NULL)
1872 return -ESRCH;
1874 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1875 if (r_skb == NULL) {
1876 xfrm_state_put(x);
1877 return -ENOMEM;
1881 * XXX: is this lock really needed - none of the other
1882 * gets lock (the concern is things getting updated
1883 * while we are still reading) - jhs
1885 spin_lock_bh(&x->lock);
1886 c.data.aevent = p->flags;
1887 c.seq = nlh->nlmsg_seq;
1888 c.portid = nlh->nlmsg_pid;
1890 if (build_aevent(r_skb, x, &c) < 0)
1891 BUG();
1892 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
1893 spin_unlock_bh(&x->lock);
1894 xfrm_state_put(x);
1895 return err;
1898 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1899 struct nlattr **attrs)
1901 struct net *net = sock_net(skb->sk);
1902 struct xfrm_state *x;
1903 struct km_event c;
1904 int err = -EINVAL;
1905 u32 mark = 0;
1906 struct xfrm_mark m;
1907 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1908 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1909 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1910 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1912 if (!lt && !rp && !re)
1913 return err;
1915 /* pedantic mode - thou shalt sayeth replaceth */
1916 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1917 return err;
1919 mark = xfrm_mark_get(attrs, &m);
1921 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1922 if (x == NULL)
1923 return -ESRCH;
1925 if (x->km.state != XFRM_STATE_VALID)
1926 goto out;
1928 err = xfrm_replay_verify_len(x->replay_esn, re);
1929 if (err)
1930 goto out;
1932 spin_lock_bh(&x->lock);
1933 xfrm_update_ae_params(x, attrs, 1);
1934 spin_unlock_bh(&x->lock);
1936 c.event = nlh->nlmsg_type;
1937 c.seq = nlh->nlmsg_seq;
1938 c.portid = nlh->nlmsg_pid;
1939 c.data.aevent = XFRM_AE_CU;
1940 km_state_notify(x, &c);
1941 err = 0;
1942 out:
1943 xfrm_state_put(x);
1944 return err;
1947 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1948 struct nlattr **attrs)
1950 struct net *net = sock_net(skb->sk);
1951 struct km_event c;
1952 u8 type = XFRM_POLICY_TYPE_MAIN;
1953 int err;
1955 err = copy_from_user_policy_type(&type, attrs);
1956 if (err)
1957 return err;
1959 err = xfrm_policy_flush(net, type, true);
1960 if (err) {
1961 if (err == -ESRCH) /* empty table */
1962 return 0;
1963 return err;
1966 c.data.type = type;
1967 c.event = nlh->nlmsg_type;
1968 c.seq = nlh->nlmsg_seq;
1969 c.portid = nlh->nlmsg_pid;
1970 c.net = net;
1971 km_policy_notify(NULL, 0, &c);
1972 return 0;
1975 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1976 struct nlattr **attrs)
1978 struct net *net = sock_net(skb->sk);
1979 struct xfrm_policy *xp;
1980 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1981 struct xfrm_userpolicy_info *p = &up->pol;
1982 u8 type = XFRM_POLICY_TYPE_MAIN;
1983 int err = -ENOENT;
1984 struct xfrm_mark m;
1985 u32 mark = xfrm_mark_get(attrs, &m);
1987 err = copy_from_user_policy_type(&type, attrs);
1988 if (err)
1989 return err;
1991 err = verify_policy_dir(p->dir);
1992 if (err)
1993 return err;
1995 if (p->index)
1996 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1997 else {
1998 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1999 struct xfrm_sec_ctx *ctx;
2001 err = verify_sec_ctx_len(attrs);
2002 if (err)
2003 return err;
2005 ctx = NULL;
2006 if (rt) {
2007 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2009 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2010 if (err)
2011 return err;
2013 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
2014 &p->sel, ctx, 0, &err);
2015 security_xfrm_policy_free(ctx);
2017 if (xp == NULL)
2018 return -ENOENT;
2020 if (unlikely(xp->walk.dead))
2021 goto out;
2023 err = 0;
2024 if (up->hard) {
2025 xfrm_policy_delete(xp, p->dir);
2026 xfrm_audit_policy_delete(xp, 1, true);
2027 } else {
2028 // reset the timers here?
2029 WARN(1, "Dont know what to do with soft policy expire\n");
2031 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2033 out:
2034 xfrm_pol_put(xp);
2035 return err;
2038 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2039 struct nlattr **attrs)
2041 struct net *net = sock_net(skb->sk);
2042 struct xfrm_state *x;
2043 int err;
2044 struct xfrm_user_expire *ue = nlmsg_data(nlh);
2045 struct xfrm_usersa_info *p = &ue->state;
2046 struct xfrm_mark m;
2047 u32 mark = xfrm_mark_get(attrs, &m);
2049 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2051 err = -ENOENT;
2052 if (x == NULL)
2053 return err;
2055 spin_lock_bh(&x->lock);
2056 err = -EINVAL;
2057 if (x->km.state != XFRM_STATE_VALID)
2058 goto out;
2059 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2061 if (ue->hard) {
2062 __xfrm_state_delete(x);
2063 xfrm_audit_state_delete(x, 1, true);
2065 err = 0;
2066 out:
2067 spin_unlock_bh(&x->lock);
2068 xfrm_state_put(x);
2069 return err;
2072 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2073 struct nlattr **attrs)
2075 struct net *net = sock_net(skb->sk);
2076 struct xfrm_policy *xp;
2077 struct xfrm_user_tmpl *ut;
2078 int i;
2079 struct nlattr *rt = attrs[XFRMA_TMPL];
2080 struct xfrm_mark mark;
2082 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2083 struct xfrm_state *x = xfrm_state_alloc(net);
2084 int err = -ENOMEM;
2086 if (!x)
2087 goto nomem;
2089 xfrm_mark_get(attrs, &mark);
2091 err = verify_newpolicy_info(&ua->policy);
2092 if (err)
2093 goto bad_policy;
2095 /* build an XP */
2096 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2097 if (!xp)
2098 goto free_state;
2100 memcpy(&x->id, &ua->id, sizeof(ua->id));
2101 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2102 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2103 xp->mark.m = x->mark.m = mark.m;
2104 xp->mark.v = x->mark.v = mark.v;
2105 ut = nla_data(rt);
2106 /* extract the templates and for each call km_key */
2107 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2108 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2109 memcpy(&x->id, &t->id, sizeof(x->id));
2110 x->props.mode = t->mode;
2111 x->props.reqid = t->reqid;
2112 x->props.family = ut->family;
2113 t->aalgos = ua->aalgos;
2114 t->ealgos = ua->ealgos;
2115 t->calgos = ua->calgos;
2116 err = km_query(x, t, xp);
2120 kfree(x);
2121 kfree(xp);
2123 return 0;
2125 bad_policy:
2126 WARN(1, "BAD policy passed\n");
2127 free_state:
2128 kfree(x);
2129 nomem:
2130 return err;
2133 #ifdef CONFIG_XFRM_MIGRATE
2134 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2135 struct xfrm_kmaddress *k,
2136 struct nlattr **attrs, int *num)
2138 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2139 struct xfrm_user_migrate *um;
2140 int i, num_migrate;
2142 if (k != NULL) {
2143 struct xfrm_user_kmaddress *uk;
2145 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2146 memcpy(&k->local, &uk->local, sizeof(k->local));
2147 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2148 k->family = uk->family;
2149 k->reserved = uk->reserved;
2152 um = nla_data(rt);
2153 num_migrate = nla_len(rt) / sizeof(*um);
2155 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2156 return -EINVAL;
2158 for (i = 0; i < num_migrate; i++, um++, ma++) {
2159 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2160 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2161 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2162 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2164 ma->proto = um->proto;
2165 ma->mode = um->mode;
2166 ma->reqid = um->reqid;
2168 ma->old_family = um->old_family;
2169 ma->new_family = um->new_family;
2172 *num = i;
2173 return 0;
2176 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2177 struct nlattr **attrs)
2179 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2180 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2181 struct xfrm_kmaddress km, *kmp;
2182 u8 type;
2183 int err;
2184 int n = 0;
2185 struct net *net = sock_net(skb->sk);
2187 if (attrs[XFRMA_MIGRATE] == NULL)
2188 return -EINVAL;
2190 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2192 err = copy_from_user_policy_type(&type, attrs);
2193 if (err)
2194 return err;
2196 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2197 if (err)
2198 return err;
2200 if (!n)
2201 return 0;
2203 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
2205 return 0;
2207 #else
2208 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2209 struct nlattr **attrs)
2211 return -ENOPROTOOPT;
2213 #endif
2215 #ifdef CONFIG_XFRM_MIGRATE
2216 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2218 struct xfrm_user_migrate um;
2220 memset(&um, 0, sizeof(um));
2221 um.proto = m->proto;
2222 um.mode = m->mode;
2223 um.reqid = m->reqid;
2224 um.old_family = m->old_family;
2225 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2226 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2227 um.new_family = m->new_family;
2228 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2229 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2231 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2234 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2236 struct xfrm_user_kmaddress uk;
2238 memset(&uk, 0, sizeof(uk));
2239 uk.family = k->family;
2240 uk.reserved = k->reserved;
2241 memcpy(&uk.local, &k->local, sizeof(uk.local));
2242 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2244 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2247 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2249 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2250 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2251 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2252 + userpolicy_type_attrsize();
2255 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2256 int num_migrate, const struct xfrm_kmaddress *k,
2257 const struct xfrm_selector *sel, u8 dir, u8 type)
2259 const struct xfrm_migrate *mp;
2260 struct xfrm_userpolicy_id *pol_id;
2261 struct nlmsghdr *nlh;
2262 int i, err;
2264 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2265 if (nlh == NULL)
2266 return -EMSGSIZE;
2268 pol_id = nlmsg_data(nlh);
2269 /* copy data from selector, dir, and type to the pol_id */
2270 memset(pol_id, 0, sizeof(*pol_id));
2271 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2272 pol_id->dir = dir;
2274 if (k != NULL) {
2275 err = copy_to_user_kmaddress(k, skb);
2276 if (err)
2277 goto out_cancel;
2279 err = copy_to_user_policy_type(type, skb);
2280 if (err)
2281 goto out_cancel;
2282 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2283 err = copy_to_user_migrate(mp, skb);
2284 if (err)
2285 goto out_cancel;
2288 nlmsg_end(skb, nlh);
2289 return 0;
2291 out_cancel:
2292 nlmsg_cancel(skb, nlh);
2293 return err;
2296 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2297 const struct xfrm_migrate *m, int num_migrate,
2298 const struct xfrm_kmaddress *k)
2300 struct net *net = &init_net;
2301 struct sk_buff *skb;
2303 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2304 if (skb == NULL)
2305 return -ENOMEM;
2307 /* build migrate */
2308 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2309 BUG();
2311 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2313 #else
2314 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2315 const struct xfrm_migrate *m, int num_migrate,
2316 const struct xfrm_kmaddress *k)
2318 return -ENOPROTOOPT;
2320 #endif
2322 #define XMSGSIZE(type) sizeof(struct type)
2324 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2325 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2326 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2327 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2328 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2329 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2330 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2331 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2332 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2333 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2334 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2335 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2336 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2337 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2338 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2339 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2340 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2341 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2342 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2343 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2344 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2345 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2348 #undef XMSGSIZE
2350 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2351 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2352 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2353 [XFRMA_LASTUSED] = { .type = NLA_U64},
2354 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2355 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2356 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2357 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2358 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2359 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2360 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2361 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2362 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2363 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2364 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2365 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2366 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2367 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2368 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2369 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2370 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2371 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2372 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2373 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2374 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
2375 [XFRMA_PROTO] = { .type = NLA_U8 },
2376 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
2379 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2380 [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2381 [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2384 static const struct xfrm_link {
2385 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2386 int (*dump)(struct sk_buff *, struct netlink_callback *);
2387 int (*done)(struct netlink_callback *);
2388 const struct nla_policy *nla_pol;
2389 int nla_max;
2390 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2391 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2392 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2393 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2394 .dump = xfrm_dump_sa,
2395 .done = xfrm_dump_sa_done },
2396 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2397 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2398 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2399 .dump = xfrm_dump_policy,
2400 .done = xfrm_dump_policy_done },
2401 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2402 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2403 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2404 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2405 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2406 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2407 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2408 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2409 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2410 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2411 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2412 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2413 [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2414 .nla_pol = xfrma_spd_policy,
2415 .nla_max = XFRMA_SPD_MAX },
2416 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2419 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2421 struct net *net = sock_net(skb->sk);
2422 struct nlattr *attrs[XFRMA_MAX+1];
2423 const struct xfrm_link *link;
2424 int type, err;
2426 #ifdef CONFIG_COMPAT
2427 if (is_compat_task())
2428 return -ENOTSUPP;
2429 #endif
2431 type = nlh->nlmsg_type;
2432 if (type > XFRM_MSG_MAX)
2433 return -EINVAL;
2435 type -= XFRM_MSG_BASE;
2436 link = &xfrm_dispatch[type];
2438 /* All operations require privileges, even GET */
2439 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2440 return -EPERM;
2442 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2443 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2444 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2445 if (link->dump == NULL)
2446 return -EINVAL;
2449 struct netlink_dump_control c = {
2450 .dump = link->dump,
2451 .done = link->done,
2453 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2457 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2458 link->nla_max ? : XFRMA_MAX,
2459 link->nla_pol ? : xfrma_policy);
2460 if (err < 0)
2461 return err;
2463 if (link->doit == NULL)
2464 return -EINVAL;
2466 return link->doit(skb, nlh, attrs);
2469 static void xfrm_netlink_rcv(struct sk_buff *skb)
2471 struct net *net = sock_net(skb->sk);
2473 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2474 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2475 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2478 static inline size_t xfrm_expire_msgsize(void)
2480 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2481 + nla_total_size(sizeof(struct xfrm_mark));
2484 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2486 struct xfrm_user_expire *ue;
2487 struct nlmsghdr *nlh;
2488 int err;
2490 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2491 if (nlh == NULL)
2492 return -EMSGSIZE;
2494 ue = nlmsg_data(nlh);
2495 copy_to_user_state(x, &ue->state);
2496 ue->hard = (c->data.hard != 0) ? 1 : 0;
2498 err = xfrm_mark_put(skb, &x->mark);
2499 if (err)
2500 return err;
2502 nlmsg_end(skb, nlh);
2503 return 0;
2506 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2508 struct net *net = xs_net(x);
2509 struct sk_buff *skb;
2511 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2512 if (skb == NULL)
2513 return -ENOMEM;
2515 if (build_expire(skb, x, c) < 0) {
2516 kfree_skb(skb);
2517 return -EMSGSIZE;
2520 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2523 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2525 struct net *net = xs_net(x);
2526 struct sk_buff *skb;
2528 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2529 if (skb == NULL)
2530 return -ENOMEM;
2532 if (build_aevent(skb, x, c) < 0)
2533 BUG();
2535 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2538 static int xfrm_notify_sa_flush(const struct km_event *c)
2540 struct net *net = c->net;
2541 struct xfrm_usersa_flush *p;
2542 struct nlmsghdr *nlh;
2543 struct sk_buff *skb;
2544 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2546 skb = nlmsg_new(len, GFP_ATOMIC);
2547 if (skb == NULL)
2548 return -ENOMEM;
2550 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2551 if (nlh == NULL) {
2552 kfree_skb(skb);
2553 return -EMSGSIZE;
2556 p = nlmsg_data(nlh);
2557 p->proto = c->data.proto;
2559 nlmsg_end(skb, nlh);
2561 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2564 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2566 size_t l = 0;
2567 if (x->aead)
2568 l += nla_total_size(aead_len(x->aead));
2569 if (x->aalg) {
2570 l += nla_total_size(sizeof(struct xfrm_algo) +
2571 (x->aalg->alg_key_len + 7) / 8);
2572 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2574 if (x->ealg)
2575 l += nla_total_size(xfrm_alg_len(x->ealg));
2576 if (x->calg)
2577 l += nla_total_size(sizeof(*x->calg));
2578 if (x->encap)
2579 l += nla_total_size(sizeof(*x->encap));
2580 if (x->tfcpad)
2581 l += nla_total_size(sizeof(x->tfcpad));
2582 if (x->replay_esn)
2583 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2584 else
2585 l += nla_total_size(sizeof(struct xfrm_replay_state));
2586 if (x->security)
2587 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2588 x->security->ctx_len);
2589 if (x->coaddr)
2590 l += nla_total_size(sizeof(*x->coaddr));
2591 if (x->props.extra_flags)
2592 l += nla_total_size(sizeof(x->props.extra_flags));
2594 /* Must count x->lastused as it may become non-zero behind our back. */
2595 l += nla_total_size(sizeof(u64));
2597 return l;
2600 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2602 struct net *net = xs_net(x);
2603 struct xfrm_usersa_info *p;
2604 struct xfrm_usersa_id *id;
2605 struct nlmsghdr *nlh;
2606 struct sk_buff *skb;
2607 int len = xfrm_sa_len(x);
2608 int headlen, err;
2610 headlen = sizeof(*p);
2611 if (c->event == XFRM_MSG_DELSA) {
2612 len += nla_total_size(headlen);
2613 headlen = sizeof(*id);
2614 len += nla_total_size(sizeof(struct xfrm_mark));
2616 len += NLMSG_ALIGN(headlen);
2618 skb = nlmsg_new(len, GFP_ATOMIC);
2619 if (skb == NULL)
2620 return -ENOMEM;
2622 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2623 err = -EMSGSIZE;
2624 if (nlh == NULL)
2625 goto out_free_skb;
2627 p = nlmsg_data(nlh);
2628 if (c->event == XFRM_MSG_DELSA) {
2629 struct nlattr *attr;
2631 id = nlmsg_data(nlh);
2632 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2633 id->spi = x->id.spi;
2634 id->family = x->props.family;
2635 id->proto = x->id.proto;
2637 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2638 err = -EMSGSIZE;
2639 if (attr == NULL)
2640 goto out_free_skb;
2642 p = nla_data(attr);
2644 err = copy_to_user_state_extra(x, p, skb);
2645 if (err)
2646 goto out_free_skb;
2648 nlmsg_end(skb, nlh);
2650 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2652 out_free_skb:
2653 kfree_skb(skb);
2654 return err;
2657 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2660 switch (c->event) {
2661 case XFRM_MSG_EXPIRE:
2662 return xfrm_exp_state_notify(x, c);
2663 case XFRM_MSG_NEWAE:
2664 return xfrm_aevent_state_notify(x, c);
2665 case XFRM_MSG_DELSA:
2666 case XFRM_MSG_UPDSA:
2667 case XFRM_MSG_NEWSA:
2668 return xfrm_notify_sa(x, c);
2669 case XFRM_MSG_FLUSHSA:
2670 return xfrm_notify_sa_flush(c);
2671 default:
2672 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2673 c->event);
2674 break;
2677 return 0;
2681 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2682 struct xfrm_policy *xp)
2684 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2685 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2686 + nla_total_size(sizeof(struct xfrm_mark))
2687 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2688 + userpolicy_type_attrsize();
2691 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2692 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2694 __u32 seq = xfrm_get_acqseq();
2695 struct xfrm_user_acquire *ua;
2696 struct nlmsghdr *nlh;
2697 int err;
2699 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2700 if (nlh == NULL)
2701 return -EMSGSIZE;
2703 ua = nlmsg_data(nlh);
2704 memcpy(&ua->id, &x->id, sizeof(ua->id));
2705 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2706 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2707 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2708 ua->aalgos = xt->aalgos;
2709 ua->ealgos = xt->ealgos;
2710 ua->calgos = xt->calgos;
2711 ua->seq = x->km.seq = seq;
2713 err = copy_to_user_tmpl(xp, skb);
2714 if (!err)
2715 err = copy_to_user_state_sec_ctx(x, skb);
2716 if (!err)
2717 err = copy_to_user_policy_type(xp->type, skb);
2718 if (!err)
2719 err = xfrm_mark_put(skb, &xp->mark);
2720 if (err) {
2721 nlmsg_cancel(skb, nlh);
2722 return err;
2725 nlmsg_end(skb, nlh);
2726 return 0;
2729 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2730 struct xfrm_policy *xp)
2732 struct net *net = xs_net(x);
2733 struct sk_buff *skb;
2735 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2736 if (skb == NULL)
2737 return -ENOMEM;
2739 if (build_acquire(skb, x, xt, xp) < 0)
2740 BUG();
2742 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2745 /* User gives us xfrm_user_policy_info followed by an array of 0
2746 * or more templates.
2748 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2749 u8 *data, int len, int *dir)
2751 struct net *net = sock_net(sk);
2752 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2753 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2754 struct xfrm_policy *xp;
2755 int nr;
2757 switch (sk->sk_family) {
2758 case AF_INET:
2759 if (opt != IP_XFRM_POLICY) {
2760 *dir = -EOPNOTSUPP;
2761 return NULL;
2763 break;
2764 #if IS_ENABLED(CONFIG_IPV6)
2765 case AF_INET6:
2766 if (opt != IPV6_XFRM_POLICY) {
2767 *dir = -EOPNOTSUPP;
2768 return NULL;
2770 break;
2771 #endif
2772 default:
2773 *dir = -EINVAL;
2774 return NULL;
2777 *dir = -EINVAL;
2779 if (len < sizeof(*p) ||
2780 verify_newpolicy_info(p))
2781 return NULL;
2783 nr = ((len - sizeof(*p)) / sizeof(*ut));
2784 if (validate_tmpl(nr, ut, p->sel.family))
2785 return NULL;
2787 if (p->dir > XFRM_POLICY_OUT)
2788 return NULL;
2790 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2791 if (xp == NULL) {
2792 *dir = -ENOBUFS;
2793 return NULL;
2796 copy_from_user_policy(xp, p);
2797 xp->type = XFRM_POLICY_TYPE_MAIN;
2798 copy_templates(xp, ut, nr);
2800 *dir = p->dir;
2802 return xp;
2805 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2807 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2808 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2809 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2810 + nla_total_size(sizeof(struct xfrm_mark))
2811 + userpolicy_type_attrsize();
2814 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2815 int dir, const struct km_event *c)
2817 struct xfrm_user_polexpire *upe;
2818 int hard = c->data.hard;
2819 struct nlmsghdr *nlh;
2820 int err;
2822 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2823 if (nlh == NULL)
2824 return -EMSGSIZE;
2826 upe = nlmsg_data(nlh);
2827 copy_to_user_policy(xp, &upe->pol, dir);
2828 err = copy_to_user_tmpl(xp, skb);
2829 if (!err)
2830 err = copy_to_user_sec_ctx(xp, skb);
2831 if (!err)
2832 err = copy_to_user_policy_type(xp->type, skb);
2833 if (!err)
2834 err = xfrm_mark_put(skb, &xp->mark);
2835 if (err) {
2836 nlmsg_cancel(skb, nlh);
2837 return err;
2839 upe->hard = !!hard;
2841 nlmsg_end(skb, nlh);
2842 return 0;
2845 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2847 struct net *net = xp_net(xp);
2848 struct sk_buff *skb;
2850 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2851 if (skb == NULL)
2852 return -ENOMEM;
2854 if (build_polexpire(skb, xp, dir, c) < 0)
2855 BUG();
2857 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2860 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2862 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2863 struct net *net = xp_net(xp);
2864 struct xfrm_userpolicy_info *p;
2865 struct xfrm_userpolicy_id *id;
2866 struct nlmsghdr *nlh;
2867 struct sk_buff *skb;
2868 int headlen, err;
2870 headlen = sizeof(*p);
2871 if (c->event == XFRM_MSG_DELPOLICY) {
2872 len += nla_total_size(headlen);
2873 headlen = sizeof(*id);
2875 len += userpolicy_type_attrsize();
2876 len += nla_total_size(sizeof(struct xfrm_mark));
2877 len += NLMSG_ALIGN(headlen);
2879 skb = nlmsg_new(len, GFP_ATOMIC);
2880 if (skb == NULL)
2881 return -ENOMEM;
2883 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2884 err = -EMSGSIZE;
2885 if (nlh == NULL)
2886 goto out_free_skb;
2888 p = nlmsg_data(nlh);
2889 if (c->event == XFRM_MSG_DELPOLICY) {
2890 struct nlattr *attr;
2892 id = nlmsg_data(nlh);
2893 memset(id, 0, sizeof(*id));
2894 id->dir = dir;
2895 if (c->data.byid)
2896 id->index = xp->index;
2897 else
2898 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2900 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2901 err = -EMSGSIZE;
2902 if (attr == NULL)
2903 goto out_free_skb;
2905 p = nla_data(attr);
2908 copy_to_user_policy(xp, p, dir);
2909 err = copy_to_user_tmpl(xp, skb);
2910 if (!err)
2911 err = copy_to_user_policy_type(xp->type, skb);
2912 if (!err)
2913 err = xfrm_mark_put(skb, &xp->mark);
2914 if (err)
2915 goto out_free_skb;
2917 nlmsg_end(skb, nlh);
2919 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2921 out_free_skb:
2922 kfree_skb(skb);
2923 return err;
2926 static int xfrm_notify_policy_flush(const struct km_event *c)
2928 struct net *net = c->net;
2929 struct nlmsghdr *nlh;
2930 struct sk_buff *skb;
2931 int err;
2933 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2934 if (skb == NULL)
2935 return -ENOMEM;
2937 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2938 err = -EMSGSIZE;
2939 if (nlh == NULL)
2940 goto out_free_skb;
2941 err = copy_to_user_policy_type(c->data.type, skb);
2942 if (err)
2943 goto out_free_skb;
2945 nlmsg_end(skb, nlh);
2947 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
2949 out_free_skb:
2950 kfree_skb(skb);
2951 return err;
2954 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2957 switch (c->event) {
2958 case XFRM_MSG_NEWPOLICY:
2959 case XFRM_MSG_UPDPOLICY:
2960 case XFRM_MSG_DELPOLICY:
2961 return xfrm_notify_policy(xp, dir, c);
2962 case XFRM_MSG_FLUSHPOLICY:
2963 return xfrm_notify_policy_flush(c);
2964 case XFRM_MSG_POLEXPIRE:
2965 return xfrm_exp_policy_notify(xp, dir, c);
2966 default:
2967 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2968 c->event);
2971 return 0;
2975 static inline size_t xfrm_report_msgsize(void)
2977 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2980 static int build_report(struct sk_buff *skb, u8 proto,
2981 struct xfrm_selector *sel, xfrm_address_t *addr)
2983 struct xfrm_user_report *ur;
2984 struct nlmsghdr *nlh;
2986 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2987 if (nlh == NULL)
2988 return -EMSGSIZE;
2990 ur = nlmsg_data(nlh);
2991 ur->proto = proto;
2992 memcpy(&ur->sel, sel, sizeof(ur->sel));
2994 if (addr) {
2995 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2996 if (err) {
2997 nlmsg_cancel(skb, nlh);
2998 return err;
3001 nlmsg_end(skb, nlh);
3002 return 0;
3005 static int xfrm_send_report(struct net *net, u8 proto,
3006 struct xfrm_selector *sel, xfrm_address_t *addr)
3008 struct sk_buff *skb;
3010 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3011 if (skb == NULL)
3012 return -ENOMEM;
3014 if (build_report(skb, proto, sel, addr) < 0)
3015 BUG();
3017 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3020 static inline size_t xfrm_mapping_msgsize(void)
3022 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3025 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3026 xfrm_address_t *new_saddr, __be16 new_sport)
3028 struct xfrm_user_mapping *um;
3029 struct nlmsghdr *nlh;
3031 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3032 if (nlh == NULL)
3033 return -EMSGSIZE;
3035 um = nlmsg_data(nlh);
3037 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3038 um->id.spi = x->id.spi;
3039 um->id.family = x->props.family;
3040 um->id.proto = x->id.proto;
3041 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3042 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3043 um->new_sport = new_sport;
3044 um->old_sport = x->encap->encap_sport;
3045 um->reqid = x->props.reqid;
3047 nlmsg_end(skb, nlh);
3048 return 0;
3051 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3052 __be16 sport)
3054 struct net *net = xs_net(x);
3055 struct sk_buff *skb;
3057 if (x->id.proto != IPPROTO_ESP)
3058 return -EINVAL;
3060 if (!x->encap)
3061 return -EINVAL;
3063 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3064 if (skb == NULL)
3065 return -ENOMEM;
3067 if (build_mapping(skb, x, ipaddr, sport) < 0)
3068 BUG();
3070 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3073 static bool xfrm_is_alive(const struct km_event *c)
3075 return (bool)xfrm_acquire_is_on(c->net);
3078 static struct xfrm_mgr netlink_mgr = {
3079 .id = "netlink",
3080 .notify = xfrm_send_state_notify,
3081 .acquire = xfrm_send_acquire,
3082 .compile_policy = xfrm_compile_policy,
3083 .notify_policy = xfrm_send_policy_notify,
3084 .report = xfrm_send_report,
3085 .migrate = xfrm_send_migrate,
3086 .new_mapping = xfrm_send_mapping,
3087 .is_alive = xfrm_is_alive,
3090 static int __net_init xfrm_user_net_init(struct net *net)
3092 struct sock *nlsk;
3093 struct netlink_kernel_cfg cfg = {
3094 .groups = XFRMNLGRP_MAX,
3095 .input = xfrm_netlink_rcv,
3098 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3099 if (nlsk == NULL)
3100 return -ENOMEM;
3101 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3102 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3103 return 0;
3106 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3108 struct net *net;
3109 list_for_each_entry(net, net_exit_list, exit_list)
3110 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3111 synchronize_net();
3112 list_for_each_entry(net, net_exit_list, exit_list)
3113 netlink_kernel_release(net->xfrm.nlsk_stash);
3116 static struct pernet_operations xfrm_user_net_ops = {
3117 .init = xfrm_user_net_init,
3118 .exit_batch = xfrm_user_net_exit,
3121 static int __init xfrm_user_init(void)
3123 int rv;
3125 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3127 rv = register_pernet_subsys(&xfrm_user_net_ops);
3128 if (rv < 0)
3129 return rv;
3130 rv = xfrm_register_km(&netlink_mgr);
3131 if (rv < 0)
3132 unregister_pernet_subsys(&xfrm_user_net_ops);
3133 return rv;
3136 static void __exit xfrm_user_exit(void)
3138 xfrm_unregister_km(&netlink_mgr);
3139 unregister_pernet_subsys(&xfrm_user_net_ops);
3142 module_init(xfrm_user_init);
3143 module_exit(xfrm_user_exit);
3144 MODULE_LICENSE("GPL");
3145 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);