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