GNU Linux-libre 4.14.303-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) {
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
1005                 if (attrs[XFRMA_PROTO])
1006                         proto = nla_get_u8(attrs[XFRMA_PROTO]);
1007
1008                 xfrm_state_walk_init(walk, proto, filter);
1009                 cb->args[0] = 1;
1010         }
1011
1012         (void) xfrm_state_walk(net, walk, dump_one_state, &info);
1013
1014         return skb->len;
1015 }
1016
1017 static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
1018                                           struct xfrm_state *x, u32 seq)
1019 {
1020         struct xfrm_dump_info info;
1021         struct sk_buff *skb;
1022         int err;
1023
1024         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1025         if (!skb)
1026                 return ERR_PTR(-ENOMEM);
1027
1028         info.in_skb = in_skb;
1029         info.out_skb = skb;
1030         info.nlmsg_seq = seq;
1031         info.nlmsg_flags = 0;
1032
1033         err = dump_one_state(x, 0, &info);
1034         if (err) {
1035                 kfree_skb(skb);
1036                 return ERR_PTR(err);
1037         }
1038
1039         return skb;
1040 }
1041
1042 /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
1043  * Must be called with RCU read lock.
1044  */
1045 static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
1046                                        u32 pid, unsigned int group)
1047 {
1048         struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
1049
1050         if (!nlsk) {
1051                 kfree_skb(skb);
1052                 return -EPIPE;
1053         }
1054
1055         return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
1056 }
1057
1058 static inline size_t xfrm_spdinfo_msgsize(void)
1059 {
1060         return NLMSG_ALIGN(4)
1061                + nla_total_size(sizeof(struct xfrmu_spdinfo))
1062                + nla_total_size(sizeof(struct xfrmu_spdhinfo))
1063                + nla_total_size(sizeof(struct xfrmu_spdhthresh))
1064                + nla_total_size(sizeof(struct xfrmu_spdhthresh));
1065 }
1066
1067 static int build_spdinfo(struct sk_buff *skb, struct net *net,
1068                          u32 portid, u32 seq, u32 flags)
1069 {
1070         struct xfrmk_spdinfo si;
1071         struct xfrmu_spdinfo spc;
1072         struct xfrmu_spdhinfo sph;
1073         struct xfrmu_spdhthresh spt4, spt6;
1074         struct nlmsghdr *nlh;
1075         int err;
1076         u32 *f;
1077         unsigned lseq;
1078
1079         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
1080         if (nlh == NULL) /* shouldn't really happen ... */
1081                 return -EMSGSIZE;
1082
1083         f = nlmsg_data(nlh);
1084         *f = flags;
1085         xfrm_spd_getinfo(net, &si);
1086         spc.incnt = si.incnt;
1087         spc.outcnt = si.outcnt;
1088         spc.fwdcnt = si.fwdcnt;
1089         spc.inscnt = si.inscnt;
1090         spc.outscnt = si.outscnt;
1091         spc.fwdscnt = si.fwdscnt;
1092         sph.spdhcnt = si.spdhcnt;
1093         sph.spdhmcnt = si.spdhmcnt;
1094
1095         do {
1096                 lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
1097
1098                 spt4.lbits = net->xfrm.policy_hthresh.lbits4;
1099                 spt4.rbits = net->xfrm.policy_hthresh.rbits4;
1100                 spt6.lbits = net->xfrm.policy_hthresh.lbits6;
1101                 spt6.rbits = net->xfrm.policy_hthresh.rbits6;
1102         } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
1103
1104         err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1105         if (!err)
1106                 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1107         if (!err)
1108                 err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
1109         if (!err)
1110                 err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
1111         if (err) {
1112                 nlmsg_cancel(skb, nlh);
1113                 return err;
1114         }
1115
1116         nlmsg_end(skb, nlh);
1117         return 0;
1118 }
1119
1120 static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1121                             struct nlattr **attrs)
1122 {
1123         struct net *net = sock_net(skb->sk);
1124         struct xfrmu_spdhthresh *thresh4 = NULL;
1125         struct xfrmu_spdhthresh *thresh6 = NULL;
1126
1127         /* selector prefixlen thresholds to hash policies */
1128         if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
1129                 struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
1130
1131                 if (nla_len(rta) < sizeof(*thresh4))
1132                         return -EINVAL;
1133                 thresh4 = nla_data(rta);
1134                 if (thresh4->lbits > 32 || thresh4->rbits > 32)
1135                         return -EINVAL;
1136         }
1137         if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
1138                 struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
1139
1140                 if (nla_len(rta) < sizeof(*thresh6))
1141                         return -EINVAL;
1142                 thresh6 = nla_data(rta);
1143                 if (thresh6->lbits > 128 || thresh6->rbits > 128)
1144                         return -EINVAL;
1145         }
1146
1147         if (thresh4 || thresh6) {
1148                 write_seqlock(&net->xfrm.policy_hthresh.lock);
1149                 if (thresh4) {
1150                         net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
1151                         net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
1152                 }
1153                 if (thresh6) {
1154                         net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
1155                         net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
1156                 }
1157                 write_sequnlock(&net->xfrm.policy_hthresh.lock);
1158
1159                 xfrm_policy_hash_rebuild(net);
1160         }
1161
1162         return 0;
1163 }
1164
1165 static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1166                 struct nlattr **attrs)
1167 {
1168         struct net *net = sock_net(skb->sk);
1169         struct sk_buff *r_skb;
1170         u32 *flags = nlmsg_data(nlh);
1171         u32 sportid = NETLINK_CB(skb).portid;
1172         u32 seq = nlh->nlmsg_seq;
1173
1174         r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
1175         if (r_skb == NULL)
1176                 return -ENOMEM;
1177
1178         if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
1179                 BUG();
1180
1181         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1182 }
1183
1184 static inline size_t xfrm_sadinfo_msgsize(void)
1185 {
1186         return NLMSG_ALIGN(4)
1187                + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1188                + nla_total_size(4); /* XFRMA_SAD_CNT */
1189 }
1190
1191 static int build_sadinfo(struct sk_buff *skb, struct net *net,
1192                          u32 portid, u32 seq, u32 flags)
1193 {
1194         struct xfrmk_sadinfo si;
1195         struct xfrmu_sadhinfo sh;
1196         struct nlmsghdr *nlh;
1197         int err;
1198         u32 *f;
1199
1200         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
1201         if (nlh == NULL) /* shouldn't really happen ... */
1202                 return -EMSGSIZE;
1203
1204         f = nlmsg_data(nlh);
1205         *f = flags;
1206         xfrm_sad_getinfo(net, &si);
1207
1208         sh.sadhmcnt = si.sadhmcnt;
1209         sh.sadhcnt = si.sadhcnt;
1210
1211         err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1212         if (!err)
1213                 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1214         if (err) {
1215                 nlmsg_cancel(skb, nlh);
1216                 return err;
1217         }
1218
1219         nlmsg_end(skb, nlh);
1220         return 0;
1221 }
1222
1223 static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
1224                 struct nlattr **attrs)
1225 {
1226         struct net *net = sock_net(skb->sk);
1227         struct sk_buff *r_skb;
1228         u32 *flags = nlmsg_data(nlh);
1229         u32 sportid = NETLINK_CB(skb).portid;
1230         u32 seq = nlh->nlmsg_seq;
1231
1232         r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
1233         if (r_skb == NULL)
1234                 return -ENOMEM;
1235
1236         if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
1237                 BUG();
1238
1239         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
1240 }
1241
1242 static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1243                 struct nlattr **attrs)
1244 {
1245         struct net *net = sock_net(skb->sk);
1246         struct xfrm_usersa_id *p = nlmsg_data(nlh);
1247         struct xfrm_state *x;
1248         struct sk_buff *resp_skb;
1249         int err = -ESRCH;
1250
1251         x = xfrm_user_state_lookup(net, p, attrs, &err);
1252         if (x == NULL)
1253                 goto out_noput;
1254
1255         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1256         if (IS_ERR(resp_skb)) {
1257                 err = PTR_ERR(resp_skb);
1258         } else {
1259                 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1260         }
1261         xfrm_state_put(x);
1262 out_noput:
1263         return err;
1264 }
1265
1266 static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
1267                 struct nlattr **attrs)
1268 {
1269         struct net *net = sock_net(skb->sk);
1270         struct xfrm_state *x;
1271         struct xfrm_userspi_info *p;
1272         struct sk_buff *resp_skb;
1273         xfrm_address_t *daddr;
1274         int family;
1275         int err;
1276         u32 mark;
1277         struct xfrm_mark m;
1278
1279         p = nlmsg_data(nlh);
1280         err = verify_spi_info(p->info.id.proto, p->min, p->max);
1281         if (err)
1282                 goto out_noput;
1283
1284         family = p->info.family;
1285         daddr = &p->info.id.daddr;
1286
1287         x = NULL;
1288
1289         mark = xfrm_mark_get(attrs, &m);
1290         if (p->info.seq) {
1291                 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
1292                 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
1293                         xfrm_state_put(x);
1294                         x = NULL;
1295                 }
1296         }
1297
1298         if (!x)
1299                 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
1300                                   p->info.id.proto, daddr,
1301                                   &p->info.saddr, 1,
1302                                   family);
1303         err = -ENOENT;
1304         if (x == NULL)
1305                 goto out_noput;
1306
1307         err = xfrm_alloc_spi(x, p->min, p->max);
1308         if (err)
1309                 goto out;
1310
1311         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1312         if (IS_ERR(resp_skb)) {
1313                 err = PTR_ERR(resp_skb);
1314                 goto out;
1315         }
1316
1317         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
1318
1319 out:
1320         xfrm_state_put(x);
1321 out_noput:
1322         return err;
1323 }
1324
1325 static int verify_policy_dir(u8 dir)
1326 {
1327         switch (dir) {
1328         case XFRM_POLICY_IN:
1329         case XFRM_POLICY_OUT:
1330         case XFRM_POLICY_FWD:
1331                 break;
1332
1333         default:
1334                 return -EINVAL;
1335         }
1336
1337         return 0;
1338 }
1339
1340 static int verify_policy_type(u8 type)
1341 {
1342         switch (type) {
1343         case XFRM_POLICY_TYPE_MAIN:
1344 #ifdef CONFIG_XFRM_SUB_POLICY
1345         case XFRM_POLICY_TYPE_SUB:
1346 #endif
1347                 break;
1348
1349         default:
1350                 return -EINVAL;
1351         }
1352
1353         return 0;
1354 }
1355
1356 static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1357 {
1358         int ret;
1359
1360         switch (p->share) {
1361         case XFRM_SHARE_ANY:
1362         case XFRM_SHARE_SESSION:
1363         case XFRM_SHARE_USER:
1364         case XFRM_SHARE_UNIQUE:
1365                 break;
1366
1367         default:
1368                 return -EINVAL;
1369         }
1370
1371         switch (p->action) {
1372         case XFRM_POLICY_ALLOW:
1373         case XFRM_POLICY_BLOCK:
1374                 break;
1375
1376         default:
1377                 return -EINVAL;
1378         }
1379
1380         switch (p->sel.family) {
1381         case AF_INET:
1382                 if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32)
1383                         return -EINVAL;
1384
1385                 break;
1386
1387         case AF_INET6:
1388 #if IS_ENABLED(CONFIG_IPV6)
1389                 if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128)
1390                         return -EINVAL;
1391
1392                 break;
1393 #else
1394                 return  -EAFNOSUPPORT;
1395 #endif
1396
1397         default:
1398                 return -EINVAL;
1399         }
1400
1401         ret = verify_policy_dir(p->dir);
1402         if (ret)
1403                 return ret;
1404         if (p->index && (xfrm_policy_id2dir(p->index) != p->dir))
1405                 return -EINVAL;
1406
1407         return 0;
1408 }
1409
1410 static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
1411 {
1412         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1413         struct xfrm_user_sec_ctx *uctx;
1414
1415         if (!rt)
1416                 return 0;
1417
1418         uctx = nla_data(rt);
1419         return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
1420 }
1421
1422 static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1423                            int nr)
1424 {
1425         int i;
1426
1427         xp->xfrm_nr = nr;
1428         for (i = 0; i < nr; i++, ut++) {
1429                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1430
1431                 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1432                 memcpy(&t->saddr, &ut->saddr,
1433                        sizeof(xfrm_address_t));
1434                 t->reqid = ut->reqid;
1435                 t->mode = ut->mode;
1436                 t->share = ut->share;
1437                 t->optional = ut->optional;
1438                 t->aalgos = ut->aalgos;
1439                 t->ealgos = ut->ealgos;
1440                 t->calgos = ut->calgos;
1441                 /* If all masks are ~0, then we allow all algorithms. */
1442                 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
1443                 t->encap_family = ut->family;
1444         }
1445 }
1446
1447 static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1448 {
1449         u16 prev_family;
1450         int i;
1451
1452         if (nr > XFRM_MAX_DEPTH)
1453                 return -EINVAL;
1454
1455         prev_family = family;
1456
1457         for (i = 0; i < nr; i++) {
1458                 /* We never validated the ut->family value, so many
1459                  * applications simply leave it at zero.  The check was
1460                  * never made and ut->family was ignored because all
1461                  * templates could be assumed to have the same family as
1462                  * the policy itself.  Now that we will have ipv4-in-ipv6
1463                  * and ipv6-in-ipv4 tunnels, this is no longer true.
1464                  */
1465                 if (!ut[i].family)
1466                         ut[i].family = family;
1467
1468                 switch (ut[i].mode) {
1469                 case XFRM_MODE_TUNNEL:
1470                 case XFRM_MODE_BEET:
1471                         break;
1472                 default:
1473                         if (ut[i].family != prev_family)
1474                                 return -EINVAL;
1475                         break;
1476                 }
1477                 if (ut[i].mode >= XFRM_MODE_MAX)
1478                         return -EINVAL;
1479
1480                 prev_family = ut[i].family;
1481
1482                 switch (ut[i].family) {
1483                 case AF_INET:
1484                         break;
1485 #if IS_ENABLED(CONFIG_IPV6)
1486                 case AF_INET6:
1487                         break;
1488 #endif
1489                 default:
1490                         return -EINVAL;
1491                 }
1492
1493                 if (!xfrm_id_proto_valid(ut[i].id.proto))
1494                         return -EINVAL;
1495         }
1496
1497         return 0;
1498 }
1499
1500 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
1501 {
1502         struct nlattr *rt = attrs[XFRMA_TMPL];
1503
1504         if (!rt) {
1505                 pol->xfrm_nr = 0;
1506         } else {
1507                 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1508                 int nr = nla_len(rt) / sizeof(*utmpl);
1509                 int err;
1510
1511                 err = validate_tmpl(nr, utmpl, pol->family);
1512                 if (err)
1513                         return err;
1514
1515                 copy_templates(pol, utmpl, nr);
1516         }
1517         return 0;
1518 }
1519
1520 static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
1521 {
1522         struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
1523         struct xfrm_userpolicy_type *upt;
1524         u8 type = XFRM_POLICY_TYPE_MAIN;
1525         int err;
1526
1527         if (rt) {
1528                 upt = nla_data(rt);
1529                 type = upt->type;
1530         }
1531
1532         err = verify_policy_type(type);
1533         if (err)
1534                 return err;
1535
1536         *tp = type;
1537         return 0;
1538 }
1539
1540 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1541 {
1542         xp->priority = p->priority;
1543         xp->index = p->index;
1544         memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1545         memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1546         xp->action = p->action;
1547         xp->flags = p->flags;
1548         xp->family = p->sel.family;
1549         /* XXX xp->share = p->share; */
1550 }
1551
1552 static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1553 {
1554         memset(p, 0, sizeof(*p));
1555         memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1556         memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1557         memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1558         p->priority = xp->priority;
1559         p->index = xp->index;
1560         p->sel.family = xp->family;
1561         p->dir = dir;
1562         p->action = xp->action;
1563         p->flags = xp->flags;
1564         p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1565 }
1566
1567 static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
1568 {
1569         struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
1570         int err;
1571
1572         if (!xp) {
1573                 *errp = -ENOMEM;
1574                 return NULL;
1575         }
1576
1577         copy_from_user_policy(xp, p);
1578
1579         err = copy_from_user_policy_type(&xp->type, attrs);
1580         if (err)
1581                 goto error;
1582
1583         if (!(err = copy_from_user_tmpl(xp, attrs)))
1584                 err = copy_from_user_sec_ctx(xp, attrs);
1585         if (err)
1586                 goto error;
1587
1588         xfrm_mark_get(attrs, &xp->mark);
1589
1590         return xp;
1591  error:
1592         *errp = err;
1593         xp->walk.dead = 1;
1594         xfrm_policy_destroy(xp);
1595         return NULL;
1596 }
1597
1598 static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1599                 struct nlattr **attrs)
1600 {
1601         struct net *net = sock_net(skb->sk);
1602         struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
1603         struct xfrm_policy *xp;
1604         struct km_event c;
1605         int err;
1606         int excl;
1607
1608         err = verify_newpolicy_info(p);
1609         if (err)
1610                 return err;
1611         err = verify_sec_ctx_len(attrs);
1612         if (err)
1613                 return err;
1614
1615         xp = xfrm_policy_construct(net, p, attrs, &err);
1616         if (!xp)
1617                 return err;
1618
1619         /* shouldn't excl be based on nlh flags??
1620          * Aha! this is anti-netlink really i.e  more pfkey derived
1621          * in netlink excl is a flag and you wouldnt need
1622          * a type XFRM_MSG_UPDPOLICY - JHS */
1623         excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1624         err = xfrm_policy_insert(p->dir, xp, excl);
1625         xfrm_audit_policy_add(xp, err ? 0 : 1, true);
1626
1627         if (err) {
1628                 security_xfrm_policy_free(xp->security);
1629                 kfree(xp);
1630                 return err;
1631         }
1632
1633         c.event = nlh->nlmsg_type;
1634         c.seq = nlh->nlmsg_seq;
1635         c.portid = nlh->nlmsg_pid;
1636         km_policy_notify(xp, p->dir, &c);
1637
1638         xfrm_pol_put(xp);
1639
1640         return 0;
1641 }
1642
1643 static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1644 {
1645         struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1646         int i;
1647
1648         if (xp->xfrm_nr == 0)
1649                 return 0;
1650
1651         for (i = 0; i < xp->xfrm_nr; i++) {
1652                 struct xfrm_user_tmpl *up = &vec[i];
1653                 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1654
1655                 memset(up, 0, sizeof(*up));
1656                 memcpy(&up->id, &kp->id, sizeof(up->id));
1657                 up->family = kp->encap_family;
1658                 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1659                 up->reqid = kp->reqid;
1660                 up->mode = kp->mode;
1661                 up->share = kp->share;
1662                 up->optional = kp->optional;
1663                 up->aalgos = kp->aalgos;
1664                 up->ealgos = kp->ealgos;
1665                 up->calgos = kp->calgos;
1666         }
1667
1668         return nla_put(skb, XFRMA_TMPL,
1669                        sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
1670 }
1671
1672 static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1673 {
1674         if (x->security) {
1675                 return copy_sec_ctx(x->security, skb);
1676         }
1677         return 0;
1678 }
1679
1680 static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1681 {
1682         if (xp->security)
1683                 return copy_sec_ctx(xp->security, skb);
1684         return 0;
1685 }
1686 static inline size_t userpolicy_type_attrsize(void)
1687 {
1688 #ifdef CONFIG_XFRM_SUB_POLICY
1689         return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1690 #else
1691         return 0;
1692 #endif
1693 }
1694
1695 #ifdef CONFIG_XFRM_SUB_POLICY
1696 static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1697 {
1698         struct xfrm_userpolicy_type upt;
1699
1700         /* Sadly there are two holes in struct xfrm_userpolicy_type */
1701         memset(&upt, 0, sizeof(upt));
1702         upt.type = type;
1703
1704         return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
1705 }
1706
1707 #else
1708 static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
1709 {
1710         return 0;
1711 }
1712 #endif
1713
1714 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1715 {
1716         struct xfrm_dump_info *sp = ptr;
1717         struct xfrm_userpolicy_info *p;
1718         struct sk_buff *in_skb = sp->in_skb;
1719         struct sk_buff *skb = sp->out_skb;
1720         struct nlmsghdr *nlh;
1721         int err;
1722
1723         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
1724                         XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1725         if (nlh == NULL)
1726                 return -EMSGSIZE;
1727
1728         p = nlmsg_data(nlh);
1729         copy_to_user_policy(xp, p, dir);
1730         err = copy_to_user_tmpl(xp, skb);
1731         if (!err)
1732                 err = copy_to_user_sec_ctx(xp, skb);
1733         if (!err)
1734                 err = copy_to_user_policy_type(xp->type, skb);
1735         if (!err)
1736                 err = xfrm_mark_put(skb, &xp->mark);
1737         if (err) {
1738                 nlmsg_cancel(skb, nlh);
1739                 return err;
1740         }
1741         nlmsg_end(skb, nlh);
1742         return 0;
1743 }
1744
1745 static int xfrm_dump_policy_done(struct netlink_callback *cb)
1746 {
1747         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1748         struct net *net = sock_net(cb->skb->sk);
1749
1750         xfrm_policy_walk_done(walk, net);
1751         return 0;
1752 }
1753
1754 static int xfrm_dump_policy_start(struct netlink_callback *cb)
1755 {
1756         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1757
1758         BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
1759
1760         xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1761         return 0;
1762 }
1763
1764 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1765 {
1766         struct net *net = sock_net(skb->sk);
1767         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
1768         struct xfrm_dump_info info;
1769
1770         info.in_skb = cb->skb;
1771         info.out_skb = skb;
1772         info.nlmsg_seq = cb->nlh->nlmsg_seq;
1773         info.nlmsg_flags = NLM_F_MULTI;
1774
1775         (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
1776
1777         return skb->len;
1778 }
1779
1780 static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1781                                           struct xfrm_policy *xp,
1782                                           int dir, u32 seq)
1783 {
1784         struct xfrm_dump_info info;
1785         struct sk_buff *skb;
1786         int err;
1787
1788         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1789         if (!skb)
1790                 return ERR_PTR(-ENOMEM);
1791
1792         info.in_skb = in_skb;
1793         info.out_skb = skb;
1794         info.nlmsg_seq = seq;
1795         info.nlmsg_flags = 0;
1796
1797         err = dump_one_policy(xp, dir, 0, &info);
1798         if (err) {
1799                 kfree_skb(skb);
1800                 return ERR_PTR(err);
1801         }
1802
1803         return skb;
1804 }
1805
1806 static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
1807                 struct nlattr **attrs)
1808 {
1809         struct net *net = sock_net(skb->sk);
1810         struct xfrm_policy *xp;
1811         struct xfrm_userpolicy_id *p;
1812         u8 type = XFRM_POLICY_TYPE_MAIN;
1813         int err;
1814         struct km_event c;
1815         int delete;
1816         struct xfrm_mark m;
1817
1818         p = nlmsg_data(nlh);
1819         delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1820
1821         err = copy_from_user_policy_type(&type, attrs);
1822         if (err)
1823                 return err;
1824
1825         err = verify_policy_dir(p->dir);
1826         if (err)
1827                 return err;
1828
1829         xfrm_mark_get(attrs, &m);
1830
1831         if (p->index)
1832                 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, delete, &err);
1833         else {
1834                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
1835                 struct xfrm_sec_ctx *ctx;
1836
1837                 err = verify_sec_ctx_len(attrs);
1838                 if (err)
1839                         return err;
1840
1841                 ctx = NULL;
1842                 if (rt) {
1843                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
1844
1845                         err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
1846                         if (err)
1847                                 return err;
1848                 }
1849                 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir, &p->sel,
1850                                            ctx, delete, &err);
1851                 security_xfrm_policy_free(ctx);
1852         }
1853         if (xp == NULL)
1854                 return -ENOENT;
1855
1856         if (!delete) {
1857                 struct sk_buff *resp_skb;
1858
1859                 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1860                 if (IS_ERR(resp_skb)) {
1861                         err = PTR_ERR(resp_skb);
1862                 } else {
1863                         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
1864                                             NETLINK_CB(skb).portid);
1865                 }
1866         } else {
1867                 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
1868
1869                 if (err != 0)
1870                         goto out;
1871
1872                 c.data.byid = p->index;
1873                 c.event = nlh->nlmsg_type;
1874                 c.seq = nlh->nlmsg_seq;
1875                 c.portid = nlh->nlmsg_pid;
1876                 km_policy_notify(xp, p->dir, &c);
1877         }
1878
1879 out:
1880         xfrm_pol_put(xp);
1881         return err;
1882 }
1883
1884 static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
1885                 struct nlattr **attrs)
1886 {
1887         struct net *net = sock_net(skb->sk);
1888         struct km_event c;
1889         struct xfrm_usersa_flush *p = nlmsg_data(nlh);
1890         int err;
1891
1892         err = xfrm_state_flush(net, p->proto, true);
1893         if (err) {
1894                 if (err == -ESRCH) /* empty table */
1895                         return 0;
1896                 return err;
1897         }
1898         c.data.proto = p->proto;
1899         c.event = nlh->nlmsg_type;
1900         c.seq = nlh->nlmsg_seq;
1901         c.portid = nlh->nlmsg_pid;
1902         c.net = net;
1903         km_state_notify(NULL, &c);
1904
1905         return 0;
1906 }
1907
1908 static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
1909 {
1910         size_t replay_size = x->replay_esn ?
1911                               xfrm_replay_state_esn_len(x->replay_esn) :
1912                               sizeof(struct xfrm_replay_state);
1913
1914         return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1915                + nla_total_size(replay_size)
1916                + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
1917                + nla_total_size(sizeof(struct xfrm_mark))
1918                + nla_total_size(4) /* XFRM_AE_RTHR */
1919                + nla_total_size(4); /* XFRM_AE_ETHR */
1920 }
1921
1922 static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
1923 {
1924         struct xfrm_aevent_id *id;
1925         struct nlmsghdr *nlh;
1926         int err;
1927
1928         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1929         if (nlh == NULL)
1930                 return -EMSGSIZE;
1931
1932         id = nlmsg_data(nlh);
1933         memset(&id->sa_id, 0, sizeof(id->sa_id));
1934         memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
1935         id->sa_id.spi = x->id.spi;
1936         id->sa_id.family = x->props.family;
1937         id->sa_id.proto = x->id.proto;
1938         memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
1939         id->reqid = x->props.reqid;
1940         id->flags = c->data.aevent;
1941
1942         if (x->replay_esn) {
1943                 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1944                               xfrm_replay_state_esn_len(x->replay_esn),
1945                               x->replay_esn);
1946         } else {
1947                 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1948                               &x->replay);
1949         }
1950         if (err)
1951                 goto out_cancel;
1952         err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
1953                             XFRMA_PAD);
1954         if (err)
1955                 goto out_cancel;
1956
1957         if (id->flags & XFRM_AE_RTHR) {
1958                 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1959                 if (err)
1960                         goto out_cancel;
1961         }
1962         if (id->flags & XFRM_AE_ETHR) {
1963                 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1964                                   x->replay_maxage * 10 / HZ);
1965                 if (err)
1966                         goto out_cancel;
1967         }
1968         err = xfrm_mark_put(skb, &x->mark);
1969         if (err)
1970                 goto out_cancel;
1971
1972         nlmsg_end(skb, nlh);
1973         return 0;
1974
1975 out_cancel:
1976         nlmsg_cancel(skb, nlh);
1977         return err;
1978 }
1979
1980 static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
1981                 struct nlattr **attrs)
1982 {
1983         struct net *net = sock_net(skb->sk);
1984         struct xfrm_state *x;
1985         struct sk_buff *r_skb;
1986         int err;
1987         struct km_event c;
1988         u32 mark;
1989         struct xfrm_mark m;
1990         struct xfrm_aevent_id *p = nlmsg_data(nlh);
1991         struct xfrm_usersa_id *id = &p->sa_id;
1992
1993         mark = xfrm_mark_get(attrs, &m);
1994
1995         x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
1996         if (x == NULL)
1997                 return -ESRCH;
1998
1999         r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2000         if (r_skb == NULL) {
2001                 xfrm_state_put(x);
2002                 return -ENOMEM;
2003         }
2004
2005         /*
2006          * XXX: is this lock really needed - none of the other
2007          * gets lock (the concern is things getting updated
2008          * while we are still reading) - jhs
2009         */
2010         spin_lock_bh(&x->lock);
2011         c.data.aevent = p->flags;
2012         c.seq = nlh->nlmsg_seq;
2013         c.portid = nlh->nlmsg_pid;
2014
2015         if (build_aevent(r_skb, x, &c) < 0)
2016                 BUG();
2017         err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
2018         spin_unlock_bh(&x->lock);
2019         xfrm_state_put(x);
2020         return err;
2021 }
2022
2023 static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
2024                 struct nlattr **attrs)
2025 {
2026         struct net *net = sock_net(skb->sk);
2027         struct xfrm_state *x;
2028         struct km_event c;
2029         int err = -EINVAL;
2030         u32 mark = 0;
2031         struct xfrm_mark m;
2032         struct xfrm_aevent_id *p = nlmsg_data(nlh);
2033         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
2034         struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
2035         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
2036         struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
2037         struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
2038
2039         if (!lt && !rp && !re && !et && !rt)
2040                 return err;
2041
2042         /* pedantic mode - thou shalt sayeth replaceth */
2043         if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
2044                 return err;
2045
2046         mark = xfrm_mark_get(attrs, &m);
2047
2048         x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
2049         if (x == NULL)
2050                 return -ESRCH;
2051
2052         if (x->km.state != XFRM_STATE_VALID)
2053                 goto out;
2054
2055         err = xfrm_replay_verify_len(x->replay_esn, re);
2056         if (err)
2057                 goto out;
2058
2059         spin_lock_bh(&x->lock);
2060         xfrm_update_ae_params(x, attrs, 1);
2061         spin_unlock_bh(&x->lock);
2062
2063         c.event = nlh->nlmsg_type;
2064         c.seq = nlh->nlmsg_seq;
2065         c.portid = nlh->nlmsg_pid;
2066         c.data.aevent = XFRM_AE_CU;
2067         km_state_notify(x, &c);
2068         err = 0;
2069 out:
2070         xfrm_state_put(x);
2071         return err;
2072 }
2073
2074 static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
2075                 struct nlattr **attrs)
2076 {
2077         struct net *net = sock_net(skb->sk);
2078         struct km_event c;
2079         u8 type = XFRM_POLICY_TYPE_MAIN;
2080         int err;
2081
2082         err = copy_from_user_policy_type(&type, attrs);
2083         if (err)
2084                 return err;
2085
2086         err = xfrm_policy_flush(net, type, true);
2087         if (err) {
2088                 if (err == -ESRCH) /* empty table */
2089                         return 0;
2090                 return err;
2091         }
2092
2093         c.data.type = type;
2094         c.event = nlh->nlmsg_type;
2095         c.seq = nlh->nlmsg_seq;
2096         c.portid = nlh->nlmsg_pid;
2097         c.net = net;
2098         km_policy_notify(NULL, 0, &c);
2099         return 0;
2100 }
2101
2102 static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2103                 struct nlattr **attrs)
2104 {
2105         struct net *net = sock_net(skb->sk);
2106         struct xfrm_policy *xp;
2107         struct xfrm_user_polexpire *up = nlmsg_data(nlh);
2108         struct xfrm_userpolicy_info *p = &up->pol;
2109         u8 type = XFRM_POLICY_TYPE_MAIN;
2110         int err = -ENOENT;
2111         struct xfrm_mark m;
2112
2113         err = copy_from_user_policy_type(&type, attrs);
2114         if (err)
2115                 return err;
2116
2117         err = verify_policy_dir(p->dir);
2118         if (err)
2119                 return err;
2120
2121         xfrm_mark_get(attrs, &m);
2122
2123         if (p->index)
2124                 xp = xfrm_policy_byid(net, &m, type, p->dir, p->index, 0, &err);
2125         else {
2126                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
2127                 struct xfrm_sec_ctx *ctx;
2128
2129                 err = verify_sec_ctx_len(attrs);
2130                 if (err)
2131                         return err;
2132
2133                 ctx = NULL;
2134                 if (rt) {
2135                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
2136
2137                         err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
2138                         if (err)
2139                                 return err;
2140                 }
2141                 xp = xfrm_policy_bysel_ctx(net, &m, type, p->dir,
2142                                            &p->sel, ctx, 0, &err);
2143                 security_xfrm_policy_free(ctx);
2144         }
2145         if (xp == NULL)
2146                 return -ENOENT;
2147
2148         if (unlikely(xp->walk.dead))
2149                 goto out;
2150
2151         err = 0;
2152         if (up->hard) {
2153                 xfrm_policy_delete(xp, p->dir);
2154                 xfrm_audit_policy_delete(xp, 1, true);
2155         }
2156         km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
2157
2158 out:
2159         xfrm_pol_put(xp);
2160         return err;
2161 }
2162
2163 static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
2164                 struct nlattr **attrs)
2165 {
2166         struct net *net = sock_net(skb->sk);
2167         struct xfrm_state *x;
2168         int err;
2169         struct xfrm_user_expire *ue = nlmsg_data(nlh);
2170         struct xfrm_usersa_info *p = &ue->state;
2171         struct xfrm_mark m;
2172         u32 mark = xfrm_mark_get(attrs, &m);
2173
2174         x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
2175
2176         err = -ENOENT;
2177         if (x == NULL)
2178                 return err;
2179
2180         spin_lock_bh(&x->lock);
2181         err = -EINVAL;
2182         if (x->km.state != XFRM_STATE_VALID)
2183                 goto out;
2184         km_state_expired(x, ue->hard, nlh->nlmsg_pid);
2185
2186         if (ue->hard) {
2187                 __xfrm_state_delete(x);
2188                 xfrm_audit_state_delete(x, 1, true);
2189         }
2190         err = 0;
2191 out:
2192         spin_unlock_bh(&x->lock);
2193         xfrm_state_put(x);
2194         return err;
2195 }
2196
2197 static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
2198                 struct nlattr **attrs)
2199 {
2200         struct net *net = sock_net(skb->sk);
2201         struct xfrm_policy *xp;
2202         struct xfrm_user_tmpl *ut;
2203         int i;
2204         struct nlattr *rt = attrs[XFRMA_TMPL];
2205         struct xfrm_mark mark;
2206
2207         struct xfrm_user_acquire *ua = nlmsg_data(nlh);
2208         struct xfrm_state *x = xfrm_state_alloc(net);
2209         int err = -ENOMEM;
2210
2211         if (!x)
2212                 goto nomem;
2213
2214         xfrm_mark_get(attrs, &mark);
2215
2216         err = verify_newpolicy_info(&ua->policy);
2217         if (err)
2218                 goto free_state;
2219         err = verify_sec_ctx_len(attrs);
2220         if (err)
2221                 goto free_state;
2222
2223         /*   build an XP */
2224         xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
2225         if (!xp)
2226                 goto free_state;
2227
2228         memcpy(&x->id, &ua->id, sizeof(ua->id));
2229         memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2230         memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
2231         xp->mark.m = x->mark.m = mark.m;
2232         xp->mark.v = x->mark.v = mark.v;
2233         ut = nla_data(rt);
2234         /* extract the templates and for each call km_key */
2235         for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2236                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2237                 memcpy(&x->id, &t->id, sizeof(x->id));
2238                 x->props.mode = t->mode;
2239                 x->props.reqid = t->reqid;
2240                 x->props.family = ut->family;
2241                 t->aalgos = ua->aalgos;
2242                 t->ealgos = ua->ealgos;
2243                 t->calgos = ua->calgos;
2244                 err = km_query(x, t, xp);
2245
2246         }
2247
2248         kfree(x);
2249         kfree(xp);
2250
2251         return 0;
2252
2253 free_state:
2254         kfree(x);
2255 nomem:
2256         return err;
2257 }
2258
2259 #ifdef CONFIG_XFRM_MIGRATE
2260 static int copy_from_user_migrate(struct xfrm_migrate *ma,
2261                                   struct xfrm_kmaddress *k,
2262                                   struct nlattr **attrs, int *num)
2263 {
2264         struct nlattr *rt = attrs[XFRMA_MIGRATE];
2265         struct xfrm_user_migrate *um;
2266         int i, num_migrate;
2267
2268         if (k != NULL) {
2269                 struct xfrm_user_kmaddress *uk;
2270
2271                 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2272                 memcpy(&k->local, &uk->local, sizeof(k->local));
2273                 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2274                 k->family = uk->family;
2275                 k->reserved = uk->reserved;
2276         }
2277
2278         um = nla_data(rt);
2279         num_migrate = nla_len(rt) / sizeof(*um);
2280
2281         if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2282                 return -EINVAL;
2283
2284         for (i = 0; i < num_migrate; i++, um++, ma++) {
2285                 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2286                 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2287                 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2288                 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2289
2290                 ma->proto = um->proto;
2291                 ma->mode = um->mode;
2292                 ma->reqid = um->reqid;
2293
2294                 ma->old_family = um->old_family;
2295                 ma->new_family = um->new_family;
2296         }
2297
2298         *num = i;
2299         return 0;
2300 }
2301
2302 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2303                            struct nlattr **attrs)
2304 {
2305         struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
2306         struct xfrm_migrate m[XFRM_MAX_DEPTH];
2307         struct xfrm_kmaddress km, *kmp;
2308         u8 type;
2309         int err;
2310         int n = 0;
2311         struct net *net = sock_net(skb->sk);
2312         struct xfrm_encap_tmpl  *encap = NULL;
2313
2314         if (attrs[XFRMA_MIGRATE] == NULL)
2315                 return -EINVAL;
2316
2317         kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2318
2319         err = copy_from_user_policy_type(&type, attrs);
2320         if (err)
2321                 return err;
2322
2323         err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
2324         if (err)
2325                 return err;
2326
2327         if (!n)
2328                 return 0;
2329
2330         if (attrs[XFRMA_ENCAP]) {
2331                 encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
2332                                 sizeof(*encap), GFP_KERNEL);
2333                 if (!encap)
2334                         return 0;
2335         }
2336
2337         err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
2338
2339         kfree(encap);
2340
2341         return err;
2342 }
2343 #else
2344 static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
2345                            struct nlattr **attrs)
2346 {
2347         return -ENOPROTOOPT;
2348 }
2349 #endif
2350
2351 #ifdef CONFIG_XFRM_MIGRATE
2352 static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
2353 {
2354         struct xfrm_user_migrate um;
2355
2356         memset(&um, 0, sizeof(um));
2357         um.proto = m->proto;
2358         um.mode = m->mode;
2359         um.reqid = m->reqid;
2360         um.old_family = m->old_family;
2361         memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2362         memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2363         um.new_family = m->new_family;
2364         memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2365         memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2366
2367         return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
2368 }
2369
2370 static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
2371 {
2372         struct xfrm_user_kmaddress uk;
2373
2374         memset(&uk, 0, sizeof(uk));
2375         uk.family = k->family;
2376         uk.reserved = k->reserved;
2377         memcpy(&uk.local, &k->local, sizeof(uk.local));
2378         memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
2379
2380         return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2381 }
2382
2383 static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
2384                                           int with_encp)
2385 {
2386         return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
2387               + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2388               + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
2389               + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2390               + userpolicy_type_attrsize();
2391 }
2392
2393 static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2394                          int num_migrate, const struct xfrm_kmaddress *k,
2395                          const struct xfrm_selector *sel,
2396                          const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
2397 {
2398         const struct xfrm_migrate *mp;
2399         struct xfrm_userpolicy_id *pol_id;
2400         struct nlmsghdr *nlh;
2401         int i, err;
2402
2403         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2404         if (nlh == NULL)
2405                 return -EMSGSIZE;
2406
2407         pol_id = nlmsg_data(nlh);
2408         /* copy data from selector, dir, and type to the pol_id */
2409         memset(pol_id, 0, sizeof(*pol_id));
2410         memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2411         pol_id->dir = dir;
2412
2413         if (k != NULL) {
2414                 err = copy_to_user_kmaddress(k, skb);
2415                 if (err)
2416                         goto out_cancel;
2417         }
2418         if (encap) {
2419                 err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
2420                 if (err)
2421                         goto out_cancel;
2422         }
2423         err = copy_to_user_policy_type(type, skb);
2424         if (err)
2425                 goto out_cancel;
2426         for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2427                 err = copy_to_user_migrate(mp, skb);
2428                 if (err)
2429                         goto out_cancel;
2430         }
2431
2432         nlmsg_end(skb, nlh);
2433         return 0;
2434
2435 out_cancel:
2436         nlmsg_cancel(skb, nlh);
2437         return err;
2438 }
2439
2440 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2441                              const struct xfrm_migrate *m, int num_migrate,
2442                              const struct xfrm_kmaddress *k,
2443                              const struct xfrm_encap_tmpl *encap)
2444 {
2445         struct net *net = &init_net;
2446         struct sk_buff *skb;
2447
2448         skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
2449                         GFP_ATOMIC);
2450         if (skb == NULL)
2451                 return -ENOMEM;
2452
2453         /* build migrate */
2454         if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
2455                 BUG();
2456
2457         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
2458 }
2459 #else
2460 static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2461                              const struct xfrm_migrate *m, int num_migrate,
2462                              const struct xfrm_kmaddress *k,
2463                              const struct xfrm_encap_tmpl *encap)
2464 {
2465         return -ENOPROTOOPT;
2466 }
2467 #endif
2468
2469 #define XMSGSIZE(type) sizeof(struct type)
2470
2471 static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
2472         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2473         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2474         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2475         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2476         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2477         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2478         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
2479         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
2480         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
2481         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2482         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
2483         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
2484         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
2485         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
2486         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2487         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2488         [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
2489         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2490         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
2491         [XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2492         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
2493 };
2494
2495 #undef XMSGSIZE
2496
2497 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
2498         [XFRMA_SA]              = { .len = sizeof(struct xfrm_usersa_info)},
2499         [XFRMA_POLICY]          = { .len = sizeof(struct xfrm_userpolicy_info)},
2500         [XFRMA_LASTUSED]        = { .type = NLA_U64},
2501         [XFRMA_ALG_AUTH_TRUNC]  = { .len = sizeof(struct xfrm_algo_auth)},
2502         [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
2503         [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
2504         [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
2505         [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
2506         [XFRMA_ENCAP]           = { .len = sizeof(struct xfrm_encap_tmpl) },
2507         [XFRMA_TMPL]            = { .len = sizeof(struct xfrm_user_tmpl) },
2508         [XFRMA_SEC_CTX]         = { .len = sizeof(struct xfrm_sec_ctx) },
2509         [XFRMA_LTIME_VAL]       = { .len = sizeof(struct xfrm_lifetime_cur) },
2510         [XFRMA_REPLAY_VAL]      = { .len = sizeof(struct xfrm_replay_state) },
2511         [XFRMA_REPLAY_THRESH]   = { .type = NLA_U32 },
2512         [XFRMA_ETIMER_THRESH]   = { .type = NLA_U32 },
2513         [XFRMA_SRCADDR]         = { .len = sizeof(xfrm_address_t) },
2514         [XFRMA_COADDR]          = { .len = sizeof(xfrm_address_t) },
2515         [XFRMA_POLICY_TYPE]     = { .len = sizeof(struct xfrm_userpolicy_type)},
2516         [XFRMA_MIGRATE]         = { .len = sizeof(struct xfrm_user_migrate) },
2517         [XFRMA_KMADDRESS]       = { .len = sizeof(struct xfrm_user_kmaddress) },
2518         [XFRMA_MARK]            = { .len = sizeof(struct xfrm_mark) },
2519         [XFRMA_TFCPAD]          = { .type = NLA_U32 },
2520         [XFRMA_REPLAY_ESN_VAL]  = { .len = sizeof(struct xfrm_replay_state_esn) },
2521         [XFRMA_SA_EXTRA_FLAGS]  = { .type = NLA_U32 },
2522         [XFRMA_PROTO]           = { .type = NLA_U8 },
2523         [XFRMA_ADDRESS_FILTER]  = { .len = sizeof(struct xfrm_address_filter) },
2524         [XFRMA_OFFLOAD_DEV]     = { .len = sizeof(struct xfrm_user_offload) },
2525         [XFRMA_OUTPUT_MARK]     = { .len = NLA_U32 },
2526 };
2527
2528 static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
2529         [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2530         [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
2531 };
2532
2533 static const struct xfrm_link {
2534         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
2535         int (*start)(struct netlink_callback *);
2536         int (*dump)(struct sk_buff *, struct netlink_callback *);
2537         int (*done)(struct netlink_callback *);
2538         const struct nla_policy *nla_pol;
2539         int nla_max;
2540 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2541         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2542         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
2543         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
2544                                                    .dump = xfrm_dump_sa,
2545                                                    .done = xfrm_dump_sa_done  },
2546         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2547         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
2548         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
2549                                                    .start = xfrm_dump_policy_start,
2550                                                    .dump = xfrm_dump_policy,
2551                                                    .done = xfrm_dump_policy_done },
2552         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
2553         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
2554         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
2555         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
2556         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
2557         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
2558         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
2559         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
2560         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
2561         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
2562         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
2563         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
2564         [XFRM_MSG_NEWSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
2565                                                    .nla_pol = xfrma_spd_policy,
2566                                                    .nla_max = XFRMA_SPD_MAX },
2567         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
2568 };
2569
2570 static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
2571                              struct netlink_ext_ack *extack)
2572 {
2573         struct net *net = sock_net(skb->sk);
2574         struct nlattr *attrs[XFRMA_MAX+1];
2575         const struct xfrm_link *link;
2576         int type, err;
2577
2578 #ifdef CONFIG_COMPAT
2579         if (in_compat_syscall())
2580                 return -EOPNOTSUPP;
2581 #endif
2582
2583         type = nlh->nlmsg_type;
2584         if (type > XFRM_MSG_MAX)
2585                 return -EINVAL;
2586
2587         type -= XFRM_MSG_BASE;
2588         link = &xfrm_dispatch[type];
2589
2590         /* All operations require privileges, even GET */
2591         if (!netlink_net_capable(skb, CAP_NET_ADMIN))
2592                 return -EPERM;
2593
2594         if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2595              type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2596             (nlh->nlmsg_flags & NLM_F_DUMP)) {
2597                 if (link->dump == NULL)
2598                         return -EINVAL;
2599
2600                 {
2601                         struct netlink_dump_control c = {
2602                                 .start = link->start,
2603                                 .dump = link->dump,
2604                                 .done = link->done,
2605                         };
2606                         return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2607                 }
2608         }
2609
2610         err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
2611                           link->nla_max ? : XFRMA_MAX,
2612                           link->nla_pol ? : xfrma_policy, extack);
2613         if (err < 0)
2614                 return err;
2615
2616         if (link->doit == NULL)
2617                 return -EINVAL;
2618
2619         return link->doit(skb, nlh, attrs);
2620 }
2621
2622 static void xfrm_netlink_rcv(struct sk_buff *skb)
2623 {
2624         struct net *net = sock_net(skb->sk);
2625
2626         mutex_lock(&net->xfrm.xfrm_cfg_mutex);
2627         netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2628         mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
2629 }
2630
2631 static inline size_t xfrm_expire_msgsize(void)
2632 {
2633         return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2634                + nla_total_size(sizeof(struct xfrm_mark));
2635 }
2636
2637 static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
2638 {
2639         struct xfrm_user_expire *ue;
2640         struct nlmsghdr *nlh;
2641         int err;
2642
2643         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2644         if (nlh == NULL)
2645                 return -EMSGSIZE;
2646
2647         ue = nlmsg_data(nlh);
2648         copy_to_user_state(x, &ue->state);
2649         ue->hard = (c->data.hard != 0) ? 1 : 0;
2650         /* clear the padding bytes */
2651         memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
2652
2653         err = xfrm_mark_put(skb, &x->mark);
2654         if (err)
2655                 return err;
2656
2657         nlmsg_end(skb, nlh);
2658         return 0;
2659 }
2660
2661 static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
2662 {
2663         struct net *net = xs_net(x);
2664         struct sk_buff *skb;
2665
2666         skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
2667         if (skb == NULL)
2668                 return -ENOMEM;
2669
2670         if (build_expire(skb, x, c) < 0) {
2671                 kfree_skb(skb);
2672                 return -EMSGSIZE;
2673         }
2674
2675         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
2676 }
2677
2678 static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
2679 {
2680         struct net *net = xs_net(x);
2681         struct sk_buff *skb;
2682
2683         skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
2684         if (skb == NULL)
2685                 return -ENOMEM;
2686
2687         if (build_aevent(skb, x, c) < 0)
2688                 BUG();
2689
2690         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
2691 }
2692
2693 static int xfrm_notify_sa_flush(const struct km_event *c)
2694 {
2695         struct net *net = c->net;
2696         struct xfrm_usersa_flush *p;
2697         struct nlmsghdr *nlh;
2698         struct sk_buff *skb;
2699         int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
2700
2701         skb = nlmsg_new(len, GFP_ATOMIC);
2702         if (skb == NULL)
2703                 return -ENOMEM;
2704
2705         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2706         if (nlh == NULL) {
2707                 kfree_skb(skb);
2708                 return -EMSGSIZE;
2709         }
2710
2711         p = nlmsg_data(nlh);
2712         p->proto = c->data.proto;
2713
2714         nlmsg_end(skb, nlh);
2715
2716         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2717 }
2718
2719 static inline size_t xfrm_sa_len(struct xfrm_state *x)
2720 {
2721         size_t l = 0;
2722         if (x->aead)
2723                 l += nla_total_size(aead_len(x->aead));
2724         if (x->aalg) {
2725                 l += nla_total_size(sizeof(struct xfrm_algo) +
2726                                     (x->aalg->alg_key_len + 7) / 8);
2727                 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2728         }
2729         if (x->ealg)
2730                 l += nla_total_size(xfrm_alg_len(x->ealg));
2731         if (x->calg)
2732                 l += nla_total_size(sizeof(*x->calg));
2733         if (x->encap)
2734                 l += nla_total_size(sizeof(*x->encap));
2735         if (x->tfcpad)
2736                 l += nla_total_size(sizeof(x->tfcpad));
2737         if (x->replay_esn)
2738                 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
2739         else
2740                 l += nla_total_size(sizeof(struct xfrm_replay_state));
2741         if (x->security)
2742                 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2743                                     x->security->ctx_len);
2744         if (x->coaddr)
2745                 l += nla_total_size(sizeof(*x->coaddr));
2746         if (x->props.extra_flags)
2747                 l += nla_total_size(sizeof(x->props.extra_flags));
2748         if (x->xso.dev)
2749                  l += nla_total_size(sizeof(x->xso));
2750         if (x->props.output_mark)
2751                 l += nla_total_size(sizeof(x->props.output_mark));
2752
2753         /* Must count x->lastused as it may become non-zero behind our back. */
2754         l += nla_total_size_64bit(sizeof(u64));
2755
2756         return l;
2757 }
2758
2759 static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
2760 {
2761         struct net *net = xs_net(x);
2762         struct xfrm_usersa_info *p;
2763         struct xfrm_usersa_id *id;
2764         struct nlmsghdr *nlh;
2765         struct sk_buff *skb;
2766         int len = xfrm_sa_len(x);
2767         int headlen, err;
2768
2769         headlen = sizeof(*p);
2770         if (c->event == XFRM_MSG_DELSA) {
2771                 len += nla_total_size(headlen);
2772                 headlen = sizeof(*id);
2773                 len += nla_total_size(sizeof(struct xfrm_mark));
2774         }
2775         len += NLMSG_ALIGN(headlen);
2776
2777         skb = nlmsg_new(len, GFP_ATOMIC);
2778         if (skb == NULL)
2779                 return -ENOMEM;
2780
2781         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
2782         err = -EMSGSIZE;
2783         if (nlh == NULL)
2784                 goto out_free_skb;
2785
2786         p = nlmsg_data(nlh);
2787         if (c->event == XFRM_MSG_DELSA) {
2788                 struct nlattr *attr;
2789
2790                 id = nlmsg_data(nlh);
2791                 memset(id, 0, sizeof(*id));
2792                 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2793                 id->spi = x->id.spi;
2794                 id->family = x->props.family;
2795                 id->proto = x->id.proto;
2796
2797                 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2798                 err = -EMSGSIZE;
2799                 if (attr == NULL)
2800                         goto out_free_skb;
2801
2802                 p = nla_data(attr);
2803         }
2804         err = copy_to_user_state_extra(x, p, skb);
2805         if (err)
2806                 goto out_free_skb;
2807
2808         nlmsg_end(skb, nlh);
2809
2810         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
2811
2812 out_free_skb:
2813         kfree_skb(skb);
2814         return err;
2815 }
2816
2817 static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
2818 {
2819
2820         switch (c->event) {
2821         case XFRM_MSG_EXPIRE:
2822                 return xfrm_exp_state_notify(x, c);
2823         case XFRM_MSG_NEWAE:
2824                 return xfrm_aevent_state_notify(x, c);
2825         case XFRM_MSG_DELSA:
2826         case XFRM_MSG_UPDSA:
2827         case XFRM_MSG_NEWSA:
2828                 return xfrm_notify_sa(x, c);
2829         case XFRM_MSG_FLUSHSA:
2830                 return xfrm_notify_sa_flush(c);
2831         default:
2832                 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2833                        c->event);
2834                 break;
2835         }
2836
2837         return 0;
2838
2839 }
2840
2841 static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2842                                           struct xfrm_policy *xp)
2843 {
2844         return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2845                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2846                + nla_total_size(sizeof(struct xfrm_mark))
2847                + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2848                + userpolicy_type_attrsize();
2849 }
2850
2851 static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2852                          struct xfrm_tmpl *xt, struct xfrm_policy *xp)
2853 {
2854         __u32 seq = xfrm_get_acqseq();
2855         struct xfrm_user_acquire *ua;
2856         struct nlmsghdr *nlh;
2857         int err;
2858
2859         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2860         if (nlh == NULL)
2861                 return -EMSGSIZE;
2862
2863         ua = nlmsg_data(nlh);
2864         memcpy(&ua->id, &x->id, sizeof(ua->id));
2865         memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2866         memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2867         copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
2868         ua->aalgos = xt->aalgos;
2869         ua->ealgos = xt->ealgos;
2870         ua->calgos = xt->calgos;
2871         ua->seq = x->km.seq = seq;
2872
2873         err = copy_to_user_tmpl(xp, skb);
2874         if (!err)
2875                 err = copy_to_user_state_sec_ctx(x, skb);
2876         if (!err)
2877                 err = copy_to_user_policy_type(xp->type, skb);
2878         if (!err)
2879                 err = xfrm_mark_put(skb, &xp->mark);
2880         if (err) {
2881                 nlmsg_cancel(skb, nlh);
2882                 return err;
2883         }
2884
2885         nlmsg_end(skb, nlh);
2886         return 0;
2887 }
2888
2889 static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2890                              struct xfrm_policy *xp)
2891 {
2892         struct net *net = xs_net(x);
2893         struct sk_buff *skb;
2894
2895         skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
2896         if (skb == NULL)
2897                 return -ENOMEM;
2898
2899         if (build_acquire(skb, x, xt, xp) < 0)
2900                 BUG();
2901
2902         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
2903 }
2904
2905 /* User gives us xfrm_user_policy_info followed by an array of 0
2906  * or more templates.
2907  */
2908 static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
2909                                                u8 *data, int len, int *dir)
2910 {
2911         struct net *net = sock_net(sk);
2912         struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2913         struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2914         struct xfrm_policy *xp;
2915         int nr;
2916
2917         switch (sk->sk_family) {
2918         case AF_INET:
2919                 if (opt != IP_XFRM_POLICY) {
2920                         *dir = -EOPNOTSUPP;
2921                         return NULL;
2922                 }
2923                 break;
2924 #if IS_ENABLED(CONFIG_IPV6)
2925         case AF_INET6:
2926                 if (opt != IPV6_XFRM_POLICY) {
2927                         *dir = -EOPNOTSUPP;
2928                         return NULL;
2929                 }
2930                 break;
2931 #endif
2932         default:
2933                 *dir = -EINVAL;
2934                 return NULL;
2935         }
2936
2937         *dir = -EINVAL;
2938
2939         if (len < sizeof(*p) ||
2940             verify_newpolicy_info(p))
2941                 return NULL;
2942
2943         nr = ((len - sizeof(*p)) / sizeof(*ut));
2944         if (validate_tmpl(nr, ut, p->sel.family))
2945                 return NULL;
2946
2947         if (p->dir > XFRM_POLICY_OUT)
2948                 return NULL;
2949
2950         xp = xfrm_policy_alloc(net, GFP_ATOMIC);
2951         if (xp == NULL) {
2952                 *dir = -ENOBUFS;
2953                 return NULL;
2954         }
2955
2956         copy_from_user_policy(xp, p);
2957         xp->type = XFRM_POLICY_TYPE_MAIN;
2958         copy_templates(xp, ut, nr);
2959
2960         *dir = p->dir;
2961
2962         return xp;
2963 }
2964
2965 static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2966 {
2967         return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2968                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2969                + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2970                + nla_total_size(sizeof(struct xfrm_mark))
2971                + userpolicy_type_attrsize();
2972 }
2973
2974 static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
2975                            int dir, const struct km_event *c)
2976 {
2977         struct xfrm_user_polexpire *upe;
2978         int hard = c->data.hard;
2979         struct nlmsghdr *nlh;
2980         int err;
2981
2982         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2983         if (nlh == NULL)
2984                 return -EMSGSIZE;
2985
2986         upe = nlmsg_data(nlh);
2987         copy_to_user_policy(xp, &upe->pol, dir);
2988         err = copy_to_user_tmpl(xp, skb);
2989         if (!err)
2990                 err = copy_to_user_sec_ctx(xp, skb);
2991         if (!err)
2992                 err = copy_to_user_policy_type(xp->type, skb);
2993         if (!err)
2994                 err = xfrm_mark_put(skb, &xp->mark);
2995         if (err) {
2996                 nlmsg_cancel(skb, nlh);
2997                 return err;
2998         }
2999         upe->hard = !!hard;
3000
3001         nlmsg_end(skb, nlh);
3002         return 0;
3003 }
3004
3005 static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3006 {
3007         struct net *net = xp_net(xp);
3008         struct sk_buff *skb;
3009
3010         skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
3011         if (skb == NULL)
3012                 return -ENOMEM;
3013
3014         if (build_polexpire(skb, xp, dir, c) < 0)
3015                 BUG();
3016
3017         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
3018 }
3019
3020 static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
3021 {
3022         int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
3023         struct net *net = xp_net(xp);
3024         struct xfrm_userpolicy_info *p;
3025         struct xfrm_userpolicy_id *id;
3026         struct nlmsghdr *nlh;
3027         struct sk_buff *skb;
3028         int headlen, err;
3029
3030         headlen = sizeof(*p);
3031         if (c->event == XFRM_MSG_DELPOLICY) {
3032                 len += nla_total_size(headlen);
3033                 headlen = sizeof(*id);
3034         }
3035         len += userpolicy_type_attrsize();
3036         len += nla_total_size(sizeof(struct xfrm_mark));
3037         len += NLMSG_ALIGN(headlen);
3038
3039         skb = nlmsg_new(len, GFP_ATOMIC);
3040         if (skb == NULL)
3041                 return -ENOMEM;
3042
3043         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
3044         err = -EMSGSIZE;
3045         if (nlh == NULL)
3046                 goto out_free_skb;
3047
3048         p = nlmsg_data(nlh);
3049         if (c->event == XFRM_MSG_DELPOLICY) {
3050                 struct nlattr *attr;
3051
3052                 id = nlmsg_data(nlh);
3053                 memset(id, 0, sizeof(*id));
3054                 id->dir = dir;
3055                 if (c->data.byid)
3056                         id->index = xp->index;
3057                 else
3058                         memcpy(&id->sel, &xp->selector, sizeof(id->sel));
3059
3060                 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
3061                 err = -EMSGSIZE;
3062                 if (attr == NULL)
3063                         goto out_free_skb;
3064
3065                 p = nla_data(attr);
3066         }
3067
3068         copy_to_user_policy(xp, p, dir);
3069         err = copy_to_user_tmpl(xp, skb);
3070         if (!err)
3071                 err = copy_to_user_policy_type(xp->type, skb);
3072         if (!err)
3073                 err = xfrm_mark_put(skb, &xp->mark);
3074         if (err)
3075                 goto out_free_skb;
3076
3077         nlmsg_end(skb, nlh);
3078
3079         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3080
3081 out_free_skb:
3082         kfree_skb(skb);
3083         return err;
3084 }
3085
3086 static int xfrm_notify_policy_flush(const struct km_event *c)
3087 {
3088         struct net *net = c->net;
3089         struct nlmsghdr *nlh;
3090         struct sk_buff *skb;
3091         int err;
3092
3093         skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
3094         if (skb == NULL)
3095                 return -ENOMEM;
3096
3097         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
3098         err = -EMSGSIZE;
3099         if (nlh == NULL)
3100                 goto out_free_skb;
3101         err = copy_to_user_policy_type(c->data.type, skb);
3102         if (err)
3103                 goto out_free_skb;
3104
3105         nlmsg_end(skb, nlh);
3106
3107         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
3108
3109 out_free_skb:
3110         kfree_skb(skb);
3111         return err;
3112 }
3113
3114 static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
3115 {
3116
3117         switch (c->event) {
3118         case XFRM_MSG_NEWPOLICY:
3119         case XFRM_MSG_UPDPOLICY:
3120         case XFRM_MSG_DELPOLICY:
3121                 return xfrm_notify_policy(xp, dir, c);
3122         case XFRM_MSG_FLUSHPOLICY:
3123                 return xfrm_notify_policy_flush(c);
3124         case XFRM_MSG_POLEXPIRE:
3125                 return xfrm_exp_policy_notify(xp, dir, c);
3126         default:
3127                 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
3128                        c->event);
3129         }
3130
3131         return 0;
3132
3133 }
3134
3135 static inline size_t xfrm_report_msgsize(void)
3136 {
3137         return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
3138 }
3139
3140 static int build_report(struct sk_buff *skb, u8 proto,
3141                         struct xfrm_selector *sel, xfrm_address_t *addr)
3142 {
3143         struct xfrm_user_report *ur;
3144         struct nlmsghdr *nlh;
3145
3146         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
3147         if (nlh == NULL)
3148                 return -EMSGSIZE;
3149
3150         ur = nlmsg_data(nlh);
3151         ur->proto = proto;
3152         memcpy(&ur->sel, sel, sizeof(ur->sel));
3153
3154         if (addr) {
3155                 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
3156                 if (err) {
3157                         nlmsg_cancel(skb, nlh);
3158                         return err;
3159                 }
3160         }
3161         nlmsg_end(skb, nlh);
3162         return 0;
3163 }
3164
3165 static int xfrm_send_report(struct net *net, u8 proto,
3166                             struct xfrm_selector *sel, xfrm_address_t *addr)
3167 {
3168         struct sk_buff *skb;
3169
3170         skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
3171         if (skb == NULL)
3172                 return -ENOMEM;
3173
3174         if (build_report(skb, proto, sel, addr) < 0)
3175                 BUG();
3176
3177         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
3178 }
3179
3180 static inline size_t xfrm_mapping_msgsize(void)
3181 {
3182         return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
3183 }
3184
3185 static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
3186                          xfrm_address_t *new_saddr, __be16 new_sport)
3187 {
3188         struct xfrm_user_mapping *um;
3189         struct nlmsghdr *nlh;
3190
3191         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
3192         if (nlh == NULL)
3193                 return -EMSGSIZE;
3194
3195         um = nlmsg_data(nlh);
3196
3197         memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
3198         um->id.spi = x->id.spi;
3199         um->id.family = x->props.family;
3200         um->id.proto = x->id.proto;
3201         memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
3202         memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
3203         um->new_sport = new_sport;
3204         um->old_sport = x->encap->encap_sport;
3205         um->reqid = x->props.reqid;
3206
3207         nlmsg_end(skb, nlh);
3208         return 0;
3209 }
3210
3211 static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
3212                              __be16 sport)
3213 {
3214         struct net *net = xs_net(x);
3215         struct sk_buff *skb;
3216
3217         if (x->id.proto != IPPROTO_ESP)
3218                 return -EINVAL;
3219
3220         if (!x->encap)
3221                 return -EINVAL;
3222
3223         skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3224         if (skb == NULL)
3225                 return -ENOMEM;
3226
3227         if (build_mapping(skb, x, ipaddr, sport) < 0)
3228                 BUG();
3229
3230         return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
3231 }
3232
3233 static bool xfrm_is_alive(const struct km_event *c)
3234 {
3235         return (bool)xfrm_acquire_is_on(c->net);
3236 }
3237
3238 static struct xfrm_mgr netlink_mgr = {
3239         .notify         = xfrm_send_state_notify,
3240         .acquire        = xfrm_send_acquire,
3241         .compile_policy = xfrm_compile_policy,
3242         .notify_policy  = xfrm_send_policy_notify,
3243         .report         = xfrm_send_report,
3244         .migrate        = xfrm_send_migrate,
3245         .new_mapping    = xfrm_send_mapping,
3246         .is_alive       = xfrm_is_alive,
3247 };
3248
3249 static int __net_init xfrm_user_net_init(struct net *net)
3250 {
3251         struct sock *nlsk;
3252         struct netlink_kernel_cfg cfg = {
3253                 .groups = XFRMNLGRP_MAX,
3254                 .input  = xfrm_netlink_rcv,
3255         };
3256
3257         nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
3258         if (nlsk == NULL)
3259                 return -ENOMEM;
3260         net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
3261         rcu_assign_pointer(net->xfrm.nlsk, nlsk);
3262         return 0;
3263 }
3264
3265 static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
3266 {
3267         struct net *net;
3268         list_for_each_entry(net, net_exit_list, exit_list)
3269                 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
3270         synchronize_net();
3271         list_for_each_entry(net, net_exit_list, exit_list)
3272                 netlink_kernel_release(net->xfrm.nlsk_stash);
3273 }
3274
3275 static struct pernet_operations xfrm_user_net_ops = {
3276         .init       = xfrm_user_net_init,
3277         .exit_batch = xfrm_user_net_exit,
3278 };
3279
3280 static int __init xfrm_user_init(void)
3281 {
3282         int rv;
3283
3284         printk(KERN_INFO "Initializing XFRM netlink socket\n");
3285
3286         rv = register_pernet_subsys(&xfrm_user_net_ops);
3287         if (rv < 0)
3288                 return rv;
3289         rv = xfrm_register_km(&netlink_mgr);
3290         if (rv < 0)
3291                 unregister_pernet_subsys(&xfrm_user_net_ops);
3292         return rv;
3293 }
3294
3295 static void __exit xfrm_user_exit(void)
3296 {
3297         xfrm_unregister_km(&netlink_mgr);
3298         unregister_pernet_subsys(&xfrm_user_net_ops);
3299 }
3300
3301 module_init(xfrm_user_init);
3302 module_exit(xfrm_user_exit);
3303 MODULE_LICENSE("GPL");
3304 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
3305