staging: hv: Fix GARP not sent after Quick Migration
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / xfrm / xfrm_user.c
blob3d15d3e1b2c49b7c124cd08d42ed5a1a77849c57
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 defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32 #include <linux/in6.h>
33 #endif
35 static inline int aead_len(struct xfrm_algo_aead *alg)
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
40 static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
42 struct nlattr *rt = attrs[type];
43 struct xfrm_algo *algp;
45 if (!rt)
46 return 0;
48 algp = nla_data(rt);
49 if (nla_len(rt) < xfrm_alg_len(algp))
50 return -EINVAL;
52 switch (type) {
53 case XFRMA_ALG_AUTH:
54 case XFRMA_ALG_CRYPT:
55 case XFRMA_ALG_COMP:
56 break;
58 default:
59 return -EINVAL;
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
66 static int verify_auth_trunc(struct nlattr **attrs)
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
71 if (!rt)
72 return 0;
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
82 static int verify_aead(struct nlattr **attrs)
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
87 if (!rt)
88 return 0;
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
98 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
99 xfrm_address_t **addrp)
101 struct nlattr *rt = attrs[type];
103 if (rt && addrp)
104 *addrp = nla_data(rt);
107 static inline int verify_sec_ctx_len(struct nlattr **attrs)
109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
110 struct xfrm_user_sec_ctx *uctx;
112 if (!rt)
113 return 0;
115 uctx = nla_data(rt);
116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
117 return -EINVAL;
119 return 0;
122 static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
127 if (!rt)
128 return 0;
130 if (p->id.proto != IPPROTO_ESP)
131 return -EINVAL;
133 if (p->replay_window != 0)
134 return -EINVAL;
136 return 0;
139 static int verify_newsa_info(struct xfrm_usersa_info *p,
140 struct nlattr **attrs)
142 int err;
144 err = -EINVAL;
145 switch (p->family) {
146 case AF_INET:
147 break;
149 case AF_INET6:
150 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
151 break;
152 #else
153 err = -EAFNOSUPPORT;
154 goto out;
155 #endif
157 default:
158 goto out;
161 err = -EINVAL;
162 switch (p->id.proto) {
163 case IPPROTO_AH:
164 if ((!attrs[XFRMA_ALG_AUTH] &&
165 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
166 attrs[XFRMA_ALG_AEAD] ||
167 attrs[XFRMA_ALG_CRYPT] ||
168 attrs[XFRMA_ALG_COMP] ||
169 attrs[XFRMA_TFCPAD])
170 goto out;
171 break;
173 case IPPROTO_ESP:
174 if (attrs[XFRMA_ALG_COMP])
175 goto out;
176 if (!attrs[XFRMA_ALG_AUTH] &&
177 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
178 !attrs[XFRMA_ALG_CRYPT] &&
179 !attrs[XFRMA_ALG_AEAD])
180 goto out;
181 if ((attrs[XFRMA_ALG_AUTH] ||
182 attrs[XFRMA_ALG_AUTH_TRUNC] ||
183 attrs[XFRMA_ALG_CRYPT]) &&
184 attrs[XFRMA_ALG_AEAD])
185 goto out;
186 if (attrs[XFRMA_TFCPAD] &&
187 p->mode != XFRM_MODE_TUNNEL)
188 goto out;
189 break;
191 case IPPROTO_COMP:
192 if (!attrs[XFRMA_ALG_COMP] ||
193 attrs[XFRMA_ALG_AEAD] ||
194 attrs[XFRMA_ALG_AUTH] ||
195 attrs[XFRMA_ALG_AUTH_TRUNC] ||
196 attrs[XFRMA_ALG_CRYPT] ||
197 attrs[XFRMA_TFCPAD])
198 goto out;
199 break;
201 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
202 case IPPROTO_DSTOPTS:
203 case IPPROTO_ROUTING:
204 if (attrs[XFRMA_ALG_COMP] ||
205 attrs[XFRMA_ALG_AUTH] ||
206 attrs[XFRMA_ALG_AUTH_TRUNC] ||
207 attrs[XFRMA_ALG_AEAD] ||
208 attrs[XFRMA_ALG_CRYPT] ||
209 attrs[XFRMA_ENCAP] ||
210 attrs[XFRMA_SEC_CTX] ||
211 attrs[XFRMA_TFCPAD] ||
212 !attrs[XFRMA_COADDR])
213 goto out;
214 break;
215 #endif
217 default:
218 goto out;
221 if ((err = verify_aead(attrs)))
222 goto out;
223 if ((err = verify_auth_trunc(attrs)))
224 goto out;
225 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
226 goto out;
227 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
228 goto out;
229 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
230 goto out;
231 if ((err = verify_sec_ctx_len(attrs)))
232 goto out;
233 if ((err = verify_replay(p, attrs)))
234 goto out;
236 err = -EINVAL;
237 switch (p->mode) {
238 case XFRM_MODE_TRANSPORT:
239 case XFRM_MODE_TUNNEL:
240 case XFRM_MODE_ROUTEOPTIMIZATION:
241 case XFRM_MODE_BEET:
242 break;
244 default:
245 goto out;
248 err = 0;
250 out:
251 return err;
254 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
255 struct xfrm_algo_desc *(*get_byname)(const char *, int),
256 struct nlattr *rta)
258 struct xfrm_algo *p, *ualg;
259 struct xfrm_algo_desc *algo;
261 if (!rta)
262 return 0;
264 ualg = nla_data(rta);
266 algo = get_byname(ualg->alg_name, 1);
267 if (!algo)
268 return -ENOSYS;
269 *props = algo->desc.sadb_alg_id;
271 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
272 if (!p)
273 return -ENOMEM;
275 strcpy(p->alg_name, algo->name);
276 *algpp = p;
277 return 0;
280 static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
281 struct nlattr *rta)
283 struct xfrm_algo *ualg;
284 struct xfrm_algo_auth *p;
285 struct xfrm_algo_desc *algo;
287 if (!rta)
288 return 0;
290 ualg = nla_data(rta);
292 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
293 if (!algo)
294 return -ENOSYS;
295 *props = algo->desc.sadb_alg_id;
297 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
298 if (!p)
299 return -ENOMEM;
301 strcpy(p->alg_name, algo->name);
302 p->alg_key_len = ualg->alg_key_len;
303 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
304 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
306 *algpp = p;
307 return 0;
310 static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
311 struct nlattr *rta)
313 struct xfrm_algo_auth *p, *ualg;
314 struct xfrm_algo_desc *algo;
316 if (!rta)
317 return 0;
319 ualg = nla_data(rta);
321 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
322 if (!algo)
323 return -ENOSYS;
324 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
325 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
326 return -EINVAL;
327 *props = algo->desc.sadb_alg_id;
329 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
330 if (!p)
331 return -ENOMEM;
333 strcpy(p->alg_name, algo->name);
334 if (!p->alg_trunc_len)
335 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
337 *algpp = p;
338 return 0;
341 static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
342 struct nlattr *rta)
344 struct xfrm_algo_aead *p, *ualg;
345 struct xfrm_algo_desc *algo;
347 if (!rta)
348 return 0;
350 ualg = nla_data(rta);
352 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
353 if (!algo)
354 return -ENOSYS;
355 *props = algo->desc.sadb_alg_id;
357 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
358 if (!p)
359 return -ENOMEM;
361 strcpy(p->alg_name, algo->name);
362 *algpp = p;
363 return 0;
366 static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
367 struct nlattr *rp)
369 struct xfrm_replay_state_esn *up;
371 if (!replay_esn || !rp)
372 return 0;
374 up = nla_data(rp);
376 if (xfrm_replay_state_esn_len(replay_esn) !=
377 xfrm_replay_state_esn_len(up))
378 return -EINVAL;
380 return 0;
383 static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
384 struct xfrm_replay_state_esn **preplay_esn,
385 struct nlattr *rta)
387 struct xfrm_replay_state_esn *p, *pp, *up;
389 if (!rta)
390 return 0;
392 up = nla_data(rta);
394 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
395 if (!p)
396 return -ENOMEM;
398 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
399 if (!pp) {
400 kfree(p);
401 return -ENOMEM;
404 *replay_esn = p;
405 *preplay_esn = pp;
407 return 0;
410 static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
412 int len = 0;
414 if (xfrm_ctx) {
415 len += sizeof(struct xfrm_user_sec_ctx);
416 len += xfrm_ctx->ctx_len;
418 return len;
421 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
423 memcpy(&x->id, &p->id, sizeof(x->id));
424 memcpy(&x->sel, &p->sel, sizeof(x->sel));
425 memcpy(&x->lft, &p->lft, sizeof(x->lft));
426 x->props.mode = p->mode;
427 x->props.replay_window = p->replay_window;
428 x->props.reqid = p->reqid;
429 x->props.family = p->family;
430 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
431 x->props.flags = p->flags;
433 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
434 x->sel.family = p->family;
438 * someday when pfkey also has support, we could have the code
439 * somehow made shareable and move it to xfrm_state.c - JHS
442 static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
444 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
445 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
446 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
447 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
448 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
450 if (re) {
451 struct xfrm_replay_state_esn *replay_esn;
452 replay_esn = nla_data(re);
453 memcpy(x->replay_esn, replay_esn,
454 xfrm_replay_state_esn_len(replay_esn));
455 memcpy(x->preplay_esn, replay_esn,
456 xfrm_replay_state_esn_len(replay_esn));
459 if (rp) {
460 struct xfrm_replay_state *replay;
461 replay = nla_data(rp);
462 memcpy(&x->replay, replay, sizeof(*replay));
463 memcpy(&x->preplay, replay, sizeof(*replay));
466 if (lt) {
467 struct xfrm_lifetime_cur *ltime;
468 ltime = nla_data(lt);
469 x->curlft.bytes = ltime->bytes;
470 x->curlft.packets = ltime->packets;
471 x->curlft.add_time = ltime->add_time;
472 x->curlft.use_time = ltime->use_time;
475 if (et)
476 x->replay_maxage = nla_get_u32(et);
478 if (rt)
479 x->replay_maxdiff = nla_get_u32(rt);
482 static struct xfrm_state *xfrm_state_construct(struct net *net,
483 struct xfrm_usersa_info *p,
484 struct nlattr **attrs,
485 int *errp)
487 struct xfrm_state *x = xfrm_state_alloc(net);
488 int err = -ENOMEM;
490 if (!x)
491 goto error_no_put;
493 copy_from_user_state(x, p);
495 if ((err = attach_aead(&x->aead, &x->props.ealgo,
496 attrs[XFRMA_ALG_AEAD])))
497 goto error;
498 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
499 attrs[XFRMA_ALG_AUTH_TRUNC])))
500 goto error;
501 if (!x->props.aalgo) {
502 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
503 attrs[XFRMA_ALG_AUTH])))
504 goto error;
506 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
507 xfrm_ealg_get_byname,
508 attrs[XFRMA_ALG_CRYPT])))
509 goto error;
510 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
511 xfrm_calg_get_byname,
512 attrs[XFRMA_ALG_COMP])))
513 goto error;
515 if (attrs[XFRMA_ENCAP]) {
516 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
517 sizeof(*x->encap), GFP_KERNEL);
518 if (x->encap == NULL)
519 goto error;
522 if (attrs[XFRMA_TFCPAD])
523 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
525 if (attrs[XFRMA_COADDR]) {
526 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
527 sizeof(*x->coaddr), GFP_KERNEL);
528 if (x->coaddr == NULL)
529 goto error;
532 xfrm_mark_get(attrs, &x->mark);
534 err = __xfrm_init_state(x, false);
535 if (err)
536 goto error;
538 if (attrs[XFRMA_SEC_CTX] &&
539 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
540 goto error;
542 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
543 attrs[XFRMA_REPLAY_ESN_VAL])))
544 goto error;
546 x->km.seq = p->seq;
547 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
548 /* sysctl_xfrm_aevent_etime is in 100ms units */
549 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
551 if ((err = xfrm_init_replay(x)))
552 goto error;
554 /* override default values from above */
555 xfrm_update_ae_params(x, attrs);
557 return x;
559 error:
560 x->km.state = XFRM_STATE_DEAD;
561 xfrm_state_put(x);
562 error_no_put:
563 *errp = err;
564 return NULL;
567 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
568 struct nlattr **attrs)
570 struct net *net = sock_net(skb->sk);
571 struct xfrm_usersa_info *p = nlmsg_data(nlh);
572 struct xfrm_state *x;
573 int err;
574 struct km_event c;
575 uid_t loginuid = audit_get_loginuid(current);
576 u32 sessionid = audit_get_sessionid(current);
577 u32 sid;
579 err = verify_newsa_info(p, attrs);
580 if (err)
581 return err;
583 x = xfrm_state_construct(net, p, attrs, &err);
584 if (!x)
585 return err;
587 xfrm_state_hold(x);
588 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
589 err = xfrm_state_add(x);
590 else
591 err = xfrm_state_update(x);
593 security_task_getsecid(current, &sid);
594 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
596 if (err < 0) {
597 x->km.state = XFRM_STATE_DEAD;
598 __xfrm_state_put(x);
599 goto out;
602 c.seq = nlh->nlmsg_seq;
603 c.pid = nlh->nlmsg_pid;
604 c.event = nlh->nlmsg_type;
606 km_state_notify(x, &c);
607 out:
608 xfrm_state_put(x);
609 return err;
612 static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
613 struct xfrm_usersa_id *p,
614 struct nlattr **attrs,
615 int *errp)
617 struct xfrm_state *x = NULL;
618 struct xfrm_mark m;
619 int err;
620 u32 mark = xfrm_mark_get(attrs, &m);
622 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
623 err = -ESRCH;
624 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
625 } else {
626 xfrm_address_t *saddr = NULL;
628 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
629 if (!saddr) {
630 err = -EINVAL;
631 goto out;
634 err = -ESRCH;
635 x = xfrm_state_lookup_byaddr(net, mark,
636 &p->daddr, saddr,
637 p->proto, p->family);
640 out:
641 if (!x && errp)
642 *errp = err;
643 return x;
646 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
647 struct nlattr **attrs)
649 struct net *net = sock_net(skb->sk);
650 struct xfrm_state *x;
651 int err = -ESRCH;
652 struct km_event c;
653 struct xfrm_usersa_id *p = nlmsg_data(nlh);
654 uid_t loginuid = audit_get_loginuid(current);
655 u32 sessionid = audit_get_sessionid(current);
656 u32 sid;
658 x = xfrm_user_state_lookup(net, p, attrs, &err);
659 if (x == NULL)
660 return err;
662 if ((err = security_xfrm_state_delete(x)) != 0)
663 goto out;
665 if (xfrm_state_kern(x)) {
666 err = -EPERM;
667 goto out;
670 err = xfrm_state_delete(x);
672 if (err < 0)
673 goto out;
675 c.seq = nlh->nlmsg_seq;
676 c.pid = nlh->nlmsg_pid;
677 c.event = nlh->nlmsg_type;
678 km_state_notify(x, &c);
680 out:
681 security_task_getsecid(current, &sid);
682 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
683 xfrm_state_put(x);
684 return err;
687 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
689 memcpy(&p->id, &x->id, sizeof(p->id));
690 memcpy(&p->sel, &x->sel, sizeof(p->sel));
691 memcpy(&p->lft, &x->lft, sizeof(p->lft));
692 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
693 memcpy(&p->stats, &x->stats, sizeof(p->stats));
694 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
695 p->mode = x->props.mode;
696 p->replay_window = x->props.replay_window;
697 p->reqid = x->props.reqid;
698 p->family = x->props.family;
699 p->flags = x->props.flags;
700 p->seq = x->km.seq;
703 struct xfrm_dump_info {
704 struct sk_buff *in_skb;
705 struct sk_buff *out_skb;
706 u32 nlmsg_seq;
707 u16 nlmsg_flags;
710 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
712 struct xfrm_user_sec_ctx *uctx;
713 struct nlattr *attr;
714 int ctx_size = sizeof(*uctx) + s->ctx_len;
716 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
717 if (attr == NULL)
718 return -EMSGSIZE;
720 uctx = nla_data(attr);
721 uctx->exttype = XFRMA_SEC_CTX;
722 uctx->len = ctx_size;
723 uctx->ctx_doi = s->ctx_doi;
724 uctx->ctx_alg = s->ctx_alg;
725 uctx->ctx_len = s->ctx_len;
726 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
728 return 0;
731 static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
733 struct xfrm_algo *algo;
734 struct nlattr *nla;
736 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
737 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
738 if (!nla)
739 return -EMSGSIZE;
741 algo = nla_data(nla);
742 strcpy(algo->alg_name, auth->alg_name);
743 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
744 algo->alg_key_len = auth->alg_key_len;
746 return 0;
749 /* Don't change this without updating xfrm_sa_len! */
750 static int copy_to_user_state_extra(struct xfrm_state *x,
751 struct xfrm_usersa_info *p,
752 struct sk_buff *skb)
754 copy_to_user_state(x, p);
756 if (x->coaddr)
757 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
759 if (x->lastused)
760 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
762 if (x->aead)
763 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
764 if (x->aalg) {
765 if (copy_to_user_auth(x->aalg, skb))
766 goto nla_put_failure;
768 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
769 xfrm_alg_auth_len(x->aalg), x->aalg);
771 if (x->ealg)
772 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
773 if (x->calg)
774 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
776 if (x->encap)
777 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
779 if (x->tfcpad)
780 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
782 if (xfrm_mark_put(skb, &x->mark))
783 goto nla_put_failure;
785 if (x->replay_esn)
786 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
787 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
789 if (x->security && copy_sec_ctx(x->security, skb) < 0)
790 goto nla_put_failure;
792 return 0;
794 nla_put_failure:
795 return -EMSGSIZE;
798 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
800 struct xfrm_dump_info *sp = ptr;
801 struct sk_buff *in_skb = sp->in_skb;
802 struct sk_buff *skb = sp->out_skb;
803 struct xfrm_usersa_info *p;
804 struct nlmsghdr *nlh;
805 int err;
807 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
808 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
809 if (nlh == NULL)
810 return -EMSGSIZE;
812 p = nlmsg_data(nlh);
814 err = copy_to_user_state_extra(x, p, skb);
815 if (err)
816 goto nla_put_failure;
818 nlmsg_end(skb, nlh);
819 return 0;
821 nla_put_failure:
822 nlmsg_cancel(skb, nlh);
823 return err;
826 static int xfrm_dump_sa_done(struct netlink_callback *cb)
828 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
829 xfrm_state_walk_done(walk);
830 return 0;
833 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
835 struct net *net = sock_net(skb->sk);
836 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
837 struct xfrm_dump_info info;
839 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
840 sizeof(cb->args) - sizeof(cb->args[0]));
842 info.in_skb = cb->skb;
843 info.out_skb = skb;
844 info.nlmsg_seq = cb->nlh->nlmsg_seq;
845 info.nlmsg_flags = NLM_F_MULTI;
847 if (!cb->args[0]) {
848 cb->args[0] = 1;
849 xfrm_state_walk_init(walk, 0);
852 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
854 return skb->len;
857 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
858 struct xfrm_state *x, u32 seq)
860 struct xfrm_dump_info info;
861 struct sk_buff *skb;
863 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
864 if (!skb)
865 return ERR_PTR(-ENOMEM);
867 info.in_skb = in_skb;
868 info.out_skb = skb;
869 info.nlmsg_seq = seq;
870 info.nlmsg_flags = 0;
872 if (dump_one_state(x, 0, &info)) {
873 kfree_skb(skb);
874 return NULL;
877 return skb;
880 static inline size_t xfrm_spdinfo_msgsize(void)
882 return NLMSG_ALIGN(4)
883 + nla_total_size(sizeof(struct xfrmu_spdinfo))
884 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
887 static int build_spdinfo(struct sk_buff *skb, struct net *net,
888 u32 pid, u32 seq, u32 flags)
890 struct xfrmk_spdinfo si;
891 struct xfrmu_spdinfo spc;
892 struct xfrmu_spdhinfo sph;
893 struct nlmsghdr *nlh;
894 u32 *f;
896 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
897 if (nlh == NULL) /* shouldnt really happen ... */
898 return -EMSGSIZE;
900 f = nlmsg_data(nlh);
901 *f = flags;
902 xfrm_spd_getinfo(net, &si);
903 spc.incnt = si.incnt;
904 spc.outcnt = si.outcnt;
905 spc.fwdcnt = si.fwdcnt;
906 spc.inscnt = si.inscnt;
907 spc.outscnt = si.outscnt;
908 spc.fwdscnt = si.fwdscnt;
909 sph.spdhcnt = si.spdhcnt;
910 sph.spdhmcnt = si.spdhmcnt;
912 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
913 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
915 return nlmsg_end(skb, nlh);
917 nla_put_failure:
918 nlmsg_cancel(skb, nlh);
919 return -EMSGSIZE;
922 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
923 struct nlattr **attrs)
925 struct net *net = sock_net(skb->sk);
926 struct sk_buff *r_skb;
927 u32 *flags = nlmsg_data(nlh);
928 u32 spid = NETLINK_CB(skb).pid;
929 u32 seq = nlh->nlmsg_seq;
931 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
932 if (r_skb == NULL)
933 return -ENOMEM;
935 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
936 BUG();
938 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
941 static inline size_t xfrm_sadinfo_msgsize(void)
943 return NLMSG_ALIGN(4)
944 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
945 + nla_total_size(4); /* XFRMA_SAD_CNT */
948 static int build_sadinfo(struct sk_buff *skb, struct net *net,
949 u32 pid, u32 seq, u32 flags)
951 struct xfrmk_sadinfo si;
952 struct xfrmu_sadhinfo sh;
953 struct nlmsghdr *nlh;
954 u32 *f;
956 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
957 if (nlh == NULL) /* shouldnt really happen ... */
958 return -EMSGSIZE;
960 f = nlmsg_data(nlh);
961 *f = flags;
962 xfrm_sad_getinfo(net, &si);
964 sh.sadhmcnt = si.sadhmcnt;
965 sh.sadhcnt = si.sadhcnt;
967 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
968 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
970 return nlmsg_end(skb, nlh);
972 nla_put_failure:
973 nlmsg_cancel(skb, nlh);
974 return -EMSGSIZE;
977 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
978 struct nlattr **attrs)
980 struct net *net = sock_net(skb->sk);
981 struct sk_buff *r_skb;
982 u32 *flags = nlmsg_data(nlh);
983 u32 spid = NETLINK_CB(skb).pid;
984 u32 seq = nlh->nlmsg_seq;
986 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
987 if (r_skb == NULL)
988 return -ENOMEM;
990 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
991 BUG();
993 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
996 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
997 struct nlattr **attrs)
999 struct net *net = sock_net(skb->sk);
1000 struct xfrm_usersa_id *p = nlmsg_data(nlh);
1001 struct xfrm_state *x;
1002 struct sk_buff *resp_skb;
1003 int err = -ESRCH;
1005 x = xfrm_user_state_lookup(net, p, attrs, &err);
1006 if (x == NULL)
1007 goto out_noput;
1009 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1010 if (IS_ERR(resp_skb)) {
1011 err = PTR_ERR(resp_skb);
1012 } else {
1013 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1015 xfrm_state_put(x);
1016 out_noput:
1017 return err;
1020 static int verify_userspi_info(struct xfrm_userspi_info *p)
1022 switch (p->info.id.proto) {
1023 case IPPROTO_AH:
1024 case IPPROTO_ESP:
1025 break;
1027 case IPPROTO_COMP:
1028 /* IPCOMP spi is 16-bits. */
1029 if (p->max >= 0x10000)
1030 return -EINVAL;
1031 break;
1033 default:
1034 return -EINVAL;
1037 if (p->min > p->max)
1038 return -EINVAL;
1040 return 0;
1043 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1044 struct nlattr **attrs)
1046 struct net *net = sock_net(skb->sk);
1047 struct xfrm_state *x;
1048 struct xfrm_userspi_info *p;
1049 struct sk_buff *resp_skb;
1050 xfrm_address_t *daddr;
1051 int family;
1052 int err;
1053 u32 mark;
1054 struct xfrm_mark m;
1056 p = nlmsg_data(nlh);
1057 err = verify_userspi_info(p);
1058 if (err)
1059 goto out_noput;
1061 family = p->info.family;
1062 daddr = &p->info.id.daddr;
1064 x = NULL;
1066 mark = xfrm_mark_get(attrs, &m);
1067 if (p->info.seq) {
1068 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1069 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1070 xfrm_state_put(x);
1071 x = NULL;
1075 if (!x)
1076 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1077 p->info.id.proto, daddr,
1078 &p->info.saddr, 1,
1079 family);
1080 err = -ENOENT;
1081 if (x == NULL)
1082 goto out_noput;
1084 err = xfrm_alloc_spi(x, p->min, p->max);
1085 if (err)
1086 goto out;
1088 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1089 if (IS_ERR(resp_skb)) {
1090 err = PTR_ERR(resp_skb);
1091 goto out;
1094 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
1096 out:
1097 xfrm_state_put(x);
1098 out_noput:
1099 return err;
1102 static int verify_policy_dir(u8 dir)
1104 switch (dir) {
1105 case XFRM_POLICY_IN:
1106 case XFRM_POLICY_OUT:
1107 case XFRM_POLICY_FWD:
1108 break;
1110 default:
1111 return -EINVAL;
1114 return 0;
1117 static int verify_policy_type(u8 type)
1119 switch (type) {
1120 case XFRM_POLICY_TYPE_MAIN:
1121 #ifdef CONFIG_XFRM_SUB_POLICY
1122 case XFRM_POLICY_TYPE_SUB:
1123 #endif
1124 break;
1126 default:
1127 return -EINVAL;
1130 return 0;
1133 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1135 switch (p->share) {
1136 case XFRM_SHARE_ANY:
1137 case XFRM_SHARE_SESSION:
1138 case XFRM_SHARE_USER:
1139 case XFRM_SHARE_UNIQUE:
1140 break;
1142 default:
1143 return -EINVAL;
1146 switch (p->action) {
1147 case XFRM_POLICY_ALLOW:
1148 case XFRM_POLICY_BLOCK:
1149 break;
1151 default:
1152 return -EINVAL;
1155 switch (p->sel.family) {
1156 case AF_INET:
1157 break;
1159 case AF_INET6:
1160 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1161 break;
1162 #else
1163 return -EAFNOSUPPORT;
1164 #endif
1166 default:
1167 return -EINVAL;
1170 return verify_policy_dir(p->dir);
1173 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1175 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1176 struct xfrm_user_sec_ctx *uctx;
1178 if (!rt)
1179 return 0;
1181 uctx = nla_data(rt);
1182 return security_xfrm_policy_alloc(&pol->security, uctx);
1185 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1186 int nr)
1188 int i;
1190 xp->xfrm_nr = nr;
1191 for (i = 0; i < nr; i++, ut++) {
1192 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1194 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1195 memcpy(&t->saddr, &ut->saddr,
1196 sizeof(xfrm_address_t));
1197 t->reqid = ut->reqid;
1198 t->mode = ut->mode;
1199 t->share = ut->share;
1200 t->optional = ut->optional;
1201 t->aalgos = ut->aalgos;
1202 t->ealgos = ut->ealgos;
1203 t->calgos = ut->calgos;
1204 /* If all masks are ~0, then we allow all algorithms. */
1205 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1206 t->encap_family = ut->family;
1210 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1212 int i;
1214 if (nr > XFRM_MAX_DEPTH)
1215 return -EINVAL;
1217 for (i = 0; i < nr; i++) {
1218 /* We never validated the ut->family value, so many
1219 * applications simply leave it at zero. The check was
1220 * never made and ut->family was ignored because all
1221 * templates could be assumed to have the same family as
1222 * the policy itself. Now that we will have ipv4-in-ipv6
1223 * and ipv6-in-ipv4 tunnels, this is no longer true.
1225 if (!ut[i].family)
1226 ut[i].family = family;
1228 switch (ut[i].family) {
1229 case AF_INET:
1230 break;
1231 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1232 case AF_INET6:
1233 break;
1234 #endif
1235 default:
1236 return -EINVAL;
1240 return 0;
1243 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1245 struct nlattr *rt = attrs[XFRMA_TMPL];
1247 if (!rt) {
1248 pol->xfrm_nr = 0;
1249 } else {
1250 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1251 int nr = nla_len(rt) / sizeof(*utmpl);
1252 int err;
1254 err = validate_tmpl(nr, utmpl, pol->family);
1255 if (err)
1256 return err;
1258 copy_templates(pol, utmpl, nr);
1260 return 0;
1263 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1265 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1266 struct xfrm_userpolicy_type *upt;
1267 u8 type = XFRM_POLICY_TYPE_MAIN;
1268 int err;
1270 if (rt) {
1271 upt = nla_data(rt);
1272 type = upt->type;
1275 err = verify_policy_type(type);
1276 if (err)
1277 return err;
1279 *tp = type;
1280 return 0;
1283 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1285 xp->priority = p->priority;
1286 xp->index = p->index;
1287 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1288 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1289 xp->action = p->action;
1290 xp->flags = p->flags;
1291 xp->family = p->sel.family;
1292 /* XXX xp->share = p->share; */
1295 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1297 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1298 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1299 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1300 p->priority = xp->priority;
1301 p->index = xp->index;
1302 p->sel.family = xp->family;
1303 p->dir = dir;
1304 p->action = xp->action;
1305 p->flags = xp->flags;
1306 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1309 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1311 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1312 int err;
1314 if (!xp) {
1315 *errp = -ENOMEM;
1316 return NULL;
1319 copy_from_user_policy(xp, p);
1321 err = copy_from_user_policy_type(&xp->type, attrs);
1322 if (err)
1323 goto error;
1325 if (!(err = copy_from_user_tmpl(xp, attrs)))
1326 err = copy_from_user_sec_ctx(xp, attrs);
1327 if (err)
1328 goto error;
1330 xfrm_mark_get(attrs, &xp->mark);
1332 return xp;
1333 error:
1334 *errp = err;
1335 xp->walk.dead = 1;
1336 xfrm_policy_destroy(xp);
1337 return NULL;
1340 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1341 struct nlattr **attrs)
1343 struct net *net = sock_net(skb->sk);
1344 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1345 struct xfrm_policy *xp;
1346 struct km_event c;
1347 int err;
1348 int excl;
1349 uid_t loginuid = audit_get_loginuid(current);
1350 u32 sessionid = audit_get_sessionid(current);
1351 u32 sid;
1353 err = verify_newpolicy_info(p);
1354 if (err)
1355 return err;
1356 err = verify_sec_ctx_len(attrs);
1357 if (err)
1358 return err;
1360 xp = xfrm_policy_construct(net, p, attrs, &err);
1361 if (!xp)
1362 return err;
1364 /* shouldnt excl be based on nlh flags??
1365 * Aha! this is anti-netlink really i.e more pfkey derived
1366 * in netlink excl is a flag and you wouldnt need
1367 * a type XFRM_MSG_UPDPOLICY - JHS */
1368 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1369 err = xfrm_policy_insert(p->dir, xp, excl);
1370 security_task_getsecid(current, &sid);
1371 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
1373 if (err) {
1374 security_xfrm_policy_free(xp->security);
1375 kfree(xp);
1376 return err;
1379 c.event = nlh->nlmsg_type;
1380 c.seq = nlh->nlmsg_seq;
1381 c.pid = nlh->nlmsg_pid;
1382 km_policy_notify(xp, p->dir, &c);
1384 xfrm_pol_put(xp);
1386 return 0;
1389 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1391 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1392 int i;
1394 if (xp->xfrm_nr == 0)
1395 return 0;
1397 for (i = 0; i < xp->xfrm_nr; i++) {
1398 struct xfrm_user_tmpl *up = &vec[i];
1399 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1401 memcpy(&up->id, &kp->id, sizeof(up->id));
1402 up->family = kp->encap_family;
1403 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1404 up->reqid = kp->reqid;
1405 up->mode = kp->mode;
1406 up->share = kp->share;
1407 up->optional = kp->optional;
1408 up->aalgos = kp->aalgos;
1409 up->ealgos = kp->ealgos;
1410 up->calgos = kp->calgos;
1413 return nla_put(skb, XFRMA_TMPL,
1414 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1417 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1419 if (x->security) {
1420 return copy_sec_ctx(x->security, skb);
1422 return 0;
1425 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1427 if (xp->security) {
1428 return copy_sec_ctx(xp->security, skb);
1430 return 0;
1432 static inline size_t userpolicy_type_attrsize(void)
1434 #ifdef CONFIG_XFRM_SUB_POLICY
1435 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1436 #else
1437 return 0;
1438 #endif
1441 #ifdef CONFIG_XFRM_SUB_POLICY
1442 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1444 struct xfrm_userpolicy_type upt = {
1445 .type = type,
1448 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1451 #else
1452 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1454 return 0;
1456 #endif
1458 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1460 struct xfrm_dump_info *sp = ptr;
1461 struct xfrm_userpolicy_info *p;
1462 struct sk_buff *in_skb = sp->in_skb;
1463 struct sk_buff *skb = sp->out_skb;
1464 struct nlmsghdr *nlh;
1466 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1467 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1468 if (nlh == NULL)
1469 return -EMSGSIZE;
1471 p = nlmsg_data(nlh);
1472 copy_to_user_policy(xp, p, dir);
1473 if (copy_to_user_tmpl(xp, skb) < 0)
1474 goto nlmsg_failure;
1475 if (copy_to_user_sec_ctx(xp, skb))
1476 goto nlmsg_failure;
1477 if (copy_to_user_policy_type(xp->type, skb) < 0)
1478 goto nlmsg_failure;
1479 if (xfrm_mark_put(skb, &xp->mark))
1480 goto nla_put_failure;
1482 nlmsg_end(skb, nlh);
1483 return 0;
1485 nla_put_failure:
1486 nlmsg_failure:
1487 nlmsg_cancel(skb, nlh);
1488 return -EMSGSIZE;
1491 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1493 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1495 xfrm_policy_walk_done(walk);
1496 return 0;
1499 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1501 struct net *net = sock_net(skb->sk);
1502 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1503 struct xfrm_dump_info info;
1505 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1506 sizeof(cb->args) - sizeof(cb->args[0]));
1508 info.in_skb = cb->skb;
1509 info.out_skb = skb;
1510 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1511 info.nlmsg_flags = NLM_F_MULTI;
1513 if (!cb->args[0]) {
1514 cb->args[0] = 1;
1515 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1518 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1520 return skb->len;
1523 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1524 struct xfrm_policy *xp,
1525 int dir, u32 seq)
1527 struct xfrm_dump_info info;
1528 struct sk_buff *skb;
1530 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1531 if (!skb)
1532 return ERR_PTR(-ENOMEM);
1534 info.in_skb = in_skb;
1535 info.out_skb = skb;
1536 info.nlmsg_seq = seq;
1537 info.nlmsg_flags = 0;
1539 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1540 kfree_skb(skb);
1541 return NULL;
1544 return skb;
1547 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1548 struct nlattr **attrs)
1550 struct net *net = sock_net(skb->sk);
1551 struct xfrm_policy *xp;
1552 struct xfrm_userpolicy_id *p;
1553 u8 type = XFRM_POLICY_TYPE_MAIN;
1554 int err;
1555 struct km_event c;
1556 int delete;
1557 struct xfrm_mark m;
1558 u32 mark = xfrm_mark_get(attrs, &m);
1560 p = nlmsg_data(nlh);
1561 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1563 err = copy_from_user_policy_type(&type, attrs);
1564 if (err)
1565 return err;
1567 err = verify_policy_dir(p->dir);
1568 if (err)
1569 return err;
1571 if (p->index)
1572 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
1573 else {
1574 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1575 struct xfrm_sec_ctx *ctx;
1577 err = verify_sec_ctx_len(attrs);
1578 if (err)
1579 return err;
1581 ctx = NULL;
1582 if (rt) {
1583 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1585 err = security_xfrm_policy_alloc(&ctx, uctx);
1586 if (err)
1587 return err;
1589 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
1590 ctx, delete, &err);
1591 security_xfrm_policy_free(ctx);
1593 if (xp == NULL)
1594 return -ENOENT;
1596 if (!delete) {
1597 struct sk_buff *resp_skb;
1599 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1600 if (IS_ERR(resp_skb)) {
1601 err = PTR_ERR(resp_skb);
1602 } else {
1603 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1604 NETLINK_CB(skb).pid);
1606 } else {
1607 uid_t loginuid = audit_get_loginuid(current);
1608 u32 sessionid = audit_get_sessionid(current);
1609 u32 sid;
1611 security_task_getsecid(current, &sid);
1612 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1613 sid);
1615 if (err != 0)
1616 goto out;
1618 c.data.byid = p->index;
1619 c.event = nlh->nlmsg_type;
1620 c.seq = nlh->nlmsg_seq;
1621 c.pid = nlh->nlmsg_pid;
1622 km_policy_notify(xp, p->dir, &c);
1625 out:
1626 xfrm_pol_put(xp);
1627 return err;
1630 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1631 struct nlattr **attrs)
1633 struct net *net = sock_net(skb->sk);
1634 struct km_event c;
1635 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1636 struct xfrm_audit audit_info;
1637 int err;
1639 audit_info.loginuid = audit_get_loginuid(current);
1640 audit_info.sessionid = audit_get_sessionid(current);
1641 security_task_getsecid(current, &audit_info.secid);
1642 err = xfrm_state_flush(net, p->proto, &audit_info);
1643 if (err) {
1644 if (err == -ESRCH) /* empty table */
1645 return 0;
1646 return err;
1648 c.data.proto = p->proto;
1649 c.event = nlh->nlmsg_type;
1650 c.seq = nlh->nlmsg_seq;
1651 c.pid = nlh->nlmsg_pid;
1652 c.net = net;
1653 km_state_notify(NULL, &c);
1655 return 0;
1658 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1660 size_t replay_size = x->replay_esn ?
1661 xfrm_replay_state_esn_len(x->replay_esn) :
1662 sizeof(struct xfrm_replay_state);
1664 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1665 + nla_total_size(replay_size)
1666 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1667 + nla_total_size(sizeof(struct xfrm_mark))
1668 + nla_total_size(4) /* XFRM_AE_RTHR */
1669 + nla_total_size(4); /* XFRM_AE_ETHR */
1672 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1674 struct xfrm_aevent_id *id;
1675 struct nlmsghdr *nlh;
1677 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1678 if (nlh == NULL)
1679 return -EMSGSIZE;
1681 id = nlmsg_data(nlh);
1682 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
1683 id->sa_id.spi = x->id.spi;
1684 id->sa_id.family = x->props.family;
1685 id->sa_id.proto = x->id.proto;
1686 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1687 id->reqid = x->props.reqid;
1688 id->flags = c->data.aevent;
1690 if (x->replay_esn)
1691 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1692 xfrm_replay_state_esn_len(x->replay_esn),
1693 x->replay_esn);
1694 else
1695 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1697 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1699 if (id->flags & XFRM_AE_RTHR)
1700 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1702 if (id->flags & XFRM_AE_ETHR)
1703 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1704 x->replay_maxage * 10 / HZ);
1706 if (xfrm_mark_put(skb, &x->mark))
1707 goto nla_put_failure;
1709 return nlmsg_end(skb, nlh);
1711 nla_put_failure:
1712 nlmsg_cancel(skb, nlh);
1713 return -EMSGSIZE;
1716 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1717 struct nlattr **attrs)
1719 struct net *net = sock_net(skb->sk);
1720 struct xfrm_state *x;
1721 struct sk_buff *r_skb;
1722 int err;
1723 struct km_event c;
1724 u32 mark;
1725 struct xfrm_mark m;
1726 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1727 struct xfrm_usersa_id *id = &p->sa_id;
1729 mark = xfrm_mark_get(attrs, &m);
1731 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1732 if (x == NULL)
1733 return -ESRCH;
1735 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1736 if (r_skb == NULL) {
1737 xfrm_state_put(x);
1738 return -ENOMEM;
1742 * XXX: is this lock really needed - none of the other
1743 * gets lock (the concern is things getting updated
1744 * while we are still reading) - jhs
1746 spin_lock_bh(&x->lock);
1747 c.data.aevent = p->flags;
1748 c.seq = nlh->nlmsg_seq;
1749 c.pid = nlh->nlmsg_pid;
1751 if (build_aevent(r_skb, x, &c) < 0)
1752 BUG();
1753 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
1754 spin_unlock_bh(&x->lock);
1755 xfrm_state_put(x);
1756 return err;
1759 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1760 struct nlattr **attrs)
1762 struct net *net = sock_net(skb->sk);
1763 struct xfrm_state *x;
1764 struct km_event c;
1765 int err = - EINVAL;
1766 u32 mark = 0;
1767 struct xfrm_mark m;
1768 struct xfrm_aevent_id *p = nlmsg_data(nlh);
1769 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1770 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
1771 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
1773 if (!lt && !rp && !re)
1774 return err;
1776 /* pedantic mode - thou shalt sayeth replaceth */
1777 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1778 return err;
1780 mark = xfrm_mark_get(attrs, &m);
1782 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1783 if (x == NULL)
1784 return -ESRCH;
1786 if (x->km.state != XFRM_STATE_VALID)
1787 goto out;
1789 err = xfrm_replay_verify_len(x->replay_esn, rp);
1790 if (err)
1791 goto out;
1793 spin_lock_bh(&x->lock);
1794 xfrm_update_ae_params(x, attrs);
1795 spin_unlock_bh(&x->lock);
1797 c.event = nlh->nlmsg_type;
1798 c.seq = nlh->nlmsg_seq;
1799 c.pid = nlh->nlmsg_pid;
1800 c.data.aevent = XFRM_AE_CU;
1801 km_state_notify(x, &c);
1802 err = 0;
1803 out:
1804 xfrm_state_put(x);
1805 return err;
1808 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1809 struct nlattr **attrs)
1811 struct net *net = sock_net(skb->sk);
1812 struct km_event c;
1813 u8 type = XFRM_POLICY_TYPE_MAIN;
1814 int err;
1815 struct xfrm_audit audit_info;
1817 err = copy_from_user_policy_type(&type, attrs);
1818 if (err)
1819 return err;
1821 audit_info.loginuid = audit_get_loginuid(current);
1822 audit_info.sessionid = audit_get_sessionid(current);
1823 security_task_getsecid(current, &audit_info.secid);
1824 err = xfrm_policy_flush(net, type, &audit_info);
1825 if (err) {
1826 if (err == -ESRCH) /* empty table */
1827 return 0;
1828 return err;
1831 c.data.type = type;
1832 c.event = nlh->nlmsg_type;
1833 c.seq = nlh->nlmsg_seq;
1834 c.pid = nlh->nlmsg_pid;
1835 c.net = net;
1836 km_policy_notify(NULL, 0, &c);
1837 return 0;
1840 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1841 struct nlattr **attrs)
1843 struct net *net = sock_net(skb->sk);
1844 struct xfrm_policy *xp;
1845 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
1846 struct xfrm_userpolicy_info *p = &up->pol;
1847 u8 type = XFRM_POLICY_TYPE_MAIN;
1848 int err = -ENOENT;
1849 struct xfrm_mark m;
1850 u32 mark = xfrm_mark_get(attrs, &m);
1852 err = copy_from_user_policy_type(&type, attrs);
1853 if (err)
1854 return err;
1856 err = verify_policy_dir(p->dir);
1857 if (err)
1858 return err;
1860 if (p->index)
1861 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
1862 else {
1863 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1864 struct xfrm_sec_ctx *ctx;
1866 err = verify_sec_ctx_len(attrs);
1867 if (err)
1868 return err;
1870 ctx = NULL;
1871 if (rt) {
1872 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1874 err = security_xfrm_policy_alloc(&ctx, uctx);
1875 if (err)
1876 return err;
1878 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
1879 &p->sel, ctx, 0, &err);
1880 security_xfrm_policy_free(ctx);
1882 if (xp == NULL)
1883 return -ENOENT;
1885 if (unlikely(xp->walk.dead))
1886 goto out;
1888 err = 0;
1889 if (up->hard) {
1890 uid_t loginuid = audit_get_loginuid(current);
1891 u32 sessionid = audit_get_sessionid(current);
1892 u32 sid;
1894 security_task_getsecid(current, &sid);
1895 xfrm_policy_delete(xp, p->dir);
1896 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
1898 } else {
1899 // reset the timers here?
1900 WARN(1, "Dont know what to do with soft policy expire\n");
1902 km_policy_expired(xp, p->dir, up->hard, current->pid);
1904 out:
1905 xfrm_pol_put(xp);
1906 return err;
1909 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
1910 struct nlattr **attrs)
1912 struct net *net = sock_net(skb->sk);
1913 struct xfrm_state *x;
1914 int err;
1915 struct xfrm_user_expire *ue = nlmsg_data(nlh);
1916 struct xfrm_usersa_info *p = &ue->state;
1917 struct xfrm_mark m;
1918 u32 mark = xfrm_mark_get(attrs, &m);
1920 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
1922 err = -ENOENT;
1923 if (x == NULL)
1924 return err;
1926 spin_lock_bh(&x->lock);
1927 err = -EINVAL;
1928 if (x->km.state != XFRM_STATE_VALID)
1929 goto out;
1930 km_state_expired(x, ue->hard, current->pid);
1932 if (ue->hard) {
1933 uid_t loginuid = audit_get_loginuid(current);
1934 u32 sessionid = audit_get_sessionid(current);
1935 u32 sid;
1937 security_task_getsecid(current, &sid);
1938 __xfrm_state_delete(x);
1939 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
1941 err = 0;
1942 out:
1943 spin_unlock_bh(&x->lock);
1944 xfrm_state_put(x);
1945 return err;
1948 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
1949 struct nlattr **attrs)
1951 struct net *net = sock_net(skb->sk);
1952 struct xfrm_policy *xp;
1953 struct xfrm_user_tmpl *ut;
1954 int i;
1955 struct nlattr *rt = attrs[XFRMA_TMPL];
1956 struct xfrm_mark mark;
1958 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
1959 struct xfrm_state *x = xfrm_state_alloc(net);
1960 int err = -ENOMEM;
1962 if (!x)
1963 goto nomem;
1965 xfrm_mark_get(attrs, &mark);
1967 err = verify_newpolicy_info(&ua->policy);
1968 if (err)
1969 goto bad_policy;
1971 /* build an XP */
1972 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
1973 if (!xp)
1974 goto free_state;
1976 memcpy(&x->id, &ua->id, sizeof(ua->id));
1977 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1978 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1979 xp->mark.m = x->mark.m = mark.m;
1980 xp->mark.v = x->mark.v = mark.v;
1981 ut = nla_data(rt);
1982 /* extract the templates and for each call km_key */
1983 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1984 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1985 memcpy(&x->id, &t->id, sizeof(x->id));
1986 x->props.mode = t->mode;
1987 x->props.reqid = t->reqid;
1988 x->props.family = ut->family;
1989 t->aalgos = ua->aalgos;
1990 t->ealgos = ua->ealgos;
1991 t->calgos = ua->calgos;
1992 err = km_query(x, t, xp);
1996 kfree(x);
1997 kfree(xp);
1999 return 0;
2001 bad_policy:
2002 WARN(1, "BAD policy passed\n");
2003 free_state:
2004 kfree(x);
2005 nomem:
2006 return err;
2009 #ifdef CONFIG_XFRM_MIGRATE
2010 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2011 struct xfrm_kmaddress *k,
2012 struct nlattr **attrs, int *num)
2014 struct nlattr *rt = attrs[XFRMA_MIGRATE];
2015 struct xfrm_user_migrate *um;
2016 int i, num_migrate;
2018 if (k != NULL) {
2019 struct xfrm_user_kmaddress *uk;
2021 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2022 memcpy(&k->local, &uk->local, sizeof(k->local));
2023 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2024 k->family = uk->family;
2025 k->reserved = uk->reserved;
2028 um = nla_data(rt);
2029 num_migrate = nla_len(rt) / sizeof(*um);
2031 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2032 return -EINVAL;
2034 for (i = 0; i < num_migrate; i++, um++, ma++) {
2035 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2036 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2037 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2038 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2040 ma->proto = um->proto;
2041 ma->mode = um->mode;
2042 ma->reqid = um->reqid;
2044 ma->old_family = um->old_family;
2045 ma->new_family = um->new_family;
2048 *num = i;
2049 return 0;
2052 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2053 struct nlattr **attrs)
2055 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2056 struct xfrm_migrate m[XFRM_MAX_DEPTH];
2057 struct xfrm_kmaddress km, *kmp;
2058 u8 type;
2059 int err;
2060 int n = 0;
2062 if (attrs[XFRMA_MIGRATE] == NULL)
2063 return -EINVAL;
2065 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2067 err = copy_from_user_policy_type(&type, attrs);
2068 if (err)
2069 return err;
2071 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2072 if (err)
2073 return err;
2075 if (!n)
2076 return 0;
2078 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
2080 return 0;
2082 #else
2083 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2084 struct nlattr **attrs)
2086 return -ENOPROTOOPT;
2088 #endif
2090 #ifdef CONFIG_XFRM_MIGRATE
2091 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2093 struct xfrm_user_migrate um;
2095 memset(&um, 0, sizeof(um));
2096 um.proto = m->proto;
2097 um.mode = m->mode;
2098 um.reqid = m->reqid;
2099 um.old_family = m->old_family;
2100 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2101 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2102 um.new_family = m->new_family;
2103 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2104 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2106 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2109 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2111 struct xfrm_user_kmaddress uk;
2113 memset(&uk, 0, sizeof(uk));
2114 uk.family = k->family;
2115 uk.reserved = k->reserved;
2116 memcpy(&uk.local, &k->local, sizeof(uk.local));
2117 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2119 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2122 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
2124 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2125 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2126 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2127 + userpolicy_type_attrsize();
2130 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2131 int num_migrate, const struct xfrm_kmaddress *k,
2132 const struct xfrm_selector *sel, u8 dir, u8 type)
2134 const struct xfrm_migrate *mp;
2135 struct xfrm_userpolicy_id *pol_id;
2136 struct nlmsghdr *nlh;
2137 int i;
2139 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2140 if (nlh == NULL)
2141 return -EMSGSIZE;
2143 pol_id = nlmsg_data(nlh);
2144 /* copy data from selector, dir, and type to the pol_id */
2145 memset(pol_id, 0, sizeof(*pol_id));
2146 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2147 pol_id->dir = dir;
2149 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2150 goto nlmsg_failure;
2152 if (copy_to_user_policy_type(type, skb) < 0)
2153 goto nlmsg_failure;
2155 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2156 if (copy_to_user_migrate(mp, skb) < 0)
2157 goto nlmsg_failure;
2160 return nlmsg_end(skb, nlh);
2161 nlmsg_failure:
2162 nlmsg_cancel(skb, nlh);
2163 return -EMSGSIZE;
2166 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2167 const struct xfrm_migrate *m, int num_migrate,
2168 const struct xfrm_kmaddress *k)
2170 struct net *net = &init_net;
2171 struct sk_buff *skb;
2173 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
2174 if (skb == NULL)
2175 return -ENOMEM;
2177 /* build migrate */
2178 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
2179 BUG();
2181 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
2183 #else
2184 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2185 const struct xfrm_migrate *m, int num_migrate,
2186 const struct xfrm_kmaddress *k)
2188 return -ENOPROTOOPT;
2190 #endif
2192 #define XMSGSIZE(type) sizeof(struct type)
2194 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2195 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2196 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2197 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2198 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2199 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2200 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2201 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2202 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2203 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2204 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2205 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2206 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2207 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2208 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2209 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2210 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2211 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2212 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2213 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2214 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
2217 #undef XMSGSIZE
2219 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2220 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2221 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2222 [XFRMA_LASTUSED] = { .type = NLA_U64},
2223 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
2224 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
2225 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2226 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2227 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2228 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2229 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2230 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2231 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2232 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2233 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2234 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2235 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2236 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2237 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2238 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
2239 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
2240 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
2241 [XFRMA_TFCPAD] = { .type = NLA_U32 },
2242 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
2245 static struct xfrm_link {
2246 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2247 int (*dump)(struct sk_buff *, struct netlink_callback *);
2248 int (*done)(struct netlink_callback *);
2249 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2250 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2251 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2252 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2253 .dump = xfrm_dump_sa,
2254 .done = xfrm_dump_sa_done },
2255 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2256 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2257 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2258 .dump = xfrm_dump_policy,
2259 .done = xfrm_dump_policy_done },
2260 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2261 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
2262 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2263 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2264 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2265 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2266 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2267 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
2268 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2269 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
2270 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
2271 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
2272 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
2275 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2277 struct net *net = sock_net(skb->sk);
2278 struct nlattr *attrs[XFRMA_MAX+1];
2279 struct xfrm_link *link;
2280 int type, err;
2282 type = nlh->nlmsg_type;
2283 if (type > XFRM_MSG_MAX)
2284 return -EINVAL;
2286 type -= XFRM_MSG_BASE;
2287 link = &xfrm_dispatch[type];
2289 /* All operations require privileges, even GET */
2290 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2291 return -EPERM;
2293 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2294 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2295 (nlh->nlmsg_flags & NLM_F_DUMP)) {
2296 if (link->dump == NULL)
2297 return -EINVAL;
2299 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
2302 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
2303 xfrma_policy);
2304 if (err < 0)
2305 return err;
2307 if (link->doit == NULL)
2308 return -EINVAL;
2310 return link->doit(skb, nlh, attrs);
2313 static void xfrm_netlink_rcv(struct sk_buff *skb)
2315 mutex_lock(&xfrm_cfg_mutex);
2316 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2317 mutex_unlock(&xfrm_cfg_mutex);
2320 static inline size_t xfrm_expire_msgsize(void)
2322 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2323 + nla_total_size(sizeof(struct xfrm_mark));
2326 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2328 struct xfrm_user_expire *ue;
2329 struct nlmsghdr *nlh;
2331 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2332 if (nlh == NULL)
2333 return -EMSGSIZE;
2335 ue = nlmsg_data(nlh);
2336 copy_to_user_state(x, &ue->state);
2337 ue->hard = (c->data.hard != 0) ? 1 : 0;
2339 if (xfrm_mark_put(skb, &x->mark))
2340 goto nla_put_failure;
2342 return nlmsg_end(skb, nlh);
2344 nla_put_failure:
2345 return -EMSGSIZE;
2348 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2350 struct net *net = xs_net(x);
2351 struct sk_buff *skb;
2353 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2354 if (skb == NULL)
2355 return -ENOMEM;
2357 if (build_expire(skb, x, c) < 0) {
2358 kfree_skb(skb);
2359 return -EMSGSIZE;
2362 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2365 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2367 struct net *net = xs_net(x);
2368 struct sk_buff *skb;
2370 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2371 if (skb == NULL)
2372 return -ENOMEM;
2374 if (build_aevent(skb, x, c) < 0)
2375 BUG();
2377 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
2380 static int xfrm_notify_sa_flush(const struct km_event *c)
2382 struct net *net = c->net;
2383 struct xfrm_usersa_flush *p;
2384 struct nlmsghdr *nlh;
2385 struct sk_buff *skb;
2386 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2388 skb = nlmsg_new(len, GFP_ATOMIC);
2389 if (skb == NULL)
2390 return -ENOMEM;
2392 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2393 if (nlh == NULL) {
2394 kfree_skb(skb);
2395 return -EMSGSIZE;
2398 p = nlmsg_data(nlh);
2399 p->proto = c->data.proto;
2401 nlmsg_end(skb, nlh);
2403 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2406 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2408 size_t l = 0;
2409 if (x->aead)
2410 l += nla_total_size(aead_len(x->aead));
2411 if (x->aalg) {
2412 l += nla_total_size(sizeof(struct xfrm_algo) +
2413 (x->aalg->alg_key_len + 7) / 8);
2414 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2416 if (x->ealg)
2417 l += nla_total_size(xfrm_alg_len(x->ealg));
2418 if (x->calg)
2419 l += nla_total_size(sizeof(*x->calg));
2420 if (x->encap)
2421 l += nla_total_size(sizeof(*x->encap));
2422 if (x->tfcpad)
2423 l += nla_total_size(sizeof(x->tfcpad));
2424 if (x->replay_esn)
2425 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2426 if (x->security)
2427 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2428 x->security->ctx_len);
2429 if (x->coaddr)
2430 l += nla_total_size(sizeof(*x->coaddr));
2432 /* Must count x->lastused as it may become non-zero behind our back. */
2433 l += nla_total_size(sizeof(u64));
2435 return l;
2438 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2440 struct net *net = xs_net(x);
2441 struct xfrm_usersa_info *p;
2442 struct xfrm_usersa_id *id;
2443 struct nlmsghdr *nlh;
2444 struct sk_buff *skb;
2445 int len = xfrm_sa_len(x);
2446 int headlen;
2448 headlen = sizeof(*p);
2449 if (c->event == XFRM_MSG_DELSA) {
2450 len += nla_total_size(headlen);
2451 headlen = sizeof(*id);
2452 len += nla_total_size(sizeof(struct xfrm_mark));
2454 len += NLMSG_ALIGN(headlen);
2456 skb = nlmsg_new(len, GFP_ATOMIC);
2457 if (skb == NULL)
2458 return -ENOMEM;
2460 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2461 if (nlh == NULL)
2462 goto nla_put_failure;
2464 p = nlmsg_data(nlh);
2465 if (c->event == XFRM_MSG_DELSA) {
2466 struct nlattr *attr;
2468 id = nlmsg_data(nlh);
2469 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2470 id->spi = x->id.spi;
2471 id->family = x->props.family;
2472 id->proto = x->id.proto;
2474 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2475 if (attr == NULL)
2476 goto nla_put_failure;
2478 p = nla_data(attr);
2481 if (copy_to_user_state_extra(x, p, skb))
2482 goto nla_put_failure;
2484 nlmsg_end(skb, nlh);
2486 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
2488 nla_put_failure:
2489 /* Somebody screwed up with xfrm_sa_len! */
2490 WARN_ON(1);
2491 kfree_skb(skb);
2492 return -1;
2495 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2498 switch (c->event) {
2499 case XFRM_MSG_EXPIRE:
2500 return xfrm_exp_state_notify(x, c);
2501 case XFRM_MSG_NEWAE:
2502 return xfrm_aevent_state_notify(x, c);
2503 case XFRM_MSG_DELSA:
2504 case XFRM_MSG_UPDSA:
2505 case XFRM_MSG_NEWSA:
2506 return xfrm_notify_sa(x, c);
2507 case XFRM_MSG_FLUSHSA:
2508 return xfrm_notify_sa_flush(c);
2509 default:
2510 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2511 c->event);
2512 break;
2515 return 0;
2519 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2520 struct xfrm_policy *xp)
2522 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2523 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2524 + nla_total_size(sizeof(struct xfrm_mark))
2525 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2526 + userpolicy_type_attrsize();
2529 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2530 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2531 int dir)
2533 struct xfrm_user_acquire *ua;
2534 struct nlmsghdr *nlh;
2535 __u32 seq = xfrm_get_acqseq();
2537 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2538 if (nlh == NULL)
2539 return -EMSGSIZE;
2541 ua = nlmsg_data(nlh);
2542 memcpy(&ua->id, &x->id, sizeof(ua->id));
2543 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2544 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2545 copy_to_user_policy(xp, &ua->policy, dir);
2546 ua->aalgos = xt->aalgos;
2547 ua->ealgos = xt->ealgos;
2548 ua->calgos = xt->calgos;
2549 ua->seq = x->km.seq = seq;
2551 if (copy_to_user_tmpl(xp, skb) < 0)
2552 goto nlmsg_failure;
2553 if (copy_to_user_state_sec_ctx(x, skb))
2554 goto nlmsg_failure;
2555 if (copy_to_user_policy_type(xp->type, skb) < 0)
2556 goto nlmsg_failure;
2557 if (xfrm_mark_put(skb, &xp->mark))
2558 goto nla_put_failure;
2560 return nlmsg_end(skb, nlh);
2562 nla_put_failure:
2563 nlmsg_failure:
2564 nlmsg_cancel(skb, nlh);
2565 return -EMSGSIZE;
2568 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2569 struct xfrm_policy *xp, int dir)
2571 struct net *net = xs_net(x);
2572 struct sk_buff *skb;
2574 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2575 if (skb == NULL)
2576 return -ENOMEM;
2578 if (build_acquire(skb, x, xt, xp, dir) < 0)
2579 BUG();
2581 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
2584 /* User gives us xfrm_user_policy_info followed by an array of 0
2585 * or more templates.
2587 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2588 u8 *data, int len, int *dir)
2590 struct net *net = sock_net(sk);
2591 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2592 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2593 struct xfrm_policy *xp;
2594 int nr;
2596 switch (sk->sk_family) {
2597 case AF_INET:
2598 if (opt != IP_XFRM_POLICY) {
2599 *dir = -EOPNOTSUPP;
2600 return NULL;
2602 break;
2603 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2604 case AF_INET6:
2605 if (opt != IPV6_XFRM_POLICY) {
2606 *dir = -EOPNOTSUPP;
2607 return NULL;
2609 break;
2610 #endif
2611 default:
2612 *dir = -EINVAL;
2613 return NULL;
2616 *dir = -EINVAL;
2618 if (len < sizeof(*p) ||
2619 verify_newpolicy_info(p))
2620 return NULL;
2622 nr = ((len - sizeof(*p)) / sizeof(*ut));
2623 if (validate_tmpl(nr, ut, p->sel.family))
2624 return NULL;
2626 if (p->dir > XFRM_POLICY_OUT)
2627 return NULL;
2629 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2630 if (xp == NULL) {
2631 *dir = -ENOBUFS;
2632 return NULL;
2635 copy_from_user_policy(xp, p);
2636 xp->type = XFRM_POLICY_TYPE_MAIN;
2637 copy_templates(xp, ut, nr);
2639 *dir = p->dir;
2641 return xp;
2644 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2646 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2647 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2648 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2649 + nla_total_size(sizeof(struct xfrm_mark))
2650 + userpolicy_type_attrsize();
2653 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2654 int dir, const struct km_event *c)
2656 struct xfrm_user_polexpire *upe;
2657 struct nlmsghdr *nlh;
2658 int hard = c->data.hard;
2660 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2661 if (nlh == NULL)
2662 return -EMSGSIZE;
2664 upe = nlmsg_data(nlh);
2665 copy_to_user_policy(xp, &upe->pol, dir);
2666 if (copy_to_user_tmpl(xp, skb) < 0)
2667 goto nlmsg_failure;
2668 if (copy_to_user_sec_ctx(xp, skb))
2669 goto nlmsg_failure;
2670 if (copy_to_user_policy_type(xp->type, skb) < 0)
2671 goto nlmsg_failure;
2672 if (xfrm_mark_put(skb, &xp->mark))
2673 goto nla_put_failure;
2674 upe->hard = !!hard;
2676 return nlmsg_end(skb, nlh);
2678 nla_put_failure:
2679 nlmsg_failure:
2680 nlmsg_cancel(skb, nlh);
2681 return -EMSGSIZE;
2684 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2686 struct net *net = xp_net(xp);
2687 struct sk_buff *skb;
2689 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
2690 if (skb == NULL)
2691 return -ENOMEM;
2693 if (build_polexpire(skb, xp, dir, c) < 0)
2694 BUG();
2696 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
2699 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
2701 struct net *net = xp_net(xp);
2702 struct xfrm_userpolicy_info *p;
2703 struct xfrm_userpolicy_id *id;
2704 struct nlmsghdr *nlh;
2705 struct sk_buff *skb;
2706 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
2707 int headlen;
2709 headlen = sizeof(*p);
2710 if (c->event == XFRM_MSG_DELPOLICY) {
2711 len += nla_total_size(headlen);
2712 headlen = sizeof(*id);
2714 len += userpolicy_type_attrsize();
2715 len += nla_total_size(sizeof(struct xfrm_mark));
2716 len += NLMSG_ALIGN(headlen);
2718 skb = nlmsg_new(len, GFP_ATOMIC);
2719 if (skb == NULL)
2720 return -ENOMEM;
2722 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2723 if (nlh == NULL)
2724 goto nlmsg_failure;
2726 p = nlmsg_data(nlh);
2727 if (c->event == XFRM_MSG_DELPOLICY) {
2728 struct nlattr *attr;
2730 id = nlmsg_data(nlh);
2731 memset(id, 0, sizeof(*id));
2732 id->dir = dir;
2733 if (c->data.byid)
2734 id->index = xp->index;
2735 else
2736 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2738 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2739 if (attr == NULL)
2740 goto nlmsg_failure;
2742 p = nla_data(attr);
2745 copy_to_user_policy(xp, p, dir);
2746 if (copy_to_user_tmpl(xp, skb) < 0)
2747 goto nlmsg_failure;
2748 if (copy_to_user_policy_type(xp->type, skb) < 0)
2749 goto nlmsg_failure;
2751 if (xfrm_mark_put(skb, &xp->mark))
2752 goto nla_put_failure;
2754 nlmsg_end(skb, nlh);
2756 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2758 nla_put_failure:
2759 nlmsg_failure:
2760 kfree_skb(skb);
2761 return -1;
2764 static int xfrm_notify_policy_flush(const struct km_event *c)
2766 struct net *net = c->net;
2767 struct nlmsghdr *nlh;
2768 struct sk_buff *skb;
2770 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
2771 if (skb == NULL)
2772 return -ENOMEM;
2774 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2775 if (nlh == NULL)
2776 goto nlmsg_failure;
2777 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2778 goto nlmsg_failure;
2780 nlmsg_end(skb, nlh);
2782 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
2784 nlmsg_failure:
2785 kfree_skb(skb);
2786 return -1;
2789 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2792 switch (c->event) {
2793 case XFRM_MSG_NEWPOLICY:
2794 case XFRM_MSG_UPDPOLICY:
2795 case XFRM_MSG_DELPOLICY:
2796 return xfrm_notify_policy(xp, dir, c);
2797 case XFRM_MSG_FLUSHPOLICY:
2798 return xfrm_notify_policy_flush(c);
2799 case XFRM_MSG_POLEXPIRE:
2800 return xfrm_exp_policy_notify(xp, dir, c);
2801 default:
2802 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2803 c->event);
2806 return 0;
2810 static inline size_t xfrm_report_msgsize(void)
2812 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2815 static int build_report(struct sk_buff *skb, u8 proto,
2816 struct xfrm_selector *sel, xfrm_address_t *addr)
2818 struct xfrm_user_report *ur;
2819 struct nlmsghdr *nlh;
2821 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2822 if (nlh == NULL)
2823 return -EMSGSIZE;
2825 ur = nlmsg_data(nlh);
2826 ur->proto = proto;
2827 memcpy(&ur->sel, sel, sizeof(ur->sel));
2829 if (addr)
2830 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
2832 return nlmsg_end(skb, nlh);
2834 nla_put_failure:
2835 nlmsg_cancel(skb, nlh);
2836 return -EMSGSIZE;
2839 static int xfrm_send_report(struct net *net, u8 proto,
2840 struct xfrm_selector *sel, xfrm_address_t *addr)
2842 struct sk_buff *skb;
2844 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
2845 if (skb == NULL)
2846 return -ENOMEM;
2848 if (build_report(skb, proto, sel, addr) < 0)
2849 BUG();
2851 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
2854 static inline size_t xfrm_mapping_msgsize(void)
2856 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2859 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2860 xfrm_address_t *new_saddr, __be16 new_sport)
2862 struct xfrm_user_mapping *um;
2863 struct nlmsghdr *nlh;
2865 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2866 if (nlh == NULL)
2867 return -EMSGSIZE;
2869 um = nlmsg_data(nlh);
2871 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2872 um->id.spi = x->id.spi;
2873 um->id.family = x->props.family;
2874 um->id.proto = x->id.proto;
2875 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2876 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2877 um->new_sport = new_sport;
2878 um->old_sport = x->encap->encap_sport;
2879 um->reqid = x->props.reqid;
2881 return nlmsg_end(skb, nlh);
2884 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2885 __be16 sport)
2887 struct net *net = xs_net(x);
2888 struct sk_buff *skb;
2890 if (x->id.proto != IPPROTO_ESP)
2891 return -EINVAL;
2893 if (!x->encap)
2894 return -EINVAL;
2896 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2897 if (skb == NULL)
2898 return -ENOMEM;
2900 if (build_mapping(skb, x, ipaddr, sport) < 0)
2901 BUG();
2903 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
2906 static struct xfrm_mgr netlink_mgr = {
2907 .id = "netlink",
2908 .notify = xfrm_send_state_notify,
2909 .acquire = xfrm_send_acquire,
2910 .compile_policy = xfrm_compile_policy,
2911 .notify_policy = xfrm_send_policy_notify,
2912 .report = xfrm_send_report,
2913 .migrate = xfrm_send_migrate,
2914 .new_mapping = xfrm_send_mapping,
2917 static int __net_init xfrm_user_net_init(struct net *net)
2919 struct sock *nlsk;
2921 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
2922 xfrm_netlink_rcv, NULL, THIS_MODULE);
2923 if (nlsk == NULL)
2924 return -ENOMEM;
2925 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
2926 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
2927 return 0;
2930 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
2932 struct net *net;
2933 list_for_each_entry(net, net_exit_list, exit_list)
2934 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2935 synchronize_net();
2936 list_for_each_entry(net, net_exit_list, exit_list)
2937 netlink_kernel_release(net->xfrm.nlsk_stash);
2940 static struct pernet_operations xfrm_user_net_ops = {
2941 .init = xfrm_user_net_init,
2942 .exit_batch = xfrm_user_net_exit,
2945 static int __init xfrm_user_init(void)
2947 int rv;
2949 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2951 rv = register_pernet_subsys(&xfrm_user_net_ops);
2952 if (rv < 0)
2953 return rv;
2954 rv = xfrm_register_km(&netlink_mgr);
2955 if (rv < 0)
2956 unregister_pernet_subsys(&xfrm_user_net_ops);
2957 return rv;
2960 static void __exit xfrm_user_exit(void)
2962 xfrm_unregister_km(&netlink_mgr);
2963 unregister_pernet_subsys(&xfrm_user_net_ops);
2966 module_init(xfrm_user_init);
2967 module_exit(xfrm_user_exit);
2968 MODULE_LICENSE("GPL");
2969 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);