1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * xfrm_state.c
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 * YOSHIFUJI Hideaki @USAGI
11 * Split up af-specific functions
12 * Derek Atkins <derek@ihtfp.com>
13 * Add UDP Encapsulation
14 *
15 */
16
17 #include <linux/workqueue.h>
18 #include <net/xfrm.h>
19 #include <linux/pfkeyv2.h>
20 #include <linux/ipsec.h>
21 #include <linux/module.h>
22 #include <linux/cache.h>
23 #include <linux/audit.h>
24 #include <linux/uaccess.h>
25 #include <linux/ktime.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/kernel.h>
29
30 #include <crypto/aead.h>
31
32 #include "xfrm_hash.h"
33
34 #define xfrm_state_deref_prot(table, net) \
35 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
36
37 static void xfrm_state_gc_task(struct work_struct *work);
38
39 /* Each xfrm_state may be linked to two tables:
40
41 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
42 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
43 destination/tunnel endpoint. (output)
44 */
45
46 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
47 static struct kmem_cache *xfrm_state_cache __ro_after_init;
48
49 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
50 static HLIST_HEAD(xfrm_state_gc_list);
51
xfrm_state_hold_rcu(struct xfrm_state __rcu * x)52 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
53 {
54 return refcount_inc_not_zero(&x->refcnt);
55 }
56
xfrm_dst_hash(struct net * net,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u32 reqid,unsigned short family)57 static inline unsigned int xfrm_dst_hash(struct net *net,
58 const xfrm_address_t *daddr,
59 const xfrm_address_t *saddr,
60 u32 reqid,
61 unsigned short family)
62 {
63 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
64 }
65
xfrm_src_hash(struct net * net,const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family)66 static inline unsigned int xfrm_src_hash(struct net *net,
67 const xfrm_address_t *daddr,
68 const xfrm_address_t *saddr,
69 unsigned short family)
70 {
71 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
72 }
73
74 static inline unsigned int
xfrm_spi_hash(struct net * net,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)75 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
76 __be32 spi, u8 proto, unsigned short family)
77 {
78 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
79 }
80
xfrm_hash_transfer(struct hlist_head * list,struct hlist_head * ndsttable,struct hlist_head * nsrctable,struct hlist_head * nspitable,unsigned int nhashmask)81 static void xfrm_hash_transfer(struct hlist_head *list,
82 struct hlist_head *ndsttable,
83 struct hlist_head *nsrctable,
84 struct hlist_head *nspitable,
85 unsigned int nhashmask)
86 {
87 struct hlist_node *tmp;
88 struct xfrm_state *x;
89
90 hlist_for_each_entry_safe(x, tmp, list, bydst) {
91 unsigned int h;
92
93 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
94 x->props.reqid, x->props.family,
95 nhashmask);
96 hlist_add_head_rcu(&x->bydst, ndsttable + h);
97
98 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
99 x->props.family,
100 nhashmask);
101 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
102
103 if (x->id.spi) {
104 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
105 x->id.proto, x->props.family,
106 nhashmask);
107 hlist_add_head_rcu(&x->byspi, nspitable + h);
108 }
109 }
110 }
111
xfrm_hash_new_size(unsigned int state_hmask)112 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
113 {
114 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
115 }
116
xfrm_hash_resize(struct work_struct * work)117 static void xfrm_hash_resize(struct work_struct *work)
118 {
119 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
120 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
121 unsigned long nsize, osize;
122 unsigned int nhashmask, ohashmask;
123 int i;
124
125 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
126 ndst = xfrm_hash_alloc(nsize);
127 if (!ndst)
128 return;
129 nsrc = xfrm_hash_alloc(nsize);
130 if (!nsrc) {
131 xfrm_hash_free(ndst, nsize);
132 return;
133 }
134 nspi = xfrm_hash_alloc(nsize);
135 if (!nspi) {
136 xfrm_hash_free(ndst, nsize);
137 xfrm_hash_free(nsrc, nsize);
138 return;
139 }
140
141 spin_lock_bh(&net->xfrm.xfrm_state_lock);
142 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
143
144 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
145 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
146 for (i = net->xfrm.state_hmask; i >= 0; i--)
147 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
148
149 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
150 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
151 ohashmask = net->xfrm.state_hmask;
152
153 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
154 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
155 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
156 net->xfrm.state_hmask = nhashmask;
157
158 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
159 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
160
161 osize = (ohashmask + 1) * sizeof(struct hlist_head);
162
163 synchronize_rcu();
164
165 xfrm_hash_free(odst, osize);
166 xfrm_hash_free(osrc, osize);
167 xfrm_hash_free(ospi, osize);
168 }
169
170 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
171 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
172
173 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
174
175 int __xfrm_state_delete(struct xfrm_state *x);
176
177 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
178 static bool km_is_alive(const struct km_event *c);
179 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
180
xfrm_register_type(const struct xfrm_type * type,unsigned short family)181 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
182 {
183 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
184 int err = 0;
185
186 if (!afinfo)
187 return -EAFNOSUPPORT;
188
189 #define X(afi, T, name) do { \
190 WARN_ON((afi)->type_ ## name); \
191 (afi)->type_ ## name = (T); \
192 } while (0)
193
194 switch (type->proto) {
195 case IPPROTO_COMP:
196 X(afinfo, type, comp);
197 break;
198 case IPPROTO_AH:
199 X(afinfo, type, ah);
200 break;
201 case IPPROTO_ESP:
202 X(afinfo, type, esp);
203 break;
204 case IPPROTO_IPIP:
205 X(afinfo, type, ipip);
206 break;
207 case IPPROTO_DSTOPTS:
208 X(afinfo, type, dstopts);
209 break;
210 case IPPROTO_ROUTING:
211 X(afinfo, type, routing);
212 break;
213 case IPPROTO_IPV6:
214 X(afinfo, type, ipip6);
215 break;
216 default:
217 WARN_ON(1);
218 err = -EPROTONOSUPPORT;
219 break;
220 }
221 #undef X
222 rcu_read_unlock();
223 return err;
224 }
225 EXPORT_SYMBOL(xfrm_register_type);
226
xfrm_unregister_type(const struct xfrm_type * type,unsigned short family)227 void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
228 {
229 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
230
231 if (unlikely(afinfo == NULL))
232 return;
233
234 #define X(afi, T, name) do { \
235 WARN_ON((afi)->type_ ## name != (T)); \
236 (afi)->type_ ## name = NULL; \
237 } while (0)
238
239 switch (type->proto) {
240 case IPPROTO_COMP:
241 X(afinfo, type, comp);
242 break;
243 case IPPROTO_AH:
244 X(afinfo, type, ah);
245 break;
246 case IPPROTO_ESP:
247 X(afinfo, type, esp);
248 break;
249 case IPPROTO_IPIP:
250 X(afinfo, type, ipip);
251 break;
252 case IPPROTO_DSTOPTS:
253 X(afinfo, type, dstopts);
254 break;
255 case IPPROTO_ROUTING:
256 X(afinfo, type, routing);
257 break;
258 case IPPROTO_IPV6:
259 X(afinfo, type, ipip6);
260 break;
261 default:
262 WARN_ON(1);
263 break;
264 }
265 #undef X
266 rcu_read_unlock();
267 }
268 EXPORT_SYMBOL(xfrm_unregister_type);
269
xfrm_get_type(u8 proto,unsigned short family)270 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
271 {
272 const struct xfrm_type *type = NULL;
273 struct xfrm_state_afinfo *afinfo;
274 int modload_attempted = 0;
275
276 retry:
277 afinfo = xfrm_state_get_afinfo(family);
278 if (unlikely(afinfo == NULL))
279 return NULL;
280
281 switch (proto) {
282 case IPPROTO_COMP:
283 type = afinfo->type_comp;
284 break;
285 case IPPROTO_AH:
286 type = afinfo->type_ah;
287 break;
288 case IPPROTO_ESP:
289 type = afinfo->type_esp;
290 break;
291 case IPPROTO_IPIP:
292 type = afinfo->type_ipip;
293 break;
294 case IPPROTO_DSTOPTS:
295 type = afinfo->type_dstopts;
296 break;
297 case IPPROTO_ROUTING:
298 type = afinfo->type_routing;
299 break;
300 case IPPROTO_IPV6:
301 type = afinfo->type_ipip6;
302 break;
303 default:
304 break;
305 }
306
307 if (unlikely(type && !try_module_get(type->owner)))
308 type = NULL;
309
310 rcu_read_unlock();
311
312 if (!type && !modload_attempted) {
313 request_module("xfrm-type-%d-%d", family, proto);
314 modload_attempted = 1;
315 goto retry;
316 }
317
318 return type;
319 }
320
xfrm_put_type(const struct xfrm_type * type)321 static void xfrm_put_type(const struct xfrm_type *type)
322 {
323 module_put(type->owner);
324 }
325
xfrm_register_type_offload(const struct xfrm_type_offload * type,unsigned short family)326 int xfrm_register_type_offload(const struct xfrm_type_offload *type,
327 unsigned short family)
328 {
329 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
330 int err = 0;
331
332 if (unlikely(afinfo == NULL))
333 return -EAFNOSUPPORT;
334
335 switch (type->proto) {
336 case IPPROTO_ESP:
337 WARN_ON(afinfo->type_offload_esp);
338 afinfo->type_offload_esp = type;
339 break;
340 default:
341 WARN_ON(1);
342 err = -EPROTONOSUPPORT;
343 break;
344 }
345
346 rcu_read_unlock();
347 return err;
348 }
349 EXPORT_SYMBOL(xfrm_register_type_offload);
350
xfrm_unregister_type_offload(const struct xfrm_type_offload * type,unsigned short family)351 void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
352 unsigned short family)
353 {
354 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
355
356 if (unlikely(afinfo == NULL))
357 return;
358
359 switch (type->proto) {
360 case IPPROTO_ESP:
361 WARN_ON(afinfo->type_offload_esp != type);
362 afinfo->type_offload_esp = NULL;
363 break;
364 default:
365 WARN_ON(1);
366 break;
367 }
368 rcu_read_unlock();
369 }
370 EXPORT_SYMBOL(xfrm_unregister_type_offload);
371
372 static const struct xfrm_type_offload *
xfrm_get_type_offload(u8 proto,unsigned short family,bool try_load)373 xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
374 {
375 const struct xfrm_type_offload *type = NULL;
376 struct xfrm_state_afinfo *afinfo;
377
378 retry:
379 afinfo = xfrm_state_get_afinfo(family);
380 if (unlikely(afinfo == NULL))
381 return NULL;
382
383 switch (proto) {
384 case IPPROTO_ESP:
385 type = afinfo->type_offload_esp;
386 break;
387 default:
388 break;
389 }
390
391 if ((type && !try_module_get(type->owner)))
392 type = NULL;
393
394 rcu_read_unlock();
395
396 if (!type && try_load) {
397 request_module("xfrm-offload-%d-%d", family, proto);
398 try_load = false;
399 goto retry;
400 }
401
402 return type;
403 }
404
xfrm_put_type_offload(const struct xfrm_type_offload * type)405 static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
406 {
407 module_put(type->owner);
408 }
409
410 static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
411 [XFRM_MODE_BEET] = {
412 .encap = XFRM_MODE_BEET,
413 .flags = XFRM_MODE_FLAG_TUNNEL,
414 .family = AF_INET,
415 },
416 [XFRM_MODE_TRANSPORT] = {
417 .encap = XFRM_MODE_TRANSPORT,
418 .family = AF_INET,
419 },
420 [XFRM_MODE_TUNNEL] = {
421 .encap = XFRM_MODE_TUNNEL,
422 .flags = XFRM_MODE_FLAG_TUNNEL,
423 .family = AF_INET,
424 },
425 };
426
427 static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
428 [XFRM_MODE_BEET] = {
429 .encap = XFRM_MODE_BEET,
430 .flags = XFRM_MODE_FLAG_TUNNEL,
431 .family = AF_INET6,
432 },
433 [XFRM_MODE_ROUTEOPTIMIZATION] = {
434 .encap = XFRM_MODE_ROUTEOPTIMIZATION,
435 .family = AF_INET6,
436 },
437 [XFRM_MODE_TRANSPORT] = {
438 .encap = XFRM_MODE_TRANSPORT,
439 .family = AF_INET6,
440 },
441 [XFRM_MODE_TUNNEL] = {
442 .encap = XFRM_MODE_TUNNEL,
443 .flags = XFRM_MODE_FLAG_TUNNEL,
444 .family = AF_INET6,
445 },
446 };
447
xfrm_get_mode(unsigned int encap,int family)448 static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
449 {
450 const struct xfrm_mode *mode;
451
452 if (unlikely(encap >= XFRM_MODE_MAX))
453 return NULL;
454
455 switch (family) {
456 case AF_INET:
457 mode = &xfrm4_mode_map[encap];
458 if (mode->family == family)
459 return mode;
460 break;
461 case AF_INET6:
462 mode = &xfrm6_mode_map[encap];
463 if (mode->family == family)
464 return mode;
465 break;
466 default:
467 break;
468 }
469
470 return NULL;
471 }
472
xfrm_state_free(struct xfrm_state * x)473 void xfrm_state_free(struct xfrm_state *x)
474 {
475 kmem_cache_free(xfrm_state_cache, x);
476 }
477 EXPORT_SYMBOL(xfrm_state_free);
478
___xfrm_state_destroy(struct xfrm_state * x)479 static void ___xfrm_state_destroy(struct xfrm_state *x)
480 {
481 hrtimer_cancel(&x->mtimer);
482 del_timer_sync(&x->rtimer);
483 kfree(x->aead);
484 kfree(x->aalg);
485 kfree(x->ealg);
486 kfree(x->calg);
487 kfree(x->encap);
488 kfree(x->coaddr);
489 kfree(x->replay_esn);
490 kfree(x->preplay_esn);
491 if (x->type_offload)
492 xfrm_put_type_offload(x->type_offload);
493 if (x->type) {
494 x->type->destructor(x);
495 xfrm_put_type(x->type);
496 }
497 if (x->xfrag.page)
498 put_page(x->xfrag.page);
499 xfrm_dev_state_free(x);
500 security_xfrm_state_free(x);
501 xfrm_state_free(x);
502 }
503
xfrm_state_gc_task(struct work_struct * work)504 static void xfrm_state_gc_task(struct work_struct *work)
505 {
506 struct xfrm_state *x;
507 struct hlist_node *tmp;
508 struct hlist_head gc_list;
509
510 spin_lock_bh(&xfrm_state_gc_lock);
511 hlist_move_list(&xfrm_state_gc_list, &gc_list);
512 spin_unlock_bh(&xfrm_state_gc_lock);
513
514 synchronize_rcu();
515
516 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
517 ___xfrm_state_destroy(x);
518 }
519
xfrm_timer_handler(struct hrtimer * me)520 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
521 {
522 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
523 enum hrtimer_restart ret = HRTIMER_NORESTART;
524 time64_t now = ktime_get_real_seconds();
525 time64_t next = TIME64_MAX;
526 int warn = 0;
527 int err = 0;
528
529 spin_lock(&x->lock);
530 if (x->km.state == XFRM_STATE_DEAD)
531 goto out;
532 if (x->km.state == XFRM_STATE_EXPIRED)
533 goto expired;
534 if (x->lft.hard_add_expires_seconds) {
535 long tmo = x->lft.hard_add_expires_seconds +
536 x->curlft.add_time - now;
537 if (tmo <= 0) {
538 if (x->xflags & XFRM_SOFT_EXPIRE) {
539 /* enter hard expire without soft expire first?!
540 * setting a new date could trigger this.
541 * workaround: fix x->curflt.add_time by below:
542 */
543 x->curlft.add_time = now - x->saved_tmo - 1;
544 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
545 } else
546 goto expired;
547 }
548 if (tmo < next)
549 next = tmo;
550 }
551 if (x->lft.hard_use_expires_seconds) {
552 long tmo = x->lft.hard_use_expires_seconds +
553 (x->curlft.use_time ? : now) - now;
554 if (tmo <= 0)
555 goto expired;
556 if (tmo < next)
557 next = tmo;
558 }
559 if (x->km.dying)
560 goto resched;
561 if (x->lft.soft_add_expires_seconds) {
562 long tmo = x->lft.soft_add_expires_seconds +
563 x->curlft.add_time - now;
564 if (tmo <= 0) {
565 warn = 1;
566 x->xflags &= ~XFRM_SOFT_EXPIRE;
567 } else if (tmo < next) {
568 next = tmo;
569 x->xflags |= XFRM_SOFT_EXPIRE;
570 x->saved_tmo = tmo;
571 }
572 }
573 if (x->lft.soft_use_expires_seconds) {
574 long tmo = x->lft.soft_use_expires_seconds +
575 (x->curlft.use_time ? : now) - now;
576 if (tmo <= 0)
577 warn = 1;
578 else if (tmo < next)
579 next = tmo;
580 }
581
582 x->km.dying = warn;
583 if (warn)
584 km_state_expired(x, 0, 0);
585 resched:
586 if (next != TIME64_MAX) {
587 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
588 ret = HRTIMER_RESTART;
589 }
590
591 goto out;
592
593 expired:
594 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
595 x->km.state = XFRM_STATE_EXPIRED;
596
597 err = __xfrm_state_delete(x);
598 if (!err)
599 km_state_expired(x, 1, 0);
600
601 xfrm_audit_state_delete(x, err ? 0 : 1, true);
602
603 out:
604 spin_unlock(&x->lock);
605 return ret;
606 }
607
608 static void xfrm_replay_timer_handler(struct timer_list *t);
609
xfrm_state_alloc(struct net * net)610 struct xfrm_state *xfrm_state_alloc(struct net *net)
611 {
612 struct xfrm_state *x;
613
614 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
615
616 if (x) {
617 write_pnet(&x->xs_net, net);
618 refcount_set(&x->refcnt, 1);
619 atomic_set(&x->tunnel_users, 0);
620 INIT_LIST_HEAD(&x->km.all);
621 INIT_HLIST_NODE(&x->bydst);
622 INIT_HLIST_NODE(&x->bysrc);
623 INIT_HLIST_NODE(&x->byspi);
624 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
625 x->mtimer.function = xfrm_timer_handler;
626 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
627 x->curlft.add_time = ktime_get_real_seconds();
628 x->lft.soft_byte_limit = XFRM_INF;
629 x->lft.soft_packet_limit = XFRM_INF;
630 x->lft.hard_byte_limit = XFRM_INF;
631 x->lft.hard_packet_limit = XFRM_INF;
632 x->replay_maxage = 0;
633 x->replay_maxdiff = 0;
634 spin_lock_init(&x->lock);
635 }
636 return x;
637 }
638 EXPORT_SYMBOL(xfrm_state_alloc);
639
__xfrm_state_destroy(struct xfrm_state * x,bool sync)640 void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
641 {
642 WARN_ON(x->km.state != XFRM_STATE_DEAD);
643
644 if (sync) {
645 synchronize_rcu();
646 ___xfrm_state_destroy(x);
647 } else {
648 spin_lock_bh(&xfrm_state_gc_lock);
649 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
650 spin_unlock_bh(&xfrm_state_gc_lock);
651 schedule_work(&xfrm_state_gc_work);
652 }
653 }
654 EXPORT_SYMBOL(__xfrm_state_destroy);
655
__xfrm_state_delete(struct xfrm_state * x)656 int __xfrm_state_delete(struct xfrm_state *x)
657 {
658 struct net *net = xs_net(x);
659 int err = -ESRCH;
660
661 if (x->km.state != XFRM_STATE_DEAD) {
662 x->km.state = XFRM_STATE_DEAD;
663 spin_lock(&net->xfrm.xfrm_state_lock);
664 list_del(&x->km.all);
665 hlist_del_rcu(&x->bydst);
666 hlist_del_rcu(&x->bysrc);
667 if (x->id.spi)
668 hlist_del_rcu(&x->byspi);
669 net->xfrm.state_num--;
670 spin_unlock(&net->xfrm.xfrm_state_lock);
671
672 if (x->encap_sk)
673 sock_put(rcu_dereference_raw(x->encap_sk));
674
675 xfrm_dev_state_delete(x);
676
677 /* All xfrm_state objects are created by xfrm_state_alloc.
678 * The xfrm_state_alloc call gives a reference, and that
679 * is what we are dropping here.
680 */
681 xfrm_state_put(x);
682 err = 0;
683 }
684
685 return err;
686 }
687 EXPORT_SYMBOL(__xfrm_state_delete);
688
xfrm_state_delete(struct xfrm_state * x)689 int xfrm_state_delete(struct xfrm_state *x)
690 {
691 int err;
692
693 spin_lock_bh(&x->lock);
694 err = __xfrm_state_delete(x);
695 spin_unlock_bh(&x->lock);
696
697 return err;
698 }
699 EXPORT_SYMBOL(xfrm_state_delete);
700
701 #ifdef CONFIG_SECURITY_NETWORK_XFRM
702 static inline int
xfrm_state_flush_secctx_check(struct net * net,u8 proto,bool task_valid)703 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
704 {
705 int i, err = 0;
706
707 for (i = 0; i <= net->xfrm.state_hmask; i++) {
708 struct xfrm_state *x;
709
710 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
711 if (xfrm_id_proto_match(x->id.proto, proto) &&
712 (err = security_xfrm_state_delete(x)) != 0) {
713 xfrm_audit_state_delete(x, 0, task_valid);
714 return err;
715 }
716 }
717 }
718
719 return err;
720 }
721
722 static inline int
xfrm_dev_state_flush_secctx_check(struct net * net,struct net_device * dev,bool task_valid)723 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
724 {
725 int i, err = 0;
726
727 for (i = 0; i <= net->xfrm.state_hmask; i++) {
728 struct xfrm_state *x;
729 struct xfrm_state_offload *xso;
730
731 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
732 xso = &x->xso;
733
734 if (xso->dev == dev &&
735 (err = security_xfrm_state_delete(x)) != 0) {
736 xfrm_audit_state_delete(x, 0, task_valid);
737 return err;
738 }
739 }
740 }
741
742 return err;
743 }
744 #else
745 static inline int
xfrm_state_flush_secctx_check(struct net * net,u8 proto,bool task_valid)746 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
747 {
748 return 0;
749 }
750
751 static inline int
xfrm_dev_state_flush_secctx_check(struct net * net,struct net_device * dev,bool task_valid)752 xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
753 {
754 return 0;
755 }
756 #endif
757
xfrm_state_flush(struct net * net,u8 proto,bool task_valid,bool sync)758 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
759 {
760 int i, err = 0, cnt = 0;
761
762 spin_lock_bh(&net->xfrm.xfrm_state_lock);
763 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
764 if (err)
765 goto out;
766
767 err = -ESRCH;
768 for (i = 0; i <= net->xfrm.state_hmask; i++) {
769 struct xfrm_state *x;
770 restart:
771 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
772 if (!xfrm_state_kern(x) &&
773 xfrm_id_proto_match(x->id.proto, proto)) {
774 xfrm_state_hold(x);
775 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
776
777 err = xfrm_state_delete(x);
778 xfrm_audit_state_delete(x, err ? 0 : 1,
779 task_valid);
780 if (sync)
781 xfrm_state_put_sync(x);
782 else
783 xfrm_state_put(x);
784 if (!err)
785 cnt++;
786
787 spin_lock_bh(&net->xfrm.xfrm_state_lock);
788 goto restart;
789 }
790 }
791 }
792 out:
793 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
794 if (cnt)
795 err = 0;
796
797 return err;
798 }
799 EXPORT_SYMBOL(xfrm_state_flush);
800
xfrm_dev_state_flush(struct net * net,struct net_device * dev,bool task_valid)801 int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
802 {
803 int i, err = 0, cnt = 0;
804
805 spin_lock_bh(&net->xfrm.xfrm_state_lock);
806 err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
807 if (err)
808 goto out;
809
810 err = -ESRCH;
811 for (i = 0; i <= net->xfrm.state_hmask; i++) {
812 struct xfrm_state *x;
813 struct xfrm_state_offload *xso;
814 restart:
815 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
816 xso = &x->xso;
817
818 if (!xfrm_state_kern(x) && xso->dev == dev) {
819 xfrm_state_hold(x);
820 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
821
822 err = xfrm_state_delete(x);
823 xfrm_audit_state_delete(x, err ? 0 : 1,
824 task_valid);
825 xfrm_state_put(x);
826 if (!err)
827 cnt++;
828
829 spin_lock_bh(&net->xfrm.xfrm_state_lock);
830 goto restart;
831 }
832 }
833 }
834 if (cnt)
835 err = 0;
836
837 out:
838 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
839 return err;
840 }
841 EXPORT_SYMBOL(xfrm_dev_state_flush);
842
xfrm_sad_getinfo(struct net * net,struct xfrmk_sadinfo * si)843 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
844 {
845 spin_lock_bh(&net->xfrm.xfrm_state_lock);
846 si->sadcnt = net->xfrm.state_num;
847 si->sadhcnt = net->xfrm.state_hmask + 1;
848 si->sadhmcnt = xfrm_state_hashmax;
849 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
850 }
851 EXPORT_SYMBOL(xfrm_sad_getinfo);
852
853 static void
__xfrm4_init_tempsel(struct xfrm_selector * sel,const struct flowi * fl)854 __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
855 {
856 const struct flowi4 *fl4 = &fl->u.ip4;
857
858 sel->daddr.a4 = fl4->daddr;
859 sel->saddr.a4 = fl4->saddr;
860 sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
861 sel->dport_mask = htons(0xffff);
862 sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
863 sel->sport_mask = htons(0xffff);
864 sel->family = AF_INET;
865 sel->prefixlen_d = 32;
866 sel->prefixlen_s = 32;
867 sel->proto = fl4->flowi4_proto;
868 sel->ifindex = fl4->flowi4_oif;
869 }
870
871 static void
__xfrm6_init_tempsel(struct xfrm_selector * sel,const struct flowi * fl)872 __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
873 {
874 const struct flowi6 *fl6 = &fl->u.ip6;
875
876 /* Initialize temporary selector matching only to current session. */
877 *(struct in6_addr *)&sel->daddr = fl6->daddr;
878 *(struct in6_addr *)&sel->saddr = fl6->saddr;
879 sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
880 sel->dport_mask = htons(0xffff);
881 sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
882 sel->sport_mask = htons(0xffff);
883 sel->family = AF_INET6;
884 sel->prefixlen_d = 128;
885 sel->prefixlen_s = 128;
886 sel->proto = fl6->flowi6_proto;
887 sel->ifindex = fl6->flowi6_oif;
888 }
889
890 static void
xfrm_init_tempstate(struct xfrm_state * x,const struct flowi * fl,const struct xfrm_tmpl * tmpl,const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family)891 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
892 const struct xfrm_tmpl *tmpl,
893 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
894 unsigned short family)
895 {
896 switch (family) {
897 case AF_INET:
898 __xfrm4_init_tempsel(&x->sel, fl);
899 break;
900 case AF_INET6:
901 __xfrm6_init_tempsel(&x->sel, fl);
902 break;
903 }
904
905 x->id = tmpl->id;
906
907 switch (tmpl->encap_family) {
908 case AF_INET:
909 if (x->id.daddr.a4 == 0)
910 x->id.daddr.a4 = daddr->a4;
911 x->props.saddr = tmpl->saddr;
912 if (x->props.saddr.a4 == 0)
913 x->props.saddr.a4 = saddr->a4;
914 break;
915 case AF_INET6:
916 if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
917 memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
918 memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
919 if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
920 memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
921 break;
922 }
923
924 x->props.mode = tmpl->mode;
925 x->props.reqid = tmpl->reqid;
926 x->props.family = tmpl->encap_family;
927 }
928
__xfrm_state_lookup(struct net * net,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)929 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
930 const xfrm_address_t *daddr,
931 __be32 spi, u8 proto,
932 unsigned short family)
933 {
934 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
935 struct xfrm_state *x;
936
937 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
938 if (x->props.family != family ||
939 x->id.spi != spi ||
940 x->id.proto != proto ||
941 !xfrm_addr_equal(&x->id.daddr, daddr, family))
942 continue;
943
944 if ((mark & x->mark.m) != x->mark.v)
945 continue;
946 if (!xfrm_state_hold_rcu(x))
947 continue;
948 return x;
949 }
950
951 return NULL;
952 }
953
__xfrm_state_lookup_byaddr(struct net * net,u32 mark,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u8 proto,unsigned short family)954 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
955 const xfrm_address_t *daddr,
956 const xfrm_address_t *saddr,
957 u8 proto, unsigned short family)
958 {
959 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
960 struct xfrm_state *x;
961
962 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
963 if (x->props.family != family ||
964 x->id.proto != proto ||
965 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
966 !xfrm_addr_equal(&x->props.saddr, saddr, family))
967 continue;
968
969 if ((mark & x->mark.m) != x->mark.v)
970 continue;
971 if (!xfrm_state_hold_rcu(x))
972 continue;
973 return x;
974 }
975
976 return NULL;
977 }
978
979 static inline struct xfrm_state *
__xfrm_state_locate(struct xfrm_state * x,int use_spi,int family)980 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
981 {
982 struct net *net = xs_net(x);
983 u32 mark = x->mark.v & x->mark.m;
984
985 if (use_spi)
986 return __xfrm_state_lookup(net, mark, &x->id.daddr,
987 x->id.spi, x->id.proto, family);
988 else
989 return __xfrm_state_lookup_byaddr(net, mark,
990 &x->id.daddr,
991 &x->props.saddr,
992 x->id.proto, family);
993 }
994
xfrm_hash_grow_check(struct net * net,int have_hash_collision)995 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
996 {
997 if (have_hash_collision &&
998 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
999 net->xfrm.state_num > net->xfrm.state_hmask)
1000 schedule_work(&net->xfrm.state_hash_work);
1001 }
1002
xfrm_state_look_at(struct xfrm_policy * pol,struct xfrm_state * x,const struct flowi * fl,unsigned short family,struct xfrm_state ** best,int * acq_in_progress,int * error)1003 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
1004 const struct flowi *fl, unsigned short family,
1005 struct xfrm_state **best, int *acq_in_progress,
1006 int *error)
1007 {
1008 /* Resolution logic:
1009 * 1. There is a valid state with matching selector. Done.
1010 * 2. Valid state with inappropriate selector. Skip.
1011 *
1012 * Entering area of "sysdeps".
1013 *
1014 * 3. If state is not valid, selector is temporary, it selects
1015 * only session which triggered previous resolution. Key
1016 * manager will do something to install a state with proper
1017 * selector.
1018 */
1019 if (x->km.state == XFRM_STATE_VALID) {
1020 if ((x->sel.family &&
1021 (x->sel.family != family ||
1022 !xfrm_selector_match(&x->sel, fl, family))) ||
1023 !security_xfrm_state_pol_flow_match(x, pol, fl))
1024 return;
1025
1026 if (!*best ||
1027 (*best)->km.dying > x->km.dying ||
1028 ((*best)->km.dying == x->km.dying &&
1029 (*best)->curlft.add_time < x->curlft.add_time))
1030 *best = x;
1031 } else if (x->km.state == XFRM_STATE_ACQ) {
1032 *acq_in_progress = 1;
1033 } else if (x->km.state == XFRM_STATE_ERROR ||
1034 x->km.state == XFRM_STATE_EXPIRED) {
1035 if ((!x->sel.family ||
1036 (x->sel.family == family &&
1037 xfrm_selector_match(&x->sel, fl, family))) &&
1038 security_xfrm_state_pol_flow_match(x, pol, fl))
1039 *error = -ESRCH;
1040 }
1041 }
1042
1043 struct xfrm_state *
xfrm_state_find(const xfrm_address_t * daddr,const xfrm_address_t * saddr,const struct flowi * fl,struct xfrm_tmpl * tmpl,struct xfrm_policy * pol,int * err,unsigned short family,u32 if_id)1044 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1045 const struct flowi *fl, struct xfrm_tmpl *tmpl,
1046 struct xfrm_policy *pol, int *err,
1047 unsigned short family, u32 if_id)
1048 {
1049 static xfrm_address_t saddr_wildcard = { };
1050 struct net *net = xp_net(pol);
1051 unsigned int h, h_wildcard;
1052 struct xfrm_state *x, *x0, *to_put;
1053 int acquire_in_progress = 0;
1054 int error = 0;
1055 struct xfrm_state *best = NULL;
1056 u32 mark = pol->mark.v & pol->mark.m;
1057 unsigned short encap_family = tmpl->encap_family;
1058 unsigned int sequence;
1059 struct km_event c;
1060
1061 to_put = NULL;
1062
1063 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
1064
1065 rcu_read_lock();
1066 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
1067 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
1068 if (x->props.family == encap_family &&
1069 x->props.reqid == tmpl->reqid &&
1070 (mark & x->mark.m) == x->mark.v &&
1071 x->if_id == if_id &&
1072 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1073 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
1074 tmpl->mode == x->props.mode &&
1075 tmpl->id.proto == x->id.proto &&
1076 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1077 xfrm_state_look_at(pol, x, fl, family,
1078 &best, &acquire_in_progress, &error);
1079 }
1080 if (best || acquire_in_progress)
1081 goto found;
1082
1083 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
1084 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
1085 if (x->props.family == encap_family &&
1086 x->props.reqid == tmpl->reqid &&
1087 (mark & x->mark.m) == x->mark.v &&
1088 x->if_id == if_id &&
1089 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1090 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
1091 tmpl->mode == x->props.mode &&
1092 tmpl->id.proto == x->id.proto &&
1093 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
1094 xfrm_state_look_at(pol, x, fl, family,
1095 &best, &acquire_in_progress, &error);
1096 }
1097
1098 found:
1099 x = best;
1100 if (!x && !error && !acquire_in_progress) {
1101 if (tmpl->id.spi &&
1102 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
1103 tmpl->id.proto, encap_family)) != NULL) {
1104 to_put = x0;
1105 error = -EEXIST;
1106 goto out;
1107 }
1108
1109 c.net = net;
1110 /* If the KMs have no listeners (yet...), avoid allocating an SA
1111 * for each and every packet - garbage collection might not
1112 * handle the flood.
1113 */
1114 if (!km_is_alive(&c)) {
1115 error = -ESRCH;
1116 goto out;
1117 }
1118
1119 x = xfrm_state_alloc(net);
1120 if (x == NULL) {
1121 error = -ENOMEM;
1122 goto out;
1123 }
1124 /* Initialize temporary state matching only
1125 * to current session. */
1126 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
1127 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
1128 x->if_id = if_id;
1129
1130 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
1131 if (error) {
1132 x->km.state = XFRM_STATE_DEAD;
1133 to_put = x;
1134 x = NULL;
1135 goto out;
1136 }
1137
1138 if (km_query(x, tmpl, pol) == 0) {
1139 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1140 x->km.state = XFRM_STATE_ACQ;
1141 list_add(&x->km.all, &net->xfrm.state_all);
1142 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1143 h = xfrm_src_hash(net, daddr, saddr, encap_family);
1144 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1145 if (x->id.spi) {
1146 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
1147 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1148 }
1149 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1150 hrtimer_start(&x->mtimer,
1151 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1152 HRTIMER_MODE_REL_SOFT);
1153 net->xfrm.state_num++;
1154 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1155 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1156 } else {
1157 x->km.state = XFRM_STATE_DEAD;
1158 to_put = x;
1159 x = NULL;
1160 error = -ESRCH;
1161 }
1162 }
1163 out:
1164 if (x) {
1165 if (!xfrm_state_hold_rcu(x)) {
1166 *err = -EAGAIN;
1167 x = NULL;
1168 }
1169 } else {
1170 *err = acquire_in_progress ? -EAGAIN : error;
1171 }
1172 rcu_read_unlock();
1173 if (to_put)
1174 xfrm_state_put(to_put);
1175
1176 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
1177 *err = -EAGAIN;
1178 if (x) {
1179 xfrm_state_put(x);
1180 x = NULL;
1181 }
1182 }
1183
1184 return x;
1185 }
1186
1187 struct xfrm_state *
xfrm_stateonly_find(struct net * net,u32 mark,u32 if_id,xfrm_address_t * daddr,xfrm_address_t * saddr,unsigned short family,u8 mode,u8 proto,u32 reqid)1188 xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
1189 xfrm_address_t *daddr, xfrm_address_t *saddr,
1190 unsigned short family, u8 mode, u8 proto, u32 reqid)
1191 {
1192 unsigned int h;
1193 struct xfrm_state *rx = NULL, *x = NULL;
1194
1195 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1196 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1197 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1198 if (x->props.family == family &&
1199 x->props.reqid == reqid &&
1200 (mark & x->mark.m) == x->mark.v &&
1201 x->if_id == if_id &&
1202 !(x->props.flags & XFRM_STATE_WILDRECV) &&
1203 xfrm_state_addr_check(x, daddr, saddr, family) &&
1204 mode == x->props.mode &&
1205 proto == x->id.proto &&
1206 x->km.state == XFRM_STATE_VALID) {
1207 rx = x;
1208 break;
1209 }
1210 }
1211
1212 if (rx)
1213 xfrm_state_hold(rx);
1214 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1215
1216
1217 return rx;
1218 }
1219 EXPORT_SYMBOL(xfrm_stateonly_find);
1220
xfrm_state_lookup_byspi(struct net * net,__be32 spi,unsigned short family)1221 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
1222 unsigned short family)
1223 {
1224 struct xfrm_state *x;
1225 struct xfrm_state_walk *w;
1226
1227 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1228 list_for_each_entry(w, &net->xfrm.state_all, all) {
1229 x = container_of(w, struct xfrm_state, km);
1230 if (x->props.family != family ||
1231 x->id.spi != spi)
1232 continue;
1233
1234 xfrm_state_hold(x);
1235 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1236 return x;
1237 }
1238 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1239 return NULL;
1240 }
1241 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1242
__xfrm_state_insert(struct xfrm_state * x)1243 static void __xfrm_state_insert(struct xfrm_state *x)
1244 {
1245 struct net *net = xs_net(x);
1246 unsigned int h;
1247
1248 list_add(&x->km.all, &net->xfrm.state_all);
1249
1250 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
1251 x->props.reqid, x->props.family);
1252 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1253
1254 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
1255 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1256
1257 if (x->id.spi) {
1258 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
1259 x->props.family);
1260
1261 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1262 }
1263
1264 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
1265 if (x->replay_maxage)
1266 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
1267
1268 net->xfrm.state_num++;
1269
1270 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1271 }
1272
1273 /* net->xfrm.xfrm_state_lock is held */
__xfrm_state_bump_genids(struct xfrm_state * xnew)1274 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1275 {
1276 struct net *net = xs_net(xnew);
1277 unsigned short family = xnew->props.family;
1278 u32 reqid = xnew->props.reqid;
1279 struct xfrm_state *x;
1280 unsigned int h;
1281 u32 mark = xnew->mark.v & xnew->mark.m;
1282 u32 if_id = xnew->if_id;
1283
1284 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1285 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1286 if (x->props.family == family &&
1287 x->props.reqid == reqid &&
1288 x->if_id == if_id &&
1289 (mark & x->mark.m) == x->mark.v &&
1290 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1291 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1292 x->genid++;
1293 }
1294 }
1295
xfrm_state_insert(struct xfrm_state * x)1296 void xfrm_state_insert(struct xfrm_state *x)
1297 {
1298 struct net *net = xs_net(x);
1299
1300 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1301 __xfrm_state_bump_genids(x);
1302 __xfrm_state_insert(x);
1303 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1304 }
1305 EXPORT_SYMBOL(xfrm_state_insert);
1306
1307 /* net->xfrm.xfrm_state_lock is held */
__find_acq_core(struct net * net,const struct xfrm_mark * m,unsigned short family,u8 mode,u32 reqid,u32 if_id,u8 proto,const xfrm_address_t * daddr,const xfrm_address_t * saddr,int create)1308 static struct xfrm_state *__find_acq_core(struct net *net,
1309 const struct xfrm_mark *m,
1310 unsigned short family, u8 mode,
1311 u32 reqid, u32 if_id, u8 proto,
1312 const xfrm_address_t *daddr,
1313 const xfrm_address_t *saddr,
1314 int create)
1315 {
1316 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1317 struct xfrm_state *x;
1318 u32 mark = m->v & m->m;
1319
1320 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1321 if (x->props.reqid != reqid ||
1322 x->props.mode != mode ||
1323 x->props.family != family ||
1324 x->km.state != XFRM_STATE_ACQ ||
1325 x->id.spi != 0 ||
1326 x->id.proto != proto ||
1327 (mark & x->mark.m) != x->mark.v ||
1328 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1329 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1330 continue;
1331
1332 xfrm_state_hold(x);
1333 return x;
1334 }
1335
1336 if (!create)
1337 return NULL;
1338
1339 x = xfrm_state_alloc(net);
1340 if (likely(x)) {
1341 switch (family) {
1342 case AF_INET:
1343 x->sel.daddr.a4 = daddr->a4;
1344 x->sel.saddr.a4 = saddr->a4;
1345 x->sel.prefixlen_d = 32;
1346 x->sel.prefixlen_s = 32;
1347 x->props.saddr.a4 = saddr->a4;
1348 x->id.daddr.a4 = daddr->a4;
1349 break;
1350
1351 case AF_INET6:
1352 x->sel.daddr.in6 = daddr->in6;
1353 x->sel.saddr.in6 = saddr->in6;
1354 x->sel.prefixlen_d = 128;
1355 x->sel.prefixlen_s = 128;
1356 x->props.saddr.in6 = saddr->in6;
1357 x->id.daddr.in6 = daddr->in6;
1358 break;
1359 }
1360
1361 x->km.state = XFRM_STATE_ACQ;
1362 x->id.proto = proto;
1363 x->props.family = family;
1364 x->props.mode = mode;
1365 x->props.reqid = reqid;
1366 x->if_id = if_id;
1367 x->mark.v = m->v;
1368 x->mark.m = m->m;
1369 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1370 xfrm_state_hold(x);
1371 hrtimer_start(&x->mtimer,
1372 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1373 HRTIMER_MODE_REL_SOFT);
1374 list_add(&x->km.all, &net->xfrm.state_all);
1375 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1376 h = xfrm_src_hash(net, daddr, saddr, family);
1377 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1378
1379 net->xfrm.state_num++;
1380
1381 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1382 }
1383
1384 return x;
1385 }
1386
1387 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1388
xfrm_state_add(struct xfrm_state * x)1389 int xfrm_state_add(struct xfrm_state *x)
1390 {
1391 struct net *net = xs_net(x);
1392 struct xfrm_state *x1, *to_put;
1393 int family;
1394 int err;
1395 u32 mark = x->mark.v & x->mark.m;
1396 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1397
1398 family = x->props.family;
1399
1400 to_put = NULL;
1401
1402 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1403
1404 x1 = __xfrm_state_locate(x, use_spi, family);
1405 if (x1) {
1406 to_put = x1;
1407 x1 = NULL;
1408 err = -EEXIST;
1409 goto out;
1410 }
1411
1412 if (use_spi && x->km.seq) {
1413 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1414 if (x1 && ((x1->id.proto != x->id.proto) ||
1415 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1416 to_put = x1;
1417 x1 = NULL;
1418 }
1419 }
1420
1421 if (use_spi && !x1)
1422 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1423 x->props.reqid, x->if_id, x->id.proto,
1424 &x->id.daddr, &x->props.saddr, 0);
1425
1426 __xfrm_state_bump_genids(x);
1427 __xfrm_state_insert(x);
1428 err = 0;
1429
1430 out:
1431 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1432
1433 if (x1) {
1434 xfrm_state_delete(x1);
1435 xfrm_state_put(x1);
1436 }
1437
1438 if (to_put)
1439 xfrm_state_put(to_put);
1440
1441 return err;
1442 }
1443 EXPORT_SYMBOL(xfrm_state_add);
1444
1445 #ifdef CONFIG_XFRM_MIGRATE
clone_security(struct xfrm_state * x,struct xfrm_sec_ctx * security)1446 static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
1447 {
1448 struct xfrm_user_sec_ctx *uctx;
1449 int size = sizeof(*uctx) + security->ctx_len;
1450 int err;
1451
1452 uctx = kmalloc(size, GFP_KERNEL);
1453 if (!uctx)
1454 return -ENOMEM;
1455
1456 uctx->exttype = XFRMA_SEC_CTX;
1457 uctx->len = size;
1458 uctx->ctx_doi = security->ctx_doi;
1459 uctx->ctx_alg = security->ctx_alg;
1460 uctx->ctx_len = security->ctx_len;
1461 memcpy(uctx + 1, security->ctx_str, security->ctx_len);
1462 err = security_xfrm_state_alloc(x, uctx);
1463 kfree(uctx);
1464 if (err)
1465 return err;
1466
1467 return 0;
1468 }
1469
xfrm_state_clone(struct xfrm_state * orig,struct xfrm_encap_tmpl * encap)1470 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1471 struct xfrm_encap_tmpl *encap)
1472 {
1473 struct net *net = xs_net(orig);
1474 struct xfrm_state *x = xfrm_state_alloc(net);
1475 if (!x)
1476 goto out;
1477
1478 memcpy(&x->id, &orig->id, sizeof(x->id));
1479 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1480 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1481 x->props.mode = orig->props.mode;
1482 x->props.replay_window = orig->props.replay_window;
1483 x->props.reqid = orig->props.reqid;
1484 x->props.family = orig->props.family;
1485 x->props.saddr = orig->props.saddr;
1486
1487 if (orig->aalg) {
1488 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1489 if (!x->aalg)
1490 goto error;
1491 }
1492 x->props.aalgo = orig->props.aalgo;
1493
1494 if (orig->aead) {
1495 x->aead = xfrm_algo_aead_clone(orig->aead);
1496 x->geniv = orig->geniv;
1497 if (!x->aead)
1498 goto error;
1499 }
1500 if (orig->ealg) {
1501 x->ealg = xfrm_algo_clone(orig->ealg);
1502 if (!x->ealg)
1503 goto error;
1504 }
1505 x->props.ealgo = orig->props.ealgo;
1506
1507 if (orig->calg) {
1508 x->calg = xfrm_algo_clone(orig->calg);
1509 if (!x->calg)
1510 goto error;
1511 }
1512 x->props.calgo = orig->props.calgo;
1513
1514 if (encap || orig->encap) {
1515 if (encap)
1516 x->encap = kmemdup(encap, sizeof(*x->encap),
1517 GFP_KERNEL);
1518 else
1519 x->encap = kmemdup(orig->encap, sizeof(*x->encap),
1520 GFP_KERNEL);
1521
1522 if (!x->encap)
1523 goto error;
1524 }
1525
1526 if (orig->security)
1527 if (clone_security(x, orig->security))
1528 goto error;
1529
1530 if (orig->coaddr) {
1531 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1532 GFP_KERNEL);
1533 if (!x->coaddr)
1534 goto error;
1535 }
1536
1537 if (orig->replay_esn) {
1538 if (xfrm_replay_clone(x, orig))
1539 goto error;
1540 }
1541
1542 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1543 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
1544
1545 if (xfrm_init_state(x) < 0)
1546 goto error;
1547
1548 x->props.flags = orig->props.flags;
1549 x->props.extra_flags = orig->props.extra_flags;
1550
1551 x->if_id = orig->if_id;
1552 x->tfcpad = orig->tfcpad;
1553 x->replay_maxdiff = orig->replay_maxdiff;
1554 x->replay_maxage = orig->replay_maxage;
1555 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
1556 x->km.state = orig->km.state;
1557 x->km.seq = orig->km.seq;
1558 x->replay = orig->replay;
1559 x->preplay = orig->preplay;
1560 x->mapping_maxage = orig->mapping_maxage;
1561 x->new_mapping = 0;
1562 x->new_mapping_sport = 0;
1563
1564 return x;
1565
1566 error:
1567 xfrm_state_put(x);
1568 out:
1569 return NULL;
1570 }
1571
xfrm_migrate_state_find(struct xfrm_migrate * m,struct net * net)1572 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1573 {
1574 unsigned int h;
1575 struct xfrm_state *x = NULL;
1576
1577 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1578
1579 if (m->reqid) {
1580 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1581 m->reqid, m->old_family);
1582 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1583 if (x->props.mode != m->mode ||
1584 x->id.proto != m->proto)
1585 continue;
1586 if (m->reqid && x->props.reqid != m->reqid)
1587 continue;
1588 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1589 m->old_family) ||
1590 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1591 m->old_family))
1592 continue;
1593 xfrm_state_hold(x);
1594 break;
1595 }
1596 } else {
1597 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1598 m->old_family);
1599 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1600 if (x->props.mode != m->mode ||
1601 x->id.proto != m->proto)
1602 continue;
1603 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1604 m->old_family) ||
1605 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1606 m->old_family))
1607 continue;
1608 xfrm_state_hold(x);
1609 break;
1610 }
1611 }
1612
1613 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1614
1615 return x;
1616 }
1617 EXPORT_SYMBOL(xfrm_migrate_state_find);
1618
xfrm_state_migrate(struct xfrm_state * x,struct xfrm_migrate * m,struct xfrm_encap_tmpl * encap)1619 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1620 struct xfrm_migrate *m,
1621 struct xfrm_encap_tmpl *encap)
1622 {
1623 struct xfrm_state *xc;
1624
1625 xc = xfrm_state_clone(x, encap);
1626 if (!xc)
1627 return NULL;
1628
1629 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1630 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1631
1632 /* add state */
1633 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1634 /* a care is needed when the destination address of the
1635 state is to be updated as it is a part of triplet */
1636 xfrm_state_insert(xc);
1637 } else {
1638 if (xfrm_state_add(xc) < 0)
1639 goto error;
1640 }
1641
1642 return xc;
1643 error:
1644 xfrm_state_put(xc);
1645 return NULL;
1646 }
1647 EXPORT_SYMBOL(xfrm_state_migrate);
1648 #endif
1649
xfrm_state_update(struct xfrm_state * x)1650 int xfrm_state_update(struct xfrm_state *x)
1651 {
1652 struct xfrm_state *x1, *to_put;
1653 int err;
1654 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1655 struct net *net = xs_net(x);
1656
1657 to_put = NULL;
1658
1659 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1660 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1661
1662 err = -ESRCH;
1663 if (!x1)
1664 goto out;
1665
1666 if (xfrm_state_kern(x1)) {
1667 to_put = x1;
1668 err = -EEXIST;
1669 goto out;
1670 }
1671
1672 if (x1->km.state == XFRM_STATE_ACQ) {
1673 __xfrm_state_insert(x);
1674 x = NULL;
1675 }
1676 err = 0;
1677
1678 out:
1679 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1680
1681 if (to_put)
1682 xfrm_state_put(to_put);
1683
1684 if (err)
1685 return err;
1686
1687 if (!x) {
1688 xfrm_state_delete(x1);
1689 xfrm_state_put(x1);
1690 return 0;
1691 }
1692
1693 err = -EINVAL;
1694 spin_lock_bh(&x1->lock);
1695 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1696 if (x->encap && x1->encap &&
1697 x->encap->encap_type == x1->encap->encap_type)
1698 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1699 else if (x->encap || x1->encap)
1700 goto fail;
1701
1702 if (x->coaddr && x1->coaddr) {
1703 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1704 }
1705 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1706 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1707 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1708 x1->km.dying = 0;
1709
1710 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1711 HRTIMER_MODE_REL_SOFT);
1712 if (x1->curlft.use_time)
1713 xfrm_state_check_expire(x1);
1714
1715 if (x->props.smark.m || x->props.smark.v || x->if_id) {
1716 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1717
1718 if (x->props.smark.m || x->props.smark.v)
1719 x1->props.smark = x->props.smark;
1720
1721 if (x->if_id)
1722 x1->if_id = x->if_id;
1723
1724 __xfrm_state_bump_genids(x1);
1725 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1726 }
1727
1728 err = 0;
1729 x->km.state = XFRM_STATE_DEAD;
1730 __xfrm_state_put(x);
1731 }
1732
1733 fail:
1734 spin_unlock_bh(&x1->lock);
1735
1736 xfrm_state_put(x1);
1737
1738 return err;
1739 }
1740 EXPORT_SYMBOL(xfrm_state_update);
1741
xfrm_state_check_expire(struct xfrm_state * x)1742 int xfrm_state_check_expire(struct xfrm_state *x)
1743 {
1744 if (!x->curlft.use_time)
1745 x->curlft.use_time = ktime_get_real_seconds();
1746
1747 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1748 x->curlft.packets >= x->lft.hard_packet_limit) {
1749 x->km.state = XFRM_STATE_EXPIRED;
1750 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
1751 return -EINVAL;
1752 }
1753
1754 if (!x->km.dying &&
1755 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1756 x->curlft.packets >= x->lft.soft_packet_limit)) {
1757 x->km.dying = 1;
1758 km_state_expired(x, 0, 0);
1759 }
1760 return 0;
1761 }
1762 EXPORT_SYMBOL(xfrm_state_check_expire);
1763
1764 struct xfrm_state *
xfrm_state_lookup(struct net * net,u32 mark,const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family)1765 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1766 u8 proto, unsigned short family)
1767 {
1768 struct xfrm_state *x;
1769
1770 rcu_read_lock();
1771 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1772 rcu_read_unlock();
1773 return x;
1774 }
1775 EXPORT_SYMBOL(xfrm_state_lookup);
1776
1777 struct xfrm_state *
xfrm_state_lookup_byaddr(struct net * net,u32 mark,const xfrm_address_t * daddr,const xfrm_address_t * saddr,u8 proto,unsigned short family)1778 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1779 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1780 u8 proto, unsigned short family)
1781 {
1782 struct xfrm_state *x;
1783
1784 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1785 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1786 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1787 return x;
1788 }
1789 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1790
1791 struct xfrm_state *
xfrm_find_acq(struct net * net,const struct xfrm_mark * mark,u8 mode,u32 reqid,u32 if_id,u8 proto,const xfrm_address_t * daddr,const xfrm_address_t * saddr,int create,unsigned short family)1792 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1793 u32 if_id, u8 proto, const xfrm_address_t *daddr,
1794 const xfrm_address_t *saddr, int create, unsigned short family)
1795 {
1796 struct xfrm_state *x;
1797
1798 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1799 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
1800 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1801
1802 return x;
1803 }
1804 EXPORT_SYMBOL(xfrm_find_acq);
1805
1806 #ifdef CONFIG_XFRM_SUB_POLICY
1807 #if IS_ENABLED(CONFIG_IPV6)
1808 /* distribution counting sort function for xfrm_state and xfrm_tmpl */
1809 static void
__xfrm6_sort(void ** dst,void ** src,int n,int (* cmp)(const void * p),int maxclass)1810 __xfrm6_sort(void **dst, void **src, int n,
1811 int (*cmp)(const void *p), int maxclass)
1812 {
1813 int count[XFRM_MAX_DEPTH] = { };
1814 int class[XFRM_MAX_DEPTH];
1815 int i;
1816
1817 for (i = 0; i < n; i++) {
1818 int c = cmp(src[i]);
1819
1820 class[i] = c;
1821 count[c]++;
1822 }
1823
1824 for (i = 2; i < maxclass; i++)
1825 count[i] += count[i - 1];
1826
1827 for (i = 0; i < n; i++) {
1828 dst[count[class[i] - 1]++] = src[i];
1829 src[i] = NULL;
1830 }
1831 }
1832
1833 /* Rule for xfrm_state:
1834 *
1835 * rule 1: select IPsec transport except AH
1836 * rule 2: select MIPv6 RO or inbound trigger
1837 * rule 3: select IPsec transport AH
1838 * rule 4: select IPsec tunnel
1839 * rule 5: others
1840 */
__xfrm6_state_sort_cmp(const void * p)1841 static int __xfrm6_state_sort_cmp(const void *p)
1842 {
1843 const struct xfrm_state *v = p;
1844
1845 switch (v->props.mode) {
1846 case XFRM_MODE_TRANSPORT:
1847 if (v->id.proto != IPPROTO_AH)
1848 return 1;
1849 else
1850 return 3;
1851 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1852 case XFRM_MODE_ROUTEOPTIMIZATION:
1853 case XFRM_MODE_IN_TRIGGER:
1854 return 2;
1855 #endif
1856 case XFRM_MODE_TUNNEL:
1857 case XFRM_MODE_BEET:
1858 return 4;
1859 }
1860 return 5;
1861 }
1862
1863 /* Rule for xfrm_tmpl:
1864 *
1865 * rule 1: select IPsec transport
1866 * rule 2: select MIPv6 RO or inbound trigger
1867 * rule 3: select IPsec tunnel
1868 * rule 4: others
1869 */
__xfrm6_tmpl_sort_cmp(const void * p)1870 static int __xfrm6_tmpl_sort_cmp(const void *p)
1871 {
1872 const struct xfrm_tmpl *v = p;
1873
1874 switch (v->mode) {
1875 case XFRM_MODE_TRANSPORT:
1876 return 1;
1877 #if IS_ENABLED(CONFIG_IPV6_MIP6)
1878 case XFRM_MODE_ROUTEOPTIMIZATION:
1879 case XFRM_MODE_IN_TRIGGER:
1880 return 2;
1881 #endif
1882 case XFRM_MODE_TUNNEL:
1883 case XFRM_MODE_BEET:
1884 return 3;
1885 }
1886 return 4;
1887 }
1888 #else
__xfrm6_state_sort_cmp(const void * p)1889 static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
__xfrm6_tmpl_sort_cmp(const void * p)1890 static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
1891
1892 static inline void
__xfrm6_sort(void ** dst,void ** src,int n,int (* cmp)(const void * p),int maxclass)1893 __xfrm6_sort(void **dst, void **src, int n,
1894 int (*cmp)(const void *p), int maxclass)
1895 {
1896 int i;
1897
1898 for (i = 0; i < n; i++)
1899 dst[i] = src[i];
1900 }
1901 #endif /* CONFIG_IPV6 */
1902
1903 void
xfrm_tmpl_sort(struct xfrm_tmpl ** dst,struct xfrm_tmpl ** src,int n,unsigned short family)1904 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1905 unsigned short family)
1906 {
1907 int i;
1908
1909 if (family == AF_INET6)
1910 __xfrm6_sort((void **)dst, (void **)src, n,
1911 __xfrm6_tmpl_sort_cmp, 5);
1912 else
1913 for (i = 0; i < n; i++)
1914 dst[i] = src[i];
1915 }
1916
1917 void
xfrm_state_sort(struct xfrm_state ** dst,struct xfrm_state ** src,int n,unsigned short family)1918 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1919 unsigned short family)
1920 {
1921 int i;
1922
1923 if (family == AF_INET6)
1924 __xfrm6_sort((void **)dst, (void **)src, n,
1925 __xfrm6_state_sort_cmp, 6);
1926 else
1927 for (i = 0; i < n; i++)
1928 dst[i] = src[i];
1929 }
1930 #endif
1931
1932 /* Silly enough, but I'm lazy to build resolution list */
1933
__xfrm_find_acq_byseq(struct net * net,u32 mark,u32 seq)1934 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1935 {
1936 int i;
1937
1938 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1939 struct xfrm_state *x;
1940
1941 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1942 if (x->km.seq == seq &&
1943 (mark & x->mark.m) == x->mark.v &&
1944 x->km.state == XFRM_STATE_ACQ) {
1945 xfrm_state_hold(x);
1946 return x;
1947 }
1948 }
1949 }
1950 return NULL;
1951 }
1952
xfrm_find_acq_byseq(struct net * net,u32 mark,u32 seq)1953 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1954 {
1955 struct xfrm_state *x;
1956
1957 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1958 x = __xfrm_find_acq_byseq(net, mark, seq);
1959 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1960 return x;
1961 }
1962 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1963
xfrm_get_acqseq(void)1964 u32 xfrm_get_acqseq(void)
1965 {
1966 u32 res;
1967 static atomic_t acqseq;
1968
1969 do {
1970 res = atomic_inc_return(&acqseq);
1971 } while (!res);
1972
1973 return res;
1974 }
1975 EXPORT_SYMBOL(xfrm_get_acqseq);
1976
verify_spi_info(u8 proto,u32 min,u32 max)1977 int verify_spi_info(u8 proto, u32 min, u32 max)
1978 {
1979 switch (proto) {
1980 case IPPROTO_AH:
1981 case IPPROTO_ESP:
1982 break;
1983
1984 case IPPROTO_COMP:
1985 /* IPCOMP spi is 16-bits. */
1986 if (max >= 0x10000)
1987 return -EINVAL;
1988 break;
1989
1990 default:
1991 return -EINVAL;
1992 }
1993
1994 if (min > max)
1995 return -EINVAL;
1996
1997 return 0;
1998 }
1999 EXPORT_SYMBOL(verify_spi_info);
2000
xfrm_alloc_spi(struct xfrm_state * x,u32 low,u32 high)2001 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
2002 {
2003 struct net *net = xs_net(x);
2004 unsigned int h;
2005 struct xfrm_state *x0;
2006 int err = -ENOENT;
2007 __be32 minspi = htonl(low);
2008 __be32 maxspi = htonl(high);
2009 __be32 newspi = 0;
2010 u32 mark = x->mark.v & x->mark.m;
2011
2012 spin_lock_bh(&x->lock);
2013 if (x->km.state == XFRM_STATE_DEAD)
2014 goto unlock;
2015
2016 err = 0;
2017 if (x->id.spi)
2018 goto unlock;
2019
2020 err = -ENOENT;
2021
2022 if (minspi == maxspi) {
2023 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
2024 if (x0) {
2025 xfrm_state_put(x0);
2026 goto unlock;
2027 }
2028 newspi = minspi;
2029 } else {
2030 u32 spi = 0;
2031 for (h = 0; h < high-low+1; h++) {
2032 spi = low + prandom_u32()%(high-low+1);
2033 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
2034 if (x0 == NULL) {
2035 newspi = htonl(spi);
2036 break;
2037 }
2038 xfrm_state_put(x0);
2039 }
2040 }
2041 if (newspi) {
2042 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2043 x->id.spi = newspi;
2044 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
2045 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
2046 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2047
2048 err = 0;
2049 }
2050
2051 unlock:
2052 spin_unlock_bh(&x->lock);
2053
2054 return err;
2055 }
2056 EXPORT_SYMBOL(xfrm_alloc_spi);
2057
__xfrm_state_filter_match(struct xfrm_state * x,struct xfrm_address_filter * filter)2058 static bool __xfrm_state_filter_match(struct xfrm_state *x,
2059 struct xfrm_address_filter *filter)
2060 {
2061 if (filter) {
2062 if ((filter->family == AF_INET ||
2063 filter->family == AF_INET6) &&
2064 x->props.family != filter->family)
2065 return false;
2066
2067 return addr_match(&x->props.saddr, &filter->saddr,
2068 filter->splen) &&
2069 addr_match(&x->id.daddr, &filter->daddr,
2070 filter->dplen);
2071 }
2072 return true;
2073 }
2074
xfrm_state_walk(struct net * net,struct xfrm_state_walk * walk,int (* func)(struct xfrm_state *,int,void *),void * data)2075 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
2076 int (*func)(struct xfrm_state *, int, void*),
2077 void *data)
2078 {
2079 struct xfrm_state *state;
2080 struct xfrm_state_walk *x;
2081 int err = 0;
2082
2083 if (walk->seq != 0 && list_empty(&walk->all))
2084 return 0;
2085
2086 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2087 if (list_empty(&walk->all))
2088 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
2089 else
2090 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
2091 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
2092 if (x->state == XFRM_STATE_DEAD)
2093 continue;
2094 state = container_of(x, struct xfrm_state, km);
2095 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
2096 continue;
2097 if (!__xfrm_state_filter_match(state, walk->filter))
2098 continue;
2099 err = func(state, walk->seq, data);
2100 if (err) {
2101 list_move_tail(&walk->all, &x->all);
2102 goto out;
2103 }
2104 walk->seq++;
2105 }
2106 if (walk->seq == 0) {
2107 err = -ENOENT;
2108 goto out;
2109 }
2110 list_del_init(&walk->all);
2111 out:
2112 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2113 return err;
2114 }
2115 EXPORT_SYMBOL(xfrm_state_walk);
2116
xfrm_state_walk_init(struct xfrm_state_walk * walk,u8 proto,struct xfrm_address_filter * filter)2117 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
2118 struct xfrm_address_filter *filter)
2119 {
2120 INIT_LIST_HEAD(&walk->all);
2121 walk->proto = proto;
2122 walk->state = XFRM_STATE_DEAD;
2123 walk->seq = 0;
2124 walk->filter = filter;
2125 }
2126 EXPORT_SYMBOL(xfrm_state_walk_init);
2127
xfrm_state_walk_done(struct xfrm_state_walk * walk,struct net * net)2128 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
2129 {
2130 kfree(walk->filter);
2131
2132 if (list_empty(&walk->all))
2133 return;
2134
2135 spin_lock_bh(&net->xfrm.xfrm_state_lock);
2136 list_del(&walk->all);
2137 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
2138 }
2139 EXPORT_SYMBOL(xfrm_state_walk_done);
2140
xfrm_replay_timer_handler(struct timer_list * t)2141 static void xfrm_replay_timer_handler(struct timer_list *t)
2142 {
2143 struct xfrm_state *x = from_timer(x, t, rtimer);
2144
2145 spin_lock(&x->lock);
2146
2147 if (x->km.state == XFRM_STATE_VALID) {
2148 if (xfrm_aevent_is_on(xs_net(x)))
2149 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
2150 else
2151 x->xflags |= XFRM_TIME_DEFER;
2152 }
2153
2154 spin_unlock(&x->lock);
2155 }
2156
2157 static LIST_HEAD(xfrm_km_list);
2158
km_policy_notify(struct xfrm_policy * xp,int dir,const struct km_event * c)2159 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
2160 {
2161 struct xfrm_mgr *km;
2162
2163 rcu_read_lock();
2164 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2165 if (km->notify_policy)
2166 km->notify_policy(xp, dir, c);
2167 rcu_read_unlock();
2168 }
2169
km_state_notify(struct xfrm_state * x,const struct km_event * c)2170 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
2171 {
2172 struct xfrm_mgr *km;
2173 rcu_read_lock();
2174 list_for_each_entry_rcu(km, &xfrm_km_list, list)
2175 if (km->notify)
2176 km->notify(x, c);
2177 rcu_read_unlock();
2178 }
2179
2180 EXPORT_SYMBOL(km_policy_notify);
2181 EXPORT_SYMBOL(km_state_notify);
2182
km_state_expired(struct xfrm_state * x,int hard,u32 portid)2183 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
2184 {
2185 struct km_event c;
2186
2187 c.data.hard = hard;
2188 c.portid = portid;
2189 c.event = XFRM_MSG_EXPIRE;
2190 km_state_notify(x, &c);
2191 }
2192
2193 EXPORT_SYMBOL(km_state_expired);
2194 /*
2195 * We send to all registered managers regardless of failure
2196 * We are happy with one success
2197 */
km_query(struct xfrm_state * x,struct xfrm_tmpl * t,struct xfrm_policy * pol)2198 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
2199 {
2200 int err = -EINVAL, acqret;
2201 struct xfrm_mgr *km;
2202
2203 rcu_read_lock();
2204 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2205 acqret = km->acquire(x, t, pol);
2206 if (!acqret)
2207 err = acqret;
2208 }
2209 rcu_read_unlock();
2210 return err;
2211 }
2212 EXPORT_SYMBOL(km_query);
2213
__km_new_mapping(struct xfrm_state * x,xfrm_address_t * ipaddr,__be16 sport)2214 static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2215 {
2216 int err = -EINVAL;
2217 struct xfrm_mgr *km;
2218
2219 rcu_read_lock();
2220 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2221 if (km->new_mapping)
2222 err = km->new_mapping(x, ipaddr, sport);
2223 if (!err)
2224 break;
2225 }
2226 rcu_read_unlock();
2227 return err;
2228 }
2229
km_new_mapping(struct xfrm_state * x,xfrm_address_t * ipaddr,__be16 sport)2230 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
2231 {
2232 int ret = 0;
2233
2234 if (x->mapping_maxage) {
2235 if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
2236 x->new_mapping_sport != sport) {
2237 x->new_mapping_sport = sport;
2238 x->new_mapping = jiffies / HZ;
2239 ret = __km_new_mapping(x, ipaddr, sport);
2240 }
2241 } else {
2242 ret = __km_new_mapping(x, ipaddr, sport);
2243 }
2244
2245 return ret;
2246 }
2247 EXPORT_SYMBOL(km_new_mapping);
2248
km_policy_expired(struct xfrm_policy * pol,int dir,int hard,u32 portid)2249 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
2250 {
2251 struct km_event c;
2252
2253 c.data.hard = hard;
2254 c.portid = portid;
2255 c.event = XFRM_MSG_POLEXPIRE;
2256 km_policy_notify(pol, dir, &c);
2257 }
2258 EXPORT_SYMBOL(km_policy_expired);
2259
2260 #ifdef CONFIG_XFRM_MIGRATE
km_migrate(const struct xfrm_selector * sel,u8 dir,u8 type,const struct xfrm_migrate * m,int num_migrate,const struct xfrm_kmaddress * k,const struct xfrm_encap_tmpl * encap)2261 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2262 const struct xfrm_migrate *m, int num_migrate,
2263 const struct xfrm_kmaddress *k,
2264 const struct xfrm_encap_tmpl *encap)
2265 {
2266 int err = -EINVAL;
2267 int ret;
2268 struct xfrm_mgr *km;
2269
2270 rcu_read_lock();
2271 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2272 if (km->migrate) {
2273 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2274 encap);
2275 if (!ret)
2276 err = ret;
2277 }
2278 }
2279 rcu_read_unlock();
2280 return err;
2281 }
2282 EXPORT_SYMBOL(km_migrate);
2283 #endif
2284
km_report(struct net * net,u8 proto,struct xfrm_selector * sel,xfrm_address_t * addr)2285 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
2286 {
2287 int err = -EINVAL;
2288 int ret;
2289 struct xfrm_mgr *km;
2290
2291 rcu_read_lock();
2292 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2293 if (km->report) {
2294 ret = km->report(net, proto, sel, addr);
2295 if (!ret)
2296 err = ret;
2297 }
2298 }
2299 rcu_read_unlock();
2300 return err;
2301 }
2302 EXPORT_SYMBOL(km_report);
2303
km_is_alive(const struct km_event * c)2304 static bool km_is_alive(const struct km_event *c)
2305 {
2306 struct xfrm_mgr *km;
2307 bool is_alive = false;
2308
2309 rcu_read_lock();
2310 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2311 if (km->is_alive && km->is_alive(c)) {
2312 is_alive = true;
2313 break;
2314 }
2315 }
2316 rcu_read_unlock();
2317
2318 return is_alive;
2319 }
2320
2321 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2322 static DEFINE_SPINLOCK(xfrm_translator_lock);
2323 static struct xfrm_translator __rcu *xfrm_translator;
2324
xfrm_get_translator(void)2325 struct xfrm_translator *xfrm_get_translator(void)
2326 {
2327 struct xfrm_translator *xtr;
2328
2329 rcu_read_lock();
2330 xtr = rcu_dereference(xfrm_translator);
2331 if (unlikely(!xtr))
2332 goto out;
2333 if (!try_module_get(xtr->owner))
2334 xtr = NULL;
2335 out:
2336 rcu_read_unlock();
2337 return xtr;
2338 }
2339 EXPORT_SYMBOL_GPL(xfrm_get_translator);
2340
xfrm_put_translator(struct xfrm_translator * xtr)2341 void xfrm_put_translator(struct xfrm_translator *xtr)
2342 {
2343 module_put(xtr->owner);
2344 }
2345 EXPORT_SYMBOL_GPL(xfrm_put_translator);
2346
xfrm_register_translator(struct xfrm_translator * xtr)2347 int xfrm_register_translator(struct xfrm_translator *xtr)
2348 {
2349 int err = 0;
2350
2351 spin_lock_bh(&xfrm_translator_lock);
2352 if (unlikely(xfrm_translator != NULL))
2353 err = -EEXIST;
2354 else
2355 rcu_assign_pointer(xfrm_translator, xtr);
2356 spin_unlock_bh(&xfrm_translator_lock);
2357
2358 return err;
2359 }
2360 EXPORT_SYMBOL_GPL(xfrm_register_translator);
2361
xfrm_unregister_translator(struct xfrm_translator * xtr)2362 int xfrm_unregister_translator(struct xfrm_translator *xtr)
2363 {
2364 int err = 0;
2365
2366 spin_lock_bh(&xfrm_translator_lock);
2367 if (likely(xfrm_translator != NULL)) {
2368 if (rcu_access_pointer(xfrm_translator) != xtr)
2369 err = -EINVAL;
2370 else
2371 RCU_INIT_POINTER(xfrm_translator, NULL);
2372 }
2373 spin_unlock_bh(&xfrm_translator_lock);
2374 synchronize_rcu();
2375
2376 return err;
2377 }
2378 EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2379 #endif
2380
xfrm_user_policy(struct sock * sk,int optname,sockptr_t optval,int optlen)2381 int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
2382 {
2383 int err;
2384 u8 *data;
2385 struct xfrm_mgr *km;
2386 struct xfrm_policy *pol = NULL;
2387
2388 if (sockptr_is_null(optval) && !optlen) {
2389 xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
2390 xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
2391 __sk_dst_reset(sk);
2392 return 0;
2393 }
2394
2395 if (optlen <= 0 || optlen > PAGE_SIZE)
2396 return -EMSGSIZE;
2397
2398 data = memdup_sockptr(optval, optlen);
2399 if (IS_ERR(data))
2400 return PTR_ERR(data);
2401
2402 if (in_compat_syscall()) {
2403 struct xfrm_translator *xtr = xfrm_get_translator();
2404
2405 if (!xtr) {
2406 kfree(data);
2407 return -EOPNOTSUPP;
2408 }
2409
2410 err = xtr->xlate_user_policy_sockptr(&data, optlen);
2411 xfrm_put_translator(xtr);
2412 if (err) {
2413 kfree(data);
2414 return err;
2415 }
2416 }
2417
2418 err = -EINVAL;
2419 rcu_read_lock();
2420 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
2421 pol = km->compile_policy(sk, optname, data,
2422 optlen, &err);
2423 if (err >= 0)
2424 break;
2425 }
2426 rcu_read_unlock();
2427
2428 if (err >= 0) {
2429 xfrm_sk_policy_insert(sk, err, pol);
2430 xfrm_pol_put(pol);
2431 __sk_dst_reset(sk);
2432 err = 0;
2433 }
2434
2435 kfree(data);
2436 return err;
2437 }
2438 EXPORT_SYMBOL(xfrm_user_policy);
2439
2440 static DEFINE_SPINLOCK(xfrm_km_lock);
2441
xfrm_register_km(struct xfrm_mgr * km)2442 int xfrm_register_km(struct xfrm_mgr *km)
2443 {
2444 spin_lock_bh(&xfrm_km_lock);
2445 list_add_tail_rcu(&km->list, &xfrm_km_list);
2446 spin_unlock_bh(&xfrm_km_lock);
2447 return 0;
2448 }
2449 EXPORT_SYMBOL(xfrm_register_km);
2450
xfrm_unregister_km(struct xfrm_mgr * km)2451 int xfrm_unregister_km(struct xfrm_mgr *km)
2452 {
2453 spin_lock_bh(&xfrm_km_lock);
2454 list_del_rcu(&km->list);
2455 spin_unlock_bh(&xfrm_km_lock);
2456 synchronize_rcu();
2457 return 0;
2458 }
2459 EXPORT_SYMBOL(xfrm_unregister_km);
2460
xfrm_state_register_afinfo(struct xfrm_state_afinfo * afinfo)2461 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2462 {
2463 int err = 0;
2464
2465 if (WARN_ON(afinfo->family >= NPROTO))
2466 return -EAFNOSUPPORT;
2467
2468 spin_lock_bh(&xfrm_state_afinfo_lock);
2469 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
2470 err = -EEXIST;
2471 else
2472 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2473 spin_unlock_bh(&xfrm_state_afinfo_lock);
2474 return err;
2475 }
2476 EXPORT_SYMBOL(xfrm_state_register_afinfo);
2477
xfrm_state_unregister_afinfo(struct xfrm_state_afinfo * afinfo)2478 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2479 {
2480 int err = 0, family = afinfo->family;
2481
2482 if (WARN_ON(family >= NPROTO))
2483 return -EAFNOSUPPORT;
2484
2485 spin_lock_bh(&xfrm_state_afinfo_lock);
2486 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
2487 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
2488 err = -EINVAL;
2489 else
2490 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
2491 }
2492 spin_unlock_bh(&xfrm_state_afinfo_lock);
2493 synchronize_rcu();
2494 return err;
2495 }
2496 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2497
xfrm_state_afinfo_get_rcu(unsigned int family)2498 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
2499 {
2500 if (unlikely(family >= NPROTO))
2501 return NULL;
2502
2503 return rcu_dereference(xfrm_state_afinfo[family]);
2504 }
2505 EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
2506
xfrm_state_get_afinfo(unsigned int family)2507 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
2508 {
2509 struct xfrm_state_afinfo *afinfo;
2510 if (unlikely(family >= NPROTO))
2511 return NULL;
2512 rcu_read_lock();
2513 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
2514 if (unlikely(!afinfo))
2515 rcu_read_unlock();
2516 return afinfo;
2517 }
2518
xfrm_flush_gc(void)2519 void xfrm_flush_gc(void)
2520 {
2521 flush_work(&xfrm_state_gc_work);
2522 }
2523 EXPORT_SYMBOL(xfrm_flush_gc);
2524
2525 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
xfrm_state_delete_tunnel(struct xfrm_state * x)2526 void xfrm_state_delete_tunnel(struct xfrm_state *x)
2527 {
2528 if (x->tunnel) {
2529 struct xfrm_state *t = x->tunnel;
2530
2531 if (atomic_read(&t->tunnel_users) == 2)
2532 xfrm_state_delete(t);
2533 atomic_dec(&t->tunnel_users);
2534 xfrm_state_put_sync(t);
2535 x->tunnel = NULL;
2536 }
2537 }
2538 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2539
__xfrm_state_mtu(struct xfrm_state * x,int mtu)2540 u32 __xfrm_state_mtu(struct xfrm_state *x, int mtu)
2541 {
2542 const struct xfrm_type *type = READ_ONCE(x->type);
2543 struct crypto_aead *aead;
2544 u32 blksize, net_adj = 0;
2545
2546 if (x->km.state != XFRM_STATE_VALID ||
2547 !type || type->proto != IPPROTO_ESP)
2548 return mtu - x->props.header_len;
2549
2550 aead = x->data;
2551 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2552
2553 switch (x->props.mode) {
2554 case XFRM_MODE_TRANSPORT:
2555 case XFRM_MODE_BEET:
2556 if (x->props.family == AF_INET)
2557 net_adj = sizeof(struct iphdr);
2558 else if (x->props.family == AF_INET6)
2559 net_adj = sizeof(struct ipv6hdr);
2560 break;
2561 case XFRM_MODE_TUNNEL:
2562 break;
2563 default:
2564 WARN_ON_ONCE(1);
2565 break;
2566 }
2567
2568 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2569 net_adj) & ~(blksize - 1)) + net_adj - 2;
2570 }
2571 EXPORT_SYMBOL_GPL(__xfrm_state_mtu);
2572
xfrm_state_mtu(struct xfrm_state * x,int mtu)2573 u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
2574 {
2575 mtu = __xfrm_state_mtu(x, mtu);
2576
2577 if (x->props.family == AF_INET6 && mtu < IPV6_MIN_MTU)
2578 return IPV6_MIN_MTU;
2579
2580 return mtu;
2581 }
2582
__xfrm_init_state(struct xfrm_state * x,bool init_replay,bool offload)2583 int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload)
2584 {
2585 const struct xfrm_mode *inner_mode;
2586 const struct xfrm_mode *outer_mode;
2587 int family = x->props.family;
2588 int err;
2589
2590 if (family == AF_INET &&
2591 xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc)
2592 x->props.flags |= XFRM_STATE_NOPMTUDISC;
2593
2594 err = -EPROTONOSUPPORT;
2595
2596 if (x->sel.family != AF_UNSPEC) {
2597 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2598 if (inner_mode == NULL)
2599 goto error;
2600
2601 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2602 family != x->sel.family)
2603 goto error;
2604
2605 x->inner_mode = *inner_mode;
2606 } else {
2607 const struct xfrm_mode *inner_mode_iaf;
2608 int iafamily = AF_INET;
2609
2610 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2611 if (inner_mode == NULL)
2612 goto error;
2613
2614 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL))
2615 goto error;
2616
2617 x->inner_mode = *inner_mode;
2618
2619 if (x->props.family == AF_INET)
2620 iafamily = AF_INET6;
2621
2622 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2623 if (inner_mode_iaf) {
2624 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2625 x->inner_mode_iaf = *inner_mode_iaf;
2626 }
2627 }
2628
2629 x->type = xfrm_get_type(x->id.proto, family);
2630 if (x->type == NULL)
2631 goto error;
2632
2633 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
2634
2635 err = x->type->init_state(x);
2636 if (err)
2637 goto error;
2638
2639 outer_mode = xfrm_get_mode(x->props.mode, family);
2640 if (!outer_mode) {
2641 err = -EPROTONOSUPPORT;
2642 goto error;
2643 }
2644
2645 x->outer_mode = *outer_mode;
2646 if (init_replay) {
2647 err = xfrm_init_replay(x);
2648 if (err)
2649 goto error;
2650 }
2651
2652 error:
2653 return err;
2654 }
2655
2656 EXPORT_SYMBOL(__xfrm_init_state);
2657
xfrm_init_state(struct xfrm_state * x)2658 int xfrm_init_state(struct xfrm_state *x)
2659 {
2660 int err;
2661
2662 err = __xfrm_init_state(x, true, false);
2663 if (!err)
2664 x->km.state = XFRM_STATE_VALID;
2665
2666 return err;
2667 }
2668
2669 EXPORT_SYMBOL(xfrm_init_state);
2670
xfrm_state_init(struct net * net)2671 int __net_init xfrm_state_init(struct net *net)
2672 {
2673 unsigned int sz;
2674
2675 if (net_eq(net, &init_net))
2676 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2677 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2678
2679 INIT_LIST_HEAD(&net->xfrm.state_all);
2680
2681 sz = sizeof(struct hlist_head) * 8;
2682
2683 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2684 if (!net->xfrm.state_bydst)
2685 goto out_bydst;
2686 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2687 if (!net->xfrm.state_bysrc)
2688 goto out_bysrc;
2689 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2690 if (!net->xfrm.state_byspi)
2691 goto out_byspi;
2692 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2693
2694 net->xfrm.state_num = 0;
2695 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2696 spin_lock_init(&net->xfrm.xfrm_state_lock);
2697 seqcount_init(&net->xfrm.xfrm_state_hash_generation);
2698 return 0;
2699
2700 out_byspi:
2701 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2702 out_bysrc:
2703 xfrm_hash_free(net->xfrm.state_bydst, sz);
2704 out_bydst:
2705 return -ENOMEM;
2706 }
2707
xfrm_state_fini(struct net * net)2708 void xfrm_state_fini(struct net *net)
2709 {
2710 unsigned int sz;
2711
2712 flush_work(&net->xfrm.state_hash_work);
2713 flush_work(&xfrm_state_gc_work);
2714 xfrm_state_flush(net, 0, false, true);
2715
2716 WARN_ON(!list_empty(&net->xfrm.state_all));
2717
2718 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2719 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2720 xfrm_hash_free(net->xfrm.state_byspi, sz);
2721 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2722 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2723 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2724 xfrm_hash_free(net->xfrm.state_bydst, sz);
2725 }
2726
2727 #ifdef CONFIG_AUDITSYSCALL
xfrm_audit_helper_sainfo(struct xfrm_state * x,struct audit_buffer * audit_buf)2728 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2729 struct audit_buffer *audit_buf)
2730 {
2731 struct xfrm_sec_ctx *ctx = x->security;
2732 u32 spi = ntohl(x->id.spi);
2733
2734 if (ctx)
2735 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2736 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2737
2738 switch (x->props.family) {
2739 case AF_INET:
2740 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2741 &x->props.saddr.a4, &x->id.daddr.a4);
2742 break;
2743 case AF_INET6:
2744 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2745 x->props.saddr.a6, x->id.daddr.a6);
2746 break;
2747 }
2748
2749 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2750 }
2751
xfrm_audit_helper_pktinfo(struct sk_buff * skb,u16 family,struct audit_buffer * audit_buf)2752 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2753 struct audit_buffer *audit_buf)
2754 {
2755 const struct iphdr *iph4;
2756 const struct ipv6hdr *iph6;
2757
2758 switch (family) {
2759 case AF_INET:
2760 iph4 = ip_hdr(skb);
2761 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2762 &iph4->saddr, &iph4->daddr);
2763 break;
2764 case AF_INET6:
2765 iph6 = ipv6_hdr(skb);
2766 audit_log_format(audit_buf,
2767 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2768 &iph6->saddr, &iph6->daddr,
2769 iph6->flow_lbl[0] & 0x0f,
2770 iph6->flow_lbl[1],
2771 iph6->flow_lbl[2]);
2772 break;
2773 }
2774 }
2775
xfrm_audit_state_add(struct xfrm_state * x,int result,bool task_valid)2776 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2777 {
2778 struct audit_buffer *audit_buf;
2779
2780 audit_buf = xfrm_audit_start("SAD-add");
2781 if (audit_buf == NULL)
2782 return;
2783 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2784 xfrm_audit_helper_sainfo(x, audit_buf);
2785 audit_log_format(audit_buf, " res=%u", result);
2786 audit_log_end(audit_buf);
2787 }
2788 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2789
xfrm_audit_state_delete(struct xfrm_state * x,int result,bool task_valid)2790 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2791 {
2792 struct audit_buffer *audit_buf;
2793
2794 audit_buf = xfrm_audit_start("SAD-delete");
2795 if (audit_buf == NULL)
2796 return;
2797 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2798 xfrm_audit_helper_sainfo(x, audit_buf);
2799 audit_log_format(audit_buf, " res=%u", result);
2800 audit_log_end(audit_buf);
2801 }
2802 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2803
xfrm_audit_state_replay_overflow(struct xfrm_state * x,struct sk_buff * skb)2804 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2805 struct sk_buff *skb)
2806 {
2807 struct audit_buffer *audit_buf;
2808 u32 spi;
2809
2810 audit_buf = xfrm_audit_start("SA-replay-overflow");
2811 if (audit_buf == NULL)
2812 return;
2813 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2814 /* don't record the sequence number because it's inherent in this kind
2815 * of audit message */
2816 spi = ntohl(x->id.spi);
2817 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2818 audit_log_end(audit_buf);
2819 }
2820 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2821
xfrm_audit_state_replay(struct xfrm_state * x,struct sk_buff * skb,__be32 net_seq)2822 void xfrm_audit_state_replay(struct xfrm_state *x,
2823 struct sk_buff *skb, __be32 net_seq)
2824 {
2825 struct audit_buffer *audit_buf;
2826 u32 spi;
2827
2828 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2829 if (audit_buf == NULL)
2830 return;
2831 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2832 spi = ntohl(x->id.spi);
2833 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2834 spi, spi, ntohl(net_seq));
2835 audit_log_end(audit_buf);
2836 }
2837 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2838
xfrm_audit_state_notfound_simple(struct sk_buff * skb,u16 family)2839 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2840 {
2841 struct audit_buffer *audit_buf;
2842
2843 audit_buf = xfrm_audit_start("SA-notfound");
2844 if (audit_buf == NULL)
2845 return;
2846 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2847 audit_log_end(audit_buf);
2848 }
2849 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2850
xfrm_audit_state_notfound(struct sk_buff * skb,u16 family,__be32 net_spi,__be32 net_seq)2851 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2852 __be32 net_spi, __be32 net_seq)
2853 {
2854 struct audit_buffer *audit_buf;
2855 u32 spi;
2856
2857 audit_buf = xfrm_audit_start("SA-notfound");
2858 if (audit_buf == NULL)
2859 return;
2860 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2861 spi = ntohl(net_spi);
2862 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2863 spi, spi, ntohl(net_seq));
2864 audit_log_end(audit_buf);
2865 }
2866 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2867
xfrm_audit_state_icvfail(struct xfrm_state * x,struct sk_buff * skb,u8 proto)2868 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2869 struct sk_buff *skb, u8 proto)
2870 {
2871 struct audit_buffer *audit_buf;
2872 __be32 net_spi;
2873 __be32 net_seq;
2874
2875 audit_buf = xfrm_audit_start("SA-icv-failure");
2876 if (audit_buf == NULL)
2877 return;
2878 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2879 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2880 u32 spi = ntohl(net_spi);
2881 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2882 spi, spi, ntohl(net_seq));
2883 }
2884 audit_log_end(audit_buf);
2885 }
2886 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2887 #endif /* CONFIG_AUDITSYSCALL */
2888