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