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