OSDN Git Service

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