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