~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/xfrm/xfrm_user.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp