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> |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 27 | #include <linux/audit.h> |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 28 | #include <net/dst.h> |
Eric Paris | 6ce74ec | 2012-02-16 15:08:39 -0500 | [diff] [blame] | 29 | #include <net/flow.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <net/xfrm.h> |
| 31 | #include <net/ip.h> |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 32 | #ifdef CONFIG_XFRM_STATISTICS |
| 33 | #include <net/snmp.h> |
| 34 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 36 | #include "xfrm_hash.h" |
| 37 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 38 | #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10)) |
| 39 | #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ)) |
| 40 | #define XFRM_MAX_QUEUE_LEN 100 |
| 41 | |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 42 | DEFINE_MUTEX(xfrm_cfg_mutex); |
| 43 | EXPORT_SYMBOL(xfrm_cfg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock); |
| 46 | static struct dst_entry *xfrm_policy_sk_bundles; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static DEFINE_RWLOCK(xfrm_policy_lock); |
| 48 | |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 49 | static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock); |
| 50 | static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO] |
| 51 | __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 53 | static struct kmem_cache *xfrm_dst_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 55 | static void xfrm_init_pmtu(struct dst_entry *dst); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 56 | static int stale_bundle(struct dst_entry *dst); |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 57 | static int xfrm_bundle_ok(struct xfrm_dst *xdst); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 58 | static void xfrm_policy_queue_process(unsigned long arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 60 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 61 | int dir); |
| 62 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 63 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 64 | __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 65 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 66 | const struct flowi4 *fl4 = &fl->u.ip4; |
| 67 | |
Alexey Dobriyan | 26bff94 | 2011-11-22 06:46:02 +0000 | [diff] [blame] | 68 | return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) && |
| 69 | addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) && |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 70 | !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) && |
| 71 | !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) && |
| 72 | (fl4->flowi4_proto == sel->proto || !sel->proto) && |
| 73 | (fl4->flowi4_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 74 | } |
| 75 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 76 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 77 | __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 78 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 79 | const struct flowi6 *fl6 = &fl->u.ip6; |
| 80 | |
| 81 | return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) && |
| 82 | addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) && |
| 83 | !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) && |
| 84 | !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) && |
| 85 | (fl6->flowi6_proto == sel->proto || !sel->proto) && |
| 86 | (fl6->flowi6_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 87 | } |
| 88 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 89 | bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl, |
| 90 | unsigned short family) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 91 | { |
| 92 | switch (family) { |
| 93 | case AF_INET: |
| 94 | return __xfrm4_selector_match(sel, fl); |
| 95 | case AF_INET6: |
| 96 | return __xfrm6_selector_match(sel, fl); |
| 97 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 98 | return false; |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 101 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family) |
| 102 | { |
| 103 | struct xfrm_policy_afinfo *afinfo; |
| 104 | |
| 105 | if (unlikely(family >= NPROTO)) |
| 106 | return NULL; |
| 107 | rcu_read_lock(); |
| 108 | afinfo = rcu_dereference(xfrm_policy_afinfo[family]); |
| 109 | if (unlikely(!afinfo)) |
| 110 | rcu_read_unlock(); |
| 111 | return afinfo; |
| 112 | } |
| 113 | |
| 114 | static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 115 | { |
| 116 | rcu_read_unlock(); |
| 117 | } |
| 118 | |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 119 | static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, |
David S. Miller | 6418c4e | 2011-02-24 00:16:53 -0500 | [diff] [blame] | 120 | const xfrm_address_t *saddr, |
| 121 | const xfrm_address_t *daddr, |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 122 | int family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 124 | struct xfrm_policy_afinfo *afinfo; |
| 125 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 127 | afinfo = xfrm_policy_get_afinfo(family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (unlikely(afinfo == NULL)) |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 129 | return ERR_PTR(-EAFNOSUPPORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 131 | dst = afinfo->dst_lookup(net, tos, saddr, daddr); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | xfrm_policy_put_afinfo(afinfo); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 134 | |
| 135 | return dst; |
| 136 | } |
| 137 | |
| 138 | static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos, |
| 139 | xfrm_address_t *prev_saddr, |
| 140 | xfrm_address_t *prev_daddr, |
| 141 | int family) |
| 142 | { |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 143 | struct net *net = xs_net(x); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 144 | xfrm_address_t *saddr = &x->props.saddr; |
| 145 | xfrm_address_t *daddr = &x->id.daddr; |
| 146 | struct dst_entry *dst; |
| 147 | |
| 148 | if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) { |
| 149 | saddr = x->coaddr; |
| 150 | daddr = prev_daddr; |
| 151 | } |
| 152 | if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) { |
| 153 | saddr = prev_saddr; |
| 154 | daddr = x->coaddr; |
| 155 | } |
| 156 | |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 157 | dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 158 | |
| 159 | if (!IS_ERR(dst)) { |
| 160 | if (prev_saddr != saddr) |
| 161 | memcpy(prev_saddr, saddr, sizeof(*prev_saddr)); |
| 162 | if (prev_daddr != daddr) |
| 163 | memcpy(prev_daddr, daddr, sizeof(*prev_daddr)); |
| 164 | } |
| 165 | |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 166 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | static inline unsigned long make_jiffies(long secs) |
| 170 | { |
| 171 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 172 | return MAX_SCHEDULE_TIMEOUT-1; |
| 173 | else |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 174 | return secs*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void xfrm_policy_timer(unsigned long data) |
| 178 | { |
| 179 | struct xfrm_policy *xp = (struct xfrm_policy*)data; |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 180 | unsigned long now = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | long next = LONG_MAX; |
| 182 | int warn = 0; |
| 183 | int dir; |
| 184 | |
| 185 | read_lock(&xp->lock); |
| 186 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 187 | if (unlikely(xp->walk.dead)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | goto out; |
| 189 | |
Herbert Xu | 77d8d7a | 2005-10-05 12:15:12 -0700 | [diff] [blame] | 190 | dir = xfrm_policy_id2dir(xp->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
| 192 | if (xp->lft.hard_add_expires_seconds) { |
| 193 | long tmo = xp->lft.hard_add_expires_seconds + |
| 194 | xp->curlft.add_time - now; |
| 195 | if (tmo <= 0) |
| 196 | goto expired; |
| 197 | if (tmo < next) |
| 198 | next = tmo; |
| 199 | } |
| 200 | if (xp->lft.hard_use_expires_seconds) { |
| 201 | long tmo = xp->lft.hard_use_expires_seconds + |
| 202 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 203 | if (tmo <= 0) |
| 204 | goto expired; |
| 205 | if (tmo < next) |
| 206 | next = tmo; |
| 207 | } |
| 208 | if (xp->lft.soft_add_expires_seconds) { |
| 209 | long tmo = xp->lft.soft_add_expires_seconds + |
| 210 | xp->curlft.add_time - now; |
| 211 | if (tmo <= 0) { |
| 212 | warn = 1; |
| 213 | tmo = XFRM_KM_TIMEOUT; |
| 214 | } |
| 215 | if (tmo < next) |
| 216 | next = tmo; |
| 217 | } |
| 218 | if (xp->lft.soft_use_expires_seconds) { |
| 219 | long tmo = xp->lft.soft_use_expires_seconds + |
| 220 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 221 | if (tmo <= 0) { |
| 222 | warn = 1; |
| 223 | tmo = XFRM_KM_TIMEOUT; |
| 224 | } |
| 225 | if (tmo < next) |
| 226 | next = tmo; |
| 227 | } |
| 228 | |
| 229 | if (warn) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 230 | km_policy_expired(xp, dir, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (next != LONG_MAX && |
| 232 | !mod_timer(&xp->timer, jiffies + make_jiffies(next))) |
| 233 | xfrm_pol_hold(xp); |
| 234 | |
| 235 | out: |
| 236 | read_unlock(&xp->lock); |
| 237 | xfrm_pol_put(xp); |
| 238 | return; |
| 239 | |
| 240 | expired: |
| 241 | read_unlock(&xp->lock); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 242 | if (!xfrm_policy_delete(xp, dir)) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 243 | km_policy_expired(xp, dir, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | xfrm_pol_put(xp); |
| 245 | } |
| 246 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 247 | static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo) |
| 248 | { |
| 249 | struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo); |
| 250 | |
| 251 | if (unlikely(pol->walk.dead)) |
| 252 | flo = NULL; |
| 253 | else |
| 254 | xfrm_pol_hold(pol); |
| 255 | |
| 256 | return flo; |
| 257 | } |
| 258 | |
| 259 | static int xfrm_policy_flo_check(struct flow_cache_object *flo) |
| 260 | { |
| 261 | struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo); |
| 262 | |
| 263 | return !pol->walk.dead; |
| 264 | } |
| 265 | |
| 266 | static void xfrm_policy_flo_delete(struct flow_cache_object *flo) |
| 267 | { |
| 268 | xfrm_pol_put(container_of(flo, struct xfrm_policy, flo)); |
| 269 | } |
| 270 | |
| 271 | static const struct flow_cache_ops xfrm_policy_fc_ops = { |
| 272 | .get = xfrm_policy_flo_get, |
| 273 | .check = xfrm_policy_flo_check, |
| 274 | .delete = xfrm_policy_flo_delete, |
| 275 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2 |
| 278 | * SPD calls. |
| 279 | */ |
| 280 | |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 281 | struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | struct xfrm_policy *policy; |
| 284 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 285 | policy = kzalloc(sizeof(struct xfrm_policy), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (policy) { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 288 | write_pnet(&policy->xp_net, net); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 289 | INIT_LIST_HEAD(&policy->walk.all); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 290 | INIT_HLIST_NODE(&policy->bydst); |
| 291 | INIT_HLIST_NODE(&policy->byidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | rwlock_init(&policy->lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 293 | atomic_set(&policy->refcnt, 1); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 294 | skb_queue_head_init(&policy->polq.hold_queue); |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 295 | setup_timer(&policy->timer, xfrm_policy_timer, |
| 296 | (unsigned long)policy); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 297 | setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process, |
| 298 | (unsigned long)policy); |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 299 | policy->flo.ops = &xfrm_policy_fc_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | return policy; |
| 302 | } |
| 303 | EXPORT_SYMBOL(xfrm_policy_alloc); |
| 304 | |
| 305 | /* Destroy xfrm_policy: descendant resources must be released to this moment. */ |
| 306 | |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 307 | void xfrm_policy_destroy(struct xfrm_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 309 | BUG_ON(!policy->walk.dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (del_timer(&policy->timer)) |
| 312 | BUG(); |
| 313 | |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 314 | security_xfrm_policy_free(policy->security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | kfree(policy); |
| 316 | } |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 317 | EXPORT_SYMBOL(xfrm_policy_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 319 | static void xfrm_queue_purge(struct sk_buff_head *list) |
| 320 | { |
| 321 | struct sk_buff *skb; |
| 322 | |
| 323 | while ((skb = skb_dequeue(list)) != NULL) { |
| 324 | dev_put(skb->dev); |
| 325 | kfree_skb(skb); |
| 326 | } |
| 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | /* Rule must be locked. Release descentant resources, announce |
| 330 | * entry dead. The rule must be unlinked from lists to the moment. |
| 331 | */ |
| 332 | |
| 333 | static void xfrm_policy_kill(struct xfrm_policy *policy) |
| 334 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 335 | policy->walk.dead = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 337 | atomic_inc(&policy->genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 339 | del_timer(&policy->polq.hold_timer); |
| 340 | xfrm_queue_purge(&policy->polq.hold_queue); |
| 341 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 342 | if (del_timer(&policy->timer)) |
| 343 | xfrm_pol_put(policy); |
| 344 | |
| 345 | xfrm_pol_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 348 | static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024; |
| 349 | |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 350 | static inline unsigned int idx_hash(struct net *net, u32 index) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 351 | { |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 352 | return __idx_hash(index, net->xfrm.policy_idx_hmask); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 353 | } |
| 354 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 355 | static struct hlist_head *policy_hash_bysel(struct net *net, |
| 356 | const struct xfrm_selector *sel, |
| 357 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 358 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 359 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 360 | unsigned int hash = __sel_hash(sel, family, hmask); |
| 361 | |
| 362 | return (hash == hmask + 1 ? |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 363 | &net->xfrm.policy_inexact[dir] : |
| 364 | net->xfrm.policy_bydst[dir].table + hash); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 365 | } |
| 366 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 367 | static struct hlist_head *policy_hash_direct(struct net *net, |
| 368 | const xfrm_address_t *daddr, |
| 369 | const xfrm_address_t *saddr, |
| 370 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 371 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 372 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 373 | unsigned int hash = __addr_hash(daddr, saddr, family, hmask); |
| 374 | |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 375 | return net->xfrm.policy_bydst[dir].table + hash; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 376 | } |
| 377 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 378 | static void xfrm_dst_hash_transfer(struct hlist_head *list, |
| 379 | struct hlist_head *ndsttable, |
| 380 | unsigned int nhashmask) |
| 381 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 382 | struct hlist_node *tmp, *entry0 = NULL; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 383 | struct xfrm_policy *pol; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 384 | unsigned int h0 = 0; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 385 | |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 386 | redo: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 387 | hlist_for_each_entry_safe(pol, tmp, list, bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 388 | unsigned int h; |
| 389 | |
| 390 | h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr, |
| 391 | pol->family, nhashmask); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 392 | if (!entry0) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 393 | hlist_del(&pol->bydst); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 394 | hlist_add_head(&pol->bydst, ndsttable+h); |
| 395 | h0 = h; |
| 396 | } else { |
| 397 | if (h != h0) |
| 398 | continue; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 399 | hlist_del(&pol->bydst); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 400 | hlist_add_after(entry0, &pol->bydst); |
| 401 | } |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 402 | entry0 = &pol->bydst; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 403 | } |
| 404 | if (!hlist_empty(list)) { |
| 405 | entry0 = NULL; |
| 406 | goto redo; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | static void xfrm_idx_hash_transfer(struct hlist_head *list, |
| 411 | struct hlist_head *nidxtable, |
| 412 | unsigned int nhashmask) |
| 413 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 414 | struct hlist_node *tmp; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 415 | struct xfrm_policy *pol; |
| 416 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 417 | hlist_for_each_entry_safe(pol, tmp, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 418 | unsigned int h; |
| 419 | |
| 420 | h = __idx_hash(pol->index, nhashmask); |
| 421 | hlist_add_head(&pol->byidx, nidxtable+h); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static unsigned long xfrm_new_hash_mask(unsigned int old_hmask) |
| 426 | { |
| 427 | return ((old_hmask + 1) << 1) - 1; |
| 428 | } |
| 429 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 430 | static void xfrm_bydst_resize(struct net *net, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 431 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 432 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 433 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 434 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 435 | struct hlist_head *odst = net->xfrm.policy_bydst[dir].table; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 436 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 437 | int i; |
| 438 | |
| 439 | if (!ndst) |
| 440 | return; |
| 441 | |
| 442 | write_lock_bh(&xfrm_policy_lock); |
| 443 | |
| 444 | for (i = hmask; i >= 0; i--) |
| 445 | xfrm_dst_hash_transfer(odst + i, ndst, nhashmask); |
| 446 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 447 | net->xfrm.policy_bydst[dir].table = ndst; |
| 448 | net->xfrm.policy_bydst[dir].hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 449 | |
| 450 | write_unlock_bh(&xfrm_policy_lock); |
| 451 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 452 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 455 | static void xfrm_byidx_resize(struct net *net, int total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 456 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 457 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 458 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 459 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 460 | struct hlist_head *oidx = net->xfrm.policy_byidx; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 461 | struct hlist_head *nidx = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 462 | int i; |
| 463 | |
| 464 | if (!nidx) |
| 465 | return; |
| 466 | |
| 467 | write_lock_bh(&xfrm_policy_lock); |
| 468 | |
| 469 | for (i = hmask; i >= 0; i--) |
| 470 | xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask); |
| 471 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 472 | net->xfrm.policy_byidx = nidx; |
| 473 | net->xfrm.policy_idx_hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 474 | |
| 475 | write_unlock_bh(&xfrm_policy_lock); |
| 476 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 477 | xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 480 | static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 481 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 482 | unsigned int cnt = net->xfrm.policy_count[dir]; |
| 483 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 484 | |
| 485 | if (total) |
| 486 | *total += cnt; |
| 487 | |
| 488 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 489 | cnt > hmask) |
| 490 | return 1; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 495 | static inline int xfrm_byidx_should_resize(struct net *net, int total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 496 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 497 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 498 | |
| 499 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 500 | total > hmask) |
| 501 | return 1; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 506 | void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si) |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 507 | { |
| 508 | read_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 509 | si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN]; |
| 510 | si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT]; |
| 511 | si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD]; |
| 512 | si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX]; |
| 513 | si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX]; |
| 514 | si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX]; |
| 515 | si->spdhcnt = net->xfrm.policy_idx_hmask; |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 516 | si->spdhmcnt = xfrm_policy_hashmax; |
| 517 | read_unlock_bh(&xfrm_policy_lock); |
| 518 | } |
| 519 | EXPORT_SYMBOL(xfrm_spd_getinfo); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 520 | |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 521 | static DEFINE_MUTEX(hash_resize_mutex); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 522 | static void xfrm_hash_resize(struct work_struct *work) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 523 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 524 | struct net *net = container_of(work, struct net, xfrm.policy_hash_work); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 525 | int dir, total; |
| 526 | |
| 527 | mutex_lock(&hash_resize_mutex); |
| 528 | |
| 529 | total = 0; |
| 530 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 531 | if (xfrm_bydst_should_resize(net, dir, &total)) |
| 532 | xfrm_bydst_resize(net, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 533 | } |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 534 | if (xfrm_byidx_should_resize(net, total)) |
| 535 | xfrm_byidx_resize(net, total); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 536 | |
| 537 | mutex_unlock(&hash_resize_mutex); |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* Generate new index... KAME seems to generate them ordered by cost |
| 541 | * of an absolute inpredictability of ordering of rules. This will not pass. */ |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 542 | static u32 xfrm_gen_index(struct net *net, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | static u32 idx_generator; |
| 545 | |
| 546 | for (;;) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 547 | struct hlist_head *list; |
| 548 | struct xfrm_policy *p; |
| 549 | u32 idx; |
| 550 | int found; |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | idx = (idx_generator | dir); |
| 553 | idx_generator += 8; |
| 554 | if (idx == 0) |
| 555 | idx = 8; |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 556 | list = net->xfrm.policy_byidx + idx_hash(net, idx); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 557 | found = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 558 | hlist_for_each_entry(p, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 559 | if (p->index == idx) { |
| 560 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | break; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 564 | if (!found) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return idx; |
| 566 | } |
| 567 | } |
| 568 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 569 | static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2) |
| 570 | { |
| 571 | u32 *p1 = (u32 *) s1; |
| 572 | u32 *p2 = (u32 *) s2; |
| 573 | int len = sizeof(struct xfrm_selector) / sizeof(u32); |
| 574 | int i; |
| 575 | |
| 576 | for (i = 0; i < len; i++) { |
| 577 | if (p1[i] != p2[i]) |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 584 | static void xfrm_policy_requeue(struct xfrm_policy *old, |
| 585 | struct xfrm_policy *new) |
| 586 | { |
| 587 | struct xfrm_policy_queue *pq = &old->polq; |
| 588 | struct sk_buff_head list; |
| 589 | |
| 590 | __skb_queue_head_init(&list); |
| 591 | |
| 592 | spin_lock_bh(&pq->hold_queue.lock); |
| 593 | skb_queue_splice_init(&pq->hold_queue, &list); |
| 594 | del_timer(&pq->hold_timer); |
| 595 | spin_unlock_bh(&pq->hold_queue.lock); |
| 596 | |
| 597 | if (skb_queue_empty(&list)) |
| 598 | return; |
| 599 | |
| 600 | pq = &new->polq; |
| 601 | |
| 602 | spin_lock_bh(&pq->hold_queue.lock); |
| 603 | skb_queue_splice(&list, &pq->hold_queue); |
| 604 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
| 605 | mod_timer(&pq->hold_timer, jiffies); |
| 606 | spin_unlock_bh(&pq->hold_queue.lock); |
| 607 | } |
| 608 | |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 609 | static bool xfrm_policy_mark_match(struct xfrm_policy *policy, |
| 610 | struct xfrm_policy *pol) |
| 611 | { |
| 612 | u32 mark = policy->mark.v & policy->mark.m; |
| 613 | |
| 614 | if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m) |
| 615 | return true; |
| 616 | |
| 617 | if ((mark & pol->mark.m) == pol->mark.v && |
| 618 | policy->priority == pol->priority) |
| 619 | return true; |
| 620 | |
| 621 | return false; |
| 622 | } |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) |
| 625 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 626 | struct net *net = xp_net(policy); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 627 | struct xfrm_policy *pol; |
| 628 | struct xfrm_policy *delpol; |
| 629 | struct hlist_head *chain; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 630 | struct hlist_node *newpos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | write_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 633 | chain = policy_hash_bysel(net, &policy->selector, policy->family, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 634 | delpol = NULL; |
| 635 | newpos = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 636 | hlist_for_each_entry(pol, chain, bydst) { |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 637 | if (pol->type == policy->type && |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 638 | !selector_cmp(&pol->selector, &policy->selector) && |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 639 | xfrm_policy_mark_match(policy, pol) && |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 640 | xfrm_sec_ctx_match(pol->security, policy->security) && |
| 641 | !WARN_ON(delpol)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | if (excl) { |
| 643 | write_unlock_bh(&xfrm_policy_lock); |
| 644 | return -EEXIST; |
| 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | delpol = pol; |
| 647 | if (policy->priority > pol->priority) |
| 648 | continue; |
| 649 | } else if (policy->priority >= pol->priority) { |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 650 | newpos = &pol->bydst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | continue; |
| 652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (delpol) |
| 654 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | if (newpos) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 657 | hlist_add_after(newpos, &policy->bydst); |
| 658 | else |
| 659 | hlist_add_head(&policy->bydst, chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | xfrm_pol_hold(policy); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 661 | net->xfrm.policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | atomic_inc(&flow_cache_genid); |
Nicolas Dichtel | ee8372d | 2012-09-10 22:09:45 +0000 | [diff] [blame] | 663 | rt_genid_bump(net); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 664 | if (delpol) { |
| 665 | xfrm_policy_requeue(delpol, policy); |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 666 | __xfrm_policy_unlink(delpol, dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 667 | } |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 668 | policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir); |
| 669 | hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index)); |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 670 | policy->curlft.add_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | policy->curlft.use_time = 0; |
| 672 | if (!mod_timer(&policy->timer, jiffies + HZ)) |
| 673 | xfrm_pol_hold(policy); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 674 | list_add(&policy->walk.all, &net->xfrm.policy_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | write_unlock_bh(&xfrm_policy_lock); |
| 676 | |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 677 | if (delpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | xfrm_policy_kill(delpol); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 679 | else if (xfrm_bydst_should_resize(net, dir, NULL)) |
| 680 | schedule_work(&net->xfrm.policy_hash_work); |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | EXPORT_SYMBOL(xfrm_policy_insert); |
| 685 | |
Jamal Hadi Salim | 8ca2e93 | 2010-02-22 11:32:57 +0000 | [diff] [blame] | 686 | struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type, |
| 687 | int dir, struct xfrm_selector *sel, |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 688 | struct xfrm_sec_ctx *ctx, int delete, |
| 689 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 691 | struct xfrm_policy *pol, *ret; |
| 692 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 694 | *err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | write_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 696 | chain = policy_hash_bysel(net, sel, sel->family, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 697 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 698 | hlist_for_each_entry(pol, chain, bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 699 | if (pol->type == type && |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 700 | (mark & pol->mark.m) == pol->mark.v && |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 701 | !selector_cmp(sel, &pol->selector) && |
| 702 | xfrm_sec_ctx_match(ctx, pol->security)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 704 | if (delete) { |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 705 | *err = security_xfrm_policy_delete( |
| 706 | pol->security); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 707 | if (*err) { |
| 708 | write_unlock_bh(&xfrm_policy_lock); |
| 709 | return pol; |
| 710 | } |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 711 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 712 | } |
| 713 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | } |
| 717 | write_unlock_bh(&xfrm_policy_lock); |
| 718 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 719 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 720 | xfrm_policy_kill(ret); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 721 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 723 | EXPORT_SYMBOL(xfrm_policy_bysel_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Jamal Hadi Salim | 8ca2e93 | 2010-02-22 11:32:57 +0000 | [diff] [blame] | 725 | struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type, |
| 726 | int dir, u32 id, int delete, int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 728 | struct xfrm_policy *pol, *ret; |
| 729 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
Herbert Xu | b5505c6 | 2007-05-14 02:15:47 -0700 | [diff] [blame] | 731 | *err = -ENOENT; |
| 732 | if (xfrm_policy_id2dir(id) != dir) |
| 733 | return NULL; |
| 734 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 735 | *err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | write_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 737 | chain = net->xfrm.policy_byidx + idx_hash(net, id); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 738 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 739 | hlist_for_each_entry(pol, chain, byidx) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 740 | if (pol->type == type && pol->index == id && |
| 741 | (mark & pol->mark.m) == pol->mark.v) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 743 | if (delete) { |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 744 | *err = security_xfrm_policy_delete( |
| 745 | pol->security); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 746 | if (*err) { |
| 747 | write_unlock_bh(&xfrm_policy_lock); |
| 748 | return pol; |
| 749 | } |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 750 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 751 | } |
| 752 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | } |
| 756 | write_unlock_bh(&xfrm_policy_lock); |
| 757 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 758 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 759 | xfrm_policy_kill(ret); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 760 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | EXPORT_SYMBOL(xfrm_policy_byid); |
| 763 | |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 764 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 765 | static inline int |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 766 | xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 768 | int dir, err = 0; |
| 769 | |
| 770 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
| 771 | struct xfrm_policy *pol; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 772 | int i; |
| 773 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 774 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 775 | &net->xfrm.policy_inexact[dir], bydst) { |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 776 | if (pol->type != type) |
| 777 | continue; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 778 | err = security_xfrm_policy_delete(pol->security); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 779 | if (err) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 780 | xfrm_audit_policy_delete(pol, 0, |
| 781 | audit_info->loginuid, |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 782 | audit_info->sessionid, |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 783 | audit_info->secid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 784 | return err; |
| 785 | } |
YOSHIFUJI Hideaki | 7dc12d6 | 2007-07-19 10:45:15 +0900 | [diff] [blame] | 786 | } |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 787 | for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 788 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 789 | net->xfrm.policy_bydst[dir].table + i, |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 790 | bydst) { |
| 791 | if (pol->type != type) |
| 792 | continue; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 793 | err = security_xfrm_policy_delete( |
| 794 | pol->security); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 795 | if (err) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 796 | xfrm_audit_policy_delete(pol, 0, |
| 797 | audit_info->loginuid, |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 798 | audit_info->sessionid, |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 799 | audit_info->secid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 800 | return err; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | return err; |
| 806 | } |
| 807 | #else |
| 808 | static inline int |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 809 | xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 810 | { |
| 811 | return 0; |
| 812 | } |
| 813 | #endif |
| 814 | |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 815 | int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 816 | { |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 817 | int dir, err = 0, cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | write_lock_bh(&xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 820 | |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 821 | err = xfrm_policy_flush_secctx_check(net, type, audit_info); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 822 | if (err) |
| 823 | goto out; |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 826 | struct xfrm_policy *pol; |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 827 | int i; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 828 | |
| 829 | again1: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 830 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 831 | &net->xfrm.policy_inexact[dir], bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 832 | if (pol->type != type) |
| 833 | continue; |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 834 | __xfrm_policy_unlink(pol, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | write_unlock_bh(&xfrm_policy_lock); |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 836 | cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 838 | xfrm_audit_policy_delete(pol, 1, audit_info->loginuid, |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 839 | audit_info->sessionid, |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 840 | audit_info->secid); |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 841 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 842 | xfrm_policy_kill(pol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | write_lock_bh(&xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 845 | goto again1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 847 | |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 848 | for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 849 | again2: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 850 | hlist_for_each_entry(pol, |
Alexey Dobriyan | 33ffbbd | 2008-11-25 17:33:32 -0800 | [diff] [blame] | 851 | net->xfrm.policy_bydst[dir].table + i, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 852 | bydst) { |
| 853 | if (pol->type != type) |
| 854 | continue; |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 855 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 856 | write_unlock_bh(&xfrm_policy_lock); |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 857 | cnt++; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 858 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 859 | xfrm_audit_policy_delete(pol, 1, |
| 860 | audit_info->loginuid, |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 861 | audit_info->sessionid, |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 862 | audit_info->secid); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 863 | xfrm_policy_kill(pol); |
| 864 | |
| 865 | write_lock_bh(&xfrm_policy_lock); |
| 866 | goto again2; |
| 867 | } |
| 868 | } |
| 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 871 | if (!cnt) |
| 872 | err = -ESRCH; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 873 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | write_unlock_bh(&xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 875 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | EXPORT_SYMBOL(xfrm_policy_flush); |
| 878 | |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 879 | int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 880 | int (*func)(struct xfrm_policy *, int, int, void*), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | void *data) |
| 882 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 883 | struct xfrm_policy *pol; |
| 884 | struct xfrm_policy_walk_entry *x; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 885 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 887 | if (walk->type >= XFRM_POLICY_TYPE_MAX && |
| 888 | walk->type != XFRM_POLICY_TYPE_ANY) |
| 889 | return -EINVAL; |
| 890 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 891 | if (list_empty(&walk->walk.all) && walk->seq != 0) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 892 | return 0; |
| 893 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 894 | write_lock_bh(&xfrm_policy_lock); |
| 895 | if (list_empty(&walk->walk.all)) |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 896 | x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 897 | else |
| 898 | x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all); |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 899 | list_for_each_entry_from(x, &net->xfrm.policy_all, all) { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 900 | if (x->dead) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 901 | continue; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 902 | pol = container_of(x, struct xfrm_policy, walk); |
| 903 | if (walk->type != XFRM_POLICY_TYPE_ANY && |
| 904 | walk->type != pol->type) |
| 905 | continue; |
| 906 | error = func(pol, xfrm_policy_id2dir(pol->index), |
| 907 | walk->seq, data); |
| 908 | if (error) { |
| 909 | list_move_tail(&walk->walk.all, &x->all); |
| 910 | goto out; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 911 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 912 | walk->seq++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 914 | if (walk->seq == 0) { |
Jamal Hadi Salim | baf5d74 | 2006-12-04 20:02:37 -0800 | [diff] [blame] | 915 | error = -ENOENT; |
| 916 | goto out; |
| 917 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 918 | list_del_init(&walk->walk.all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | out: |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 920 | write_unlock_bh(&xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return error; |
| 922 | } |
| 923 | EXPORT_SYMBOL(xfrm_policy_walk); |
| 924 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 925 | void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type) |
| 926 | { |
| 927 | INIT_LIST_HEAD(&walk->walk.all); |
| 928 | walk->walk.dead = 1; |
| 929 | walk->type = type; |
| 930 | walk->seq = 0; |
| 931 | } |
| 932 | EXPORT_SYMBOL(xfrm_policy_walk_init); |
| 933 | |
| 934 | void xfrm_policy_walk_done(struct xfrm_policy_walk *walk) |
| 935 | { |
| 936 | if (list_empty(&walk->walk.all)) |
| 937 | return; |
| 938 | |
| 939 | write_lock_bh(&xfrm_policy_lock); |
| 940 | list_del(&walk->walk.all); |
| 941 | write_unlock_bh(&xfrm_policy_lock); |
| 942 | } |
| 943 | EXPORT_SYMBOL(xfrm_policy_walk_done); |
| 944 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 945 | /* |
| 946 | * Find policy to apply to this flow. |
| 947 | * |
| 948 | * Returns 0 if policy found, else an -errno. |
| 949 | */ |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 950 | static int xfrm_policy_match(const struct xfrm_policy *pol, |
| 951 | const struct flowi *fl, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 952 | u8 type, u16 family, int dir) |
| 953 | { |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 954 | const struct xfrm_selector *sel = &pol->selector; |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 955 | int ret = -ESRCH; |
| 956 | bool match; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 957 | |
| 958 | if (pol->family != family || |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 959 | (fl->flowi_mark & pol->mark.m) != pol->mark.v || |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 960 | pol->type != type) |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 961 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 962 | |
| 963 | match = xfrm_selector_match(sel, fl, family); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 964 | if (match) |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 965 | ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid, |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 966 | dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 967 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 968 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 971 | static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type, |
David S. Miller | 062cdb4 | 2011-02-22 18:31:08 -0800 | [diff] [blame] | 972 | const struct flowi *fl, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 973 | u16 family, u8 dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 975 | int err; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 976 | struct xfrm_policy *pol, *ret; |
David S. Miller | 0b597e7 | 2011-02-24 01:22:48 -0500 | [diff] [blame] | 977 | const xfrm_address_t *daddr, *saddr; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 978 | struct hlist_head *chain; |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 979 | u32 priority = ~0U; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 980 | |
| 981 | daddr = xfrm_flowi_daddr(fl, family); |
| 982 | saddr = xfrm_flowi_saddr(fl, family); |
| 983 | if (unlikely(!daddr || !saddr)) |
| 984 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | read_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 987 | chain = policy_hash_direct(net, daddr, saddr, family, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 988 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 989 | hlist_for_each_entry(pol, chain, bydst) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 990 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 991 | if (err) { |
| 992 | if (err == -ESRCH) |
| 993 | continue; |
| 994 | else { |
| 995 | ret = ERR_PTR(err); |
| 996 | goto fail; |
| 997 | } |
| 998 | } else { |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 999 | ret = pol; |
| 1000 | priority = ret->priority; |
| 1001 | break; |
| 1002 | } |
| 1003 | } |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 1004 | chain = &net->xfrm.policy_inexact[dir]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1005 | hlist_for_each_entry(pol, chain, bydst) { |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1006 | err = xfrm_policy_match(pol, fl, type, family, dir); |
| 1007 | if (err) { |
| 1008 | if (err == -ESRCH) |
| 1009 | continue; |
| 1010 | else { |
| 1011 | ret = ERR_PTR(err); |
| 1012 | goto fail; |
| 1013 | } |
| 1014 | } else if (pol->priority < priority) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1015 | ret = pol; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 1019 | if (ret) |
| 1020 | xfrm_pol_hold(ret); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1021 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | read_unlock_bh(&xfrm_policy_lock); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1023 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1024 | return ret; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1027 | static struct xfrm_policy * |
David S. Miller | 73ff93c | 2011-02-22 18:33:42 -0800 | [diff] [blame] | 1028 | __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1029 | { |
| 1030 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1031 | struct xfrm_policy *pol; |
| 1032 | |
| 1033 | pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir); |
| 1034 | if (pol != NULL) |
| 1035 | return pol; |
| 1036 | #endif |
| 1037 | return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir); |
| 1038 | } |
| 1039 | |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 1040 | static int flow_to_policy_dir(int dir) |
| 1041 | { |
| 1042 | if (XFRM_POLICY_IN == FLOW_DIR_IN && |
| 1043 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
| 1044 | XFRM_POLICY_FWD == FLOW_DIR_FWD) |
| 1045 | return dir; |
| 1046 | |
| 1047 | switch (dir) { |
| 1048 | default: |
| 1049 | case FLOW_DIR_IN: |
| 1050 | return XFRM_POLICY_IN; |
| 1051 | case FLOW_DIR_OUT: |
| 1052 | return XFRM_POLICY_OUT; |
| 1053 | case FLOW_DIR_FWD: |
| 1054 | return XFRM_POLICY_FWD; |
| 1055 | } |
| 1056 | } |
| 1057 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1058 | static struct flow_cache_object * |
David S. Miller | dee9f4b | 2011-02-22 18:44:31 -0800 | [diff] [blame] | 1059 | xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1060 | u8 dir, struct flow_cache_object *old_obj, void *ctx) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1061 | { |
| 1062 | struct xfrm_policy *pol; |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (old_obj) |
| 1065 | xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo)); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1066 | |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 1067 | pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir)); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1068 | if (IS_ERR_OR_NULL(pol)) |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1069 | return ERR_CAST(pol); |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1070 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1071 | /* Resolver returns two references: |
| 1072 | * one for cache and one for caller of flow_cache_lookup() */ |
| 1073 | xfrm_pol_hold(pol); |
| 1074 | |
| 1075 | return &pol->flo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1078 | static inline int policy_to_flow_dir(int dir) |
| 1079 | { |
| 1080 | if (XFRM_POLICY_IN == FLOW_DIR_IN && |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1081 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
| 1082 | XFRM_POLICY_FWD == FLOW_DIR_FWD) |
| 1083 | return dir; |
| 1084 | switch (dir) { |
| 1085 | default: |
| 1086 | case XFRM_POLICY_IN: |
| 1087 | return FLOW_DIR_IN; |
| 1088 | case XFRM_POLICY_OUT: |
| 1089 | return FLOW_DIR_OUT; |
| 1090 | case XFRM_POLICY_FWD: |
| 1091 | return FLOW_DIR_FWD; |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1092 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
David S. Miller | dee9f4b | 2011-02-22 18:44:31 -0800 | [diff] [blame] | 1095 | static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, |
| 1096 | const struct flowi *fl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | { |
| 1098 | struct xfrm_policy *pol; |
| 1099 | |
| 1100 | read_lock_bh(&xfrm_policy_lock); |
| 1101 | if ((pol = sk->sk_policy[dir]) != NULL) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 1102 | bool match = xfrm_selector_match(&pol->selector, fl, |
| 1103 | sk->sk_family); |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1104 | int err = 0; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1105 | |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1106 | if (match) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1107 | if ((sk->sk_mark & pol->mark.m) != pol->mark.v) { |
| 1108 | pol = NULL; |
| 1109 | goto out; |
| 1110 | } |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1111 | err = security_xfrm_policy_lookup(pol->security, |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1112 | fl->flowi_secid, |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1113 | policy_to_flow_dir(dir)); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 1114 | if (!err) |
| 1115 | xfrm_pol_hold(pol); |
| 1116 | else if (err == -ESRCH) |
| 1117 | pol = NULL; |
| 1118 | else |
| 1119 | pol = ERR_PTR(err); |
| 1120 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | pol = NULL; |
| 1122 | } |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1123 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | read_unlock_bh(&xfrm_policy_lock); |
| 1125 | return pol; |
| 1126 | } |
| 1127 | |
| 1128 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir) |
| 1129 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1130 | struct net *net = xp_net(pol); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1131 | struct hlist_head *chain = policy_hash_bysel(net, &pol->selector, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1132 | pol->family, dir); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1133 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1134 | list_add(&pol->walk.all, &net->xfrm.policy_all); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1135 | hlist_add_head(&pol->bydst, chain); |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 1136 | hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index)); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1137 | net->xfrm.policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1139 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1140 | if (xfrm_bydst_should_resize(net, dir, NULL)) |
| 1141 | schedule_work(&net->xfrm.policy_hash_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 1145 | int dir) |
| 1146 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1147 | struct net *net = xp_net(pol); |
| 1148 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1149 | if (hlist_unhashed(&pol->bydst)) |
| 1150 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1152 | hlist_del(&pol->bydst); |
| 1153 | hlist_del(&pol->byidx); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1154 | list_del(&pol->walk.all); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 1155 | net->xfrm.policy_count[dir]--; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1156 | |
| 1157 | return pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1160 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { |
| 1162 | write_lock_bh(&xfrm_policy_lock); |
| 1163 | pol = __xfrm_policy_unlink(pol, dir); |
| 1164 | write_unlock_bh(&xfrm_policy_lock); |
| 1165 | if (pol) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | xfrm_policy_kill(pol); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1167 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | } |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 1169 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 1171 | EXPORT_SYMBOL(xfrm_policy_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol) |
| 1174 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1175 | struct net *net = xp_net(pol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | struct xfrm_policy *old_pol; |
| 1177 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1178 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1179 | if (pol && pol->type != XFRM_POLICY_TYPE_MAIN) |
| 1180 | return -EINVAL; |
| 1181 | #endif |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | write_lock_bh(&xfrm_policy_lock); |
| 1184 | old_pol = sk->sk_policy[dir]; |
| 1185 | sk->sk_policy[dir] = pol; |
| 1186 | if (pol) { |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1187 | pol->curlft.add_time = get_seconds(); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1188 | pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir); |
| 1190 | } |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1191 | if (old_pol) { |
| 1192 | if (pol) |
| 1193 | xfrm_policy_requeue(old_pol, pol); |
| 1194 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 1195 | /* Unlinking succeeds always. This is the only function |
| 1196 | * allowed to delete or replace socket policy. |
| 1197 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | write_unlock_bh(&xfrm_policy_lock); |
| 1201 | |
| 1202 | if (old_pol) { |
| 1203 | xfrm_policy_kill(old_pol); |
| 1204 | } |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
David S. Miller | d3e40a9 | 2011-02-24 01:25:41 -0500 | [diff] [blame] | 1208 | static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 1210 | struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (newp) { |
| 1213 | newp->selector = old->selector; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1214 | if (security_xfrm_policy_clone(old->security, |
| 1215 | &newp->security)) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1216 | kfree(newp); |
| 1217 | return NULL; /* ENOMEM */ |
| 1218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | newp->lft = old->lft; |
| 1220 | newp->curlft = old->curlft; |
Jamal Hadi Salim | fb977e2 | 2010-02-23 15:09:53 -0800 | [diff] [blame] | 1221 | newp->mark = old->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | newp->action = old->action; |
| 1223 | newp->flags = old->flags; |
| 1224 | newp->xfrm_nr = old->xfrm_nr; |
| 1225 | newp->index = old->index; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1226 | newp->type = old->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | memcpy(newp->xfrm_vec, old->xfrm_vec, |
| 1228 | newp->xfrm_nr*sizeof(struct xfrm_tmpl)); |
| 1229 | write_lock_bh(&xfrm_policy_lock); |
| 1230 | __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir); |
| 1231 | write_unlock_bh(&xfrm_policy_lock); |
| 1232 | xfrm_pol_put(newp); |
| 1233 | } |
| 1234 | return newp; |
| 1235 | } |
| 1236 | |
| 1237 | int __xfrm_sk_clone_policy(struct sock *sk) |
| 1238 | { |
| 1239 | struct xfrm_policy *p0 = sk->sk_policy[0], |
| 1240 | *p1 = sk->sk_policy[1]; |
| 1241 | |
| 1242 | sk->sk_policy[0] = sk->sk_policy[1] = NULL; |
| 1243 | if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL) |
| 1244 | return -ENOMEM; |
| 1245 | if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL) |
| 1246 | return -ENOMEM; |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1250 | static int |
Alexey Dobriyan | fbda33b | 2008-11-25 17:56:49 -0800 | [diff] [blame] | 1251 | xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote, |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1252 | unsigned short family) |
| 1253 | { |
| 1254 | int err; |
| 1255 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1256 | |
| 1257 | if (unlikely(afinfo == NULL)) |
| 1258 | return -EINVAL; |
Alexey Dobriyan | fbda33b | 2008-11-25 17:56:49 -0800 | [diff] [blame] | 1259 | err = afinfo->get_saddr(net, local, remote); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1260 | xfrm_policy_put_afinfo(afinfo); |
| 1261 | return err; |
| 1262 | } |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | /* Resolve list of templates for the flow, given policy. */ |
| 1265 | |
| 1266 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 1267 | xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl, |
| 1268 | struct xfrm_state **xfrm, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | { |
Alexey Dobriyan | fbda33b | 2008-11-25 17:56:49 -0800 | [diff] [blame] | 1270 | struct net *net = xp_net(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | int nx; |
| 1272 | int i, error; |
| 1273 | xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family); |
| 1274 | xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1275 | xfrm_address_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
| 1277 | for (nx=0, i = 0; i < policy->xfrm_nr; i++) { |
| 1278 | struct xfrm_state *x; |
| 1279 | xfrm_address_t *remote = daddr; |
| 1280 | xfrm_address_t *local = saddr; |
| 1281 | struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i]; |
| 1282 | |
Joakim Koskela | 48b8d78 | 2007-07-26 00:08:42 -0700 | [diff] [blame] | 1283 | if (tmpl->mode == XFRM_MODE_TUNNEL || |
| 1284 | tmpl->mode == XFRM_MODE_BEET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | remote = &tmpl->id.daddr; |
| 1286 | local = &tmpl->saddr; |
Thomas Egerer | 8444cf7 | 2010-09-20 11:11:38 -0700 | [diff] [blame] | 1287 | if (xfrm_addr_any(local, tmpl->encap_family)) { |
| 1288 | error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 1289 | if (error) |
| 1290 | goto fail; |
| 1291 | local = &tmp; |
| 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family); |
| 1296 | |
| 1297 | if (x && x->km.state == XFRM_STATE_VALID) { |
| 1298 | xfrm[nx++] = x; |
| 1299 | daddr = remote; |
| 1300 | saddr = local; |
| 1301 | continue; |
| 1302 | } |
| 1303 | if (x) { |
| 1304 | error = (x->km.state == XFRM_STATE_ERROR ? |
| 1305 | -EINVAL : -EAGAIN); |
| 1306 | xfrm_state_put(x); |
| 1307 | } |
fernando@oss.ntt.co | a4322266 | 2008-10-23 04:27:19 +0000 | [diff] [blame] | 1308 | else if (error == -ESRCH) |
| 1309 | error = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (!tmpl->optional) |
| 1312 | goto fail; |
| 1313 | } |
| 1314 | return nx; |
| 1315 | |
| 1316 | fail: |
| 1317 | for (nx--; nx>=0; nx--) |
| 1318 | xfrm_state_put(xfrm[nx]); |
| 1319 | return error; |
| 1320 | } |
| 1321 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1322 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 1323 | xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl, |
| 1324 | struct xfrm_state **xfrm, unsigned short family) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1325 | { |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1326 | struct xfrm_state *tp[XFRM_MAX_DEPTH]; |
| 1327 | struct xfrm_state **tpp = (npols > 1) ? tp : xfrm; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1328 | int cnx = 0; |
| 1329 | int error; |
| 1330 | int ret; |
| 1331 | int i; |
| 1332 | |
| 1333 | for (i = 0; i < npols; i++) { |
| 1334 | if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) { |
| 1335 | error = -ENOBUFS; |
| 1336 | goto fail; |
| 1337 | } |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1338 | |
| 1339 | ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1340 | if (ret < 0) { |
| 1341 | error = ret; |
| 1342 | goto fail; |
| 1343 | } else |
| 1344 | cnx += ret; |
| 1345 | } |
| 1346 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1347 | /* found states are sorted for outbound processing */ |
| 1348 | if (npols > 1) |
| 1349 | xfrm_state_sort(xfrm, tpp, cnx, family); |
| 1350 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1351 | return cnx; |
| 1352 | |
| 1353 | fail: |
| 1354 | for (cnx--; cnx>=0; cnx--) |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 1355 | xfrm_state_put(tpp[cnx]); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 1356 | return error; |
| 1357 | |
| 1358 | } |
| 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | /* Check that the bundle accepts the flow and its components are |
| 1361 | * still valid. |
| 1362 | */ |
| 1363 | |
David S. Miller | 05d8402 | 2011-02-22 17:47:10 -0800 | [diff] [blame] | 1364 | static inline int xfrm_get_tos(const struct flowi *fl, int family) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1365 | { |
| 1366 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 1367 | int tos; |
| 1368 | |
| 1369 | if (!afinfo) |
| 1370 | return -EINVAL; |
| 1371 | |
| 1372 | tos = afinfo->get_tos(fl); |
| 1373 | |
| 1374 | xfrm_policy_put_afinfo(afinfo); |
| 1375 | |
| 1376 | return tos; |
| 1377 | } |
| 1378 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1379 | static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo) |
| 1380 | { |
| 1381 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1382 | struct dst_entry *dst = &xdst->u.dst; |
| 1383 | |
| 1384 | if (xdst->route == NULL) { |
| 1385 | /* Dummy bundle - if it has xfrms we were not |
| 1386 | * able to build bundle as template resolution failed. |
| 1387 | * It means we need to try again resolving. */ |
| 1388 | if (xdst->num_xfrms > 0) |
| 1389 | return NULL; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1390 | } else if (dst->flags & DST_XFRM_QUEUE) { |
| 1391 | return NULL; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1392 | } else { |
| 1393 | /* Real bundle */ |
| 1394 | if (stale_bundle(dst)) |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | |
| 1398 | dst_hold(dst); |
| 1399 | return flo; |
| 1400 | } |
| 1401 | |
| 1402 | static int xfrm_bundle_flo_check(struct flow_cache_object *flo) |
| 1403 | { |
| 1404 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1405 | struct dst_entry *dst = &xdst->u.dst; |
| 1406 | |
| 1407 | if (!xdst->route) |
| 1408 | return 0; |
| 1409 | if (stale_bundle(dst)) |
| 1410 | return 0; |
| 1411 | |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
| 1415 | static void xfrm_bundle_flo_delete(struct flow_cache_object *flo) |
| 1416 | { |
| 1417 | struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo); |
| 1418 | struct dst_entry *dst = &xdst->u.dst; |
| 1419 | |
| 1420 | dst_free(dst); |
| 1421 | } |
| 1422 | |
| 1423 | static const struct flow_cache_ops xfrm_bundle_fc_ops = { |
| 1424 | .get = xfrm_bundle_flo_get, |
| 1425 | .check = xfrm_bundle_flo_check, |
| 1426 | .delete = xfrm_bundle_flo_delete, |
| 1427 | }; |
| 1428 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1429 | static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1430 | { |
| 1431 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1432 | struct dst_ops *dst_ops; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1433 | struct xfrm_dst *xdst; |
| 1434 | |
| 1435 | if (!afinfo) |
| 1436 | return ERR_PTR(-EINVAL); |
| 1437 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1438 | switch (family) { |
| 1439 | case AF_INET: |
| 1440 | dst_ops = &net->xfrm.xfrm4_dst_ops; |
| 1441 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1442 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1443 | case AF_INET6: |
| 1444 | dst_ops = &net->xfrm.xfrm6_dst_ops; |
| 1445 | break; |
| 1446 | #endif |
| 1447 | default: |
| 1448 | BUG(); |
| 1449 | } |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 1450 | xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1451 | |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1452 | if (likely(xdst)) { |
Steffen Klassert | 141e369 | 2012-07-05 23:39:34 +0000 | [diff] [blame] | 1453 | struct dst_entry *dst = &xdst->u.dst; |
| 1454 | |
| 1455 | memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst)); |
Hiroaki SHIMODA | 0b15093 | 2011-02-10 23:08:33 -0800 | [diff] [blame] | 1456 | xdst->flo.ops = &xfrm_bundle_fc_ops; |
Patrick McHardy | 9d7b0fc | 2012-08-20 02:56:56 -0700 | [diff] [blame] | 1457 | if (afinfo->init_dst) |
| 1458 | afinfo->init_dst(net, xdst); |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1459 | } else |
Hiroaki SHIMODA | 0b15093 | 2011-02-10 23:08:33 -0800 | [diff] [blame] | 1460 | xdst = ERR_PTR(-ENOBUFS); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1461 | |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 1462 | xfrm_policy_put_afinfo(afinfo); |
| 1463 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1464 | return xdst; |
| 1465 | } |
| 1466 | |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1467 | static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst, |
| 1468 | int nfheader_len) |
| 1469 | { |
| 1470 | struct xfrm_policy_afinfo *afinfo = |
| 1471 | xfrm_policy_get_afinfo(dst->ops->family); |
| 1472 | int err; |
| 1473 | |
| 1474 | if (!afinfo) |
| 1475 | return -EINVAL; |
| 1476 | |
| 1477 | err = afinfo->init_path(path, dst, nfheader_len); |
| 1478 | |
| 1479 | xfrm_policy_put_afinfo(afinfo); |
| 1480 | |
| 1481 | return err; |
| 1482 | } |
| 1483 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1484 | static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
David S. Miller | 0c7b3ee | 2011-02-22 17:48:57 -0800 | [diff] [blame] | 1485 | const struct flowi *fl) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1486 | { |
| 1487 | struct xfrm_policy_afinfo *afinfo = |
| 1488 | xfrm_policy_get_afinfo(xdst->u.dst.ops->family); |
| 1489 | int err; |
| 1490 | |
| 1491 | if (!afinfo) |
| 1492 | return -EINVAL; |
| 1493 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1494 | err = afinfo->fill_dst(xdst, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1495 | |
| 1496 | xfrm_policy_put_afinfo(afinfo); |
| 1497 | |
| 1498 | return err; |
| 1499 | } |
| 1500 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 1503 | * all the metrics... Shortly, bundle a bundle. |
| 1504 | */ |
| 1505 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1506 | static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy, |
| 1507 | struct xfrm_state **xfrm, int nx, |
David S. Miller | 98313ad | 2011-02-22 18:36:50 -0800 | [diff] [blame] | 1508 | const struct flowi *fl, |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1509 | struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1511 | struct net *net = xp_net(policy); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1512 | unsigned long now = jiffies; |
| 1513 | struct net_device *dev; |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1514 | struct xfrm_mode *inner_mode; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1515 | struct dst_entry *dst_prev = NULL; |
| 1516 | struct dst_entry *dst0 = NULL; |
| 1517 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | int err; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1519 | int header_len = 0; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1520 | int nfheader_len = 0; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1521 | int trailer_len = 0; |
| 1522 | int tos; |
| 1523 | int family = policy->selector.family; |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 1524 | xfrm_address_t saddr, daddr; |
| 1525 | |
| 1526 | xfrm_flowi_addr_get(fl, &saddr, &daddr, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1527 | |
| 1528 | tos = xfrm_get_tos(fl, family); |
| 1529 | err = tos; |
| 1530 | if (tos < 0) |
| 1531 | goto put_states; |
| 1532 | |
| 1533 | dst_hold(dst); |
| 1534 | |
| 1535 | for (; i < nx; i++) { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 1536 | struct xfrm_dst *xdst = xfrm_alloc_dst(net, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1537 | struct dst_entry *dst1 = &xdst->u.dst; |
| 1538 | |
| 1539 | err = PTR_ERR(xdst); |
| 1540 | if (IS_ERR(xdst)) { |
| 1541 | dst_release(dst); |
| 1542 | goto put_states; |
| 1543 | } |
| 1544 | |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1545 | if (xfrm[i]->sel.family == AF_UNSPEC) { |
| 1546 | inner_mode = xfrm_ip2inner_mode(xfrm[i], |
| 1547 | xfrm_af2proto(family)); |
| 1548 | if (!inner_mode) { |
| 1549 | err = -EAFNOSUPPORT; |
| 1550 | dst_release(dst); |
| 1551 | goto put_states; |
| 1552 | } |
| 1553 | } else |
| 1554 | inner_mode = xfrm[i]->inner_mode; |
| 1555 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1556 | if (!dst_prev) |
| 1557 | dst0 = dst1; |
| 1558 | else { |
| 1559 | dst_prev->child = dst_clone(dst1); |
| 1560 | dst1->flags |= DST_NOHASH; |
| 1561 | } |
| 1562 | |
| 1563 | xdst->route = dst; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1564 | dst_copy_metrics(dst1, dst); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1565 | |
| 1566 | if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) { |
| 1567 | family = xfrm[i]->props.family; |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 1568 | dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr, |
| 1569 | family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1570 | err = PTR_ERR(dst); |
| 1571 | if (IS_ERR(dst)) |
| 1572 | goto put_states; |
| 1573 | } else |
| 1574 | dst_hold(dst); |
| 1575 | |
| 1576 | dst1->xfrm = xfrm[i]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1577 | xdst->xfrm_genid = xfrm[i]->genid; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1578 | |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 1579 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1580 | dst1->flags |= DST_HOST; |
| 1581 | dst1->lastuse = now; |
| 1582 | |
| 1583 | dst1->input = dst_discard; |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 1584 | dst1->output = inner_mode->afinfo->output; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1585 | |
| 1586 | dst1->next = dst_prev; |
| 1587 | dst_prev = dst1; |
| 1588 | |
| 1589 | header_len += xfrm[i]->props.header_len; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1590 | if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT) |
| 1591 | nfheader_len += xfrm[i]->props.header_len; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1592 | trailer_len += xfrm[i]->props.trailer_len; |
| 1593 | } |
| 1594 | |
| 1595 | dst_prev->child = dst; |
| 1596 | dst0->path = dst; |
| 1597 | |
| 1598 | err = -ENODEV; |
| 1599 | dev = dst->dev; |
| 1600 | if (!dev) |
| 1601 | goto free_dst; |
| 1602 | |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 1603 | xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1604 | xfrm_init_pmtu(dst_prev); |
| 1605 | |
| 1606 | for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) { |
| 1607 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev; |
| 1608 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 1609 | err = xfrm_fill_dst(xdst, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 1610 | if (err) |
| 1611 | goto free_dst; |
| 1612 | |
| 1613 | dst_prev->header_len = header_len; |
| 1614 | dst_prev->trailer_len = trailer_len; |
| 1615 | header_len -= xdst->u.dst.xfrm->props.header_len; |
| 1616 | trailer_len -= xdst->u.dst.xfrm->props.trailer_len; |
| 1617 | } |
| 1618 | |
| 1619 | out: |
| 1620 | return dst0; |
| 1621 | |
| 1622 | put_states: |
| 1623 | for (; i < nx; i++) |
| 1624 | xfrm_state_put(xfrm[i]); |
| 1625 | free_dst: |
| 1626 | if (dst0) |
| 1627 | dst_free(dst0); |
| 1628 | dst0 = ERR_PTR(err); |
| 1629 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Masahide NAKAMURA | 157bfc2 | 2007-04-30 00:33:35 -0700 | [diff] [blame] | 1632 | static int inline |
David S. Miller | 3f0e18f | 2011-02-22 18:38:14 -0800 | [diff] [blame] | 1633 | xfrm_dst_alloc_copy(void **target, const void *src, int size) |
Masahide NAKAMURA | 157bfc2 | 2007-04-30 00:33:35 -0700 | [diff] [blame] | 1634 | { |
| 1635 | if (!*target) { |
| 1636 | *target = kmalloc(size, GFP_ATOMIC); |
| 1637 | if (!*target) |
| 1638 | return -ENOMEM; |
| 1639 | } |
| 1640 | memcpy(*target, src, size); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | static int inline |
David S. Miller | 1786b38 | 2011-02-24 01:32:54 -0500 | [diff] [blame] | 1645 | xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel) |
Masahide NAKAMURA | 157bfc2 | 2007-04-30 00:33:35 -0700 | [diff] [blame] | 1646 | { |
| 1647 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1648 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 1649 | return xfrm_dst_alloc_copy((void **)&(xdst->partner), |
| 1650 | sel, sizeof(*sel)); |
| 1651 | #else |
| 1652 | return 0; |
| 1653 | #endif |
| 1654 | } |
| 1655 | |
| 1656 | static int inline |
David S. Miller | 3f0e18f | 2011-02-22 18:38:14 -0800 | [diff] [blame] | 1657 | xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl) |
Masahide NAKAMURA | 157bfc2 | 2007-04-30 00:33:35 -0700 | [diff] [blame] | 1658 | { |
| 1659 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1660 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 1661 | return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl)); |
| 1662 | #else |
| 1663 | return 0; |
| 1664 | #endif |
| 1665 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
David S. Miller | 73ff93c | 2011-02-22 18:33:42 -0800 | [diff] [blame] | 1667 | static int xfrm_expand_policies(const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1668 | struct xfrm_policy **pols, |
| 1669 | int *num_pols, int *num_xfrms) |
| 1670 | { |
| 1671 | int i; |
| 1672 | |
| 1673 | if (*num_pols == 0 || !pols[0]) { |
| 1674 | *num_pols = 0; |
| 1675 | *num_xfrms = 0; |
| 1676 | return 0; |
| 1677 | } |
| 1678 | if (IS_ERR(pols[0])) |
| 1679 | return PTR_ERR(pols[0]); |
| 1680 | |
| 1681 | *num_xfrms = pols[0]->xfrm_nr; |
| 1682 | |
| 1683 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 1684 | if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW && |
| 1685 | pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
| 1686 | pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]), |
| 1687 | XFRM_POLICY_TYPE_MAIN, |
| 1688 | fl, family, |
| 1689 | XFRM_POLICY_OUT); |
| 1690 | if (pols[1]) { |
| 1691 | if (IS_ERR(pols[1])) { |
| 1692 | xfrm_pols_put(pols, *num_pols); |
| 1693 | return PTR_ERR(pols[1]); |
| 1694 | } |
| 1695 | (*num_pols) ++; |
| 1696 | (*num_xfrms) += pols[1]->xfrm_nr; |
| 1697 | } |
| 1698 | } |
| 1699 | #endif |
| 1700 | for (i = 0; i < *num_pols; i++) { |
| 1701 | if (pols[i]->action != XFRM_POLICY_ALLOW) { |
| 1702 | *num_xfrms = -1; |
| 1703 | break; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | return 0; |
| 1708 | |
| 1709 | } |
| 1710 | |
| 1711 | static struct xfrm_dst * |
| 1712 | xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols, |
David S. Miller | 4ca2e68 | 2011-02-22 18:38:51 -0800 | [diff] [blame] | 1713 | const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1714 | struct dst_entry *dst_orig) |
| 1715 | { |
| 1716 | struct net *net = xp_net(pols[0]); |
| 1717 | struct xfrm_state *xfrm[XFRM_MAX_DEPTH]; |
| 1718 | struct dst_entry *dst; |
| 1719 | struct xfrm_dst *xdst; |
| 1720 | int err; |
| 1721 | |
| 1722 | /* Try to instantiate a bundle */ |
| 1723 | err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family); |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 1724 | if (err <= 0) { |
| 1725 | if (err != 0 && err != -EAGAIN) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1726 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 1727 | return ERR_PTR(err); |
| 1728 | } |
| 1729 | |
| 1730 | dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig); |
| 1731 | if (IS_ERR(dst)) { |
| 1732 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR); |
| 1733 | return ERR_CAST(dst); |
| 1734 | } |
| 1735 | |
| 1736 | xdst = (struct xfrm_dst *)dst; |
| 1737 | xdst->num_xfrms = err; |
| 1738 | if (num_pols > 1) |
| 1739 | err = xfrm_dst_update_parent(dst, &pols[1]->selector); |
| 1740 | else |
| 1741 | err = xfrm_dst_update_origin(dst, fl); |
| 1742 | if (unlikely(err)) { |
| 1743 | dst_free(dst); |
| 1744 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR); |
| 1745 | return ERR_PTR(err); |
| 1746 | } |
| 1747 | |
| 1748 | xdst->num_pols = num_pols; |
| 1749 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols); |
| 1750 | xdst->policy_genid = atomic_read(&pols[0]->genid); |
| 1751 | |
| 1752 | return xdst; |
| 1753 | } |
| 1754 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1755 | static void xfrm_policy_queue_process(unsigned long arg) |
| 1756 | { |
| 1757 | int err = 0; |
| 1758 | struct sk_buff *skb; |
| 1759 | struct sock *sk; |
| 1760 | struct dst_entry *dst; |
| 1761 | struct net_device *dev; |
| 1762 | struct xfrm_policy *pol = (struct xfrm_policy *)arg; |
| 1763 | struct xfrm_policy_queue *pq = &pol->polq; |
| 1764 | struct flowi fl; |
| 1765 | struct sk_buff_head list; |
| 1766 | |
| 1767 | spin_lock(&pq->hold_queue.lock); |
| 1768 | skb = skb_peek(&pq->hold_queue); |
| 1769 | dst = skb_dst(skb); |
| 1770 | sk = skb->sk; |
| 1771 | xfrm_decode_session(skb, &fl, dst->ops->family); |
| 1772 | spin_unlock(&pq->hold_queue.lock); |
| 1773 | |
| 1774 | dst_hold(dst->path); |
| 1775 | dst = xfrm_lookup(xp_net(pol), dst->path, &fl, |
| 1776 | sk, 0); |
| 1777 | if (IS_ERR(dst)) |
| 1778 | goto purge_queue; |
| 1779 | |
| 1780 | if (dst->flags & DST_XFRM_QUEUE) { |
| 1781 | dst_release(dst); |
| 1782 | |
| 1783 | if (pq->timeout >= XFRM_QUEUE_TMO_MAX) |
| 1784 | goto purge_queue; |
| 1785 | |
| 1786 | pq->timeout = pq->timeout << 1; |
| 1787 | mod_timer(&pq->hold_timer, jiffies + pq->timeout); |
| 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | dst_release(dst); |
| 1792 | |
| 1793 | __skb_queue_head_init(&list); |
| 1794 | |
| 1795 | spin_lock(&pq->hold_queue.lock); |
| 1796 | pq->timeout = 0; |
| 1797 | skb_queue_splice_init(&pq->hold_queue, &list); |
| 1798 | spin_unlock(&pq->hold_queue.lock); |
| 1799 | |
| 1800 | while (!skb_queue_empty(&list)) { |
| 1801 | skb = __skb_dequeue(&list); |
| 1802 | |
| 1803 | xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family); |
| 1804 | dst_hold(skb_dst(skb)->path); |
| 1805 | dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path, |
| 1806 | &fl, skb->sk, 0); |
| 1807 | if (IS_ERR(dst)) { |
| 1808 | dev_put(skb->dev); |
| 1809 | kfree_skb(skb); |
| 1810 | continue; |
| 1811 | } |
| 1812 | |
| 1813 | nf_reset(skb); |
| 1814 | skb_dst_drop(skb); |
| 1815 | skb_dst_set(skb, dst); |
| 1816 | |
| 1817 | dev = skb->dev; |
| 1818 | err = dst_output(skb); |
| 1819 | dev_put(dev); |
| 1820 | } |
| 1821 | |
| 1822 | return; |
| 1823 | |
| 1824 | purge_queue: |
| 1825 | pq->timeout = 0; |
| 1826 | xfrm_queue_purge(&pq->hold_queue); |
| 1827 | } |
| 1828 | |
| 1829 | static int xdst_queue_output(struct sk_buff *skb) |
| 1830 | { |
| 1831 | unsigned long sched_next; |
| 1832 | struct dst_entry *dst = skb_dst(skb); |
| 1833 | struct xfrm_dst *xdst = (struct xfrm_dst *) dst; |
| 1834 | struct xfrm_policy_queue *pq = &xdst->pols[0]->polq; |
| 1835 | |
| 1836 | if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) { |
| 1837 | kfree_skb(skb); |
| 1838 | return -EAGAIN; |
| 1839 | } |
| 1840 | |
| 1841 | skb_dst_force(skb); |
| 1842 | dev_hold(skb->dev); |
| 1843 | |
| 1844 | spin_lock_bh(&pq->hold_queue.lock); |
| 1845 | |
| 1846 | if (!pq->timeout) |
| 1847 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
| 1848 | |
| 1849 | sched_next = jiffies + pq->timeout; |
| 1850 | |
| 1851 | if (del_timer(&pq->hold_timer)) { |
| 1852 | if (time_before(pq->hold_timer.expires, sched_next)) |
| 1853 | sched_next = pq->hold_timer.expires; |
| 1854 | } |
| 1855 | |
| 1856 | __skb_queue_tail(&pq->hold_queue, skb); |
| 1857 | mod_timer(&pq->hold_timer, sched_next); |
| 1858 | |
| 1859 | spin_unlock_bh(&pq->hold_queue.lock); |
| 1860 | |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
| 1864 | static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net, |
| 1865 | struct dst_entry *dst, |
| 1866 | const struct flowi *fl, |
| 1867 | int num_xfrms, |
| 1868 | u16 family) |
| 1869 | { |
| 1870 | int err; |
| 1871 | struct net_device *dev; |
| 1872 | struct dst_entry *dst1; |
| 1873 | struct xfrm_dst *xdst; |
| 1874 | |
| 1875 | xdst = xfrm_alloc_dst(net, family); |
| 1876 | if (IS_ERR(xdst)) |
| 1877 | return xdst; |
| 1878 | |
| 1879 | if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 || |
| 1880 | (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP)) |
| 1881 | return xdst; |
| 1882 | |
| 1883 | dst1 = &xdst->u.dst; |
| 1884 | dst_hold(dst); |
| 1885 | xdst->route = dst; |
| 1886 | |
| 1887 | dst_copy_metrics(dst1, dst); |
| 1888 | |
| 1889 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
| 1890 | dst1->flags |= DST_HOST | DST_XFRM_QUEUE; |
| 1891 | dst1->lastuse = jiffies; |
| 1892 | |
| 1893 | dst1->input = dst_discard; |
| 1894 | dst1->output = xdst_queue_output; |
| 1895 | |
| 1896 | dst_hold(dst); |
| 1897 | dst1->child = dst; |
| 1898 | dst1->path = dst; |
| 1899 | |
| 1900 | xfrm_init_path((struct xfrm_dst *)dst1, dst, 0); |
| 1901 | |
| 1902 | err = -ENODEV; |
| 1903 | dev = dst->dev; |
| 1904 | if (!dev) |
| 1905 | goto free_dst; |
| 1906 | |
| 1907 | err = xfrm_fill_dst(xdst, dev, fl); |
| 1908 | if (err) |
| 1909 | goto free_dst; |
| 1910 | |
| 1911 | out: |
| 1912 | return xdst; |
| 1913 | |
| 1914 | free_dst: |
| 1915 | dst_release(dst1); |
| 1916 | xdst = ERR_PTR(err); |
| 1917 | goto out; |
| 1918 | } |
| 1919 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1920 | static struct flow_cache_object * |
David S. Miller | dee9f4b | 2011-02-22 18:44:31 -0800 | [diff] [blame] | 1921 | xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1922 | struct flow_cache_object *oldflo, void *ctx) |
| 1923 | { |
| 1924 | struct dst_entry *dst_orig = (struct dst_entry *)ctx; |
| 1925 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 1926 | struct xfrm_dst *xdst, *new_xdst; |
| 1927 | int num_pols = 0, num_xfrms = 0, i, err, pol_dead; |
| 1928 | |
| 1929 | /* Check if the policies from old bundle are usable */ |
| 1930 | xdst = NULL; |
| 1931 | if (oldflo) { |
| 1932 | xdst = container_of(oldflo, struct xfrm_dst, flo); |
| 1933 | num_pols = xdst->num_pols; |
| 1934 | num_xfrms = xdst->num_xfrms; |
| 1935 | pol_dead = 0; |
| 1936 | for (i = 0; i < num_pols; i++) { |
| 1937 | pols[i] = xdst->pols[i]; |
| 1938 | pol_dead |= pols[i]->walk.dead; |
| 1939 | } |
| 1940 | if (pol_dead) { |
| 1941 | dst_free(&xdst->u.dst); |
| 1942 | xdst = NULL; |
| 1943 | num_pols = 0; |
| 1944 | num_xfrms = 0; |
| 1945 | oldflo = NULL; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /* Resolve policies to use if we couldn't get them from |
| 1950 | * previous cache entry */ |
| 1951 | if (xdst == NULL) { |
| 1952 | num_pols = 1; |
Baker Zhang | b5fb82c | 2013-03-19 04:24:30 +0000 | [diff] [blame] | 1953 | pols[0] = __xfrm_policy_lookup(net, fl, family, |
| 1954 | flow_to_policy_dir(dir)); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1955 | err = xfrm_expand_policies(fl, family, pols, |
| 1956 | &num_pols, &num_xfrms); |
| 1957 | if (err < 0) |
| 1958 | goto inc_error; |
| 1959 | if (num_pols == 0) |
| 1960 | return NULL; |
| 1961 | if (num_xfrms <= 0) |
| 1962 | goto make_dummy_bundle; |
| 1963 | } |
| 1964 | |
| 1965 | new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig); |
| 1966 | if (IS_ERR(new_xdst)) { |
| 1967 | err = PTR_ERR(new_xdst); |
| 1968 | if (err != -EAGAIN) |
| 1969 | goto error; |
| 1970 | if (oldflo == NULL) |
| 1971 | goto make_dummy_bundle; |
| 1972 | dst_hold(&xdst->u.dst); |
| 1973 | return oldflo; |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 1974 | } else if (new_xdst == NULL) { |
| 1975 | num_xfrms = 0; |
| 1976 | if (oldflo == NULL) |
| 1977 | goto make_dummy_bundle; |
| 1978 | xdst->num_xfrms = 0; |
| 1979 | dst_hold(&xdst->u.dst); |
| 1980 | return oldflo; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* Kill the previous bundle */ |
| 1984 | if (xdst) { |
| 1985 | /* The policies were stolen for newly generated bundle */ |
| 1986 | xdst->num_pols = 0; |
| 1987 | dst_free(&xdst->u.dst); |
| 1988 | } |
| 1989 | |
| 1990 | /* Flow cache does not have reference, it dst_free()'s, |
| 1991 | * but we do need to return one reference for original caller */ |
| 1992 | dst_hold(&new_xdst->u.dst); |
| 1993 | return &new_xdst->flo; |
| 1994 | |
| 1995 | make_dummy_bundle: |
| 1996 | /* We found policies, but there's no bundles to instantiate: |
| 1997 | * either because the policy blocks, has no transformations or |
| 1998 | * we could not build template (no xfrm_states).*/ |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1999 | xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2000 | if (IS_ERR(xdst)) { |
| 2001 | xfrm_pols_put(pols, num_pols); |
| 2002 | return ERR_CAST(xdst); |
| 2003 | } |
| 2004 | xdst->num_pols = num_pols; |
| 2005 | xdst->num_xfrms = num_xfrms; |
| 2006 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols); |
| 2007 | |
| 2008 | dst_hold(&xdst->u.dst); |
| 2009 | return &xdst->flo; |
| 2010 | |
| 2011 | inc_error: |
| 2012 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 2013 | error: |
| 2014 | if (xdst != NULL) |
| 2015 | dst_free(&xdst->u.dst); |
| 2016 | else |
| 2017 | xfrm_pols_put(pols, num_pols); |
| 2018 | return ERR_PTR(err); |
| 2019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2021 | static struct dst_entry *make_blackhole(struct net *net, u16 family, |
| 2022 | struct dst_entry *dst_orig) |
| 2023 | { |
| 2024 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
| 2025 | struct dst_entry *ret; |
| 2026 | |
| 2027 | if (!afinfo) { |
| 2028 | dst_release(dst_orig); |
Li RongQing | 433a195 | 2012-09-17 22:40:10 +0000 | [diff] [blame] | 2029 | return ERR_PTR(-EINVAL); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2030 | } else { |
| 2031 | ret = afinfo->blackhole_route(net, dst_orig); |
| 2032 | } |
| 2033 | xfrm_policy_put_afinfo(afinfo); |
| 2034 | |
| 2035 | return ret; |
| 2036 | } |
| 2037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | /* Main function: finds/creates a bundle for given flow. |
| 2039 | * |
| 2040 | * At the moment we eat a raw IP route. Mostly to speed up lookups |
| 2041 | * on interfaces with disabled IPsec. |
| 2042 | */ |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2043 | struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig, |
| 2044 | const struct flowi *fl, |
| 2045 | struct sock *sk, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | { |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2047 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2048 | struct flow_cache_object *flo; |
| 2049 | struct xfrm_dst *xdst; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2050 | struct dst_entry *dst, *route; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2051 | u16 family = dst_orig->ops->family; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 2052 | u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT); |
Changli Gao | 4b02162 | 2010-04-27 21:20:22 +0000 | [diff] [blame] | 2053 | int i, err, num_pols, num_xfrms = 0, drop_pols = 0; |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | restart: |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2056 | dst = NULL; |
| 2057 | xdst = NULL; |
| 2058 | route = NULL; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2059 | |
Thomas Graf | f7944fb | 2007-08-25 13:46:55 -0700 | [diff] [blame] | 2060 | if (sk && sk->sk_policy[XFRM_POLICY_OUT]) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2061 | num_pols = 1; |
| 2062 | pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl); |
| 2063 | err = xfrm_expand_policies(fl, family, pols, |
| 2064 | &num_pols, &num_xfrms); |
| 2065 | if (err < 0) |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2066 | goto dropdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2067 | |
| 2068 | if (num_pols) { |
| 2069 | if (num_xfrms <= 0) { |
| 2070 | drop_pols = num_pols; |
| 2071 | goto no_transform; |
| 2072 | } |
| 2073 | |
| 2074 | xdst = xfrm_resolve_and_create_bundle( |
| 2075 | pols, num_pols, fl, |
| 2076 | family, dst_orig); |
| 2077 | if (IS_ERR(xdst)) { |
| 2078 | xfrm_pols_put(pols, num_pols); |
| 2079 | err = PTR_ERR(xdst); |
| 2080 | goto dropdst; |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 2081 | } else if (xdst == NULL) { |
| 2082 | num_xfrms = 0; |
| 2083 | drop_pols = num_pols; |
| 2084 | goto no_transform; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Steffen Klassert | fbd5060 | 2011-03-15 21:12:49 +0000 | [diff] [blame] | 2087 | dst_hold(&xdst->u.dst); |
| 2088 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2089 | spin_lock_bh(&xfrm_policy_sk_bundle_lock); |
| 2090 | xdst->u.dst.next = xfrm_policy_sk_bundles; |
| 2091 | xfrm_policy_sk_bundles = &xdst->u.dst; |
| 2092 | spin_unlock_bh(&xfrm_policy_sk_bundle_lock); |
| 2093 | |
| 2094 | route = xdst->route; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2095 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2098 | if (xdst == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | /* To accelerate a bit... */ |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2100 | if ((dst_orig->flags & DST_NOXFRM) || |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 2101 | !net->xfrm.policy_count[XFRM_POLICY_OUT]) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2102 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2104 | flo = flow_cache_lookup(net, fl, family, dir, |
| 2105 | xfrm_bundle_lookup, dst_orig); |
| 2106 | if (flo == NULL) |
| 2107 | goto nopol; |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 2108 | if (IS_ERR(flo)) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2109 | err = PTR_ERR(flo); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2110 | goto dropdst; |
Masahide NAKAMURA | d66e37a | 2008-01-07 21:46:15 -0800 | [diff] [blame] | 2111 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2112 | xdst = container_of(flo, struct xfrm_dst, flo); |
| 2113 | |
| 2114 | num_pols = xdst->num_pols; |
| 2115 | num_xfrms = xdst->num_xfrms; |
| 2116 | memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols); |
| 2117 | route = xdst->route; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2120 | dst = &xdst->u.dst; |
| 2121 | if (route == NULL && num_xfrms > 0) { |
| 2122 | /* The only case when xfrm_bundle_lookup() returns a |
| 2123 | * bundle with null route, is when the template could |
| 2124 | * not be resolved. It means policies are there, but |
| 2125 | * bundle could not be created, since we don't yet |
| 2126 | * have the xfrm_state's. We need to wait for KM to |
| 2127 | * negotiate new SA's or bail out with error.*/ |
| 2128 | if (net->xfrm.sysctl_larval_drop) { |
| 2129 | /* EREMOTE tells the caller to generate |
| 2130 | * a one-shot blackhole route. */ |
| 2131 | dst_release(dst); |
Timo Teras | a1aa348 | 2010-05-15 23:49:26 -0700 | [diff] [blame] | 2132 | xfrm_pols_put(pols, drop_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2133 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2134 | |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2135 | return make_blackhole(net, family, dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2136 | } |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 2137 | if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2138 | DECLARE_WAITQUEUE(wait, current); |
| 2139 | |
| 2140 | add_wait_queue(&net->xfrm.km_waitq, &wait); |
| 2141 | set_current_state(TASK_INTERRUPTIBLE); |
| 2142 | schedule(); |
| 2143 | set_current_state(TASK_RUNNING); |
| 2144 | remove_wait_queue(&net->xfrm.km_waitq, &wait); |
| 2145 | |
| 2146 | if (!signal_pending(current)) { |
| 2147 | dst_release(dst); |
| 2148 | goto restart; |
| 2149 | } |
| 2150 | |
| 2151 | err = -ERESTART; |
| 2152 | } else |
| 2153 | err = -EAGAIN; |
| 2154 | |
| 2155 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
| 2156 | goto error; |
| 2157 | } |
| 2158 | |
| 2159 | no_transform: |
| 2160 | if (num_pols == 0) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2161 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2163 | if ((flags & XFRM_LOOKUP_ICMP) && |
| 2164 | !(pols[0]->flags & XFRM_POLICY_ICMP)) { |
| 2165 | err = -ENOENT; |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2166 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2167 | } |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2168 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2169 | for (i = 0; i < num_pols; i++) |
| 2170 | pols[i]->curlft.use_time = get_seconds(); |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 2171 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2172 | if (num_xfrms < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | /* Prohibit the flow */ |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2174 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK); |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 2175 | err = -EPERM; |
| 2176 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2177 | } else if (num_xfrms > 0) { |
| 2178 | /* Flow transformed */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2179 | dst_release(dst_orig); |
| 2180 | } else { |
| 2181 | /* Flow passes untransformed */ |
| 2182 | dst_release(dst); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2183 | dst = dst_orig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2185 | ok: |
| 2186 | xfrm_pols_put(pols, drop_pols); |
Gao feng | 0c18337 | 2012-05-26 01:30:53 +0000 | [diff] [blame] | 2187 | if (dst && dst->xfrm && |
| 2188 | dst->xfrm->props.mode == XFRM_MODE_TUNNEL) |
| 2189 | dst->flags |= DST_XFRM_TUNNEL; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2190 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2192 | nopol: |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2193 | if (!(flags & XFRM_LOOKUP_ICMP)) { |
| 2194 | dst = dst_orig; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2195 | goto ok; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2196 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2197 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | error: |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2199 | dst_release(dst); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 2200 | dropdst: |
| 2201 | dst_release(dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2202 | xfrm_pols_put(pols, drop_pols); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2203 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
| 2205 | EXPORT_SYMBOL(xfrm_lookup); |
| 2206 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2207 | static inline int |
David S. Miller | 8f029de | 2011-02-22 17:59:59 -0800 | [diff] [blame] | 2208 | xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl) |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2209 | { |
| 2210 | struct xfrm_state *x; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2211 | |
| 2212 | if (!skb->sp || idx < 0 || idx >= skb->sp->len) |
| 2213 | return 0; |
| 2214 | x = skb->sp->xvec[idx]; |
| 2215 | if (!x->type->reject) |
| 2216 | return 0; |
Herbert Xu | 1ecafed | 2007-10-09 13:24:07 -0700 | [diff] [blame] | 2217 | return x->type->reject(x, skb, fl); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | /* When skb is transformed back to its "native" form, we have to |
| 2221 | * check policy restrictions. At the moment we make this in maximally |
| 2222 | * stupid way. Shame on me. :-) Of course, connected sockets must |
| 2223 | * have policy cached at them. |
| 2224 | */ |
| 2225 | |
| 2226 | static inline int |
David S. Miller | 7db454b | 2011-02-24 01:43:01 -0500 | [diff] [blame] | 2227 | xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | unsigned short family) |
| 2229 | { |
| 2230 | if (xfrm_state_kern(x)) |
Kazunori MIYAZAWA | 928ba41 | 2007-02-13 12:57:16 -0800 | [diff] [blame] | 2231 | return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | return x->id.proto == tmpl->id.proto && |
| 2233 | (x->id.spi == tmpl->id.spi || !tmpl->id.spi) && |
| 2234 | (x->props.reqid == tmpl->reqid || !tmpl->reqid) && |
| 2235 | x->props.mode == tmpl->mode && |
Herbert Xu | c5d18e9 | 2008-04-22 00:46:42 -0700 | [diff] [blame] | 2236 | (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) || |
Masahide NAKAMURA | f3bd484 | 2006-08-23 18:00:48 -0700 | [diff] [blame] | 2237 | !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) && |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 2238 | !(x->props.mode != XFRM_MODE_TRANSPORT && |
| 2239 | xfrm_state_addr_cmp(tmpl, x, family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2242 | /* |
| 2243 | * 0 or more than 0 is returned when validation is succeeded (either bypass |
| 2244 | * because of optional transport mode, or next index of the mathced secpath |
| 2245 | * state with the template. |
| 2246 | * -1 is returned when no matching template is found. |
| 2247 | * Otherwise "-2 - errored_index" is returned. |
| 2248 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | static inline int |
David S. Miller | 22cccb7 | 2011-02-24 01:43:33 -0500 | [diff] [blame] | 2250 | xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | unsigned short family) |
| 2252 | { |
| 2253 | int idx = start; |
| 2254 | |
| 2255 | if (tmpl->optional) { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 2256 | if (tmpl->mode == XFRM_MODE_TRANSPORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | return start; |
| 2258 | } else |
| 2259 | start = -1; |
| 2260 | for (; idx < sp->len; idx++) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 2261 | if (xfrm_state_ok(tmpl, sp->xvec[idx], family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | return ++idx; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2263 | if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) { |
| 2264 | if (start == -1) |
| 2265 | start = -2-idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | break; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | } |
| 2269 | return start; |
| 2270 | } |
| 2271 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2272 | int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, |
| 2273 | unsigned int family, int reverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | { |
| 2275 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2276 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | |
| 2278 | if (unlikely(afinfo == NULL)) |
| 2279 | return -EAFNOSUPPORT; |
| 2280 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2281 | afinfo->decode_session(skb, fl, reverse); |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 2282 | err = security_xfrm_decode_session(skb, &fl->flowi_secid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | xfrm_policy_put_afinfo(afinfo); |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2284 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | } |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2286 | EXPORT_SYMBOL(__xfrm_decode_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
David S. Miller | 9a7386e | 2011-02-24 01:44:12 -0500 | [diff] [blame] | 2288 | static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
| 2290 | for (; k < sp->len; k++) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2291 | if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2292 | *idxp = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | return 1; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 2300 | 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] | 2301 | unsigned short family) |
| 2302 | { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 2303 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | struct xfrm_policy *pol; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2305 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 2306 | int npols = 0; |
| 2307 | int xfrm_nr; |
| 2308 | int pi; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2309 | int reverse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | struct flowi fl; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2311 | u8 fl_dir; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2312 | int xerr_idx = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 2314 | reverse = dir & ~XFRM_POLICY_MASK; |
| 2315 | dir &= XFRM_POLICY_MASK; |
| 2316 | fl_dir = policy_to_flow_dir(dir); |
| 2317 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2318 | if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2319 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2321 | } |
| 2322 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 2323 | nf_nat_decode_session(skb, &fl, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
| 2325 | /* First, check used SA against their selectors. */ |
| 2326 | if (skb->sp) { |
| 2327 | int i; |
| 2328 | |
| 2329 | for (i=skb->sp->len-1; i>=0; i--) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 2330 | struct xfrm_state *x = skb->sp->xvec[i]; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2331 | if (!xfrm_selector_match(&x->sel, &fl, family)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2332 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | pol = NULL; |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2339 | if (sk && sk->sk_policy[dir]) { |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 2340 | pol = xfrm_sk_policy_lookup(sk, dir, &fl); |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2341 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2342 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2343 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2344 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 2347 | if (!pol) { |
| 2348 | struct flow_cache_object *flo; |
| 2349 | |
| 2350 | flo = flow_cache_lookup(net, &fl, family, fl_dir, |
| 2351 | xfrm_policy_lookup, NULL); |
| 2352 | if (IS_ERR_OR_NULL(flo)) |
| 2353 | pol = ERR_CAST(flo); |
| 2354 | else |
| 2355 | pol = container_of(flo, struct xfrm_policy, flo); |
| 2356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2358 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2359 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2360 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2361 | } |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2362 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2363 | if (!pol) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2364 | if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2365 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2366 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2367 | return 0; |
| 2368 | } |
| 2369 | return 1; |
| 2370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 2372 | pol->curlft.use_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2374 | pols[0] = pol; |
| 2375 | npols ++; |
| 2376 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 2377 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 2378 | pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2379 | &fl, family, |
| 2380 | XFRM_POLICY_IN); |
| 2381 | if (pols[1]) { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2382 | if (IS_ERR(pols[1])) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2383 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2384 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2385 | } |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 2386 | pols[1]->curlft.use_time = get_seconds(); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2387 | npols ++; |
| 2388 | } |
| 2389 | } |
| 2390 | #endif |
| 2391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | if (pol->action == XFRM_POLICY_ALLOW) { |
| 2393 | struct sec_path *sp; |
| 2394 | static struct sec_path dummy; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2395 | struct xfrm_tmpl *tp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2396 | struct xfrm_tmpl *stp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2397 | struct xfrm_tmpl **tpp = tp; |
| 2398 | int ti = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | int i, k; |
| 2400 | |
| 2401 | if ((sp = skb->sp) == NULL) |
| 2402 | sp = &dummy; |
| 2403 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2404 | for (pi = 0; pi < npols; pi++) { |
| 2405 | if (pols[pi] != pol && |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2406 | pols[pi]->action != XFRM_POLICY_ALLOW) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2407 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2408 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2409 | } |
| 2410 | if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2411 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2412 | goto reject_error; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2413 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2414 | for (i = 0; i < pols[pi]->xfrm_nr; i++) |
| 2415 | tpp[ti++] = &pols[pi]->xfrm_vec[i]; |
| 2416 | } |
| 2417 | xfrm_nr = ti; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2418 | if (npols > 1) { |
| 2419 | xfrm_tmpl_sort(stp, tpp, xfrm_nr, family); |
| 2420 | tpp = stp; |
| 2421 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | /* For each tunnel xfrm, find the first matching tmpl. |
| 2424 | * For each tmpl before that, find corresponding xfrm. |
| 2425 | * Order is _important_. Later we will implement |
| 2426 | * some barriers, but at the moment barriers |
| 2427 | * are implied between each two transformations. |
| 2428 | */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2429 | for (i = xfrm_nr-1, k = 0; i >= 0; i--) { |
| 2430 | k = xfrm_policy_ok(tpp[i], sp, k, family); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2431 | if (k < 0) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 2432 | if (k < -1) |
| 2433 | /* "-2 - errored_index" returned */ |
| 2434 | xerr_idx = -(2+k); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2435 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | goto reject; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2440 | if (secpath_has_nontransport(sp, k, &xerr_idx)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2441 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2445 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | return 1; |
| 2447 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2448 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
| 2450 | reject: |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 2451 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2452 | reject_error: |
| 2453 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | return 0; |
| 2455 | } |
| 2456 | EXPORT_SYMBOL(__xfrm_policy_check); |
| 2457 | |
| 2458 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 2459 | { |
Alexey Dobriyan | 99a6665 | 2008-11-25 17:36:13 -0800 | [diff] [blame] | 2460 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | struct flowi fl; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2462 | struct dst_entry *dst; |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 2463 | int res = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2465 | if (xfrm_decode_session(skb, &fl, family) < 0) { |
jamal | 72032fd | 2010-02-18 03:35:07 +0000 | [diff] [blame] | 2466 | XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 2468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | |
Eric Dumazet | fafeeb6 | 2010-06-01 10:04:49 +0000 | [diff] [blame] | 2470 | skb_dst_force(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2471 | |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2472 | dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0); |
| 2473 | if (IS_ERR(dst)) { |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 2474 | res = 0; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 2475 | dst = NULL; |
| 2476 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2477 | skb_dst_set(skb, dst); |
| 2478 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | } |
| 2480 | EXPORT_SYMBOL(__xfrm_route_forward); |
| 2481 | |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2482 | /* Optimize later using cookies and generation ids. */ |
| 2483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie) |
| 2485 | { |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2486 | /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 2487 | * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to |
| 2488 | * get validated by dst_ops->check on every use. We do this |
| 2489 | * because when a normal route referenced by an XFRM dst is |
| 2490 | * obsoleted we do not go looking around for all parent |
| 2491 | * referencing XFRM dsts so that we can invalidate them. It |
| 2492 | * is just too much work. Instead we make the checks here on |
| 2493 | * every use. For example: |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2494 | * |
| 2495 | * XFRM dst A --> IPv4 dst X |
| 2496 | * |
| 2497 | * X is the "xdst->route" of A (X is also the "dst->path" of A |
| 2498 | * in this example). If X is marked obsolete, "A" will not |
| 2499 | * notice. That's what we are validating here via the |
| 2500 | * stale_bundle() check. |
| 2501 | * |
| 2502 | * When a policy's bundle is pruned, we dst_free() the XFRM |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 2503 | * dst which causes it's ->obsolete field to be set to |
| 2504 | * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like |
| 2505 | * this, we want to force a new route lookup. |
David S. Miller | 399c180 | 2005-12-19 14:23:23 -0800 | [diff] [blame] | 2506 | */ |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 2507 | if (dst->obsolete < 0 && !stale_bundle(dst)) |
| 2508 | return dst; |
| 2509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | return NULL; |
| 2511 | } |
| 2512 | |
| 2513 | static int stale_bundle(struct dst_entry *dst) |
| 2514 | { |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 2515 | return !xfrm_bundle_ok((struct xfrm_dst *)dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 2518 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | while ((dst = dst->child) && dst->xfrm && dst->dev == dev) { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2521 | dst->dev = dev_net(dev)->loopback_dev; |
Daniel Lezcano | de3cb74 | 2007-09-25 19:16:28 -0700 | [diff] [blame] | 2522 | dev_hold(dst->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | dev_put(dev); |
| 2524 | } |
| 2525 | } |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 2526 | EXPORT_SYMBOL(xfrm_dst_ifdown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
| 2528 | static void xfrm_link_failure(struct sk_buff *skb) |
| 2529 | { |
| 2530 | /* Impossible. Such dst must be popped before reaches point of failure. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst) |
| 2534 | { |
| 2535 | if (dst) { |
| 2536 | if (dst->obsolete) { |
| 2537 | dst_release(dst); |
| 2538 | dst = NULL; |
| 2539 | } |
| 2540 | } |
| 2541 | return dst; |
| 2542 | } |
| 2543 | |
Alexey Dobriyan | ddcfd79 | 2008-11-25 17:37:23 -0800 | [diff] [blame] | 2544 | static void __xfrm_garbage_collect(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2546 | struct dst_entry *head, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2548 | spin_lock_bh(&xfrm_policy_sk_bundle_lock); |
| 2549 | head = xfrm_policy_sk_bundles; |
| 2550 | xfrm_policy_sk_bundles = NULL; |
| 2551 | spin_unlock_bh(&xfrm_policy_sk_bundle_lock); |
| 2552 | |
| 2553 | while (head) { |
| 2554 | next = head->next; |
| 2555 | dst_free(head); |
| 2556 | head = next; |
| 2557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2560 | static void xfrm_garbage_collect(struct net *net) |
| 2561 | { |
| 2562 | flow_cache_flush(); |
| 2563 | __xfrm_garbage_collect(net); |
| 2564 | } |
| 2565 | |
| 2566 | static void xfrm_garbage_collect_deferred(struct net *net) |
| 2567 | { |
| 2568 | flow_cache_flush_deferred(); |
| 2569 | __xfrm_garbage_collect(net); |
| 2570 | } |
| 2571 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2572 | static void xfrm_init_pmtu(struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | { |
| 2574 | do { |
| 2575 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 2576 | u32 pmtu, route_mtu_cached; |
| 2577 | |
| 2578 | pmtu = dst_mtu(dst->child); |
| 2579 | xdst->child_mtu_cached = pmtu; |
| 2580 | |
| 2581 | pmtu = xfrm_state_mtu(dst->xfrm, pmtu); |
| 2582 | |
| 2583 | route_mtu_cached = dst_mtu(xdst->route); |
| 2584 | xdst->route_mtu_cached = route_mtu_cached; |
| 2585 | |
| 2586 | if (pmtu > route_mtu_cached) |
| 2587 | pmtu = route_mtu_cached; |
| 2588 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2589 | dst_metric_set(dst, RTAX_MTU, pmtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | } while ((dst = dst->next)); |
| 2591 | } |
| 2592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | /* Check that the bundle accepts the flow and its components are |
| 2594 | * still valid. |
| 2595 | */ |
| 2596 | |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 2597 | static int xfrm_bundle_ok(struct xfrm_dst *first) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | { |
| 2599 | struct dst_entry *dst = &first->u.dst; |
| 2600 | struct xfrm_dst *last; |
| 2601 | u32 mtu; |
| 2602 | |
Hideaki YOSHIFUJI | 92d63dec | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 2603 | if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | (dst->dev && !netif_running(dst->dev))) |
| 2605 | return 0; |
| 2606 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2607 | if (dst->flags & DST_XFRM_QUEUE) |
| 2608 | return 1; |
| 2609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | last = NULL; |
| 2611 | |
| 2612 | do { |
| 2613 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 2614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | if (dst->xfrm->km.state != XFRM_STATE_VALID) |
| 2616 | return 0; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2617 | if (xdst->xfrm_genid != dst->xfrm->genid) |
| 2618 | return 0; |
Timo Teräs | b1312c8 | 2010-06-24 14:35:00 -0700 | [diff] [blame] | 2619 | if (xdst->num_pols > 0 && |
| 2620 | xdst->policy_genid != atomic_read(&xdst->pols[0]->genid)) |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 2621 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
| 2623 | mtu = dst_mtu(dst->child); |
| 2624 | if (xdst->child_mtu_cached != mtu) { |
| 2625 | last = xdst; |
| 2626 | xdst->child_mtu_cached = mtu; |
| 2627 | } |
| 2628 | |
Hideaki YOSHIFUJI | 92d63dec | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 2629 | if (!dst_check(xdst->route, xdst->route_cookie)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | return 0; |
| 2631 | mtu = dst_mtu(xdst->route); |
| 2632 | if (xdst->route_mtu_cached != mtu) { |
| 2633 | last = xdst; |
| 2634 | xdst->route_mtu_cached = mtu; |
| 2635 | } |
| 2636 | |
| 2637 | dst = dst->child; |
| 2638 | } while (dst->xfrm); |
| 2639 | |
| 2640 | if (likely(!last)) |
| 2641 | return 1; |
| 2642 | |
| 2643 | mtu = last->child_mtu_cached; |
| 2644 | for (;;) { |
| 2645 | dst = &last->u.dst; |
| 2646 | |
| 2647 | mtu = xfrm_state_mtu(dst->xfrm, mtu); |
| 2648 | if (mtu > last->route_mtu_cached) |
| 2649 | mtu = last->route_mtu_cached; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2650 | dst_metric_set(dst, RTAX_MTU, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | |
| 2652 | if (last == first) |
| 2653 | break; |
| 2654 | |
Patrick McHardy | bd0bf07 | 2007-07-18 01:55:52 -0700 | [diff] [blame] | 2655 | last = (struct xfrm_dst *)last->u.dst.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | last->child_mtu_cached = mtu; |
| 2657 | } |
| 2658 | |
| 2659 | return 1; |
| 2660 | } |
| 2661 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2662 | static unsigned int xfrm_default_advmss(const struct dst_entry *dst) |
| 2663 | { |
| 2664 | return dst_metric_advmss(dst->path); |
| 2665 | } |
| 2666 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2667 | static unsigned int xfrm_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2668 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 2669 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 2670 | |
| 2671 | return mtu ? : dst_mtu(dst->path); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 2672 | } |
| 2673 | |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 2674 | static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst, |
| 2675 | struct sk_buff *skb, |
| 2676 | const void *daddr) |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2677 | { |
David S. Miller | f894cbf | 2012-07-02 21:52:24 -0700 | [diff] [blame] | 2678 | return dst->path->ops->neigh_lookup(dst, skb, daddr); |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2682 | { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2683 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | int err = 0; |
| 2685 | if (unlikely(afinfo == NULL)) |
| 2686 | return -EINVAL; |
| 2687 | if (unlikely(afinfo->family >= NPROTO)) |
| 2688 | return -EAFNOSUPPORT; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2689 | spin_lock(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL)) |
| 2691 | err = -ENOBUFS; |
| 2692 | else { |
| 2693 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 2694 | if (likely(dst_ops->kmem_cachep == NULL)) |
| 2695 | dst_ops->kmem_cachep = xfrm_dst_cache; |
| 2696 | if (likely(dst_ops->check == NULL)) |
| 2697 | dst_ops->check = xfrm_dst_check; |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 2698 | if (likely(dst_ops->default_advmss == NULL)) |
| 2699 | dst_ops->default_advmss = xfrm_default_advmss; |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 2700 | if (likely(dst_ops->mtu == NULL)) |
| 2701 | dst_ops->mtu = xfrm_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | if (likely(dst_ops->negative_advice == NULL)) |
| 2703 | dst_ops->negative_advice = xfrm_negative_advice; |
| 2704 | if (likely(dst_ops->link_failure == NULL)) |
| 2705 | dst_ops->link_failure = xfrm_link_failure; |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 2706 | if (likely(dst_ops->neigh_lookup == NULL)) |
| 2707 | dst_ops->neigh_lookup = xfrm_neigh_lookup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | if (likely(afinfo->garbage_collect == NULL)) |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2709 | afinfo->garbage_collect = xfrm_garbage_collect_deferred; |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 2710 | rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | } |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2712 | spin_unlock(&xfrm_policy_afinfo_lock); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2713 | |
| 2714 | rtnl_lock(); |
| 2715 | for_each_net(net) { |
| 2716 | struct dst_ops *xfrm_dst_ops; |
| 2717 | |
| 2718 | switch (afinfo->family) { |
| 2719 | case AF_INET: |
| 2720 | xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops; |
| 2721 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 2722 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2723 | case AF_INET6: |
| 2724 | xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops; |
| 2725 | break; |
| 2726 | #endif |
| 2727 | default: |
| 2728 | BUG(); |
| 2729 | } |
| 2730 | *xfrm_dst_ops = *afinfo->dst_ops; |
| 2731 | } |
| 2732 | rtnl_unlock(); |
| 2733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | return err; |
| 2735 | } |
| 2736 | EXPORT_SYMBOL(xfrm_policy_register_afinfo); |
| 2737 | |
| 2738 | int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) |
| 2739 | { |
| 2740 | int err = 0; |
| 2741 | if (unlikely(afinfo == NULL)) |
| 2742 | return -EINVAL; |
| 2743 | if (unlikely(afinfo->family >= NPROTO)) |
| 2744 | return -EAFNOSUPPORT; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2745 | spin_lock(&xfrm_policy_afinfo_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) { |
| 2747 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo)) |
| 2748 | err = -EINVAL; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2749 | else |
| 2750 | RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family], |
| 2751 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | } |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2753 | spin_unlock(&xfrm_policy_afinfo_lock); |
| 2754 | if (!err) { |
| 2755 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 2756 | |
| 2757 | synchronize_rcu(); |
| 2758 | |
| 2759 | dst_ops->kmem_cachep = NULL; |
| 2760 | dst_ops->check = NULL; |
| 2761 | dst_ops->negative_advice = NULL; |
| 2762 | dst_ops->link_failure = NULL; |
| 2763 | afinfo->garbage_collect = NULL; |
| 2764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | return err; |
| 2766 | } |
| 2767 | EXPORT_SYMBOL(xfrm_policy_unregister_afinfo); |
| 2768 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2769 | static void __net_init xfrm_dst_ops_init(struct net *net) |
| 2770 | { |
| 2771 | struct xfrm_policy_afinfo *afinfo; |
| 2772 | |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2773 | rcu_read_lock(); |
| 2774 | afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2775 | if (afinfo) |
| 2776 | net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 2777 | #if IS_ENABLED(CONFIG_IPV6) |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 2778 | afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2779 | if (afinfo) |
| 2780 | net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops; |
| 2781 | #endif |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 2782 | rcu_read_unlock(); |
Herbert Xu | 546be24 | 2006-05-27 23:03:58 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 2786 | { |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 2787 | struct net_device *dev = ptr; |
| 2788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | switch (event) { |
| 2790 | case NETDEV_DOWN: |
Steffen Klassert | c0ed1c1 | 2011-12-21 16:48:08 -0500 | [diff] [blame] | 2791 | xfrm_garbage_collect(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | } |
| 2793 | return NOTIFY_DONE; |
| 2794 | } |
| 2795 | |
| 2796 | static struct notifier_block xfrm_dev_notifier = { |
Alexey Dobriyan | d5917a3 | 2008-10-31 00:41:59 -0700 | [diff] [blame] | 2797 | .notifier_call = xfrm_dev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | }; |
| 2799 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2800 | #ifdef CONFIG_XFRM_STATISTICS |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2801 | static int __net_init xfrm_statistics_init(struct net *net) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2802 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2803 | int rv; |
| 2804 | |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 2805 | if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics, |
Eric Dumazet | 1823e4c8 | 2010-06-22 20:58:41 +0000 | [diff] [blame] | 2806 | sizeof(struct linux_xfrm_mib), |
| 2807 | __alignof__(struct linux_xfrm_mib)) < 0) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2808 | return -ENOMEM; |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2809 | rv = xfrm_proc_init(net); |
| 2810 | if (rv < 0) |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 2811 | snmp_mib_free((void __percpu **)net->mib.xfrm_statistics); |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2812 | return rv; |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2813 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2814 | |
| 2815 | static void xfrm_statistics_fini(struct net *net) |
| 2816 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 2817 | xfrm_proc_fini(net); |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 2818 | snmp_mib_free((void __percpu **)net->mib.xfrm_statistics); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2819 | } |
| 2820 | #else |
| 2821 | static int __net_init xfrm_statistics_init(struct net *net) |
| 2822 | { |
| 2823 | return 0; |
| 2824 | } |
| 2825 | |
| 2826 | static void xfrm_statistics_fini(struct net *net) |
| 2827 | { |
| 2828 | } |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 2829 | #endif |
| 2830 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2831 | static int __net_init xfrm_policy_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2833 | unsigned int hmask, sz; |
| 2834 | int dir; |
| 2835 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2836 | if (net_eq(net, &init_net)) |
| 2837 | xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | sizeof(struct xfrm_dst), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2839 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2840 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2842 | hmask = 8 - 1; |
| 2843 | sz = (hmask+1) * sizeof(struct hlist_head); |
| 2844 | |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2845 | net->xfrm.policy_byidx = xfrm_hash_alloc(sz); |
| 2846 | if (!net->xfrm.policy_byidx) |
| 2847 | goto out_byidx; |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 2848 | net->xfrm.policy_idx_hmask = hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2849 | |
| 2850 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
| 2851 | struct xfrm_policy_hash *htab; |
| 2852 | |
Alexey Dobriyan | dc2caba | 2008-11-25 17:24:15 -0800 | [diff] [blame] | 2853 | net->xfrm.policy_count[dir] = 0; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 2854 | INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2855 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2856 | htab = &net->xfrm.policy_bydst[dir]; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 2857 | htab->table = xfrm_hash_alloc(sz); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2858 | if (!htab->table) |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2859 | goto out_bydst; |
| 2860 | htab->hmask = hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2861 | } |
| 2862 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 2863 | INIT_LIST_HEAD(&net->xfrm.policy_all); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 2864 | INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2865 | if (net_eq(net, &init_net)) |
| 2866 | register_netdevice_notifier(&xfrm_dev_notifier); |
| 2867 | return 0; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2868 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2869 | out_bydst: |
| 2870 | for (dir--; dir >= 0; dir--) { |
| 2871 | struct xfrm_policy_hash *htab; |
| 2872 | |
| 2873 | htab = &net->xfrm.policy_bydst[dir]; |
| 2874 | xfrm_hash_free(htab->table, sz); |
| 2875 | } |
| 2876 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2877 | out_byidx: |
| 2878 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2881 | static void xfrm_policy_fini(struct net *net) |
| 2882 | { |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2883 | struct xfrm_audit audit_info; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2884 | unsigned int sz; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 2885 | int dir; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2886 | |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2887 | flush_work(&net->xfrm.policy_hash_work); |
| 2888 | #ifdef CONFIG_XFRM_SUB_POLICY |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 2889 | audit_info.loginuid = INVALID_UID; |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2890 | audit_info.sessionid = -1; |
| 2891 | audit_info.secid = 0; |
| 2892 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info); |
| 2893 | #endif |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 2894 | audit_info.loginuid = INVALID_UID; |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2895 | audit_info.sessionid = -1; |
| 2896 | audit_info.secid = 0; |
| 2897 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 2898 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 2899 | WARN_ON(!list_empty(&net->xfrm.policy_all)); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2900 | |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 2901 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2902 | struct xfrm_policy_hash *htab; |
| 2903 | |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 2904 | WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir])); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2905 | |
| 2906 | htab = &net->xfrm.policy_bydst[dir]; |
Michal Kubecek | 5b653b2 | 2013-01-18 16:03:48 +0100 | [diff] [blame] | 2907 | sz = (htab->hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 2908 | WARN_ON(!hlist_empty(htab->table)); |
| 2909 | xfrm_hash_free(htab->table, sz); |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 2910 | } |
| 2911 | |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 2912 | sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 2913 | WARN_ON(!hlist_empty(net->xfrm.policy_byidx)); |
| 2914 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | static int __net_init xfrm_net_init(struct net *net) |
| 2918 | { |
| 2919 | int rv; |
| 2920 | |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2921 | rv = xfrm_statistics_init(net); |
| 2922 | if (rv < 0) |
| 2923 | goto out_statistics; |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2924 | rv = xfrm_state_init(net); |
| 2925 | if (rv < 0) |
| 2926 | goto out_state; |
| 2927 | rv = xfrm_policy_init(net); |
| 2928 | if (rv < 0) |
| 2929 | goto out_policy; |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2930 | xfrm_dst_ops_init(net); |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 2931 | rv = xfrm_sysctl_init(net); |
| 2932 | if (rv < 0) |
| 2933 | goto out_sysctl; |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2934 | return 0; |
| 2935 | |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 2936 | out_sysctl: |
| 2937 | xfrm_policy_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2938 | out_policy: |
| 2939 | xfrm_state_fini(net); |
| 2940 | out_state: |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2941 | xfrm_statistics_fini(net); |
| 2942 | out_statistics: |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2943 | return rv; |
| 2944 | } |
| 2945 | |
| 2946 | static void __net_exit xfrm_net_exit(struct net *net) |
| 2947 | { |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 2948 | xfrm_sysctl_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2949 | xfrm_policy_fini(net); |
| 2950 | xfrm_state_fini(net); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 2951 | xfrm_statistics_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | static struct pernet_operations __net_initdata xfrm_net_ops = { |
| 2955 | .init = xfrm_net_init, |
| 2956 | .exit = xfrm_net_exit, |
| 2957 | }; |
| 2958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | void __init xfrm_init(void) |
| 2960 | { |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 2961 | register_pernet_subsys(&xfrm_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | xfrm_input_init(); |
| 2963 | } |
| 2964 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2965 | #ifdef CONFIG_AUDITSYSCALL |
Ilpo Järvinen | 1486cbd7 | 2008-01-12 03:20:03 -0800 | [diff] [blame] | 2966 | static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp, |
| 2967 | struct audit_buffer *audit_buf) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2968 | { |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2969 | struct xfrm_sec_ctx *ctx = xp->security; |
| 2970 | struct xfrm_selector *sel = &xp->selector; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2971 | |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2972 | if (ctx) |
| 2973 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", |
| 2974 | ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); |
| 2975 | |
| 2976 | switch(sel->family) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2977 | case AF_INET: |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 2978 | audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2979 | if (sel->prefixlen_s != 32) |
| 2980 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 2981 | sel->prefixlen_s); |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 2982 | audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2983 | if (sel->prefixlen_d != 32) |
| 2984 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 2985 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2986 | break; |
| 2987 | case AF_INET6: |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2988 | audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2989 | if (sel->prefixlen_s != 128) |
| 2990 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 2991 | sel->prefixlen_s); |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 2992 | audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 2993 | if (sel->prefixlen_d != 128) |
| 2994 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 2995 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 2996 | break; |
| 2997 | } |
| 2998 | } |
| 2999 | |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 3000 | void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 3001 | kuid_t auid, u32 sessionid, u32 secid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3002 | { |
| 3003 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3004 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3005 | audit_buf = xfrm_audit_start("SPD-add"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3006 | if (audit_buf == NULL) |
| 3007 | return; |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 3008 | xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3009 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3010 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 3011 | audit_log_end(audit_buf); |
| 3012 | } |
| 3013 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_add); |
| 3014 | |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 3015 | void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 3016 | kuid_t auid, u32 sessionid, u32 secid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3017 | { |
| 3018 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3019 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3020 | audit_buf = xfrm_audit_start("SPD-delete"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3021 | if (audit_buf == NULL) |
| 3022 | return; |
Eric Paris | 2532386 | 2008-04-18 10:09:25 -0400 | [diff] [blame] | 3023 | xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 3024 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 3025 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 3026 | audit_log_end(audit_buf); |
| 3027 | } |
| 3028 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete); |
| 3029 | #endif |
| 3030 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3031 | #ifdef CONFIG_XFRM_MIGRATE |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3032 | static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp, |
| 3033 | const struct xfrm_selector *sel_tgt) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3034 | { |
| 3035 | if (sel_cmp->proto == IPSEC_ULPROTO_ANY) { |
| 3036 | if (sel_tgt->family == sel_cmp->family && |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3037 | xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr, |
| 3038 | sel_cmp->family) && |
| 3039 | xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr, |
| 3040 | sel_cmp->family) && |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3041 | sel_tgt->prefixlen_d == sel_cmp->prefixlen_d && |
| 3042 | sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3043 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3044 | } |
| 3045 | } else { |
| 3046 | if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3047 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3048 | } |
| 3049 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 3050 | return false; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3051 | } |
| 3052 | |
David S. Miller | b4b7c0b | 2011-02-24 00:35:06 -0500 | [diff] [blame] | 3053 | static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel, |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3054 | u8 dir, u8 type) |
| 3055 | { |
| 3056 | struct xfrm_policy *pol, *ret = NULL; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3057 | struct hlist_head *chain; |
| 3058 | u32 priority = ~0U; |
| 3059 | |
| 3060 | read_lock_bh(&xfrm_policy_lock); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 3061 | chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3062 | hlist_for_each_entry(pol, chain, bydst) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3063 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
| 3064 | pol->type == type) { |
| 3065 | ret = pol; |
| 3066 | priority = ret->priority; |
| 3067 | break; |
| 3068 | } |
| 3069 | } |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 3070 | chain = &init_net.xfrm.policy_inexact[dir]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 3071 | hlist_for_each_entry(pol, chain, bydst) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3072 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
| 3073 | pol->type == type && |
| 3074 | pol->priority < priority) { |
| 3075 | ret = pol; |
| 3076 | break; |
| 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | if (ret) |
| 3081 | xfrm_pol_hold(ret); |
| 3082 | |
| 3083 | read_unlock_bh(&xfrm_policy_lock); |
| 3084 | |
| 3085 | return ret; |
| 3086 | } |
| 3087 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 3088 | static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3089 | { |
| 3090 | int match = 0; |
| 3091 | |
| 3092 | if (t->mode == m->mode && t->id.proto == m->proto && |
| 3093 | (m->reqid == 0 || t->reqid == m->reqid)) { |
| 3094 | switch (t->mode) { |
| 3095 | case XFRM_MODE_TUNNEL: |
| 3096 | case XFRM_MODE_BEET: |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3097 | if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr, |
| 3098 | m->old_family) && |
| 3099 | xfrm_addr_equal(&t->saddr, &m->old_saddr, |
| 3100 | m->old_family)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3101 | match = 1; |
| 3102 | } |
| 3103 | break; |
| 3104 | case XFRM_MODE_TRANSPORT: |
| 3105 | /* in case of transport mode, template does not store |
| 3106 | any IP addresses, hence we just compare mode and |
| 3107 | protocol */ |
| 3108 | match = 1; |
| 3109 | break; |
| 3110 | default: |
| 3111 | break; |
| 3112 | } |
| 3113 | } |
| 3114 | return match; |
| 3115 | } |
| 3116 | |
| 3117 | /* update endpoint address(es) of template(s) */ |
| 3118 | static int xfrm_policy_migrate(struct xfrm_policy *pol, |
| 3119 | struct xfrm_migrate *m, int num_migrate) |
| 3120 | { |
| 3121 | struct xfrm_migrate *mp; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3122 | int i, j, n = 0; |
| 3123 | |
| 3124 | write_lock_bh(&pol->lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 3125 | if (unlikely(pol->walk.dead)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3126 | /* target policy has been deleted */ |
| 3127 | write_unlock_bh(&pol->lock); |
| 3128 | return -ENOENT; |
| 3129 | } |
| 3130 | |
| 3131 | for (i = 0; i < pol->xfrm_nr; i++) { |
| 3132 | for (j = 0, mp = m; j < num_migrate; j++, mp++) { |
| 3133 | if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i])) |
| 3134 | continue; |
| 3135 | n++; |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 3136 | if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL && |
| 3137 | pol->xfrm_vec[i].mode != XFRM_MODE_BEET) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3138 | continue; |
| 3139 | /* update endpoints */ |
| 3140 | memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr, |
| 3141 | sizeof(pol->xfrm_vec[i].id.daddr)); |
| 3142 | memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr, |
| 3143 | sizeof(pol->xfrm_vec[i].saddr)); |
| 3144 | pol->xfrm_vec[i].encap_family = mp->new_family; |
| 3145 | /* flush bundles */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3146 | atomic_inc(&pol->genid); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | write_unlock_bh(&pol->lock); |
| 3151 | |
| 3152 | if (!n) |
| 3153 | return -ENODATA; |
| 3154 | |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 3158 | static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3159 | { |
| 3160 | int i, j; |
| 3161 | |
| 3162 | if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH) |
| 3163 | return -EINVAL; |
| 3164 | |
| 3165 | for (i = 0; i < num_migrate; i++) { |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 3166 | if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr, |
| 3167 | m[i].old_family) && |
| 3168 | xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr, |
| 3169 | m[i].old_family)) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3170 | return -EINVAL; |
| 3171 | if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) || |
| 3172 | xfrm_addr_any(&m[i].new_saddr, m[i].new_family)) |
| 3173 | return -EINVAL; |
| 3174 | |
| 3175 | /* check if there is any duplicated entry */ |
| 3176 | for (j = i + 1; j < num_migrate; j++) { |
| 3177 | if (!memcmp(&m[i].old_daddr, &m[j].old_daddr, |
| 3178 | sizeof(m[i].old_daddr)) && |
| 3179 | !memcmp(&m[i].old_saddr, &m[j].old_saddr, |
| 3180 | sizeof(m[i].old_saddr)) && |
| 3181 | m[i].proto == m[j].proto && |
| 3182 | m[i].mode == m[j].mode && |
| 3183 | m[i].reqid == m[j].reqid && |
| 3184 | m[i].old_family == m[j].old_family) |
| 3185 | return -EINVAL; |
| 3186 | } |
| 3187 | } |
| 3188 | |
| 3189 | return 0; |
| 3190 | } |
| 3191 | |
David S. Miller | b4b7c0b | 2011-02-24 00:35:06 -0500 | [diff] [blame] | 3192 | int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 3193 | struct xfrm_migrate *m, int num_migrate, |
| 3194 | struct xfrm_kmaddress *k) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3195 | { |
| 3196 | int i, err, nx_cur = 0, nx_new = 0; |
| 3197 | struct xfrm_policy *pol = NULL; |
| 3198 | struct xfrm_state *x, *xc; |
| 3199 | struct xfrm_state *x_cur[XFRM_MAX_DEPTH]; |
| 3200 | struct xfrm_state *x_new[XFRM_MAX_DEPTH]; |
| 3201 | struct xfrm_migrate *mp; |
| 3202 | |
| 3203 | if ((err = xfrm_migrate_check(m, num_migrate)) < 0) |
| 3204 | goto out; |
| 3205 | |
| 3206 | /* Stage 1 - find policy */ |
| 3207 | if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) { |
| 3208 | err = -ENOENT; |
| 3209 | goto out; |
| 3210 | } |
| 3211 | |
| 3212 | /* Stage 2 - find and update state(s) */ |
| 3213 | for (i = 0, mp = m; i < num_migrate; i++, mp++) { |
| 3214 | if ((x = xfrm_migrate_state_find(mp))) { |
| 3215 | x_cur[nx_cur] = x; |
| 3216 | nx_cur++; |
| 3217 | if ((xc = xfrm_state_migrate(x, mp))) { |
| 3218 | x_new[nx_new] = xc; |
| 3219 | nx_new++; |
| 3220 | } else { |
| 3221 | err = -ENODATA; |
| 3222 | goto restore_state; |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /* Stage 3 - update policy */ |
| 3228 | if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0) |
| 3229 | goto restore_state; |
| 3230 | |
| 3231 | /* Stage 4 - delete old state(s) */ |
| 3232 | if (nx_cur) { |
| 3233 | xfrm_states_put(x_cur, nx_cur); |
| 3234 | xfrm_states_delete(x_cur, nx_cur); |
| 3235 | } |
| 3236 | |
| 3237 | /* Stage 5 - announce */ |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 3238 | km_migrate(sel, dir, type, m, num_migrate, k); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3239 | |
| 3240 | xfrm_pol_put(pol); |
| 3241 | |
| 3242 | return 0; |
| 3243 | out: |
| 3244 | return err; |
| 3245 | |
| 3246 | restore_state: |
| 3247 | if (pol) |
| 3248 | xfrm_pol_put(pol); |
| 3249 | if (nx_cur) |
| 3250 | xfrm_states_put(x_cur, nx_cur); |
| 3251 | if (nx_new) |
| 3252 | xfrm_states_delete(x_new, nx_new); |
| 3253 | |
| 3254 | return err; |
| 3255 | } |
David S. Miller | e610e67 | 2007-02-08 13:29:15 -0800 | [diff] [blame] | 3256 | EXPORT_SYMBOL(xfrm_migrate); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 3257 | #endif |