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