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