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