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