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