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