OSDN Git Service

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