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