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