Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/kmod.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/notifier.h> |
| 22 | #include <linux/netdevice.h> |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 23 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 25 | #include <linux/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <net/xfrm.h> |
| 27 | #include <net/ip.h> |
| 28 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 29 | #include "xfrm_hash.h" |
| 30 | |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 31 | DEFINE_MUTEX(xfrm_cfg_mutex); |
| 32 | EXPORT_SYMBOL(xfrm_cfg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static DEFINE_RWLOCK(xfrm_policy_lock); |
| 35 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 36 | unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2]; |
| 37 | EXPORT_SYMBOL(xfrm_policy_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | static DEFINE_RWLOCK(xfrm_policy_afinfo_lock); |
| 40 | static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO]; |
| 41 | |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 42 | static kmem_cache_t *xfrm_dst_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | static struct work_struct xfrm_policy_gc_work; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 45 | static HLIST_HEAD(xfrm_policy_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static DEFINE_SPINLOCK(xfrm_policy_gc_lock); |
| 47 | |
| 48 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family); |
| 49 | static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo); |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 50 | static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family); |
| 51 | static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | int xfrm_register_type(struct xfrm_type *type, unsigned short family) |
| 54 | { |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 55 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family); |
| 56 | struct xfrm_type **typemap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int err = 0; |
| 58 | |
| 59 | if (unlikely(afinfo == NULL)) |
| 60 | return -EAFNOSUPPORT; |
| 61 | typemap = afinfo->type_map; |
| 62 | |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 63 | if (likely(typemap[type->proto] == NULL)) |
| 64 | typemap[type->proto] = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | else |
| 66 | err = -EEXIST; |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 67 | xfrm_policy_unlock_afinfo(afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return err; |
| 69 | } |
| 70 | EXPORT_SYMBOL(xfrm_register_type); |
| 71 | |
| 72 | int xfrm_unregister_type(struct xfrm_type *type, unsigned short family) |
| 73 | { |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 74 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family); |
| 75 | struct xfrm_type **typemap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | int err = 0; |
| 77 | |
| 78 | if (unlikely(afinfo == NULL)) |
| 79 | return -EAFNOSUPPORT; |
| 80 | typemap = afinfo->type_map; |
| 81 | |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 82 | if (unlikely(typemap[type->proto] != type)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | err = -ENOENT; |
| 84 | else |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 85 | typemap[type->proto] = NULL; |
| 86 | xfrm_policy_unlock_afinfo(afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return err; |
| 88 | } |
| 89 | EXPORT_SYMBOL(xfrm_unregister_type); |
| 90 | |
| 91 | struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) |
| 92 | { |
| 93 | struct xfrm_policy_afinfo *afinfo; |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 94 | struct xfrm_type **typemap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct xfrm_type *type; |
| 96 | int modload_attempted = 0; |
| 97 | |
| 98 | retry: |
| 99 | afinfo = xfrm_policy_get_afinfo(family); |
| 100 | if (unlikely(afinfo == NULL)) |
| 101 | return NULL; |
| 102 | typemap = afinfo->type_map; |
| 103 | |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 104 | type = typemap[proto]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | if (unlikely(type && !try_module_get(type->owner))) |
| 106 | type = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (!type && !modload_attempted) { |
| 108 | xfrm_policy_put_afinfo(afinfo); |
| 109 | request_module("xfrm-type-%d-%d", |
| 110 | (int) family, (int) proto); |
| 111 | modload_attempted = 1; |
| 112 | goto retry; |
| 113 | } |
| 114 | |
| 115 | xfrm_policy_put_afinfo(afinfo); |
| 116 | return type; |
| 117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, |
| 120 | unsigned short family) |
| 121 | { |
| 122 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 123 | int err = 0; |
| 124 | |
| 125 | if (unlikely(afinfo == NULL)) |
| 126 | return -EAFNOSUPPORT; |
| 127 | |
| 128 | if (likely(afinfo->dst_lookup != NULL)) |
| 129 | err = afinfo->dst_lookup(dst, fl); |
| 130 | else |
| 131 | err = -EINVAL; |
| 132 | xfrm_policy_put_afinfo(afinfo); |
| 133 | return err; |
| 134 | } |
| 135 | EXPORT_SYMBOL(xfrm_dst_lookup); |
| 136 | |
| 137 | void xfrm_put_type(struct xfrm_type *type) |
| 138 | { |
| 139 | module_put(type->owner); |
| 140 | } |
| 141 | |
Herbert Xu | b59f45d | 2006-05-27 23:05:54 -0700 | [diff] [blame] | 142 | int xfrm_register_mode(struct xfrm_mode *mode, int family) |
| 143 | { |
| 144 | struct xfrm_policy_afinfo *afinfo; |
| 145 | struct xfrm_mode **modemap; |
| 146 | int err; |
| 147 | |
| 148 | if (unlikely(mode->encap >= XFRM_MODE_MAX)) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | afinfo = xfrm_policy_lock_afinfo(family); |
| 152 | if (unlikely(afinfo == NULL)) |
| 153 | return -EAFNOSUPPORT; |
| 154 | |
| 155 | err = -EEXIST; |
| 156 | modemap = afinfo->mode_map; |
| 157 | if (likely(modemap[mode->encap] == NULL)) { |
| 158 | modemap[mode->encap] = mode; |
| 159 | err = 0; |
| 160 | } |
| 161 | |
| 162 | xfrm_policy_unlock_afinfo(afinfo); |
| 163 | return err; |
| 164 | } |
| 165 | EXPORT_SYMBOL(xfrm_register_mode); |
| 166 | |
| 167 | int xfrm_unregister_mode(struct xfrm_mode *mode, int family) |
| 168 | { |
| 169 | struct xfrm_policy_afinfo *afinfo; |
| 170 | struct xfrm_mode **modemap; |
| 171 | int err; |
| 172 | |
| 173 | if (unlikely(mode->encap >= XFRM_MODE_MAX)) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | afinfo = xfrm_policy_lock_afinfo(family); |
| 177 | if (unlikely(afinfo == NULL)) |
| 178 | return -EAFNOSUPPORT; |
| 179 | |
| 180 | err = -ENOENT; |
| 181 | modemap = afinfo->mode_map; |
| 182 | if (likely(modemap[mode->encap] == mode)) { |
| 183 | modemap[mode->encap] = NULL; |
| 184 | err = 0; |
| 185 | } |
| 186 | |
| 187 | xfrm_policy_unlock_afinfo(afinfo); |
| 188 | return err; |
| 189 | } |
| 190 | EXPORT_SYMBOL(xfrm_unregister_mode); |
| 191 | |
| 192 | struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) |
| 193 | { |
| 194 | struct xfrm_policy_afinfo *afinfo; |
| 195 | struct xfrm_mode *mode; |
| 196 | int modload_attempted = 0; |
| 197 | |
| 198 | if (unlikely(encap >= XFRM_MODE_MAX)) |
| 199 | return NULL; |
| 200 | |
| 201 | retry: |
| 202 | afinfo = xfrm_policy_get_afinfo(family); |
| 203 | if (unlikely(afinfo == NULL)) |
| 204 | return NULL; |
| 205 | |
| 206 | mode = afinfo->mode_map[encap]; |
| 207 | if (unlikely(mode && !try_module_get(mode->owner))) |
| 208 | mode = NULL; |
| 209 | if (!mode && !modload_attempted) { |
| 210 | xfrm_policy_put_afinfo(afinfo); |
| 211 | request_module("xfrm-mode-%d-%d", family, encap); |
| 212 | modload_attempted = 1; |
| 213 | goto retry; |
| 214 | } |
| 215 | |
| 216 | xfrm_policy_put_afinfo(afinfo); |
| 217 | return mode; |
| 218 | } |
| 219 | |
| 220 | void xfrm_put_mode(struct xfrm_mode *mode) |
| 221 | { |
| 222 | module_put(mode->owner); |
| 223 | } |
| 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | static inline unsigned long make_jiffies(long secs) |
| 226 | { |
| 227 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 228 | return MAX_SCHEDULE_TIMEOUT-1; |
| 229 | else |
| 230 | return secs*HZ; |
| 231 | } |
| 232 | |
| 233 | static void xfrm_policy_timer(unsigned long data) |
| 234 | { |
| 235 | struct xfrm_policy *xp = (struct xfrm_policy*)data; |
| 236 | unsigned long now = (unsigned long)xtime.tv_sec; |
| 237 | long next = LONG_MAX; |
| 238 | int warn = 0; |
| 239 | int dir; |
| 240 | |
| 241 | read_lock(&xp->lock); |
| 242 | |
| 243 | if (xp->dead) |
| 244 | goto out; |
| 245 | |
Herbert Xu | 77d8d7a | 2005-10-05 12:15:12 -0700 | [diff] [blame] | 246 | dir = xfrm_policy_id2dir(xp->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | if (xp->lft.hard_add_expires_seconds) { |
| 249 | long tmo = xp->lft.hard_add_expires_seconds + |
| 250 | xp->curlft.add_time - now; |
| 251 | if (tmo <= 0) |
| 252 | goto expired; |
| 253 | if (tmo < next) |
| 254 | next = tmo; |
| 255 | } |
| 256 | if (xp->lft.hard_use_expires_seconds) { |
| 257 | long tmo = xp->lft.hard_use_expires_seconds + |
| 258 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 259 | if (tmo <= 0) |
| 260 | goto expired; |
| 261 | if (tmo < next) |
| 262 | next = tmo; |
| 263 | } |
| 264 | if (xp->lft.soft_add_expires_seconds) { |
| 265 | long tmo = xp->lft.soft_add_expires_seconds + |
| 266 | xp->curlft.add_time - now; |
| 267 | if (tmo <= 0) { |
| 268 | warn = 1; |
| 269 | tmo = XFRM_KM_TIMEOUT; |
| 270 | } |
| 271 | if (tmo < next) |
| 272 | next = tmo; |
| 273 | } |
| 274 | if (xp->lft.soft_use_expires_seconds) { |
| 275 | long tmo = xp->lft.soft_use_expires_seconds + |
| 276 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 277 | if (tmo <= 0) { |
| 278 | warn = 1; |
| 279 | tmo = XFRM_KM_TIMEOUT; |
| 280 | } |
| 281 | if (tmo < next) |
| 282 | next = tmo; |
| 283 | } |
| 284 | |
| 285 | if (warn) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 286 | km_policy_expired(xp, dir, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (next != LONG_MAX && |
| 288 | !mod_timer(&xp->timer, jiffies + make_jiffies(next))) |
| 289 | xfrm_pol_hold(xp); |
| 290 | |
| 291 | out: |
| 292 | read_unlock(&xp->lock); |
| 293 | xfrm_pol_put(xp); |
| 294 | return; |
| 295 | |
| 296 | expired: |
| 297 | read_unlock(&xp->lock); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 298 | if (!xfrm_policy_delete(xp, dir)) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 299 | km_policy_expired(xp, dir, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | xfrm_pol_put(xp); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2 |
| 305 | * SPD calls. |
| 306 | */ |
| 307 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 308 | struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | struct xfrm_policy *policy; |
| 311 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 312 | policy = kzalloc(sizeof(struct xfrm_policy), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | if (policy) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 315 | INIT_HLIST_NODE(&policy->bydst); |
| 316 | INIT_HLIST_NODE(&policy->byidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | rwlock_init(&policy->lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 318 | atomic_set(&policy->refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | init_timer(&policy->timer); |
| 320 | policy->timer.data = (unsigned long)policy; |
| 321 | policy->timer.function = xfrm_policy_timer; |
| 322 | } |
| 323 | return policy; |
| 324 | } |
| 325 | EXPORT_SYMBOL(xfrm_policy_alloc); |
| 326 | |
| 327 | /* Destroy xfrm_policy: descendant resources must be released to this moment. */ |
| 328 | |
| 329 | void __xfrm_policy_destroy(struct xfrm_policy *policy) |
| 330 | { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 331 | BUG_ON(!policy->dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 333 | BUG_ON(policy->bundles); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | if (del_timer(&policy->timer)) |
| 336 | BUG(); |
| 337 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 338 | security_xfrm_policy_free(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | kfree(policy); |
| 340 | } |
| 341 | EXPORT_SYMBOL(__xfrm_policy_destroy); |
| 342 | |
| 343 | static void xfrm_policy_gc_kill(struct xfrm_policy *policy) |
| 344 | { |
| 345 | struct dst_entry *dst; |
| 346 | |
| 347 | while ((dst = policy->bundles) != NULL) { |
| 348 | policy->bundles = dst->next; |
| 349 | dst_free(dst); |
| 350 | } |
| 351 | |
| 352 | if (del_timer(&policy->timer)) |
| 353 | atomic_dec(&policy->refcnt); |
| 354 | |
| 355 | if (atomic_read(&policy->refcnt) > 1) |
| 356 | flow_cache_flush(); |
| 357 | |
| 358 | xfrm_pol_put(policy); |
| 359 | } |
| 360 | |
| 361 | static void xfrm_policy_gc_task(void *data) |
| 362 | { |
| 363 | struct xfrm_policy *policy; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 364 | struct hlist_node *entry, *tmp; |
| 365 | struct hlist_head gc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | spin_lock_bh(&xfrm_policy_gc_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 368 | gc_list.first = xfrm_policy_gc_list.first; |
| 369 | INIT_HLIST_HEAD(&xfrm_policy_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | spin_unlock_bh(&xfrm_policy_gc_lock); |
| 371 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 372 | hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | xfrm_policy_gc_kill(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Rule must be locked. Release descentant resources, announce |
| 377 | * entry dead. The rule must be unlinked from lists to the moment. |
| 378 | */ |
| 379 | |
| 380 | static void xfrm_policy_kill(struct xfrm_policy *policy) |
| 381 | { |
| 382 | int dead; |
| 383 | |
| 384 | write_lock_bh(&policy->lock); |
| 385 | dead = policy->dead; |
| 386 | policy->dead = 1; |
| 387 | write_unlock_bh(&policy->lock); |
| 388 | |
| 389 | if (unlikely(dead)) { |
| 390 | WARN_ON(1); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | spin_lock(&xfrm_policy_gc_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 395 | hlist_add_head(&policy->bydst, &xfrm_policy_gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | spin_unlock(&xfrm_policy_gc_lock); |
| 397 | |
| 398 | schedule_work(&xfrm_policy_gc_work); |
| 399 | } |
| 400 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 401 | struct xfrm_policy_hash { |
| 402 | struct hlist_head *table; |
| 403 | unsigned int hmask; |
| 404 | }; |
| 405 | |
| 406 | static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2]; |
| 407 | static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly; |
| 408 | static struct hlist_head *xfrm_policy_byidx __read_mostly; |
| 409 | static unsigned int xfrm_idx_hmask __read_mostly; |
| 410 | static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024; |
| 411 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 412 | static inline unsigned int idx_hash(u32 index) |
| 413 | { |
| 414 | return __idx_hash(index, xfrm_idx_hmask); |
| 415 | } |
| 416 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 417 | static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir) |
| 418 | { |
| 419 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; |
| 420 | unsigned int hash = __sel_hash(sel, family, hmask); |
| 421 | |
| 422 | return (hash == hmask + 1 ? |
| 423 | &xfrm_policy_inexact[dir] : |
| 424 | xfrm_policy_bydst[dir].table + hash); |
| 425 | } |
| 426 | |
| 427 | static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir) |
| 428 | { |
| 429 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; |
| 430 | unsigned int hash = __addr_hash(daddr, saddr, family, hmask); |
| 431 | |
| 432 | return xfrm_policy_bydst[dir].table + hash; |
| 433 | } |
| 434 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 435 | static void xfrm_dst_hash_transfer(struct hlist_head *list, |
| 436 | struct hlist_head *ndsttable, |
| 437 | unsigned int nhashmask) |
| 438 | { |
| 439 | struct hlist_node *entry, *tmp; |
| 440 | struct xfrm_policy *pol; |
| 441 | |
| 442 | hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) { |
| 443 | unsigned int h; |
| 444 | |
| 445 | h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr, |
| 446 | pol->family, nhashmask); |
| 447 | hlist_add_head(&pol->bydst, ndsttable+h); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | static void xfrm_idx_hash_transfer(struct hlist_head *list, |
| 452 | struct hlist_head *nidxtable, |
| 453 | unsigned int nhashmask) |
| 454 | { |
| 455 | struct hlist_node *entry, *tmp; |
| 456 | struct xfrm_policy *pol; |
| 457 | |
| 458 | hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) { |
| 459 | unsigned int h; |
| 460 | |
| 461 | h = __idx_hash(pol->index, nhashmask); |
| 462 | hlist_add_head(&pol->byidx, nidxtable+h); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static unsigned long xfrm_new_hash_mask(unsigned int old_hmask) |
| 467 | { |
| 468 | return ((old_hmask + 1) << 1) - 1; |
| 469 | } |
| 470 | |
| 471 | static void xfrm_bydst_resize(int dir) |
| 472 | { |
| 473 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; |
| 474 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 475 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
| 476 | struct hlist_head *odst = xfrm_policy_bydst[dir].table; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 477 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 478 | int i; |
| 479 | |
| 480 | if (!ndst) |
| 481 | return; |
| 482 | |
| 483 | write_lock_bh(&xfrm_policy_lock); |
| 484 | |
| 485 | for (i = hmask; i >= 0; i--) |
| 486 | xfrm_dst_hash_transfer(odst + i, ndst, nhashmask); |
| 487 | |
| 488 | xfrm_policy_bydst[dir].table = ndst; |
| 489 | xfrm_policy_bydst[dir].hmask = nhashmask; |
| 490 | |
| 491 | write_unlock_bh(&xfrm_policy_lock); |
| 492 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 493 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void xfrm_byidx_resize(int total) |
| 497 | { |
| 498 | unsigned int hmask = xfrm_idx_hmask; |
| 499 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 500 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
| 501 | struct hlist_head *oidx = xfrm_policy_byidx; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 502 | struct hlist_head *nidx = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 503 | int i; |
| 504 | |
| 505 | if (!nidx) |
| 506 | return; |
| 507 | |
| 508 | write_lock_bh(&xfrm_policy_lock); |
| 509 | |
| 510 | for (i = hmask; i >= 0; i--) |
| 511 | xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask); |
| 512 | |
| 513 | xfrm_policy_byidx = nidx; |
| 514 | xfrm_idx_hmask = nhashmask; |
| 515 | |
| 516 | write_unlock_bh(&xfrm_policy_lock); |
| 517 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 518 | xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static inline int xfrm_bydst_should_resize(int dir, int *total) |
| 522 | { |
| 523 | unsigned int cnt = xfrm_policy_count[dir]; |
| 524 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; |
| 525 | |
| 526 | if (total) |
| 527 | *total += cnt; |
| 528 | |
| 529 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 530 | cnt > hmask) |
| 531 | return 1; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static inline int xfrm_byidx_should_resize(int total) |
| 537 | { |
| 538 | unsigned int hmask = xfrm_idx_hmask; |
| 539 | |
| 540 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 541 | total > hmask) |
| 542 | return 1; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static DEFINE_MUTEX(hash_resize_mutex); |
| 548 | |
| 549 | static void xfrm_hash_resize(void *__unused) |
| 550 | { |
| 551 | int dir, total; |
| 552 | |
| 553 | mutex_lock(&hash_resize_mutex); |
| 554 | |
| 555 | total = 0; |
| 556 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
| 557 | if (xfrm_bydst_should_resize(dir, &total)) |
| 558 | xfrm_bydst_resize(dir); |
| 559 | } |
| 560 | if (xfrm_byidx_should_resize(total)) |
| 561 | xfrm_byidx_resize(total); |
| 562 | |
| 563 | mutex_unlock(&hash_resize_mutex); |
| 564 | } |
| 565 | |
| 566 | static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize, NULL); |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* Generate new index... KAME seems to generate them ordered by cost |
| 569 | * of an absolute inpredictability of ordering of rules. This will not pass. */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 570 | static u32 xfrm_gen_index(u8 type, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | static u32 idx_generator; |
| 573 | |
| 574 | for (;;) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 575 | struct hlist_node *entry; |
| 576 | struct hlist_head *list; |
| 577 | struct xfrm_policy *p; |
| 578 | u32 idx; |
| 579 | int found; |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | idx = (idx_generator | dir); |
| 582 | idx_generator += 8; |
| 583 | if (idx == 0) |
| 584 | idx = 8; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 585 | list = xfrm_policy_byidx + idx_hash(idx); |
| 586 | found = 0; |
| 587 | hlist_for_each_entry(p, entry, list, byidx) { |
| 588 | if (p->index == idx) { |
| 589 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | break; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 593 | if (!found) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return idx; |
| 595 | } |
| 596 | } |
| 597 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 598 | static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2) |
| 599 | { |
| 600 | u32 *p1 = (u32 *) s1; |
| 601 | u32 *p2 = (u32 *) s2; |
| 602 | int len = sizeof(struct xfrm_selector) / sizeof(u32); |
| 603 | int i; |
| 604 | |
| 605 | for (i = 0; i < len; i++) { |
| 606 | if (p1[i] != p2[i]) |
| 607 | return 1; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) |
| 614 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 615 | struct xfrm_policy *pol; |
| 616 | struct xfrm_policy *delpol; |
| 617 | struct hlist_head *chain; |
| 618 | struct hlist_node *entry, *newpos, *last; |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 619 | struct dst_entry *gc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | write_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 622 | chain = policy_hash_bysel(&policy->selector, policy->family, dir); |
| 623 | delpol = NULL; |
| 624 | newpos = NULL; |
| 625 | last = NULL; |
| 626 | hlist_for_each_entry(pol, entry, chain, bydst) { |
| 627 | if (!delpol && |
| 628 | pol->type == policy->type && |
| 629 | !selector_cmp(&pol->selector, &policy->selector) && |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 630 | xfrm_sec_ctx_match(pol->security, policy->security)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (excl) { |
| 632 | write_unlock_bh(&xfrm_policy_lock); |
| 633 | return -EEXIST; |
| 634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | delpol = pol; |
| 636 | if (policy->priority > pol->priority) |
| 637 | continue; |
| 638 | } else if (policy->priority >= pol->priority) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 639 | last = &pol->bydst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | continue; |
| 641 | } |
| 642 | if (!newpos) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 643 | newpos = &pol->bydst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (delpol) |
| 645 | break; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 646 | last = &pol->bydst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 648 | if (!newpos) |
| 649 | newpos = last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (newpos) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 651 | hlist_add_after(newpos, &policy->bydst); |
| 652 | else |
| 653 | hlist_add_head(&policy->bydst, chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | xfrm_pol_hold(policy); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 655 | xfrm_policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | atomic_inc(&flow_cache_genid); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 657 | if (delpol) { |
| 658 | hlist_del(&delpol->bydst); |
| 659 | hlist_del(&delpol->byidx); |
| 660 | xfrm_policy_count[dir]--; |
| 661 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 662 | policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 663 | hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | policy->curlft.add_time = (unsigned long)xtime.tv_sec; |
| 665 | policy->curlft.use_time = 0; |
| 666 | if (!mod_timer(&policy->timer, jiffies + HZ)) |
| 667 | xfrm_pol_hold(policy); |
| 668 | write_unlock_bh(&xfrm_policy_lock); |
| 669 | |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 670 | if (delpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | xfrm_policy_kill(delpol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 672 | else if (xfrm_bydst_should_resize(dir, NULL)) |
| 673 | schedule_work(&xfrm_hash_work); |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 674 | |
| 675 | read_lock_bh(&xfrm_policy_lock); |
| 676 | gc_list = NULL; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 677 | entry = &policy->bydst; |
| 678 | hlist_for_each_entry_continue(policy, entry, bydst) { |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 679 | struct dst_entry *dst; |
| 680 | |
| 681 | write_lock(&policy->lock); |
| 682 | dst = policy->bundles; |
| 683 | if (dst) { |
| 684 | struct dst_entry *tail = dst; |
| 685 | while (tail->next) |
| 686 | tail = tail->next; |
| 687 | tail->next = gc_list; |
| 688 | gc_list = dst; |
| 689 | |
| 690 | policy->bundles = NULL; |
| 691 | } |
| 692 | write_unlock(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 694 | read_unlock_bh(&xfrm_policy_lock); |
| 695 | |
| 696 | while (gc_list) { |
| 697 | struct dst_entry *dst = gc_list; |
| 698 | |
| 699 | gc_list = dst->next; |
| 700 | dst_free(dst); |
| 701 | } |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | EXPORT_SYMBOL(xfrm_policy_insert); |
| 706 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 707 | struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir, |
| 708 | struct xfrm_selector *sel, |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 709 | struct xfrm_sec_ctx *ctx, int delete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 711 | struct xfrm_policy *pol, *ret; |
| 712 | struct hlist_head *chain; |
| 713 | struct hlist_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | write_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 716 | chain = policy_hash_bysel(sel, sel->family, dir); |
| 717 | ret = NULL; |
| 718 | hlist_for_each_entry(pol, entry, chain, bydst) { |
| 719 | if (pol->type == type && |
| 720 | !selector_cmp(sel, &pol->selector) && |
| 721 | xfrm_sec_ctx_match(ctx, pol->security)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 723 | if (delete) { |
| 724 | hlist_del(&pol->bydst); |
| 725 | hlist_del(&pol->byidx); |
| 726 | xfrm_policy_count[dir]--; |
| 727 | } |
| 728 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | break; |
| 730 | } |
| 731 | } |
| 732 | write_unlock_bh(&xfrm_policy_lock); |
| 733 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 734 | if (ret && delete) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | atomic_inc(&flow_cache_genid); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 736 | xfrm_policy_kill(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 738 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 740 | EXPORT_SYMBOL(xfrm_policy_bysel_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 742 | struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 744 | struct xfrm_policy *pol, *ret; |
| 745 | struct hlist_head *chain; |
| 746 | struct hlist_node *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | write_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 749 | chain = xfrm_policy_byidx + idx_hash(id); |
| 750 | ret = NULL; |
| 751 | hlist_for_each_entry(pol, entry, chain, byidx) { |
| 752 | if (pol->type == type && pol->index == id) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 754 | if (delete) { |
| 755 | hlist_del(&pol->bydst); |
| 756 | hlist_del(&pol->byidx); |
| 757 | xfrm_policy_count[dir]--; |
| 758 | } |
| 759 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | } |
| 763 | write_unlock_bh(&xfrm_policy_lock); |
| 764 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 765 | if (ret && delete) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | atomic_inc(&flow_cache_genid); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 767 | xfrm_policy_kill(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 769 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | EXPORT_SYMBOL(xfrm_policy_byid); |
| 772 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 773 | void xfrm_policy_flush(u8 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | int dir; |
| 776 | |
| 777 | write_lock_bh(&xfrm_policy_lock); |
| 778 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 779 | struct xfrm_policy *pol; |
| 780 | struct hlist_node *entry; |
David S. Miller | ae8c057 | 2006-10-03 16:00:26 -0700 | [diff] [blame] | 781 | int i, killed; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 782 | |
David S. Miller | ae8c057 | 2006-10-03 16:00:26 -0700 | [diff] [blame] | 783 | killed = 0; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 784 | again1: |
| 785 | hlist_for_each_entry(pol, entry, |
| 786 | &xfrm_policy_inexact[dir], bydst) { |
| 787 | if (pol->type != type) |
| 788 | continue; |
| 789 | hlist_del(&pol->bydst); |
| 790 | hlist_del(&pol->byidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | write_unlock_bh(&xfrm_policy_lock); |
| 792 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 793 | xfrm_policy_kill(pol); |
David S. Miller | ae8c057 | 2006-10-03 16:00:26 -0700 | [diff] [blame] | 794 | killed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | write_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 797 | goto again1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 799 | |
| 800 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
| 801 | again2: |
| 802 | hlist_for_each_entry(pol, entry, |
| 803 | xfrm_policy_bydst[dir].table + i, |
| 804 | bydst) { |
| 805 | if (pol->type != type) |
| 806 | continue; |
| 807 | hlist_del(&pol->bydst); |
| 808 | hlist_del(&pol->byidx); |
| 809 | write_unlock_bh(&xfrm_policy_lock); |
| 810 | |
| 811 | xfrm_policy_kill(pol); |
David S. Miller | ae8c057 | 2006-10-03 16:00:26 -0700 | [diff] [blame] | 812 | killed++; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 813 | |
| 814 | write_lock_bh(&xfrm_policy_lock); |
| 815 | goto again2; |
| 816 | } |
| 817 | } |
| 818 | |
David S. Miller | ae8c057 | 2006-10-03 16:00:26 -0700 | [diff] [blame] | 819 | xfrm_policy_count[dir] -= killed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | atomic_inc(&flow_cache_genid); |
| 822 | write_unlock_bh(&xfrm_policy_lock); |
| 823 | } |
| 824 | EXPORT_SYMBOL(xfrm_policy_flush); |
| 825 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 826 | int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | void *data) |
| 828 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 829 | struct xfrm_policy *pol; |
| 830 | struct hlist_node *entry; |
| 831 | int dir, count, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
| 833 | read_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 834 | count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 836 | struct hlist_head *table = xfrm_policy_bydst[dir].table; |
| 837 | int i; |
| 838 | |
| 839 | hlist_for_each_entry(pol, entry, |
| 840 | &xfrm_policy_inexact[dir], bydst) { |
| 841 | if (pol->type == type) |
| 842 | count++; |
| 843 | } |
| 844 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
| 845 | hlist_for_each_entry(pol, entry, table + i, bydst) { |
| 846 | if (pol->type == type) |
| 847 | count++; |
| 848 | } |
| 849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | if (count == 0) { |
| 853 | error = -ENOENT; |
| 854 | goto out; |
| 855 | } |
| 856 | |
| 857 | for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 858 | struct hlist_head *table = xfrm_policy_bydst[dir].table; |
| 859 | int i; |
| 860 | |
| 861 | hlist_for_each_entry(pol, entry, |
| 862 | &xfrm_policy_inexact[dir], bydst) { |
| 863 | if (pol->type != type) |
| 864 | continue; |
| 865 | error = func(pol, dir % XFRM_POLICY_MAX, --count, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (error) |
| 867 | goto out; |
| 868 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 869 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
| 870 | hlist_for_each_entry(pol, entry, table + i, bydst) { |
| 871 | if (pol->type != type) |
| 872 | continue; |
| 873 | error = func(pol, dir % XFRM_POLICY_MAX, --count, data); |
| 874 | if (error) |
| 875 | goto out; |
| 876 | } |
| 877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 879 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | out: |
| 881 | read_unlock_bh(&xfrm_policy_lock); |
| 882 | return error; |
| 883 | } |
| 884 | EXPORT_SYMBOL(xfrm_policy_walk); |
| 885 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 886 | /* |
| 887 | * Find policy to apply to this flow. |
| 888 | * |
| 889 | * Returns 0 if policy found, else an -errno. |
| 890 | */ |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 891 | static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl, |
| 892 | u8 type, u16 family, int dir) |
| 893 | { |
| 894 | struct xfrm_selector *sel = &pol->selector; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 895 | int match, ret = -ESRCH; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 896 | |
| 897 | if (pol->family != family || |
| 898 | pol->type != type) |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 899 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 900 | |
| 901 | match = xfrm_selector_match(sel, fl, family); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 902 | if (match) |
| 903 | ret = security_xfrm_policy_lookup(pol, fl->secid, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 904 | |
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 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 908 | static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl, |
| 909 | u16 family, u8 dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 911 | int err; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 912 | struct xfrm_policy *pol, *ret; |
| 913 | xfrm_address_t *daddr, *saddr; |
| 914 | struct hlist_node *entry; |
| 915 | struct hlist_head *chain; |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 916 | u32 priority = ~0U; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 917 | |
| 918 | daddr = xfrm_flowi_daddr(fl, family); |
| 919 | saddr = xfrm_flowi_saddr(fl, family); |
| 920 | if (unlikely(!daddr || !saddr)) |
| 921 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | read_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 924 | chain = policy_hash_direct(daddr, saddr, family, dir); |
| 925 | ret = NULL; |
| 926 | hlist_for_each_entry(pol, entry, chain, bydst) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 927 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 928 | if (err) { |
| 929 | if (err == -ESRCH) |
| 930 | continue; |
| 931 | else { |
| 932 | ret = ERR_PTR(err); |
| 933 | goto fail; |
| 934 | } |
| 935 | } else { |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 936 | ret = pol; |
| 937 | priority = ret->priority; |
| 938 | break; |
| 939 | } |
| 940 | } |
| 941 | chain = &xfrm_policy_inexact[dir]; |
| 942 | hlist_for_each_entry(pol, entry, chain, bydst) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 943 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 944 | if (err) { |
| 945 | if (err == -ESRCH) |
| 946 | continue; |
| 947 | else { |
| 948 | ret = ERR_PTR(err); |
| 949 | goto fail; |
| 950 | } |
| 951 | } else if (pol->priority < priority) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 952 | ret = pol; |
| 953 | break; |
| 954 | } |
| 955 | } |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 956 | if (ret) |
| 957 | xfrm_pol_hold(ret); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 958 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | read_unlock_bh(&xfrm_policy_lock); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 960 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 961 | return ret; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 962 | } |
| 963 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 964 | static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 965 | void **objp, atomic_t **obj_refp) |
| 966 | { |
| 967 | struct xfrm_policy *pol; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 968 | int err = 0; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 969 | |
| 970 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 971 | pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 972 | if (IS_ERR(pol)) { |
| 973 | err = PTR_ERR(pol); |
| 974 | pol = NULL; |
| 975 | } |
| 976 | if (pol || err) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 977 | goto end; |
| 978 | #endif |
| 979 | pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 980 | if (IS_ERR(pol)) { |
| 981 | err = PTR_ERR(pol); |
| 982 | pol = NULL; |
| 983 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 984 | #ifdef CONFIG_XFRM_SUB_POLICY |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 985 | end: |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 986 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | if ((*objp = (void *) pol) != NULL) |
| 988 | *obj_refp = &pol->refcnt; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 989 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 992 | static inline int policy_to_flow_dir(int dir) |
| 993 | { |
| 994 | if (XFRM_POLICY_IN == FLOW_DIR_IN && |
| 995 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
| 996 | XFRM_POLICY_FWD == FLOW_DIR_FWD) |
| 997 | return dir; |
| 998 | switch (dir) { |
| 999 | default: |
| 1000 | case XFRM_POLICY_IN: |
| 1001 | return FLOW_DIR_IN; |
| 1002 | case XFRM_POLICY_OUT: |
| 1003 | return FLOW_DIR_OUT; |
| 1004 | case XFRM_POLICY_FWD: |
| 1005 | return FLOW_DIR_FWD; |
| 1006 | }; |
| 1007 | } |
| 1008 | |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1009 | static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | { |
| 1011 | struct xfrm_policy *pol; |
| 1012 | |
| 1013 | read_lock_bh(&xfrm_policy_lock); |
| 1014 | if ((pol = sk->sk_policy[dir]) != NULL) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1015 | int match = xfrm_selector_match(&pol->selector, fl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | sk->sk_family); |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1017 | int err = 0; |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if (match) |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1020 | err = security_xfrm_policy_lookup(pol, fl->secid, policy_to_flow_dir(dir)); |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1021 | |
| 1022 | if (match && !err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | xfrm_pol_hold(pol); |
| 1024 | else |
| 1025 | pol = NULL; |
| 1026 | } |
| 1027 | read_unlock_bh(&xfrm_policy_lock); |
| 1028 | return pol; |
| 1029 | } |
| 1030 | |
| 1031 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir) |
| 1032 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1033 | struct hlist_head *chain = policy_hash_bysel(&pol->selector, |
| 1034 | pol->family, dir); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1035 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1036 | hlist_add_head(&pol->bydst, chain); |
| 1037 | hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index)); |
| 1038 | xfrm_policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1040 | |
| 1041 | if (xfrm_bydst_should_resize(dir, NULL)) |
| 1042 | schedule_work(&xfrm_hash_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 1046 | int dir) |
| 1047 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1048 | if (hlist_unhashed(&pol->bydst)) |
| 1049 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1051 | hlist_del(&pol->bydst); |
| 1052 | hlist_del(&pol->byidx); |
| 1053 | xfrm_policy_count[dir]--; |
| 1054 | |
| 1055 | return pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1058 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
| 1060 | write_lock_bh(&xfrm_policy_lock); |
| 1061 | pol = __xfrm_policy_unlink(pol, dir); |
| 1062 | write_unlock_bh(&xfrm_policy_lock); |
| 1063 | if (pol) { |
| 1064 | if (dir < XFRM_POLICY_MAX) |
| 1065 | atomic_inc(&flow_cache_genid); |
| 1066 | xfrm_policy_kill(pol); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1067 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1069 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1071 | EXPORT_SYMBOL(xfrm_policy_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
| 1073 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol) |
| 1074 | { |
| 1075 | struct xfrm_policy *old_pol; |
| 1076 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1077 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1078 | if (pol && pol->type != XFRM_POLICY_TYPE_MAIN) |
| 1079 | return -EINVAL; |
| 1080 | #endif |
| 1081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | write_lock_bh(&xfrm_policy_lock); |
| 1083 | old_pol = sk->sk_policy[dir]; |
| 1084 | sk->sk_policy[dir] = pol; |
| 1085 | if (pol) { |
| 1086 | pol->curlft.add_time = (unsigned long)xtime.tv_sec; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1087 | pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir); |
| 1089 | } |
| 1090 | if (old_pol) |
| 1091 | __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir); |
| 1092 | write_unlock_bh(&xfrm_policy_lock); |
| 1093 | |
| 1094 | if (old_pol) { |
| 1095 | xfrm_policy_kill(old_pol); |
| 1096 | } |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir) |
| 1101 | { |
| 1102 | struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC); |
| 1103 | |
| 1104 | if (newp) { |
| 1105 | newp->selector = old->selector; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1106 | if (security_xfrm_policy_clone(old, newp)) { |
| 1107 | kfree(newp); |
| 1108 | return NULL; /* ENOMEM */ |
| 1109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | newp->lft = old->lft; |
| 1111 | newp->curlft = old->curlft; |
| 1112 | newp->action = old->action; |
| 1113 | newp->flags = old->flags; |
| 1114 | newp->xfrm_nr = old->xfrm_nr; |
| 1115 | newp->index = old->index; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1116 | newp->type = old->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | memcpy(newp->xfrm_vec, old->xfrm_vec, |
| 1118 | newp->xfrm_nr*sizeof(struct xfrm_tmpl)); |
| 1119 | write_lock_bh(&xfrm_policy_lock); |
| 1120 | __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir); |
| 1121 | write_unlock_bh(&xfrm_policy_lock); |
| 1122 | xfrm_pol_put(newp); |
| 1123 | } |
| 1124 | return newp; |
| 1125 | } |
| 1126 | |
| 1127 | int __xfrm_sk_clone_policy(struct sock *sk) |
| 1128 | { |
| 1129 | struct xfrm_policy *p0 = sk->sk_policy[0], |
| 1130 | *p1 = sk->sk_policy[1]; |
| 1131 | |
| 1132 | sk->sk_policy[0] = sk->sk_policy[1] = NULL; |
| 1133 | if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL) |
| 1134 | return -ENOMEM; |
| 1135 | if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL) |
| 1136 | return -ENOMEM; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1140 | static int |
| 1141 | xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote, |
| 1142 | unsigned short family) |
| 1143 | { |
| 1144 | int err; |
| 1145 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1146 | |
| 1147 | if (unlikely(afinfo == NULL)) |
| 1148 | return -EINVAL; |
| 1149 | err = afinfo->get_saddr(local, remote); |
| 1150 | xfrm_policy_put_afinfo(afinfo); |
| 1151 | return err; |
| 1152 | } |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | /* Resolve list of templates for the flow, given policy. */ |
| 1155 | |
| 1156 | static int |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1157 | xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl, |
| 1158 | struct xfrm_state **xfrm, |
| 1159 | unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
| 1161 | int nx; |
| 1162 | int i, error; |
| 1163 | xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family); |
| 1164 | xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1165 | xfrm_address_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | for (nx=0, i = 0; i < policy->xfrm_nr; i++) { |
| 1168 | struct xfrm_state *x; |
| 1169 | xfrm_address_t *remote = daddr; |
| 1170 | xfrm_address_t *local = saddr; |
| 1171 | struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i]; |
| 1172 | |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 1173 | if (tmpl->mode == XFRM_MODE_TUNNEL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | remote = &tmpl->id.daddr; |
| 1175 | local = &tmpl->saddr; |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1176 | if (xfrm_addr_any(local, family)) { |
| 1177 | error = xfrm_get_saddr(&tmp, remote, family); |
| 1178 | if (error) |
| 1179 | goto fail; |
| 1180 | local = &tmp; |
| 1181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family); |
| 1185 | |
| 1186 | if (x && x->km.state == XFRM_STATE_VALID) { |
| 1187 | xfrm[nx++] = x; |
| 1188 | daddr = remote; |
| 1189 | saddr = local; |
| 1190 | continue; |
| 1191 | } |
| 1192 | if (x) { |
| 1193 | error = (x->km.state == XFRM_STATE_ERROR ? |
| 1194 | -EINVAL : -EAGAIN); |
| 1195 | xfrm_state_put(x); |
| 1196 | } |
| 1197 | |
| 1198 | if (!tmpl->optional) |
| 1199 | goto fail; |
| 1200 | } |
| 1201 | return nx; |
| 1202 | |
| 1203 | fail: |
| 1204 | for (nx--; nx>=0; nx--) |
| 1205 | xfrm_state_put(xfrm[nx]); |
| 1206 | return error; |
| 1207 | } |
| 1208 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1209 | static int |
| 1210 | xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl, |
| 1211 | struct xfrm_state **xfrm, |
| 1212 | unsigned short family) |
| 1213 | { |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1214 | struct xfrm_state *tp[XFRM_MAX_DEPTH]; |
| 1215 | struct xfrm_state **tpp = (npols > 1) ? tp : xfrm; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1216 | int cnx = 0; |
| 1217 | int error; |
| 1218 | int ret; |
| 1219 | int i; |
| 1220 | |
| 1221 | for (i = 0; i < npols; i++) { |
| 1222 | if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) { |
| 1223 | error = -ENOBUFS; |
| 1224 | goto fail; |
| 1225 | } |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1226 | |
| 1227 | ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1228 | if (ret < 0) { |
| 1229 | error = ret; |
| 1230 | goto fail; |
| 1231 | } else |
| 1232 | cnx += ret; |
| 1233 | } |
| 1234 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1235 | /* found states are sorted for outbound processing */ |
| 1236 | if (npols > 1) |
| 1237 | xfrm_state_sort(xfrm, tpp, cnx, family); |
| 1238 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1239 | return cnx; |
| 1240 | |
| 1241 | fail: |
| 1242 | for (cnx--; cnx>=0; cnx--) |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1243 | xfrm_state_put(tpp[cnx]); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1244 | return error; |
| 1245 | |
| 1246 | } |
| 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | /* Check that the bundle accepts the flow and its components are |
| 1249 | * still valid. |
| 1250 | */ |
| 1251 | |
| 1252 | static struct dst_entry * |
| 1253 | xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family) |
| 1254 | { |
| 1255 | struct dst_entry *x; |
| 1256 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1257 | if (unlikely(afinfo == NULL)) |
| 1258 | return ERR_PTR(-EINVAL); |
| 1259 | x = afinfo->find_bundle(fl, policy); |
| 1260 | xfrm_policy_put_afinfo(afinfo); |
| 1261 | return x; |
| 1262 | } |
| 1263 | |
| 1264 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 1265 | * all the metrics... Shortly, bundle a bundle. |
| 1266 | */ |
| 1267 | |
| 1268 | static int |
| 1269 | xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx, |
| 1270 | struct flowi *fl, struct dst_entry **dst_p, |
| 1271 | unsigned short family) |
| 1272 | { |
| 1273 | int err; |
| 1274 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1275 | if (unlikely(afinfo == NULL)) |
| 1276 | return -EINVAL; |
| 1277 | err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p); |
| 1278 | xfrm_policy_put_afinfo(afinfo); |
| 1279 | return err; |
| 1280 | } |
| 1281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | static int stale_bundle(struct dst_entry *dst); |
| 1284 | |
| 1285 | /* Main function: finds/creates a bundle for given flow. |
| 1286 | * |
| 1287 | * At the moment we eat a raw IP route. Mostly to speed up lookups |
| 1288 | * on interfaces with disabled IPsec. |
| 1289 | */ |
| 1290 | int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl, |
| 1291 | struct sock *sk, int flags) |
| 1292 | { |
| 1293 | struct xfrm_policy *policy; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1294 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 1295 | int npols; |
| 1296 | int pol_dead; |
| 1297 | int xfrm_nr; |
| 1298 | int pi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | struct xfrm_state *xfrm[XFRM_MAX_DEPTH]; |
| 1300 | struct dst_entry *dst, *dst_orig = *dst_p; |
| 1301 | int nx = 0; |
| 1302 | int err; |
| 1303 | u32 genid; |
Patrick McHardy | 42cf93c | 2006-02-21 13:37:35 -0800 | [diff] [blame] | 1304 | u16 family; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1305 | u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | restart: |
| 1308 | genid = atomic_read(&flow_cache_genid); |
| 1309 | policy = NULL; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1310 | for (pi = 0; pi < ARRAY_SIZE(pols); pi++) |
| 1311 | pols[pi] = NULL; |
| 1312 | npols = 0; |
| 1313 | pol_dead = 0; |
| 1314 | xfrm_nr = 0; |
| 1315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | if (sk && sk->sk_policy[1]) |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1317 | policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | if (!policy) { |
| 1320 | /* To accelerate a bit... */ |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1321 | if ((dst_orig->flags & DST_NOXFRM) || |
| 1322 | !xfrm_policy_count[XFRM_POLICY_OUT]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | return 0; |
| 1324 | |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1325 | policy = flow_cache_lookup(fl, dst_orig->ops->family, |
Patrick McHardy | 42cf93c | 2006-02-21 13:37:35 -0800 | [diff] [blame] | 1326 | dir, xfrm_policy_lookup); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1327 | if (IS_ERR(policy)) |
| 1328 | return PTR_ERR(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | if (!policy) |
| 1332 | return 0; |
| 1333 | |
Patrick McHardy | 42cf93c | 2006-02-21 13:37:35 -0800 | [diff] [blame] | 1334 | family = dst_orig->ops->family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | policy->curlft.use_time = (unsigned long)xtime.tv_sec; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1336 | pols[0] = policy; |
| 1337 | npols ++; |
| 1338 | xfrm_nr += pols[0]->xfrm_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | switch (policy->action) { |
| 1341 | case XFRM_POLICY_BLOCK: |
| 1342 | /* Prohibit the flow */ |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 1343 | err = -EPERM; |
| 1344 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | case XFRM_POLICY_ALLOW: |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1347 | #ifndef CONFIG_XFRM_SUB_POLICY |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | if (policy->xfrm_nr == 0) { |
| 1349 | /* Flow passes not transformed. */ |
| 1350 | xfrm_pol_put(policy); |
| 1351 | return 0; |
| 1352 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1353 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* Try to find matching bundle. |
| 1356 | * |
| 1357 | * LATER: help from flow cache. It is optional, this |
| 1358 | * is required only for output policy. |
| 1359 | */ |
| 1360 | dst = xfrm_find_bundle(fl, policy, family); |
| 1361 | if (IS_ERR(dst)) { |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 1362 | err = PTR_ERR(dst); |
| 1363 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | if (dst) |
| 1367 | break; |
| 1368 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1369 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1370 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
| 1371 | pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, |
| 1372 | fl, family, |
| 1373 | XFRM_POLICY_OUT); |
| 1374 | if (pols[1]) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1375 | if (IS_ERR(pols[1])) { |
| 1376 | err = PTR_ERR(pols[1]); |
| 1377 | goto error; |
| 1378 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1379 | if (pols[1]->action == XFRM_POLICY_BLOCK) { |
| 1380 | err = -EPERM; |
| 1381 | goto error; |
| 1382 | } |
| 1383 | npols ++; |
| 1384 | xfrm_nr += pols[1]->xfrm_nr; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Because neither flowi nor bundle information knows about |
| 1390 | * transformation template size. On more than one policy usage |
| 1391 | * we can realize whether all of them is bypass or not after |
| 1392 | * they are searched. See above not-transformed bypass |
| 1393 | * is surrounded by non-sub policy configuration, too. |
| 1394 | */ |
| 1395 | if (xfrm_nr == 0) { |
| 1396 | /* Flow passes not transformed. */ |
| 1397 | xfrm_pols_put(pols, npols); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | #endif |
| 1402 | nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (unlikely(nx<0)) { |
| 1405 | err = nx; |
| 1406 | if (err == -EAGAIN && flags) { |
| 1407 | DECLARE_WAITQUEUE(wait, current); |
| 1408 | |
| 1409 | add_wait_queue(&km_waitq, &wait); |
| 1410 | set_current_state(TASK_INTERRUPTIBLE); |
| 1411 | schedule(); |
| 1412 | set_current_state(TASK_RUNNING); |
| 1413 | remove_wait_queue(&km_waitq, &wait); |
| 1414 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1415 | nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (nx == -EAGAIN && signal_pending(current)) { |
| 1418 | err = -ERESTART; |
| 1419 | goto error; |
| 1420 | } |
| 1421 | if (nx == -EAGAIN || |
| 1422 | genid != atomic_read(&flow_cache_genid)) { |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1423 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | goto restart; |
| 1425 | } |
| 1426 | err = nx; |
| 1427 | } |
| 1428 | if (err < 0) |
| 1429 | goto error; |
| 1430 | } |
| 1431 | if (nx == 0) { |
| 1432 | /* Flow passes not transformed. */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1433 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | dst = dst_orig; |
| 1438 | err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family); |
| 1439 | |
| 1440 | if (unlikely(err)) { |
| 1441 | int i; |
| 1442 | for (i=0; i<nx; i++) |
| 1443 | xfrm_state_put(xfrm[i]); |
| 1444 | goto error; |
| 1445 | } |
| 1446 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1447 | for (pi = 0; pi < npols; pi++) { |
| 1448 | read_lock_bh(&pols[pi]->lock); |
| 1449 | pol_dead |= pols[pi]->dead; |
| 1450 | read_unlock_bh(&pols[pi]->lock); |
| 1451 | } |
| 1452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | write_lock_bh(&policy->lock); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1454 | if (unlikely(pol_dead || stale_bundle(dst))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | /* Wow! While we worked on resolving, this |
| 1456 | * policy has gone. Retry. It is not paranoia, |
| 1457 | * we just cannot enlist new bundle to dead object. |
| 1458 | * We can't enlist stable bundles either. |
| 1459 | */ |
| 1460 | write_unlock_bh(&policy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | if (dst) |
| 1462 | dst_free(dst); |
Herbert Xu | 00de651 | 2006-02-13 16:01:27 -0800 | [diff] [blame] | 1463 | |
| 1464 | err = -EHOSTUNREACH; |
| 1465 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | } |
| 1467 | dst->next = policy->bundles; |
| 1468 | policy->bundles = dst; |
| 1469 | dst_hold(dst); |
| 1470 | write_unlock_bh(&policy->lock); |
| 1471 | } |
| 1472 | *dst_p = dst; |
| 1473 | dst_release(dst_orig); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1474 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | return 0; |
| 1476 | |
| 1477 | error: |
| 1478 | dst_release(dst_orig); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1479 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | *dst_p = NULL; |
| 1481 | return err; |
| 1482 | } |
| 1483 | EXPORT_SYMBOL(xfrm_lookup); |
| 1484 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1485 | static inline int |
| 1486 | xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl) |
| 1487 | { |
| 1488 | struct xfrm_state *x; |
| 1489 | int err; |
| 1490 | |
| 1491 | if (!skb->sp || idx < 0 || idx >= skb->sp->len) |
| 1492 | return 0; |
| 1493 | x = skb->sp->xvec[idx]; |
| 1494 | if (!x->type->reject) |
| 1495 | return 0; |
| 1496 | xfrm_state_hold(x); |
| 1497 | err = x->type->reject(x, skb, fl); |
| 1498 | xfrm_state_put(x); |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | /* When skb is transformed back to its "native" form, we have to |
| 1503 | * check policy restrictions. At the moment we make this in maximally |
| 1504 | * stupid way. Shame on me. :-) Of course, connected sockets must |
| 1505 | * have policy cached at them. |
| 1506 | */ |
| 1507 | |
| 1508 | static inline int |
| 1509 | xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x, |
| 1510 | unsigned short family) |
| 1511 | { |
| 1512 | if (xfrm_state_kern(x)) |
| 1513 | return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, family); |
| 1514 | return x->id.proto == tmpl->id.proto && |
| 1515 | (x->id.spi == tmpl->id.spi || !tmpl->id.spi) && |
| 1516 | (x->props.reqid == tmpl->reqid || !tmpl->reqid) && |
| 1517 | x->props.mode == tmpl->mode && |
Masahide NAKAMURA | f3bd484 | 2006-08-23 18:00:48 -0700 | [diff] [blame] | 1518 | ((tmpl->aalgos & (1<<x->props.aalgo)) || |
| 1519 | !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) && |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 1520 | !(x->props.mode != XFRM_MODE_TRANSPORT && |
| 1521 | xfrm_state_addr_cmp(tmpl, x, family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1524 | /* |
| 1525 | * 0 or more than 0 is returned when validation is succeeded (either bypass |
| 1526 | * because of optional transport mode, or next index of the mathced secpath |
| 1527 | * state with the template. |
| 1528 | * -1 is returned when no matching template is found. |
| 1529 | * Otherwise "-2 - errored_index" is returned. |
| 1530 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | static inline int |
| 1532 | xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start, |
| 1533 | unsigned short family) |
| 1534 | { |
| 1535 | int idx = start; |
| 1536 | |
| 1537 | if (tmpl->optional) { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 1538 | if (tmpl->mode == XFRM_MODE_TRANSPORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | return start; |
| 1540 | } else |
| 1541 | start = -1; |
| 1542 | for (; idx < sp->len; idx++) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 1543 | if (xfrm_state_ok(tmpl, sp->xvec[idx], family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | return ++idx; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1545 | if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) { |
| 1546 | if (start == -1) |
| 1547 | start = -2-idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | break; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
| 1551 | return start; |
| 1552 | } |
| 1553 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 1554 | int |
| 1555 | xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | { |
| 1557 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1558 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
| 1560 | if (unlikely(afinfo == NULL)) |
| 1561 | return -EAFNOSUPPORT; |
| 1562 | |
| 1563 | afinfo->decode_session(skb, fl); |
Venkat Yekkirala | beb8d13 | 2006-08-04 23:12:42 -0700 | [diff] [blame] | 1564 | err = security_xfrm_decode_session(skb, &fl->secid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | xfrm_policy_put_afinfo(afinfo); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1566 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | } |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 1568 | EXPORT_SYMBOL(xfrm_decode_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1570 | static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | { |
| 1572 | for (; k < sp->len; k++) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1573 | if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 1574 | *idxp = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | return 1; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, |
| 1583 | unsigned short family) |
| 1584 | { |
| 1585 | struct xfrm_policy *pol; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1586 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 1587 | int npols = 0; |
| 1588 | int xfrm_nr; |
| 1589 | int pi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | struct flowi fl; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1591 | u8 fl_dir = policy_to_flow_dir(dir); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1592 | int xerr_idx = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 1594 | if (xfrm_decode_session(skb, &fl, family) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | return 0; |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 1596 | nf_nat_decode_session(skb, &fl, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | |
| 1598 | /* First, check used SA against their selectors. */ |
| 1599 | if (skb->sp) { |
| 1600 | int i; |
| 1601 | |
| 1602 | for (i=skb->sp->len-1; i>=0; i--) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 1603 | struct xfrm_state *x = skb->sp->xvec[i]; |
| 1604 | if (!xfrm_selector_match(&x->sel, &fl, family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | pol = NULL; |
| 1610 | if (sk && sk->sk_policy[dir]) |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1611 | pol = xfrm_sk_policy_lookup(sk, dir, &fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
| 1613 | if (!pol) |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1614 | pol = flow_cache_lookup(&fl, family, fl_dir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | xfrm_policy_lookup); |
| 1616 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1617 | if (IS_ERR(pol)) |
| 1618 | return 0; |
| 1619 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1620 | if (!pol) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 1621 | if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1622 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | return 1; |
| 1626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | |
| 1628 | pol->curlft.use_time = (unsigned long)xtime.tv_sec; |
| 1629 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1630 | pols[0] = pol; |
| 1631 | npols ++; |
| 1632 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1633 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
| 1634 | pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, |
| 1635 | &fl, family, |
| 1636 | XFRM_POLICY_IN); |
| 1637 | if (pols[1]) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1638 | if (IS_ERR(pols[1])) |
| 1639 | return 0; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1640 | pols[1]->curlft.use_time = (unsigned long)xtime.tv_sec; |
| 1641 | npols ++; |
| 1642 | } |
| 1643 | } |
| 1644 | #endif |
| 1645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | if (pol->action == XFRM_POLICY_ALLOW) { |
| 1647 | struct sec_path *sp; |
| 1648 | static struct sec_path dummy; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1649 | struct xfrm_tmpl *tp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1650 | struct xfrm_tmpl *stp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1651 | struct xfrm_tmpl **tpp = tp; |
| 1652 | int ti = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | int i, k; |
| 1654 | |
| 1655 | if ((sp = skb->sp) == NULL) |
| 1656 | sp = &dummy; |
| 1657 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1658 | for (pi = 0; pi < npols; pi++) { |
| 1659 | if (pols[pi] != pol && |
| 1660 | pols[pi]->action != XFRM_POLICY_ALLOW) |
| 1661 | goto reject; |
| 1662 | if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) |
| 1663 | goto reject_error; |
| 1664 | for (i = 0; i < pols[pi]->xfrm_nr; i++) |
| 1665 | tpp[ti++] = &pols[pi]->xfrm_vec[i]; |
| 1666 | } |
| 1667 | xfrm_nr = ti; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1668 | if (npols > 1) { |
| 1669 | xfrm_tmpl_sort(stp, tpp, xfrm_nr, family); |
| 1670 | tpp = stp; |
| 1671 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | /* For each tunnel xfrm, find the first matching tmpl. |
| 1674 | * For each tmpl before that, find corresponding xfrm. |
| 1675 | * Order is _important_. Later we will implement |
| 1676 | * some barriers, but at the moment barriers |
| 1677 | * are implied between each two transformations. |
| 1678 | */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1679 | for (i = xfrm_nr-1, k = 0; i >= 0; i--) { |
| 1680 | k = xfrm_policy_ok(tpp[i], sp, k, family); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1681 | if (k < 0) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 1682 | if (k < -1) |
| 1683 | /* "-2 - errored_index" returned */ |
| 1684 | xerr_idx = -(2+k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | goto reject; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 1689 | if (secpath_has_nontransport(sp, k, &xerr_idx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | goto reject; |
| 1691 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1692 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | return 1; |
| 1694 | } |
| 1695 | |
| 1696 | reject: |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 1697 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1698 | reject_error: |
| 1699 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | EXPORT_SYMBOL(__xfrm_policy_check); |
| 1703 | |
| 1704 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 1705 | { |
| 1706 | struct flowi fl; |
| 1707 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 1708 | if (xfrm_decode_session(skb, &fl, family) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | return 0; |
| 1710 | |
| 1711 | return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0; |
| 1712 | } |
| 1713 | EXPORT_SYMBOL(__xfrm_route_forward); |
| 1714 | |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 1715 | /* Optimize later using cookies and generation ids. */ |
| 1716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie) |
| 1718 | { |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 1719 | /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete |
| 1720 | * to "-1" to force all XFRM destinations to get validated by |
| 1721 | * dst_ops->check on every use. We do this because when a |
| 1722 | * normal route referenced by an XFRM dst is obsoleted we do |
| 1723 | * not go looking around for all parent referencing XFRM dsts |
| 1724 | * so that we can invalidate them. It is just too much work. |
| 1725 | * Instead we make the checks here on every use. For example: |
| 1726 | * |
| 1727 | * XFRM dst A --> IPv4 dst X |
| 1728 | * |
| 1729 | * X is the "xdst->route" of A (X is also the "dst->path" of A |
| 1730 | * in this example). If X is marked obsolete, "A" will not |
| 1731 | * notice. That's what we are validating here via the |
| 1732 | * stale_bundle() check. |
| 1733 | * |
| 1734 | * When a policy's bundle is pruned, we dst_free() the XFRM |
| 1735 | * dst which causes it's ->obsolete field to be set to a |
| 1736 | * positive non-zero integer. If an XFRM dst has been pruned |
| 1737 | * like this, we want to force a new route lookup. |
David S. Miller | 399c180 | 2005-12-19 14:23:23 -0800 | [diff] [blame] | 1738 | */ |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 1739 | if (dst->obsolete < 0 && !stale_bundle(dst)) |
| 1740 | return dst; |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | return NULL; |
| 1743 | } |
| 1744 | |
| 1745 | static int stale_bundle(struct dst_entry *dst) |
| 1746 | { |
Venkat Yekkirala | 5b368e6 | 2006-10-05 15:42:18 -0500 | [diff] [blame^] | 1747 | return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 1750 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | while ((dst = dst->child) && dst->xfrm && dst->dev == dev) { |
| 1753 | dst->dev = &loopback_dev; |
| 1754 | dev_hold(&loopback_dev); |
| 1755 | dev_put(dev); |
| 1756 | } |
| 1757 | } |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 1758 | EXPORT_SYMBOL(xfrm_dst_ifdown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
| 1760 | static void xfrm_link_failure(struct sk_buff *skb) |
| 1761 | { |
| 1762 | /* Impossible. Such dst must be popped before reaches point of failure. */ |
| 1763 | return; |
| 1764 | } |
| 1765 | |
| 1766 | static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst) |
| 1767 | { |
| 1768 | if (dst) { |
| 1769 | if (dst->obsolete) { |
| 1770 | dst_release(dst); |
| 1771 | dst = NULL; |
| 1772 | } |
| 1773 | } |
| 1774 | return dst; |
| 1775 | } |
| 1776 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1777 | static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p) |
| 1778 | { |
| 1779 | struct dst_entry *dst, **dstp; |
| 1780 | |
| 1781 | write_lock(&pol->lock); |
| 1782 | dstp = &pol->bundles; |
| 1783 | while ((dst=*dstp) != NULL) { |
| 1784 | if (func(dst)) { |
| 1785 | *dstp = dst->next; |
| 1786 | dst->next = *gc_list_p; |
| 1787 | *gc_list_p = dst; |
| 1788 | } else { |
| 1789 | dstp = &dst->next; |
| 1790 | } |
| 1791 | } |
| 1792 | write_unlock(&pol->lock); |
| 1793 | } |
| 1794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | static void xfrm_prune_bundles(int (*func)(struct dst_entry *)) |
| 1796 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1797 | struct dst_entry *gc_list = NULL; |
| 1798 | int dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
| 1800 | read_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1801 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
| 1802 | struct xfrm_policy *pol; |
| 1803 | struct hlist_node *entry; |
| 1804 | struct hlist_head *table; |
| 1805 | int i; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1806 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1807 | hlist_for_each_entry(pol, entry, |
| 1808 | &xfrm_policy_inexact[dir], bydst) |
| 1809 | prune_one_bundle(pol, func, &gc_list); |
| 1810 | |
| 1811 | table = xfrm_policy_bydst[dir].table; |
| 1812 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
| 1813 | hlist_for_each_entry(pol, entry, table + i, bydst) |
| 1814 | prune_one_bundle(pol, func, &gc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | read_unlock_bh(&xfrm_policy_lock); |
| 1818 | |
| 1819 | while (gc_list) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1820 | struct dst_entry *dst = gc_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | gc_list = dst->next; |
| 1822 | dst_free(dst); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | static int unused_bundle(struct dst_entry *dst) |
| 1827 | { |
| 1828 | return !atomic_read(&dst->__refcnt); |
| 1829 | } |
| 1830 | |
| 1831 | static void __xfrm_garbage_collect(void) |
| 1832 | { |
| 1833 | xfrm_prune_bundles(unused_bundle); |
| 1834 | } |
| 1835 | |
David S. Miller | 1c09539 | 2006-08-24 03:30:28 -0700 | [diff] [blame] | 1836 | static int xfrm_flush_bundles(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | { |
| 1838 | xfrm_prune_bundles(stale_bundle); |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
| 1842 | void xfrm_init_pmtu(struct dst_entry *dst) |
| 1843 | { |
| 1844 | do { |
| 1845 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 1846 | u32 pmtu, route_mtu_cached; |
| 1847 | |
| 1848 | pmtu = dst_mtu(dst->child); |
| 1849 | xdst->child_mtu_cached = pmtu; |
| 1850 | |
| 1851 | pmtu = xfrm_state_mtu(dst->xfrm, pmtu); |
| 1852 | |
| 1853 | route_mtu_cached = dst_mtu(xdst->route); |
| 1854 | xdst->route_mtu_cached = route_mtu_cached; |
| 1855 | |
| 1856 | if (pmtu > route_mtu_cached) |
| 1857 | pmtu = route_mtu_cached; |
| 1858 | |
| 1859 | dst->metrics[RTAX_MTU-1] = pmtu; |
| 1860 | } while ((dst = dst->next)); |
| 1861 | } |
| 1862 | |
| 1863 | EXPORT_SYMBOL(xfrm_init_pmtu); |
| 1864 | |
| 1865 | /* Check that the bundle accepts the flow and its components are |
| 1866 | * still valid. |
| 1867 | */ |
| 1868 | |
Venkat Yekkirala | 5b368e6 | 2006-10-05 15:42:18 -0500 | [diff] [blame^] | 1869 | int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first, |
| 1870 | struct flowi *fl, int family, int strict) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | { |
| 1872 | struct dst_entry *dst = &first->u.dst; |
| 1873 | struct xfrm_dst *last; |
| 1874 | u32 mtu; |
| 1875 | |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 1876 | if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | (dst->dev && !netif_running(dst->dev))) |
| 1878 | return 0; |
| 1879 | |
| 1880 | last = NULL; |
| 1881 | |
| 1882 | do { |
| 1883 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 1884 | |
| 1885 | if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family)) |
| 1886 | return 0; |
Venkat Yekkirala | 5b368e6 | 2006-10-05 15:42:18 -0500 | [diff] [blame^] | 1887 | if (fl && !security_xfrm_flow_state_match(fl, dst->xfrm, pol)) |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 1888 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | if (dst->xfrm->km.state != XFRM_STATE_VALID) |
| 1890 | return 0; |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 1891 | if (xdst->genid != dst->xfrm->genid) |
| 1892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
Masahide NAKAMURA | e53820d | 2006-08-23 19:12:01 -0700 | [diff] [blame] | 1894 | if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL && |
| 1895 | !xfrm_state_addr_flow_check(dst->xfrm, fl, family)) |
| 1896 | return 0; |
| 1897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | mtu = dst_mtu(dst->child); |
| 1899 | if (xdst->child_mtu_cached != mtu) { |
| 1900 | last = xdst; |
| 1901 | xdst->child_mtu_cached = mtu; |
| 1902 | } |
| 1903 | |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 1904 | if (!dst_check(xdst->route, xdst->route_cookie)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | return 0; |
| 1906 | mtu = dst_mtu(xdst->route); |
| 1907 | if (xdst->route_mtu_cached != mtu) { |
| 1908 | last = xdst; |
| 1909 | xdst->route_mtu_cached = mtu; |
| 1910 | } |
| 1911 | |
| 1912 | dst = dst->child; |
| 1913 | } while (dst->xfrm); |
| 1914 | |
| 1915 | if (likely(!last)) |
| 1916 | return 1; |
| 1917 | |
| 1918 | mtu = last->child_mtu_cached; |
| 1919 | for (;;) { |
| 1920 | dst = &last->u.dst; |
| 1921 | |
| 1922 | mtu = xfrm_state_mtu(dst->xfrm, mtu); |
| 1923 | if (mtu > last->route_mtu_cached) |
| 1924 | mtu = last->route_mtu_cached; |
| 1925 | dst->metrics[RTAX_MTU-1] = mtu; |
| 1926 | |
| 1927 | if (last == first) |
| 1928 | break; |
| 1929 | |
| 1930 | last = last->u.next; |
| 1931 | last->child_mtu_cached = mtu; |
| 1932 | } |
| 1933 | |
| 1934 | return 1; |
| 1935 | } |
| 1936 | |
| 1937 | EXPORT_SYMBOL(xfrm_bundle_ok); |
| 1938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 1940 | { |
| 1941 | int err = 0; |
| 1942 | if (unlikely(afinfo == NULL)) |
| 1943 | return -EINVAL; |
| 1944 | if (unlikely(afinfo->family >= NPROTO)) |
| 1945 | return -EAFNOSUPPORT; |
Ingo Molnar | e959d81 | 2006-04-28 15:32:29 -0700 | [diff] [blame] | 1946 | write_lock_bh(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL)) |
| 1948 | err = -ENOBUFS; |
| 1949 | else { |
| 1950 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 1951 | if (likely(dst_ops->kmem_cachep == NULL)) |
| 1952 | dst_ops->kmem_cachep = xfrm_dst_cache; |
| 1953 | if (likely(dst_ops->check == NULL)) |
| 1954 | dst_ops->check = xfrm_dst_check; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | if (likely(dst_ops->negative_advice == NULL)) |
| 1956 | dst_ops->negative_advice = xfrm_negative_advice; |
| 1957 | if (likely(dst_ops->link_failure == NULL)) |
| 1958 | dst_ops->link_failure = xfrm_link_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | if (likely(afinfo->garbage_collect == NULL)) |
| 1960 | afinfo->garbage_collect = __xfrm_garbage_collect; |
| 1961 | xfrm_policy_afinfo[afinfo->family] = afinfo; |
| 1962 | } |
Ingo Molnar | e959d81 | 2006-04-28 15:32:29 -0700 | [diff] [blame] | 1963 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | return err; |
| 1965 | } |
| 1966 | EXPORT_SYMBOL(xfrm_policy_register_afinfo); |
| 1967 | |
| 1968 | int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 1969 | { |
| 1970 | int err = 0; |
| 1971 | if (unlikely(afinfo == NULL)) |
| 1972 | return -EINVAL; |
| 1973 | if (unlikely(afinfo->family >= NPROTO)) |
| 1974 | return -EAFNOSUPPORT; |
Ingo Molnar | e959d81 | 2006-04-28 15:32:29 -0700 | [diff] [blame] | 1975 | write_lock_bh(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) { |
| 1977 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo)) |
| 1978 | err = -EINVAL; |
| 1979 | else { |
| 1980 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 1981 | xfrm_policy_afinfo[afinfo->family] = NULL; |
| 1982 | dst_ops->kmem_cachep = NULL; |
| 1983 | dst_ops->check = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | dst_ops->negative_advice = NULL; |
| 1985 | dst_ops->link_failure = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | afinfo->garbage_collect = NULL; |
| 1987 | } |
| 1988 | } |
Ingo Molnar | e959d81 | 2006-04-28 15:32:29 -0700 | [diff] [blame] | 1989 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | return err; |
| 1991 | } |
| 1992 | EXPORT_SYMBOL(xfrm_policy_unregister_afinfo); |
| 1993 | |
| 1994 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family) |
| 1995 | { |
| 1996 | struct xfrm_policy_afinfo *afinfo; |
| 1997 | if (unlikely(family >= NPROTO)) |
| 1998 | return NULL; |
| 1999 | read_lock(&xfrm_policy_afinfo_lock); |
| 2000 | afinfo = xfrm_policy_afinfo[family]; |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 2001 | if (unlikely(!afinfo)) |
| 2002 | read_unlock(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | return afinfo; |
| 2004 | } |
| 2005 | |
| 2006 | static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2007 | { |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 2008 | read_unlock(&xfrm_policy_afinfo_lock); |
| 2009 | } |
| 2010 | |
| 2011 | static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family) |
| 2012 | { |
| 2013 | struct xfrm_policy_afinfo *afinfo; |
| 2014 | if (unlikely(family >= NPROTO)) |
| 2015 | return NULL; |
| 2016 | write_lock_bh(&xfrm_policy_afinfo_lock); |
| 2017 | afinfo = xfrm_policy_afinfo[family]; |
| 2018 | if (unlikely(!afinfo)) |
| 2019 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
| 2020 | return afinfo; |
| 2021 | } |
| 2022 | |
| 2023 | static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2024 | { |
| 2025 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 2029 | { |
| 2030 | switch (event) { |
| 2031 | case NETDEV_DOWN: |
| 2032 | xfrm_flush_bundles(); |
| 2033 | } |
| 2034 | return NOTIFY_DONE; |
| 2035 | } |
| 2036 | |
| 2037 | static struct notifier_block xfrm_dev_notifier = { |
| 2038 | xfrm_dev_event, |
| 2039 | NULL, |
| 2040 | 0 |
| 2041 | }; |
| 2042 | |
| 2043 | static void __init xfrm_policy_init(void) |
| 2044 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2045 | unsigned int hmask, sz; |
| 2046 | int dir; |
| 2047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache", |
| 2049 | sizeof(struct xfrm_dst), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2050 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2053 | hmask = 8 - 1; |
| 2054 | sz = (hmask+1) * sizeof(struct hlist_head); |
| 2055 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 2056 | xfrm_policy_byidx = xfrm_hash_alloc(sz); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2057 | xfrm_idx_hmask = hmask; |
| 2058 | if (!xfrm_policy_byidx) |
| 2059 | panic("XFRM: failed to allocate byidx hash\n"); |
| 2060 | |
| 2061 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
| 2062 | struct xfrm_policy_hash *htab; |
| 2063 | |
| 2064 | INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]); |
| 2065 | |
| 2066 | htab = &xfrm_policy_bydst[dir]; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 2067 | htab->table = xfrm_hash_alloc(sz); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2068 | htab->hmask = hmask; |
| 2069 | if (!htab->table) |
| 2070 | panic("XFRM: failed to allocate bydst hash\n"); |
| 2071 | } |
| 2072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task, NULL); |
| 2074 | register_netdevice_notifier(&xfrm_dev_notifier); |
| 2075 | } |
| 2076 | |
| 2077 | void __init xfrm_init(void) |
| 2078 | { |
| 2079 | xfrm_state_init(); |
| 2080 | xfrm_policy_init(); |
| 2081 | xfrm_input_init(); |
| 2082 | } |
| 2083 | |