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