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