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