OSDN Git Service

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