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