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