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