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