OSDN Git Service

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