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