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