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