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