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