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