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