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