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