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