xref: /openbmc/linux/net/xfrm/xfrm_user.c (revision 87c2ce3b)
1 /* xfrm_user.c: User interface to configure xfrm engine.
2  *
3  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4  *
5  * Changes:
6  *	Mitsuru KANDA @USAGI
7  * 	Kazunori MIYAZAWA @USAGI
8  * 	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9  * 		IPv6 support
10  *
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/socket.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/skbuff.h>
21 #include <linux/rtnetlink.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 <asm/uaccess.h>
30 
31 static struct sock *xfrm_nl;
32 
33 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
34 {
35 	struct rtattr *rt = xfrma[type - 1];
36 	struct xfrm_algo *algp;
37 	int len;
38 
39 	if (!rt)
40 		return 0;
41 
42 	len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
43 	if (len < 0)
44 		return -EINVAL;
45 
46 	algp = RTA_DATA(rt);
47 
48 	len -= (algp->alg_key_len + 7U) / 8;
49 	if (len < 0)
50 		return -EINVAL;
51 
52 	switch (type) {
53 	case XFRMA_ALG_AUTH:
54 		if (!algp->alg_key_len &&
55 		    strcmp(algp->alg_name, "digest_null") != 0)
56 			return -EINVAL;
57 		break;
58 
59 	case XFRMA_ALG_CRYPT:
60 		if (!algp->alg_key_len &&
61 		    strcmp(algp->alg_name, "cipher_null") != 0)
62 			return -EINVAL;
63 		break;
64 
65 	case XFRMA_ALG_COMP:
66 		/* Zero length keys are legal.  */
67 		break;
68 
69 	default:
70 		return -EINVAL;
71 	};
72 
73 	algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 	return 0;
75 }
76 
77 static int verify_encap_tmpl(struct rtattr **xfrma)
78 {
79 	struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
80 	struct xfrm_encap_tmpl *encap;
81 
82 	if (!rt)
83 		return 0;
84 
85 	if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
86 		return -EINVAL;
87 
88 	return 0;
89 }
90 
91 
92 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
93 {
94 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
95 	struct xfrm_user_sec_ctx *uctx;
96 	int len = 0;
97 
98 	if (!rt)
99 		return 0;
100 
101 	if (rt->rta_len < sizeof(*uctx))
102 		return -EINVAL;
103 
104 	uctx = RTA_DATA(rt);
105 
106 	if (uctx->ctx_len > PAGE_SIZE)
107 		return -EINVAL;
108 
109 	len += sizeof(struct xfrm_user_sec_ctx);
110 	len += uctx->ctx_len;
111 
112 	if (uctx->len != len)
113 		return -EINVAL;
114 
115 	return 0;
116 }
117 
118 
119 static int verify_newsa_info(struct xfrm_usersa_info *p,
120 			     struct rtattr **xfrma)
121 {
122 	int err;
123 
124 	err = -EINVAL;
125 	switch (p->family) {
126 	case AF_INET:
127 		break;
128 
129 	case AF_INET6:
130 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
131 		break;
132 #else
133 		err = -EAFNOSUPPORT;
134 		goto out;
135 #endif
136 
137 	default:
138 		goto out;
139 	};
140 
141 	err = -EINVAL;
142 	switch (p->id.proto) {
143 	case IPPROTO_AH:
144 		if (!xfrma[XFRMA_ALG_AUTH-1]	||
145 		    xfrma[XFRMA_ALG_CRYPT-1]	||
146 		    xfrma[XFRMA_ALG_COMP-1])
147 			goto out;
148 		break;
149 
150 	case IPPROTO_ESP:
151 		if ((!xfrma[XFRMA_ALG_AUTH-1] &&
152 		     !xfrma[XFRMA_ALG_CRYPT-1])	||
153 		    xfrma[XFRMA_ALG_COMP-1])
154 			goto out;
155 		break;
156 
157 	case IPPROTO_COMP:
158 		if (!xfrma[XFRMA_ALG_COMP-1]	||
159 		    xfrma[XFRMA_ALG_AUTH-1]	||
160 		    xfrma[XFRMA_ALG_CRYPT-1])
161 			goto out;
162 		break;
163 
164 	default:
165 		goto out;
166 	};
167 
168 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
169 		goto out;
170 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
171 		goto out;
172 	if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
173 		goto out;
174 	if ((err = verify_encap_tmpl(xfrma)))
175 		goto out;
176 	if ((err = verify_sec_ctx_len(xfrma)))
177 		goto out;
178 
179 	err = -EINVAL;
180 	switch (p->mode) {
181 	case 0:
182 	case 1:
183 		break;
184 
185 	default:
186 		goto out;
187 	};
188 
189 	err = 0;
190 
191 out:
192 	return err;
193 }
194 
195 static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
196 			   struct xfrm_algo_desc *(*get_byname)(char *, int),
197 			   struct rtattr *u_arg)
198 {
199 	struct rtattr *rta = u_arg;
200 	struct xfrm_algo *p, *ualg;
201 	struct xfrm_algo_desc *algo;
202 	int len;
203 
204 	if (!rta)
205 		return 0;
206 
207 	ualg = RTA_DATA(rta);
208 
209 	algo = get_byname(ualg->alg_name, 1);
210 	if (!algo)
211 		return -ENOSYS;
212 	*props = algo->desc.sadb_alg_id;
213 
214 	len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
215 	p = kmalloc(len, GFP_KERNEL);
216 	if (!p)
217 		return -ENOMEM;
218 
219 	memcpy(p, ualg, len);
220 	*algpp = p;
221 	return 0;
222 }
223 
224 static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
225 {
226 	struct rtattr *rta = u_arg;
227 	struct xfrm_encap_tmpl *p, *uencap;
228 
229 	if (!rta)
230 		return 0;
231 
232 	uencap = RTA_DATA(rta);
233 	p = kmalloc(sizeof(*p), GFP_KERNEL);
234 	if (!p)
235 		return -ENOMEM;
236 
237 	memcpy(p, uencap, sizeof(*p));
238 	*encapp = p;
239 	return 0;
240 }
241 
242 
243 static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
244 {
245 	struct xfrm_sec_ctx *xfrm_ctx = xp->security;
246 	int len = 0;
247 
248 	if (xfrm_ctx) {
249 		len += sizeof(struct xfrm_user_sec_ctx);
250 		len += xfrm_ctx->ctx_len;
251 	}
252 	return len;
253 }
254 
255 static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
256 {
257 	struct xfrm_user_sec_ctx *uctx;
258 
259 	if (!u_arg)
260 		return 0;
261 
262 	uctx = RTA_DATA(u_arg);
263 	return security_xfrm_state_alloc(x, uctx);
264 }
265 
266 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
267 {
268 	memcpy(&x->id, &p->id, sizeof(x->id));
269 	memcpy(&x->sel, &p->sel, sizeof(x->sel));
270 	memcpy(&x->lft, &p->lft, sizeof(x->lft));
271 	x->props.mode = p->mode;
272 	x->props.replay_window = p->replay_window;
273 	x->props.reqid = p->reqid;
274 	x->props.family = p->family;
275 	x->props.saddr = p->saddr;
276 	x->props.flags = p->flags;
277 }
278 
279 static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
280 					       struct rtattr **xfrma,
281 					       int *errp)
282 {
283 	struct xfrm_state *x = xfrm_state_alloc();
284 	int err = -ENOMEM;
285 
286 	if (!x)
287 		goto error_no_put;
288 
289 	copy_from_user_state(x, p);
290 
291 	if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
292 				   xfrm_aalg_get_byname,
293 				   xfrma[XFRMA_ALG_AUTH-1])))
294 		goto error;
295 	if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
296 				   xfrm_ealg_get_byname,
297 				   xfrma[XFRMA_ALG_CRYPT-1])))
298 		goto error;
299 	if ((err = attach_one_algo(&x->calg, &x->props.calgo,
300 				   xfrm_calg_get_byname,
301 				   xfrma[XFRMA_ALG_COMP-1])))
302 		goto error;
303 	if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
304 		goto error;
305 
306 	err = xfrm_init_state(x);
307 	if (err)
308 		goto error;
309 
310 	if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
311 		goto error;
312 
313 	x->km.seq = p->seq;
314 
315 	return x;
316 
317 error:
318 	x->km.state = XFRM_STATE_DEAD;
319 	xfrm_state_put(x);
320 error_no_put:
321 	*errp = err;
322 	return NULL;
323 }
324 
325 static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
326 {
327 	struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
328 	struct xfrm_state *x;
329 	int err;
330 	struct km_event c;
331 
332 	err = verify_newsa_info(p, (struct rtattr **)xfrma);
333 	if (err)
334 		return err;
335 
336 	x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
337 	if (!x)
338 		return err;
339 
340 	xfrm_state_hold(x);
341 	if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
342 		err = xfrm_state_add(x);
343 	else
344 		err = xfrm_state_update(x);
345 
346 	if (err < 0) {
347 		x->km.state = XFRM_STATE_DEAD;
348 		xfrm_state_put(x);
349 		goto out;
350 	}
351 
352 	c.seq = nlh->nlmsg_seq;
353 	c.pid = nlh->nlmsg_pid;
354 	c.event = nlh->nlmsg_type;
355 
356 	km_state_notify(x, &c);
357 out:
358 	xfrm_state_put(x);
359 	return err;
360 }
361 
362 static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
363 {
364 	struct xfrm_state *x;
365 	int err;
366 	struct km_event c;
367 	struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
368 
369 	x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
370 	if (x == NULL)
371 		return -ESRCH;
372 
373 	if (xfrm_state_kern(x)) {
374 		xfrm_state_put(x);
375 		return -EPERM;
376 	}
377 
378 	err = xfrm_state_delete(x);
379 	if (err < 0) {
380 		xfrm_state_put(x);
381 		return err;
382 	}
383 
384 	c.seq = nlh->nlmsg_seq;
385 	c.pid = nlh->nlmsg_pid;
386 	c.event = nlh->nlmsg_type;
387 	km_state_notify(x, &c);
388 	xfrm_state_put(x);
389 
390 	return err;
391 }
392 
393 static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
394 {
395 	memcpy(&p->id, &x->id, sizeof(p->id));
396 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
397 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
398 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
399 	memcpy(&p->stats, &x->stats, sizeof(p->stats));
400 	p->saddr = x->props.saddr;
401 	p->mode = x->props.mode;
402 	p->replay_window = x->props.replay_window;
403 	p->reqid = x->props.reqid;
404 	p->family = x->props.family;
405 	p->flags = x->props.flags;
406 	p->seq = x->km.seq;
407 }
408 
409 struct xfrm_dump_info {
410 	struct sk_buff *in_skb;
411 	struct sk_buff *out_skb;
412 	u32 nlmsg_seq;
413 	u16 nlmsg_flags;
414 	int start_idx;
415 	int this_idx;
416 };
417 
418 static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
419 {
420 	struct xfrm_dump_info *sp = ptr;
421 	struct sk_buff *in_skb = sp->in_skb;
422 	struct sk_buff *skb = sp->out_skb;
423 	struct xfrm_usersa_info *p;
424 	struct nlmsghdr *nlh;
425 	unsigned char *b = skb->tail;
426 
427 	if (sp->this_idx < sp->start_idx)
428 		goto out;
429 
430 	nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
431 			sp->nlmsg_seq,
432 			XFRM_MSG_NEWSA, sizeof(*p));
433 	nlh->nlmsg_flags = sp->nlmsg_flags;
434 
435 	p = NLMSG_DATA(nlh);
436 	copy_to_user_state(x, p);
437 
438 	if (x->aalg)
439 		RTA_PUT(skb, XFRMA_ALG_AUTH,
440 			sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
441 	if (x->ealg)
442 		RTA_PUT(skb, XFRMA_ALG_CRYPT,
443 			sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
444 	if (x->calg)
445 		RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
446 
447 	if (x->encap)
448 		RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
449 
450 	if (x->security) {
451 		int ctx_size = sizeof(struct xfrm_sec_ctx) +
452 				x->security->ctx_len;
453 		struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
454 		struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
455 
456 		uctx->exttype = XFRMA_SEC_CTX;
457 		uctx->len = ctx_size;
458 		uctx->ctx_doi = x->security->ctx_doi;
459 		uctx->ctx_alg = x->security->ctx_alg;
460 		uctx->ctx_len = x->security->ctx_len;
461 		memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
462 	}
463 	nlh->nlmsg_len = skb->tail - b;
464 out:
465 	sp->this_idx++;
466 	return 0;
467 
468 nlmsg_failure:
469 rtattr_failure:
470 	skb_trim(skb, b - skb->data);
471 	return -1;
472 }
473 
474 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
475 {
476 	struct xfrm_dump_info info;
477 
478 	info.in_skb = cb->skb;
479 	info.out_skb = skb;
480 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
481 	info.nlmsg_flags = NLM_F_MULTI;
482 	info.this_idx = 0;
483 	info.start_idx = cb->args[0];
484 	(void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
485 	cb->args[0] = info.this_idx;
486 
487 	return skb->len;
488 }
489 
490 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
491 					  struct xfrm_state *x, u32 seq)
492 {
493 	struct xfrm_dump_info info;
494 	struct sk_buff *skb;
495 
496 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
497 	if (!skb)
498 		return ERR_PTR(-ENOMEM);
499 
500 	NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
501 	info.in_skb = in_skb;
502 	info.out_skb = skb;
503 	info.nlmsg_seq = seq;
504 	info.nlmsg_flags = 0;
505 	info.this_idx = info.start_idx = 0;
506 
507 	if (dump_one_state(x, 0, &info)) {
508 		kfree_skb(skb);
509 		return NULL;
510 	}
511 
512 	return skb;
513 }
514 
515 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
516 {
517 	struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
518 	struct xfrm_state *x;
519 	struct sk_buff *resp_skb;
520 	int err;
521 
522 	x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
523 	err = -ESRCH;
524 	if (x == NULL)
525 		goto out_noput;
526 
527 	resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
528 	if (IS_ERR(resp_skb)) {
529 		err = PTR_ERR(resp_skb);
530 	} else {
531 		err = netlink_unicast(xfrm_nl, resp_skb,
532 				      NETLINK_CB(skb).pid, MSG_DONTWAIT);
533 	}
534 	xfrm_state_put(x);
535 out_noput:
536 	return err;
537 }
538 
539 static int verify_userspi_info(struct xfrm_userspi_info *p)
540 {
541 	switch (p->info.id.proto) {
542 	case IPPROTO_AH:
543 	case IPPROTO_ESP:
544 		break;
545 
546 	case IPPROTO_COMP:
547 		/* IPCOMP spi is 16-bits. */
548 		if (p->max >= 0x10000)
549 			return -EINVAL;
550 		break;
551 
552 	default:
553 		return -EINVAL;
554 	};
555 
556 	if (p->min > p->max)
557 		return -EINVAL;
558 
559 	return 0;
560 }
561 
562 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
563 {
564 	struct xfrm_state *x;
565 	struct xfrm_userspi_info *p;
566 	struct sk_buff *resp_skb;
567 	xfrm_address_t *daddr;
568 	int family;
569 	int err;
570 
571 	p = NLMSG_DATA(nlh);
572 	err = verify_userspi_info(p);
573 	if (err)
574 		goto out_noput;
575 
576 	family = p->info.family;
577 	daddr = &p->info.id.daddr;
578 
579 	x = NULL;
580 	if (p->info.seq) {
581 		x = xfrm_find_acq_byseq(p->info.seq);
582 		if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
583 			xfrm_state_put(x);
584 			x = NULL;
585 		}
586 	}
587 
588 	if (!x)
589 		x = xfrm_find_acq(p->info.mode, p->info.reqid,
590 				  p->info.id.proto, daddr,
591 				  &p->info.saddr, 1,
592 				  family);
593 	err = -ENOENT;
594 	if (x == NULL)
595 		goto out_noput;
596 
597 	resp_skb = ERR_PTR(-ENOENT);
598 
599 	spin_lock_bh(&x->lock);
600 	if (x->km.state != XFRM_STATE_DEAD) {
601 		xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
602 		if (x->id.spi)
603 			resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
604 	}
605 	spin_unlock_bh(&x->lock);
606 
607 	if (IS_ERR(resp_skb)) {
608 		err = PTR_ERR(resp_skb);
609 		goto out;
610 	}
611 
612 	err = netlink_unicast(xfrm_nl, resp_skb,
613 			      NETLINK_CB(skb).pid, MSG_DONTWAIT);
614 
615 out:
616 	xfrm_state_put(x);
617 out_noput:
618 	return err;
619 }
620 
621 static int verify_policy_dir(__u8 dir)
622 {
623 	switch (dir) {
624 	case XFRM_POLICY_IN:
625 	case XFRM_POLICY_OUT:
626 	case XFRM_POLICY_FWD:
627 		break;
628 
629 	default:
630 		return -EINVAL;
631 	};
632 
633 	return 0;
634 }
635 
636 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
637 {
638 	switch (p->share) {
639 	case XFRM_SHARE_ANY:
640 	case XFRM_SHARE_SESSION:
641 	case XFRM_SHARE_USER:
642 	case XFRM_SHARE_UNIQUE:
643 		break;
644 
645 	default:
646 		return -EINVAL;
647 	};
648 
649 	switch (p->action) {
650 	case XFRM_POLICY_ALLOW:
651 	case XFRM_POLICY_BLOCK:
652 		break;
653 
654 	default:
655 		return -EINVAL;
656 	};
657 
658 	switch (p->sel.family) {
659 	case AF_INET:
660 		break;
661 
662 	case AF_INET6:
663 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
664 		break;
665 #else
666 		return  -EAFNOSUPPORT;
667 #endif
668 
669 	default:
670 		return -EINVAL;
671 	};
672 
673 	return verify_policy_dir(p->dir);
674 }
675 
676 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
677 {
678 	struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
679 	struct xfrm_user_sec_ctx *uctx;
680 
681 	if (!rt)
682 		return 0;
683 
684 	uctx = RTA_DATA(rt);
685 	return security_xfrm_policy_alloc(pol, uctx);
686 }
687 
688 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
689 			   int nr)
690 {
691 	int i;
692 
693 	xp->xfrm_nr = nr;
694 	for (i = 0; i < nr; i++, ut++) {
695 		struct xfrm_tmpl *t = &xp->xfrm_vec[i];
696 
697 		memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
698 		memcpy(&t->saddr, &ut->saddr,
699 		       sizeof(xfrm_address_t));
700 		t->reqid = ut->reqid;
701 		t->mode = ut->mode;
702 		t->share = ut->share;
703 		t->optional = ut->optional;
704 		t->aalgos = ut->aalgos;
705 		t->ealgos = ut->ealgos;
706 		t->calgos = ut->calgos;
707 	}
708 }
709 
710 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
711 {
712 	struct rtattr *rt = xfrma[XFRMA_TMPL-1];
713 	struct xfrm_user_tmpl *utmpl;
714 	int nr;
715 
716 	if (!rt) {
717 		pol->xfrm_nr = 0;
718 	} else {
719 		nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
720 
721 		if (nr > XFRM_MAX_DEPTH)
722 			return -EINVAL;
723 
724 		copy_templates(pol, RTA_DATA(rt), nr);
725 	}
726 	return 0;
727 }
728 
729 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
730 {
731 	xp->priority = p->priority;
732 	xp->index = p->index;
733 	memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
734 	memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
735 	xp->action = p->action;
736 	xp->flags = p->flags;
737 	xp->family = p->sel.family;
738 	/* XXX xp->share = p->share; */
739 }
740 
741 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
742 {
743 	memcpy(&p->sel, &xp->selector, sizeof(p->sel));
744 	memcpy(&p->lft, &xp->lft, sizeof(p->lft));
745 	memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
746 	p->priority = xp->priority;
747 	p->index = xp->index;
748 	p->sel.family = xp->family;
749 	p->dir = dir;
750 	p->action = xp->action;
751 	p->flags = xp->flags;
752 	p->share = XFRM_SHARE_ANY; /* XXX xp->share */
753 }
754 
755 static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
756 {
757 	struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
758 	int err;
759 
760 	if (!xp) {
761 		*errp = -ENOMEM;
762 		return NULL;
763 	}
764 
765 	copy_from_user_policy(xp, p);
766 
767 	if (!(err = copy_from_user_tmpl(xp, xfrma)))
768 		err = copy_from_user_sec_ctx(xp, xfrma);
769 
770 	if (err) {
771 		*errp = err;
772 		kfree(xp);
773 		xp = NULL;
774 	}
775 
776 	return xp;
777 }
778 
779 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
780 {
781 	struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
782 	struct xfrm_policy *xp;
783 	struct km_event c;
784 	int err;
785 	int excl;
786 
787 	err = verify_newpolicy_info(p);
788 	if (err)
789 		return err;
790 	err = verify_sec_ctx_len((struct rtattr **)xfrma);
791 	if (err)
792 		return err;
793 
794 	xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
795 	if (!xp)
796 		return err;
797 
798 	/* shouldnt excl be based on nlh flags??
799 	 * Aha! this is anti-netlink really i.e  more pfkey derived
800 	 * in netlink excl is a flag and you wouldnt need
801 	 * a type XFRM_MSG_UPDPOLICY - JHS */
802 	excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
803 	err = xfrm_policy_insert(p->dir, xp, excl);
804 	if (err) {
805 		security_xfrm_policy_free(xp);
806 		kfree(xp);
807 		return err;
808 	}
809 
810 	c.event = nlh->nlmsg_type;
811 	c.seq = nlh->nlmsg_seq;
812 	c.pid = nlh->nlmsg_pid;
813 	km_policy_notify(xp, p->dir, &c);
814 
815 	xfrm_pol_put(xp);
816 
817 	return 0;
818 }
819 
820 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
821 {
822 	struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
823 	int i;
824 
825 	if (xp->xfrm_nr == 0)
826 		return 0;
827 
828 	for (i = 0; i < xp->xfrm_nr; i++) {
829 		struct xfrm_user_tmpl *up = &vec[i];
830 		struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
831 
832 		memcpy(&up->id, &kp->id, sizeof(up->id));
833 		up->family = xp->family;
834 		memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
835 		up->reqid = kp->reqid;
836 		up->mode = kp->mode;
837 		up->share = kp->share;
838 		up->optional = kp->optional;
839 		up->aalgos = kp->aalgos;
840 		up->ealgos = kp->ealgos;
841 		up->calgos = kp->calgos;
842 	}
843 	RTA_PUT(skb, XFRMA_TMPL,
844 		(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
845 		vec);
846 
847 	return 0;
848 
849 rtattr_failure:
850 	return -1;
851 }
852 
853 static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
854 {
855 	if (xp->security) {
856 		int ctx_size = sizeof(struct xfrm_sec_ctx) +
857 				xp->security->ctx_len;
858 		struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
859 		struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
860 
861 		uctx->exttype = XFRMA_SEC_CTX;
862 		uctx->len = ctx_size;
863 		uctx->ctx_doi = xp->security->ctx_doi;
864 		uctx->ctx_alg = xp->security->ctx_alg;
865 		uctx->ctx_len = xp->security->ctx_len;
866 		memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
867 	}
868 	return 0;
869 
870  rtattr_failure:
871 	return -1;
872 }
873 
874 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
875 {
876 	struct xfrm_dump_info *sp = ptr;
877 	struct xfrm_userpolicy_info *p;
878 	struct sk_buff *in_skb = sp->in_skb;
879 	struct sk_buff *skb = sp->out_skb;
880 	struct nlmsghdr *nlh;
881 	unsigned char *b = skb->tail;
882 
883 	if (sp->this_idx < sp->start_idx)
884 		goto out;
885 
886 	nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
887 			sp->nlmsg_seq,
888 			XFRM_MSG_NEWPOLICY, sizeof(*p));
889 	p = NLMSG_DATA(nlh);
890 	nlh->nlmsg_flags = sp->nlmsg_flags;
891 
892 	copy_to_user_policy(xp, p, dir);
893 	if (copy_to_user_tmpl(xp, skb) < 0)
894 		goto nlmsg_failure;
895 	if (copy_to_user_sec_ctx(xp, skb))
896 		goto nlmsg_failure;
897 
898 	nlh->nlmsg_len = skb->tail - b;
899 out:
900 	sp->this_idx++;
901 	return 0;
902 
903 nlmsg_failure:
904 	skb_trim(skb, b - skb->data);
905 	return -1;
906 }
907 
908 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
909 {
910 	struct xfrm_dump_info info;
911 
912 	info.in_skb = cb->skb;
913 	info.out_skb = skb;
914 	info.nlmsg_seq = cb->nlh->nlmsg_seq;
915 	info.nlmsg_flags = NLM_F_MULTI;
916 	info.this_idx = 0;
917 	info.start_idx = cb->args[0];
918 	(void) xfrm_policy_walk(dump_one_policy, &info);
919 	cb->args[0] = info.this_idx;
920 
921 	return skb->len;
922 }
923 
924 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
925 					  struct xfrm_policy *xp,
926 					  int dir, u32 seq)
927 {
928 	struct xfrm_dump_info info;
929 	struct sk_buff *skb;
930 
931 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
932 	if (!skb)
933 		return ERR_PTR(-ENOMEM);
934 
935 	NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
936 	info.in_skb = in_skb;
937 	info.out_skb = skb;
938 	info.nlmsg_seq = seq;
939 	info.nlmsg_flags = 0;
940 	info.this_idx = info.start_idx = 0;
941 
942 	if (dump_one_policy(xp, dir, 0, &info) < 0) {
943 		kfree_skb(skb);
944 		return NULL;
945 	}
946 
947 	return skb;
948 }
949 
950 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
951 {
952 	struct xfrm_policy *xp;
953 	struct xfrm_userpolicy_id *p;
954 	int err;
955 	struct km_event c;
956 	int delete;
957 
958 	p = NLMSG_DATA(nlh);
959 	delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
960 
961 	err = verify_policy_dir(p->dir);
962 	if (err)
963 		return err;
964 
965 	if (p->index)
966 		xp = xfrm_policy_byid(p->dir, p->index, delete);
967 	else {
968 		struct rtattr **rtattrs = (struct rtattr **)xfrma;
969 		struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
970 		struct xfrm_policy tmp;
971 
972 		err = verify_sec_ctx_len(rtattrs);
973 		if (err)
974 			return err;
975 
976 		memset(&tmp, 0, sizeof(struct xfrm_policy));
977 		if (rt) {
978 			struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
979 
980 			if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
981 				return err;
982 		}
983 		xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
984 		security_xfrm_policy_free(&tmp);
985 	}
986 	if (xp == NULL)
987 		return -ENOENT;
988 
989 	if (!delete) {
990 		struct sk_buff *resp_skb;
991 
992 		resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
993 		if (IS_ERR(resp_skb)) {
994 			err = PTR_ERR(resp_skb);
995 		} else {
996 			err = netlink_unicast(xfrm_nl, resp_skb,
997 					      NETLINK_CB(skb).pid,
998 					      MSG_DONTWAIT);
999 		}
1000 	} else {
1001 		c.data.byid = p->index;
1002 		c.event = nlh->nlmsg_type;
1003 		c.seq = nlh->nlmsg_seq;
1004 		c.pid = nlh->nlmsg_pid;
1005 		km_policy_notify(xp, p->dir, &c);
1006 	}
1007 
1008 	xfrm_pol_put(xp);
1009 
1010 	return err;
1011 }
1012 
1013 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1014 {
1015 	struct km_event c;
1016 	struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1017 
1018 	xfrm_state_flush(p->proto);
1019 	c.data.proto = p->proto;
1020 	c.event = nlh->nlmsg_type;
1021 	c.seq = nlh->nlmsg_seq;
1022 	c.pid = nlh->nlmsg_pid;
1023 	km_state_notify(NULL, &c);
1024 
1025 	return 0;
1026 }
1027 
1028 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1029 {
1030 	struct km_event c;
1031 
1032 	xfrm_policy_flush();
1033 	c.event = nlh->nlmsg_type;
1034 	c.seq = nlh->nlmsg_seq;
1035 	c.pid = nlh->nlmsg_pid;
1036 	km_policy_notify(NULL, 0, &c);
1037 	return 0;
1038 }
1039 
1040 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1041 
1042 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1043 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1044 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1045 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1046 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1047 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1048 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1049 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
1050 	[XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
1051 	[XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
1052 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1053 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1054 	[XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
1055 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1056 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
1057 };
1058 
1059 #undef XMSGSIZE
1060 
1061 static struct xfrm_link {
1062 	int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1063 	int (*dump)(struct sk_buff *, struct netlink_callback *);
1064 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1065 	[XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1066 	[XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
1067 	[XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1068 						   .dump = xfrm_dump_sa       },
1069 	[XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1070 	[XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
1071 	[XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1072 						   .dump = xfrm_dump_policy   },
1073 	[XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
1074 	[XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
1075 	[XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
1076 	[XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
1077 	[XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
1078 };
1079 
1080 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1081 {
1082 	struct rtattr *xfrma[XFRMA_MAX];
1083 	struct xfrm_link *link;
1084 	int type, min_len;
1085 
1086 	if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1087 		return 0;
1088 
1089 	type = nlh->nlmsg_type;
1090 
1091 	/* A control message: ignore them */
1092 	if (type < XFRM_MSG_BASE)
1093 		return 0;
1094 
1095 	/* Unknown message: reply with EINVAL */
1096 	if (type > XFRM_MSG_MAX)
1097 		goto err_einval;
1098 
1099 	type -= XFRM_MSG_BASE;
1100 	link = &xfrm_dispatch[type];
1101 
1102 	/* All operations require privileges, even GET */
1103 	if (security_netlink_recv(skb)) {
1104 		*errp = -EPERM;
1105 		return -1;
1106 	}
1107 
1108 	if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1109 	     type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1110 	    (nlh->nlmsg_flags & NLM_F_DUMP)) {
1111 		if (link->dump == NULL)
1112 			goto err_einval;
1113 
1114 		if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
1115 						link->dump, NULL)) != 0) {
1116 			return -1;
1117 		}
1118 
1119 		netlink_queue_skip(nlh, skb);
1120 		return -1;
1121 	}
1122 
1123 	memset(xfrma, 0, sizeof(xfrma));
1124 
1125 	if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1126 		goto err_einval;
1127 
1128 	if (nlh->nlmsg_len > min_len) {
1129 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1130 		struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1131 
1132 		while (RTA_OK(attr, attrlen)) {
1133 			unsigned short flavor = attr->rta_type;
1134 			if (flavor) {
1135 				if (flavor > XFRMA_MAX)
1136 					goto err_einval;
1137 				xfrma[flavor - 1] = attr;
1138 			}
1139 			attr = RTA_NEXT(attr, attrlen);
1140 		}
1141 	}
1142 
1143 	if (link->doit == NULL)
1144 		goto err_einval;
1145 	*errp = link->doit(skb, nlh, (void **) &xfrma);
1146 
1147 	return *errp;
1148 
1149 err_einval:
1150 	*errp = -EINVAL;
1151 	return -1;
1152 }
1153 
1154 static void xfrm_netlink_rcv(struct sock *sk, int len)
1155 {
1156 	unsigned int qlen = 0;
1157 
1158 	do {
1159 		down(&xfrm_cfg_sem);
1160 		netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1161 		up(&xfrm_cfg_sem);
1162 
1163 	} while (qlen);
1164 }
1165 
1166 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
1167 {
1168 	struct xfrm_user_expire *ue;
1169 	struct nlmsghdr *nlh;
1170 	unsigned char *b = skb->tail;
1171 
1172 	nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
1173 			sizeof(*ue));
1174 	ue = NLMSG_DATA(nlh);
1175 	nlh->nlmsg_flags = 0;
1176 
1177 	copy_to_user_state(x, &ue->state);
1178 	ue->hard = (hard != 0) ? 1 : 0;
1179 
1180 	nlh->nlmsg_len = skb->tail - b;
1181 	return skb->len;
1182 
1183 nlmsg_failure:
1184 	skb_trim(skb, b - skb->data);
1185 	return -1;
1186 }
1187 
1188 static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1189 {
1190 	struct sk_buff *skb;
1191 	int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1192 
1193 	skb = alloc_skb(len, GFP_ATOMIC);
1194 	if (skb == NULL)
1195 		return -ENOMEM;
1196 
1197 	if (build_expire(skb, x, c->data.hard) < 0)
1198 		BUG();
1199 
1200 	NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1201 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1202 }
1203 
1204 static int xfrm_notify_sa_flush(struct km_event *c)
1205 {
1206 	struct xfrm_usersa_flush *p;
1207 	struct nlmsghdr *nlh;
1208 	struct sk_buff *skb;
1209 	unsigned char *b;
1210 	int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1211 
1212 	skb = alloc_skb(len, GFP_ATOMIC);
1213 	if (skb == NULL)
1214 		return -ENOMEM;
1215 	b = skb->tail;
1216 
1217 	nlh = NLMSG_PUT(skb, c->pid, c->seq,
1218 			XFRM_MSG_FLUSHSA, sizeof(*p));
1219 	nlh->nlmsg_flags = 0;
1220 
1221 	p = NLMSG_DATA(nlh);
1222 	p->proto = c->data.proto;
1223 
1224 	nlh->nlmsg_len = skb->tail - b;
1225 
1226 	NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1227 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1228 
1229 nlmsg_failure:
1230 	kfree_skb(skb);
1231 	return -1;
1232 }
1233 
1234 static int inline xfrm_sa_len(struct xfrm_state *x)
1235 {
1236 	int l = 0;
1237 	if (x->aalg)
1238 		l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1239 	if (x->ealg)
1240 		l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1241 	if (x->calg)
1242 		l += RTA_SPACE(sizeof(*x->calg));
1243 	if (x->encap)
1244 		l += RTA_SPACE(sizeof(*x->encap));
1245 
1246 	return l;
1247 }
1248 
1249 static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1250 {
1251 	struct xfrm_usersa_info *p;
1252 	struct xfrm_usersa_id *id;
1253 	struct nlmsghdr *nlh;
1254 	struct sk_buff *skb;
1255 	unsigned char *b;
1256 	int len = xfrm_sa_len(x);
1257 	int headlen;
1258 
1259 	headlen = sizeof(*p);
1260 	if (c->event == XFRM_MSG_DELSA) {
1261 		len += RTA_SPACE(headlen);
1262 		headlen = sizeof(*id);
1263 	}
1264 	len += NLMSG_SPACE(headlen);
1265 
1266 	skb = alloc_skb(len, GFP_ATOMIC);
1267 	if (skb == NULL)
1268 		return -ENOMEM;
1269 	b = skb->tail;
1270 
1271 	nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1272 	nlh->nlmsg_flags = 0;
1273 
1274 	p = NLMSG_DATA(nlh);
1275 	if (c->event == XFRM_MSG_DELSA) {
1276 		id = NLMSG_DATA(nlh);
1277 		memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1278 		id->spi = x->id.spi;
1279 		id->family = x->props.family;
1280 		id->proto = x->id.proto;
1281 
1282 		p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1283 	}
1284 
1285 	copy_to_user_state(x, p);
1286 
1287 	if (x->aalg)
1288 		RTA_PUT(skb, XFRMA_ALG_AUTH,
1289 			sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1290 	if (x->ealg)
1291 		RTA_PUT(skb, XFRMA_ALG_CRYPT,
1292 			sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1293 	if (x->calg)
1294 		RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1295 
1296 	if (x->encap)
1297 		RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1298 
1299 	nlh->nlmsg_len = skb->tail - b;
1300 
1301 	NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1302 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
1303 
1304 nlmsg_failure:
1305 rtattr_failure:
1306 	kfree_skb(skb);
1307 	return -1;
1308 }
1309 
1310 static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1311 {
1312 
1313 	switch (c->event) {
1314 	case XFRM_MSG_EXPIRE:
1315 		return xfrm_exp_state_notify(x, c);
1316 	case XFRM_MSG_DELSA:
1317 	case XFRM_MSG_UPDSA:
1318 	case XFRM_MSG_NEWSA:
1319 		return xfrm_notify_sa(x, c);
1320 	case XFRM_MSG_FLUSHSA:
1321 		return xfrm_notify_sa_flush(c);
1322 	default:
1323 		 printk("xfrm_user: Unknown SA event %d\n", c->event);
1324 		 break;
1325 	}
1326 
1327 	return 0;
1328 
1329 }
1330 
1331 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1332 			 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1333 			 int dir)
1334 {
1335 	struct xfrm_user_acquire *ua;
1336 	struct nlmsghdr *nlh;
1337 	unsigned char *b = skb->tail;
1338 	__u32 seq = xfrm_get_acqseq();
1339 
1340 	nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1341 			sizeof(*ua));
1342 	ua = NLMSG_DATA(nlh);
1343 	nlh->nlmsg_flags = 0;
1344 
1345 	memcpy(&ua->id, &x->id, sizeof(ua->id));
1346 	memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1347 	memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1348 	copy_to_user_policy(xp, &ua->policy, dir);
1349 	ua->aalgos = xt->aalgos;
1350 	ua->ealgos = xt->ealgos;
1351 	ua->calgos = xt->calgos;
1352 	ua->seq = x->km.seq = seq;
1353 
1354 	if (copy_to_user_tmpl(xp, skb) < 0)
1355 		goto nlmsg_failure;
1356 	if (copy_to_user_sec_ctx(xp, skb))
1357 		goto nlmsg_failure;
1358 
1359 	nlh->nlmsg_len = skb->tail - b;
1360 	return skb->len;
1361 
1362 nlmsg_failure:
1363 	skb_trim(skb, b - skb->data);
1364 	return -1;
1365 }
1366 
1367 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1368 			     struct xfrm_policy *xp, int dir)
1369 {
1370 	struct sk_buff *skb;
1371 	size_t len;
1372 
1373 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1374 	len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
1375 	len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1376 	skb = alloc_skb(len, GFP_ATOMIC);
1377 	if (skb == NULL)
1378 		return -ENOMEM;
1379 
1380 	if (build_acquire(skb, x, xt, xp, dir) < 0)
1381 		BUG();
1382 
1383 	NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1384 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1385 }
1386 
1387 /* User gives us xfrm_user_policy_info followed by an array of 0
1388  * or more templates.
1389  */
1390 static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1391 					       u8 *data, int len, int *dir)
1392 {
1393 	struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1394 	struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1395 	struct xfrm_policy *xp;
1396 	int nr;
1397 
1398 	switch (family) {
1399 	case AF_INET:
1400 		if (opt != IP_XFRM_POLICY) {
1401 			*dir = -EOPNOTSUPP;
1402 			return NULL;
1403 		}
1404 		break;
1405 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1406 	case AF_INET6:
1407 		if (opt != IPV6_XFRM_POLICY) {
1408 			*dir = -EOPNOTSUPP;
1409 			return NULL;
1410 		}
1411 		break;
1412 #endif
1413 	default:
1414 		*dir = -EINVAL;
1415 		return NULL;
1416 	}
1417 
1418 	*dir = -EINVAL;
1419 
1420 	if (len < sizeof(*p) ||
1421 	    verify_newpolicy_info(p))
1422 		return NULL;
1423 
1424 	nr = ((len - sizeof(*p)) / sizeof(*ut));
1425 	if (nr > XFRM_MAX_DEPTH)
1426 		return NULL;
1427 
1428 	if (p->dir > XFRM_POLICY_OUT)
1429 		return NULL;
1430 
1431 	xp = xfrm_policy_alloc(GFP_KERNEL);
1432 	if (xp == NULL) {
1433 		*dir = -ENOBUFS;
1434 		return NULL;
1435 	}
1436 
1437 	copy_from_user_policy(xp, p);
1438 	copy_templates(xp, ut, nr);
1439 
1440 	*dir = p->dir;
1441 
1442 	return xp;
1443 }
1444 
1445 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
1446 			   int dir, int hard)
1447 {
1448 	struct xfrm_user_polexpire *upe;
1449 	struct nlmsghdr *nlh;
1450 	unsigned char *b = skb->tail;
1451 
1452 	nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1453 	upe = NLMSG_DATA(nlh);
1454 	nlh->nlmsg_flags = 0;
1455 
1456 	copy_to_user_policy(xp, &upe->pol, dir);
1457 	if (copy_to_user_tmpl(xp, skb) < 0)
1458 		goto nlmsg_failure;
1459 	if (copy_to_user_sec_ctx(xp, skb))
1460 		goto nlmsg_failure;
1461 	upe->hard = !!hard;
1462 
1463 	nlh->nlmsg_len = skb->tail - b;
1464 	return skb->len;
1465 
1466 nlmsg_failure:
1467 	skb_trim(skb, b - skb->data);
1468 	return -1;
1469 }
1470 
1471 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1472 {
1473 	struct sk_buff *skb;
1474 	size_t len;
1475 
1476 	len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1477 	len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
1478 	len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1479 	skb = alloc_skb(len, GFP_ATOMIC);
1480 	if (skb == NULL)
1481 		return -ENOMEM;
1482 
1483 	if (build_polexpire(skb, xp, dir, c->data.hard) < 0)
1484 		BUG();
1485 
1486 	NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1487 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1488 }
1489 
1490 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1491 {
1492 	struct xfrm_userpolicy_info *p;
1493 	struct xfrm_userpolicy_id *id;
1494 	struct nlmsghdr *nlh;
1495 	struct sk_buff *skb;
1496 	unsigned char *b;
1497 	int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1498 	int headlen;
1499 
1500 	headlen = sizeof(*p);
1501 	if (c->event == XFRM_MSG_DELPOLICY) {
1502 		len += RTA_SPACE(headlen);
1503 		headlen = sizeof(*id);
1504 	}
1505 	len += NLMSG_SPACE(headlen);
1506 
1507 	skb = alloc_skb(len, GFP_ATOMIC);
1508 	if (skb == NULL)
1509 		return -ENOMEM;
1510 	b = skb->tail;
1511 
1512 	nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
1513 
1514 	p = NLMSG_DATA(nlh);
1515 	if (c->event == XFRM_MSG_DELPOLICY) {
1516 		id = NLMSG_DATA(nlh);
1517 		memset(id, 0, sizeof(*id));
1518 		id->dir = dir;
1519 		if (c->data.byid)
1520 			id->index = xp->index;
1521 		else
1522 			memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1523 
1524 		p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1525 	}
1526 
1527 	nlh->nlmsg_flags = 0;
1528 
1529 	copy_to_user_policy(xp, p, dir);
1530 	if (copy_to_user_tmpl(xp, skb) < 0)
1531 		goto nlmsg_failure;
1532 
1533 	nlh->nlmsg_len = skb->tail - b;
1534 
1535 	NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1536 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1537 
1538 nlmsg_failure:
1539 rtattr_failure:
1540 	kfree_skb(skb);
1541 	return -1;
1542 }
1543 
1544 static int xfrm_notify_policy_flush(struct km_event *c)
1545 {
1546 	struct nlmsghdr *nlh;
1547 	struct sk_buff *skb;
1548 	unsigned char *b;
1549 	int len = NLMSG_LENGTH(0);
1550 
1551 	skb = alloc_skb(len, GFP_ATOMIC);
1552 	if (skb == NULL)
1553 		return -ENOMEM;
1554 	b = skb->tail;
1555 
1556 
1557 	nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1558 
1559 	nlh->nlmsg_len = skb->tail - b;
1560 
1561 	NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1562 	return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
1563 
1564 nlmsg_failure:
1565 	kfree_skb(skb);
1566 	return -1;
1567 }
1568 
1569 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1570 {
1571 
1572 	switch (c->event) {
1573 	case XFRM_MSG_NEWPOLICY:
1574 	case XFRM_MSG_UPDPOLICY:
1575 	case XFRM_MSG_DELPOLICY:
1576 		return xfrm_notify_policy(xp, dir, c);
1577 	case XFRM_MSG_FLUSHPOLICY:
1578 		return xfrm_notify_policy_flush(c);
1579 	case XFRM_MSG_POLEXPIRE:
1580 		return xfrm_exp_policy_notify(xp, dir, c);
1581 	default:
1582 		printk("xfrm_user: Unknown Policy event %d\n", c->event);
1583 	}
1584 
1585 	return 0;
1586 
1587 }
1588 
1589 static struct xfrm_mgr netlink_mgr = {
1590 	.id		= "netlink",
1591 	.notify		= xfrm_send_state_notify,
1592 	.acquire	= xfrm_send_acquire,
1593 	.compile_policy	= xfrm_compile_policy,
1594 	.notify_policy	= xfrm_send_policy_notify,
1595 };
1596 
1597 static int __init xfrm_user_init(void)
1598 {
1599 	printk(KERN_INFO "Initializing IPsec netlink socket\n");
1600 
1601 	xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1602 	                                xfrm_netlink_rcv, THIS_MODULE);
1603 	if (xfrm_nl == NULL)
1604 		return -ENOMEM;
1605 
1606 	xfrm_register_km(&netlink_mgr);
1607 
1608 	return 0;
1609 }
1610 
1611 static void __exit xfrm_user_exit(void)
1612 {
1613 	xfrm_unregister_km(&netlink_mgr);
1614 	sock_release(xfrm_nl->sk_socket);
1615 }
1616 
1617 module_init(xfrm_user_init);
1618 module_exit(xfrm_user_exit);
1619 MODULE_LICENSE("GPL");
1620 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
1621