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