YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * xfrm_policy.c |
| 3 | * |
| 4 | * Changes: |
| 5 | * Mitsuru KANDA @USAGI |
| 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 8 | * IPv6 support |
| 9 | * Kazunori MIYAZAWA @USAGI |
| 10 | * YOSHIFUJI Hideaki |
| 11 | * Split up af-specific portion |
| 12 | * Derek Atkins <derek@ihtfp.com> Add the post_input processor |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/kmod.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/notifier.h> |
| 23 | #include <linux/netdevice.h> |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 24 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 26 | #include <linux/cache.h> |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 27 | #include <linux/audit.h> |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 28 | #include <net/dst.h> |
Eric Paris | 6ce74ec | 2012-02-16 15:08:39 -0500 | [diff] [blame] | 29 | #include <net/flow.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <net/xfrm.h> |
| 31 | #include <net/ip.h> |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_XFRM_STATISTICS |
| 33 | #include <net/snmp.h> |
| 34 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 36 | #include "xfrm_hash.h" |
| 37 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 38 | #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10)) |
| 39 | #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ)) |
| 40 | #define XFRM_MAX_QUEUE_LEN 100 |
| 41 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 42 | struct xfrm_flo { |
| 43 | struct dst_entry *dst_orig; |
| 44 | u8 flags; |
| 45 | }; |
| 46 | |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 47 | static DEFINE_SPINLOCK(xfrm_if_cb_lock); |
| 48 | static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly; |
| 49 | |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 50 | static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock); |
| 51 | static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO] |
| 52 | __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 54 | static struct kmem_cache *xfrm_dst_cache __read_mostly; |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 55 | static __read_mostly seqcount_t xfrm_policy_hash_generation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 57 | static void xfrm_init_pmtu(struct dst_entry *dst); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 58 | static int stale_bundle(struct dst_entry *dst); |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 59 | static int xfrm_bundle_ok(struct xfrm_dst *xdst); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 60 | static void xfrm_policy_queue_process(unsigned long arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Herbert Xu | 12bfa8b | 2014-11-13 17:09:50 +0800 | [diff] [blame] | 62 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir); |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 63 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 64 | int dir); |
| 65 | |
Florian Westphal | e37cc8a | 2016-08-11 15:17:55 +0200 | [diff] [blame] | 66 | static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy) |
| 67 | { |
| 68 | return atomic_inc_not_zero(&policy->refcnt); |
| 69 | } |
| 70 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 71 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 72 | __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 73 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 74 | const struct flowi4 *fl4 = &fl->u.ip4; |
| 75 | |
Alexey Dobriyan | 26bff94 | 2011-11-22 06:46:02 +0000 | [diff] [blame] | 76 | return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) && |
| 77 | addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) && |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 78 | !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) && |
| 79 | !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) && |
| 80 | (fl4->flowi4_proto == sel->proto || !sel->proto) && |
| 81 | (fl4->flowi4_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 84 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 85 | __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 86 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 87 | const struct flowi6 *fl6 = &fl->u.ip6; |
| 88 | |
| 89 | return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) && |
| 90 | addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) && |
| 91 | !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) && |
| 92 | !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) && |
| 93 | (fl6->flowi6_proto == sel->proto || !sel->proto) && |
| 94 | (fl6->flowi6_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 95 | } |
| 96 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 97 | bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl, |
| 98 | unsigned short family) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 99 | { |
| 100 | switch (family) { |
| 101 | case AF_INET: |
| 102 | return __xfrm4_selector_match(sel, fl); |
| 103 | case AF_INET6: |
| 104 | return __xfrm6_selector_match(sel, fl); |
| 105 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 106 | return false; |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 109 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family) |
| 110 | { |
| 111 | struct xfrm_policy_afinfo *afinfo; |
| 112 | |
| 113 | if (unlikely(family >= NPROTO)) |
| 114 | return NULL; |
| 115 | rcu_read_lock(); |
| 116 | afinfo = rcu_dereference(xfrm_policy_afinfo[family]); |
| 117 | if (unlikely(!afinfo)) |
| 118 | rcu_read_unlock(); |
| 119 | return afinfo; |
| 120 | } |
| 121 | |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 122 | /* Called with rcu_read_lock(). */ |
| 123 | static const struct xfrm_if_cb *xfrm_if_get_cb(void) |
| 124 | { |
| 125 | return rcu_dereference(xfrm_if_cb); |
| 126 | } |
| 127 | |
| 128 | static void xfrm_policy_put_afinfo(const struct xfrm_policy_afinfo *afinfo) |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 129 | { |
| 130 | rcu_read_unlock(); |
| 131 | } |
| 132 | |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 133 | static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, |
| 134 | int tos, int oif, |
David S. Miller | 6418c4e | 2011-02-24 00:16:53 -0500 | [diff] [blame] | 135 | const xfrm_address_t *saddr, |
| 136 | const xfrm_address_t *daddr, |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 137 | int family, u32 mark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 139 | struct xfrm_policy_afinfo *afinfo; |
| 140 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 142 | afinfo = xfrm_policy_get_afinfo(family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (unlikely(afinfo == NULL)) |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 144 | return ERR_PTR(-EAFNOSUPPORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 146 | dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | xfrm_policy_put_afinfo(afinfo); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 149 | |
| 150 | return dst; |
| 151 | } |
| 152 | |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 153 | static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, |
| 154 | int tos, int oif, |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 155 | xfrm_address_t *prev_saddr, |
| 156 | xfrm_address_t *prev_daddr, |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 157 | int family, u32 mark) |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 158 | { |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 159 | struct net *net = xs_net(x); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 160 | xfrm_address_t *saddr = &x->props.saddr; |
| 161 | xfrm_address_t *daddr = &x->id.daddr; |
| 162 | struct dst_entry *dst; |
| 163 | |
| 164 | if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) { |
| 165 | saddr = x->coaddr; |
| 166 | daddr = prev_daddr; |
| 167 | } |
| 168 | if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) { |
| 169 | saddr = prev_saddr; |
| 170 | daddr = x->coaddr; |
| 171 | } |
| 172 | |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 173 | dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 174 | |
| 175 | if (!IS_ERR(dst)) { |
| 176 | if (prev_saddr != saddr) |
| 177 | memcpy(prev_saddr, saddr, sizeof(*prev_saddr)); |
| 178 | if (prev_daddr != daddr) |
| 179 | memcpy(prev_daddr, daddr, sizeof(*prev_daddr)); |
| 180 | } |
| 181 | |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 182 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | static inline unsigned long make_jiffies(long secs) |
| 186 | { |
| 187 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 188 | return MAX_SCHEDULE_TIMEOUT-1; |
| 189 | else |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 190 | return secs*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void xfrm_policy_timer(unsigned long data) |
| 194 | { |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 195 | struct xfrm_policy *xp = (struct xfrm_policy *)data; |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 196 | unsigned long now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | long next = LONG_MAX; |
| 198 | int warn = 0; |
| 199 | int dir; |
| 200 | |
| 201 | read_lock(&xp->lock); |
| 202 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 203 | if (unlikely(xp->walk.dead)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | goto out; |
| 205 | |
Herbert Xu | 77d8d7a | 2005-10-05 12:15:12 -0700 | [diff] [blame] | 206 | dir = xfrm_policy_id2dir(xp->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | if (xp->lft.hard_add_expires_seconds) { |
| 209 | long tmo = xp->lft.hard_add_expires_seconds + |
| 210 | xp->curlft.add_time - now; |
| 211 | if (tmo <= 0) |
| 212 | goto expired; |
| 213 | if (tmo < next) |
| 214 | next = tmo; |
| 215 | } |
| 216 | if (xp->lft.hard_use_expires_seconds) { |
| 217 | long tmo = xp->lft.hard_use_expires_seconds + |
| 218 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 219 | if (tmo <= 0) |
| 220 | goto expired; |
| 221 | if (tmo < next) |
| 222 | next = tmo; |
| 223 | } |
| 224 | if (xp->lft.soft_add_expires_seconds) { |
| 225 | long tmo = xp->lft.soft_add_expires_seconds + |
| 226 | xp->curlft.add_time - now; |
| 227 | if (tmo <= 0) { |
| 228 | warn = 1; |
| 229 | tmo = XFRM_KM_TIMEOUT; |
| 230 | } |
| 231 | if (tmo < next) |
| 232 | next = tmo; |
| 233 | } |
| 234 | if (xp->lft.soft_use_expires_seconds) { |
| 235 | long tmo = xp->lft.soft_use_expires_seconds + |
| 236 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 237 | if (tmo <= 0) { |
| 238 | warn = 1; |
| 239 | tmo = XFRM_KM_TIMEOUT; |
| 240 | } |
| 241 | if (tmo < next) |
| 242 | next = tmo; |
| 243 | } |
| 244 | |
| 245 | if (warn) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 246 | km_policy_expired(xp, dir, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (next != LONG_MAX && |
| 248 | !mod_timer(&xp->timer, jiffies + make_jiffies(next))) |
| 249 | xfrm_pol_hold(xp); |
| 250 | |
| 251 | out: |
| 252 | read_unlock(&xp->lock); |
| 253 | xfrm_pol_put(xp); |
| 254 | return; |
| 255 | |
| 256 | expired: |
| 257 | read_unlock(&xp->lock); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 258 | if (!xfrm_policy_delete(xp, dir)) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 259 | km_policy_expired(xp, dir, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | xfrm_pol_put(xp); |
| 261 | } |
| 262 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 263 | static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo) |
| 264 | { |
| 265 | struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo); |
| 266 | |
| 267 | if (unlikely(pol->walk.dead)) |
| 268 | flo = NULL; |
| 269 | else |
| 270 | xfrm_pol_hold(pol); |
| 271 | |
| 272 | return flo; |
| 273 | } |
| 274 | |
| 275 | static int xfrm_policy_flo_check(struct flow_cache_object *flo) |
| 276 | { |
| 277 | struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo); |
| 278 | |
| 279 | return !pol->walk.dead; |
| 280 | } |
| 281 | |
| 282 | static void xfrm_policy_flo_delete(struct flow_cache_object *flo) |
| 283 | { |
| 284 | xfrm_pol_put(container_of(flo, struct xfrm_policy, flo)); |
| 285 | } |
| 286 | |
| 287 | static const struct flow_cache_ops xfrm_policy_fc_ops = { |
| 288 | .get = xfrm_policy_flo_get, |
| 289 | .check = xfrm_policy_flo_check, |
| 290 | .delete = xfrm_policy_flo_delete, |
| 291 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2 |
| 294 | * SPD calls. |
| 295 | */ |
| 296 | |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 297 | struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
| 299 | struct xfrm_policy *policy; |
| 300 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 301 | policy = kzalloc(sizeof(struct xfrm_policy), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | if (policy) { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 304 | write_pnet(&policy->xp_net, net); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 305 | INIT_LIST_HEAD(&policy->walk.all); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 306 | INIT_HLIST_NODE(&policy->bydst); |
| 307 | INIT_HLIST_NODE(&policy->byidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | rwlock_init(&policy->lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 309 | atomic_set(&policy->refcnt, 1); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 310 | skb_queue_head_init(&policy->polq.hold_queue); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 311 | setup_timer(&policy->timer, xfrm_policy_timer, |
| 312 | (unsigned long)policy); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 313 | setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process, |
| 314 | (unsigned long)policy); |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 315 | policy->flo.ops = &xfrm_policy_fc_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | return policy; |
| 318 | } |
| 319 | EXPORT_SYMBOL(xfrm_policy_alloc); |
| 320 | |
Eric Dumazet | 56f0473 | 2015-12-08 07:22:01 -0800 | [diff] [blame] | 321 | static void xfrm_policy_destroy_rcu(struct rcu_head *head) |
| 322 | { |
| 323 | struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu); |
| 324 | |
| 325 | security_xfrm_policy_free(policy->security); |
| 326 | kfree(policy); |
| 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | /* Destroy xfrm_policy: descendant resources must be released to this moment. */ |
| 330 | |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 331 | void xfrm_policy_destroy(struct xfrm_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 333 | BUG_ON(!policy->walk.dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Fan Du | 0659eea | 2013-08-01 18:08:36 +0800 | [diff] [blame] | 335 | if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | BUG(); |
| 337 | |
Eric Dumazet | 56f0473 | 2015-12-08 07:22:01 -0800 | [diff] [blame] | 338 | call_rcu(&policy->rcu, xfrm_policy_destroy_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | } |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 340 | EXPORT_SYMBOL(xfrm_policy_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* Rule must be locked. Release descentant resources, announce |
| 343 | * entry dead. The rule must be unlinked from lists to the moment. |
| 344 | */ |
| 345 | |
| 346 | static void xfrm_policy_kill(struct xfrm_policy *policy) |
| 347 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 348 | policy->walk.dead = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 350 | atomic_inc(&policy->genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 352 | if (del_timer(&policy->polq.hold_timer)) |
| 353 | xfrm_pol_put(policy); |
Li RongQing | 1ee5e66 | 2015-04-22 15:51:16 +0800 | [diff] [blame] | 354 | skb_queue_purge(&policy->polq.hold_queue); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 355 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 356 | if (del_timer(&policy->timer)) |
| 357 | xfrm_pol_put(policy); |
| 358 | |
| 359 | xfrm_pol_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 362 | static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024; |
| 363 | |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 364 | static inline unsigned int idx_hash(struct net *net, u32 index) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 365 | { |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 366 | return __idx_hash(index, net->xfrm.policy_idx_hmask); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 369 | /* calculate policy hash thresholds */ |
| 370 | static void __get_hash_thresh(struct net *net, |
| 371 | unsigned short family, int dir, |
| 372 | u8 *dbits, u8 *sbits) |
| 373 | { |
| 374 | switch (family) { |
| 375 | case AF_INET: |
| 376 | *dbits = net->xfrm.policy_bydst[dir].dbits4; |
| 377 | *sbits = net->xfrm.policy_bydst[dir].sbits4; |
| 378 | break; |
| 379 | |
| 380 | case AF_INET6: |
| 381 | *dbits = net->xfrm.policy_bydst[dir].dbits6; |
| 382 | *sbits = net->xfrm.policy_bydst[dir].sbits6; |
| 383 | break; |
| 384 | |
| 385 | default: |
| 386 | *dbits = 0; |
| 387 | *sbits = 0; |
| 388 | } |
| 389 | } |
| 390 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 391 | static struct hlist_head *policy_hash_bysel(struct net *net, |
| 392 | const struct xfrm_selector *sel, |
| 393 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 394 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 395 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 396 | unsigned int hash; |
| 397 | u8 dbits; |
| 398 | u8 sbits; |
| 399 | |
| 400 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
| 401 | hash = __sel_hash(sel, family, hmask, dbits, sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 402 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 403 | if (hash == hmask + 1) |
| 404 | return &net->xfrm.policy_inexact[dir]; |
| 405 | |
| 406 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, |
| 407 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 408 | } |
| 409 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 410 | static struct hlist_head *policy_hash_direct(struct net *net, |
| 411 | const xfrm_address_t *daddr, |
| 412 | const xfrm_address_t *saddr, |
| 413 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 414 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 415 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 416 | unsigned int hash; |
| 417 | u8 dbits; |
| 418 | u8 sbits; |
| 419 | |
| 420 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
| 421 | hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 422 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 423 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, |
| 424 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 427 | static void xfrm_dst_hash_transfer(struct net *net, |
| 428 | struct hlist_head *list, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 429 | struct hlist_head *ndsttable, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 430 | unsigned int nhashmask, |
| 431 | int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 432 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 433 | struct hlist_node *tmp, *entry0 = NULL; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 434 | struct xfrm_policy *pol; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 435 | unsigned int h0 = 0; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 436 | u8 dbits; |
| 437 | u8 sbits; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 438 | |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 439 | redo: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 440 | hlist_for_each_entry_safe(pol, tmp, list, bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 441 | unsigned int h; |
| 442 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 443 | __get_hash_thresh(net, pol->family, dir, &dbits, &sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 444 | h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 445 | pol->family, nhashmask, dbits, sbits); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 446 | if (!entry0) { |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 447 | hlist_del_rcu(&pol->bydst); |
| 448 | hlist_add_head_rcu(&pol->bydst, ndsttable + h); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 449 | h0 = h; |
| 450 | } else { |
| 451 | if (h != h0) |
| 452 | continue; |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 453 | hlist_del_rcu(&pol->bydst); |
| 454 | hlist_add_behind_rcu(&pol->bydst, entry0); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 455 | } |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 456 | entry0 = &pol->bydst; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 457 | } |
| 458 | if (!hlist_empty(list)) { |
| 459 | entry0 = NULL; |
| 460 | goto redo; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | static void xfrm_idx_hash_transfer(struct hlist_head *list, |
| 465 | struct hlist_head *nidxtable, |
| 466 | unsigned int nhashmask) |
| 467 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 468 | struct hlist_node *tmp; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 469 | struct xfrm_policy *pol; |
| 470 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 471 | hlist_for_each_entry_safe(pol, tmp, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 472 | unsigned int h; |
| 473 | |
| 474 | h = __idx_hash(pol->index, nhashmask); |
| 475 | hlist_add_head(&pol->byidx, nidxtable+h); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static unsigned long xfrm_new_hash_mask(unsigned int old_hmask) |
| 480 | { |
| 481 | return ((old_hmask + 1) << 1) - 1; |
| 482 | } |
| 483 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 484 | static void xfrm_bydst_resize(struct net *net, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 485 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 486 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 487 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 488 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 489 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 490 | struct hlist_head *odst; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 491 | int i; |
| 492 | |
| 493 | if (!ndst) |
| 494 | return; |
| 495 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 496 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 497 | write_seqcount_begin(&xfrm_policy_hash_generation); |
| 498 | |
| 499 | odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table, |
| 500 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 501 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 502 | odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table, |
| 503 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
| 504 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 505 | for (i = hmask; i >= 0; i--) |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 506 | xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 507 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 508 | rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 509 | net->xfrm.policy_bydst[dir].hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 510 | |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 511 | write_seqcount_end(&xfrm_policy_hash_generation); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 512 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 513 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 514 | synchronize_rcu(); |
| 515 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 516 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 519 | static void xfrm_byidx_resize(struct net *net, int total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 520 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 521 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 522 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 523 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 524 | struct hlist_head *oidx = net->xfrm.policy_byidx; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 525 | struct hlist_head *nidx = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 526 | int i; |
| 527 | |
| 528 | if (!nidx) |
| 529 | return; |
| 530 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 531 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 532 | |
| 533 | for (i = hmask; i >= 0; i--) |
| 534 | xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask); |
| 535 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 536 | net->xfrm.policy_byidx = nidx; |
| 537 | net->xfrm.policy_idx_hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 538 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 539 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 540 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 541 | xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 544 | static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 545 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 546 | unsigned int cnt = net->xfrm.policy_count[dir]; |
| 547 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 548 | |
| 549 | if (total) |
| 550 | *total += cnt; |
| 551 | |
| 552 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 553 | cnt > hmask) |
| 554 | return 1; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 559 | static inline int xfrm_byidx_should_resize(struct net *net, int total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 560 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 561 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 562 | |
| 563 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 564 | total > hmask) |
| 565 | return 1; |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 570 | void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si) |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 571 | { |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 572 | si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN]; |
| 573 | si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT]; |
| 574 | si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD]; |
| 575 | si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX]; |
| 576 | si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX]; |
| 577 | si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX]; |
| 578 | si->spdhcnt = net->xfrm.policy_idx_hmask; |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 579 | si->spdhmcnt = xfrm_policy_hashmax; |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 580 | } |
| 581 | EXPORT_SYMBOL(xfrm_spd_getinfo); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 582 | |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 583 | static DEFINE_MUTEX(hash_resize_mutex); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 584 | static void xfrm_hash_resize(struct work_struct *work) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 585 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 586 | struct net *net = container_of(work, struct net, xfrm.policy_hash_work); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 587 | int dir, total; |
| 588 | |
| 589 | mutex_lock(&hash_resize_mutex); |
| 590 | |
| 591 | total = 0; |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 592 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 593 | if (xfrm_bydst_should_resize(net, dir, &total)) |
| 594 | xfrm_bydst_resize(net, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 595 | } |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 596 | if (xfrm_byidx_should_resize(net, total)) |
| 597 | xfrm_byidx_resize(net, total); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 598 | |
| 599 | mutex_unlock(&hash_resize_mutex); |
| 600 | } |
| 601 | |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 602 | static void xfrm_hash_rebuild(struct work_struct *work) |
| 603 | { |
| 604 | struct net *net = container_of(work, struct net, |
| 605 | xfrm.policy_hthresh.work); |
| 606 | unsigned int hmask; |
| 607 | struct xfrm_policy *pol; |
| 608 | struct xfrm_policy *policy; |
| 609 | struct hlist_head *chain; |
| 610 | struct hlist_head *odst; |
| 611 | struct hlist_node *newpos; |
| 612 | int i; |
| 613 | int dir; |
| 614 | unsigned seq; |
| 615 | u8 lbits4, rbits4, lbits6, rbits6; |
| 616 | |
| 617 | mutex_lock(&hash_resize_mutex); |
| 618 | |
| 619 | /* read selector prefixlen thresholds */ |
| 620 | do { |
| 621 | seq = read_seqbegin(&net->xfrm.policy_hthresh.lock); |
| 622 | |
| 623 | lbits4 = net->xfrm.policy_hthresh.lbits4; |
| 624 | rbits4 = net->xfrm.policy_hthresh.rbits4; |
| 625 | lbits6 = net->xfrm.policy_hthresh.lbits6; |
| 626 | rbits6 = net->xfrm.policy_hthresh.rbits6; |
| 627 | } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq)); |
| 628 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 629 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 630 | |
| 631 | /* reset the bydst and inexact table in all directions */ |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 632 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 633 | INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]); |
| 634 | hmask = net->xfrm.policy_bydst[dir].hmask; |
| 635 | odst = net->xfrm.policy_bydst[dir].table; |
| 636 | for (i = hmask; i >= 0; i--) |
| 637 | INIT_HLIST_HEAD(odst + i); |
| 638 | if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) { |
| 639 | /* dir out => dst = remote, src = local */ |
| 640 | net->xfrm.policy_bydst[dir].dbits4 = rbits4; |
| 641 | net->xfrm.policy_bydst[dir].sbits4 = lbits4; |
| 642 | net->xfrm.policy_bydst[dir].dbits6 = rbits6; |
| 643 | net->xfrm.policy_bydst[dir].sbits6 = lbits6; |
| 644 | } else { |
| 645 | /* dir in/fwd => dst = local, src = remote */ |
| 646 | net->xfrm.policy_bydst[dir].dbits4 = lbits4; |
| 647 | net->xfrm.policy_bydst[dir].sbits4 = rbits4; |
| 648 | net->xfrm.policy_bydst[dir].dbits6 = lbits6; |
| 649 | net->xfrm.policy_bydst[dir].sbits6 = rbits6; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* re-insert all policies by order of creation */ |
| 654 | list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) { |
Florian Westphal | 5d89917 | 2017-12-27 23:25:45 +0100 | [diff] [blame] | 655 | if (policy->walk.dead || |
| 656 | xfrm_policy_id2dir(policy->index) >= XFRM_POLICY_MAX) { |
Tobias Brunner | 6916fb3 | 2016-07-29 09:57:32 +0200 | [diff] [blame] | 657 | /* skip socket policies */ |
| 658 | continue; |
| 659 | } |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 660 | newpos = NULL; |
| 661 | chain = policy_hash_bysel(net, &policy->selector, |
| 662 | policy->family, |
| 663 | xfrm_policy_id2dir(policy->index)); |
| 664 | hlist_for_each_entry(pol, chain, bydst) { |
| 665 | if (policy->priority >= pol->priority) |
| 666 | newpos = &pol->bydst; |
| 667 | else |
| 668 | break; |
| 669 | } |
| 670 | if (newpos) |
Florian Westphal | 9da255b | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 671 | hlist_add_behind_rcu(&policy->bydst, newpos); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 672 | else |
Florian Westphal | 9da255b | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 673 | hlist_add_head_rcu(&policy->bydst, chain); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 676 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 677 | |
| 678 | mutex_unlock(&hash_resize_mutex); |
| 679 | } |
| 680 | |
| 681 | void xfrm_policy_hash_rebuild(struct net *net) |
| 682 | { |
| 683 | schedule_work(&net->xfrm.policy_hthresh.work); |
| 684 | } |
| 685 | EXPORT_SYMBOL(xfrm_policy_hash_rebuild); |
| 686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | /* Generate new index... KAME seems to generate them ordered by cost |
| 688 | * of an absolute inpredictability of ordering of rules. This will not pass. */ |
Fan Du | e682adf | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 689 | static u32 xfrm_gen_index(struct net *net, int dir, u32 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | static u32 idx_generator; |
| 692 | |
| 693 | for (;;) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 694 | struct hlist_head *list; |
| 695 | struct xfrm_policy *p; |
| 696 | u32 idx; |
| 697 | int found; |
| 698 | |
Fan Du | e682adf | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 699 | if (!index) { |
| 700 | idx = (idx_generator | dir); |
| 701 | idx_generator += 8; |
| 702 | } else { |
| 703 | idx = index; |
| 704 | index = 0; |
| 705 | } |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (idx == 0) |
| 708 | idx = 8; |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 709 | list = net->xfrm.policy_byidx + idx_hash(net, idx); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 710 | found = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 711 | hlist_for_each_entry(p, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 712 | if (p->index == idx) { |
| 713 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | break; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 717 | if (!found) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return idx; |
| 719 | } |
| 720 | } |
| 721 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 722 | static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2) |
| 723 | { |
| 724 | u32 *p1 = (u32 *) s1; |
| 725 | u32 *p2 = (u32 *) s2; |
| 726 | int len = sizeof(struct xfrm_selector) / sizeof(u32); |
| 727 | int i; |
| 728 | |
| 729 | for (i = 0; i < len; i++) { |
| 730 | if (p1[i] != p2[i]) |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 737 | static void xfrm_policy_requeue(struct xfrm_policy *old, |
| 738 | struct xfrm_policy *new) |
| 739 | { |
| 740 | struct xfrm_policy_queue *pq = &old->polq; |
| 741 | struct sk_buff_head list; |
| 742 | |
Li RongQing | de2ad48 | 2015-04-30 17:25:19 +0800 | [diff] [blame] | 743 | if (skb_queue_empty(&pq->hold_queue)) |
| 744 | return; |
| 745 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 746 | __skb_queue_head_init(&list); |
| 747 | |
| 748 | spin_lock_bh(&pq->hold_queue.lock); |
| 749 | skb_queue_splice_init(&pq->hold_queue, &list); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 750 | if (del_timer(&pq->hold_timer)) |
| 751 | xfrm_pol_put(old); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 752 | spin_unlock_bh(&pq->hold_queue.lock); |
| 753 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 754 | pq = &new->polq; |
| 755 | |
| 756 | spin_lock_bh(&pq->hold_queue.lock); |
| 757 | skb_queue_splice(&list, &pq->hold_queue); |
| 758 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 759 | if (!mod_timer(&pq->hold_timer, jiffies)) |
| 760 | xfrm_pol_hold(new); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 761 | spin_unlock_bh(&pq->hold_queue.lock); |
| 762 | } |
| 763 | |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 764 | static bool xfrm_policy_mark_match(struct xfrm_policy *policy, |
| 765 | struct xfrm_policy *pol) |
| 766 | { |
| 767 | u32 mark = policy->mark.v & policy->mark.m; |
| 768 | |
| 769 | if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m) |
| 770 | return true; |
| 771 | |
| 772 | if ((mark & pol->mark.m) == pol->mark.v && |
| 773 | policy->priority == pol->priority) |
| 774 | return true; |
| 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) |
| 780 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 781 | struct net *net = xp_net(policy); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 782 | struct xfrm_policy *pol; |
| 783 | struct xfrm_policy *delpol; |
| 784 | struct hlist_head *chain; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 785 | struct hlist_node *newpos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 787 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 788 | chain = policy_hash_bysel(net, &policy->selector, policy->family, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 789 | delpol = NULL; |
| 790 | newpos = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 791 | hlist_for_each_entry(pol, chain, bydst) { |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 792 | if (pol->type == policy->type && |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 793 | pol->if_id == policy->if_id && |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 794 | !selector_cmp(&pol->selector, &policy->selector) && |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 795 | xfrm_policy_mark_match(policy, pol) && |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 796 | xfrm_sec_ctx_match(pol->security, policy->security) && |
| 797 | !WARN_ON(delpol)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (excl) { |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 799 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | return -EEXIST; |
| 801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | delpol = pol; |
| 803 | if (policy->priority > pol->priority) |
| 804 | continue; |
| 805 | } else if (policy->priority >= pol->priority) { |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 806 | newpos = &pol->bydst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | continue; |
| 808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | if (delpol) |
| 810 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | if (newpos) |
Florian Westphal | 9da255b | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 813 | hlist_add_behind_rcu(&policy->bydst, newpos); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 814 | else |
Florian Westphal | 9da255b | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 815 | hlist_add_head_rcu(&policy->bydst, chain); |
Herbert Xu | 12bfa8b | 2014-11-13 17:09:50 +0800 | [diff] [blame] | 816 | __xfrm_policy_link(policy, dir); |
Fan Du | ca925cf | 2014-01-18 09:55:27 +0800 | [diff] [blame] | 817 | atomic_inc(&net->xfrm.flow_cache_genid); |
fan.du | ca4c3fc | 2013-07-30 08:33:53 +0800 | [diff] [blame] | 818 | |
| 819 | /* After previous checking, family can either be AF_INET or AF_INET6 */ |
| 820 | if (policy->family == AF_INET) |
| 821 | rt_genid_bump_ipv4(net); |
| 822 | else |
| 823 | rt_genid_bump_ipv6(net); |
| 824 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 825 | if (delpol) { |
| 826 | xfrm_policy_requeue(delpol, policy); |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 827 | __xfrm_policy_unlink(delpol, dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 828 | } |
Fan Du | e682adf | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 829 | policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 830 | hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index)); |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 831 | policy->curlft.add_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | policy->curlft.use_time = 0; |
| 833 | if (!mod_timer(&policy->timer, jiffies + HZ)) |
| 834 | xfrm_pol_hold(policy); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 835 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 837 | if (delpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | xfrm_policy_kill(delpol); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 839 | else if (xfrm_bydst_should_resize(net, dir, NULL)) |
| 840 | schedule_work(&net->xfrm.policy_hash_work); |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return 0; |
| 843 | } |
| 844 | EXPORT_SYMBOL(xfrm_policy_insert); |
| 845 | |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 846 | struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id, |
| 847 | u8 type, int dir, |
| 848 | struct xfrm_selector *sel, |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 849 | struct xfrm_sec_ctx *ctx, int delete, |
| 850 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 852 | struct xfrm_policy *pol, *ret; |
| 853 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 855 | *err = 0; |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 856 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 857 | chain = policy_hash_bysel(net, sel, sel->family, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 858 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 859 | hlist_for_each_entry(pol, chain, bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 860 | if (pol->type == type && |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 861 | pol->if_id == if_id && |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 862 | (mark & pol->mark.m) == pol->mark.v && |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 863 | !selector_cmp(sel, &pol->selector) && |
| 864 | xfrm_sec_ctx_match(ctx, pol->security)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 866 | if (delete) { |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 867 | *err = security_xfrm_policy_delete( |
| 868 | pol->security); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 869 | if (*err) { |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 870 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 871 | return pol; |
| 872 | } |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 873 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 874 | } |
| 875 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 879 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 881 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 882 | xfrm_policy_kill(ret); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 883 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 885 | EXPORT_SYMBOL(xfrm_policy_bysel_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 887 | struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, |
| 888 | u8 type, int dir, u32 id, int delete, |
| 889 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 891 | struct xfrm_policy *pol, *ret; |
| 892 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Herbert Xu | b5505c6 | 2007-05-14 02:15:47 -0700 | [diff] [blame] | 894 | *err = -ENOENT; |
| 895 | if (xfrm_policy_id2dir(id) != dir) |
| 896 | return NULL; |
| 897 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 898 | *err = 0; |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 899 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 900 | chain = net->xfrm.policy_byidx + idx_hash(net, id); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 901 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 902 | hlist_for_each_entry(pol, chain, byidx) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 903 | if (pol->type == type && pol->index == id && |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 904 | pol->if_id == if_id && |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 905 | (mark & pol->mark.m) == pol->mark.v) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 907 | if (delete) { |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 908 | *err = security_xfrm_policy_delete( |
| 909 | pol->security); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 910 | if (*err) { |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 911 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 912 | return pol; |
| 913 | } |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 914 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 915 | } |
| 916 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 920 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 922 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 923 | xfrm_policy_kill(ret); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 924 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | EXPORT_SYMBOL(xfrm_policy_byid); |
| 927 | |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 928 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 929 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 930 | xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 932 | int dir, err = 0; |
| 933 | |
| 934 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
| 935 | struct xfrm_policy *pol; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 936 | int i; |
| 937 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 938 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 939 | &net->xfrm.policy_inexact[dir], bydst) { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 940 | if (pol->type != type) |
| 941 | continue; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 942 | err = security_xfrm_policy_delete(pol->security); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 943 | if (err) { |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 944 | xfrm_audit_policy_delete(pol, 0, task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 945 | return err; |
| 946 | } |
YOSHIFUJI Hideaki | 7dc12d6 | 2007-07-19 10:45:15 +0900 | [diff] [blame] | 947 | } |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 948 | for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 949 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 950 | net->xfrm.policy_bydst[dir].table + i, |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 951 | bydst) { |
| 952 | if (pol->type != type) |
| 953 | continue; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 954 | err = security_xfrm_policy_delete( |
| 955 | pol->security); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 956 | if (err) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 957 | xfrm_audit_policy_delete(pol, 0, |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 958 | task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 959 | return err; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | return err; |
| 965 | } |
| 966 | #else |
| 967 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 968 | xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 969 | { |
| 970 | return 0; |
| 971 | } |
| 972 | #endif |
| 973 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 974 | int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 975 | { |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 976 | int dir, err = 0, cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 978 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 979 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 980 | err = xfrm_policy_flush_secctx_check(net, type, task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 981 | if (err) |
| 982 | goto out; |
| 983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 985 | struct xfrm_policy *pol; |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 986 | int i; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 987 | |
| 988 | again1: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 989 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 990 | &net->xfrm.policy_inexact[dir], bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 991 | if (pol->type != type) |
| 992 | continue; |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 993 | __xfrm_policy_unlink(pol, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 994 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 995 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 997 | xfrm_audit_policy_delete(pol, 1, task_valid); |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 998 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 999 | xfrm_policy_kill(pol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Steffen Klassert | 4141b36 | 2016-08-24 13:08:40 +0200 | [diff] [blame] | 1001 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1002 | goto again1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1004 | |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 1005 | for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1006 | again2: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1007 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 1008 | net->xfrm.policy_bydst[dir].table + i, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1009 | bydst) { |
| 1010 | if (pol->type != type) |
| 1011 | continue; |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 1012 | __xfrm_policy_unlink(pol, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1013 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 1014 | cnt++; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1015 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 1016 | xfrm_audit_policy_delete(pol, 1, task_valid); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1017 | xfrm_policy_kill(pol); |
| 1018 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1019 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1020 | goto again2; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 1025 | if (!cnt) |
| 1026 | err = -ESRCH; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1027 | out: |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1028 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1029 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | EXPORT_SYMBOL(xfrm_policy_flush); |
| 1032 | |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1033 | int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1034 | int (*func)(struct xfrm_policy *, int, int, void*), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | void *data) |
| 1036 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1037 | struct xfrm_policy *pol; |
| 1038 | struct xfrm_policy_walk_entry *x; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1039 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1041 | if (walk->type >= XFRM_POLICY_TYPE_MAX && |
| 1042 | walk->type != XFRM_POLICY_TYPE_ANY) |
| 1043 | return -EINVAL; |
| 1044 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1045 | if (list_empty(&walk->walk.all) && walk->seq != 0) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1046 | return 0; |
| 1047 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1048 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1049 | if (list_empty(&walk->walk.all)) |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1050 | x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1051 | else |
Li RongQing | 8007770 | 2015-04-22 17:09:54 +0800 | [diff] [blame] | 1052 | x = list_first_entry(&walk->walk.all, |
| 1053 | struct xfrm_policy_walk_entry, all); |
| 1054 | |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1055 | list_for_each_entry_from(x, &net->xfrm.policy_all, all) { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1056 | if (x->dead) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1057 | continue; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1058 | pol = container_of(x, struct xfrm_policy, walk); |
| 1059 | if (walk->type != XFRM_POLICY_TYPE_ANY && |
| 1060 | walk->type != pol->type) |
| 1061 | continue; |
| 1062 | error = func(pol, xfrm_policy_id2dir(pol->index), |
| 1063 | walk->seq, data); |
| 1064 | if (error) { |
| 1065 | list_move_tail(&walk->walk.all, &x->all); |
| 1066 | goto out; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1067 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1068 | walk->seq++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1070 | if (walk->seq == 0) { |
Jamal Hadi Salim | baf5d74 | 2006-12-04 20:02:37 -0800 | [diff] [blame] | 1071 | error = -ENOENT; |
| 1072 | goto out; |
| 1073 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1074 | list_del_init(&walk->walk.all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | out: |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1076 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | return error; |
| 1078 | } |
| 1079 | EXPORT_SYMBOL(xfrm_policy_walk); |
| 1080 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1081 | void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type) |
| 1082 | { |
| 1083 | INIT_LIST_HEAD(&walk->walk.all); |
| 1084 | walk->walk.dead = 1; |
| 1085 | walk->type = type; |
| 1086 | walk->seq = 0; |
| 1087 | } |
| 1088 | EXPORT_SYMBOL(xfrm_policy_walk_init); |
| 1089 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1090 | void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net) |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1091 | { |
| 1092 | if (list_empty(&walk->walk.all)) |
| 1093 | return; |
| 1094 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1095 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */ |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1096 | list_del(&walk->walk.all); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1097 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1098 | } |
| 1099 | EXPORT_SYMBOL(xfrm_policy_walk_done); |
| 1100 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1101 | /* |
| 1102 | * Find policy to apply to this flow. |
| 1103 | * |
| 1104 | * Returns 0 if policy found, else an -errno. |
| 1105 | */ |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 1106 | static int xfrm_policy_match(const struct xfrm_policy *pol, |
| 1107 | const struct flowi *fl, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1108 | u8 type, u16 family, int dir) |
| 1109 | { |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 1110 | const struct xfrm_selector *sel = &pol->selector; |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 1111 | int ret = -ESRCH; |
| 1112 | bool match; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1113 | |
| 1114 | if (pol->family != family || |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1115 | pol->if_id != fl->flowi_xfrm.if_id || |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1116 | (fl->flowi_mark & pol->mark.m) != pol->mark.v || |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1117 | pol->type != type) |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1118 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1119 | |
| 1120 | match = xfrm_selector_match(sel, fl, family); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1121 | if (match) |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1122 | ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid, |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1123 | dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1124 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1125 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1128 | static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type, |
David S. Miller | 062cdb4 | 2011-02-22 18:31:08 -0800 | [diff] [blame] | 1129 | const struct flowi *fl, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1130 | u16 family, u8 dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1132 | int err; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1133 | struct xfrm_policy *pol, *ret; |
David S. Miller | 0b597e7 | 2011-02-24 01:22:48 -0500 | [diff] [blame] | 1134 | const xfrm_address_t *daddr, *saddr; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1135 | struct hlist_head *chain; |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 1136 | unsigned int sequence; |
| 1137 | u32 priority; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1138 | |
| 1139 | daddr = xfrm_flowi_daddr(fl, family); |
| 1140 | saddr = xfrm_flowi_saddr(fl, family); |
| 1141 | if (unlikely(!daddr || !saddr)) |
| 1142 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | |
Florian Westphal | a7c44247 | 2016-08-11 15:17:56 +0200 | [diff] [blame] | 1144 | rcu_read_lock(); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 1145 | retry: |
| 1146 | do { |
| 1147 | sequence = read_seqcount_begin(&xfrm_policy_hash_generation); |
| 1148 | chain = policy_hash_direct(net, daddr, saddr, family, dir); |
| 1149 | } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)); |
| 1150 | |
| 1151 | priority = ~0U; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1152 | ret = NULL; |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 1153 | hlist_for_each_entry_rcu(pol, chain, bydst) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1154 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 1155 | if (err) { |
| 1156 | if (err == -ESRCH) |
| 1157 | continue; |
| 1158 | else { |
| 1159 | ret = ERR_PTR(err); |
| 1160 | goto fail; |
| 1161 | } |
| 1162 | } else { |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 1163 | ret = pol; |
| 1164 | priority = ret->priority; |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1168 | chain = &net->xfrm.policy_inexact[dir]; |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 1169 | hlist_for_each_entry_rcu(pol, chain, bydst) { |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 1170 | if ((pol->priority >= priority) && ret) |
| 1171 | break; |
| 1172 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1173 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 1174 | if (err) { |
| 1175 | if (err == -ESRCH) |
| 1176 | continue; |
| 1177 | else { |
| 1178 | ret = ERR_PTR(err); |
| 1179 | goto fail; |
| 1180 | } |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 1181 | } else { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1182 | ret = pol; |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
Li RongQing | 586f2eb | 2015-04-30 17:13:41 +0800 | [diff] [blame] | 1186 | |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 1187 | if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)) |
| 1188 | goto retry; |
| 1189 | |
Florian Westphal | e37cc8a | 2016-08-11 15:17:55 +0200 | [diff] [blame] | 1190 | if (ret && !xfrm_pol_hold_rcu(ret)) |
| 1191 | goto retry; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1192 | fail: |
Florian Westphal | a7c44247 | 2016-08-11 15:17:56 +0200 | [diff] [blame] | 1193 | rcu_read_unlock(); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1194 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1195 | return ret; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1198 | static struct xfrm_policy * |
David S. Miller | 73ff93c | 2011-02-22 18:33:42 -0800 | [diff] [blame] | 1199 | __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1200 | { |
| 1201 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1202 | struct xfrm_policy *pol; |
| 1203 | |
| 1204 | pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir); |
| 1205 | if (pol != NULL) |
| 1206 | return pol; |
| 1207 | #endif |
| 1208 | return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir); |
| 1209 | } |
| 1210 | |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 1211 | static int flow_to_policy_dir(int dir) |
| 1212 | { |
| 1213 | if (XFRM_POLICY_IN == FLOW_DIR_IN && |
| 1214 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
| 1215 | XFRM_POLICY_FWD == FLOW_DIR_FWD) |
| 1216 | return dir; |
| 1217 | |
| 1218 | switch (dir) { |
| 1219 | default: |
| 1220 | case FLOW_DIR_IN: |
| 1221 | return XFRM_POLICY_IN; |
| 1222 | case FLOW_DIR_OUT: |
| 1223 | return XFRM_POLICY_OUT; |
| 1224 | case FLOW_DIR_FWD: |
| 1225 | return XFRM_POLICY_FWD; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1229 | static struct flow_cache_object * |
David S. Miller | dee9f4b | 2011-02-22 18:44:31 -0800 | [diff] [blame] | 1230 | xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1231 | u8 dir, struct flow_cache_object *old_obj, void *ctx) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1232 | { |
| 1233 | struct xfrm_policy *pol; |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1234 | |
| 1235 | if (old_obj) |
| 1236 | xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo)); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1237 | |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 1238 | pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir)); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1239 | if (IS_ERR_OR_NULL(pol)) |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1240 | return ERR_CAST(pol); |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1241 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1242 | /* Resolver returns two references: |
| 1243 | * one for cache and one for caller of flow_cache_lookup() */ |
| 1244 | xfrm_pol_hold(pol); |
| 1245 | |
| 1246 | return &pol->flo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1249 | static inline int policy_to_flow_dir(int dir) |
| 1250 | { |
| 1251 | if (XFRM_POLICY_IN == FLOW_DIR_IN && |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1252 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
| 1253 | XFRM_POLICY_FWD == FLOW_DIR_FWD) |
| 1254 | return dir; |
| 1255 | switch (dir) { |
| 1256 | default: |
| 1257 | case XFRM_POLICY_IN: |
| 1258 | return FLOW_DIR_IN; |
| 1259 | case XFRM_POLICY_OUT: |
| 1260 | return FLOW_DIR_OUT; |
| 1261 | case XFRM_POLICY_FWD: |
| 1262 | return FLOW_DIR_FWD; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1263 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1264 | } |
| 1265 | |
Eric Dumazet | 6f9c961 | 2015-09-25 07:39:10 -0700 | [diff] [blame] | 1266 | static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, |
Steffen Klassert | 0b86564 | 2017-02-14 07:43:56 +0100 | [diff] [blame] | 1267 | const struct flowi *fl, u16 family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | { |
| 1269 | struct xfrm_policy *pol; |
| 1270 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1271 | rcu_read_lock(); |
Florian Westphal | ae33786 | 2016-08-11 15:17:57 +0200 | [diff] [blame] | 1272 | again: |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1273 | pol = rcu_dereference(sk->sk_policy[dir]); |
| 1274 | if (pol != NULL) { |
Steffen Klassert | 46b3171 | 2017-11-29 06:53:55 +0100 | [diff] [blame] | 1275 | bool match; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1276 | int err = 0; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1277 | |
Steffen Klassert | 46b3171 | 2017-11-29 06:53:55 +0100 | [diff] [blame] | 1278 | if (pol->family != family) { |
| 1279 | pol = NULL; |
| 1280 | goto out; |
| 1281 | } |
| 1282 | |
| 1283 | match = xfrm_selector_match(&pol->selector, fl, family); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1284 | if (match) { |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1285 | if ((sk->sk_mark & pol->mark.m) != pol->mark.v || |
| 1286 | pol->if_id != fl->flowi_xfrm.if_id) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1287 | pol = NULL; |
| 1288 | goto out; |
| 1289 | } |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1290 | err = security_xfrm_policy_lookup(pol->security, |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1291 | fl->flowi_secid, |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1292 | policy_to_flow_dir(dir)); |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 1293 | if (!err) { |
| 1294 | if (!xfrm_pol_hold_rcu(pol)) |
| 1295 | goto again; |
| 1296 | } else if (err == -ESRCH) { |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1297 | pol = NULL; |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 1298 | } else { |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1299 | pol = ERR_PTR(err); |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 1300 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1301 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | pol = NULL; |
| 1303 | } |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1304 | out: |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1305 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | return pol; |
| 1307 | } |
| 1308 | |
| 1309 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir) |
| 1310 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1311 | struct net *net = xp_net(pol); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1312 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1313 | list_add(&pol->walk.all, &net->xfrm.policy_all); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1314 | net->xfrm.policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | xfrm_pol_hold(pol); |
| 1316 | } |
| 1317 | |
| 1318 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 1319 | int dir) |
| 1320 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1321 | struct net *net = xp_net(pol); |
| 1322 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1323 | if (list_empty(&pol->walk.all)) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1324 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1326 | /* Socket policies are not hashed. */ |
| 1327 | if (!hlist_unhashed(&pol->bydst)) { |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 1328 | hlist_del_rcu(&pol->bydst); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1329 | hlist_del(&pol->byidx); |
| 1330 | } |
| 1331 | |
| 1332 | list_del_init(&pol->walk.all); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1333 | net->xfrm.policy_count[dir]--; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1334 | |
| 1335 | return pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1338 | static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir) |
| 1339 | { |
| 1340 | __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir); |
| 1341 | } |
| 1342 | |
| 1343 | static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir) |
| 1344 | { |
| 1345 | __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir); |
| 1346 | } |
| 1347 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1348 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1350 | struct net *net = xp_net(pol); |
| 1351 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1352 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | pol = __xfrm_policy_unlink(pol, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1354 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | if (pol) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | xfrm_policy_kill(pol); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1357 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1359 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1361 | EXPORT_SYMBOL(xfrm_policy_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol) |
| 1364 | { |
Lorenzo Colitti | 38884cc | 2017-11-20 19:26:02 +0900 | [diff] [blame] | 1365 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | struct xfrm_policy *old_pol; |
| 1367 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1368 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1369 | if (pol && pol->type != XFRM_POLICY_TYPE_MAIN) |
| 1370 | return -EINVAL; |
| 1371 | #endif |
| 1372 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1373 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1374 | old_pol = rcu_dereference_protected(sk->sk_policy[dir], |
| 1375 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | if (pol) { |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1377 | pol->curlft.add_time = get_seconds(); |
Fan Du | e682adf | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 1378 | pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1379 | xfrm_sk_policy_link(pol, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | } |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1381 | rcu_assign_pointer(sk->sk_policy[dir], pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1382 | if (old_pol) { |
| 1383 | if (pol) |
| 1384 | xfrm_policy_requeue(old_pol, pol); |
| 1385 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 1386 | /* Unlinking succeeds always. This is the only function |
| 1387 | * allowed to delete or replace socket policy. |
| 1388 | */ |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1389 | xfrm_sk_policy_unlink(old_pol, dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1390 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1391 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
| 1393 | if (old_pol) { |
| 1394 | xfrm_policy_kill(old_pol); |
| 1395 | } |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
David S. Miller | d3e40a9 | 2011-02-24 01:25:41 -0500 | [diff] [blame] | 1399 | static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 1401 | struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1402 | struct net *net = xp_net(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (newp) { |
| 1405 | newp->selector = old->selector; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1406 | if (security_xfrm_policy_clone(old->security, |
| 1407 | &newp->security)) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1408 | kfree(newp); |
| 1409 | return NULL; /* ENOMEM */ |
| 1410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | newp->lft = old->lft; |
| 1412 | newp->curlft = old->curlft; |
Jamal Hadi Salim | fb977e2 | 2010-02-23 15:09:53 -0800 | [diff] [blame] | 1413 | newp->mark = old->mark; |
Steffen Klassert | a80f560 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1414 | newp->if_id = old->if_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | newp->action = old->action; |
| 1416 | newp->flags = old->flags; |
| 1417 | newp->xfrm_nr = old->xfrm_nr; |
| 1418 | newp->index = old->index; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1419 | newp->type = old->type; |
Herbert Xu | 6ceabde | 2017-11-10 14:14:06 +1100 | [diff] [blame] | 1420 | newp->family = old->family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | memcpy(newp->xfrm_vec, old->xfrm_vec, |
| 1422 | newp->xfrm_nr*sizeof(struct xfrm_tmpl)); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1423 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1424 | xfrm_sk_policy_link(newp, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1425 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | xfrm_pol_put(newp); |
| 1427 | } |
| 1428 | return newp; |
| 1429 | } |
| 1430 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1431 | int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | { |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1433 | const struct xfrm_policy *p; |
| 1434 | struct xfrm_policy *np; |
| 1435 | int i, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 1437 | rcu_read_lock(); |
| 1438 | for (i = 0; i < 2; i++) { |
| 1439 | p = rcu_dereference(osk->sk_policy[i]); |
| 1440 | if (p) { |
| 1441 | np = clone_policy(p, i); |
| 1442 | if (unlikely(!np)) { |
| 1443 | ret = -ENOMEM; |
| 1444 | break; |
| 1445 | } |
| 1446 | rcu_assign_pointer(sk->sk_policy[i], np); |
| 1447 | } |
| 1448 | } |
| 1449 | rcu_read_unlock(); |
| 1450 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1453 | static int |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 1454 | xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local, |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 1455 | xfrm_address_t *remote, unsigned short family, u32 mark) |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1456 | { |
| 1457 | int err; |
| 1458 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1459 | |
| 1460 | if (unlikely(afinfo == NULL)) |
| 1461 | return -EINVAL; |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 1462 | err = afinfo->get_saddr(net, oif, local, remote, mark); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1463 | xfrm_policy_put_afinfo(afinfo); |
| 1464 | return err; |
| 1465 | } |
| 1466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* Resolve list of templates for the flow, given policy. */ |
| 1468 | |
| 1469 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 1470 | xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl, |
| 1471 | struct xfrm_state **xfrm, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | { |
Alexey Dobriyan | fbda33b | 2008-11-25 17:56:49 -0800 | [diff] [blame] | 1473 | struct net *net = xp_net(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | int nx; |
| 1475 | int i, error; |
| 1476 | xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family); |
| 1477 | xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1478 | xfrm_address_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 1480 | for (nx = 0, i = 0; i < policy->xfrm_nr; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | struct xfrm_state *x; |
| 1482 | xfrm_address_t *remote = daddr; |
| 1483 | xfrm_address_t *local = saddr; |
| 1484 | struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i]; |
| 1485 | |
Joakim Koskela | 48b8d78 | 2007-07-26 00:08:42 -0700 | [diff] [blame] | 1486 | if (tmpl->mode == XFRM_MODE_TUNNEL || |
| 1487 | tmpl->mode == XFRM_MODE_BEET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | remote = &tmpl->id.daddr; |
| 1489 | local = &tmpl->saddr; |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 1490 | if (xfrm_addr_any(local, tmpl->encap_family)) { |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 1491 | error = xfrm_get_saddr(net, fl->flowi_oif, |
| 1492 | &tmp, remote, |
Lorenzo Colitti | 34e23de | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 1493 | tmpl->encap_family, 0); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1494 | if (error) |
| 1495 | goto fail; |
| 1496 | local = &tmp; |
| 1497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family); |
| 1501 | |
| 1502 | if (x && x->km.state == XFRM_STATE_VALID) { |
| 1503 | xfrm[nx++] = x; |
| 1504 | daddr = remote; |
| 1505 | saddr = local; |
| 1506 | continue; |
| 1507 | } |
| 1508 | if (x) { |
| 1509 | error = (x->km.state == XFRM_STATE_ERROR ? |
| 1510 | -EINVAL : -EAGAIN); |
| 1511 | xfrm_state_put(x); |
Weilong Chen | 42054569 | 2013-12-24 09:43:49 +0800 | [diff] [blame] | 1512 | } else if (error == -ESRCH) { |
fernando@oss.ntt.co | a4322266 | 2008-10-23 04:27:19 +0000 | [diff] [blame] | 1513 | error = -EAGAIN; |
Weilong Chen | 42054569 | 2013-12-24 09:43:49 +0800 | [diff] [blame] | 1514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | if (!tmpl->optional) |
| 1517 | goto fail; |
| 1518 | } |
| 1519 | return nx; |
| 1520 | |
| 1521 | fail: |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 1522 | for (nx--; nx >= 0; nx--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | xfrm_state_put(xfrm[nx]); |
| 1524 | return error; |
| 1525 | } |
| 1526 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1527 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 1528 | xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl, |
| 1529 | struct xfrm_state **xfrm, unsigned short family) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1530 | { |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1531 | struct xfrm_state *tp[XFRM_MAX_DEPTH]; |
| 1532 | struct xfrm_state **tpp = (npols > 1) ? tp : xfrm; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1533 | int cnx = 0; |
| 1534 | int error; |
| 1535 | int ret; |
| 1536 | int i; |
| 1537 | |
| 1538 | for (i = 0; i < npols; i++) { |
| 1539 | if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) { |
| 1540 | error = -ENOBUFS; |
| 1541 | goto fail; |
| 1542 | } |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1543 | |
| 1544 | ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1545 | if (ret < 0) { |
| 1546 | error = ret; |
| 1547 | goto fail; |
| 1548 | } else |
| 1549 | cnx += ret; |
| 1550 | } |
| 1551 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1552 | /* found states are sorted for outbound processing */ |
| 1553 | if (npols > 1) |
| 1554 | xfrm_state_sort(xfrm, tpp, cnx, family); |
| 1555 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1556 | return cnx; |
| 1557 | |
| 1558 | fail: |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 1559 | for (cnx--; cnx >= 0; cnx--) |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1560 | xfrm_state_put(tpp[cnx]); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1561 | return error; |
| 1562 | |
| 1563 | } |
| 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | /* Check that the bundle accepts the flow and its components are |
| 1566 | * still valid. |
| 1567 | */ |
| 1568 | |
David S. Miller | 05d8402 | 2011-02-22 17:47:10 -0800 | [diff] [blame] | 1569 | static inline int xfrm_get_tos(const struct flowi *fl, int family) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1570 | { |
| 1571 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1572 | int tos; |
| 1573 | |
| 1574 | if (!afinfo) |
| 1575 | return -EINVAL; |
| 1576 | |
| 1577 | tos = afinfo->get_tos(fl); |
| 1578 | |
| 1579 | xfrm_policy_put_afinfo(afinfo); |
| 1580 | |
| 1581 | return tos; |
| 1582 | } |
| 1583 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1584 | static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo) |
| 1585 | { |
| 1586 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1587 | struct dst_entry *dst = &xdst->u.dst; |
| 1588 | |
| 1589 | if (xdst->route == NULL) { |
| 1590 | /* Dummy bundle - if it has xfrms we were not |
| 1591 | * able to build bundle as template resolution failed. |
| 1592 | * It means we need to try again resolving. */ |
| 1593 | if (xdst->num_xfrms > 0) |
| 1594 | return NULL; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1595 | } else if (dst->flags & DST_XFRM_QUEUE) { |
| 1596 | return NULL; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1597 | } else { |
| 1598 | /* Real bundle */ |
| 1599 | if (stale_bundle(dst)) |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | |
| 1603 | dst_hold(dst); |
| 1604 | return flo; |
| 1605 | } |
| 1606 | |
| 1607 | static int xfrm_bundle_flo_check(struct flow_cache_object *flo) |
| 1608 | { |
| 1609 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1610 | struct dst_entry *dst = &xdst->u.dst; |
| 1611 | |
| 1612 | if (!xdst->route) |
| 1613 | return 0; |
| 1614 | if (stale_bundle(dst)) |
| 1615 | return 0; |
| 1616 | |
| 1617 | return 1; |
| 1618 | } |
| 1619 | |
| 1620 | static void xfrm_bundle_flo_delete(struct flow_cache_object *flo) |
| 1621 | { |
| 1622 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1623 | struct dst_entry *dst = &xdst->u.dst; |
| 1624 | |
| 1625 | dst_free(dst); |
| 1626 | } |
| 1627 | |
| 1628 | static const struct flow_cache_ops xfrm_bundle_fc_ops = { |
| 1629 | .get = xfrm_bundle_flo_get, |
| 1630 | .check = xfrm_bundle_flo_check, |
| 1631 | .delete = xfrm_bundle_flo_delete, |
| 1632 | }; |
| 1633 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1634 | static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1635 | { |
| 1636 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1637 | struct dst_ops *dst_ops; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1638 | struct xfrm_dst *xdst; |
| 1639 | |
| 1640 | if (!afinfo) |
| 1641 | return ERR_PTR(-EINVAL); |
| 1642 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1643 | switch (family) { |
| 1644 | case AF_INET: |
| 1645 | dst_ops = &net->xfrm.xfrm4_dst_ops; |
| 1646 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1647 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1648 | case AF_INET6: |
| 1649 | dst_ops = &net->xfrm.xfrm6_dst_ops; |
| 1650 | break; |
| 1651 | #endif |
| 1652 | default: |
| 1653 | BUG(); |
| 1654 | } |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 1655 | xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1656 | |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1657 | if (likely(xdst)) { |
Steffen Klassert | 141e369 | 2012-07-05 23:39:34 +0000 | [diff] [blame] | 1658 | struct dst_entry *dst = &xdst->u.dst; |
| 1659 | |
| 1660 | memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst)); |
Hiroaki SHIMODA | 0b15093 | 2011-02-10 23:08:33 -0800 | [diff] [blame] | 1661 | xdst->flo.ops = &xfrm_bundle_fc_ops; |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1662 | } else |
Hiroaki SHIMODA | 0b15093 | 2011-02-10 23:08:33 -0800 | [diff] [blame] | 1663 | xdst = ERR_PTR(-ENOBUFS); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1664 | |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1665 | xfrm_policy_put_afinfo(afinfo); |
| 1666 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1667 | return xdst; |
| 1668 | } |
| 1669 | |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1670 | static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst, |
| 1671 | int nfheader_len) |
| 1672 | { |
| 1673 | struct xfrm_policy_afinfo *afinfo = |
| 1674 | xfrm_policy_get_afinfo(dst->ops->family); |
| 1675 | int err; |
| 1676 | |
| 1677 | if (!afinfo) |
| 1678 | return -EINVAL; |
| 1679 | |
| 1680 | err = afinfo->init_path(path, dst, nfheader_len); |
| 1681 | |
| 1682 | xfrm_policy_put_afinfo(afinfo); |
| 1683 | |
| 1684 | return err; |
| 1685 | } |
| 1686 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1687 | static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
David S. Miller | 0c7b3ee | 2011-02-22 17:48:57 -0800 | [diff] [blame] | 1688 | const struct flowi *fl) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1689 | { |
| 1690 | struct xfrm_policy_afinfo *afinfo = |
| 1691 | xfrm_policy_get_afinfo(xdst->u.dst.ops->family); |
| 1692 | int err; |
| 1693 | |
| 1694 | if (!afinfo) |
| 1695 | return -EINVAL; |
| 1696 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1697 | err = afinfo->fill_dst(xdst, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1698 | |
| 1699 | xfrm_policy_put_afinfo(afinfo); |
| 1700 | |
| 1701 | return err; |
| 1702 | } |
| 1703 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 1706 | * all the metrics... Shortly, bundle a bundle. |
| 1707 | */ |
| 1708 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1709 | static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy, |
| 1710 | struct xfrm_state **xfrm, int nx, |
David S. Miller | 98313ad | 2011-02-22 18:36:50 -0800 | [diff] [blame] | 1711 | const struct flowi *fl, |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1712 | struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1714 | struct net *net = xp_net(policy); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1715 | unsigned long now = jiffies; |
| 1716 | struct net_device *dev; |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1717 | struct xfrm_mode *inner_mode; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1718 | struct dst_entry *dst_prev = NULL; |
| 1719 | struct dst_entry *dst0 = NULL; |
| 1720 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | int err; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1722 | int header_len = 0; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1723 | int nfheader_len = 0; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1724 | int trailer_len = 0; |
| 1725 | int tos; |
| 1726 | int family = policy->selector.family; |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 1727 | xfrm_address_t saddr, daddr; |
| 1728 | |
| 1729 | xfrm_flowi_addr_get(fl, &saddr, &daddr, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1730 | |
| 1731 | tos = xfrm_get_tos(fl, family); |
| 1732 | err = tos; |
| 1733 | if (tos < 0) |
| 1734 | goto put_states; |
| 1735 | |
| 1736 | dst_hold(dst); |
| 1737 | |
| 1738 | for (; i < nx; i++) { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1739 | struct xfrm_dst *xdst = xfrm_alloc_dst(net, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1740 | struct dst_entry *dst1 = &xdst->u.dst; |
| 1741 | |
| 1742 | err = PTR_ERR(xdst); |
| 1743 | if (IS_ERR(xdst)) { |
| 1744 | dst_release(dst); |
| 1745 | goto put_states; |
| 1746 | } |
| 1747 | |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1748 | if (xfrm[i]->sel.family == AF_UNSPEC) { |
| 1749 | inner_mode = xfrm_ip2inner_mode(xfrm[i], |
| 1750 | xfrm_af2proto(family)); |
| 1751 | if (!inner_mode) { |
| 1752 | err = -EAFNOSUPPORT; |
| 1753 | dst_release(dst); |
| 1754 | goto put_states; |
| 1755 | } |
| 1756 | } else |
| 1757 | inner_mode = xfrm[i]->inner_mode; |
| 1758 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1759 | if (!dst_prev) |
| 1760 | dst0 = dst1; |
| 1761 | else { |
| 1762 | dst_prev->child = dst_clone(dst1); |
| 1763 | dst1->flags |= DST_NOHASH; |
| 1764 | } |
| 1765 | |
| 1766 | xdst->route = dst; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1767 | dst_copy_metrics(dst1, dst); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1768 | |
| 1769 | if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) { |
Benedict Wong | 6fffec4 | 2019-01-14 11:24:38 -0800 | [diff] [blame] | 1770 | __u32 mark = 0; |
| 1771 | |
| 1772 | if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m) |
| 1773 | mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]); |
Steffen Klassert | 78f10c5 | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 1774 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1775 | family = xfrm[i]->props.family; |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 1776 | dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif, |
Steffen Klassert | 78f10c5 | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 1777 | &saddr, &daddr, family, mark); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1778 | err = PTR_ERR(dst); |
| 1779 | if (IS_ERR(dst)) |
| 1780 | goto put_states; |
| 1781 | } else |
| 1782 | dst_hold(dst); |
| 1783 | |
| 1784 | dst1->xfrm = xfrm[i]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1785 | xdst->xfrm_genid = xfrm[i]->genid; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1786 | |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 1787 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1788 | dst1->flags |= DST_HOST; |
| 1789 | dst1->lastuse = now; |
| 1790 | |
| 1791 | dst1->input = dst_discard; |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1792 | dst1->output = inner_mode->afinfo->output; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1793 | |
| 1794 | dst1->next = dst_prev; |
| 1795 | dst_prev = dst1; |
| 1796 | |
| 1797 | header_len += xfrm[i]->props.header_len; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1798 | if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT) |
| 1799 | nfheader_len += xfrm[i]->props.header_len; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1800 | trailer_len += xfrm[i]->props.trailer_len; |
| 1801 | } |
| 1802 | |
| 1803 | dst_prev->child = dst; |
| 1804 | dst0->path = dst; |
| 1805 | |
| 1806 | err = -ENODEV; |
| 1807 | dev = dst->dev; |
| 1808 | if (!dev) |
| 1809 | goto free_dst; |
| 1810 | |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1811 | xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1812 | xfrm_init_pmtu(dst_prev); |
| 1813 | |
| 1814 | for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) { |
| 1815 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev; |
| 1816 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1817 | err = xfrm_fill_dst(xdst, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1818 | if (err) |
| 1819 | goto free_dst; |
| 1820 | |
| 1821 | dst_prev->header_len = header_len; |
| 1822 | dst_prev->trailer_len = trailer_len; |
| 1823 | header_len -= xdst->u.dst.xfrm->props.header_len; |
| 1824 | trailer_len -= xdst->u.dst.xfrm->props.trailer_len; |
| 1825 | } |
| 1826 | |
| 1827 | out: |
| 1828 | return dst0; |
| 1829 | |
| 1830 | put_states: |
| 1831 | for (; i < nx; i++) |
| 1832 | xfrm_state_put(xfrm[i]); |
| 1833 | free_dst: |
| 1834 | if (dst0) |
| 1835 | dst_free(dst0); |
| 1836 | dst0 = ERR_PTR(err); |
| 1837 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
David S. Miller | 73ff93c | 2011-02-22 18:33:42 -0800 | [diff] [blame] | 1840 | static int xfrm_expand_policies(const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1841 | struct xfrm_policy **pols, |
| 1842 | int *num_pols, int *num_xfrms) |
| 1843 | { |
| 1844 | int i; |
| 1845 | |
| 1846 | if (*num_pols == 0 || !pols[0]) { |
| 1847 | *num_pols = 0; |
| 1848 | *num_xfrms = 0; |
| 1849 | return 0; |
| 1850 | } |
| 1851 | if (IS_ERR(pols[0])) |
| 1852 | return PTR_ERR(pols[0]); |
| 1853 | |
| 1854 | *num_xfrms = pols[0]->xfrm_nr; |
| 1855 | |
| 1856 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1857 | if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW && |
| 1858 | pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
| 1859 | pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]), |
| 1860 | XFRM_POLICY_TYPE_MAIN, |
| 1861 | fl, family, |
| 1862 | XFRM_POLICY_OUT); |
| 1863 | if (pols[1]) { |
| 1864 | if (IS_ERR(pols[1])) { |
| 1865 | xfrm_pols_put(pols, *num_pols); |
| 1866 | return PTR_ERR(pols[1]); |
| 1867 | } |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 1868 | (*num_pols)++; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1869 | (*num_xfrms) += pols[1]->xfrm_nr; |
| 1870 | } |
| 1871 | } |
| 1872 | #endif |
| 1873 | for (i = 0; i < *num_pols; i++) { |
| 1874 | if (pols[i]->action != XFRM_POLICY_ALLOW) { |
| 1875 | *num_xfrms = -1; |
| 1876 | break; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | return 0; |
| 1881 | |
| 1882 | } |
| 1883 | |
| 1884 | static struct xfrm_dst * |
| 1885 | xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols, |
David S. Miller | 4ca2e68 | 2011-02-22 18:38:51 -0800 | [diff] [blame] | 1886 | const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1887 | struct dst_entry *dst_orig) |
| 1888 | { |
| 1889 | struct net *net = xp_net(pols[0]); |
| 1890 | struct xfrm_state *xfrm[XFRM_MAX_DEPTH]; |
| 1891 | struct dst_entry *dst; |
| 1892 | struct xfrm_dst *xdst; |
| 1893 | int err; |
| 1894 | |
| 1895 | /* Try to instantiate a bundle */ |
| 1896 | err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family); |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 1897 | if (err <= 0) { |
YueHaibing | af322ba | 2018-07-25 16:54:33 +0800 | [diff] [blame] | 1898 | if (err == 0) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | if (err != -EAGAIN) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1902 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 1903 | return ERR_PTR(err); |
| 1904 | } |
| 1905 | |
| 1906 | dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig); |
| 1907 | if (IS_ERR(dst)) { |
| 1908 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR); |
| 1909 | return ERR_CAST(dst); |
| 1910 | } |
| 1911 | |
| 1912 | xdst = (struct xfrm_dst *)dst; |
| 1913 | xdst->num_xfrms = err; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1914 | xdst->num_pols = num_pols; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 1915 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1916 | xdst->policy_genid = atomic_read(&pols[0]->genid); |
| 1917 | |
| 1918 | return xdst; |
| 1919 | } |
| 1920 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1921 | static void xfrm_policy_queue_process(unsigned long arg) |
| 1922 | { |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1923 | struct sk_buff *skb; |
| 1924 | struct sock *sk; |
| 1925 | struct dst_entry *dst; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1926 | struct xfrm_policy *pol = (struct xfrm_policy *)arg; |
Eric W. Biederman | 3f5312a | 2015-10-07 16:48:34 -0500 | [diff] [blame] | 1927 | struct net *net = xp_net(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1928 | struct xfrm_policy_queue *pq = &pol->polq; |
| 1929 | struct flowi fl; |
| 1930 | struct sk_buff_head list; |
| 1931 | |
| 1932 | spin_lock(&pq->hold_queue.lock); |
| 1933 | skb = skb_peek(&pq->hold_queue); |
Steffen Klassert | 2bb53e2 | 2013-10-08 10:49:51 +0200 | [diff] [blame] | 1934 | if (!skb) { |
| 1935 | spin_unlock(&pq->hold_queue.lock); |
| 1936 | goto out; |
| 1937 | } |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1938 | dst = skb_dst(skb); |
| 1939 | sk = skb->sk; |
| 1940 | xfrm_decode_session(skb, &fl, dst->ops->family); |
| 1941 | spin_unlock(&pq->hold_queue.lock); |
| 1942 | |
| 1943 | dst_hold(dst->path); |
Eric W. Biederman | 3f5312a | 2015-10-07 16:48:34 -0500 | [diff] [blame] | 1944 | dst = xfrm_lookup(net, dst->path, &fl, sk, 0); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1945 | if (IS_ERR(dst)) |
| 1946 | goto purge_queue; |
| 1947 | |
| 1948 | if (dst->flags & DST_XFRM_QUEUE) { |
| 1949 | dst_release(dst); |
| 1950 | |
| 1951 | if (pq->timeout >= XFRM_QUEUE_TMO_MAX) |
| 1952 | goto purge_queue; |
| 1953 | |
| 1954 | pq->timeout = pq->timeout << 1; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 1955 | if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout)) |
| 1956 | xfrm_pol_hold(pol); |
| 1957 | goto out; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | dst_release(dst); |
| 1961 | |
| 1962 | __skb_queue_head_init(&list); |
| 1963 | |
| 1964 | spin_lock(&pq->hold_queue.lock); |
| 1965 | pq->timeout = 0; |
| 1966 | skb_queue_splice_init(&pq->hold_queue, &list); |
| 1967 | spin_unlock(&pq->hold_queue.lock); |
| 1968 | |
| 1969 | while (!skb_queue_empty(&list)) { |
| 1970 | skb = __skb_dequeue(&list); |
| 1971 | |
| 1972 | xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family); |
| 1973 | dst_hold(skb_dst(skb)->path); |
Eric W. Biederman | 3f5312a | 2015-10-07 16:48:34 -0500 | [diff] [blame] | 1974 | dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1975 | if (IS_ERR(dst)) { |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1976 | kfree_skb(skb); |
| 1977 | continue; |
| 1978 | } |
| 1979 | |
| 1980 | nf_reset(skb); |
| 1981 | skb_dst_drop(skb); |
| 1982 | skb_dst_set(skb, dst); |
| 1983 | |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 1984 | dst_output(net, skb->sk, skb); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 1987 | out: |
| 1988 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1989 | return; |
| 1990 | |
| 1991 | purge_queue: |
| 1992 | pq->timeout = 0; |
Li RongQing | 1ee5e66 | 2015-04-22 15:51:16 +0800 | [diff] [blame] | 1993 | skb_queue_purge(&pq->hold_queue); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 1994 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 1997 | static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1998 | { |
| 1999 | unsigned long sched_next; |
| 2000 | struct dst_entry *dst = skb_dst(skb); |
| 2001 | struct xfrm_dst *xdst = (struct xfrm_dst *) dst; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2002 | struct xfrm_policy *pol = xdst->pols[0]; |
| 2003 | struct xfrm_policy_queue *pq = &pol->polq; |
Steffen Klassert | 4d53eff | 2013-10-16 13:42:46 +0200 | [diff] [blame] | 2004 | |
Eric Dumazet | 39bb5e6 | 2014-10-30 10:32:34 -0700 | [diff] [blame] | 2005 | if (unlikely(skb_fclone_busy(sk, skb))) { |
Steffen Klassert | 4d53eff | 2013-10-16 13:42:46 +0200 | [diff] [blame] | 2006 | kfree_skb(skb); |
| 2007 | return 0; |
| 2008 | } |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2009 | |
| 2010 | if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) { |
| 2011 | kfree_skb(skb); |
| 2012 | return -EAGAIN; |
| 2013 | } |
| 2014 | |
| 2015 | skb_dst_force(skb); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2016 | |
| 2017 | spin_lock_bh(&pq->hold_queue.lock); |
| 2018 | |
| 2019 | if (!pq->timeout) |
| 2020 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
| 2021 | |
| 2022 | sched_next = jiffies + pq->timeout; |
| 2023 | |
| 2024 | if (del_timer(&pq->hold_timer)) { |
| 2025 | if (time_before(pq->hold_timer.expires, sched_next)) |
| 2026 | sched_next = pq->hold_timer.expires; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2027 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | __skb_queue_tail(&pq->hold_queue, skb); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2031 | if (!mod_timer(&pq->hold_timer, sched_next)) |
| 2032 | xfrm_pol_hold(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2033 | |
| 2034 | spin_unlock_bh(&pq->hold_queue.lock); |
| 2035 | |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
| 2039 | static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net, |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2040 | struct xfrm_flo *xflo, |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2041 | const struct flowi *fl, |
| 2042 | int num_xfrms, |
| 2043 | u16 family) |
| 2044 | { |
| 2045 | int err; |
| 2046 | struct net_device *dev; |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2047 | struct dst_entry *dst; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2048 | struct dst_entry *dst1; |
| 2049 | struct xfrm_dst *xdst; |
| 2050 | |
| 2051 | xdst = xfrm_alloc_dst(net, family); |
| 2052 | if (IS_ERR(xdst)) |
| 2053 | return xdst; |
| 2054 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2055 | if (!(xflo->flags & XFRM_LOOKUP_QUEUE) || |
| 2056 | net->xfrm.sysctl_larval_drop || |
| 2057 | num_xfrms <= 0) |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2058 | return xdst; |
| 2059 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2060 | dst = xflo->dst_orig; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2061 | dst1 = &xdst->u.dst; |
| 2062 | dst_hold(dst); |
| 2063 | xdst->route = dst; |
| 2064 | |
| 2065 | dst_copy_metrics(dst1, dst); |
| 2066 | |
| 2067 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
| 2068 | dst1->flags |= DST_HOST | DST_XFRM_QUEUE; |
| 2069 | dst1->lastuse = jiffies; |
| 2070 | |
| 2071 | dst1->input = dst_discard; |
| 2072 | dst1->output = xdst_queue_output; |
| 2073 | |
| 2074 | dst_hold(dst); |
| 2075 | dst1->child = dst; |
| 2076 | dst1->path = dst; |
| 2077 | |
| 2078 | xfrm_init_path((struct xfrm_dst *)dst1, dst, 0); |
| 2079 | |
| 2080 | err = -ENODEV; |
| 2081 | dev = dst->dev; |
| 2082 | if (!dev) |
| 2083 | goto free_dst; |
| 2084 | |
| 2085 | err = xfrm_fill_dst(xdst, dev, fl); |
| 2086 | if (err) |
| 2087 | goto free_dst; |
| 2088 | |
| 2089 | out: |
| 2090 | return xdst; |
| 2091 | |
| 2092 | free_dst: |
| 2093 | dst_release(dst1); |
| 2094 | xdst = ERR_PTR(err); |
| 2095 | goto out; |
| 2096 | } |
| 2097 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2098 | static struct flow_cache_object * |
David S. Miller | dee9f4b | 2011-02-22 18:44:31 -0800 | [diff] [blame] | 2099 | xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2100 | struct flow_cache_object *oldflo, void *ctx) |
| 2101 | { |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2102 | struct xfrm_flo *xflo = (struct xfrm_flo *)ctx; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2103 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 2104 | struct xfrm_dst *xdst, *new_xdst; |
| 2105 | int num_pols = 0, num_xfrms = 0, i, err, pol_dead; |
| 2106 | |
| 2107 | /* Check if the policies from old bundle are usable */ |
| 2108 | xdst = NULL; |
| 2109 | if (oldflo) { |
| 2110 | xdst = container_of(oldflo, struct xfrm_dst, flo); |
| 2111 | num_pols = xdst->num_pols; |
| 2112 | num_xfrms = xdst->num_xfrms; |
| 2113 | pol_dead = 0; |
| 2114 | for (i = 0; i < num_pols; i++) { |
| 2115 | pols[i] = xdst->pols[i]; |
| 2116 | pol_dead |= pols[i]->walk.dead; |
| 2117 | } |
| 2118 | if (pol_dead) { |
| 2119 | dst_free(&xdst->u.dst); |
| 2120 | xdst = NULL; |
| 2121 | num_pols = 0; |
| 2122 | num_xfrms = 0; |
| 2123 | oldflo = NULL; |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | /* Resolve policies to use if we couldn't get them from |
| 2128 | * previous cache entry */ |
| 2129 | if (xdst == NULL) { |
| 2130 | num_pols = 1; |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 2131 | pols[0] = __xfrm_policy_lookup(net, fl, family, |
| 2132 | flow_to_policy_dir(dir)); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2133 | err = xfrm_expand_policies(fl, family, pols, |
| 2134 | &num_pols, &num_xfrms); |
| 2135 | if (err < 0) |
| 2136 | goto inc_error; |
| 2137 | if (num_pols == 0) |
| 2138 | return NULL; |
| 2139 | if (num_xfrms <= 0) |
| 2140 | goto make_dummy_bundle; |
| 2141 | } |
| 2142 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2143 | new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, |
| 2144 | xflo->dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2145 | if (IS_ERR(new_xdst)) { |
| 2146 | err = PTR_ERR(new_xdst); |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2147 | if (err == -EREMOTE) { |
| 2148 | xfrm_pols_put(pols, num_pols); |
| 2149 | return NULL; |
| 2150 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2151 | if (err != -EAGAIN) |
| 2152 | goto error; |
| 2153 | if (oldflo == NULL) |
| 2154 | goto make_dummy_bundle; |
| 2155 | dst_hold(&xdst->u.dst); |
| 2156 | return oldflo; |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 2157 | } else if (new_xdst == NULL) { |
| 2158 | num_xfrms = 0; |
| 2159 | if (oldflo == NULL) |
| 2160 | goto make_dummy_bundle; |
| 2161 | xdst->num_xfrms = 0; |
| 2162 | dst_hold(&xdst->u.dst); |
| 2163 | return oldflo; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | /* Kill the previous bundle */ |
| 2167 | if (xdst) { |
| 2168 | /* The policies were stolen for newly generated bundle */ |
| 2169 | xdst->num_pols = 0; |
| 2170 | dst_free(&xdst->u.dst); |
| 2171 | } |
| 2172 | |
| 2173 | /* Flow cache does not have reference, it dst_free()'s, |
| 2174 | * but we do need to return one reference for original caller */ |
| 2175 | dst_hold(&new_xdst->u.dst); |
| 2176 | return &new_xdst->flo; |
| 2177 | |
| 2178 | make_dummy_bundle: |
| 2179 | /* We found policies, but there's no bundles to instantiate: |
| 2180 | * either because the policy blocks, has no transformations or |
| 2181 | * we could not build template (no xfrm_states).*/ |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2182 | xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2183 | if (IS_ERR(xdst)) { |
| 2184 | xfrm_pols_put(pols, num_pols); |
| 2185 | return ERR_CAST(xdst); |
| 2186 | } |
| 2187 | xdst->num_pols = num_pols; |
| 2188 | xdst->num_xfrms = num_xfrms; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 2189 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2190 | |
| 2191 | dst_hold(&xdst->u.dst); |
| 2192 | return &xdst->flo; |
| 2193 | |
| 2194 | inc_error: |
| 2195 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 2196 | error: |
| 2197 | if (xdst != NULL) |
| 2198 | dst_free(&xdst->u.dst); |
| 2199 | else |
| 2200 | xfrm_pols_put(pols, num_pols); |
| 2201 | return ERR_PTR(err); |
| 2202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2204 | static struct dst_entry *make_blackhole(struct net *net, u16 family, |
| 2205 | struct dst_entry *dst_orig) |
| 2206 | { |
| 2207 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 2208 | struct dst_entry *ret; |
| 2209 | |
| 2210 | if (!afinfo) { |
| 2211 | dst_release(dst_orig); |
Li RongQing | 433a195 | 2012-09-17 22:40:10 +0000 | [diff] [blame] | 2212 | return ERR_PTR(-EINVAL); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2213 | } else { |
| 2214 | ret = afinfo->blackhole_route(net, dst_orig); |
| 2215 | } |
| 2216 | xfrm_policy_put_afinfo(afinfo); |
| 2217 | |
| 2218 | return ret; |
| 2219 | } |
| 2220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | /* Main function: finds/creates a bundle for given flow. |
| 2222 | * |
| 2223 | * At the moment we eat a raw IP route. Mostly to speed up lookups |
| 2224 | * on interfaces with disabled IPsec. |
| 2225 | */ |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2226 | struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig, |
| 2227 | const struct flowi *fl, |
Eric Dumazet | 6f9c961 | 2015-09-25 07:39:10 -0700 | [diff] [blame] | 2228 | const struct sock *sk, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | { |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2230 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2231 | struct flow_cache_object *flo; |
| 2232 | struct xfrm_dst *xdst; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2233 | struct dst_entry *dst, *route; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2234 | u16 family = dst_orig->ops->family; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 2235 | u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT); |
Changli Gao | 4b02162 | 2010-04-27 21:20:22 +0000 | [diff] [blame] | 2236 | int i, err, num_pols, num_xfrms = 0, drop_pols = 0; |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2237 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2238 | dst = NULL; |
| 2239 | xdst = NULL; |
| 2240 | route = NULL; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2241 | |
Eric Dumazet | bd5eb35 | 2015-12-07 08:53:17 -0800 | [diff] [blame] | 2242 | sk = sk_const_to_full_sk(sk); |
Thomas Graf | f7944fb | 2007-08-25 13:46:55 -0700 | [diff] [blame] | 2243 | if (sk && sk->sk_policy[XFRM_POLICY_OUT]) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2244 | num_pols = 1; |
Steffen Klassert | 0b86564 | 2017-02-14 07:43:56 +0100 | [diff] [blame] | 2245 | pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2246 | err = xfrm_expand_policies(fl, family, pols, |
| 2247 | &num_pols, &num_xfrms); |
| 2248 | if (err < 0) |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2249 | goto dropdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2250 | |
| 2251 | if (num_pols) { |
| 2252 | if (num_xfrms <= 0) { |
| 2253 | drop_pols = num_pols; |
| 2254 | goto no_transform; |
| 2255 | } |
| 2256 | |
| 2257 | xdst = xfrm_resolve_and_create_bundle( |
| 2258 | pols, num_pols, fl, |
| 2259 | family, dst_orig); |
| 2260 | if (IS_ERR(xdst)) { |
| 2261 | xfrm_pols_put(pols, num_pols); |
| 2262 | err = PTR_ERR(xdst); |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2263 | if (err == -EREMOTE) |
| 2264 | goto nopol; |
| 2265 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2266 | goto dropdst; |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 2267 | } else if (xdst == NULL) { |
| 2268 | num_xfrms = 0; |
| 2269 | drop_pols = num_pols; |
| 2270 | goto no_transform; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Steffen Klassert | b7eea45 | 2014-06-18 12:34:21 +0200 | [diff] [blame] | 2273 | dst_hold(&xdst->u.dst); |
| 2274 | xdst->u.dst.flags |= DST_NOCACHE; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2275 | route = xdst->route; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2276 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2279 | if (xdst == NULL) { |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2280 | struct xfrm_flo xflo; |
| 2281 | |
| 2282 | xflo.dst_orig = dst_orig; |
| 2283 | xflo.flags = flags; |
| 2284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | /* To accelerate a bit... */ |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2286 | if ((dst_orig->flags & DST_NOXFRM) || |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 2287 | !net->xfrm.policy_count[XFRM_POLICY_OUT]) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2288 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2290 | flo = flow_cache_lookup(net, fl, family, dir, |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2291 | xfrm_bundle_lookup, &xflo); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2292 | if (flo == NULL) |
| 2293 | goto nopol; |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 2294 | if (IS_ERR(flo)) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2295 | err = PTR_ERR(flo); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2296 | goto dropdst; |
Masahide NAKAMURA | d66e37a | 2008-01-07 21:46:15 -0800 | [diff] [blame] | 2297 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2298 | xdst = container_of(flo, struct xfrm_dst, flo); |
| 2299 | |
| 2300 | num_pols = xdst->num_pols; |
| 2301 | num_xfrms = xdst->num_xfrms; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 2302 | memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2303 | route = xdst->route; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2306 | dst = &xdst->u.dst; |
| 2307 | if (route == NULL && num_xfrms > 0) { |
| 2308 | /* The only case when xfrm_bundle_lookup() returns a |
| 2309 | * bundle with null route, is when the template could |
| 2310 | * not be resolved. It means policies are there, but |
| 2311 | * bundle could not be created, since we don't yet |
| 2312 | * have the xfrm_state's. We need to wait for KM to |
| 2313 | * negotiate new SA's or bail out with error.*/ |
| 2314 | if (net->xfrm.sysctl_larval_drop) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2315 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 2316 | err = -EREMOTE; |
| 2317 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2318 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2319 | |
Steffen Klassert | 5b8ef34 | 2013-08-27 13:43:30 +0200 | [diff] [blame] | 2320 | err = -EAGAIN; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2321 | |
| 2322 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
| 2323 | goto error; |
| 2324 | } |
| 2325 | |
| 2326 | no_transform: |
| 2327 | if (num_pols == 0) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2328 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2330 | if ((flags & XFRM_LOOKUP_ICMP) && |
| 2331 | !(pols[0]->flags & XFRM_POLICY_ICMP)) { |
| 2332 | err = -ENOENT; |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2333 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2334 | } |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2335 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2336 | for (i = 0; i < num_pols; i++) |
| 2337 | pols[i]->curlft.use_time = get_seconds(); |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2338 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2339 | if (num_xfrms < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | /* Prohibit the flow */ |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2341 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK); |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 2342 | err = -EPERM; |
| 2343 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2344 | } else if (num_xfrms > 0) { |
| 2345 | /* Flow transformed */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2346 | dst_release(dst_orig); |
| 2347 | } else { |
| 2348 | /* Flow passes untransformed */ |
| 2349 | dst_release(dst); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2350 | dst = dst_orig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2352 | ok: |
| 2353 | xfrm_pols_put(pols, drop_pols); |
Gao feng | 0c18337 | 2012-05-26 01:30:53 +0000 | [diff] [blame] | 2354 | if (dst && dst->xfrm && |
| 2355 | dst->xfrm->props.mode == XFRM_MODE_TUNNEL) |
| 2356 | dst->flags |= DST_XFRM_TUNNEL; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2357 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2359 | nopol: |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2360 | if (!(flags & XFRM_LOOKUP_ICMP)) { |
| 2361 | dst = dst_orig; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2362 | goto ok; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2363 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2364 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | error: |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2366 | dst_release(dst); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2367 | dropdst: |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 2368 | if (!(flags & XFRM_LOOKUP_KEEP_DST_REF)) |
| 2369 | dst_release(dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2370 | xfrm_pols_put(pols, drop_pols); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2371 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
| 2373 | EXPORT_SYMBOL(xfrm_lookup); |
| 2374 | |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 2375 | /* Callers of xfrm_lookup_route() must ensure a call to dst_output(). |
| 2376 | * Otherwise we may send out blackholed packets. |
| 2377 | */ |
| 2378 | struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig, |
| 2379 | const struct flowi *fl, |
Eric Dumazet | 6f9c961 | 2015-09-25 07:39:10 -0700 | [diff] [blame] | 2380 | const struct sock *sk, int flags) |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 2381 | { |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2382 | struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk, |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 2383 | flags | XFRM_LOOKUP_QUEUE | |
| 2384 | XFRM_LOOKUP_KEEP_DST_REF); |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 2385 | |
| 2386 | if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE) |
| 2387 | return make_blackhole(net, dst_orig->ops->family, dst_orig); |
| 2388 | |
Tommi Rantala | 590f312 | 2018-06-21 09:30:47 +0300 | [diff] [blame] | 2389 | if (IS_ERR(dst)) |
| 2390 | dst_release(dst_orig); |
| 2391 | |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 2392 | return dst; |
| 2393 | } |
| 2394 | EXPORT_SYMBOL(xfrm_lookup_route); |
| 2395 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2396 | static inline int |
David S. Miller | 8f029de | 2011-02-22 17:59:59 -0800 | [diff] [blame] | 2397 | xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl) |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2398 | { |
| 2399 | struct xfrm_state *x; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2400 | |
| 2401 | if (!skb->sp || idx < 0 || idx >= skb->sp->len) |
| 2402 | return 0; |
| 2403 | x = skb->sp->xvec[idx]; |
| 2404 | if (!x->type->reject) |
| 2405 | return 0; |
Herbert Xu | 1ecafed | 2007-10-09 13:24:07 -0700 | [diff] [blame] | 2406 | return x->type->reject(x, skb, fl); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | /* When skb is transformed back to its "native" form, we have to |
| 2410 | * check policy restrictions. At the moment we make this in maximally |
| 2411 | * stupid way. Shame on me. :-) Of course, connected sockets must |
| 2412 | * have policy cached at them. |
| 2413 | */ |
| 2414 | |
| 2415 | static inline int |
David S. Miller | 7db454b | 2011-02-24 01:43:01 -0500 | [diff] [blame] | 2416 | xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | unsigned short family) |
| 2418 | { |
| 2419 | if (xfrm_state_kern(x)) |
Kazunori MIYAZAWA | 928ba41 | 2007-02-13 12:57:16 -0800 | [diff] [blame] | 2420 | return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | return x->id.proto == tmpl->id.proto && |
| 2422 | (x->id.spi == tmpl->id.spi || !tmpl->id.spi) && |
| 2423 | (x->props.reqid == tmpl->reqid || !tmpl->reqid) && |
| 2424 | x->props.mode == tmpl->mode && |
Herbert Xu | c5d18e9 | 2008-04-22 00:46:42 -0700 | [diff] [blame] | 2425 | (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) || |
Masahide NAKAMURA | f3bd484 | 2006-08-23 18:00:48 -0700 | [diff] [blame] | 2426 | !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) && |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 2427 | !(x->props.mode != XFRM_MODE_TRANSPORT && |
| 2428 | xfrm_state_addr_cmp(tmpl, x, family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2431 | /* |
| 2432 | * 0 or more than 0 is returned when validation is succeeded (either bypass |
| 2433 | * because of optional transport mode, or next index of the mathced secpath |
| 2434 | * state with the template. |
| 2435 | * -1 is returned when no matching template is found. |
| 2436 | * Otherwise "-2 - errored_index" is returned. |
| 2437 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | static inline int |
David S. Miller | 22cccb7 | 2011-02-24 01:43:33 -0500 | [diff] [blame] | 2439 | xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | unsigned short family) |
| 2441 | { |
| 2442 | int idx = start; |
| 2443 | |
| 2444 | if (tmpl->optional) { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 2445 | if (tmpl->mode == XFRM_MODE_TRANSPORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | return start; |
| 2447 | } else |
| 2448 | start = -1; |
| 2449 | for (; idx < sp->len; idx++) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 2450 | if (xfrm_state_ok(tmpl, sp->xvec[idx], family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | return ++idx; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2452 | if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) { |
| 2453 | if (start == -1) |
| 2454 | start = -2-idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | break; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | } |
| 2458 | return start; |
| 2459 | } |
| 2460 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2461 | int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, |
| 2462 | unsigned int family, int reverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | { |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2464 | const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 2465 | const struct xfrm_if_cb *ifcb = xfrm_if_get_cb(); |
| 2466 | struct xfrm_if *xi; |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2467 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | |
| 2469 | if (unlikely(afinfo == NULL)) |
| 2470 | return -EAFNOSUPPORT; |
| 2471 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2472 | afinfo->decode_session(skb, fl, reverse); |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2473 | if (ifcb) { |
| 2474 | xi = ifcb->decode_session(skb); |
| 2475 | if (xi) |
| 2476 | fl->flowi_xfrm.if_id = xi->p.if_id; |
| 2477 | } |
| 2478 | |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 2479 | err = security_xfrm_decode_session(skb, &fl->flowi_secid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | xfrm_policy_put_afinfo(afinfo); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2481 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | } |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2483 | EXPORT_SYMBOL(__xfrm_decode_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | |
David S. Miller | 9a7386e | 2011-02-24 01:44:12 -0500 | [diff] [blame] | 2485 | static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | { |
| 2487 | for (; k < sp->len; k++) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2488 | if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2489 | *idxp = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | return 1; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | return 0; |
| 2495 | } |
| 2496 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 2497 | int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | unsigned short family) |
| 2499 | { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 2500 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | struct xfrm_policy *pol; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2502 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 2503 | int npols = 0; |
| 2504 | int xfrm_nr; |
| 2505 | int pi; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2506 | int reverse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | struct flowi fl; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2508 | u8 fl_dir; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2509 | int xerr_idx = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2511 | reverse = dir & ~XFRM_POLICY_MASK; |
| 2512 | dir &= XFRM_POLICY_MASK; |
| 2513 | fl_dir = policy_to_flow_dir(dir); |
| 2514 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2515 | if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2516 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 2520 | nf_nat_decode_session(skb, &fl, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
| 2522 | /* First, check used SA against their selectors. */ |
| 2523 | if (skb->sp) { |
| 2524 | int i; |
| 2525 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2526 | for (i = skb->sp->len-1; i >= 0; i--) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 2527 | struct xfrm_state *x = skb->sp->xvec[i]; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2528 | if (!xfrm_selector_match(&x->sel, &fl, family)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2529 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | pol = NULL; |
Eric Dumazet | bd5eb35 | 2015-12-07 08:53:17 -0800 | [diff] [blame] | 2536 | sk = sk_to_full_sk(sk); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2537 | if (sk && sk->sk_policy[dir]) { |
Steffen Klassert | 0b86564 | 2017-02-14 07:43:56 +0100 | [diff] [blame] | 2538 | pol = xfrm_sk_policy_lookup(sk, dir, &fl, family); |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2539 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2540 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2541 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2542 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 2545 | if (!pol) { |
| 2546 | struct flow_cache_object *flo; |
| 2547 | |
| 2548 | flo = flow_cache_lookup(net, &fl, family, fl_dir, |
| 2549 | xfrm_policy_lookup, NULL); |
| 2550 | if (IS_ERR_OR_NULL(flo)) |
| 2551 | pol = ERR_CAST(flo); |
| 2552 | else |
| 2553 | pol = container_of(flo, struct xfrm_policy, flo); |
| 2554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2556 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2557 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2558 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2559 | } |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2560 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2561 | if (!pol) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2562 | if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2563 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2564 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2565 | return 0; |
| 2566 | } |
| 2567 | return 1; |
| 2568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 2570 | pol->curlft.use_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2572 | pols[0] = pol; |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 2573 | npols++; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2574 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 2575 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 2576 | pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2577 | &fl, family, |
| 2578 | XFRM_POLICY_IN); |
| 2579 | if (pols[1]) { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2580 | if (IS_ERR(pols[1])) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2581 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2582 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2583 | } |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 2584 | pols[1]->curlft.use_time = get_seconds(); |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 2585 | npols++; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | #endif |
| 2589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | if (pol->action == XFRM_POLICY_ALLOW) { |
| 2591 | struct sec_path *sp; |
| 2592 | static struct sec_path dummy; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2593 | struct xfrm_tmpl *tp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2594 | struct xfrm_tmpl *stp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2595 | struct xfrm_tmpl **tpp = tp; |
| 2596 | int ti = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | int i, k; |
| 2598 | |
| 2599 | if ((sp = skb->sp) == NULL) |
| 2600 | sp = &dummy; |
| 2601 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2602 | for (pi = 0; pi < npols; pi++) { |
| 2603 | if (pols[pi] != pol && |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2604 | pols[pi]->action != XFRM_POLICY_ALLOW) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2605 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2606 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2607 | } |
| 2608 | if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2609 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2610 | goto reject_error; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2611 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2612 | for (i = 0; i < pols[pi]->xfrm_nr; i++) |
| 2613 | tpp[ti++] = &pols[pi]->xfrm_vec[i]; |
| 2614 | } |
| 2615 | xfrm_nr = ti; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2616 | if (npols > 1) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 2617 | xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net); |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2618 | tpp = stp; |
| 2619 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | /* For each tunnel xfrm, find the first matching tmpl. |
| 2622 | * For each tmpl before that, find corresponding xfrm. |
| 2623 | * Order is _important_. Later we will implement |
| 2624 | * some barriers, but at the moment barriers |
| 2625 | * are implied between each two transformations. |
| 2626 | */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2627 | for (i = xfrm_nr-1, k = 0; i >= 0; i--) { |
| 2628 | k = xfrm_policy_ok(tpp[i], sp, k, family); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2629 | if (k < 0) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2630 | if (k < -1) |
| 2631 | /* "-2 - errored_index" returned */ |
| 2632 | xerr_idx = -(2+k); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2633 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | goto reject; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2638 | if (secpath_has_nontransport(sp, k, &xerr_idx)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2639 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2643 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | return 1; |
| 2645 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2646 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | |
| 2648 | reject: |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2649 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2650 | reject_error: |
| 2651 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | return 0; |
| 2653 | } |
| 2654 | EXPORT_SYMBOL(__xfrm_policy_check); |
| 2655 | |
| 2656 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 2657 | { |
Alexey Dobriyan | 99a6665 | 2008-11-25 17:36:13 -0800 | [diff] [blame] | 2658 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | struct flowi fl; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2660 | struct dst_entry *dst; |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 2661 | int res = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2663 | if (xfrm_decode_session(skb, &fl, family) < 0) { |
jamal | 72032fd | 2010-02-18 03:35:07 +0000 | [diff] [blame] | 2664 | XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2666 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | |
Eric Dumazet | fafeeb6 | 2010-06-01 10:04:49 +0000 | [diff] [blame] | 2668 | skb_dst_force(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2669 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2670 | dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2671 | if (IS_ERR(dst)) { |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 2672 | res = 0; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2673 | dst = NULL; |
| 2674 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2675 | skb_dst_set(skb, dst); |
| 2676 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | } |
| 2678 | EXPORT_SYMBOL(__xfrm_route_forward); |
| 2679 | |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2680 | /* Optimize later using cookies and generation ids. */ |
| 2681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie) |
| 2683 | { |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2684 | /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 2685 | * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to |
| 2686 | * get validated by dst_ops->check on every use. We do this |
| 2687 | * because when a normal route referenced by an XFRM dst is |
| 2688 | * obsoleted we do not go looking around for all parent |
| 2689 | * referencing XFRM dsts so that we can invalidate them. It |
| 2690 | * is just too much work. Instead we make the checks here on |
| 2691 | * every use. For example: |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2692 | * |
| 2693 | * XFRM dst A --> IPv4 dst X |
| 2694 | * |
| 2695 | * X is the "xdst->route" of A (X is also the "dst->path" of A |
| 2696 | * in this example). If X is marked obsolete, "A" will not |
| 2697 | * notice. That's what we are validating here via the |
| 2698 | * stale_bundle() check. |
| 2699 | * |
| 2700 | * When a policy's bundle is pruned, we dst_free() the XFRM |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 2701 | * dst which causes it's ->obsolete field to be set to |
| 2702 | * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like |
| 2703 | * this, we want to force a new route lookup. |
David S. Miller | 399c180 | 2005-12-19 14:23:23 -0800 | [diff] [blame] | 2704 | */ |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2705 | if (dst->obsolete < 0 && !stale_bundle(dst)) |
| 2706 | return dst; |
| 2707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | return NULL; |
| 2709 | } |
| 2710 | |
| 2711 | static int stale_bundle(struct dst_entry *dst) |
| 2712 | { |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 2713 | return !xfrm_bundle_ok((struct xfrm_dst *)dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 2716 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | while ((dst = dst->child) && dst->xfrm && dst->dev == dev) { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2719 | dst->dev = dev_net(dev)->loopback_dev; |
Daniel Lezcano | de3cb74 | 2007-09-25 19:16:28 -0700 | [diff] [blame] | 2720 | dev_hold(dst->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | dev_put(dev); |
| 2722 | } |
| 2723 | } |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 2724 | EXPORT_SYMBOL(xfrm_dst_ifdown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
| 2726 | static void xfrm_link_failure(struct sk_buff *skb) |
| 2727 | { |
| 2728 | /* Impossible. Such dst must be popped before reaches point of failure. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst) |
| 2732 | { |
| 2733 | if (dst) { |
| 2734 | if (dst->obsolete) { |
| 2735 | dst_release(dst); |
| 2736 | dst = NULL; |
| 2737 | } |
| 2738 | } |
| 2739 | return dst; |
| 2740 | } |
| 2741 | |
Paul Moore | e4c1721 | 2013-05-29 07:36:25 +0000 | [diff] [blame] | 2742 | void xfrm_garbage_collect(struct net *net) |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2743 | { |
Fan Du | ca925cf | 2014-01-18 09:55:27 +0800 | [diff] [blame] | 2744 | flow_cache_flush(net); |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2745 | } |
Paul Moore | e4c1721 | 2013-05-29 07:36:25 +0000 | [diff] [blame] | 2746 | EXPORT_SYMBOL(xfrm_garbage_collect); |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2747 | |
| 2748 | static void xfrm_garbage_collect_deferred(struct net *net) |
| 2749 | { |
Fan Du | ca925cf | 2014-01-18 09:55:27 +0800 | [diff] [blame] | 2750 | flow_cache_flush_deferred(net); |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2751 | } |
| 2752 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2753 | static void xfrm_init_pmtu(struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | { |
| 2755 | do { |
| 2756 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 2757 | u32 pmtu, route_mtu_cached; |
| 2758 | |
| 2759 | pmtu = dst_mtu(dst->child); |
| 2760 | xdst->child_mtu_cached = pmtu; |
| 2761 | |
| 2762 | pmtu = xfrm_state_mtu(dst->xfrm, pmtu); |
| 2763 | |
| 2764 | route_mtu_cached = dst_mtu(xdst->route); |
| 2765 | xdst->route_mtu_cached = route_mtu_cached; |
| 2766 | |
| 2767 | if (pmtu > route_mtu_cached) |
| 2768 | pmtu = route_mtu_cached; |
| 2769 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2770 | dst_metric_set(dst, RTAX_MTU, pmtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | } while ((dst = dst->next)); |
| 2772 | } |
| 2773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | /* Check that the bundle accepts the flow and its components are |
| 2775 | * still valid. |
| 2776 | */ |
| 2777 | |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 2778 | static int xfrm_bundle_ok(struct xfrm_dst *first) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | { |
| 2780 | struct dst_entry *dst = &first->u.dst; |
| 2781 | struct xfrm_dst *last; |
| 2782 | u32 mtu; |
| 2783 | |
Hideaki YOSHIFUJI | 92d63dec | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 2784 | if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | (dst->dev && !netif_running(dst->dev))) |
| 2786 | return 0; |
| 2787 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2788 | if (dst->flags & DST_XFRM_QUEUE) |
| 2789 | return 1; |
| 2790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | last = NULL; |
| 2792 | |
| 2793 | do { |
| 2794 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 2795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | if (dst->xfrm->km.state != XFRM_STATE_VALID) |
| 2797 | return 0; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2798 | if (xdst->xfrm_genid != dst->xfrm->genid) |
| 2799 | return 0; |
Timo Teräs | b1312c8 | 2010-06-24 14:35:00 -0700 | [diff] [blame] | 2800 | if (xdst->num_pols > 0 && |
| 2801 | xdst->policy_genid != atomic_read(&xdst->pols[0]->genid)) |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 2802 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
| 2804 | mtu = dst_mtu(dst->child); |
| 2805 | if (xdst->child_mtu_cached != mtu) { |
| 2806 | last = xdst; |
| 2807 | xdst->child_mtu_cached = mtu; |
| 2808 | } |
| 2809 | |
Hideaki YOSHIFUJI | 92d63dec | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 2810 | if (!dst_check(xdst->route, xdst->route_cookie)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | return 0; |
| 2812 | mtu = dst_mtu(xdst->route); |
| 2813 | if (xdst->route_mtu_cached != mtu) { |
| 2814 | last = xdst; |
| 2815 | xdst->route_mtu_cached = mtu; |
| 2816 | } |
| 2817 | |
| 2818 | dst = dst->child; |
| 2819 | } while (dst->xfrm); |
| 2820 | |
| 2821 | if (likely(!last)) |
| 2822 | return 1; |
| 2823 | |
| 2824 | mtu = last->child_mtu_cached; |
| 2825 | for (;;) { |
| 2826 | dst = &last->u.dst; |
| 2827 | |
| 2828 | mtu = xfrm_state_mtu(dst->xfrm, mtu); |
| 2829 | if (mtu > last->route_mtu_cached) |
| 2830 | mtu = last->route_mtu_cached; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2831 | dst_metric_set(dst, RTAX_MTU, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | if (last == first) |
| 2834 | break; |
| 2835 | |
Patrick McHardy | bd0bf07 | 2007-07-18 01:55:52 -0700 | [diff] [blame] | 2836 | last = (struct xfrm_dst *)last->u.dst.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | last->child_mtu_cached = mtu; |
| 2838 | } |
| 2839 | |
| 2840 | return 1; |
| 2841 | } |
| 2842 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2843 | static unsigned int xfrm_default_advmss(const struct dst_entry *dst) |
| 2844 | { |
| 2845 | return dst_metric_advmss(dst->path); |
| 2846 | } |
| 2847 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2848 | static unsigned int xfrm_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2849 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2850 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2851 | |
| 2852 | return mtu ? : dst_mtu(dst->path); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2853 | } |
| 2854 | |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 2855 | static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst, |
| 2856 | struct sk_buff *skb, |
| 2857 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2858 | { |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 2859 | return dst->path->ops->neigh_lookup(dst, skb, daddr); |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2863 | { |
| 2864 | int err = 0; |
| 2865 | if (unlikely(afinfo == NULL)) |
| 2866 | return -EINVAL; |
| 2867 | if (unlikely(afinfo->family >= NPROTO)) |
| 2868 | return -EAFNOSUPPORT; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2869 | spin_lock(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL)) |
Li RongQing | f31e8d4f | 2015-04-23 11:06:53 +0800 | [diff] [blame] | 2871 | err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | else { |
| 2873 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 2874 | if (likely(dst_ops->kmem_cachep == NULL)) |
| 2875 | dst_ops->kmem_cachep = xfrm_dst_cache; |
| 2876 | if (likely(dst_ops->check == NULL)) |
| 2877 | dst_ops->check = xfrm_dst_check; |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2878 | if (likely(dst_ops->default_advmss == NULL)) |
| 2879 | dst_ops->default_advmss = xfrm_default_advmss; |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2880 | if (likely(dst_ops->mtu == NULL)) |
| 2881 | dst_ops->mtu = xfrm_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | if (likely(dst_ops->negative_advice == NULL)) |
| 2883 | dst_ops->negative_advice = xfrm_negative_advice; |
| 2884 | if (likely(dst_ops->link_failure == NULL)) |
| 2885 | dst_ops->link_failure = xfrm_link_failure; |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2886 | if (likely(dst_ops->neigh_lookup == NULL)) |
| 2887 | dst_ops->neigh_lookup = xfrm_neigh_lookup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | if (likely(afinfo->garbage_collect == NULL)) |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2889 | afinfo->garbage_collect = xfrm_garbage_collect_deferred; |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 2890 | rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | } |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2892 | spin_unlock(&xfrm_policy_afinfo_lock); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | return err; |
| 2895 | } |
| 2896 | EXPORT_SYMBOL(xfrm_policy_register_afinfo); |
| 2897 | |
| 2898 | int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2899 | { |
| 2900 | int err = 0; |
| 2901 | if (unlikely(afinfo == NULL)) |
| 2902 | return -EINVAL; |
| 2903 | if (unlikely(afinfo->family >= NPROTO)) |
| 2904 | return -EAFNOSUPPORT; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2905 | spin_lock(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) { |
| 2907 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo)) |
| 2908 | err = -EINVAL; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2909 | else |
| 2910 | RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family], |
| 2911 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | } |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2913 | spin_unlock(&xfrm_policy_afinfo_lock); |
| 2914 | if (!err) { |
| 2915 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 2916 | |
| 2917 | synchronize_rcu(); |
| 2918 | |
| 2919 | dst_ops->kmem_cachep = NULL; |
| 2920 | dst_ops->check = NULL; |
| 2921 | dst_ops->negative_advice = NULL; |
| 2922 | dst_ops->link_failure = NULL; |
| 2923 | afinfo->garbage_collect = NULL; |
| 2924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | return err; |
| 2926 | } |
| 2927 | EXPORT_SYMBOL(xfrm_policy_unregister_afinfo); |
| 2928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 2930 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 2931 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 2932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | switch (event) { |
| 2934 | case NETDEV_DOWN: |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2935 | xfrm_garbage_collect(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | } |
| 2937 | return NOTIFY_DONE; |
| 2938 | } |
| 2939 | |
| 2940 | static struct notifier_block xfrm_dev_notifier = { |
Alexey Dobriyan | d5917a3 | 2008-10-31 00:41:59 -0700 | [diff] [blame] | 2941 | .notifier_call = xfrm_dev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | }; |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2943 | void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb) |
| 2944 | { |
| 2945 | spin_lock(&xfrm_if_cb_lock); |
| 2946 | rcu_assign_pointer(xfrm_if_cb, ifcb); |
| 2947 | spin_unlock(&xfrm_if_cb_lock); |
| 2948 | } |
| 2949 | EXPORT_SYMBOL(xfrm_if_register_cb); |
| 2950 | |
| 2951 | void xfrm_if_unregister_cb(void) |
| 2952 | { |
| 2953 | RCU_INIT_POINTER(xfrm_if_cb, NULL); |
| 2954 | synchronize_rcu(); |
| 2955 | } |
| 2956 | EXPORT_SYMBOL(xfrm_if_unregister_cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2958 | #ifdef CONFIG_XFRM_STATISTICS |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2959 | static int __net_init xfrm_statistics_init(struct net *net) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2960 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2961 | int rv; |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 2962 | net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib); |
| 2963 | if (!net->mib.xfrm_statistics) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2964 | return -ENOMEM; |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2965 | rv = xfrm_proc_init(net); |
| 2966 | if (rv < 0) |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 2967 | free_percpu(net->mib.xfrm_statistics); |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2968 | return rv; |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2969 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2970 | |
| 2971 | static void xfrm_statistics_fini(struct net *net) |
| 2972 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2973 | xfrm_proc_fini(net); |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 2974 | free_percpu(net->mib.xfrm_statistics); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2975 | } |
| 2976 | #else |
| 2977 | static int __net_init xfrm_statistics_init(struct net *net) |
| 2978 | { |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | static void xfrm_statistics_fini(struct net *net) |
| 2983 | { |
| 2984 | } |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2985 | #endif |
| 2986 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2987 | static int __net_init xfrm_policy_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2989 | unsigned int hmask, sz; |
| 2990 | int dir; |
| 2991 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2992 | if (net_eq(net, &init_net)) |
| 2993 | xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | sizeof(struct xfrm_dst), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2995 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2996 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2998 | hmask = 8 - 1; |
| 2999 | sz = (hmask+1) * sizeof(struct hlist_head); |
| 3000 | |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3001 | net->xfrm.policy_byidx = xfrm_hash_alloc(sz); |
| 3002 | if (!net->xfrm.policy_byidx) |
| 3003 | goto out_byidx; |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 3004 | net->xfrm.policy_idx_hmask = hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3005 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 3006 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3007 | struct xfrm_policy_hash *htab; |
| 3008 | |
Alexey Dobriyan | dc2caba | 2008-11-25 17:24:15 -0800 | [diff] [blame] | 3009 | net->xfrm.policy_count[dir] = 0; |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 3010 | net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 3011 | INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3012 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3013 | htab = &net->xfrm.policy_bydst[dir]; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 3014 | htab->table = xfrm_hash_alloc(sz); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3015 | if (!htab->table) |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3016 | goto out_bydst; |
| 3017 | htab->hmask = hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 3018 | htab->dbits4 = 32; |
| 3019 | htab->sbits4 = 32; |
| 3020 | htab->dbits6 = 128; |
| 3021 | htab->sbits6 = 128; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3022 | } |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 3023 | net->xfrm.policy_hthresh.lbits4 = 32; |
| 3024 | net->xfrm.policy_hthresh.rbits4 = 32; |
| 3025 | net->xfrm.policy_hthresh.lbits6 = 128; |
| 3026 | net->xfrm.policy_hthresh.rbits6 = 128; |
| 3027 | |
| 3028 | seqlock_init(&net->xfrm.policy_hthresh.lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3029 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 3030 | INIT_LIST_HEAD(&net->xfrm.policy_all); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 3031 | INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 3032 | INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3033 | if (net_eq(net, &init_net)) |
| 3034 | register_netdevice_notifier(&xfrm_dev_notifier); |
| 3035 | return 0; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3036 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3037 | out_bydst: |
| 3038 | for (dir--; dir >= 0; dir--) { |
| 3039 | struct xfrm_policy_hash *htab; |
| 3040 | |
| 3041 | htab = &net->xfrm.policy_bydst[dir]; |
| 3042 | xfrm_hash_free(htab->table, sz); |
| 3043 | } |
| 3044 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3045 | out_byidx: |
| 3046 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3049 | static void xfrm_policy_fini(struct net *net) |
| 3050 | { |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3051 | unsigned int sz; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 3052 | int dir; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3053 | |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 3054 | flush_work(&net->xfrm.policy_hash_work); |
| 3055 | #ifdef CONFIG_XFRM_SUB_POLICY |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3056 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 3057 | #endif |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3058 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 3059 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 3060 | WARN_ON(!list_empty(&net->xfrm.policy_all)); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3061 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 3062 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3063 | struct xfrm_policy_hash *htab; |
| 3064 | |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 3065 | WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir])); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3066 | |
| 3067 | htab = &net->xfrm.policy_bydst[dir]; |
Michal Kubecek | 5b653b2 | 2013-01-18 16:03:48 +0100 | [diff] [blame] | 3068 | sz = (htab->hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 3069 | WARN_ON(!hlist_empty(htab->table)); |
| 3070 | xfrm_hash_free(htab->table, sz); |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 3071 | } |
| 3072 | |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 3073 | sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 3074 | WARN_ON(!hlist_empty(net->xfrm.policy_byidx)); |
| 3075 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | static int __net_init xfrm_net_init(struct net *net) |
| 3079 | { |
| 3080 | int rv; |
| 3081 | |
Florian Westphal | f68a09c | 2017-02-08 11:52:29 +0100 | [diff] [blame] | 3082 | /* Initialize the per-net locks here */ |
| 3083 | spin_lock_init(&net->xfrm.xfrm_state_lock); |
| 3084 | spin_lock_init(&net->xfrm.xfrm_policy_lock); |
| 3085 | mutex_init(&net->xfrm.xfrm_cfg_mutex); |
| 3086 | |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3087 | rv = xfrm_statistics_init(net); |
| 3088 | if (rv < 0) |
| 3089 | goto out_statistics; |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3090 | rv = xfrm_state_init(net); |
| 3091 | if (rv < 0) |
| 3092 | goto out_state; |
| 3093 | rv = xfrm_policy_init(net); |
| 3094 | if (rv < 0) |
| 3095 | goto out_policy; |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 3096 | rv = xfrm_sysctl_init(net); |
| 3097 | if (rv < 0) |
| 3098 | goto out_sysctl; |
Steffen Klassert | 4a93f50 | 2014-03-12 09:43:17 +0100 | [diff] [blame] | 3099 | rv = flow_cache_init(net); |
| 3100 | if (rv < 0) |
| 3101 | goto out; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 3102 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3103 | return 0; |
| 3104 | |
Steffen Klassert | 4a93f50 | 2014-03-12 09:43:17 +0100 | [diff] [blame] | 3105 | out: |
| 3106 | xfrm_sysctl_fini(net); |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 3107 | out_sysctl: |
| 3108 | xfrm_policy_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3109 | out_policy: |
| 3110 | xfrm_state_fini(net); |
| 3111 | out_state: |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3112 | xfrm_statistics_fini(net); |
| 3113 | out_statistics: |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3114 | return rv; |
| 3115 | } |
| 3116 | |
| 3117 | static void __net_exit xfrm_net_exit(struct net *net) |
| 3118 | { |
Steffen Klassert | 4a93f50 | 2014-03-12 09:43:17 +0100 | [diff] [blame] | 3119 | flow_cache_fini(net); |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 3120 | xfrm_sysctl_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3121 | xfrm_policy_fini(net); |
| 3122 | xfrm_state_fini(net); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3123 | xfrm_statistics_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | static struct pernet_operations __net_initdata xfrm_net_ops = { |
| 3127 | .init = xfrm_net_init, |
| 3128 | .exit = xfrm_net_exit, |
| 3129 | }; |
| 3130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | void __init xfrm_init(void) |
| 3132 | { |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 3133 | register_pernet_subsys(&xfrm_net_ops); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 3134 | seqcount_init(&xfrm_policy_hash_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | xfrm_input_init(); |
Steffen Klassert | f5bb9e9 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 3136 | |
| 3137 | RCU_INIT_POINTER(xfrm_if_cb, NULL); |
| 3138 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3141 | #ifdef CONFIG_AUDITSYSCALL |
Ilpo Järvinen | 1486cbd7 | 2008-01-12 03:20:03 -0800 | [diff] [blame] | 3142 | static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp, |
| 3143 | struct audit_buffer *audit_buf) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3144 | { |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3145 | struct xfrm_sec_ctx *ctx = xp->security; |
| 3146 | struct xfrm_selector *sel = &xp->selector; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3147 | |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3148 | if (ctx) |
| 3149 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", |
| 3150 | ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); |
| 3151 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 3152 | switch (sel->family) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3153 | case AF_INET: |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 3154 | audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3155 | if (sel->prefixlen_s != 32) |
| 3156 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 3157 | sel->prefixlen_s); |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 3158 | audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3159 | if (sel->prefixlen_d != 32) |
| 3160 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 3161 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3162 | break; |
| 3163 | case AF_INET6: |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 3164 | audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3165 | if (sel->prefixlen_s != 128) |
| 3166 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 3167 | sel->prefixlen_s); |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 3168 | audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 3169 | if (sel->prefixlen_d != 128) |
| 3170 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 3171 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3172 | break; |
| 3173 | } |
| 3174 | } |
| 3175 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3176 | void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3177 | { |
| 3178 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3179 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3180 | audit_buf = xfrm_audit_start("SPD-add"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3181 | if (audit_buf == NULL) |
| 3182 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3183 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3184 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3185 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 3186 | audit_log_end(audit_buf); |
| 3187 | } |
| 3188 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_add); |
| 3189 | |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 3190 | void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3191 | bool task_valid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3192 | { |
| 3193 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3194 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3195 | audit_buf = xfrm_audit_start("SPD-delete"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3196 | if (audit_buf == NULL) |
| 3197 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 3198 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3199 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3200 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 3201 | audit_log_end(audit_buf); |
| 3202 | } |
| 3203 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete); |
| 3204 | #endif |
| 3205 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3206 | #ifdef CONFIG_XFRM_MIGRATE |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3207 | static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp, |
| 3208 | const struct xfrm_selector *sel_tgt) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3209 | { |
| 3210 | if (sel_cmp->proto == IPSEC_ULPROTO_ANY) { |
| 3211 | if (sel_tgt->family == sel_cmp->family && |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3212 | xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr, |
| 3213 | sel_cmp->family) && |
| 3214 | xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr, |
| 3215 | sel_cmp->family) && |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3216 | sel_tgt->prefixlen_d == sel_cmp->prefixlen_d && |
| 3217 | sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3218 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3219 | } |
| 3220 | } else { |
| 3221 | if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3222 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3223 | } |
| 3224 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3225 | return false; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3226 | } |
| 3227 | |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 3228 | static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel, |
| 3229 | u8 dir, u8 type, struct net *net) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3230 | { |
| 3231 | struct xfrm_policy *pol, *ret = NULL; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3232 | struct hlist_head *chain; |
| 3233 | u32 priority = ~0U; |
| 3234 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 3235 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 3236 | chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3237 | hlist_for_each_entry(pol, chain, bydst) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3238 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
| 3239 | pol->type == type) { |
| 3240 | ret = pol; |
| 3241 | priority = ret->priority; |
| 3242 | break; |
| 3243 | } |
| 3244 | } |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 3245 | chain = &net->xfrm.policy_inexact[dir]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3246 | hlist_for_each_entry(pol, chain, bydst) { |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 3247 | if ((pol->priority >= priority) && ret) |
| 3248 | break; |
| 3249 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3250 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 3251 | pol->type == type) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3252 | ret = pol; |
| 3253 | break; |
| 3254 | } |
| 3255 | } |
| 3256 | |
Li RongQing | 586f2eb | 2015-04-30 17:13:41 +0800 | [diff] [blame] | 3257 | xfrm_pol_hold(ret); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3258 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 3259 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3260 | |
| 3261 | return ret; |
| 3262 | } |
| 3263 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 3264 | static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3265 | { |
| 3266 | int match = 0; |
| 3267 | |
| 3268 | if (t->mode == m->mode && t->id.proto == m->proto && |
| 3269 | (m->reqid == 0 || t->reqid == m->reqid)) { |
| 3270 | switch (t->mode) { |
| 3271 | case XFRM_MODE_TUNNEL: |
| 3272 | case XFRM_MODE_BEET: |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3273 | if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr, |
| 3274 | m->old_family) && |
| 3275 | xfrm_addr_equal(&t->saddr, &m->old_saddr, |
| 3276 | m->old_family)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3277 | match = 1; |
| 3278 | } |
| 3279 | break; |
| 3280 | case XFRM_MODE_TRANSPORT: |
| 3281 | /* in case of transport mode, template does not store |
| 3282 | any IP addresses, hence we just compare mode and |
| 3283 | protocol */ |
| 3284 | match = 1; |
| 3285 | break; |
| 3286 | default: |
| 3287 | break; |
| 3288 | } |
| 3289 | } |
| 3290 | return match; |
| 3291 | } |
| 3292 | |
| 3293 | /* update endpoint address(es) of template(s) */ |
| 3294 | static int xfrm_policy_migrate(struct xfrm_policy *pol, |
| 3295 | struct xfrm_migrate *m, int num_migrate) |
| 3296 | { |
| 3297 | struct xfrm_migrate *mp; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3298 | int i, j, n = 0; |
| 3299 | |
| 3300 | write_lock_bh(&pol->lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 3301 | if (unlikely(pol->walk.dead)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3302 | /* target policy has been deleted */ |
| 3303 | write_unlock_bh(&pol->lock); |
| 3304 | return -ENOENT; |
| 3305 | } |
| 3306 | |
| 3307 | for (i = 0; i < pol->xfrm_nr; i++) { |
| 3308 | for (j = 0, mp = m; j < num_migrate; j++, mp++) { |
| 3309 | if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i])) |
| 3310 | continue; |
| 3311 | n++; |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 3312 | if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL && |
| 3313 | pol->xfrm_vec[i].mode != XFRM_MODE_BEET) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3314 | continue; |
| 3315 | /* update endpoints */ |
| 3316 | memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr, |
| 3317 | sizeof(pol->xfrm_vec[i].id.daddr)); |
| 3318 | memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr, |
| 3319 | sizeof(pol->xfrm_vec[i].saddr)); |
| 3320 | pol->xfrm_vec[i].encap_family = mp->new_family; |
| 3321 | /* flush bundles */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3322 | atomic_inc(&pol->genid); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | write_unlock_bh(&pol->lock); |
| 3327 | |
| 3328 | if (!n) |
| 3329 | return -ENODATA; |
| 3330 | |
| 3331 | return 0; |
| 3332 | } |
| 3333 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 3334 | static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3335 | { |
| 3336 | int i, j; |
| 3337 | |
| 3338 | if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH) |
| 3339 | return -EINVAL; |
| 3340 | |
| 3341 | for (i = 0; i < num_migrate; i++) { |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3342 | if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr, |
| 3343 | m[i].old_family) && |
| 3344 | xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr, |
| 3345 | m[i].old_family)) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3346 | return -EINVAL; |
| 3347 | if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) || |
| 3348 | xfrm_addr_any(&m[i].new_saddr, m[i].new_family)) |
| 3349 | return -EINVAL; |
| 3350 | |
| 3351 | /* check if there is any duplicated entry */ |
| 3352 | for (j = i + 1; j < num_migrate; j++) { |
| 3353 | if (!memcmp(&m[i].old_daddr, &m[j].old_daddr, |
| 3354 | sizeof(m[i].old_daddr)) && |
| 3355 | !memcmp(&m[i].old_saddr, &m[j].old_saddr, |
| 3356 | sizeof(m[i].old_saddr)) && |
| 3357 | m[i].proto == m[j].proto && |
| 3358 | m[i].mode == m[j].mode && |
| 3359 | m[i].reqid == m[j].reqid && |
| 3360 | m[i].old_family == m[j].old_family) |
| 3361 | return -EINVAL; |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | return 0; |
| 3366 | } |
| 3367 | |
David S. Miller | b4b7c0b | 2011-02-24 00:35:06 -0500 | [diff] [blame] | 3368 | int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 3369 | struct xfrm_migrate *m, int num_migrate, |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 3370 | struct xfrm_kmaddress *k, struct net *net) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3371 | { |
| 3372 | int i, err, nx_cur = 0, nx_new = 0; |
| 3373 | struct xfrm_policy *pol = NULL; |
| 3374 | struct xfrm_state *x, *xc; |
| 3375 | struct xfrm_state *x_cur[XFRM_MAX_DEPTH]; |
| 3376 | struct xfrm_state *x_new[XFRM_MAX_DEPTH]; |
| 3377 | struct xfrm_migrate *mp; |
| 3378 | |
Vladis Dronov | 12a70cc | 2017-08-02 19:50:14 +0200 | [diff] [blame] | 3379 | /* Stage 0 - sanity checks */ |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3380 | if ((err = xfrm_migrate_check(m, num_migrate)) < 0) |
| 3381 | goto out; |
| 3382 | |
Vladis Dronov | 12a70cc | 2017-08-02 19:50:14 +0200 | [diff] [blame] | 3383 | if (dir >= XFRM_POLICY_MAX) { |
| 3384 | err = -EINVAL; |
| 3385 | goto out; |
| 3386 | } |
| 3387 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3388 | /* Stage 1 - find policy */ |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 3389 | if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3390 | err = -ENOENT; |
| 3391 | goto out; |
| 3392 | } |
| 3393 | |
| 3394 | /* Stage 2 - find and update state(s) */ |
| 3395 | for (i = 0, mp = m; i < num_migrate; i++, mp++) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 3396 | if ((x = xfrm_migrate_state_find(mp, net))) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3397 | x_cur[nx_cur] = x; |
| 3398 | nx_cur++; |
| 3399 | if ((xc = xfrm_state_migrate(x, mp))) { |
| 3400 | x_new[nx_new] = xc; |
| 3401 | nx_new++; |
| 3402 | } else { |
| 3403 | err = -ENODATA; |
| 3404 | goto restore_state; |
| 3405 | } |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | /* Stage 3 - update policy */ |
| 3410 | if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0) |
| 3411 | goto restore_state; |
| 3412 | |
| 3413 | /* Stage 4 - delete old state(s) */ |
| 3414 | if (nx_cur) { |
| 3415 | xfrm_states_put(x_cur, nx_cur); |
| 3416 | xfrm_states_delete(x_cur, nx_cur); |
| 3417 | } |
| 3418 | |
| 3419 | /* Stage 5 - announce */ |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 3420 | km_migrate(sel, dir, type, m, num_migrate, k); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3421 | |
| 3422 | xfrm_pol_put(pol); |
| 3423 | |
| 3424 | return 0; |
| 3425 | out: |
| 3426 | return err; |
| 3427 | |
| 3428 | restore_state: |
| 3429 | if (pol) |
| 3430 | xfrm_pol_put(pol); |
| 3431 | if (nx_cur) |
| 3432 | xfrm_states_put(x_cur, nx_cur); |
| 3433 | if (nx_new) |
| 3434 | xfrm_states_delete(x_new, nx_new); |
| 3435 | |
| 3436 | return err; |
| 3437 | } |
David S. Miller | e610e67 | 2007-02-08 13:29:15 -0800 | [diff] [blame] | 3438 | EXPORT_SYMBOL(xfrm_migrate); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3439 | #endif |