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