Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/net/xfrm/xfrm_user.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us