[IPSEC]: Sync series - acquire insert
[linux-2.6-block.git] / net / xfrm / xfrm_user.c
CommitLineData
1da177e4
LT
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
df71837d 10 *
1da177e4
LT
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>
1da177e4
LT
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>
88fc2c84 28#include <net/netlink.h>
1da177e4
LT
29#include <asm/uaccess.h>
30
f8cd5488 31struct sock *xfrm_nl;
1da177e4
LT
32
33static 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;
31c26852 37 int len;
1da177e4
LT
38
39 if (!rt)
40 return 0;
41
31c26852
HX
42 len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
43 if (len < 0)
1da177e4
LT
44 return -EINVAL;
45
46 algp = RTA_DATA(rt);
31c26852
HX
47
48 len -= (algp->alg_key_len + 7U) / 8;
49 if (len < 0)
50 return -EINVAL;
51
1da177e4
LT
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
77static 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
df71837d
TJ
91
92static 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
1da177e4
LT
119static 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;
df71837d
TJ
176 if ((err = verify_sec_ctx_len(xfrma)))
177 goto out;
1da177e4
LT
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
191out:
192 return err;
193}
194
195static 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;
b9e9dead 202 int len;
1da177e4
LT
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
b9e9dead
HX
214 len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
215 p = kmalloc(len, GFP_KERNEL);
1da177e4
LT
216 if (!p)
217 return -ENOMEM;
218
b9e9dead 219 memcpy(p, ualg, len);
1da177e4
LT
220 *algpp = p;
221 return 0;
222}
223
224static 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
df71837d
TJ
242
243static 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
255static 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
1da177e4
LT
266static 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
d51d081d
JHS
279/*
280 * someday when pfkey also has support, we could have the code
281 * somehow made shareable and move it to xfrm_state.c - JHS
282 *
283*/
284static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
285{
286 int err = - EINVAL;
287 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
288 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
289 struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
290 struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
291
292 if (rp) {
293 struct xfrm_replay_state *replay;
294 if (RTA_PAYLOAD(rp) < sizeof(*replay))
295 goto error;
296 replay = RTA_DATA(rp);
297 memcpy(&x->replay, replay, sizeof(*replay));
298 memcpy(&x->preplay, replay, sizeof(*replay));
299 }
300
301 if (lt) {
302 struct xfrm_lifetime_cur *ltime;
303 if (RTA_PAYLOAD(lt) < sizeof(*ltime))
304 goto error;
305 ltime = RTA_DATA(lt);
306 x->curlft.bytes = ltime->bytes;
307 x->curlft.packets = ltime->packets;
308 x->curlft.add_time = ltime->add_time;
309 x->curlft.use_time = ltime->use_time;
310 }
311
312 if (et) {
313 if (RTA_PAYLOAD(et) < sizeof(u32))
314 goto error;
315 x->replay_maxage = *(u32*)RTA_DATA(et);
316 }
317
318 if (rt) {
319 if (RTA_PAYLOAD(rt) < sizeof(u32))
320 goto error;
321 x->replay_maxdiff = *(u32*)RTA_DATA(rt);
322 }
323
324 return 0;
325error:
326 return err;
327}
328
1da177e4
LT
329static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
330 struct rtattr **xfrma,
331 int *errp)
332{
333 struct xfrm_state *x = xfrm_state_alloc();
334 int err = -ENOMEM;
335
336 if (!x)
337 goto error_no_put;
338
339 copy_from_user_state(x, p);
340
341 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
342 xfrm_aalg_get_byname,
343 xfrma[XFRMA_ALG_AUTH-1])))
344 goto error;
345 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
346 xfrm_ealg_get_byname,
347 xfrma[XFRMA_ALG_CRYPT-1])))
348 goto error;
349 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
350 xfrm_calg_get_byname,
351 xfrma[XFRMA_ALG_COMP-1])))
352 goto error;
353 if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
354 goto error;
355
72cb6962 356 err = xfrm_init_state(x);
1da177e4
LT
357 if (err)
358 goto error;
359
df71837d
TJ
360 if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
361 goto error;
362
1da177e4 363 x->km.seq = p->seq;
d51d081d
JHS
364 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
365 /* sysctl_xfrm_aevent_etime is in 100ms units */
366 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
367 x->preplay.bitmap = 0;
368 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
369 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
370
371 /* override default values from above */
372
373 err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
374 if (err < 0)
375 goto error;
1da177e4
LT
376
377 return x;
378
379error:
380 x->km.state = XFRM_STATE_DEAD;
381 xfrm_state_put(x);
382error_no_put:
383 *errp = err;
384 return NULL;
385}
386
387static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
388{
389 struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
390 struct xfrm_state *x;
391 int err;
26b15dad 392 struct km_event c;
1da177e4 393
df71837d 394 err = verify_newsa_info(p, (struct rtattr **)xfrma);
1da177e4
LT
395 if (err)
396 return err;
397
df71837d 398 x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
1da177e4
LT
399 if (!x)
400 return err;
401
26b15dad 402 xfrm_state_hold(x);
1da177e4
LT
403 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
404 err = xfrm_state_add(x);
405 else
406 err = xfrm_state_update(x);
407
408 if (err < 0) {
409 x->km.state = XFRM_STATE_DEAD;
21380b81 410 __xfrm_state_put(x);
7d6dfe1f 411 goto out;
1da177e4
LT
412 }
413
26b15dad
JHS
414 c.seq = nlh->nlmsg_seq;
415 c.pid = nlh->nlmsg_pid;
f60f6b8f 416 c.event = nlh->nlmsg_type;
26b15dad
JHS
417
418 km_state_notify(x, &c);
7d6dfe1f 419out:
26b15dad 420 xfrm_state_put(x);
1da177e4
LT
421 return err;
422}
423
424static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
425{
426 struct xfrm_state *x;
26b15dad
JHS
427 int err;
428 struct km_event c;
1da177e4
LT
429 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
430
431 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
432 if (x == NULL)
433 return -ESRCH;
434
435 if (xfrm_state_kern(x)) {
436 xfrm_state_put(x);
437 return -EPERM;
438 }
439
26b15dad
JHS
440 err = xfrm_state_delete(x);
441 if (err < 0) {
442 xfrm_state_put(x);
443 return err;
444 }
445
446 c.seq = nlh->nlmsg_seq;
447 c.pid = nlh->nlmsg_pid;
f60f6b8f 448 c.event = nlh->nlmsg_type;
26b15dad 449 km_state_notify(x, &c);
1da177e4
LT
450 xfrm_state_put(x);
451
26b15dad 452 return err;
1da177e4
LT
453}
454
455static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
456{
457 memcpy(&p->id, &x->id, sizeof(p->id));
458 memcpy(&p->sel, &x->sel, sizeof(p->sel));
459 memcpy(&p->lft, &x->lft, sizeof(p->lft));
460 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
461 memcpy(&p->stats, &x->stats, sizeof(p->stats));
462 p->saddr = x->props.saddr;
463 p->mode = x->props.mode;
464 p->replay_window = x->props.replay_window;
465 p->reqid = x->props.reqid;
466 p->family = x->props.family;
467 p->flags = x->props.flags;
468 p->seq = x->km.seq;
469}
470
471struct xfrm_dump_info {
472 struct sk_buff *in_skb;
473 struct sk_buff *out_skb;
474 u32 nlmsg_seq;
475 u16 nlmsg_flags;
476 int start_idx;
477 int this_idx;
478};
479
480static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
481{
482 struct xfrm_dump_info *sp = ptr;
483 struct sk_buff *in_skb = sp->in_skb;
484 struct sk_buff *skb = sp->out_skb;
485 struct xfrm_usersa_info *p;
486 struct nlmsghdr *nlh;
487 unsigned char *b = skb->tail;
488
489 if (sp->this_idx < sp->start_idx)
490 goto out;
491
492 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
493 sp->nlmsg_seq,
494 XFRM_MSG_NEWSA, sizeof(*p));
495 nlh->nlmsg_flags = sp->nlmsg_flags;
496
497 p = NLMSG_DATA(nlh);
498 copy_to_user_state(x, p);
499
500 if (x->aalg)
501 RTA_PUT(skb, XFRMA_ALG_AUTH,
502 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
503 if (x->ealg)
504 RTA_PUT(skb, XFRMA_ALG_CRYPT,
505 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
506 if (x->calg)
507 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
508
509 if (x->encap)
510 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
511
df71837d
TJ
512 if (x->security) {
513 int ctx_size = sizeof(struct xfrm_sec_ctx) +
514 x->security->ctx_len;
515 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
516 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
517
518 uctx->exttype = XFRMA_SEC_CTX;
519 uctx->len = ctx_size;
520 uctx->ctx_doi = x->security->ctx_doi;
521 uctx->ctx_alg = x->security->ctx_alg;
522 uctx->ctx_len = x->security->ctx_len;
523 memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
524 }
1da177e4
LT
525 nlh->nlmsg_len = skb->tail - b;
526out:
527 sp->this_idx++;
528 return 0;
529
530nlmsg_failure:
531rtattr_failure:
532 skb_trim(skb, b - skb->data);
533 return -1;
534}
535
536static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
537{
538 struct xfrm_dump_info info;
539
540 info.in_skb = cb->skb;
541 info.out_skb = skb;
542 info.nlmsg_seq = cb->nlh->nlmsg_seq;
543 info.nlmsg_flags = NLM_F_MULTI;
544 info.this_idx = 0;
545 info.start_idx = cb->args[0];
546 (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
547 cb->args[0] = info.this_idx;
548
549 return skb->len;
550}
551
552static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
553 struct xfrm_state *x, u32 seq)
554{
555 struct xfrm_dump_info info;
556 struct sk_buff *skb;
557
558 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
559 if (!skb)
560 return ERR_PTR(-ENOMEM);
561
562 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
563 info.in_skb = in_skb;
564 info.out_skb = skb;
565 info.nlmsg_seq = seq;
566 info.nlmsg_flags = 0;
567 info.this_idx = info.start_idx = 0;
568
569 if (dump_one_state(x, 0, &info)) {
570 kfree_skb(skb);
571 return NULL;
572 }
573
574 return skb;
575}
576
577static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
578{
579 struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
580 struct xfrm_state *x;
581 struct sk_buff *resp_skb;
582 int err;
583
584 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
585 err = -ESRCH;
586 if (x == NULL)
587 goto out_noput;
588
589 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
590 if (IS_ERR(resp_skb)) {
591 err = PTR_ERR(resp_skb);
592 } else {
593 err = netlink_unicast(xfrm_nl, resp_skb,
594 NETLINK_CB(skb).pid, MSG_DONTWAIT);
595 }
596 xfrm_state_put(x);
597out_noput:
598 return err;
599}
600
601static int verify_userspi_info(struct xfrm_userspi_info *p)
602{
603 switch (p->info.id.proto) {
604 case IPPROTO_AH:
605 case IPPROTO_ESP:
606 break;
607
608 case IPPROTO_COMP:
609 /* IPCOMP spi is 16-bits. */
610 if (p->max >= 0x10000)
611 return -EINVAL;
612 break;
613
614 default:
615 return -EINVAL;
616 };
617
618 if (p->min > p->max)
619 return -EINVAL;
620
621 return 0;
622}
623
624static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
625{
626 struct xfrm_state *x;
627 struct xfrm_userspi_info *p;
628 struct sk_buff *resp_skb;
629 xfrm_address_t *daddr;
630 int family;
631 int err;
632
633 p = NLMSG_DATA(nlh);
634 err = verify_userspi_info(p);
635 if (err)
636 goto out_noput;
637
638 family = p->info.family;
639 daddr = &p->info.id.daddr;
640
641 x = NULL;
642 if (p->info.seq) {
643 x = xfrm_find_acq_byseq(p->info.seq);
644 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
645 xfrm_state_put(x);
646 x = NULL;
647 }
648 }
649
650 if (!x)
651 x = xfrm_find_acq(p->info.mode, p->info.reqid,
652 p->info.id.proto, daddr,
653 &p->info.saddr, 1,
654 family);
655 err = -ENOENT;
656 if (x == NULL)
657 goto out_noput;
658
659 resp_skb = ERR_PTR(-ENOENT);
660
661 spin_lock_bh(&x->lock);
662 if (x->km.state != XFRM_STATE_DEAD) {
663 xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
664 if (x->id.spi)
665 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
666 }
667 spin_unlock_bh(&x->lock);
668
669 if (IS_ERR(resp_skb)) {
670 err = PTR_ERR(resp_skb);
671 goto out;
672 }
673
674 err = netlink_unicast(xfrm_nl, resp_skb,
675 NETLINK_CB(skb).pid, MSG_DONTWAIT);
676
677out:
678 xfrm_state_put(x);
679out_noput:
680 return err;
681}
682
683static int verify_policy_dir(__u8 dir)
684{
685 switch (dir) {
686 case XFRM_POLICY_IN:
687 case XFRM_POLICY_OUT:
688 case XFRM_POLICY_FWD:
689 break;
690
691 default:
692 return -EINVAL;
693 };
694
695 return 0;
696}
697
698static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
699{
700 switch (p->share) {
701 case XFRM_SHARE_ANY:
702 case XFRM_SHARE_SESSION:
703 case XFRM_SHARE_USER:
704 case XFRM_SHARE_UNIQUE:
705 break;
706
707 default:
708 return -EINVAL;
709 };
710
711 switch (p->action) {
712 case XFRM_POLICY_ALLOW:
713 case XFRM_POLICY_BLOCK:
714 break;
715
716 default:
717 return -EINVAL;
718 };
719
720 switch (p->sel.family) {
721 case AF_INET:
722 break;
723
724 case AF_INET6:
725#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
726 break;
727#else
728 return -EAFNOSUPPORT;
729#endif
730
731 default:
732 return -EINVAL;
733 };
734
735 return verify_policy_dir(p->dir);
736}
737
df71837d
TJ
738static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
739{
740 struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
741 struct xfrm_user_sec_ctx *uctx;
742
743 if (!rt)
744 return 0;
745
746 uctx = RTA_DATA(rt);
747 return security_xfrm_policy_alloc(pol, uctx);
748}
749
1da177e4
LT
750static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
751 int nr)
752{
753 int i;
754
755 xp->xfrm_nr = nr;
756 for (i = 0; i < nr; i++, ut++) {
757 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
758
759 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
760 memcpy(&t->saddr, &ut->saddr,
761 sizeof(xfrm_address_t));
762 t->reqid = ut->reqid;
763 t->mode = ut->mode;
764 t->share = ut->share;
765 t->optional = ut->optional;
766 t->aalgos = ut->aalgos;
767 t->ealgos = ut->ealgos;
768 t->calgos = ut->calgos;
769 }
770}
771
772static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
773{
774 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
775 struct xfrm_user_tmpl *utmpl;
776 int nr;
777
778 if (!rt) {
779 pol->xfrm_nr = 0;
780 } else {
781 nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
782
783 if (nr > XFRM_MAX_DEPTH)
784 return -EINVAL;
785
786 copy_templates(pol, RTA_DATA(rt), nr);
787 }
788 return 0;
789}
790
791static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
792{
793 xp->priority = p->priority;
794 xp->index = p->index;
795 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
796 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
797 xp->action = p->action;
798 xp->flags = p->flags;
799 xp->family = p->sel.family;
800 /* XXX xp->share = p->share; */
801}
802
803static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
804{
805 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
806 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
807 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
808 p->priority = xp->priority;
809 p->index = xp->index;
810 p->sel.family = xp->family;
811 p->dir = dir;
812 p->action = xp->action;
813 p->flags = xp->flags;
814 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
815}
816
817static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
818{
819 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
820 int err;
821
822 if (!xp) {
823 *errp = -ENOMEM;
824 return NULL;
825 }
826
827 copy_from_user_policy(xp, p);
df71837d
TJ
828
829 if (!(err = copy_from_user_tmpl(xp, xfrma)))
830 err = copy_from_user_sec_ctx(xp, xfrma);
831
1da177e4
LT
832 if (err) {
833 *errp = err;
834 kfree(xp);
835 xp = NULL;
836 }
837
838 return xp;
839}
840
841static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
842{
843 struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
844 struct xfrm_policy *xp;
26b15dad 845 struct km_event c;
1da177e4
LT
846 int err;
847 int excl;
848
849 err = verify_newpolicy_info(p);
df71837d
TJ
850 if (err)
851 return err;
852 err = verify_sec_ctx_len((struct rtattr **)xfrma);
1da177e4
LT
853 if (err)
854 return err;
855
df71837d 856 xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
1da177e4
LT
857 if (!xp)
858 return err;
859
26b15dad
JHS
860 /* shouldnt excl be based on nlh flags??
861 * Aha! this is anti-netlink really i.e more pfkey derived
862 * in netlink excl is a flag and you wouldnt need
863 * a type XFRM_MSG_UPDPOLICY - JHS */
1da177e4
LT
864 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
865 err = xfrm_policy_insert(p->dir, xp, excl);
866 if (err) {
5f8ac64b 867 security_xfrm_policy_free(xp);
1da177e4
LT
868 kfree(xp);
869 return err;
870 }
871
f60f6b8f 872 c.event = nlh->nlmsg_type;
26b15dad
JHS
873 c.seq = nlh->nlmsg_seq;
874 c.pid = nlh->nlmsg_pid;
875 km_policy_notify(xp, p->dir, &c);
876
1da177e4
LT
877 xfrm_pol_put(xp);
878
879 return 0;
880}
881
882static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
883{
884 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
885 int i;
886
887 if (xp->xfrm_nr == 0)
888 return 0;
889
890 for (i = 0; i < xp->xfrm_nr; i++) {
891 struct xfrm_user_tmpl *up = &vec[i];
892 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
893
894 memcpy(&up->id, &kp->id, sizeof(up->id));
895 up->family = xp->family;
896 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
897 up->reqid = kp->reqid;
898 up->mode = kp->mode;
899 up->share = kp->share;
900 up->optional = kp->optional;
901 up->aalgos = kp->aalgos;
902 up->ealgos = kp->ealgos;
903 up->calgos = kp->calgos;
904 }
905 RTA_PUT(skb, XFRMA_TMPL,
906 (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
907 vec);
908
909 return 0;
910
911rtattr_failure:
912 return -1;
913}
914
df71837d
TJ
915static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
916{
917 if (xp->security) {
918 int ctx_size = sizeof(struct xfrm_sec_ctx) +
919 xp->security->ctx_len;
920 struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
921 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
922
923 uctx->exttype = XFRMA_SEC_CTX;
924 uctx->len = ctx_size;
925 uctx->ctx_doi = xp->security->ctx_doi;
926 uctx->ctx_alg = xp->security->ctx_alg;
927 uctx->ctx_len = xp->security->ctx_len;
928 memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
929 }
930 return 0;
931
932 rtattr_failure:
933 return -1;
934}
935
1da177e4
LT
936static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
937{
938 struct xfrm_dump_info *sp = ptr;
939 struct xfrm_userpolicy_info *p;
940 struct sk_buff *in_skb = sp->in_skb;
941 struct sk_buff *skb = sp->out_skb;
942 struct nlmsghdr *nlh;
943 unsigned char *b = skb->tail;
944
945 if (sp->this_idx < sp->start_idx)
946 goto out;
947
948 nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
949 sp->nlmsg_seq,
950 XFRM_MSG_NEWPOLICY, sizeof(*p));
951 p = NLMSG_DATA(nlh);
952 nlh->nlmsg_flags = sp->nlmsg_flags;
953
954 copy_to_user_policy(xp, p, dir);
955 if (copy_to_user_tmpl(xp, skb) < 0)
956 goto nlmsg_failure;
df71837d
TJ
957 if (copy_to_user_sec_ctx(xp, skb))
958 goto nlmsg_failure;
1da177e4
LT
959
960 nlh->nlmsg_len = skb->tail - b;
961out:
962 sp->this_idx++;
963 return 0;
964
965nlmsg_failure:
966 skb_trim(skb, b - skb->data);
967 return -1;
968}
969
970static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
971{
972 struct xfrm_dump_info info;
973
974 info.in_skb = cb->skb;
975 info.out_skb = skb;
976 info.nlmsg_seq = cb->nlh->nlmsg_seq;
977 info.nlmsg_flags = NLM_F_MULTI;
978 info.this_idx = 0;
979 info.start_idx = cb->args[0];
980 (void) xfrm_policy_walk(dump_one_policy, &info);
981 cb->args[0] = info.this_idx;
982
983 return skb->len;
984}
985
986static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
987 struct xfrm_policy *xp,
988 int dir, u32 seq)
989{
990 struct xfrm_dump_info info;
991 struct sk_buff *skb;
992
993 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
994 if (!skb)
995 return ERR_PTR(-ENOMEM);
996
997 NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
998 info.in_skb = in_skb;
999 info.out_skb = skb;
1000 info.nlmsg_seq = seq;
1001 info.nlmsg_flags = 0;
1002 info.this_idx = info.start_idx = 0;
1003
1004 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1005 kfree_skb(skb);
1006 return NULL;
1007 }
1008
1009 return skb;
1010}
1011
1012static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1013{
1014 struct xfrm_policy *xp;
1015 struct xfrm_userpolicy_id *p;
1016 int err;
26b15dad 1017 struct km_event c;
1da177e4
LT
1018 int delete;
1019
1020 p = NLMSG_DATA(nlh);
1021 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1022
1023 err = verify_policy_dir(p->dir);
1024 if (err)
1025 return err;
1026
1027 if (p->index)
1028 xp = xfrm_policy_byid(p->dir, p->index, delete);
df71837d
TJ
1029 else {
1030 struct rtattr **rtattrs = (struct rtattr **)xfrma;
1031 struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
1032 struct xfrm_policy tmp;
1033
1034 err = verify_sec_ctx_len(rtattrs);
1035 if (err)
1036 return err;
1037
1038 memset(&tmp, 0, sizeof(struct xfrm_policy));
1039 if (rt) {
1040 struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
1041
1042 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1043 return err;
1044 }
1045 xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
1046 security_xfrm_policy_free(&tmp);
1047 }
1da177e4
LT
1048 if (xp == NULL)
1049 return -ENOENT;
1050
1051 if (!delete) {
1052 struct sk_buff *resp_skb;
1053
1054 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1055 if (IS_ERR(resp_skb)) {
1056 err = PTR_ERR(resp_skb);
1057 } else {
1058 err = netlink_unicast(xfrm_nl, resp_skb,
1059 NETLINK_CB(skb).pid,
1060 MSG_DONTWAIT);
1061 }
26b15dad 1062 } else {
e7443892 1063 c.data.byid = p->index;
f60f6b8f 1064 c.event = nlh->nlmsg_type;
26b15dad
JHS
1065 c.seq = nlh->nlmsg_seq;
1066 c.pid = nlh->nlmsg_pid;
1067 km_policy_notify(xp, p->dir, &c);
1da177e4
LT
1068 }
1069
1070 xfrm_pol_put(xp);
1071
1072 return err;
1073}
1074
1075static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1076{
26b15dad 1077 struct km_event c;
1da177e4
LT
1078 struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
1079
1080 xfrm_state_flush(p->proto);
bf08867f 1081 c.data.proto = p->proto;
f60f6b8f 1082 c.event = nlh->nlmsg_type;
26b15dad
JHS
1083 c.seq = nlh->nlmsg_seq;
1084 c.pid = nlh->nlmsg_pid;
1085 km_state_notify(NULL, &c);
1086
1da177e4
LT
1087 return 0;
1088}
1089
d51d081d
JHS
1090
1091static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1092{
1093 struct xfrm_aevent_id *id;
1094 struct nlmsghdr *nlh;
1095 struct xfrm_lifetime_cur ltime;
1096 unsigned char *b = skb->tail;
1097
1098 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
1099 id = NLMSG_DATA(nlh);
1100 nlh->nlmsg_flags = 0;
1101
1102 id->sa_id.daddr = x->id.daddr;
1103 id->sa_id.spi = x->id.spi;
1104 id->sa_id.family = x->props.family;
1105 id->sa_id.proto = x->id.proto;
1106 id->flags = c->data.aevent;
1107
1108 RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1109
1110 ltime.bytes = x->curlft.bytes;
1111 ltime.packets = x->curlft.packets;
1112 ltime.add_time = x->curlft.add_time;
1113 ltime.use_time = x->curlft.use_time;
1114
1115 RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
1116
1117 if (id->flags&XFRM_AE_RTHR) {
1118 RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
1119 }
1120
1121 if (id->flags&XFRM_AE_ETHR) {
1122 u32 etimer = x->replay_maxage*10/HZ;
1123 RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
1124 }
1125
1126 nlh->nlmsg_len = skb->tail - b;
1127 return skb->len;
1128
1129rtattr_failure:
1130nlmsg_failure:
1131 skb_trim(skb, b - skb->data);
1132 return -1;
1133}
1134
1135static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1136{
1137 struct xfrm_state *x;
1138 struct sk_buff *r_skb;
1139 int err;
1140 struct km_event c;
1141 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1142 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1143 struct xfrm_usersa_id *id = &p->sa_id;
1144
1145 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1146 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1147
1148 if (p->flags&XFRM_AE_RTHR)
1149 len+=RTA_SPACE(sizeof(u32));
1150
1151 if (p->flags&XFRM_AE_ETHR)
1152 len+=RTA_SPACE(sizeof(u32));
1153
1154 r_skb = alloc_skb(len, GFP_ATOMIC);
1155 if (r_skb == NULL)
1156 return -ENOMEM;
1157
1158 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1159 if (x == NULL) {
1160 kfree(r_skb);
1161 return -ESRCH;
1162 }
1163
1164 /*
1165 * XXX: is this lock really needed - none of the other
1166 * gets lock (the concern is things getting updated
1167 * while we are still reading) - jhs
1168 */
1169 spin_lock_bh(&x->lock);
1170 c.data.aevent = p->flags;
1171 c.seq = nlh->nlmsg_seq;
1172 c.pid = nlh->nlmsg_pid;
1173
1174 if (build_aevent(r_skb, x, &c) < 0)
1175 BUG();
1176 err = netlink_unicast(xfrm_nl, r_skb,
1177 NETLINK_CB(skb).pid, MSG_DONTWAIT);
1178 spin_unlock_bh(&x->lock);
1179 xfrm_state_put(x);
1180 return err;
1181}
1182
1183static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1184{
1185 struct xfrm_state *x;
1186 struct km_event c;
1187 int err = - EINVAL;
1188 struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
1189 struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
1190 struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
1191
1192 if (!lt && !rp)
1193 return err;
1194
1195 /* pedantic mode - thou shalt sayeth replaceth */
1196 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1197 return err;
1198
1199 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1200 if (x == NULL)
1201 return -ESRCH;
1202
1203 if (x->km.state != XFRM_STATE_VALID)
1204 goto out;
1205
1206 spin_lock_bh(&x->lock);
1207 err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
1208 spin_unlock_bh(&x->lock);
1209 if (err < 0)
1210 goto out;
1211
1212 c.event = nlh->nlmsg_type;
1213 c.seq = nlh->nlmsg_seq;
1214 c.pid = nlh->nlmsg_pid;
1215 c.data.aevent = XFRM_AE_CU;
1216 km_state_notify(x, &c);
1217 err = 0;
1218out:
1219 xfrm_state_put(x);
1220 return err;
1221}
1222
1da177e4
LT
1223static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1224{
26b15dad
JHS
1225 struct km_event c;
1226
1da177e4 1227 xfrm_policy_flush();
f60f6b8f 1228 c.event = nlh->nlmsg_type;
26b15dad
JHS
1229 c.seq = nlh->nlmsg_seq;
1230 c.pid = nlh->nlmsg_pid;
1231 km_policy_notify(NULL, 0, &c);
1da177e4
LT
1232 return 0;
1233}
1234
980ebd25
JHS
1235static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
1236{
1237 struct xfrm_policy *xp;
1238 struct xfrm_user_tmpl *ut;
1239 int i;
1240 struct rtattr *rt = xfrma[XFRMA_TMPL-1];
1241
1242 struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
1243 struct xfrm_state *x = xfrm_state_alloc();
1244 int err = -ENOMEM;
1245
1246 if (!x)
1247 return err;
1248
1249 err = verify_newpolicy_info(&ua->policy);
1250 if (err) {
1251 printk("BAD policy passed\n");
1252 kfree(x);
1253 return err;
1254 }
1255
1256 /* build an XP */
1257 xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
1258 kfree(x);
1259 return err;
1260 }
1261
1262 memcpy(&x->id, &ua->id, sizeof(ua->id));
1263 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1264 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1265
1266 ut = RTA_DATA(rt);
1267 /* extract the templates and for each call km_key */
1268 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1269 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1270 memcpy(&x->id, &t->id, sizeof(x->id));
1271 x->props.mode = t->mode;
1272 x->props.reqid = t->reqid;
1273 x->props.family = ut->family;
1274 t->aalgos = ua->aalgos;
1275 t->ealgos = ua->ealgos;
1276 t->calgos = ua->calgos;
1277 err = km_query(x, t, xp);
1278
1279 }
1280
1281 kfree(x);
1282 kfree(xp);
1283
1284 return 0;
1285}
1286
d51d081d 1287
492b558b
TG
1288#define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
1289
1290static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1291 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1292 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1293 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1294 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1295 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1296 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1297 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
980ebd25 1298 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
492b558b
TG
1299 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1300 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
492b558b
TG
1301 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
1302 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
d51d081d
JHS
1303 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1304 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1da177e4
LT
1305};
1306
492b558b
TG
1307#undef XMSGSIZE
1308
1da177e4
LT
1309static struct xfrm_link {
1310 int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
1311 int (*dump)(struct sk_buff *, struct netlink_callback *);
492b558b
TG
1312} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1313 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1314 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1315 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1316 .dump = xfrm_dump_sa },
1317 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1318 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1319 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1320 .dump = xfrm_dump_policy },
1321 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
980ebd25 1322 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
492b558b
TG
1323 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1324 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1325 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1326 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
d51d081d
JHS
1327 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1328 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
1da177e4
LT
1329};
1330
1da177e4
LT
1331static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
1332{
1333 struct rtattr *xfrma[XFRMA_MAX];
1334 struct xfrm_link *link;
1335 int type, min_len;
1336
1337 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
1338 return 0;
1339
1340 type = nlh->nlmsg_type;
1341
1342 /* A control message: ignore them */
1343 if (type < XFRM_MSG_BASE)
1344 return 0;
1345
1346 /* Unknown message: reply with EINVAL */
1347 if (type > XFRM_MSG_MAX)
1348 goto err_einval;
1349
1350 type -= XFRM_MSG_BASE;
1351 link = &xfrm_dispatch[type];
1352
1353 /* All operations require privileges, even GET */
1354 if (security_netlink_recv(skb)) {
1355 *errp = -EPERM;
1356 return -1;
1357 }
1358
492b558b
TG
1359 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1360 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1361 (nlh->nlmsg_flags & NLM_F_DUMP)) {
1da177e4
LT
1362 if (link->dump == NULL)
1363 goto err_einval;
1364
1365 if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
a8f74b22 1366 link->dump, NULL)) != 0) {
1da177e4
LT
1367 return -1;
1368 }
88fc2c84
TG
1369
1370 netlink_queue_skip(nlh, skb);
1da177e4
LT
1371 return -1;
1372 }
1373
1374 memset(xfrma, 0, sizeof(xfrma));
1375
1376 if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
1377 goto err_einval;
1378
1379 if (nlh->nlmsg_len > min_len) {
1380 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1381 struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
1382
1383 while (RTA_OK(attr, attrlen)) {
1384 unsigned short flavor = attr->rta_type;
1385 if (flavor) {
1386 if (flavor > XFRMA_MAX)
1387 goto err_einval;
1388 xfrma[flavor - 1] = attr;
1389 }
1390 attr = RTA_NEXT(attr, attrlen);
1391 }
1392 }
1393
1394 if (link->doit == NULL)
1395 goto err_einval;
1396 *errp = link->doit(skb, nlh, (void **) &xfrma);
1397
1398 return *errp;
1399
1400err_einval:
1401 *errp = -EINVAL;
1402 return -1;
1403}
1404
1da177e4
LT
1405static void xfrm_netlink_rcv(struct sock *sk, int len)
1406{
88fc2c84 1407 unsigned int qlen = 0;
2a0a6ebe 1408
1da177e4 1409 do {
1da177e4 1410 down(&xfrm_cfg_sem);
88fc2c84 1411 netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
1da177e4
LT
1412 up(&xfrm_cfg_sem);
1413
2a0a6ebe 1414 } while (qlen);
1da177e4
LT
1415}
1416
d51d081d 1417static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1da177e4
LT
1418{
1419 struct xfrm_user_expire *ue;
1420 struct nlmsghdr *nlh;
1421 unsigned char *b = skb->tail;
1422
d51d081d 1423 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
1da177e4
LT
1424 sizeof(*ue));
1425 ue = NLMSG_DATA(nlh);
1426 nlh->nlmsg_flags = 0;
1427
1428 copy_to_user_state(x, &ue->state);
d51d081d 1429 ue->hard = (c->data.hard != 0) ? 1 : 0;
1da177e4
LT
1430
1431 nlh->nlmsg_len = skb->tail - b;
1432 return skb->len;
1433
1434nlmsg_failure:
1435 skb_trim(skb, b - skb->data);
1436 return -1;
1437}
1438
26b15dad 1439static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
1da177e4
LT
1440{
1441 struct sk_buff *skb;
ee57eef9 1442 int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
1da177e4 1443
ee57eef9 1444 skb = alloc_skb(len, GFP_ATOMIC);
1da177e4
LT
1445 if (skb == NULL)
1446 return -ENOMEM;
1447
d51d081d 1448 if (build_expire(skb, x, c) < 0)
1da177e4
LT
1449 BUG();
1450
ac6d439d
PM
1451 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1452 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
1453}
1454
d51d081d
JHS
1455static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1456{
1457 struct sk_buff *skb;
1458 int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
1459
1460 len += RTA_SPACE(sizeof(struct xfrm_replay_state));
1461 len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
1462 skb = alloc_skb(len, GFP_ATOMIC);
1463 if (skb == NULL)
1464 return -ENOMEM;
1465
1466 if (build_aevent(skb, x, c) < 0)
1467 BUG();
1468
1469 NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
1470 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
1471}
1472
26b15dad
JHS
1473static int xfrm_notify_sa_flush(struct km_event *c)
1474{
1475 struct xfrm_usersa_flush *p;
1476 struct nlmsghdr *nlh;
1477 struct sk_buff *skb;
1478 unsigned char *b;
1479 int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
1480
1481 skb = alloc_skb(len, GFP_ATOMIC);
1482 if (skb == NULL)
1483 return -ENOMEM;
1484 b = skb->tail;
1485
1486 nlh = NLMSG_PUT(skb, c->pid, c->seq,
1487 XFRM_MSG_FLUSHSA, sizeof(*p));
1488 nlh->nlmsg_flags = 0;
1489
1490 p = NLMSG_DATA(nlh);
bf08867f 1491 p->proto = c->data.proto;
26b15dad
JHS
1492
1493 nlh->nlmsg_len = skb->tail - b;
1494
ac6d439d
PM
1495 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1496 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad
JHS
1497
1498nlmsg_failure:
1499 kfree_skb(skb);
1500 return -1;
1501}
1502
1503static int inline xfrm_sa_len(struct xfrm_state *x)
1504{
0603eac0 1505 int l = 0;
26b15dad
JHS
1506 if (x->aalg)
1507 l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
1508 if (x->ealg)
1509 l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
1510 if (x->calg)
1511 l += RTA_SPACE(sizeof(*x->calg));
1512 if (x->encap)
1513 l += RTA_SPACE(sizeof(*x->encap));
1514
1515 return l;
1516}
1517
1518static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
1519{
1520 struct xfrm_usersa_info *p;
0603eac0 1521 struct xfrm_usersa_id *id;
26b15dad
JHS
1522 struct nlmsghdr *nlh;
1523 struct sk_buff *skb;
26b15dad
JHS
1524 unsigned char *b;
1525 int len = xfrm_sa_len(x);
0603eac0
HX
1526 int headlen;
1527
1528 headlen = sizeof(*p);
1529 if (c->event == XFRM_MSG_DELSA) {
1530 len += RTA_SPACE(headlen);
1531 headlen = sizeof(*id);
1532 }
1533 len += NLMSG_SPACE(headlen);
26b15dad
JHS
1534
1535 skb = alloc_skb(len, GFP_ATOMIC);
1536 if (skb == NULL)
1537 return -ENOMEM;
1538 b = skb->tail;
1539
0603eac0 1540 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
26b15dad
JHS
1541 nlh->nlmsg_flags = 0;
1542
1543 p = NLMSG_DATA(nlh);
0603eac0
HX
1544 if (c->event == XFRM_MSG_DELSA) {
1545 id = NLMSG_DATA(nlh);
1546 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
1547 id->spi = x->id.spi;
1548 id->family = x->props.family;
1549 id->proto = x->id.proto;
1550
1551 p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
1552 }
1553
26b15dad
JHS
1554 copy_to_user_state(x, p);
1555
1556 if (x->aalg)
1557 RTA_PUT(skb, XFRMA_ALG_AUTH,
1558 sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
1559 if (x->ealg)
1560 RTA_PUT(skb, XFRMA_ALG_CRYPT,
1561 sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
1562 if (x->calg)
1563 RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
1564
1565 if (x->encap)
1566 RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
1567
1568 nlh->nlmsg_len = skb->tail - b;
1569
ac6d439d
PM
1570 NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
1571 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
26b15dad
JHS
1572
1573nlmsg_failure:
1574rtattr_failure:
1575 kfree_skb(skb);
1576 return -1;
1577}
1578
1579static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
1580{
1581
1582 switch (c->event) {
f60f6b8f 1583 case XFRM_MSG_EXPIRE:
26b15dad 1584 return xfrm_exp_state_notify(x, c);
d51d081d
JHS
1585 case XFRM_MSG_NEWAE:
1586 return xfrm_aevent_state_notify(x, c);
f60f6b8f
HX
1587 case XFRM_MSG_DELSA:
1588 case XFRM_MSG_UPDSA:
1589 case XFRM_MSG_NEWSA:
26b15dad 1590 return xfrm_notify_sa(x, c);
f60f6b8f 1591 case XFRM_MSG_FLUSHSA:
26b15dad
JHS
1592 return xfrm_notify_sa_flush(c);
1593 default:
1594 printk("xfrm_user: Unknown SA event %d\n", c->event);
1595 break;
1596 }
1597
1598 return 0;
1599
1600}
1601
1da177e4
LT
1602static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
1603 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
1604 int dir)
1605{
1606 struct xfrm_user_acquire *ua;
1607 struct nlmsghdr *nlh;
1608 unsigned char *b = skb->tail;
1609 __u32 seq = xfrm_get_acqseq();
1610
1611 nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
1612 sizeof(*ua));
1613 ua = NLMSG_DATA(nlh);
1614 nlh->nlmsg_flags = 0;
1615
1616 memcpy(&ua->id, &x->id, sizeof(ua->id));
1617 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
1618 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
1619 copy_to_user_policy(xp, &ua->policy, dir);
1620 ua->aalgos = xt->aalgos;
1621 ua->ealgos = xt->ealgos;
1622 ua->calgos = xt->calgos;
1623 ua->seq = x->km.seq = seq;
1624
1625 if (copy_to_user_tmpl(xp, skb) < 0)
1626 goto nlmsg_failure;
df71837d
TJ
1627 if (copy_to_user_sec_ctx(xp, skb))
1628 goto nlmsg_failure;
1da177e4
LT
1629
1630 nlh->nlmsg_len = skb->tail - b;
1631 return skb->len;
1632
1633nlmsg_failure:
1634 skb_trim(skb, b - skb->data);
1635 return -1;
1636}
1637
1638static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
1639 struct xfrm_policy *xp, int dir)
1640{
1641 struct sk_buff *skb;
1642 size_t len;
1643
1644 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1645 len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
df71837d 1646 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1da177e4
LT
1647 skb = alloc_skb(len, GFP_ATOMIC);
1648 if (skb == NULL)
1649 return -ENOMEM;
1650
1651 if (build_acquire(skb, x, xt, xp, dir) < 0)
1652 BUG();
1653
ac6d439d
PM
1654 NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
1655 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
1da177e4
LT
1656}
1657
1658/* User gives us xfrm_user_policy_info followed by an array of 0
1659 * or more templates.
1660 */
1661static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
1662 u8 *data, int len, int *dir)
1663{
1664 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
1665 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
1666 struct xfrm_policy *xp;
1667 int nr;
1668
1669 switch (family) {
1670 case AF_INET:
1671 if (opt != IP_XFRM_POLICY) {
1672 *dir = -EOPNOTSUPP;
1673 return NULL;
1674 }
1675 break;
1676#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1677 case AF_INET6:
1678 if (opt != IPV6_XFRM_POLICY) {
1679 *dir = -EOPNOTSUPP;
1680 return NULL;
1681 }
1682 break;
1683#endif
1684 default:
1685 *dir = -EINVAL;
1686 return NULL;
1687 }
1688
1689 *dir = -EINVAL;
1690
1691 if (len < sizeof(*p) ||
1692 verify_newpolicy_info(p))
1693 return NULL;
1694
1695 nr = ((len - sizeof(*p)) / sizeof(*ut));
1696 if (nr > XFRM_MAX_DEPTH)
1697 return NULL;
1698
a4f1bac6
HX
1699 if (p->dir > XFRM_POLICY_OUT)
1700 return NULL;
1701
1da177e4
LT
1702 xp = xfrm_policy_alloc(GFP_KERNEL);
1703 if (xp == NULL) {
1704 *dir = -ENOBUFS;
1705 return NULL;
1706 }
1707
1708 copy_from_user_policy(xp, p);
1709 copy_templates(xp, ut, nr);
1710
1711 *dir = p->dir;
1712
1713 return xp;
1714}
1715
1716static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
d51d081d 1717 int dir, struct km_event *c)
1da177e4
LT
1718{
1719 struct xfrm_user_polexpire *upe;
1720 struct nlmsghdr *nlh;
d51d081d 1721 int hard = c->data.hard;
1da177e4
LT
1722 unsigned char *b = skb->tail;
1723
d51d081d 1724 nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
1da177e4
LT
1725 upe = NLMSG_DATA(nlh);
1726 nlh->nlmsg_flags = 0;
1727
1728 copy_to_user_policy(xp, &upe->pol, dir);
1729 if (copy_to_user_tmpl(xp, skb) < 0)
1730 goto nlmsg_failure;
df71837d
TJ
1731 if (copy_to_user_sec_ctx(xp, skb))
1732 goto nlmsg_failure;
1da177e4
LT
1733 upe->hard = !!hard;
1734
1735 nlh->nlmsg_len = skb->tail - b;
1736 return skb->len;
1737
1738nlmsg_failure:
1739 skb_trim(skb, b - skb->data);
1740 return -1;
1741}
1742
26b15dad 1743static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1da177e4
LT
1744{
1745 struct sk_buff *skb;
1746 size_t len;
1747
1748 len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
1749 len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
df71837d 1750 len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
1da177e4
LT
1751 skb = alloc_skb(len, GFP_ATOMIC);
1752 if (skb == NULL)
1753 return -ENOMEM;
1754
d51d081d 1755 if (build_polexpire(skb, xp, dir, c) < 0)
1da177e4
LT
1756 BUG();
1757
ac6d439d
PM
1758 NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
1759 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
1da177e4
LT
1760}
1761
26b15dad
JHS
1762static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
1763{
1764 struct xfrm_userpolicy_info *p;
0603eac0 1765 struct xfrm_userpolicy_id *id;
26b15dad
JHS
1766 struct nlmsghdr *nlh;
1767 struct sk_buff *skb;
26b15dad
JHS
1768 unsigned char *b;
1769 int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
0603eac0
HX
1770 int headlen;
1771
1772 headlen = sizeof(*p);
1773 if (c->event == XFRM_MSG_DELPOLICY) {
1774 len += RTA_SPACE(headlen);
1775 headlen = sizeof(*id);
1776 }
1777 len += NLMSG_SPACE(headlen);
26b15dad
JHS
1778
1779 skb = alloc_skb(len, GFP_ATOMIC);
1780 if (skb == NULL)
1781 return -ENOMEM;
1782 b = skb->tail;
1783
0603eac0 1784 nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
26b15dad
JHS
1785
1786 p = NLMSG_DATA(nlh);
0603eac0
HX
1787 if (c->event == XFRM_MSG_DELPOLICY) {
1788 id = NLMSG_DATA(nlh);
1789 memset(id, 0, sizeof(*id));
1790 id->dir = dir;
1791 if (c->data.byid)
1792 id->index = xp->index;
1793 else
1794 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
1795
1796 p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
1797 }
26b15dad
JHS
1798
1799 nlh->nlmsg_flags = 0;
1800
1801 copy_to_user_policy(xp, p, dir);
1802 if (copy_to_user_tmpl(xp, skb) < 0)
1803 goto nlmsg_failure;
1804
1805 nlh->nlmsg_len = skb->tail - b;
1806
ac6d439d
PM
1807 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1808 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
1809
1810nlmsg_failure:
0603eac0 1811rtattr_failure:
26b15dad
JHS
1812 kfree_skb(skb);
1813 return -1;
1814}
1815
1816static int xfrm_notify_policy_flush(struct km_event *c)
1817{
1818 struct nlmsghdr *nlh;
1819 struct sk_buff *skb;
1820 unsigned char *b;
1821 int len = NLMSG_LENGTH(0);
1822
1823 skb = alloc_skb(len, GFP_ATOMIC);
1824 if (skb == NULL)
1825 return -ENOMEM;
1826 b = skb->tail;
1827
1828
1829 nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
1830
1831 nlh->nlmsg_len = skb->tail - b;
1832
ac6d439d
PM
1833 NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
1834 return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
26b15dad
JHS
1835
1836nlmsg_failure:
1837 kfree_skb(skb);
1838 return -1;
1839}
1840
1841static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
1842{
1843
1844 switch (c->event) {
f60f6b8f
HX
1845 case XFRM_MSG_NEWPOLICY:
1846 case XFRM_MSG_UPDPOLICY:
1847 case XFRM_MSG_DELPOLICY:
26b15dad 1848 return xfrm_notify_policy(xp, dir, c);
f60f6b8f 1849 case XFRM_MSG_FLUSHPOLICY:
26b15dad 1850 return xfrm_notify_policy_flush(c);
f60f6b8f 1851 case XFRM_MSG_POLEXPIRE:
26b15dad
JHS
1852 return xfrm_exp_policy_notify(xp, dir, c);
1853 default:
1854 printk("xfrm_user: Unknown Policy event %d\n", c->event);
1855 }
1856
1857 return 0;
1858
1859}
1860
1da177e4
LT
1861static struct xfrm_mgr netlink_mgr = {
1862 .id = "netlink",
1863 .notify = xfrm_send_state_notify,
1864 .acquire = xfrm_send_acquire,
1865 .compile_policy = xfrm_compile_policy,
1866 .notify_policy = xfrm_send_policy_notify,
1867};
1868
1869static int __init xfrm_user_init(void)
1870{
1871 printk(KERN_INFO "Initializing IPsec netlink socket\n");
1872
06628607
PM
1873 xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
1874 xfrm_netlink_rcv, THIS_MODULE);
1da177e4
LT
1875 if (xfrm_nl == NULL)
1876 return -ENOMEM;
1877
1878 xfrm_register_km(&netlink_mgr);
1879
1880 return 0;
1881}
1882
1883static void __exit xfrm_user_exit(void)
1884{
1885 xfrm_unregister_km(&netlink_mgr);
1886 sock_release(xfrm_nl->sk_socket);
1887}
1888
1889module_init(xfrm_user_init);
1890module_exit(xfrm_user_exit);
1891MODULE_LICENSE("GPL");
4fdb3bb7 1892MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
f8cd5488
JHS
1893EXPORT_SYMBOL(xfrm_nl);
1894