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