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