]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/xfrm/xfrm_state.c
xfrm: state: simplify rcu_read_unlock handling in two spots
[mirror_ubuntu-artful-kernel.git] / net / xfrm / xfrm_state.c
1 /*
2 * xfrm_state.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * YOSHIFUJI Hideaki @USAGI
10 * Split up af-specific functions
11 * Derek Atkins <derek@ihtfp.com>
12 * Add UDP Encapsulation
13 *
14 */
15
16 #include <linux/workqueue.h>
17 #include <net/xfrm.h>
18 #include <linux/pfkeyv2.h>
19 #include <linux/ipsec.h>
20 #include <linux/module.h>
21 #include <linux/cache.h>
22 #include <linux/audit.h>
23 #include <linux/uaccess.h>
24 #include <linux/ktime.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28
29 #include "xfrm_hash.h"
30
31 #define xfrm_state_deref_prot(table, net) \
32 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
33
34 static void xfrm_state_gc_task(struct work_struct *work);
35
36 /* Each xfrm_state may be linked to two tables:
37
38 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
39 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
40 destination/tunnel endpoint. (output)
41 */
42
43 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
44 static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
45
46 static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
47 static HLIST_HEAD(xfrm_state_gc_list);
48
49 static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
50 {
51 return atomic_inc_not_zero(&x->refcnt);
52 }
53
54 static inline unsigned int xfrm_dst_hash(struct net *net,
55 const xfrm_address_t *daddr,
56 const xfrm_address_t *saddr,
57 u32 reqid,
58 unsigned short family)
59 {
60 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
61 }
62
63 static inline unsigned int xfrm_src_hash(struct net *net,
64 const xfrm_address_t *daddr,
65 const xfrm_address_t *saddr,
66 unsigned short family)
67 {
68 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
69 }
70
71 static inline unsigned int
72 xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
73 __be32 spi, u8 proto, unsigned short family)
74 {
75 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
76 }
77
78 static void xfrm_hash_transfer(struct hlist_head *list,
79 struct hlist_head *ndsttable,
80 struct hlist_head *nsrctable,
81 struct hlist_head *nspitable,
82 unsigned int nhashmask)
83 {
84 struct hlist_node *tmp;
85 struct xfrm_state *x;
86
87 hlist_for_each_entry_safe(x, tmp, list, bydst) {
88 unsigned int h;
89
90 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
91 x->props.reqid, x->props.family,
92 nhashmask);
93 hlist_add_head_rcu(&x->bydst, ndsttable + h);
94
95 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
96 x->props.family,
97 nhashmask);
98 hlist_add_head_rcu(&x->bysrc, nsrctable + h);
99
100 if (x->id.spi) {
101 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
102 x->id.proto, x->props.family,
103 nhashmask);
104 hlist_add_head_rcu(&x->byspi, nspitable + h);
105 }
106 }
107 }
108
109 static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
110 {
111 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
112 }
113
114 static void xfrm_hash_resize(struct work_struct *work)
115 {
116 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
117 struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
118 unsigned long nsize, osize;
119 unsigned int nhashmask, ohashmask;
120 int i;
121
122 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
123 ndst = xfrm_hash_alloc(nsize);
124 if (!ndst)
125 return;
126 nsrc = xfrm_hash_alloc(nsize);
127 if (!nsrc) {
128 xfrm_hash_free(ndst, nsize);
129 return;
130 }
131 nspi = xfrm_hash_alloc(nsize);
132 if (!nspi) {
133 xfrm_hash_free(ndst, nsize);
134 xfrm_hash_free(nsrc, nsize);
135 return;
136 }
137
138 spin_lock_bh(&net->xfrm.xfrm_state_lock);
139 write_seqcount_begin(&xfrm_state_hash_generation);
140
141 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
142 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
143 for (i = net->xfrm.state_hmask; i >= 0; i--)
144 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
145
146 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
147 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
148 ohashmask = net->xfrm.state_hmask;
149
150 rcu_assign_pointer(net->xfrm.state_bydst, ndst);
151 rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
152 rcu_assign_pointer(net->xfrm.state_byspi, nspi);
153 net->xfrm.state_hmask = nhashmask;
154
155 write_seqcount_end(&xfrm_state_hash_generation);
156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
157
158 osize = (ohashmask + 1) * sizeof(struct hlist_head);
159
160 synchronize_rcu();
161
162 xfrm_hash_free(odst, osize);
163 xfrm_hash_free(osrc, osize);
164 xfrm_hash_free(ospi, osize);
165 }
166
167 static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
168 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
169
170 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
171
172 int __xfrm_state_delete(struct xfrm_state *x);
173
174 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
175 bool km_is_alive(const struct km_event *c);
176 void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
177
178 static DEFINE_SPINLOCK(xfrm_type_lock);
179 int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
180 {
181 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
182 const struct xfrm_type **typemap;
183 int err = 0;
184
185 if (unlikely(afinfo == NULL))
186 return -EAFNOSUPPORT;
187 typemap = afinfo->type_map;
188 spin_lock_bh(&xfrm_type_lock);
189
190 if (likely(typemap[type->proto] == NULL))
191 typemap[type->proto] = type;
192 else
193 err = -EEXIST;
194 spin_unlock_bh(&xfrm_type_lock);
195 rcu_read_unlock();
196 return err;
197 }
198 EXPORT_SYMBOL(xfrm_register_type);
199
200 int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
201 {
202 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
203 const struct xfrm_type **typemap;
204 int err = 0;
205
206 if (unlikely(afinfo == NULL))
207 return -EAFNOSUPPORT;
208 typemap = afinfo->type_map;
209 spin_lock_bh(&xfrm_type_lock);
210
211 if (unlikely(typemap[type->proto] != type))
212 err = -ENOENT;
213 else
214 typemap[type->proto] = NULL;
215 spin_unlock_bh(&xfrm_type_lock);
216 rcu_read_unlock();
217 return err;
218 }
219 EXPORT_SYMBOL(xfrm_unregister_type);
220
221 static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
222 {
223 struct xfrm_state_afinfo *afinfo;
224 const struct xfrm_type **typemap;
225 const struct xfrm_type *type;
226 int modload_attempted = 0;
227
228 retry:
229 afinfo = xfrm_state_get_afinfo(family);
230 if (unlikely(afinfo == NULL))
231 return NULL;
232 typemap = afinfo->type_map;
233
234 type = READ_ONCE(typemap[proto]);
235 if (unlikely(type && !try_module_get(type->owner)))
236 type = NULL;
237
238 rcu_read_unlock();
239
240 if (!type && !modload_attempted) {
241 request_module("xfrm-type-%d-%d", family, proto);
242 modload_attempted = 1;
243 goto retry;
244 }
245
246 return type;
247 }
248
249 static void xfrm_put_type(const struct xfrm_type *type)
250 {
251 module_put(type->owner);
252 }
253
254 static DEFINE_SPINLOCK(xfrm_mode_lock);
255 int xfrm_register_mode(struct xfrm_mode *mode, int family)
256 {
257 struct xfrm_state_afinfo *afinfo;
258 struct xfrm_mode **modemap;
259 int err;
260
261 if (unlikely(mode->encap >= XFRM_MODE_MAX))
262 return -EINVAL;
263
264 afinfo = xfrm_state_get_afinfo(family);
265 if (unlikely(afinfo == NULL))
266 return -EAFNOSUPPORT;
267
268 err = -EEXIST;
269 modemap = afinfo->mode_map;
270 spin_lock_bh(&xfrm_mode_lock);
271 if (modemap[mode->encap])
272 goto out;
273
274 err = -ENOENT;
275 if (!try_module_get(afinfo->owner))
276 goto out;
277
278 mode->afinfo = afinfo;
279 modemap[mode->encap] = mode;
280 err = 0;
281
282 out:
283 spin_unlock_bh(&xfrm_mode_lock);
284 rcu_read_unlock();
285 return err;
286 }
287 EXPORT_SYMBOL(xfrm_register_mode);
288
289 int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
290 {
291 struct xfrm_state_afinfo *afinfo;
292 struct xfrm_mode **modemap;
293 int err;
294
295 if (unlikely(mode->encap >= XFRM_MODE_MAX))
296 return -EINVAL;
297
298 afinfo = xfrm_state_get_afinfo(family);
299 if (unlikely(afinfo == NULL))
300 return -EAFNOSUPPORT;
301
302 err = -ENOENT;
303 modemap = afinfo->mode_map;
304 spin_lock_bh(&xfrm_mode_lock);
305 if (likely(modemap[mode->encap] == mode)) {
306 modemap[mode->encap] = NULL;
307 module_put(mode->afinfo->owner);
308 err = 0;
309 }
310
311 spin_unlock_bh(&xfrm_mode_lock);
312 rcu_read_unlock();
313 return err;
314 }
315 EXPORT_SYMBOL(xfrm_unregister_mode);
316
317 static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
318 {
319 struct xfrm_state_afinfo *afinfo;
320 struct xfrm_mode *mode;
321 int modload_attempted = 0;
322
323 if (unlikely(encap >= XFRM_MODE_MAX))
324 return NULL;
325
326 retry:
327 afinfo = xfrm_state_get_afinfo(family);
328 if (unlikely(afinfo == NULL))
329 return NULL;
330
331 mode = READ_ONCE(afinfo->mode_map[encap]);
332 if (unlikely(mode && !try_module_get(mode->owner)))
333 mode = NULL;
334
335 rcu_read_unlock();
336 if (!mode && !modload_attempted) {
337 request_module("xfrm-mode-%d-%d", family, encap);
338 modload_attempted = 1;
339 goto retry;
340 }
341
342 return mode;
343 }
344
345 static void xfrm_put_mode(struct xfrm_mode *mode)
346 {
347 module_put(mode->owner);
348 }
349
350 static void xfrm_state_gc_destroy(struct xfrm_state *x)
351 {
352 tasklet_hrtimer_cancel(&x->mtimer);
353 del_timer_sync(&x->rtimer);
354 kfree(x->aead);
355 kfree(x->aalg);
356 kfree(x->ealg);
357 kfree(x->calg);
358 kfree(x->encap);
359 kfree(x->coaddr);
360 kfree(x->replay_esn);
361 kfree(x->preplay_esn);
362 if (x->inner_mode)
363 xfrm_put_mode(x->inner_mode);
364 if (x->inner_mode_iaf)
365 xfrm_put_mode(x->inner_mode_iaf);
366 if (x->outer_mode)
367 xfrm_put_mode(x->outer_mode);
368 if (x->type) {
369 x->type->destructor(x);
370 xfrm_put_type(x->type);
371 }
372 security_xfrm_state_free(x);
373 kfree(x);
374 }
375
376 static void xfrm_state_gc_task(struct work_struct *work)
377 {
378 struct xfrm_state *x;
379 struct hlist_node *tmp;
380 struct hlist_head gc_list;
381
382 spin_lock_bh(&xfrm_state_gc_lock);
383 hlist_move_list(&xfrm_state_gc_list, &gc_list);
384 spin_unlock_bh(&xfrm_state_gc_lock);
385
386 synchronize_rcu();
387
388 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
389 xfrm_state_gc_destroy(x);
390 }
391
392 static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
393 {
394 struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
395 struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
396 unsigned long now = get_seconds();
397 long next = LONG_MAX;
398 int warn = 0;
399 int err = 0;
400
401 spin_lock(&x->lock);
402 if (x->km.state == XFRM_STATE_DEAD)
403 goto out;
404 if (x->km.state == XFRM_STATE_EXPIRED)
405 goto expired;
406 if (x->lft.hard_add_expires_seconds) {
407 long tmo = x->lft.hard_add_expires_seconds +
408 x->curlft.add_time - now;
409 if (tmo <= 0) {
410 if (x->xflags & XFRM_SOFT_EXPIRE) {
411 /* enter hard expire without soft expire first?!
412 * setting a new date could trigger this.
413 * workaround: fix x->curflt.add_time by below:
414 */
415 x->curlft.add_time = now - x->saved_tmo - 1;
416 tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
417 } else
418 goto expired;
419 }
420 if (tmo < next)
421 next = tmo;
422 }
423 if (x->lft.hard_use_expires_seconds) {
424 long tmo = x->lft.hard_use_expires_seconds +
425 (x->curlft.use_time ? : now) - now;
426 if (tmo <= 0)
427 goto expired;
428 if (tmo < next)
429 next = tmo;
430 }
431 if (x->km.dying)
432 goto resched;
433 if (x->lft.soft_add_expires_seconds) {
434 long tmo = x->lft.soft_add_expires_seconds +
435 x->curlft.add_time - now;
436 if (tmo <= 0) {
437 warn = 1;
438 x->xflags &= ~XFRM_SOFT_EXPIRE;
439 } else if (tmo < next) {
440 next = tmo;
441 x->xflags |= XFRM_SOFT_EXPIRE;
442 x->saved_tmo = tmo;
443 }
444 }
445 if (x->lft.soft_use_expires_seconds) {
446 long tmo = x->lft.soft_use_expires_seconds +
447 (x->curlft.use_time ? : now) - now;
448 if (tmo <= 0)
449 warn = 1;
450 else if (tmo < next)
451 next = tmo;
452 }
453
454 x->km.dying = warn;
455 if (warn)
456 km_state_expired(x, 0, 0);
457 resched:
458 if (next != LONG_MAX) {
459 tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
460 }
461
462 goto out;
463
464 expired:
465 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
466 x->km.state = XFRM_STATE_EXPIRED;
467
468 err = __xfrm_state_delete(x);
469 if (!err)
470 km_state_expired(x, 1, 0);
471
472 xfrm_audit_state_delete(x, err ? 0 : 1, true);
473
474 out:
475 spin_unlock(&x->lock);
476 return HRTIMER_NORESTART;
477 }
478
479 static void xfrm_replay_timer_handler(unsigned long data);
480
481 struct xfrm_state *xfrm_state_alloc(struct net *net)
482 {
483 struct xfrm_state *x;
484
485 x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
486
487 if (x) {
488 write_pnet(&x->xs_net, net);
489 atomic_set(&x->refcnt, 1);
490 atomic_set(&x->tunnel_users, 0);
491 INIT_LIST_HEAD(&x->km.all);
492 INIT_HLIST_NODE(&x->bydst);
493 INIT_HLIST_NODE(&x->bysrc);
494 INIT_HLIST_NODE(&x->byspi);
495 tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
496 CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
497 setup_timer(&x->rtimer, xfrm_replay_timer_handler,
498 (unsigned long)x);
499 x->curlft.add_time = get_seconds();
500 x->lft.soft_byte_limit = XFRM_INF;
501 x->lft.soft_packet_limit = XFRM_INF;
502 x->lft.hard_byte_limit = XFRM_INF;
503 x->lft.hard_packet_limit = XFRM_INF;
504 x->replay_maxage = 0;
505 x->replay_maxdiff = 0;
506 x->inner_mode = NULL;
507 x->inner_mode_iaf = NULL;
508 spin_lock_init(&x->lock);
509 }
510 return x;
511 }
512 EXPORT_SYMBOL(xfrm_state_alloc);
513
514 void __xfrm_state_destroy(struct xfrm_state *x)
515 {
516 WARN_ON(x->km.state != XFRM_STATE_DEAD);
517
518 spin_lock_bh(&xfrm_state_gc_lock);
519 hlist_add_head(&x->gclist, &xfrm_state_gc_list);
520 spin_unlock_bh(&xfrm_state_gc_lock);
521 schedule_work(&xfrm_state_gc_work);
522 }
523 EXPORT_SYMBOL(__xfrm_state_destroy);
524
525 int __xfrm_state_delete(struct xfrm_state *x)
526 {
527 struct net *net = xs_net(x);
528 int err = -ESRCH;
529
530 if (x->km.state != XFRM_STATE_DEAD) {
531 x->km.state = XFRM_STATE_DEAD;
532 spin_lock(&net->xfrm.xfrm_state_lock);
533 list_del(&x->km.all);
534 hlist_del_rcu(&x->bydst);
535 hlist_del_rcu(&x->bysrc);
536 if (x->id.spi)
537 hlist_del_rcu(&x->byspi);
538 net->xfrm.state_num--;
539 spin_unlock(&net->xfrm.xfrm_state_lock);
540
541 /* All xfrm_state objects are created by xfrm_state_alloc.
542 * The xfrm_state_alloc call gives a reference, and that
543 * is what we are dropping here.
544 */
545 xfrm_state_put(x);
546 err = 0;
547 }
548
549 return err;
550 }
551 EXPORT_SYMBOL(__xfrm_state_delete);
552
553 int xfrm_state_delete(struct xfrm_state *x)
554 {
555 int err;
556
557 spin_lock_bh(&x->lock);
558 err = __xfrm_state_delete(x);
559 spin_unlock_bh(&x->lock);
560
561 return err;
562 }
563 EXPORT_SYMBOL(xfrm_state_delete);
564
565 #ifdef CONFIG_SECURITY_NETWORK_XFRM
566 static inline int
567 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
568 {
569 int i, err = 0;
570
571 for (i = 0; i <= net->xfrm.state_hmask; i++) {
572 struct xfrm_state *x;
573
574 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
575 if (xfrm_id_proto_match(x->id.proto, proto) &&
576 (err = security_xfrm_state_delete(x)) != 0) {
577 xfrm_audit_state_delete(x, 0, task_valid);
578 return err;
579 }
580 }
581 }
582
583 return err;
584 }
585 #else
586 static inline int
587 xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
588 {
589 return 0;
590 }
591 #endif
592
593 int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
594 {
595 int i, err = 0, cnt = 0;
596
597 spin_lock_bh(&net->xfrm.xfrm_state_lock);
598 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
599 if (err)
600 goto out;
601
602 err = -ESRCH;
603 for (i = 0; i <= net->xfrm.state_hmask; i++) {
604 struct xfrm_state *x;
605 restart:
606 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
607 if (!xfrm_state_kern(x) &&
608 xfrm_id_proto_match(x->id.proto, proto)) {
609 xfrm_state_hold(x);
610 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
611
612 err = xfrm_state_delete(x);
613 xfrm_audit_state_delete(x, err ? 0 : 1,
614 task_valid);
615 xfrm_state_put(x);
616 if (!err)
617 cnt++;
618
619 spin_lock_bh(&net->xfrm.xfrm_state_lock);
620 goto restart;
621 }
622 }
623 }
624 if (cnt)
625 err = 0;
626
627 out:
628 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
629 return err;
630 }
631 EXPORT_SYMBOL(xfrm_state_flush);
632
633 void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
634 {
635 spin_lock_bh(&net->xfrm.xfrm_state_lock);
636 si->sadcnt = net->xfrm.state_num;
637 si->sadhcnt = net->xfrm.state_hmask;
638 si->sadhmcnt = xfrm_state_hashmax;
639 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
640 }
641 EXPORT_SYMBOL(xfrm_sad_getinfo);
642
643 static void
644 xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
645 const struct xfrm_tmpl *tmpl,
646 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
647 unsigned short family)
648 {
649 struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family);
650
651 if (afinfo)
652 afinfo->init_tempsel(&x->sel, fl);
653
654 if (family != tmpl->encap_family) {
655 afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family);
656 if (!afinfo)
657 return;
658 }
659 afinfo->init_temprop(x, tmpl, daddr, saddr);
660 }
661
662 static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
663 const xfrm_address_t *daddr,
664 __be32 spi, u8 proto,
665 unsigned short family)
666 {
667 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
668 struct xfrm_state *x;
669
670 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
671 if (x->props.family != family ||
672 x->id.spi != spi ||
673 x->id.proto != proto ||
674 !xfrm_addr_equal(&x->id.daddr, daddr, family))
675 continue;
676
677 if ((mark & x->mark.m) != x->mark.v)
678 continue;
679 if (!xfrm_state_hold_rcu(x))
680 continue;
681 return x;
682 }
683
684 return NULL;
685 }
686
687 static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
688 const xfrm_address_t *daddr,
689 const xfrm_address_t *saddr,
690 u8 proto, unsigned short family)
691 {
692 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
693 struct xfrm_state *x;
694
695 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
696 if (x->props.family != family ||
697 x->id.proto != proto ||
698 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
699 !xfrm_addr_equal(&x->props.saddr, saddr, family))
700 continue;
701
702 if ((mark & x->mark.m) != x->mark.v)
703 continue;
704 if (!xfrm_state_hold_rcu(x))
705 continue;
706 return x;
707 }
708
709 return NULL;
710 }
711
712 static inline struct xfrm_state *
713 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
714 {
715 struct net *net = xs_net(x);
716 u32 mark = x->mark.v & x->mark.m;
717
718 if (use_spi)
719 return __xfrm_state_lookup(net, mark, &x->id.daddr,
720 x->id.spi, x->id.proto, family);
721 else
722 return __xfrm_state_lookup_byaddr(net, mark,
723 &x->id.daddr,
724 &x->props.saddr,
725 x->id.proto, family);
726 }
727
728 static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
729 {
730 if (have_hash_collision &&
731 (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
732 net->xfrm.state_num > net->xfrm.state_hmask)
733 schedule_work(&net->xfrm.state_hash_work);
734 }
735
736 static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
737 const struct flowi *fl, unsigned short family,
738 struct xfrm_state **best, int *acq_in_progress,
739 int *error)
740 {
741 /* Resolution logic:
742 * 1. There is a valid state with matching selector. Done.
743 * 2. Valid state with inappropriate selector. Skip.
744 *
745 * Entering area of "sysdeps".
746 *
747 * 3. If state is not valid, selector is temporary, it selects
748 * only session which triggered previous resolution. Key
749 * manager will do something to install a state with proper
750 * selector.
751 */
752 if (x->km.state == XFRM_STATE_VALID) {
753 if ((x->sel.family &&
754 !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
755 !security_xfrm_state_pol_flow_match(x, pol, fl))
756 return;
757
758 if (!*best ||
759 (*best)->km.dying > x->km.dying ||
760 ((*best)->km.dying == x->km.dying &&
761 (*best)->curlft.add_time < x->curlft.add_time))
762 *best = x;
763 } else if (x->km.state == XFRM_STATE_ACQ) {
764 *acq_in_progress = 1;
765 } else if (x->km.state == XFRM_STATE_ERROR ||
766 x->km.state == XFRM_STATE_EXPIRED) {
767 if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
768 security_xfrm_state_pol_flow_match(x, pol, fl))
769 *error = -ESRCH;
770 }
771 }
772
773 struct xfrm_state *
774 xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
775 const struct flowi *fl, struct xfrm_tmpl *tmpl,
776 struct xfrm_policy *pol, int *err,
777 unsigned short family)
778 {
779 static xfrm_address_t saddr_wildcard = { };
780 struct net *net = xp_net(pol);
781 unsigned int h, h_wildcard;
782 struct xfrm_state *x, *x0, *to_put;
783 int acquire_in_progress = 0;
784 int error = 0;
785 struct xfrm_state *best = NULL;
786 u32 mark = pol->mark.v & pol->mark.m;
787 unsigned short encap_family = tmpl->encap_family;
788 unsigned int sequence;
789 struct km_event c;
790
791 to_put = NULL;
792
793 sequence = read_seqcount_begin(&xfrm_state_hash_generation);
794
795 rcu_read_lock();
796 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
797 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
798 if (x->props.family == encap_family &&
799 x->props.reqid == tmpl->reqid &&
800 (mark & x->mark.m) == x->mark.v &&
801 !(x->props.flags & XFRM_STATE_WILDRECV) &&
802 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
803 tmpl->mode == x->props.mode &&
804 tmpl->id.proto == x->id.proto &&
805 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
806 xfrm_state_look_at(pol, x, fl, encap_family,
807 &best, &acquire_in_progress, &error);
808 }
809 if (best || acquire_in_progress)
810 goto found;
811
812 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
813 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
814 if (x->props.family == encap_family &&
815 x->props.reqid == tmpl->reqid &&
816 (mark & x->mark.m) == x->mark.v &&
817 !(x->props.flags & XFRM_STATE_WILDRECV) &&
818 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
819 tmpl->mode == x->props.mode &&
820 tmpl->id.proto == x->id.proto &&
821 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
822 xfrm_state_look_at(pol, x, fl, encap_family,
823 &best, &acquire_in_progress, &error);
824 }
825
826 found:
827 x = best;
828 if (!x && !error && !acquire_in_progress) {
829 if (tmpl->id.spi &&
830 (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
831 tmpl->id.proto, encap_family)) != NULL) {
832 to_put = x0;
833 error = -EEXIST;
834 goto out;
835 }
836
837 c.net = net;
838 /* If the KMs have no listeners (yet...), avoid allocating an SA
839 * for each and every packet - garbage collection might not
840 * handle the flood.
841 */
842 if (!km_is_alive(&c)) {
843 error = -ESRCH;
844 goto out;
845 }
846
847 x = xfrm_state_alloc(net);
848 if (x == NULL) {
849 error = -ENOMEM;
850 goto out;
851 }
852 /* Initialize temporary state matching only
853 * to current session. */
854 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
855 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
856
857 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
858 if (error) {
859 x->km.state = XFRM_STATE_DEAD;
860 to_put = x;
861 x = NULL;
862 goto out;
863 }
864
865 if (km_query(x, tmpl, pol) == 0) {
866 spin_lock_bh(&net->xfrm.xfrm_state_lock);
867 x->km.state = XFRM_STATE_ACQ;
868 list_add(&x->km.all, &net->xfrm.state_all);
869 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
870 h = xfrm_src_hash(net, daddr, saddr, encap_family);
871 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
872 if (x->id.spi) {
873 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
874 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
875 }
876 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
877 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
878 net->xfrm.state_num++;
879 xfrm_hash_grow_check(net, x->bydst.next != NULL);
880 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
881 } else {
882 x->km.state = XFRM_STATE_DEAD;
883 to_put = x;
884 x = NULL;
885 error = -ESRCH;
886 }
887 }
888 out:
889 if (x) {
890 if (!xfrm_state_hold_rcu(x)) {
891 *err = -EAGAIN;
892 x = NULL;
893 }
894 } else {
895 *err = acquire_in_progress ? -EAGAIN : error;
896 }
897 rcu_read_unlock();
898 if (to_put)
899 xfrm_state_put(to_put);
900
901 if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
902 *err = -EAGAIN;
903 if (x) {
904 xfrm_state_put(x);
905 x = NULL;
906 }
907 }
908
909 return x;
910 }
911
912 struct xfrm_state *
913 xfrm_stateonly_find(struct net *net, u32 mark,
914 xfrm_address_t *daddr, xfrm_address_t *saddr,
915 unsigned short family, u8 mode, u8 proto, u32 reqid)
916 {
917 unsigned int h;
918 struct xfrm_state *rx = NULL, *x = NULL;
919
920 spin_lock_bh(&net->xfrm.xfrm_state_lock);
921 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
922 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
923 if (x->props.family == family &&
924 x->props.reqid == reqid &&
925 (mark & x->mark.m) == x->mark.v &&
926 !(x->props.flags & XFRM_STATE_WILDRECV) &&
927 xfrm_state_addr_check(x, daddr, saddr, family) &&
928 mode == x->props.mode &&
929 proto == x->id.proto &&
930 x->km.state == XFRM_STATE_VALID) {
931 rx = x;
932 break;
933 }
934 }
935
936 if (rx)
937 xfrm_state_hold(rx);
938 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
939
940
941 return rx;
942 }
943 EXPORT_SYMBOL(xfrm_stateonly_find);
944
945 struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
946 unsigned short family)
947 {
948 struct xfrm_state *x;
949 struct xfrm_state_walk *w;
950
951 spin_lock_bh(&net->xfrm.xfrm_state_lock);
952 list_for_each_entry(w, &net->xfrm.state_all, all) {
953 x = container_of(w, struct xfrm_state, km);
954 if (x->props.family != family ||
955 x->id.spi != spi)
956 continue;
957
958 xfrm_state_hold(x);
959 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
960 return x;
961 }
962 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
963 return NULL;
964 }
965 EXPORT_SYMBOL(xfrm_state_lookup_byspi);
966
967 static void __xfrm_state_insert(struct xfrm_state *x)
968 {
969 struct net *net = xs_net(x);
970 unsigned int h;
971
972 list_add(&x->km.all, &net->xfrm.state_all);
973
974 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
975 x->props.reqid, x->props.family);
976 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
977
978 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
979 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
980
981 if (x->id.spi) {
982 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
983 x->props.family);
984
985 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
986 }
987
988 tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
989 if (x->replay_maxage)
990 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
991
992 net->xfrm.state_num++;
993
994 xfrm_hash_grow_check(net, x->bydst.next != NULL);
995 }
996
997 /* net->xfrm.xfrm_state_lock is held */
998 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
999 {
1000 struct net *net = xs_net(xnew);
1001 unsigned short family = xnew->props.family;
1002 u32 reqid = xnew->props.reqid;
1003 struct xfrm_state *x;
1004 unsigned int h;
1005 u32 mark = xnew->mark.v & xnew->mark.m;
1006
1007 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
1008 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1009 if (x->props.family == family &&
1010 x->props.reqid == reqid &&
1011 (mark & x->mark.m) == x->mark.v &&
1012 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1013 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
1014 x->genid++;
1015 }
1016 }
1017
1018 void xfrm_state_insert(struct xfrm_state *x)
1019 {
1020 struct net *net = xs_net(x);
1021
1022 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1023 __xfrm_state_bump_genids(x);
1024 __xfrm_state_insert(x);
1025 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1026 }
1027 EXPORT_SYMBOL(xfrm_state_insert);
1028
1029 /* net->xfrm.xfrm_state_lock is held */
1030 static struct xfrm_state *__find_acq_core(struct net *net,
1031 const struct xfrm_mark *m,
1032 unsigned short family, u8 mode,
1033 u32 reqid, u8 proto,
1034 const xfrm_address_t *daddr,
1035 const xfrm_address_t *saddr,
1036 int create)
1037 {
1038 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
1039 struct xfrm_state *x;
1040 u32 mark = m->v & m->m;
1041
1042 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1043 if (x->props.reqid != reqid ||
1044 x->props.mode != mode ||
1045 x->props.family != family ||
1046 x->km.state != XFRM_STATE_ACQ ||
1047 x->id.spi != 0 ||
1048 x->id.proto != proto ||
1049 (mark & x->mark.m) != x->mark.v ||
1050 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1051 !xfrm_addr_equal(&x->props.saddr, saddr, family))
1052 continue;
1053
1054 xfrm_state_hold(x);
1055 return x;
1056 }
1057
1058 if (!create)
1059 return NULL;
1060
1061 x = xfrm_state_alloc(net);
1062 if (likely(x)) {
1063 switch (family) {
1064 case AF_INET:
1065 x->sel.daddr.a4 = daddr->a4;
1066 x->sel.saddr.a4 = saddr->a4;
1067 x->sel.prefixlen_d = 32;
1068 x->sel.prefixlen_s = 32;
1069 x->props.saddr.a4 = saddr->a4;
1070 x->id.daddr.a4 = daddr->a4;
1071 break;
1072
1073 case AF_INET6:
1074 x->sel.daddr.in6 = daddr->in6;
1075 x->sel.saddr.in6 = saddr->in6;
1076 x->sel.prefixlen_d = 128;
1077 x->sel.prefixlen_s = 128;
1078 x->props.saddr.in6 = saddr->in6;
1079 x->id.daddr.in6 = daddr->in6;
1080 break;
1081 }
1082
1083 x->km.state = XFRM_STATE_ACQ;
1084 x->id.proto = proto;
1085 x->props.family = family;
1086 x->props.mode = mode;
1087 x->props.reqid = reqid;
1088 x->mark.v = m->v;
1089 x->mark.m = m->m;
1090 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
1091 xfrm_state_hold(x);
1092 tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
1093 list_add(&x->km.all, &net->xfrm.state_all);
1094 hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
1095 h = xfrm_src_hash(net, daddr, saddr, family);
1096 hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
1097
1098 net->xfrm.state_num++;
1099
1100 xfrm_hash_grow_check(net, x->bydst.next != NULL);
1101 }
1102
1103 return x;
1104 }
1105
1106 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
1107
1108 int xfrm_state_add(struct xfrm_state *x)
1109 {
1110 struct net *net = xs_net(x);
1111 struct xfrm_state *x1, *to_put;
1112 int family;
1113 int err;
1114 u32 mark = x->mark.v & x->mark.m;
1115 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1116
1117 family = x->props.family;
1118
1119 to_put = NULL;
1120
1121 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1122
1123 x1 = __xfrm_state_locate(x, use_spi, family);
1124 if (x1) {
1125 to_put = x1;
1126 x1 = NULL;
1127 err = -EEXIST;
1128 goto out;
1129 }
1130
1131 if (use_spi && x->km.seq) {
1132 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
1133 if (x1 && ((x1->id.proto != x->id.proto) ||
1134 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
1135 to_put = x1;
1136 x1 = NULL;
1137 }
1138 }
1139
1140 if (use_spi && !x1)
1141 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
1142 x->props.reqid, x->id.proto,
1143 &x->id.daddr, &x->props.saddr, 0);
1144
1145 __xfrm_state_bump_genids(x);
1146 __xfrm_state_insert(x);
1147 err = 0;
1148
1149 out:
1150 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1151
1152 if (x1) {
1153 xfrm_state_delete(x1);
1154 xfrm_state_put(x1);
1155 }
1156
1157 if (to_put)
1158 xfrm_state_put(to_put);
1159
1160 return err;
1161 }
1162 EXPORT_SYMBOL(xfrm_state_add);
1163
1164 #ifdef CONFIG_XFRM_MIGRATE
1165 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
1166 {
1167 struct net *net = xs_net(orig);
1168 struct xfrm_state *x = xfrm_state_alloc(net);
1169 if (!x)
1170 goto out;
1171
1172 memcpy(&x->id, &orig->id, sizeof(x->id));
1173 memcpy(&x->sel, &orig->sel, sizeof(x->sel));
1174 memcpy(&x->lft, &orig->lft, sizeof(x->lft));
1175 x->props.mode = orig->props.mode;
1176 x->props.replay_window = orig->props.replay_window;
1177 x->props.reqid = orig->props.reqid;
1178 x->props.family = orig->props.family;
1179 x->props.saddr = orig->props.saddr;
1180
1181 if (orig->aalg) {
1182 x->aalg = xfrm_algo_auth_clone(orig->aalg);
1183 if (!x->aalg)
1184 goto error;
1185 }
1186 x->props.aalgo = orig->props.aalgo;
1187
1188 if (orig->aead) {
1189 x->aead = xfrm_algo_aead_clone(orig->aead);
1190 if (!x->aead)
1191 goto error;
1192 }
1193 if (orig->ealg) {
1194 x->ealg = xfrm_algo_clone(orig->ealg);
1195 if (!x->ealg)
1196 goto error;
1197 }
1198 x->props.ealgo = orig->props.ealgo;
1199
1200 if (orig->calg) {
1201 x->calg = xfrm_algo_clone(orig->calg);
1202 if (!x->calg)
1203 goto error;
1204 }
1205 x->props.calgo = orig->props.calgo;
1206
1207 if (orig->encap) {
1208 x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
1209 if (!x->encap)
1210 goto error;
1211 }
1212
1213 if (orig->coaddr) {
1214 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1215 GFP_KERNEL);
1216 if (!x->coaddr)
1217 goto error;
1218 }
1219
1220 if (orig->replay_esn) {
1221 if (xfrm_replay_clone(x, orig))
1222 goto error;
1223 }
1224
1225 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
1226
1227 if (xfrm_init_state(x) < 0)
1228 goto error;
1229
1230 x->props.flags = orig->props.flags;
1231 x->props.extra_flags = orig->props.extra_flags;
1232
1233 x->tfcpad = orig->tfcpad;
1234 x->replay_maxdiff = orig->replay_maxdiff;
1235 x->replay_maxage = orig->replay_maxage;
1236 x->curlft.add_time = orig->curlft.add_time;
1237 x->km.state = orig->km.state;
1238 x->km.seq = orig->km.seq;
1239
1240 return x;
1241
1242 error:
1243 xfrm_state_put(x);
1244 out:
1245 return NULL;
1246 }
1247
1248 struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
1249 {
1250 unsigned int h;
1251 struct xfrm_state *x = NULL;
1252
1253 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1254
1255 if (m->reqid) {
1256 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
1257 m->reqid, m->old_family);
1258 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
1259 if (x->props.mode != m->mode ||
1260 x->id.proto != m->proto)
1261 continue;
1262 if (m->reqid && x->props.reqid != m->reqid)
1263 continue;
1264 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1265 m->old_family) ||
1266 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1267 m->old_family))
1268 continue;
1269 xfrm_state_hold(x);
1270 break;
1271 }
1272 } else {
1273 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
1274 m->old_family);
1275 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
1276 if (x->props.mode != m->mode ||
1277 x->id.proto != m->proto)
1278 continue;
1279 if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
1280 m->old_family) ||
1281 !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
1282 m->old_family))
1283 continue;
1284 xfrm_state_hold(x);
1285 break;
1286 }
1287 }
1288
1289 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1290
1291 return x;
1292 }
1293 EXPORT_SYMBOL(xfrm_migrate_state_find);
1294
1295 struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
1296 struct xfrm_migrate *m)
1297 {
1298 struct xfrm_state *xc;
1299
1300 xc = xfrm_state_clone(x);
1301 if (!xc)
1302 return NULL;
1303
1304 memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
1305 memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
1306
1307 /* add state */
1308 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
1309 /* a care is needed when the destination address of the
1310 state is to be updated as it is a part of triplet */
1311 xfrm_state_insert(xc);
1312 } else {
1313 if (xfrm_state_add(xc) < 0)
1314 goto error;
1315 }
1316
1317 return xc;
1318 error:
1319 xfrm_state_put(xc);
1320 return NULL;
1321 }
1322 EXPORT_SYMBOL(xfrm_state_migrate);
1323 #endif
1324
1325 int xfrm_state_update(struct xfrm_state *x)
1326 {
1327 struct xfrm_state *x1, *to_put;
1328 int err;
1329 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
1330 struct net *net = xs_net(x);
1331
1332 to_put = NULL;
1333
1334 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1335 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
1336
1337 err = -ESRCH;
1338 if (!x1)
1339 goto out;
1340
1341 if (xfrm_state_kern(x1)) {
1342 to_put = x1;
1343 err = -EEXIST;
1344 goto out;
1345 }
1346
1347 if (x1->km.state == XFRM_STATE_ACQ) {
1348 __xfrm_state_insert(x);
1349 x = NULL;
1350 }
1351 err = 0;
1352
1353 out:
1354 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1355
1356 if (to_put)
1357 xfrm_state_put(to_put);
1358
1359 if (err)
1360 return err;
1361
1362 if (!x) {
1363 xfrm_state_delete(x1);
1364 xfrm_state_put(x1);
1365 return 0;
1366 }
1367
1368 err = -EINVAL;
1369 spin_lock_bh(&x1->lock);
1370 if (likely(x1->km.state == XFRM_STATE_VALID)) {
1371 if (x->encap && x1->encap)
1372 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
1373 if (x->coaddr && x1->coaddr) {
1374 memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
1375 }
1376 if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
1377 memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
1378 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1379 x1->km.dying = 0;
1380
1381 tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
1382 if (x1->curlft.use_time)
1383 xfrm_state_check_expire(x1);
1384
1385 err = 0;
1386 x->km.state = XFRM_STATE_DEAD;
1387 __xfrm_state_put(x);
1388 }
1389 spin_unlock_bh(&x1->lock);
1390
1391 xfrm_state_put(x1);
1392
1393 return err;
1394 }
1395 EXPORT_SYMBOL(xfrm_state_update);
1396
1397 int xfrm_state_check_expire(struct xfrm_state *x)
1398 {
1399 if (!x->curlft.use_time)
1400 x->curlft.use_time = get_seconds();
1401
1402 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1403 x->curlft.packets >= x->lft.hard_packet_limit) {
1404 x->km.state = XFRM_STATE_EXPIRED;
1405 tasklet_hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL);
1406 return -EINVAL;
1407 }
1408
1409 if (!x->km.dying &&
1410 (x->curlft.bytes >= x->lft.soft_byte_limit ||
1411 x->curlft.packets >= x->lft.soft_packet_limit)) {
1412 x->km.dying = 1;
1413 km_state_expired(x, 0, 0);
1414 }
1415 return 0;
1416 }
1417 EXPORT_SYMBOL(xfrm_state_check_expire);
1418
1419 struct xfrm_state *
1420 xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
1421 u8 proto, unsigned short family)
1422 {
1423 struct xfrm_state *x;
1424
1425 rcu_read_lock();
1426 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
1427 rcu_read_unlock();
1428 return x;
1429 }
1430 EXPORT_SYMBOL(xfrm_state_lookup);
1431
1432 struct xfrm_state *
1433 xfrm_state_lookup_byaddr(struct net *net, u32 mark,
1434 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
1435 u8 proto, unsigned short family)
1436 {
1437 struct xfrm_state *x;
1438
1439 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1440 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
1441 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1442 return x;
1443 }
1444 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1445
1446 struct xfrm_state *
1447 xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
1448 u8 proto, const xfrm_address_t *daddr,
1449 const xfrm_address_t *saddr, int create, unsigned short family)
1450 {
1451 struct xfrm_state *x;
1452
1453 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1454 x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
1455 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1456
1457 return x;
1458 }
1459 EXPORT_SYMBOL(xfrm_find_acq);
1460
1461 #ifdef CONFIG_XFRM_SUB_POLICY
1462 int
1463 xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
1464 unsigned short family, struct net *net)
1465 {
1466 int err = 0;
1467 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1468 if (!afinfo)
1469 return -EAFNOSUPPORT;
1470
1471 spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
1472 if (afinfo->tmpl_sort)
1473 err = afinfo->tmpl_sort(dst, src, n);
1474 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1475 rcu_read_unlock();
1476 return err;
1477 }
1478 EXPORT_SYMBOL(xfrm_tmpl_sort);
1479
1480 int
1481 xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
1482 unsigned short family)
1483 {
1484 int err = 0;
1485 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
1486 struct net *net = xs_net(*src);
1487
1488 if (!afinfo)
1489 return -EAFNOSUPPORT;
1490
1491 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1492 if (afinfo->state_sort)
1493 err = afinfo->state_sort(dst, src, n);
1494 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1495 rcu_read_unlock();
1496 return err;
1497 }
1498 EXPORT_SYMBOL(xfrm_state_sort);
1499 #endif
1500
1501 /* Silly enough, but I'm lazy to build resolution list */
1502
1503 static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1504 {
1505 int i;
1506
1507 for (i = 0; i <= net->xfrm.state_hmask; i++) {
1508 struct xfrm_state *x;
1509
1510 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
1511 if (x->km.seq == seq &&
1512 (mark & x->mark.m) == x->mark.v &&
1513 x->km.state == XFRM_STATE_ACQ) {
1514 xfrm_state_hold(x);
1515 return x;
1516 }
1517 }
1518 }
1519 return NULL;
1520 }
1521
1522 struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
1523 {
1524 struct xfrm_state *x;
1525
1526 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1527 x = __xfrm_find_acq_byseq(net, mark, seq);
1528 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1529 return x;
1530 }
1531 EXPORT_SYMBOL(xfrm_find_acq_byseq);
1532
1533 u32 xfrm_get_acqseq(void)
1534 {
1535 u32 res;
1536 static atomic_t acqseq;
1537
1538 do {
1539 res = atomic_inc_return(&acqseq);
1540 } while (!res);
1541
1542 return res;
1543 }
1544 EXPORT_SYMBOL(xfrm_get_acqseq);
1545
1546 int verify_spi_info(u8 proto, u32 min, u32 max)
1547 {
1548 switch (proto) {
1549 case IPPROTO_AH:
1550 case IPPROTO_ESP:
1551 break;
1552
1553 case IPPROTO_COMP:
1554 /* IPCOMP spi is 16-bits. */
1555 if (max >= 0x10000)
1556 return -EINVAL;
1557 break;
1558
1559 default:
1560 return -EINVAL;
1561 }
1562
1563 if (min > max)
1564 return -EINVAL;
1565
1566 return 0;
1567 }
1568 EXPORT_SYMBOL(verify_spi_info);
1569
1570 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
1571 {
1572 struct net *net = xs_net(x);
1573 unsigned int h;
1574 struct xfrm_state *x0;
1575 int err = -ENOENT;
1576 __be32 minspi = htonl(low);
1577 __be32 maxspi = htonl(high);
1578 u32 mark = x->mark.v & x->mark.m;
1579
1580 spin_lock_bh(&x->lock);
1581 if (x->km.state == XFRM_STATE_DEAD)
1582 goto unlock;
1583
1584 err = 0;
1585 if (x->id.spi)
1586 goto unlock;
1587
1588 err = -ENOENT;
1589
1590 if (minspi == maxspi) {
1591 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
1592 if (x0) {
1593 xfrm_state_put(x0);
1594 goto unlock;
1595 }
1596 x->id.spi = minspi;
1597 } else {
1598 u32 spi = 0;
1599 for (h = 0; h < high-low+1; h++) {
1600 spi = low + prandom_u32()%(high-low+1);
1601 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
1602 if (x0 == NULL) {
1603 x->id.spi = htonl(spi);
1604 break;
1605 }
1606 xfrm_state_put(x0);
1607 }
1608 }
1609 if (x->id.spi) {
1610 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1611 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
1612 hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
1613 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1614
1615 err = 0;
1616 }
1617
1618 unlock:
1619 spin_unlock_bh(&x->lock);
1620
1621 return err;
1622 }
1623 EXPORT_SYMBOL(xfrm_alloc_spi);
1624
1625 static bool __xfrm_state_filter_match(struct xfrm_state *x,
1626 struct xfrm_address_filter *filter)
1627 {
1628 if (filter) {
1629 if ((filter->family == AF_INET ||
1630 filter->family == AF_INET6) &&
1631 x->props.family != filter->family)
1632 return false;
1633
1634 return addr_match(&x->props.saddr, &filter->saddr,
1635 filter->splen) &&
1636 addr_match(&x->id.daddr, &filter->daddr,
1637 filter->dplen);
1638 }
1639 return true;
1640 }
1641
1642 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
1643 int (*func)(struct xfrm_state *, int, void*),
1644 void *data)
1645 {
1646 struct xfrm_state *state;
1647 struct xfrm_state_walk *x;
1648 int err = 0;
1649
1650 if (walk->seq != 0 && list_empty(&walk->all))
1651 return 0;
1652
1653 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1654 if (list_empty(&walk->all))
1655 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
1656 else
1657 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
1658 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
1659 if (x->state == XFRM_STATE_DEAD)
1660 continue;
1661 state = container_of(x, struct xfrm_state, km);
1662 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
1663 continue;
1664 if (!__xfrm_state_filter_match(state, walk->filter))
1665 continue;
1666 err = func(state, walk->seq, data);
1667 if (err) {
1668 list_move_tail(&walk->all, &x->all);
1669 goto out;
1670 }
1671 walk->seq++;
1672 }
1673 if (walk->seq == 0) {
1674 err = -ENOENT;
1675 goto out;
1676 }
1677 list_del_init(&walk->all);
1678 out:
1679 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1680 return err;
1681 }
1682 EXPORT_SYMBOL(xfrm_state_walk);
1683
1684 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
1685 struct xfrm_address_filter *filter)
1686 {
1687 INIT_LIST_HEAD(&walk->all);
1688 walk->proto = proto;
1689 walk->state = XFRM_STATE_DEAD;
1690 walk->seq = 0;
1691 walk->filter = filter;
1692 }
1693 EXPORT_SYMBOL(xfrm_state_walk_init);
1694
1695 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
1696 {
1697 kfree(walk->filter);
1698
1699 if (list_empty(&walk->all))
1700 return;
1701
1702 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1703 list_del(&walk->all);
1704 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1705 }
1706 EXPORT_SYMBOL(xfrm_state_walk_done);
1707
1708 static void xfrm_replay_timer_handler(unsigned long data)
1709 {
1710 struct xfrm_state *x = (struct xfrm_state *)data;
1711
1712 spin_lock(&x->lock);
1713
1714 if (x->km.state == XFRM_STATE_VALID) {
1715 if (xfrm_aevent_is_on(xs_net(x)))
1716 x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
1717 else
1718 x->xflags |= XFRM_TIME_DEFER;
1719 }
1720
1721 spin_unlock(&x->lock);
1722 }
1723
1724 static LIST_HEAD(xfrm_km_list);
1725
1726 void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
1727 {
1728 struct xfrm_mgr *km;
1729
1730 rcu_read_lock();
1731 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1732 if (km->notify_policy)
1733 km->notify_policy(xp, dir, c);
1734 rcu_read_unlock();
1735 }
1736
1737 void km_state_notify(struct xfrm_state *x, const struct km_event *c)
1738 {
1739 struct xfrm_mgr *km;
1740 rcu_read_lock();
1741 list_for_each_entry_rcu(km, &xfrm_km_list, list)
1742 if (km->notify)
1743 km->notify(x, c);
1744 rcu_read_unlock();
1745 }
1746
1747 EXPORT_SYMBOL(km_policy_notify);
1748 EXPORT_SYMBOL(km_state_notify);
1749
1750 void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
1751 {
1752 struct km_event c;
1753
1754 c.data.hard = hard;
1755 c.portid = portid;
1756 c.event = XFRM_MSG_EXPIRE;
1757 km_state_notify(x, &c);
1758 }
1759
1760 EXPORT_SYMBOL(km_state_expired);
1761 /*
1762 * We send to all registered managers regardless of failure
1763 * We are happy with one success
1764 */
1765 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
1766 {
1767 int err = -EINVAL, acqret;
1768 struct xfrm_mgr *km;
1769
1770 rcu_read_lock();
1771 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1772 acqret = km->acquire(x, t, pol);
1773 if (!acqret)
1774 err = acqret;
1775 }
1776 rcu_read_unlock();
1777 return err;
1778 }
1779 EXPORT_SYMBOL(km_query);
1780
1781 int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
1782 {
1783 int err = -EINVAL;
1784 struct xfrm_mgr *km;
1785
1786 rcu_read_lock();
1787 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1788 if (km->new_mapping)
1789 err = km->new_mapping(x, ipaddr, sport);
1790 if (!err)
1791 break;
1792 }
1793 rcu_read_unlock();
1794 return err;
1795 }
1796 EXPORT_SYMBOL(km_new_mapping);
1797
1798 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
1799 {
1800 struct km_event c;
1801
1802 c.data.hard = hard;
1803 c.portid = portid;
1804 c.event = XFRM_MSG_POLEXPIRE;
1805 km_policy_notify(pol, dir, &c);
1806 }
1807 EXPORT_SYMBOL(km_policy_expired);
1808
1809 #ifdef CONFIG_XFRM_MIGRATE
1810 int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
1811 const struct xfrm_migrate *m, int num_migrate,
1812 const struct xfrm_kmaddress *k)
1813 {
1814 int err = -EINVAL;
1815 int ret;
1816 struct xfrm_mgr *km;
1817
1818 rcu_read_lock();
1819 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1820 if (km->migrate) {
1821 ret = km->migrate(sel, dir, type, m, num_migrate, k);
1822 if (!ret)
1823 err = ret;
1824 }
1825 }
1826 rcu_read_unlock();
1827 return err;
1828 }
1829 EXPORT_SYMBOL(km_migrate);
1830 #endif
1831
1832 int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
1833 {
1834 int err = -EINVAL;
1835 int ret;
1836 struct xfrm_mgr *km;
1837
1838 rcu_read_lock();
1839 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1840 if (km->report) {
1841 ret = km->report(net, proto, sel, addr);
1842 if (!ret)
1843 err = ret;
1844 }
1845 }
1846 rcu_read_unlock();
1847 return err;
1848 }
1849 EXPORT_SYMBOL(km_report);
1850
1851 bool km_is_alive(const struct km_event *c)
1852 {
1853 struct xfrm_mgr *km;
1854 bool is_alive = false;
1855
1856 rcu_read_lock();
1857 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1858 if (km->is_alive && km->is_alive(c)) {
1859 is_alive = true;
1860 break;
1861 }
1862 }
1863 rcu_read_unlock();
1864
1865 return is_alive;
1866 }
1867 EXPORT_SYMBOL(km_is_alive);
1868
1869 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
1870 {
1871 int err;
1872 u8 *data;
1873 struct xfrm_mgr *km;
1874 struct xfrm_policy *pol = NULL;
1875
1876 if (optlen <= 0 || optlen > PAGE_SIZE)
1877 return -EMSGSIZE;
1878
1879 data = kmalloc(optlen, GFP_KERNEL);
1880 if (!data)
1881 return -ENOMEM;
1882
1883 err = -EFAULT;
1884 if (copy_from_user(data, optval, optlen))
1885 goto out;
1886
1887 err = -EINVAL;
1888 rcu_read_lock();
1889 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
1890 pol = km->compile_policy(sk, optname, data,
1891 optlen, &err);
1892 if (err >= 0)
1893 break;
1894 }
1895 rcu_read_unlock();
1896
1897 if (err >= 0) {
1898 xfrm_sk_policy_insert(sk, err, pol);
1899 xfrm_pol_put(pol);
1900 err = 0;
1901 }
1902
1903 out:
1904 kfree(data);
1905 return err;
1906 }
1907 EXPORT_SYMBOL(xfrm_user_policy);
1908
1909 static DEFINE_SPINLOCK(xfrm_km_lock);
1910
1911 int xfrm_register_km(struct xfrm_mgr *km)
1912 {
1913 spin_lock_bh(&xfrm_km_lock);
1914 list_add_tail_rcu(&km->list, &xfrm_km_list);
1915 spin_unlock_bh(&xfrm_km_lock);
1916 return 0;
1917 }
1918 EXPORT_SYMBOL(xfrm_register_km);
1919
1920 int xfrm_unregister_km(struct xfrm_mgr *km)
1921 {
1922 spin_lock_bh(&xfrm_km_lock);
1923 list_del_rcu(&km->list);
1924 spin_unlock_bh(&xfrm_km_lock);
1925 synchronize_rcu();
1926 return 0;
1927 }
1928 EXPORT_SYMBOL(xfrm_unregister_km);
1929
1930 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
1931 {
1932 int err = 0;
1933
1934 if (WARN_ON(afinfo->family >= NPROTO))
1935 return -EAFNOSUPPORT;
1936
1937 spin_lock_bh(&xfrm_state_afinfo_lock);
1938 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
1939 err = -EEXIST;
1940 else
1941 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
1942 spin_unlock_bh(&xfrm_state_afinfo_lock);
1943 return err;
1944 }
1945 EXPORT_SYMBOL(xfrm_state_register_afinfo);
1946
1947 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
1948 {
1949 int err = 0, family = afinfo->family;
1950
1951 if (WARN_ON(family >= NPROTO))
1952 return -EAFNOSUPPORT;
1953
1954 spin_lock_bh(&xfrm_state_afinfo_lock);
1955 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
1956 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
1957 err = -EINVAL;
1958 else
1959 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
1960 }
1961 spin_unlock_bh(&xfrm_state_afinfo_lock);
1962 synchronize_rcu();
1963 return err;
1964 }
1965 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
1966
1967 struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
1968 {
1969 if (unlikely(family >= NPROTO))
1970 return NULL;
1971
1972 return rcu_dereference(xfrm_state_afinfo[family]);
1973 }
1974
1975 struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
1976 {
1977 struct xfrm_state_afinfo *afinfo;
1978 if (unlikely(family >= NPROTO))
1979 return NULL;
1980 rcu_read_lock();
1981 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
1982 if (unlikely(!afinfo))
1983 rcu_read_unlock();
1984 return afinfo;
1985 }
1986
1987 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
1988 void xfrm_state_delete_tunnel(struct xfrm_state *x)
1989 {
1990 if (x->tunnel) {
1991 struct xfrm_state *t = x->tunnel;
1992
1993 if (atomic_read(&t->tunnel_users) == 2)
1994 xfrm_state_delete(t);
1995 atomic_dec(&t->tunnel_users);
1996 xfrm_state_put(t);
1997 x->tunnel = NULL;
1998 }
1999 }
2000 EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2001
2002 int xfrm_state_mtu(struct xfrm_state *x, int mtu)
2003 {
2004 const struct xfrm_type *type = READ_ONCE(x->type);
2005
2006 if (x->km.state == XFRM_STATE_VALID &&
2007 type && type->get_mtu)
2008 return type->get_mtu(x, mtu);
2009
2010 return mtu - x->props.header_len;
2011 }
2012
2013 int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
2014 {
2015 struct xfrm_state_afinfo *afinfo;
2016 struct xfrm_mode *inner_mode;
2017 int family = x->props.family;
2018 int err;
2019
2020 err = -EAFNOSUPPORT;
2021 afinfo = xfrm_state_get_afinfo(family);
2022 if (!afinfo)
2023 goto error;
2024
2025 err = 0;
2026 if (afinfo->init_flags)
2027 err = afinfo->init_flags(x);
2028
2029 rcu_read_unlock();
2030
2031 if (err)
2032 goto error;
2033
2034 err = -EPROTONOSUPPORT;
2035
2036 if (x->sel.family != AF_UNSPEC) {
2037 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
2038 if (inner_mode == NULL)
2039 goto error;
2040
2041 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
2042 family != x->sel.family) {
2043 xfrm_put_mode(inner_mode);
2044 goto error;
2045 }
2046
2047 x->inner_mode = inner_mode;
2048 } else {
2049 struct xfrm_mode *inner_mode_iaf;
2050 int iafamily = AF_INET;
2051
2052 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
2053 if (inner_mode == NULL)
2054 goto error;
2055
2056 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2057 xfrm_put_mode(inner_mode);
2058 goto error;
2059 }
2060 x->inner_mode = inner_mode;
2061
2062 if (x->props.family == AF_INET)
2063 iafamily = AF_INET6;
2064
2065 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2066 if (inner_mode_iaf) {
2067 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
2068 x->inner_mode_iaf = inner_mode_iaf;
2069 else
2070 xfrm_put_mode(inner_mode_iaf);
2071 }
2072 }
2073
2074 x->type = xfrm_get_type(x->id.proto, family);
2075 if (x->type == NULL)
2076 goto error;
2077
2078 err = x->type->init_state(x);
2079 if (err)
2080 goto error;
2081
2082 x->outer_mode = xfrm_get_mode(x->props.mode, family);
2083 if (x->outer_mode == NULL) {
2084 err = -EPROTONOSUPPORT;
2085 goto error;
2086 }
2087
2088 if (init_replay) {
2089 err = xfrm_init_replay(x);
2090 if (err)
2091 goto error;
2092 }
2093
2094 x->km.state = XFRM_STATE_VALID;
2095
2096 error:
2097 return err;
2098 }
2099
2100 EXPORT_SYMBOL(__xfrm_init_state);
2101
2102 int xfrm_init_state(struct xfrm_state *x)
2103 {
2104 return __xfrm_init_state(x, true);
2105 }
2106
2107 EXPORT_SYMBOL(xfrm_init_state);
2108
2109 int __net_init xfrm_state_init(struct net *net)
2110 {
2111 unsigned int sz;
2112
2113 INIT_LIST_HEAD(&net->xfrm.state_all);
2114
2115 sz = sizeof(struct hlist_head) * 8;
2116
2117 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2118 if (!net->xfrm.state_bydst)
2119 goto out_bydst;
2120 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2121 if (!net->xfrm.state_bysrc)
2122 goto out_bysrc;
2123 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2124 if (!net->xfrm.state_byspi)
2125 goto out_byspi;
2126 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
2127
2128 net->xfrm.state_num = 0;
2129 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
2130 spin_lock_init(&net->xfrm.xfrm_state_lock);
2131 return 0;
2132
2133 out_byspi:
2134 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2135 out_bysrc:
2136 xfrm_hash_free(net->xfrm.state_bydst, sz);
2137 out_bydst:
2138 return -ENOMEM;
2139 }
2140
2141 void xfrm_state_fini(struct net *net)
2142 {
2143 unsigned int sz;
2144
2145 flush_work(&net->xfrm.state_hash_work);
2146 xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
2147 flush_work(&xfrm_state_gc_work);
2148
2149 WARN_ON(!list_empty(&net->xfrm.state_all));
2150
2151 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
2152 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2153 xfrm_hash_free(net->xfrm.state_byspi, sz);
2154 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2155 xfrm_hash_free(net->xfrm.state_bysrc, sz);
2156 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2157 xfrm_hash_free(net->xfrm.state_bydst, sz);
2158 }
2159
2160 #ifdef CONFIG_AUDITSYSCALL
2161 static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2162 struct audit_buffer *audit_buf)
2163 {
2164 struct xfrm_sec_ctx *ctx = x->security;
2165 u32 spi = ntohl(x->id.spi);
2166
2167 if (ctx)
2168 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2169 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2170
2171 switch (x->props.family) {
2172 case AF_INET:
2173 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2174 &x->props.saddr.a4, &x->id.daddr.a4);
2175 break;
2176 case AF_INET6:
2177 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
2178 x->props.saddr.a6, x->id.daddr.a6);
2179 break;
2180 }
2181
2182 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2183 }
2184
2185 static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2186 struct audit_buffer *audit_buf)
2187 {
2188 const struct iphdr *iph4;
2189 const struct ipv6hdr *iph6;
2190
2191 switch (family) {
2192 case AF_INET:
2193 iph4 = ip_hdr(skb);
2194 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2195 &iph4->saddr, &iph4->daddr);
2196 break;
2197 case AF_INET6:
2198 iph6 = ipv6_hdr(skb);
2199 audit_log_format(audit_buf,
2200 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
2201 &iph6->saddr, &iph6->daddr,
2202 iph6->flow_lbl[0] & 0x0f,
2203 iph6->flow_lbl[1],
2204 iph6->flow_lbl[2]);
2205 break;
2206 }
2207 }
2208
2209 void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
2210 {
2211 struct audit_buffer *audit_buf;
2212
2213 audit_buf = xfrm_audit_start("SAD-add");
2214 if (audit_buf == NULL)
2215 return;
2216 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2217 xfrm_audit_helper_sainfo(x, audit_buf);
2218 audit_log_format(audit_buf, " res=%u", result);
2219 audit_log_end(audit_buf);
2220 }
2221 EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
2222
2223 void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
2224 {
2225 struct audit_buffer *audit_buf;
2226
2227 audit_buf = xfrm_audit_start("SAD-delete");
2228 if (audit_buf == NULL)
2229 return;
2230 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
2231 xfrm_audit_helper_sainfo(x, audit_buf);
2232 audit_log_format(audit_buf, " res=%u", result);
2233 audit_log_end(audit_buf);
2234 }
2235 EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
2236
2237 void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
2238 struct sk_buff *skb)
2239 {
2240 struct audit_buffer *audit_buf;
2241 u32 spi;
2242
2243 audit_buf = xfrm_audit_start("SA-replay-overflow");
2244 if (audit_buf == NULL)
2245 return;
2246 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2247 /* don't record the sequence number because it's inherent in this kind
2248 * of audit message */
2249 spi = ntohl(x->id.spi);
2250 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
2251 audit_log_end(audit_buf);
2252 }
2253 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
2254
2255 void xfrm_audit_state_replay(struct xfrm_state *x,
2256 struct sk_buff *skb, __be32 net_seq)
2257 {
2258 struct audit_buffer *audit_buf;
2259 u32 spi;
2260
2261 audit_buf = xfrm_audit_start("SA-replayed-pkt");
2262 if (audit_buf == NULL)
2263 return;
2264 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2265 spi = ntohl(x->id.spi);
2266 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2267 spi, spi, ntohl(net_seq));
2268 audit_log_end(audit_buf);
2269 }
2270 EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
2271
2272 void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
2273 {
2274 struct audit_buffer *audit_buf;
2275
2276 audit_buf = xfrm_audit_start("SA-notfound");
2277 if (audit_buf == NULL)
2278 return;
2279 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2280 audit_log_end(audit_buf);
2281 }
2282 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
2283
2284 void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
2285 __be32 net_spi, __be32 net_seq)
2286 {
2287 struct audit_buffer *audit_buf;
2288 u32 spi;
2289
2290 audit_buf = xfrm_audit_start("SA-notfound");
2291 if (audit_buf == NULL)
2292 return;
2293 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
2294 spi = ntohl(net_spi);
2295 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2296 spi, spi, ntohl(net_seq));
2297 audit_log_end(audit_buf);
2298 }
2299 EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
2300
2301 void xfrm_audit_state_icvfail(struct xfrm_state *x,
2302 struct sk_buff *skb, u8 proto)
2303 {
2304 struct audit_buffer *audit_buf;
2305 __be32 net_spi;
2306 __be32 net_seq;
2307
2308 audit_buf = xfrm_audit_start("SA-icv-failure");
2309 if (audit_buf == NULL)
2310 return;
2311 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
2312 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
2313 u32 spi = ntohl(net_spi);
2314 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
2315 spi, spi, ntohl(net_seq));
2316 }
2317 audit_log_end(audit_buf);
2318 }
2319 EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
2320 #endif /* CONFIG_AUDITSYSCALL */