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> |
Florian Westphal | ec30d78 | 2017-07-17 13:57:27 +0200 | [diff] [blame] | 27 | #include <linux/cpu.h> |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 28 | #include <linux/audit.h> |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 29 | #include <linux/rhashtable.h> |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 30 | #include <linux/if_tunnel.h> |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 31 | #include <net/dst.h> |
Eric Paris | 6ce74ec | 2012-02-16 15:08:39 -0500 | [diff] [blame] | 32 | #include <net/flow.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <net/xfrm.h> |
| 34 | #include <net/ip.h> |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 35 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 36 | #include <net/mip6.h> |
| 37 | #endif |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 38 | #ifdef CONFIG_XFRM_STATISTICS |
| 39 | #include <net/snmp.h> |
| 40 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 42 | #include "xfrm_hash.h" |
| 43 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 44 | #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10)) |
| 45 | #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ)) |
| 46 | #define XFRM_MAX_QUEUE_LEN 100 |
| 47 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 48 | struct xfrm_flo { |
| 49 | struct dst_entry *dst_orig; |
| 50 | u8 flags; |
| 51 | }; |
| 52 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 53 | /* prefixes smaller than this are stored in lists, not trees. */ |
| 54 | #define INEXACT_PREFIXLEN_IPV4 16 |
| 55 | #define INEXACT_PREFIXLEN_IPV6 48 |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 56 | |
| 57 | struct xfrm_pol_inexact_node { |
| 58 | struct rb_node node; |
| 59 | union { |
| 60 | xfrm_address_t addr; |
| 61 | struct rcu_head rcu; |
| 62 | }; |
| 63 | u8 prefixlen; |
| 64 | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 65 | struct rb_root root; |
| 66 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 67 | /* the policies matching this node, can be empty list */ |
| 68 | struct hlist_head hhead; |
| 69 | }; |
| 70 | |
| 71 | /* xfrm inexact policy search tree: |
| 72 | * xfrm_pol_inexact_bin = hash(dir,type,family,if_id); |
| 73 | * | |
| 74 | * +---- root_d: sorted by daddr:prefix |
| 75 | * | | |
| 76 | * | xfrm_pol_inexact_node |
| 77 | * | | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 78 | * | +- root: sorted by saddr/prefix |
| 79 | * | | | |
| 80 | * | | xfrm_pol_inexact_node |
| 81 | * | | | |
| 82 | * | | + root: unused |
| 83 | * | | | |
| 84 | * | | + hhead: saddr:daddr policies |
| 85 | * | | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 86 | * | +- coarse policies and all any:daddr policies |
| 87 | * | |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 88 | * +---- root_s: sorted by saddr:prefix |
| 89 | * | | |
| 90 | * | xfrm_pol_inexact_node |
| 91 | * | | |
| 92 | * | + root: unused |
| 93 | * | | |
| 94 | * | + hhead: saddr:any policies |
| 95 | * | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 96 | * +---- coarse policies and all any:any policies |
| 97 | * |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 98 | * Lookups return four candidate lists: |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 99 | * 1. any:any list from top-level xfrm_pol_inexact_bin |
| 100 | * 2. any:daddr list from daddr tree |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 101 | * 3. saddr:daddr list from 2nd level daddr tree |
| 102 | * 4. saddr:any list from saddr tree |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 103 | * |
| 104 | * This result set then needs to be searched for the policy with |
| 105 | * the lowest priority. If two results have same prio, youngest one wins. |
| 106 | */ |
| 107 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 108 | struct xfrm_pol_inexact_key { |
| 109 | possible_net_t net; |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 110 | u32 if_id; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 111 | u16 family; |
| 112 | u8 dir, type; |
| 113 | }; |
| 114 | |
| 115 | struct xfrm_pol_inexact_bin { |
| 116 | struct xfrm_pol_inexact_key k; |
| 117 | struct rhash_head head; |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 118 | /* list containing '*:*' policies */ |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 119 | struct hlist_head hhead; |
| 120 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 121 | seqcount_t count; |
| 122 | /* tree sorted by daddr/prefix */ |
| 123 | struct rb_root root_d; |
| 124 | |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 125 | /* tree sorted by saddr/prefix */ |
| 126 | struct rb_root root_s; |
| 127 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 128 | /* slow path below */ |
| 129 | struct list_head inexact_bins; |
| 130 | struct rcu_head rcu; |
| 131 | }; |
| 132 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 133 | enum xfrm_pol_inexact_candidate_type { |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 134 | XFRM_POL_CAND_BOTH, |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 135 | XFRM_POL_CAND_SADDR, |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 136 | XFRM_POL_CAND_DADDR, |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 137 | XFRM_POL_CAND_ANY, |
| 138 | |
| 139 | XFRM_POL_CAND_MAX, |
| 140 | }; |
| 141 | |
| 142 | struct xfrm_pol_inexact_candidates { |
| 143 | struct hlist_head *res[XFRM_POL_CAND_MAX]; |
| 144 | }; |
| 145 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 146 | static DEFINE_SPINLOCK(xfrm_if_cb_lock); |
| 147 | static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly; |
| 148 | |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 149 | static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock); |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 150 | static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1] |
Priyanka Jain | 418a99a | 2012-08-12 21:22:29 +0000 | [diff] [blame] | 151 | __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Alexey Dobriyan | f8c3d0d | 2018-02-24 21:21:38 +0300 | [diff] [blame] | 153 | static struct kmem_cache *xfrm_dst_cache __ro_after_init; |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 154 | static __read_mostly seqcount_t xfrm_policy_hash_generation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 156 | static struct rhashtable xfrm_policy_inexact_table; |
| 157 | static const struct rhashtable_params xfrm_pol_inexact_params; |
| 158 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 159 | static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 160 | static int stale_bundle(struct dst_entry *dst); |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 161 | static int xfrm_bundle_ok(struct xfrm_dst *xdst); |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 162 | static void xfrm_policy_queue_process(struct timer_list *t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Herbert Xu | 12bfa8b | 2014-11-13 17:09:50 +0800 | [diff] [blame] | 164 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir); |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 165 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 166 | int dir); |
| 167 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 168 | static struct xfrm_pol_inexact_bin * |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 169 | xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir, |
| 170 | u32 if_id); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 171 | |
| 172 | static struct xfrm_pol_inexact_bin * |
| 173 | xfrm_policy_inexact_lookup_rcu(struct net *net, |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 174 | u8 type, u16 family, u8 dir, u32 if_id); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 175 | static struct xfrm_policy * |
| 176 | xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy, |
| 177 | bool excl); |
| 178 | static void xfrm_policy_insert_inexact_list(struct hlist_head *chain, |
| 179 | struct xfrm_policy *policy); |
| 180 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 181 | static bool |
| 182 | xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand, |
| 183 | struct xfrm_pol_inexact_bin *b, |
| 184 | const xfrm_address_t *saddr, |
| 185 | const xfrm_address_t *daddr); |
| 186 | |
Florian Westphal | e37cc8ad | 2016-08-11 15:17:55 +0200 | [diff] [blame] | 187 | static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy) |
| 188 | { |
Reshetova, Elena | 850a621 | 2017-07-04 15:53:22 +0300 | [diff] [blame] | 189 | return refcount_inc_not_zero(&policy->refcnt); |
Florian Westphal | e37cc8ad | 2016-08-11 15:17:55 +0200 | [diff] [blame] | 190 | } |
| 191 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 192 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 193 | __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 194 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 195 | const struct flowi4 *fl4 = &fl->u.ip4; |
| 196 | |
Alexey Dobriyan | 26bff94 | 2011-11-22 06:46:02 +0000 | [diff] [blame] | 197 | return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) && |
| 198 | addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) && |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 199 | !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) && |
| 200 | !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) && |
| 201 | (fl4->flowi4_proto == sel->proto || !sel->proto) && |
| 202 | (fl4->flowi4_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 203 | } |
| 204 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 205 | static inline bool |
David S. Miller | 200ce96 | 2011-02-24 00:12:25 -0500 | [diff] [blame] | 206 | __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 207 | { |
David S. Miller | 7e1dc7b | 2011-03-12 02:42:11 -0500 | [diff] [blame] | 208 | const struct flowi6 *fl6 = &fl->u.ip6; |
| 209 | |
| 210 | return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) && |
| 211 | addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) && |
| 212 | !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) && |
| 213 | !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) && |
| 214 | (fl6->flowi6_proto == sel->proto || !sel->proto) && |
| 215 | (fl6->flowi6_oif == sel->ifindex || !sel->ifindex); |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 216 | } |
| 217 | |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 218 | bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl, |
| 219 | unsigned short family) |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 220 | { |
| 221 | switch (family) { |
| 222 | case AF_INET: |
| 223 | return __xfrm4_selector_match(sel, fl); |
| 224 | case AF_INET6: |
| 225 | return __xfrm6_selector_match(sel, fl); |
| 226 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 227 | return false; |
Andrew Morton | 7768102 | 2006-11-08 22:46:26 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 230 | static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family) |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 231 | { |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 232 | const struct xfrm_policy_afinfo *afinfo; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 233 | |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 234 | if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo))) |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 235 | return NULL; |
| 236 | rcu_read_lock(); |
| 237 | afinfo = rcu_dereference(xfrm_policy_afinfo[family]); |
| 238 | if (unlikely(!afinfo)) |
| 239 | rcu_read_unlock(); |
| 240 | return afinfo; |
| 241 | } |
| 242 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 243 | /* Called with rcu_read_lock(). */ |
| 244 | static const struct xfrm_if_cb *xfrm_if_get_cb(void) |
| 245 | { |
| 246 | return rcu_dereference(xfrm_if_cb); |
| 247 | } |
| 248 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 249 | struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif, |
| 250 | const xfrm_address_t *saddr, |
| 251 | const xfrm_address_t *daddr, |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 252 | int family, u32 mark) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 254 | const struct xfrm_policy_afinfo *afinfo; |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 255 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 257 | afinfo = xfrm_policy_get_afinfo(family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (unlikely(afinfo == NULL)) |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 259 | return ERR_PTR(-EAFNOSUPPORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 261 | dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 262 | |
Florian Westphal | bdba9fe | 2017-02-07 15:00:18 +0100 | [diff] [blame] | 263 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 264 | |
| 265 | return dst; |
| 266 | } |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 267 | EXPORT_SYMBOL(__xfrm_dst_lookup); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 268 | |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 269 | static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, |
| 270 | int tos, int oif, |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 271 | xfrm_address_t *prev_saddr, |
| 272 | xfrm_address_t *prev_daddr, |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 273 | int family, u32 mark) |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 274 | { |
Alexey Dobriyan | c5b3cf4 | 2008-11-25 17:51:25 -0800 | [diff] [blame] | 275 | struct net *net = xs_net(x); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 276 | xfrm_address_t *saddr = &x->props.saddr; |
| 277 | xfrm_address_t *daddr = &x->id.daddr; |
| 278 | struct dst_entry *dst; |
| 279 | |
| 280 | if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) { |
| 281 | saddr = x->coaddr; |
| 282 | daddr = prev_daddr; |
| 283 | } |
| 284 | if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) { |
| 285 | saddr = prev_saddr; |
| 286 | daddr = x->coaddr; |
| 287 | } |
| 288 | |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 289 | dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark); |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 290 | |
| 291 | if (!IS_ERR(dst)) { |
| 292 | if (prev_saddr != saddr) |
| 293 | memcpy(prev_saddr, saddr, sizeof(*prev_saddr)); |
| 294 | if (prev_daddr != daddr) |
| 295 | memcpy(prev_daddr, daddr, sizeof(*prev_daddr)); |
| 296 | } |
| 297 | |
Herbert Xu | 66cdb3c | 2007-11-13 21:37:28 -0800 | [diff] [blame] | 298 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | static inline unsigned long make_jiffies(long secs) |
| 302 | { |
| 303 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) |
| 304 | return MAX_SCHEDULE_TIMEOUT-1; |
| 305 | else |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 306 | return secs*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 309 | static void xfrm_policy_timer(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 311 | struct xfrm_policy *xp = from_timer(xp, t, timer); |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 312 | time64_t now = ktime_get_real_seconds(); |
| 313 | time64_t next = TIME64_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | int warn = 0; |
| 315 | int dir; |
| 316 | |
| 317 | read_lock(&xp->lock); |
| 318 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 319 | if (unlikely(xp->walk.dead)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | goto out; |
| 321 | |
Herbert Xu | 77d8d7a | 2005-10-05 12:15:12 -0700 | [diff] [blame] | 322 | dir = xfrm_policy_id2dir(xp->index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | if (xp->lft.hard_add_expires_seconds) { |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 325 | time64_t tmo = xp->lft.hard_add_expires_seconds + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | xp->curlft.add_time - now; |
| 327 | if (tmo <= 0) |
| 328 | goto expired; |
| 329 | if (tmo < next) |
| 330 | next = tmo; |
| 331 | } |
| 332 | if (xp->lft.hard_use_expires_seconds) { |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 333 | time64_t tmo = xp->lft.hard_use_expires_seconds + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 335 | if (tmo <= 0) |
| 336 | goto expired; |
| 337 | if (tmo < next) |
| 338 | next = tmo; |
| 339 | } |
| 340 | if (xp->lft.soft_add_expires_seconds) { |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 341 | time64_t tmo = xp->lft.soft_add_expires_seconds + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | xp->curlft.add_time - now; |
| 343 | if (tmo <= 0) { |
| 344 | warn = 1; |
| 345 | tmo = XFRM_KM_TIMEOUT; |
| 346 | } |
| 347 | if (tmo < next) |
| 348 | next = tmo; |
| 349 | } |
| 350 | if (xp->lft.soft_use_expires_seconds) { |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 351 | time64_t tmo = xp->lft.soft_use_expires_seconds + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; |
| 353 | if (tmo <= 0) { |
| 354 | warn = 1; |
| 355 | tmo = XFRM_KM_TIMEOUT; |
| 356 | } |
| 357 | if (tmo < next) |
| 358 | next = tmo; |
| 359 | } |
| 360 | |
| 361 | if (warn) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 362 | km_policy_expired(xp, dir, 0, 0); |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 363 | if (next != TIME64_MAX && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | !mod_timer(&xp->timer, jiffies + make_jiffies(next))) |
| 365 | xfrm_pol_hold(xp); |
| 366 | |
| 367 | out: |
| 368 | read_unlock(&xp->lock); |
| 369 | xfrm_pol_put(xp); |
| 370 | return; |
| 371 | |
| 372 | expired: |
| 373 | read_unlock(&xp->lock); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 374 | if (!xfrm_policy_delete(xp, dir)) |
Jamal Hadi Salim | 6c5c8ca | 2006-03-20 19:17:25 -0800 | [diff] [blame] | 375 | km_policy_expired(xp, dir, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | xfrm_pol_put(xp); |
| 377 | } |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2 |
| 380 | * SPD calls. |
| 381 | */ |
| 382 | |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 383 | struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
| 385 | struct xfrm_policy *policy; |
| 386 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 387 | policy = kzalloc(sizeof(struct xfrm_policy), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | if (policy) { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 390 | write_pnet(&policy->xp_net, net); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 391 | INIT_LIST_HEAD(&policy->walk.all); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 392 | INIT_HLIST_NODE(&policy->bydst_inexact_list); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 393 | INIT_HLIST_NODE(&policy->bydst); |
| 394 | INIT_HLIST_NODE(&policy->byidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | rwlock_init(&policy->lock); |
Reshetova, Elena | 850a621 | 2017-07-04 15:53:22 +0300 | [diff] [blame] | 396 | refcount_set(&policy->refcnt, 1); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 397 | skb_queue_head_init(&policy->polq.hold_queue); |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 398 | timer_setup(&policy->timer, xfrm_policy_timer, 0); |
| 399 | timer_setup(&policy->polq.hold_timer, |
| 400 | xfrm_policy_queue_process, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | return policy; |
| 403 | } |
| 404 | EXPORT_SYMBOL(xfrm_policy_alloc); |
| 405 | |
Eric Dumazet | 56f0473 | 2015-12-08 07:22:01 -0800 | [diff] [blame] | 406 | static void xfrm_policy_destroy_rcu(struct rcu_head *head) |
| 407 | { |
| 408 | struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu); |
| 409 | |
| 410 | security_xfrm_policy_free(policy->security); |
| 411 | kfree(policy); |
| 412 | } |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | /* Destroy xfrm_policy: descendant resources must be released to this moment. */ |
| 415 | |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 416 | void xfrm_policy_destroy(struct xfrm_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 418 | BUG_ON(!policy->walk.dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Fan Du | 0659eea | 2013-08-01 18:08:36 +0800 | [diff] [blame] | 420 | if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | BUG(); |
| 422 | |
Eric Dumazet | 56f0473 | 2015-12-08 07:22:01 -0800 | [diff] [blame] | 423 | call_rcu(&policy->rcu, xfrm_policy_destroy_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
WANG Cong | 64c31b3 | 2008-01-07 22:34:29 -0800 | [diff] [blame] | 425 | EXPORT_SYMBOL(xfrm_policy_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Alexander Alemayhu | 1365e547c | 2017-01-03 17:13:20 +0100 | [diff] [blame] | 427 | /* Rule must be locked. Release descendant resources, announce |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * entry dead. The rule must be unlinked from lists to the moment. |
| 429 | */ |
| 430 | |
| 431 | static void xfrm_policy_kill(struct xfrm_policy *policy) |
| 432 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 433 | policy->walk.dead = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 435 | atomic_inc(&policy->genid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 437 | if (del_timer(&policy->polq.hold_timer)) |
| 438 | xfrm_pol_put(policy); |
Li RongQing | 1ee5e66 | 2015-04-22 15:51:16 +0800 | [diff] [blame] | 439 | skb_queue_purge(&policy->polq.hold_queue); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 440 | |
Timo Teräs | 285ead1 | 2010-04-07 00:30:06 +0000 | [diff] [blame] | 441 | if (del_timer(&policy->timer)) |
| 442 | xfrm_pol_put(policy); |
| 443 | |
| 444 | xfrm_pol_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 447 | static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024; |
| 448 | |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 449 | static inline unsigned int idx_hash(struct net *net, u32 index) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 450 | { |
Alexey Dobriyan | e92303f | 2008-11-25 17:32:41 -0800 | [diff] [blame] | 451 | return __idx_hash(index, net->xfrm.policy_idx_hmask); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 454 | /* calculate policy hash thresholds */ |
| 455 | static void __get_hash_thresh(struct net *net, |
| 456 | unsigned short family, int dir, |
| 457 | u8 *dbits, u8 *sbits) |
| 458 | { |
| 459 | switch (family) { |
| 460 | case AF_INET: |
| 461 | *dbits = net->xfrm.policy_bydst[dir].dbits4; |
| 462 | *sbits = net->xfrm.policy_bydst[dir].sbits4; |
| 463 | break; |
| 464 | |
| 465 | case AF_INET6: |
| 466 | *dbits = net->xfrm.policy_bydst[dir].dbits6; |
| 467 | *sbits = net->xfrm.policy_bydst[dir].sbits6; |
| 468 | break; |
| 469 | |
| 470 | default: |
| 471 | *dbits = 0; |
| 472 | *sbits = 0; |
| 473 | } |
| 474 | } |
| 475 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 476 | static struct hlist_head *policy_hash_bysel(struct net *net, |
| 477 | const struct xfrm_selector *sel, |
| 478 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 479 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 480 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 481 | unsigned int hash; |
| 482 | u8 dbits; |
| 483 | u8 sbits; |
| 484 | |
| 485 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
| 486 | hash = __sel_hash(sel, family, hmask, dbits, sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 487 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 488 | if (hash == hmask + 1) |
Florian Westphal | cc1bb84 | 2018-11-07 23:00:34 +0100 | [diff] [blame] | 489 | return NULL; |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 490 | |
| 491 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, |
| 492 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 493 | } |
| 494 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 495 | static struct hlist_head *policy_hash_direct(struct net *net, |
| 496 | const xfrm_address_t *daddr, |
| 497 | const xfrm_address_t *saddr, |
| 498 | unsigned short family, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 499 | { |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 500 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 501 | unsigned int hash; |
| 502 | u8 dbits; |
| 503 | u8 sbits; |
| 504 | |
| 505 | __get_hash_thresh(net, family, dir, &dbits, &sbits); |
| 506 | hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 507 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 508 | return rcu_dereference_check(net->xfrm.policy_bydst[dir].table, |
| 509 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 512 | static void xfrm_dst_hash_transfer(struct net *net, |
| 513 | struct hlist_head *list, |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 514 | struct hlist_head *ndsttable, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 515 | unsigned int nhashmask, |
| 516 | int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 517 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 518 | struct hlist_node *tmp, *entry0 = NULL; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 519 | struct xfrm_policy *pol; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 520 | unsigned int h0 = 0; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 521 | u8 dbits; |
| 522 | u8 sbits; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 523 | |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 524 | redo: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 525 | hlist_for_each_entry_safe(pol, tmp, list, bydst) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 526 | unsigned int h; |
| 527 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 528 | __get_hash_thresh(net, pol->family, dir, &dbits, &sbits); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 529 | h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 530 | pol->family, nhashmask, dbits, sbits); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 531 | if (!entry0) { |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 532 | hlist_del_rcu(&pol->bydst); |
| 533 | hlist_add_head_rcu(&pol->bydst, ndsttable + h); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 534 | h0 = h; |
| 535 | } else { |
| 536 | if (h != h0) |
| 537 | continue; |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 538 | hlist_del_rcu(&pol->bydst); |
| 539 | hlist_add_behind_rcu(&pol->bydst, entry0); |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 540 | } |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 541 | entry0 = &pol->bydst; |
YOSHIFUJI Hideaki | b791160 | 2008-02-17 23:29:30 -0800 | [diff] [blame] | 542 | } |
| 543 | if (!hlist_empty(list)) { |
| 544 | entry0 = NULL; |
| 545 | goto redo; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | static void xfrm_idx_hash_transfer(struct hlist_head *list, |
| 550 | struct hlist_head *nidxtable, |
| 551 | unsigned int nhashmask) |
| 552 | { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 553 | struct hlist_node *tmp; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 554 | struct xfrm_policy *pol; |
| 555 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 556 | hlist_for_each_entry_safe(pol, tmp, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 557 | unsigned int h; |
| 558 | |
| 559 | h = __idx_hash(pol->index, nhashmask); |
| 560 | hlist_add_head(&pol->byidx, nidxtable+h); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | static unsigned long xfrm_new_hash_mask(unsigned int old_hmask) |
| 565 | { |
| 566 | return ((old_hmask + 1) << 1) - 1; |
| 567 | } |
| 568 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 569 | static void xfrm_bydst_resize(struct net *net, int dir) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 570 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 571 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 572 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 573 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 574 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 575 | struct hlist_head *odst; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 576 | int i; |
| 577 | |
| 578 | if (!ndst) |
| 579 | return; |
| 580 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 581 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 582 | write_seqcount_begin(&xfrm_policy_hash_generation); |
| 583 | |
| 584 | odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table, |
| 585 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 586 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 587 | odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table, |
| 588 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
| 589 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 590 | for (i = hmask; i >= 0; i--) |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 591 | xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 592 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 593 | rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 594 | net->xfrm.policy_bydst[dir].hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 595 | |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 596 | write_seqcount_end(&xfrm_policy_hash_generation); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 597 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 598 | |
Florian Westphal | e1e551b | 2016-08-11 15:17:53 +0200 | [diff] [blame] | 599 | synchronize_rcu(); |
| 600 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 601 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 604 | static void xfrm_byidx_resize(struct net *net, int total) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 605 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 606 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 607 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); |
| 608 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 609 | struct hlist_head *oidx = net->xfrm.policy_byidx; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 610 | struct hlist_head *nidx = xfrm_hash_alloc(nsize); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 611 | int i; |
| 612 | |
| 613 | if (!nidx) |
| 614 | return; |
| 615 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 616 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 617 | |
| 618 | for (i = hmask; i >= 0; i--) |
| 619 | xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask); |
| 620 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 621 | net->xfrm.policy_byidx = nidx; |
| 622 | net->xfrm.policy_idx_hmask = nhashmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 623 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 624 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 625 | |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 626 | xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head)); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 629 | 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] | 630 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 631 | unsigned int cnt = net->xfrm.policy_count[dir]; |
| 632 | unsigned int hmask = net->xfrm.policy_bydst[dir].hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 633 | |
| 634 | if (total) |
| 635 | *total += cnt; |
| 636 | |
| 637 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 638 | cnt > hmask) |
| 639 | return 1; |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 644 | 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] | 645 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 646 | unsigned int hmask = net->xfrm.policy_idx_hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 647 | |
| 648 | if ((hmask + 1) < xfrm_policy_hashmax && |
| 649 | total > hmask) |
| 650 | return 1; |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 655 | void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si) |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 656 | { |
Alexey Dobriyan | e071041 | 2010-01-23 13:37:10 +0000 | [diff] [blame] | 657 | si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN]; |
| 658 | si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT]; |
| 659 | si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD]; |
| 660 | si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX]; |
| 661 | si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX]; |
| 662 | si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX]; |
| 663 | si->spdhcnt = net->xfrm.policy_idx_hmask; |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 664 | si->spdhmcnt = xfrm_policy_hashmax; |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 665 | } |
| 666 | EXPORT_SYMBOL(xfrm_spd_getinfo); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 667 | |
Jamal Hadi Salim | ecfd6b1 | 2007-04-28 21:20:32 -0700 | [diff] [blame] | 668 | static DEFINE_MUTEX(hash_resize_mutex); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 669 | static void xfrm_hash_resize(struct work_struct *work) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 670 | { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 671 | 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] | 672 | int dir, total; |
| 673 | |
| 674 | mutex_lock(&hash_resize_mutex); |
| 675 | |
| 676 | total = 0; |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 677 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 678 | if (xfrm_bydst_should_resize(net, dir, &total)) |
| 679 | xfrm_bydst_resize(net, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 680 | } |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 681 | if (xfrm_byidx_should_resize(net, total)) |
| 682 | xfrm_byidx_resize(net, total); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 683 | |
| 684 | mutex_unlock(&hash_resize_mutex); |
| 685 | } |
| 686 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 687 | /* Make sure *pol can be inserted into fastbin. |
| 688 | * Useful to check that later insert requests will be sucessful |
| 689 | * (provided xfrm_policy_lock is held throughout). |
| 690 | */ |
| 691 | static struct xfrm_pol_inexact_bin * |
| 692 | xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir) |
| 693 | { |
| 694 | struct xfrm_pol_inexact_bin *bin, *prev; |
| 695 | struct xfrm_pol_inexact_key k = { |
| 696 | .family = pol->family, |
| 697 | .type = pol->type, |
| 698 | .dir = dir, |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 699 | .if_id = pol->if_id, |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 700 | }; |
| 701 | struct net *net = xp_net(pol); |
| 702 | |
| 703 | lockdep_assert_held(&net->xfrm.xfrm_policy_lock); |
| 704 | |
| 705 | write_pnet(&k.net, net); |
| 706 | bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k, |
| 707 | xfrm_pol_inexact_params); |
| 708 | if (bin) |
| 709 | return bin; |
| 710 | |
| 711 | bin = kzalloc(sizeof(*bin), GFP_ATOMIC); |
| 712 | if (!bin) |
| 713 | return NULL; |
| 714 | |
| 715 | bin->k = k; |
| 716 | INIT_HLIST_HEAD(&bin->hhead); |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 717 | bin->root_d = RB_ROOT; |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 718 | bin->root_s = RB_ROOT; |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 719 | seqcount_init(&bin->count); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 720 | |
| 721 | prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table, |
| 722 | &bin->k, &bin->head, |
| 723 | xfrm_pol_inexact_params); |
| 724 | if (!prev) { |
| 725 | list_add(&bin->inexact_bins, &net->xfrm.inexact_bins); |
| 726 | return bin; |
| 727 | } |
| 728 | |
| 729 | kfree(bin); |
| 730 | |
| 731 | return IS_ERR(prev) ? NULL : prev; |
| 732 | } |
| 733 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 734 | static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr, |
| 735 | int family, u8 prefixlen) |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 736 | { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 737 | if (xfrm_addr_any(addr, family)) |
| 738 | return true; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 739 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 740 | if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6) |
| 741 | return true; |
| 742 | |
| 743 | if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4) |
| 744 | return true; |
| 745 | |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | static bool |
| 750 | xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy) |
| 751 | { |
| 752 | const xfrm_address_t *addr; |
| 753 | bool saddr_any, daddr_any; |
| 754 | u8 prefixlen; |
| 755 | |
| 756 | addr = &policy->selector.saddr; |
| 757 | prefixlen = policy->selector.prefixlen_s; |
| 758 | |
| 759 | saddr_any = xfrm_pol_inexact_addr_use_any_list(addr, |
| 760 | policy->family, |
| 761 | prefixlen); |
| 762 | addr = &policy->selector.daddr; |
| 763 | prefixlen = policy->selector.prefixlen_d; |
| 764 | daddr_any = xfrm_pol_inexact_addr_use_any_list(addr, |
| 765 | policy->family, |
| 766 | prefixlen); |
| 767 | return saddr_any && daddr_any; |
| 768 | } |
| 769 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 770 | static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node, |
| 771 | const xfrm_address_t *addr, u8 prefixlen) |
| 772 | { |
| 773 | node->addr = *addr; |
| 774 | node->prefixlen = prefixlen; |
| 775 | } |
| 776 | |
| 777 | static struct xfrm_pol_inexact_node * |
| 778 | xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen) |
| 779 | { |
| 780 | struct xfrm_pol_inexact_node *node; |
| 781 | |
| 782 | node = kzalloc(sizeof(*node), GFP_ATOMIC); |
| 783 | if (node) |
| 784 | xfrm_pol_inexact_node_init(node, addr, prefixlen); |
| 785 | |
| 786 | return node; |
| 787 | } |
| 788 | |
| 789 | static int xfrm_policy_addr_delta(const xfrm_address_t *a, |
| 790 | const xfrm_address_t *b, |
| 791 | u8 prefixlen, u16 family) |
| 792 | { |
| 793 | unsigned int pdw, pbi; |
| 794 | int delta = 0; |
| 795 | |
| 796 | switch (family) { |
| 797 | case AF_INET: |
| 798 | if (sizeof(long) == 4 && prefixlen == 0) |
| 799 | return ntohl(a->a4) - ntohl(b->a4); |
| 800 | return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) - |
| 801 | (ntohl(b->a4) & ((~0UL << (32 - prefixlen)))); |
| 802 | case AF_INET6: |
| 803 | pdw = prefixlen >> 5; |
| 804 | pbi = prefixlen & 0x1f; |
| 805 | |
| 806 | if (pdw) { |
| 807 | delta = memcmp(a->a6, b->a6, pdw << 2); |
| 808 | if (delta) |
| 809 | return delta; |
| 810 | } |
| 811 | if (pbi) { |
| 812 | u32 mask = ~0u << (32 - pbi); |
| 813 | |
| 814 | delta = (ntohl(a->a6[pdw]) & mask) - |
| 815 | (ntohl(b->a6[pdw]) & mask); |
| 816 | } |
| 817 | break; |
| 818 | default: |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | return delta; |
| 823 | } |
| 824 | |
| 825 | static void xfrm_policy_inexact_list_reinsert(struct net *net, |
| 826 | struct xfrm_pol_inexact_node *n, |
| 827 | u16 family) |
| 828 | { |
Florian Westphal | e901cbc | 2018-11-07 23:00:39 +0100 | [diff] [blame] | 829 | unsigned int matched_s, matched_d; |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 830 | struct xfrm_policy *policy, *p; |
| 831 | |
Florian Westphal | e901cbc | 2018-11-07 23:00:39 +0100 | [diff] [blame] | 832 | matched_s = 0; |
| 833 | matched_d = 0; |
| 834 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 835 | list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) { |
Florian Westphal | 1d38900 | 2019-01-04 14:17:03 +0100 | [diff] [blame] | 836 | struct hlist_node *newpos = NULL; |
Florian Westphal | e901cbc | 2018-11-07 23:00:39 +0100 | [diff] [blame] | 837 | bool matches_s, matches_d; |
| 838 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 839 | if (!policy->bydst_reinsert) |
| 840 | continue; |
| 841 | |
| 842 | WARN_ON_ONCE(policy->family != family); |
| 843 | |
| 844 | policy->bydst_reinsert = false; |
| 845 | hlist_for_each_entry(p, &n->hhead, bydst) { |
Florian Westphal | 1d38900 | 2019-01-04 14:17:03 +0100 | [diff] [blame] | 846 | if (policy->priority > p->priority) |
| 847 | newpos = &p->bydst; |
| 848 | else if (policy->priority == p->priority && |
| 849 | policy->pos > p->pos) |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 850 | newpos = &p->bydst; |
| 851 | else |
| 852 | break; |
| 853 | } |
| 854 | |
| 855 | if (newpos) |
Florian Westphal | 355b00d | 2019-01-04 14:17:00 +0100 | [diff] [blame] | 856 | hlist_add_behind_rcu(&policy->bydst, newpos); |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 857 | else |
Florian Westphal | 355b00d | 2019-01-04 14:17:00 +0100 | [diff] [blame] | 858 | hlist_add_head_rcu(&policy->bydst, &n->hhead); |
Florian Westphal | e901cbc | 2018-11-07 23:00:39 +0100 | [diff] [blame] | 859 | |
| 860 | /* paranoia checks follow. |
| 861 | * Check that the reinserted policy matches at least |
| 862 | * saddr or daddr for current node prefix. |
| 863 | * |
| 864 | * Matching both is fine, matching saddr in one policy |
| 865 | * (but not daddr) and then matching only daddr in another |
| 866 | * is a bug. |
| 867 | */ |
| 868 | matches_s = xfrm_policy_addr_delta(&policy->selector.saddr, |
| 869 | &n->addr, |
| 870 | n->prefixlen, |
| 871 | family) == 0; |
| 872 | matches_d = xfrm_policy_addr_delta(&policy->selector.daddr, |
| 873 | &n->addr, |
| 874 | n->prefixlen, |
| 875 | family) == 0; |
| 876 | if (matches_s && matches_d) |
| 877 | continue; |
| 878 | |
| 879 | WARN_ON_ONCE(!matches_s && !matches_d); |
| 880 | if (matches_s) |
| 881 | matched_s++; |
| 882 | if (matches_d) |
| 883 | matched_d++; |
| 884 | WARN_ON_ONCE(matched_s && matched_d); |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 888 | static void xfrm_policy_inexact_node_reinsert(struct net *net, |
| 889 | struct xfrm_pol_inexact_node *n, |
| 890 | struct rb_root *new, |
| 891 | u16 family) |
| 892 | { |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 893 | struct xfrm_pol_inexact_node *node; |
Florian Westphal | 12750ab | 2019-01-04 14:17:05 +0100 | [diff] [blame] | 894 | struct rb_node **p, *parent; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 895 | |
| 896 | /* we should not have another subtree here */ |
| 897 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root)); |
Florian Westphal | 12750ab | 2019-01-04 14:17:05 +0100 | [diff] [blame] | 898 | restart: |
| 899 | parent = NULL; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 900 | p = &new->rb_node; |
| 901 | while (*p) { |
| 902 | u8 prefixlen; |
| 903 | int delta; |
| 904 | |
| 905 | parent = *p; |
| 906 | node = rb_entry(*p, struct xfrm_pol_inexact_node, node); |
| 907 | |
| 908 | prefixlen = min(node->prefixlen, n->prefixlen); |
| 909 | |
| 910 | delta = xfrm_policy_addr_delta(&n->addr, &node->addr, |
| 911 | prefixlen, family); |
| 912 | if (delta < 0) { |
| 913 | p = &parent->rb_left; |
| 914 | } else if (delta > 0) { |
| 915 | p = &parent->rb_right; |
| 916 | } else { |
| 917 | struct xfrm_policy *tmp; |
| 918 | |
Florian Westphal | 12750ab | 2019-01-04 14:17:05 +0100 | [diff] [blame] | 919 | hlist_for_each_entry(tmp, &n->hhead, bydst) { |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 920 | tmp->bydst_reinsert = true; |
Florian Westphal | 12750ab | 2019-01-04 14:17:05 +0100 | [diff] [blame] | 921 | hlist_del_rcu(&tmp->bydst); |
| 922 | } |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 923 | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 924 | xfrm_policy_inexact_list_reinsert(net, node, family); |
| 925 | |
| 926 | if (node->prefixlen == n->prefixlen) { |
| 927 | kfree_rcu(n, rcu); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | rb_erase(*p, new); |
| 932 | kfree_rcu(n, rcu); |
| 933 | n = node; |
| 934 | n->prefixlen = prefixlen; |
Florian Westphal | 12750ab | 2019-01-04 14:17:05 +0100 | [diff] [blame] | 935 | goto restart; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
| 939 | rb_link_node_rcu(&n->node, parent, p); |
| 940 | rb_insert_color(&n->node, new); |
| 941 | } |
| 942 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 943 | /* merge nodes v and n */ |
| 944 | static void xfrm_policy_inexact_node_merge(struct net *net, |
| 945 | struct xfrm_pol_inexact_node *v, |
| 946 | struct xfrm_pol_inexact_node *n, |
| 947 | u16 family) |
| 948 | { |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 949 | struct xfrm_pol_inexact_node *node; |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 950 | struct xfrm_policy *tmp; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 951 | struct rb_node *rnode; |
| 952 | |
| 953 | /* To-be-merged node v has a subtree. |
| 954 | * |
| 955 | * Dismantle it and insert its nodes to n->root. |
| 956 | */ |
| 957 | while ((rnode = rb_first(&v->root)) != NULL) { |
| 958 | node = rb_entry(rnode, struct xfrm_pol_inexact_node, node); |
| 959 | rb_erase(&node->node, &v->root); |
| 960 | xfrm_policy_inexact_node_reinsert(net, node, &n->root, |
| 961 | family); |
| 962 | } |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 963 | |
Florian Westphal | 1d38900 | 2019-01-04 14:17:03 +0100 | [diff] [blame] | 964 | hlist_for_each_entry(tmp, &v->hhead, bydst) { |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 965 | tmp->bydst_reinsert = true; |
Florian Westphal | 1d38900 | 2019-01-04 14:17:03 +0100 | [diff] [blame] | 966 | hlist_del_rcu(&tmp->bydst); |
| 967 | } |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 968 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 969 | xfrm_policy_inexact_list_reinsert(net, n, family); |
| 970 | } |
| 971 | |
| 972 | static struct xfrm_pol_inexact_node * |
| 973 | xfrm_policy_inexact_insert_node(struct net *net, |
| 974 | struct rb_root *root, |
| 975 | xfrm_address_t *addr, |
| 976 | u16 family, u8 prefixlen, u8 dir) |
| 977 | { |
| 978 | struct xfrm_pol_inexact_node *cached = NULL; |
| 979 | struct rb_node **p, *parent = NULL; |
| 980 | struct xfrm_pol_inexact_node *node; |
| 981 | |
| 982 | p = &root->rb_node; |
| 983 | while (*p) { |
| 984 | int delta; |
| 985 | |
| 986 | parent = *p; |
| 987 | node = rb_entry(*p, struct xfrm_pol_inexact_node, node); |
| 988 | |
| 989 | delta = xfrm_policy_addr_delta(addr, &node->addr, |
| 990 | node->prefixlen, |
| 991 | family); |
| 992 | if (delta == 0 && prefixlen >= node->prefixlen) { |
| 993 | WARN_ON_ONCE(cached); /* ipsec policies got lost */ |
| 994 | return node; |
| 995 | } |
| 996 | |
| 997 | if (delta < 0) |
| 998 | p = &parent->rb_left; |
| 999 | else |
| 1000 | p = &parent->rb_right; |
| 1001 | |
| 1002 | if (prefixlen < node->prefixlen) { |
| 1003 | delta = xfrm_policy_addr_delta(addr, &node->addr, |
| 1004 | prefixlen, |
| 1005 | family); |
| 1006 | if (delta) |
| 1007 | continue; |
| 1008 | |
| 1009 | /* This node is a subnet of the new prefix. It needs |
| 1010 | * to be removed and re-inserted with the smaller |
| 1011 | * prefix and all nodes that are now also covered |
| 1012 | * by the reduced prefixlen. |
| 1013 | */ |
| 1014 | rb_erase(&node->node, root); |
| 1015 | |
| 1016 | if (!cached) { |
| 1017 | xfrm_pol_inexact_node_init(node, addr, |
| 1018 | prefixlen); |
| 1019 | cached = node; |
| 1020 | } else { |
| 1021 | /* This node also falls within the new |
| 1022 | * prefixlen. Merge the to-be-reinserted |
| 1023 | * node and this one. |
| 1024 | */ |
| 1025 | xfrm_policy_inexact_node_merge(net, node, |
| 1026 | cached, family); |
| 1027 | kfree_rcu(node, rcu); |
| 1028 | } |
| 1029 | |
| 1030 | /* restart */ |
| 1031 | p = &root->rb_node; |
| 1032 | parent = NULL; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | node = cached; |
| 1037 | if (!node) { |
| 1038 | node = xfrm_pol_inexact_node_alloc(addr, prefixlen); |
| 1039 | if (!node) |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | |
| 1043 | rb_link_node_rcu(&node->node, parent, p); |
| 1044 | rb_insert_color(&node->node, root); |
| 1045 | |
| 1046 | return node; |
| 1047 | } |
| 1048 | |
| 1049 | static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm) |
| 1050 | { |
| 1051 | struct xfrm_pol_inexact_node *node; |
| 1052 | struct rb_node *rn = rb_first(r); |
| 1053 | |
| 1054 | while (rn) { |
| 1055 | node = rb_entry(rn, struct xfrm_pol_inexact_node, node); |
| 1056 | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 1057 | xfrm_policy_inexact_gc_tree(&node->root, rm); |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1058 | rn = rb_next(rn); |
| 1059 | |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 1060 | if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) { |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1061 | WARN_ON_ONCE(rm); |
| 1062 | continue; |
| 1063 | } |
| 1064 | |
| 1065 | rb_erase(&node->node, r); |
| 1066 | kfree_rcu(node, rcu); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1070 | static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit) |
| 1071 | { |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1072 | write_seqcount_begin(&b->count); |
| 1073 | xfrm_policy_inexact_gc_tree(&b->root_d, net_exit); |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 1074 | xfrm_policy_inexact_gc_tree(&b->root_s, net_exit); |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1075 | write_seqcount_end(&b->count); |
| 1076 | |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 1077 | if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) || |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1078 | !hlist_empty(&b->hhead)) { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1079 | WARN_ON_ONCE(net_exit); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1080 | return; |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1081 | } |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1082 | |
| 1083 | if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head, |
| 1084 | xfrm_pol_inexact_params) == 0) { |
| 1085 | list_del(&b->inexact_bins); |
| 1086 | kfree_rcu(b, rcu); |
| 1087 | } |
| 1088 | } |
| 1089 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1090 | static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b) |
| 1091 | { |
| 1092 | struct net *net = read_pnet(&b->k.net); |
| 1093 | |
| 1094 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
| 1095 | __xfrm_policy_inexact_prune_bin(b, false); |
| 1096 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1097 | } |
| 1098 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1099 | static void __xfrm_policy_inexact_flush(struct net *net) |
| 1100 | { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1101 | struct xfrm_pol_inexact_bin *bin, *t; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1102 | |
| 1103 | lockdep_assert_held(&net->xfrm.xfrm_policy_lock); |
| 1104 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1105 | list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins) |
| 1106 | __xfrm_policy_inexact_prune_bin(bin, false); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1109 | static struct hlist_head * |
| 1110 | xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin, |
| 1111 | struct xfrm_policy *policy, u8 dir) |
| 1112 | { |
| 1113 | struct xfrm_pol_inexact_node *n; |
| 1114 | struct net *net; |
| 1115 | |
| 1116 | net = xp_net(policy); |
| 1117 | lockdep_assert_held(&net->xfrm.xfrm_policy_lock); |
| 1118 | |
| 1119 | if (xfrm_policy_inexact_insert_use_any_list(policy)) |
| 1120 | return &bin->hhead; |
| 1121 | |
| 1122 | if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr, |
| 1123 | policy->family, |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 1124 | policy->selector.prefixlen_d)) { |
| 1125 | write_seqcount_begin(&bin->count); |
| 1126 | n = xfrm_policy_inexact_insert_node(net, |
| 1127 | &bin->root_s, |
| 1128 | &policy->selector.saddr, |
| 1129 | policy->family, |
| 1130 | policy->selector.prefixlen_s, |
| 1131 | dir); |
| 1132 | write_seqcount_end(&bin->count); |
| 1133 | if (!n) |
| 1134 | return NULL; |
| 1135 | |
| 1136 | return &n->hhead; |
| 1137 | } |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1138 | |
| 1139 | /* daddr is fixed */ |
| 1140 | write_seqcount_begin(&bin->count); |
| 1141 | n = xfrm_policy_inexact_insert_node(net, |
| 1142 | &bin->root_d, |
| 1143 | &policy->selector.daddr, |
| 1144 | policy->family, |
| 1145 | policy->selector.prefixlen_d, dir); |
| 1146 | write_seqcount_end(&bin->count); |
| 1147 | if (!n) |
| 1148 | return NULL; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* saddr is wildcard */ |
| 1151 | if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr, |
| 1152 | policy->family, |
| 1153 | policy->selector.prefixlen_s)) |
| 1154 | return &n->hhead; |
| 1155 | |
| 1156 | write_seqcount_begin(&bin->count); |
| 1157 | n = xfrm_policy_inexact_insert_node(net, |
| 1158 | &n->root, |
| 1159 | &policy->selector.saddr, |
| 1160 | policy->family, |
| 1161 | policy->selector.prefixlen_s, dir); |
| 1162 | write_seqcount_end(&bin->count); |
| 1163 | if (!n) |
| 1164 | return NULL; |
| 1165 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1166 | return &n->hhead; |
| 1167 | } |
| 1168 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1169 | static struct xfrm_policy * |
| 1170 | xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl) |
| 1171 | { |
| 1172 | struct xfrm_pol_inexact_bin *bin; |
| 1173 | struct xfrm_policy *delpol; |
| 1174 | struct hlist_head *chain; |
| 1175 | struct net *net; |
| 1176 | |
| 1177 | bin = xfrm_policy_inexact_alloc_bin(policy, dir); |
| 1178 | if (!bin) |
| 1179 | return ERR_PTR(-ENOMEM); |
| 1180 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1181 | net = xp_net(policy); |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1182 | lockdep_assert_held(&net->xfrm.xfrm_policy_lock); |
| 1183 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1184 | chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir); |
| 1185 | if (!chain) { |
| 1186 | __xfrm_policy_inexact_prune_bin(bin, false); |
| 1187 | return ERR_PTR(-ENOMEM); |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1190 | delpol = xfrm_policy_insert_list(chain, policy, excl); |
| 1191 | if (delpol && excl) { |
| 1192 | __xfrm_policy_inexact_prune_bin(bin, false); |
| 1193 | return ERR_PTR(-EEXIST); |
| 1194 | } |
| 1195 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1196 | chain = &net->xfrm.policy_inexact[dir]; |
| 1197 | xfrm_policy_insert_inexact_list(chain, policy); |
| 1198 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1199 | if (delpol) |
| 1200 | __xfrm_policy_inexact_prune_bin(bin, false); |
| 1201 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1202 | return delpol; |
| 1203 | } |
| 1204 | |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1205 | static void xfrm_hash_rebuild(struct work_struct *work) |
| 1206 | { |
| 1207 | struct net *net = container_of(work, struct net, |
| 1208 | xfrm.policy_hthresh.work); |
| 1209 | unsigned int hmask; |
| 1210 | struct xfrm_policy *pol; |
| 1211 | struct xfrm_policy *policy; |
| 1212 | struct hlist_head *chain; |
| 1213 | struct hlist_head *odst; |
| 1214 | struct hlist_node *newpos; |
| 1215 | int i; |
| 1216 | int dir; |
| 1217 | unsigned seq; |
| 1218 | u8 lbits4, rbits4, lbits6, rbits6; |
| 1219 | |
| 1220 | mutex_lock(&hash_resize_mutex); |
| 1221 | |
| 1222 | /* read selector prefixlen thresholds */ |
| 1223 | do { |
| 1224 | seq = read_seqbegin(&net->xfrm.policy_hthresh.lock); |
| 1225 | |
| 1226 | lbits4 = net->xfrm.policy_hthresh.lbits4; |
| 1227 | rbits4 = net->xfrm.policy_hthresh.rbits4; |
| 1228 | lbits6 = net->xfrm.policy_hthresh.lbits6; |
| 1229 | rbits6 = net->xfrm.policy_hthresh.rbits6; |
| 1230 | } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq)); |
| 1231 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1232 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Florian Westphal | 7a474c3 | 2019-01-04 14:17:01 +0100 | [diff] [blame] | 1233 | write_seqcount_begin(&xfrm_policy_hash_generation); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1234 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1235 | /* make sure that we can insert the indirect policies again before |
| 1236 | * we start with destructive action. |
| 1237 | */ |
| 1238 | list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1239 | struct xfrm_pol_inexact_bin *bin; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1240 | u8 dbits, sbits; |
| 1241 | |
| 1242 | dir = xfrm_policy_id2dir(policy->index); |
| 1243 | if (policy->walk.dead || dir >= XFRM_POLICY_MAX) |
| 1244 | continue; |
| 1245 | |
| 1246 | if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) { |
| 1247 | if (policy->family == AF_INET) { |
| 1248 | dbits = rbits4; |
| 1249 | sbits = lbits4; |
| 1250 | } else { |
| 1251 | dbits = rbits6; |
| 1252 | sbits = lbits6; |
| 1253 | } |
| 1254 | } else { |
| 1255 | if (policy->family == AF_INET) { |
| 1256 | dbits = lbits4; |
| 1257 | sbits = rbits4; |
| 1258 | } else { |
| 1259 | dbits = lbits6; |
| 1260 | sbits = rbits6; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | if (policy->selector.prefixlen_d < dbits || |
| 1265 | policy->selector.prefixlen_s < sbits) |
| 1266 | continue; |
| 1267 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1268 | bin = xfrm_policy_inexact_alloc_bin(policy, dir); |
| 1269 | if (!bin) |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1270 | goto out_unlock; |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1271 | |
| 1272 | if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir)) |
| 1273 | goto out_unlock; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1276 | /* reset the bydst and inexact table in all directions */ |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 1277 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Florian Westphal | 1548bc4 | 2019-01-04 14:17:02 +0100 | [diff] [blame] | 1278 | struct hlist_node *n; |
| 1279 | |
| 1280 | hlist_for_each_entry_safe(policy, n, |
| 1281 | &net->xfrm.policy_inexact[dir], |
| 1282 | bydst_inexact_list) |
| 1283 | hlist_del_init(&policy->bydst_inexact_list); |
| 1284 | |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1285 | hmask = net->xfrm.policy_bydst[dir].hmask; |
| 1286 | odst = net->xfrm.policy_bydst[dir].table; |
| 1287 | for (i = hmask; i >= 0; i--) |
| 1288 | INIT_HLIST_HEAD(odst + i); |
| 1289 | if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) { |
| 1290 | /* dir out => dst = remote, src = local */ |
| 1291 | net->xfrm.policy_bydst[dir].dbits4 = rbits4; |
| 1292 | net->xfrm.policy_bydst[dir].sbits4 = lbits4; |
| 1293 | net->xfrm.policy_bydst[dir].dbits6 = rbits6; |
| 1294 | net->xfrm.policy_bydst[dir].sbits6 = lbits6; |
| 1295 | } else { |
| 1296 | /* dir in/fwd => dst = local, src = remote */ |
| 1297 | net->xfrm.policy_bydst[dir].dbits4 = lbits4; |
| 1298 | net->xfrm.policy_bydst[dir].sbits4 = rbits4; |
| 1299 | net->xfrm.policy_bydst[dir].dbits6 = lbits6; |
| 1300 | net->xfrm.policy_bydst[dir].sbits6 = rbits6; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | /* re-insert all policies by order of creation */ |
| 1305 | list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) { |
Florian Westphal | 88584c3 | 2018-11-27 13:28:54 +0100 | [diff] [blame] | 1306 | if (policy->walk.dead) |
| 1307 | continue; |
| 1308 | dir = xfrm_policy_id2dir(policy->index); |
| 1309 | if (dir >= XFRM_POLICY_MAX) { |
Tobias Brunner | 6916fb3 | 2016-07-29 09:57:32 +0200 | [diff] [blame] | 1310 | /* skip socket policies */ |
| 1311 | continue; |
| 1312 | } |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1313 | newpos = NULL; |
| 1314 | chain = policy_hash_bysel(net, &policy->selector, |
Florian Westphal | 88584c3 | 2018-11-27 13:28:54 +0100 | [diff] [blame] | 1315 | policy->family, dir); |
Florian Westphal | 1548bc4 | 2019-01-04 14:17:02 +0100 | [diff] [blame] | 1316 | |
| 1317 | hlist_del_rcu(&policy->bydst); |
| 1318 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1319 | if (!chain) { |
| 1320 | void *p = xfrm_policy_inexact_insert(policy, dir, 0); |
| 1321 | |
| 1322 | WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p)); |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1326 | hlist_for_each_entry(pol, chain, bydst) { |
| 1327 | if (policy->priority >= pol->priority) |
| 1328 | newpos = &pol->bydst; |
| 1329 | else |
| 1330 | break; |
| 1331 | } |
| 1332 | if (newpos) |
Florian Westphal | 9dffff2 | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 1333 | hlist_add_behind_rcu(&policy->bydst, newpos); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1334 | else |
Florian Westphal | 9dffff2 | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 1335 | hlist_add_head_rcu(&policy->bydst, chain); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1338 | out_unlock: |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1339 | __xfrm_policy_inexact_flush(net); |
Florian Westphal | 7a474c3 | 2019-01-04 14:17:01 +0100 | [diff] [blame] | 1340 | write_seqcount_end(&xfrm_policy_hash_generation); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1341 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 1342 | |
| 1343 | mutex_unlock(&hash_resize_mutex); |
| 1344 | } |
| 1345 | |
| 1346 | void xfrm_policy_hash_rebuild(struct net *net) |
| 1347 | { |
| 1348 | schedule_work(&net->xfrm.policy_hthresh.work); |
| 1349 | } |
| 1350 | EXPORT_SYMBOL(xfrm_policy_hash_rebuild); |
| 1351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | /* Generate new index... KAME seems to generate them ordered by cost |
| 1353 | * of an absolute inpredictability of ordering of rules. This will not pass. */ |
Fan Du | e682adf0 | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 1354 | static u32 xfrm_gen_index(struct net *net, int dir, u32 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | static u32 idx_generator; |
| 1357 | |
| 1358 | for (;;) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1359 | struct hlist_head *list; |
| 1360 | struct xfrm_policy *p; |
| 1361 | u32 idx; |
| 1362 | int found; |
| 1363 | |
Fan Du | e682adf0 | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 1364 | if (!index) { |
| 1365 | idx = (idx_generator | dir); |
| 1366 | idx_generator += 8; |
| 1367 | } else { |
| 1368 | idx = index; |
| 1369 | index = 0; |
| 1370 | } |
| 1371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | if (idx == 0) |
| 1373 | idx = 8; |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1374 | list = net->xfrm.policy_byidx + idx_hash(net, idx); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1375 | found = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1376 | hlist_for_each_entry(p, list, byidx) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1377 | if (p->index == idx) { |
| 1378 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | break; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1382 | if (!found) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | return idx; |
| 1384 | } |
| 1385 | } |
| 1386 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1387 | static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2) |
| 1388 | { |
| 1389 | u32 *p1 = (u32 *) s1; |
| 1390 | u32 *p2 = (u32 *) s2; |
| 1391 | int len = sizeof(struct xfrm_selector) / sizeof(u32); |
| 1392 | int i; |
| 1393 | |
| 1394 | for (i = 0; i < len; i++) { |
| 1395 | if (p1[i] != p2[i]) |
| 1396 | return 1; |
| 1397 | } |
| 1398 | |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1402 | static void xfrm_policy_requeue(struct xfrm_policy *old, |
| 1403 | struct xfrm_policy *new) |
| 1404 | { |
| 1405 | struct xfrm_policy_queue *pq = &old->polq; |
| 1406 | struct sk_buff_head list; |
| 1407 | |
Li RongQing | de2ad48 | 2015-04-30 17:25:19 +0800 | [diff] [blame] | 1408 | if (skb_queue_empty(&pq->hold_queue)) |
| 1409 | return; |
| 1410 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1411 | __skb_queue_head_init(&list); |
| 1412 | |
| 1413 | spin_lock_bh(&pq->hold_queue.lock); |
| 1414 | skb_queue_splice_init(&pq->hold_queue, &list); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 1415 | if (del_timer(&pq->hold_timer)) |
| 1416 | xfrm_pol_put(old); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1417 | spin_unlock_bh(&pq->hold_queue.lock); |
| 1418 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1419 | pq = &new->polq; |
| 1420 | |
| 1421 | spin_lock_bh(&pq->hold_queue.lock); |
| 1422 | skb_queue_splice(&list, &pq->hold_queue); |
| 1423 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 1424 | if (!mod_timer(&pq->hold_timer, jiffies)) |
| 1425 | xfrm_pol_hold(new); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1426 | spin_unlock_bh(&pq->hold_queue.lock); |
| 1427 | } |
| 1428 | |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 1429 | static bool xfrm_policy_mark_match(struct xfrm_policy *policy, |
| 1430 | struct xfrm_policy *pol) |
| 1431 | { |
| 1432 | u32 mark = policy->mark.v & policy->mark.m; |
| 1433 | |
| 1434 | if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m) |
| 1435 | return true; |
| 1436 | |
| 1437 | if ((mark & pol->mark.m) == pol->mark.v && |
| 1438 | policy->priority == pol->priority) |
| 1439 | return true; |
| 1440 | |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1444 | static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed) |
| 1445 | { |
| 1446 | const struct xfrm_pol_inexact_key *k = data; |
| 1447 | u32 a = k->type << 24 | k->dir << 16 | k->family; |
| 1448 | |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1449 | return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)), |
| 1450 | seed); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed) |
| 1454 | { |
| 1455 | const struct xfrm_pol_inexact_bin *b = data; |
| 1456 | |
| 1457 | return xfrm_pol_bin_key(&b->k, 0, seed); |
| 1458 | } |
| 1459 | |
| 1460 | static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg, |
| 1461 | const void *ptr) |
| 1462 | { |
| 1463 | const struct xfrm_pol_inexact_key *key = arg->key; |
| 1464 | const struct xfrm_pol_inexact_bin *b = ptr; |
| 1465 | int ret; |
| 1466 | |
| 1467 | if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net))) |
| 1468 | return -1; |
| 1469 | |
| 1470 | ret = b->k.dir ^ key->dir; |
| 1471 | if (ret) |
| 1472 | return ret; |
| 1473 | |
| 1474 | ret = b->k.type ^ key->type; |
| 1475 | if (ret) |
| 1476 | return ret; |
| 1477 | |
| 1478 | ret = b->k.family ^ key->family; |
| 1479 | if (ret) |
| 1480 | return ret; |
| 1481 | |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1482 | return b->k.if_id ^ key->if_id; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | static const struct rhashtable_params xfrm_pol_inexact_params = { |
| 1486 | .head_offset = offsetof(struct xfrm_pol_inexact_bin, head), |
| 1487 | .hashfn = xfrm_pol_bin_key, |
| 1488 | .obj_hashfn = xfrm_pol_bin_obj, |
| 1489 | .obj_cmpfn = xfrm_pol_bin_cmp, |
| 1490 | .automatic_shrinking = true, |
| 1491 | }; |
| 1492 | |
| 1493 | static void xfrm_policy_insert_inexact_list(struct hlist_head *chain, |
| 1494 | struct xfrm_policy *policy) |
| 1495 | { |
| 1496 | struct xfrm_policy *pol, *delpol = NULL; |
| 1497 | struct hlist_node *newpos = NULL; |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1498 | int i = 0; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1499 | |
| 1500 | hlist_for_each_entry(pol, chain, bydst_inexact_list) { |
| 1501 | if (pol->type == policy->type && |
| 1502 | pol->if_id == policy->if_id && |
| 1503 | !selector_cmp(&pol->selector, &policy->selector) && |
| 1504 | xfrm_policy_mark_match(policy, pol) && |
| 1505 | xfrm_sec_ctx_match(pol->security, policy->security) && |
| 1506 | !WARN_ON(delpol)) { |
| 1507 | delpol = pol; |
| 1508 | if (policy->priority > pol->priority) |
| 1509 | continue; |
| 1510 | } else if (policy->priority >= pol->priority) { |
| 1511 | newpos = &pol->bydst_inexact_list; |
| 1512 | continue; |
| 1513 | } |
| 1514 | if (delpol) |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | if (newpos) |
| 1519 | hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos); |
| 1520 | else |
| 1521 | hlist_add_head_rcu(&policy->bydst_inexact_list, chain); |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1522 | |
| 1523 | hlist_for_each_entry(pol, chain, bydst_inexact_list) { |
| 1524 | pol->pos = i; |
| 1525 | i++; |
| 1526 | } |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1529 | static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain, |
| 1530 | struct xfrm_policy *policy, |
| 1531 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1533 | struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1535 | hlist_for_each_entry(pol, chain, bydst) { |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 1536 | if (pol->type == policy->type && |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1537 | pol->if_id == policy->if_id && |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1538 | !selector_cmp(&pol->selector, &policy->selector) && |
Steffen Klassert | 7cb8a93 | 2013-02-11 07:02:36 +0100 | [diff] [blame] | 1539 | xfrm_policy_mark_match(policy, pol) && |
Herbert Xu | a6c7ab5 | 2007-01-16 16:52:02 -0800 | [diff] [blame] | 1540 | xfrm_sec_ctx_match(pol->security, policy->security) && |
| 1541 | !WARN_ON(delpol)) { |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1542 | if (excl) |
| 1543 | return ERR_PTR(-EEXIST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | delpol = pol; |
| 1545 | if (policy->priority > pol->priority) |
| 1546 | continue; |
| 1547 | } else if (policy->priority >= pol->priority) { |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1548 | newpos = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | continue; |
| 1550 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | if (delpol) |
| 1552 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | } |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | if (newpos) |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1556 | hlist_add_behind_rcu(&policy->bydst, &newpos->bydst); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1557 | else |
Florian Westphal | 9dffff2 | 2018-10-10 18:02:21 +0200 | [diff] [blame] | 1558 | hlist_add_head_rcu(&policy->bydst, chain); |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1559 | |
| 1560 | return delpol; |
| 1561 | } |
| 1562 | |
| 1563 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) |
| 1564 | { |
| 1565 | struct net *net = xp_net(policy); |
| 1566 | struct xfrm_policy *delpol; |
| 1567 | struct hlist_head *chain; |
| 1568 | |
| 1569 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
| 1570 | chain = policy_hash_bysel(net, &policy->selector, policy->family, dir); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1571 | if (chain) |
Florian Westphal | cc1bb84 | 2018-11-07 23:00:34 +0100 | [diff] [blame] | 1572 | delpol = xfrm_policy_insert_list(chain, policy, excl); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1573 | else |
| 1574 | delpol = xfrm_policy_inexact_insert(policy, dir, excl); |
Florian Westphal | a927d6af | 2018-11-07 23:00:33 +0100 | [diff] [blame] | 1575 | |
| 1576 | if (IS_ERR(delpol)) { |
| 1577 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1578 | return PTR_ERR(delpol); |
| 1579 | } |
| 1580 | |
Herbert Xu | 12bfa8b | 2014-11-13 17:09:50 +0800 | [diff] [blame] | 1581 | __xfrm_policy_link(policy, dir); |
fan.du | ca4c3fc | 2013-07-30 08:33:53 +0800 | [diff] [blame] | 1582 | |
| 1583 | /* After previous checking, family can either be AF_INET or AF_INET6 */ |
| 1584 | if (policy->family == AF_INET) |
| 1585 | rt_genid_bump_ipv4(net); |
| 1586 | else |
| 1587 | rt_genid_bump_ipv6(net); |
| 1588 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1589 | if (delpol) { |
| 1590 | xfrm_policy_requeue(delpol, policy); |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 1591 | __xfrm_policy_unlink(delpol, dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 1592 | } |
Fan Du | e682adf0 | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 1593 | policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1594 | hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index)); |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 1595 | policy->curlft.add_time = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | policy->curlft.use_time = 0; |
| 1597 | if (!mod_timer(&policy->timer, jiffies + HZ)) |
| 1598 | xfrm_pol_hold(policy); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1599 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 1601 | if (delpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | xfrm_policy_kill(delpol); |
Alexey Dobriyan | 1121994 | 2008-11-25 17:33:06 -0800 | [diff] [blame] | 1603 | else if (xfrm_bydst_should_resize(net, dir, NULL)) |
| 1604 | schedule_work(&net->xfrm.policy_hash_work); |
David S. Miller | 9b78a82 | 2005-12-22 07:39:48 -0800 | [diff] [blame] | 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | return 0; |
| 1607 | } |
| 1608 | EXPORT_SYMBOL(xfrm_policy_insert); |
| 1609 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1610 | static struct xfrm_policy * |
| 1611 | __xfrm_policy_bysel_ctx(struct hlist_head *chain, u32 mark, u32 if_id, |
| 1612 | u8 type, int dir, |
| 1613 | struct xfrm_selector *sel, |
| 1614 | struct xfrm_sec_ctx *ctx) |
| 1615 | { |
| 1616 | struct xfrm_policy *pol; |
| 1617 | |
| 1618 | if (!chain) |
| 1619 | return NULL; |
| 1620 | |
| 1621 | hlist_for_each_entry(pol, chain, bydst) { |
| 1622 | if (pol->type == type && |
| 1623 | pol->if_id == if_id && |
| 1624 | (mark & pol->mark.m) == pol->mark.v && |
| 1625 | !selector_cmp(sel, &pol->selector) && |
| 1626 | xfrm_sec_ctx_match(ctx, pol->security)) |
| 1627 | return pol; |
| 1628 | } |
| 1629 | |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1633 | struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id, |
| 1634 | u8 type, int dir, |
| 1635 | struct xfrm_selector *sel, |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 1636 | struct xfrm_sec_ctx *ctx, int delete, |
| 1637 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | { |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1639 | struct xfrm_pol_inexact_bin *bin = NULL; |
| 1640 | struct xfrm_policy *pol, *ret = NULL; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1641 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 1643 | *err = 0; |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1644 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 1645 | chain = policy_hash_bysel(net, sel, sel->family, dir); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1646 | if (!chain) { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1647 | struct xfrm_pol_inexact_candidates cand; |
| 1648 | int i; |
| 1649 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1650 | bin = xfrm_policy_inexact_lookup(net, type, |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1651 | sel->family, dir, if_id); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1652 | if (!bin) { |
| 1653 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1657 | if (!xfrm_policy_find_inexact_candidates(&cand, bin, |
| 1658 | &sel->saddr, |
| 1659 | &sel->daddr)) { |
| 1660 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | pol = NULL; |
| 1665 | for (i = 0; i < ARRAY_SIZE(cand.res); i++) { |
| 1666 | struct xfrm_policy *tmp; |
| 1667 | |
| 1668 | tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark, |
| 1669 | if_id, type, dir, |
| 1670 | sel, ctx); |
Florian Westphal | 39aa692 | 2018-11-15 02:51:57 +0100 | [diff] [blame] | 1671 | if (!tmp) |
| 1672 | continue; |
| 1673 | |
| 1674 | if (!pol || tmp->pos < pol->pos) |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1675 | pol = tmp; |
| 1676 | } |
| 1677 | } else { |
| 1678 | pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir, |
| 1679 | sel, ctx); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1682 | if (pol) { |
| 1683 | xfrm_pol_hold(pol); |
| 1684 | if (delete) { |
| 1685 | *err = security_xfrm_policy_delete(pol->security); |
| 1686 | if (*err) { |
| 1687 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1688 | return pol; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1689 | } |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1690 | __xfrm_policy_unlink(pol, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1692 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1694 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1696 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1697 | xfrm_policy_kill(ret); |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1698 | if (bin && delete) |
| 1699 | xfrm_policy_inexact_prune_bin(bin); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1700 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 1702 | EXPORT_SYMBOL(xfrm_policy_bysel_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1704 | struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, |
| 1705 | u8 type, int dir, u32 id, int delete, |
| 1706 | int *err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1708 | struct xfrm_policy *pol, *ret; |
| 1709 | struct hlist_head *chain; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
Herbert Xu | b5505c6 | 2007-05-14 02:15:47 -0700 | [diff] [blame] | 1711 | *err = -ENOENT; |
| 1712 | if (xfrm_policy_id2dir(id) != dir) |
| 1713 | return NULL; |
| 1714 | |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 1715 | *err = 0; |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1716 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | 8d1211a | 2008-11-25 17:34:20 -0800 | [diff] [blame] | 1717 | chain = net->xfrm.policy_byidx + idx_hash(net, id); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1718 | ret = NULL; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1719 | hlist_for_each_entry(pol, chain, byidx) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1720 | if (pol->type == type && pol->index == id && |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 1721 | pol->if_id == if_id && |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 1722 | (mark & pol->mark.m) == pol->mark.v) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | xfrm_pol_hold(pol); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1724 | if (delete) { |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1725 | *err = security_xfrm_policy_delete( |
| 1726 | pol->security); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 1727 | if (*err) { |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1728 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Eric Paris | ef41aaa | 2007-03-07 15:37:58 -0800 | [diff] [blame] | 1729 | return pol; |
| 1730 | } |
Wei Yongjun | 29fa0b30 | 2008-12-03 00:33:09 -0800 | [diff] [blame] | 1731 | __xfrm_policy_unlink(pol, dir); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1732 | } |
| 1733 | ret = pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | break; |
| 1735 | } |
| 1736 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1737 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | |
Timo Teräs | fe1a5f0 | 2010-04-07 00:30:04 +0000 | [diff] [blame] | 1739 | if (ret && delete) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1740 | xfrm_policy_kill(ret); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1741 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | } |
| 1743 | EXPORT_SYMBOL(xfrm_policy_byid); |
| 1744 | |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1745 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
| 1746 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 1747 | 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] | 1748 | { |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1749 | struct xfrm_policy *pol; |
| 1750 | int err = 0; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1751 | |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1752 | list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) { |
| 1753 | if (pol->walk.dead || |
| 1754 | xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX || |
| 1755 | pol->type != type) |
| 1756 | continue; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1757 | |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1758 | err = security_xfrm_policy_delete(pol->security); |
| 1759 | if (err) { |
| 1760 | xfrm_audit_policy_delete(pol, 0, task_valid); |
| 1761 | return err; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | return err; |
| 1765 | } |
| 1766 | #else |
| 1767 | static inline int |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 1768 | 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] | 1769 | { |
| 1770 | return 0; |
| 1771 | } |
| 1772 | #endif |
| 1773 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 1774 | int xfrm_policy_flush(struct net *net, u8 type, bool task_valid) |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1775 | { |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 1776 | int dir, err = 0, cnt = 0; |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1777 | struct xfrm_policy *pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1779 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1780 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 1781 | err = xfrm_policy_flush_secctx_check(net, type, task_valid); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1782 | if (err) |
| 1783 | goto out; |
| 1784 | |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1785 | again: |
| 1786 | list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) { |
| 1787 | dir = xfrm_policy_id2dir(pol->index); |
| 1788 | if (pol->walk.dead || |
| 1789 | dir >= XFRM_POLICY_MAX || |
| 1790 | pol->type != type) |
| 1791 | continue; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1792 | |
Florian Westphal | ceb159e | 2018-11-07 23:00:32 +0100 | [diff] [blame] | 1793 | __xfrm_policy_unlink(pol, dir); |
| 1794 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
| 1795 | cnt++; |
| 1796 | xfrm_audit_policy_delete(pol, 1, task_valid); |
| 1797 | xfrm_policy_kill(pol); |
| 1798 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
| 1799 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1801 | if (cnt) |
| 1802 | __xfrm_policy_inexact_flush(net); |
| 1803 | else |
Jamal Hadi Salim | 2f1eb65 | 2010-02-19 02:00:42 +0000 | [diff] [blame] | 1804 | err = -ESRCH; |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1805 | out: |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1806 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Joy Latten | 4aa2e62 | 2007-06-04 19:05:57 -0400 | [diff] [blame] | 1807 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
| 1809 | EXPORT_SYMBOL(xfrm_policy_flush); |
| 1810 | |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1811 | int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk, |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1812 | int (*func)(struct xfrm_policy *, int, int, void*), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | void *data) |
| 1814 | { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1815 | struct xfrm_policy *pol; |
| 1816 | struct xfrm_policy_walk_entry *x; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1817 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1819 | if (walk->type >= XFRM_POLICY_TYPE_MAX && |
| 1820 | walk->type != XFRM_POLICY_TYPE_ANY) |
| 1821 | return -EINVAL; |
| 1822 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1823 | if (list_empty(&walk->walk.all) && walk->seq != 0) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1824 | return 0; |
| 1825 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1826 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1827 | if (list_empty(&walk->walk.all)) |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1828 | 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] | 1829 | else |
Li RongQing | 8007770 | 2015-04-22 17:09:54 +0800 | [diff] [blame] | 1830 | x = list_first_entry(&walk->walk.all, |
| 1831 | struct xfrm_policy_walk_entry, all); |
| 1832 | |
Alexey Dobriyan | cdcbca7 | 2008-11-25 17:34:49 -0800 | [diff] [blame] | 1833 | list_for_each_entry_from(x, &net->xfrm.policy_all, all) { |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1834 | if (x->dead) |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1835 | continue; |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1836 | pol = container_of(x, struct xfrm_policy, walk); |
| 1837 | if (walk->type != XFRM_POLICY_TYPE_ANY && |
| 1838 | walk->type != pol->type) |
| 1839 | continue; |
| 1840 | error = func(pol, xfrm_policy_id2dir(pol->index), |
| 1841 | walk->seq, data); |
| 1842 | if (error) { |
| 1843 | list_move_tail(&walk->walk.all, &x->all); |
| 1844 | goto out; |
Timo Teras | 4c563f7 | 2008-02-28 21:31:08 -0800 | [diff] [blame] | 1845 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1846 | walk->seq++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1848 | if (walk->seq == 0) { |
Jamal Hadi Salim | baf5d74 | 2006-12-04 20:02:37 -0800 | [diff] [blame] | 1849 | error = -ENOENT; |
| 1850 | goto out; |
| 1851 | } |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1852 | list_del_init(&walk->walk.all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | out: |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1854 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | return error; |
| 1856 | } |
| 1857 | EXPORT_SYMBOL(xfrm_policy_walk); |
| 1858 | |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1859 | void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type) |
| 1860 | { |
| 1861 | INIT_LIST_HEAD(&walk->walk.all); |
| 1862 | walk->walk.dead = 1; |
| 1863 | walk->type = type; |
| 1864 | walk->seq = 0; |
| 1865 | } |
| 1866 | EXPORT_SYMBOL(xfrm_policy_walk_init); |
| 1867 | |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 1868 | 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] | 1869 | { |
| 1870 | if (list_empty(&walk->walk.all)) |
| 1871 | return; |
| 1872 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1873 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */ |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1874 | list_del(&walk->walk.all); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 1875 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 1876 | } |
| 1877 | EXPORT_SYMBOL(xfrm_policy_walk_done); |
| 1878 | |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1879 | /* |
| 1880 | * Find policy to apply to this flow. |
| 1881 | * |
| 1882 | * Returns 0 if policy found, else an -errno. |
| 1883 | */ |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 1884 | static int xfrm_policy_match(const struct xfrm_policy *pol, |
| 1885 | const struct flowi *fl, |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 1886 | u8 type, u16 family, int dir, u32 if_id) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1887 | { |
David S. Miller | f299d55 | 2011-02-24 01:23:30 -0500 | [diff] [blame] | 1888 | const struct xfrm_selector *sel = &pol->selector; |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 1889 | int ret = -ESRCH; |
| 1890 | bool match; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1891 | |
| 1892 | if (pol->family != family || |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 1893 | pol->if_id != if_id || |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1894 | (fl->flowi_mark & pol->mark.m) != pol->mark.v || |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1895 | pol->type != type) |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1896 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1897 | |
| 1898 | match = xfrm_selector_match(sel, fl, family); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1899 | if (match) |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 1900 | ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid, |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 1901 | dir); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 1902 | return ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1905 | static struct xfrm_pol_inexact_node * |
| 1906 | xfrm_policy_lookup_inexact_addr(const struct rb_root *r, |
| 1907 | seqcount_t *count, |
| 1908 | const xfrm_address_t *addr, u16 family) |
| 1909 | { |
| 1910 | const struct rb_node *parent; |
| 1911 | int seq; |
| 1912 | |
| 1913 | again: |
| 1914 | seq = read_seqcount_begin(count); |
| 1915 | |
| 1916 | parent = rcu_dereference_raw(r->rb_node); |
| 1917 | while (parent) { |
| 1918 | struct xfrm_pol_inexact_node *node; |
| 1919 | int delta; |
| 1920 | |
| 1921 | node = rb_entry(parent, struct xfrm_pol_inexact_node, node); |
| 1922 | |
| 1923 | delta = xfrm_policy_addr_delta(addr, &node->addr, |
| 1924 | node->prefixlen, family); |
| 1925 | if (delta < 0) { |
| 1926 | parent = rcu_dereference_raw(parent->rb_left); |
| 1927 | continue; |
| 1928 | } else if (delta > 0) { |
| 1929 | parent = rcu_dereference_raw(parent->rb_right); |
| 1930 | continue; |
| 1931 | } |
| 1932 | |
| 1933 | return node; |
| 1934 | } |
| 1935 | |
| 1936 | if (read_seqcount_retry(count, seq)) |
| 1937 | goto again; |
| 1938 | |
| 1939 | return NULL; |
| 1940 | } |
| 1941 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1942 | static bool |
| 1943 | xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand, |
| 1944 | struct xfrm_pol_inexact_bin *b, |
| 1945 | const xfrm_address_t *saddr, |
| 1946 | const xfrm_address_t *daddr) |
| 1947 | { |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1948 | struct xfrm_pol_inexact_node *n; |
| 1949 | u16 family; |
| 1950 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1951 | if (!b) |
| 1952 | return false; |
| 1953 | |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1954 | family = b->k.family; |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1955 | memset(cand, 0, sizeof(*cand)); |
| 1956 | cand->res[XFRM_POL_CAND_ANY] = &b->hhead; |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1957 | |
| 1958 | n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr, |
| 1959 | family); |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 1960 | if (n) { |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1961 | cand->res[XFRM_POL_CAND_DADDR] = &n->hhead; |
Florian Westphal | 6ac098b | 2018-11-07 23:00:41 +0100 | [diff] [blame] | 1962 | n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr, |
| 1963 | family); |
| 1964 | if (n) |
| 1965 | cand->res[XFRM_POL_CAND_BOTH] = &n->hhead; |
| 1966 | } |
Florian Westphal | 9cf545e | 2018-11-07 23:00:38 +0100 | [diff] [blame] | 1967 | |
Florian Westphal | 64a09a7 | 2018-11-07 23:00:40 +0100 | [diff] [blame] | 1968 | n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr, |
| 1969 | family); |
| 1970 | if (n) |
| 1971 | cand->res[XFRM_POL_CAND_SADDR] = &n->hhead; |
| 1972 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 1973 | return true; |
| 1974 | } |
| 1975 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1976 | static struct xfrm_pol_inexact_bin * |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1977 | xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family, |
| 1978 | u8 dir, u32 if_id) |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1979 | { |
| 1980 | struct xfrm_pol_inexact_key k = { |
| 1981 | .family = family, |
| 1982 | .type = type, |
| 1983 | .dir = dir, |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1984 | .if_id = if_id, |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1985 | }; |
| 1986 | |
| 1987 | write_pnet(&k.net, net); |
| 1988 | |
| 1989 | return rhashtable_lookup(&xfrm_policy_inexact_table, &k, |
| 1990 | xfrm_pol_inexact_params); |
| 1991 | } |
| 1992 | |
| 1993 | static struct xfrm_pol_inexact_bin * |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 1994 | xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, |
| 1995 | u8 dir, u32 if_id) |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 1996 | { |
| 1997 | struct xfrm_pol_inexact_bin *bin; |
| 1998 | |
| 1999 | lockdep_assert_held(&net->xfrm.xfrm_policy_lock); |
| 2000 | |
| 2001 | rcu_read_lock(); |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 2002 | bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2003 | rcu_read_unlock(); |
| 2004 | |
| 2005 | return bin; |
| 2006 | } |
| 2007 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 2008 | static struct xfrm_policy * |
| 2009 | __xfrm_policy_eval_candidates(struct hlist_head *chain, |
| 2010 | struct xfrm_policy *prefer, |
| 2011 | const struct flowi *fl, |
| 2012 | u8 type, u16 family, int dir, u32 if_id) |
| 2013 | { |
| 2014 | u32 priority = prefer ? prefer->priority : ~0u; |
| 2015 | struct xfrm_policy *pol; |
| 2016 | |
| 2017 | if (!chain) |
| 2018 | return NULL; |
| 2019 | |
| 2020 | hlist_for_each_entry_rcu(pol, chain, bydst) { |
| 2021 | int err; |
| 2022 | |
| 2023 | if (pol->priority > priority) |
| 2024 | break; |
| 2025 | |
| 2026 | err = xfrm_policy_match(pol, fl, type, family, dir, if_id); |
| 2027 | if (err) { |
| 2028 | if (err != -ESRCH) |
| 2029 | return ERR_PTR(err); |
| 2030 | |
| 2031 | continue; |
| 2032 | } |
| 2033 | |
| 2034 | if (prefer) { |
| 2035 | /* matches. Is it older than *prefer? */ |
| 2036 | if (pol->priority == priority && |
| 2037 | prefer->pos < pol->pos) |
| 2038 | return prefer; |
| 2039 | } |
| 2040 | |
| 2041 | return pol; |
| 2042 | } |
| 2043 | |
| 2044 | return NULL; |
| 2045 | } |
| 2046 | |
| 2047 | static struct xfrm_policy * |
| 2048 | xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand, |
| 2049 | struct xfrm_policy *prefer, |
| 2050 | const struct flowi *fl, |
| 2051 | u8 type, u16 family, int dir, u32 if_id) |
| 2052 | { |
| 2053 | struct xfrm_policy *tmp; |
| 2054 | int i; |
| 2055 | |
| 2056 | for (i = 0; i < ARRAY_SIZE(cand->res); i++) { |
| 2057 | tmp = __xfrm_policy_eval_candidates(cand->res[i], |
| 2058 | prefer, |
| 2059 | fl, type, family, dir, |
| 2060 | if_id); |
| 2061 | if (!tmp) |
| 2062 | continue; |
| 2063 | |
| 2064 | if (IS_ERR(tmp)) |
| 2065 | return tmp; |
| 2066 | prefer = tmp; |
| 2067 | } |
| 2068 | |
| 2069 | return prefer; |
| 2070 | } |
| 2071 | |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 2072 | 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] | 2073 | const struct flowi *fl, |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2074 | u16 family, u8 dir, |
| 2075 | u32 if_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 2077 | struct xfrm_pol_inexact_candidates cand; |
David S. Miller | 0b597e7 | 2011-02-24 01:22:48 -0500 | [diff] [blame] | 2078 | const xfrm_address_t *daddr, *saddr; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2079 | struct xfrm_pol_inexact_bin *bin; |
| 2080 | struct xfrm_policy *pol, *ret; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2081 | struct hlist_head *chain; |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 2082 | unsigned int sequence; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2083 | int err; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2084 | |
| 2085 | daddr = xfrm_flowi_daddr(fl, family); |
| 2086 | saddr = xfrm_flowi_saddr(fl, family); |
| 2087 | if (unlikely(!daddr || !saddr)) |
| 2088 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | |
Florian Westphal | a7c44247 | 2016-08-11 15:17:56 +0200 | [diff] [blame] | 2090 | rcu_read_lock(); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 2091 | retry: |
| 2092 | do { |
| 2093 | sequence = read_seqcount_begin(&xfrm_policy_hash_generation); |
| 2094 | chain = policy_hash_direct(net, daddr, saddr, family, dir); |
| 2095 | } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)); |
| 2096 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2097 | ret = NULL; |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 2098 | hlist_for_each_entry_rcu(pol, chain, bydst) { |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2099 | err = xfrm_policy_match(pol, fl, type, family, dir, if_id); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2100 | if (err) { |
| 2101 | if (err == -ESRCH) |
| 2102 | continue; |
| 2103 | else { |
| 2104 | ret = ERR_PTR(err); |
| 2105 | goto fail; |
| 2106 | } |
| 2107 | } else { |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 2108 | ret = pol; |
David S. Miller | acba48e | 2006-08-25 15:46:46 -0700 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | } |
Florian Westphal | b5fe22e | 2018-11-07 23:00:36 +0100 | [diff] [blame] | 2112 | bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id); |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 2113 | if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr, |
| 2114 | daddr)) |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2115 | goto skip_inexact; |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 2116 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 2117 | pol = xfrm_policy_eval_candidates(&cand, ret, fl, type, |
| 2118 | family, dir, if_id); |
| 2119 | if (pol) { |
| 2120 | ret = pol; |
| 2121 | if (IS_ERR(pol)) |
| 2122 | goto fail; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2123 | } |
Li RongQing | 586f2eb | 2015-04-30 17:13:41 +0800 | [diff] [blame] | 2124 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2125 | skip_inexact: |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 2126 | if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence)) |
| 2127 | goto retry; |
| 2128 | |
Florian Westphal | e37cc8ad | 2016-08-11 15:17:55 +0200 | [diff] [blame] | 2129 | if (ret && !xfrm_pol_hold_rcu(ret)) |
| 2130 | goto retry; |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 2131 | fail: |
Florian Westphal | a7c44247 | 2016-08-11 15:17:56 +0200 | [diff] [blame] | 2132 | rcu_read_unlock(); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2133 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2134 | return ret; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2137 | static struct xfrm_policy *xfrm_policy_lookup(struct net *net, |
| 2138 | const struct flowi *fl, |
| 2139 | u16 family, u8 dir, u32 if_id) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2140 | { |
| 2141 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 2142 | struct xfrm_policy *pol; |
| 2143 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2144 | pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, |
| 2145 | dir, if_id); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2146 | if (pol != NULL) |
| 2147 | return pol; |
| 2148 | #endif |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2149 | return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, |
| 2150 | dir, if_id); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Eric Dumazet | 6f9c961 | 2015-09-25 07:39:10 -0700 | [diff] [blame] | 2153 | static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir, |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2154 | const struct flowi *fl, |
| 2155 | u16 family, u32 if_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { |
| 2157 | struct xfrm_policy *pol; |
| 2158 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2159 | rcu_read_lock(); |
Florian Westphal | ae33786 | 2016-08-11 15:17:57 +0200 | [diff] [blame] | 2160 | again: |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2161 | pol = rcu_dereference(sk->sk_policy[dir]); |
| 2162 | if (pol != NULL) { |
Steffen Klassert | ddc47e4 | 2017-11-29 06:53:55 +0100 | [diff] [blame] | 2163 | bool match; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 2164 | int err = 0; |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 2165 | |
Steffen Klassert | ddc47e4 | 2017-11-29 06:53:55 +0100 | [diff] [blame] | 2166 | if (pol->family != family) { |
| 2167 | pol = NULL; |
| 2168 | goto out; |
| 2169 | } |
| 2170 | |
| 2171 | match = xfrm_selector_match(&pol->selector, fl, family); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2172 | if (match) { |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 2173 | if ((sk->sk_mark & pol->mark.m) != pol->mark.v || |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2174 | pol->if_id != if_id) { |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 2175 | pol = NULL; |
| 2176 | goto out; |
| 2177 | } |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 2178 | err = security_xfrm_policy_lookup(pol->security, |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 2179 | fl->flowi_secid, |
Florian Westphal | aff669b | 2017-07-17 13:57:23 +0200 | [diff] [blame] | 2180 | dir); |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 2181 | if (!err) { |
| 2182 | if (!xfrm_pol_hold_rcu(pol)) |
| 2183 | goto again; |
| 2184 | } else if (err == -ESRCH) { |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2185 | pol = NULL; |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 2186 | } else { |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2187 | pol = ERR_PTR(err); |
Florian Westphal | 330e832 | 2016-11-17 13:21:46 +0100 | [diff] [blame] | 2188 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 2189 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | pol = NULL; |
| 2191 | } |
Jamal Hadi Salim | 34f8d88 | 2010-02-22 11:32:58 +0000 | [diff] [blame] | 2192 | out: |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2193 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | return pol; |
| 2195 | } |
| 2196 | |
| 2197 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir) |
| 2198 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 2199 | struct net *net = xp_net(pol); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2200 | |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 2201 | list_add(&pol->walk.all, &net->xfrm.policy_all); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 2202 | net->xfrm.policy_count[dir]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | xfrm_pol_hold(pol); |
| 2204 | } |
| 2205 | |
| 2206 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, |
| 2207 | int dir) |
| 2208 | { |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 2209 | struct net *net = xp_net(pol); |
| 2210 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2211 | if (list_empty(&pol->walk.all)) |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2212 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2214 | /* Socket policies are not hashed. */ |
| 2215 | if (!hlist_unhashed(&pol->bydst)) { |
Florian Westphal | a5eefc1 | 2016-08-11 15:17:52 +0200 | [diff] [blame] | 2216 | hlist_del_rcu(&pol->bydst); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 2217 | hlist_del_init(&pol->bydst_inexact_list); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2218 | hlist_del(&pol->byidx); |
| 2219 | } |
| 2220 | |
| 2221 | list_del_init(&pol->walk.all); |
Alexey Dobriyan | 98806f7 | 2008-11-25 17:29:47 -0800 | [diff] [blame] | 2222 | net->xfrm.policy_count[dir]--; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 2223 | |
| 2224 | return pol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2227 | static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir) |
| 2228 | { |
| 2229 | __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir); |
| 2230 | } |
| 2231 | |
| 2232 | static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir) |
| 2233 | { |
| 2234 | __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir); |
| 2235 | } |
| 2236 | |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 2237 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 2239 | struct net *net = xp_net(pol); |
| 2240 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2241 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | pol = __xfrm_policy_unlink(pol, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2243 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | if (pol) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | xfrm_policy_kill(pol); |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 2246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | } |
Herbert Xu | 4666faa | 2005-06-18 22:43:22 -0700 | [diff] [blame] | 2248 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | } |
David S. Miller | a70fcb0 | 2006-03-20 19:18:52 -0800 | [diff] [blame] | 2250 | EXPORT_SYMBOL(xfrm_policy_delete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | |
| 2252 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol) |
| 2253 | { |
Lorenzo Colitti | be8f828 | 2017-11-20 19:26:02 +0900 | [diff] [blame] | 2254 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | struct xfrm_policy *old_pol; |
| 2256 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2257 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 2258 | if (pol && pol->type != XFRM_POLICY_TYPE_MAIN) |
| 2259 | return -EINVAL; |
| 2260 | #endif |
| 2261 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2262 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2263 | old_pol = rcu_dereference_protected(sk->sk_policy[dir], |
| 2264 | lockdep_is_held(&net->xfrm.xfrm_policy_lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | if (pol) { |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 2266 | pol->curlft.add_time = ktime_get_real_seconds(); |
Fan Du | e682adf0 | 2013-11-07 17:47:48 +0800 | [diff] [blame] | 2267 | pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2268 | xfrm_sk_policy_link(pol, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | } |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2270 | rcu_assign_pointer(sk->sk_policy[dir], pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2271 | if (old_pol) { |
| 2272 | if (pol) |
| 2273 | xfrm_policy_requeue(old_pol, pol); |
| 2274 | |
Timo Teräs | ea2dea9 | 2010-03-31 00:17:05 +0000 | [diff] [blame] | 2275 | /* Unlinking succeeds always. This is the only function |
| 2276 | * allowed to delete or replace socket policy. |
| 2277 | */ |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2278 | xfrm_sk_policy_unlink(old_pol, dir); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2279 | } |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2280 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
| 2282 | if (old_pol) { |
| 2283 | xfrm_policy_kill(old_pol); |
| 2284 | } |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
David S. Miller | d3e40a9 | 2011-02-24 01:25:41 -0500 | [diff] [blame] | 2288 | 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] | 2289 | { |
Alexey Dobriyan | 0331b1f | 2008-11-25 17:21:45 -0800 | [diff] [blame] | 2290 | struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC); |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 2291 | struct net *net = xp_net(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | |
| 2293 | if (newp) { |
| 2294 | newp->selector = old->selector; |
Paul Moore | 03e1ad7 | 2008-04-12 19:07:52 -0700 | [diff] [blame] | 2295 | if (security_xfrm_policy_clone(old->security, |
| 2296 | &newp->security)) { |
Trent Jaeger | df71837 | 2005-12-13 23:12:27 -0800 | [diff] [blame] | 2297 | kfree(newp); |
| 2298 | return NULL; /* ENOMEM */ |
| 2299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | newp->lft = old->lft; |
| 2301 | newp->curlft = old->curlft; |
Jamal Hadi Salim | fb977e2 | 2010-02-23 15:09:53 -0800 | [diff] [blame] | 2302 | newp->mark = old->mark; |
Steffen Klassert | 7e65264 | 2018-06-12 14:07:07 +0200 | [diff] [blame] | 2303 | newp->if_id = old->if_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | newp->action = old->action; |
| 2305 | newp->flags = old->flags; |
| 2306 | newp->xfrm_nr = old->xfrm_nr; |
| 2307 | newp->index = old->index; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2308 | newp->type = old->type; |
Herbert Xu | 0e74aa1 | 2017-11-10 14:14:06 +1100 | [diff] [blame] | 2309 | newp->family = old->family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | memcpy(newp->xfrm_vec, old->xfrm_vec, |
| 2311 | newp->xfrm_nr*sizeof(struct xfrm_tmpl)); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2312 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 2313 | xfrm_sk_policy_link(newp, dir); |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 2314 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | xfrm_pol_put(newp); |
| 2316 | } |
| 2317 | return newp; |
| 2318 | } |
| 2319 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2320 | int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | { |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2322 | const struct xfrm_policy *p; |
| 2323 | struct xfrm_policy *np; |
| 2324 | int i, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | |
Eric Dumazet | d188ba8 | 2015-12-08 07:22:02 -0800 | [diff] [blame] | 2326 | rcu_read_lock(); |
| 2327 | for (i = 0; i < 2; i++) { |
| 2328 | p = rcu_dereference(osk->sk_policy[i]); |
| 2329 | if (p) { |
| 2330 | np = clone_policy(p, i); |
| 2331 | if (unlikely(!np)) { |
| 2332 | ret = -ENOMEM; |
| 2333 | break; |
| 2334 | } |
| 2335 | rcu_assign_pointer(sk->sk_policy[i], np); |
| 2336 | } |
| 2337 | } |
| 2338 | rcu_read_unlock(); |
| 2339 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | } |
| 2341 | |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 2342 | static int |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 2343 | xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local, |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 2344 | xfrm_address_t *remote, unsigned short family, u32 mark) |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 2345 | { |
| 2346 | int err; |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 2347 | const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 2348 | |
| 2349 | if (unlikely(afinfo == NULL)) |
| 2350 | return -EINVAL; |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 2351 | err = afinfo->get_saddr(net, oif, local, remote, mark); |
Florian Westphal | bdba9fe | 2017-02-07 15:00:18 +0100 | [diff] [blame] | 2352 | rcu_read_unlock(); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 2353 | return err; |
| 2354 | } |
| 2355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | /* Resolve list of templates for the flow, given policy. */ |
| 2357 | |
| 2358 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 2359 | xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl, |
| 2360 | struct xfrm_state **xfrm, unsigned short family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | { |
Alexey Dobriyan | fbda33b | 2008-11-25 17:56:49 -0800 | [diff] [blame] | 2362 | struct net *net = xp_net(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | int nx; |
| 2364 | int i, error; |
Steffen Klassert | 9480215 | 2017-11-15 06:40:57 +0100 | [diff] [blame] | 2365 | xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family); |
| 2366 | xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family); |
Patrick McHardy | a1e59ab | 2006-09-19 12:57:34 -0700 | [diff] [blame] | 2367 | xfrm_address_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2369 | for (nx = 0, i = 0; i < policy->xfrm_nr; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | struct xfrm_state *x; |
Steffen Klassert | 9480215 | 2017-11-15 06:40:57 +0100 | [diff] [blame] | 2371 | xfrm_address_t *remote = daddr; |
| 2372 | xfrm_address_t *local = saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i]; |
| 2374 | |
Steffen Klassert | 9480215 | 2017-11-15 06:40:57 +0100 | [diff] [blame] | 2375 | if (tmpl->mode == XFRM_MODE_TUNNEL || |
| 2376 | tmpl->mode == XFRM_MODE_BEET) { |
| 2377 | remote = &tmpl->id.daddr; |
| 2378 | local = &tmpl->saddr; |
| 2379 | if (xfrm_addr_any(local, tmpl->encap_family)) { |
| 2380 | error = xfrm_get_saddr(net, fl->flowi_oif, |
| 2381 | &tmp, remote, |
| 2382 | tmpl->encap_family, 0); |
| 2383 | if (error) |
| 2384 | goto fail; |
| 2385 | local = &tmp; |
| 2386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | } |
| 2388 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2389 | x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, |
| 2390 | family, policy->if_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
| 2392 | if (x && x->km.state == XFRM_STATE_VALID) { |
| 2393 | xfrm[nx++] = x; |
Steffen Klassert | 9480215 | 2017-11-15 06:40:57 +0100 | [diff] [blame] | 2394 | daddr = remote; |
| 2395 | saddr = local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | continue; |
| 2397 | } |
| 2398 | if (x) { |
| 2399 | error = (x->km.state == XFRM_STATE_ERROR ? |
| 2400 | -EINVAL : -EAGAIN); |
| 2401 | xfrm_state_put(x); |
Weilong Chen | 42054569 | 2013-12-24 09:43:49 +0800 | [diff] [blame] | 2402 | } else if (error == -ESRCH) { |
fernando@oss.ntt.co | a4322266 | 2008-10-23 04:27:19 +0000 | [diff] [blame] | 2403 | error = -EAGAIN; |
Weilong Chen | 42054569 | 2013-12-24 09:43:49 +0800 | [diff] [blame] | 2404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
| 2406 | if (!tmpl->optional) |
| 2407 | goto fail; |
| 2408 | } |
| 2409 | return nx; |
| 2410 | |
| 2411 | fail: |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2412 | for (nx--; nx >= 0; nx--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | xfrm_state_put(xfrm[nx]); |
| 2414 | return error; |
| 2415 | } |
| 2416 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2417 | static int |
David S. Miller | a6c2e61 | 2011-02-22 18:35:39 -0800 | [diff] [blame] | 2418 | xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl, |
| 2419 | struct xfrm_state **xfrm, unsigned short family) |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2420 | { |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2421 | struct xfrm_state *tp[XFRM_MAX_DEPTH]; |
| 2422 | struct xfrm_state **tpp = (npols > 1) ? tp : xfrm; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2423 | int cnx = 0; |
| 2424 | int error; |
| 2425 | int ret; |
| 2426 | int i; |
| 2427 | |
| 2428 | for (i = 0; i < npols; i++) { |
| 2429 | if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) { |
| 2430 | error = -ENOBUFS; |
| 2431 | goto fail; |
| 2432 | } |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2433 | |
| 2434 | ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2435 | if (ret < 0) { |
| 2436 | error = ret; |
| 2437 | goto fail; |
| 2438 | } else |
| 2439 | cnx += ret; |
| 2440 | } |
| 2441 | |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2442 | /* found states are sorted for outbound processing */ |
| 2443 | if (npols > 1) |
| 2444 | xfrm_state_sort(xfrm, tpp, cnx, family); |
| 2445 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2446 | return cnx; |
| 2447 | |
| 2448 | fail: |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 2449 | for (cnx--; cnx >= 0; cnx--) |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 2450 | xfrm_state_put(tpp[cnx]); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 2451 | return error; |
| 2452 | |
| 2453 | } |
| 2454 | |
Florian Westphal | f5e2bb4 | 2017-02-07 15:00:14 +0100 | [diff] [blame] | 2455 | static int xfrm_get_tos(const struct flowi *fl, int family) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2456 | { |
Florian Westphal | f24ea52 | 2019-04-16 16:44:37 +0200 | [diff] [blame] | 2457 | if (family == AF_INET) |
| 2458 | return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2459 | |
Florian Westphal | f24ea52 | 2019-04-16 16:44:37 +0200 | [diff] [blame] | 2460 | return 0; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2463 | 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] | 2464 | { |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 2465 | const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2466 | struct dst_ops *dst_ops; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2467 | struct xfrm_dst *xdst; |
| 2468 | |
| 2469 | if (!afinfo) |
| 2470 | return ERR_PTR(-EINVAL); |
| 2471 | |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2472 | switch (family) { |
| 2473 | case AF_INET: |
| 2474 | dst_ops = &net->xfrm.xfrm4_dst_ops; |
| 2475 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 2476 | #if IS_ENABLED(CONFIG_IPV6) |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2477 | case AF_INET6: |
| 2478 | dst_ops = &net->xfrm.xfrm6_dst_ops; |
| 2479 | break; |
| 2480 | #endif |
| 2481 | default: |
| 2482 | BUG(); |
| 2483 | } |
Wei Wang | b2a9c0e | 2017-06-17 10:42:41 -0700 | [diff] [blame] | 2484 | xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2485 | |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 2486 | if (likely(xdst)) { |
Steffen Klassert | 141e369 | 2012-07-05 23:39:34 +0000 | [diff] [blame] | 2487 | struct dst_entry *dst = &xdst->u.dst; |
| 2488 | |
| 2489 | memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst)); |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 2490 | } else |
Hiroaki SHIMODA | 0b15093 | 2011-02-10 23:08:33 -0800 | [diff] [blame] | 2491 | xdst = ERR_PTR(-ENOBUFS); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2492 | |
Florian Westphal | bdba9fe | 2017-02-07 15:00:18 +0100 | [diff] [blame] | 2493 | rcu_read_unlock(); |
Madalin Bucur | d4cae56 | 2011-09-26 07:04:36 +0000 | [diff] [blame] | 2494 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2495 | return xdst; |
| 2496 | } |
| 2497 | |
Florian Westphal | 2e8b4aa | 2019-04-16 16:44:38 +0200 | [diff] [blame] | 2498 | static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst, |
| 2499 | int nfheader_len) |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 2500 | { |
Florian Westphal | 2e8b4aa | 2019-04-16 16:44:38 +0200 | [diff] [blame] | 2501 | if (dst->ops->family == AF_INET6) { |
| 2502 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 2503 | path->path_cookie = rt6_get_cookie(rt); |
| 2504 | path->u.rt6.rt6i_nfheader_len = nfheader_len; |
| 2505 | } |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 2506 | } |
| 2507 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 2508 | 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] | 2509 | const struct flowi *fl) |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2510 | { |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 2511 | const struct xfrm_policy_afinfo *afinfo = |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2512 | xfrm_policy_get_afinfo(xdst->u.dst.ops->family); |
| 2513 | int err; |
| 2514 | |
| 2515 | if (!afinfo) |
| 2516 | return -EINVAL; |
| 2517 | |
Herbert Xu | 87c1e12 | 2010-03-02 02:51:56 +0000 | [diff] [blame] | 2518 | err = afinfo->fill_dst(xdst, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2519 | |
Florian Westphal | bdba9fe | 2017-02-07 15:00:18 +0100 | [diff] [blame] | 2520 | rcu_read_unlock(); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2521 | |
| 2522 | return err; |
| 2523 | } |
| 2524 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate |
| 2527 | * all the metrics... Shortly, bundle a bundle. |
| 2528 | */ |
| 2529 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2530 | static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy, |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 2531 | struct xfrm_state **xfrm, |
| 2532 | struct xfrm_dst **bundle, |
| 2533 | int nx, |
David S. Miller | 98313ad | 2011-02-22 18:36:50 -0800 | [diff] [blame] | 2534 | const struct flowi *fl, |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2535 | struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | { |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 2537 | const struct xfrm_state_afinfo *afinfo; |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 2538 | const struct xfrm_mode *inner_mode; |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2539 | struct net *net = xp_net(policy); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2540 | unsigned long now = jiffies; |
| 2541 | struct net_device *dev; |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2542 | struct xfrm_dst *xdst_prev = NULL; |
| 2543 | struct xfrm_dst *xdst0 = NULL; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2544 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | int err; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2546 | int header_len = 0; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 2547 | int nfheader_len = 0; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2548 | int trailer_len = 0; |
| 2549 | int tos; |
| 2550 | int family = policy->selector.family; |
YOSHIFUJI Hideaki | 9bb182a | 2008-02-22 14:48:22 +0900 | [diff] [blame] | 2551 | xfrm_address_t saddr, daddr; |
| 2552 | |
| 2553 | xfrm_flowi_addr_get(fl, &saddr, &daddr, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2554 | |
| 2555 | tos = xfrm_get_tos(fl, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2556 | |
| 2557 | dst_hold(dst); |
| 2558 | |
| 2559 | for (; i < nx; i++) { |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 2560 | struct xfrm_dst *xdst = xfrm_alloc_dst(net, family); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2561 | struct dst_entry *dst1 = &xdst->u.dst; |
| 2562 | |
| 2563 | err = PTR_ERR(xdst); |
| 2564 | if (IS_ERR(xdst)) { |
| 2565 | dst_release(dst); |
| 2566 | goto put_states; |
| 2567 | } |
| 2568 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 2569 | bundle[i] = xdst; |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2570 | if (!xdst_prev) |
| 2571 | xdst0 = xdst; |
David Miller | 10a7ef3 | 2017-10-10 20:59:38 -0700 | [diff] [blame] | 2572 | else |
| 2573 | /* Ref count is taken during xfrm_alloc_dst() |
| 2574 | * No need to do dst_clone() on dst1 |
| 2575 | */ |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2576 | xfrm_dst_set_child(xdst_prev, &xdst->u.dst); |
David Miller | 10a7ef3 | 2017-10-10 20:59:38 -0700 | [diff] [blame] | 2577 | |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 2578 | if (xfrm[i]->sel.family == AF_UNSPEC) { |
| 2579 | inner_mode = xfrm_ip2inner_mode(xfrm[i], |
| 2580 | xfrm_af2proto(family)); |
| 2581 | if (!inner_mode) { |
| 2582 | err = -EAFNOSUPPORT; |
| 2583 | dst_release(dst); |
| 2584 | goto put_states; |
| 2585 | } |
| 2586 | } else |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 2587 | inner_mode = &xfrm[i]->inner_mode; |
Steffen Klassert | 43a4dea | 2011-05-09 19:36:38 +0000 | [diff] [blame] | 2588 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2589 | xdst->route = dst; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2590 | dst_copy_metrics(dst1, dst); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2591 | |
| 2592 | if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) { |
Benedict Wong | e2612cd | 2019-01-14 11:24:38 -0800 | [diff] [blame] | 2593 | __u32 mark = 0; |
| 2594 | |
| 2595 | if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m) |
| 2596 | mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]); |
Steffen Klassert | 9b42c1f | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 2597 | |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2598 | family = xfrm[i]->props.family; |
David Ahern | 42a7b32 | 2015-08-10 16:58:11 -0600 | [diff] [blame] | 2599 | dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif, |
Steffen Klassert | 9b42c1f | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 2600 | &saddr, &daddr, family, mark); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2601 | err = PTR_ERR(dst); |
| 2602 | if (IS_ERR(dst)) |
| 2603 | goto put_states; |
| 2604 | } else |
| 2605 | dst_hold(dst); |
| 2606 | |
| 2607 | dst1->xfrm = xfrm[i]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2608 | xdst->xfrm_genid = xfrm[i]->genid; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2609 | |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 2610 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2611 | dst1->flags |= DST_HOST; |
| 2612 | dst1->lastuse = now; |
| 2613 | |
| 2614 | dst1->input = dst_discard; |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 2615 | |
| 2616 | rcu_read_lock(); |
| 2617 | afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family); |
| 2618 | if (likely(afinfo)) |
| 2619 | dst1->output = afinfo->output; |
| 2620 | else |
| 2621 | dst1->output = dst_discard_out; |
| 2622 | rcu_read_unlock(); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2623 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2624 | xdst_prev = xdst; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2625 | |
| 2626 | header_len += xfrm[i]->props.header_len; |
Masahide NAKAMURA | a1b0514 | 2007-12-20 20:41:12 -0800 | [diff] [blame] | 2627 | if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT) |
| 2628 | nfheader_len += xfrm[i]->props.header_len; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2629 | trailer_len += xfrm[i]->props.trailer_len; |
| 2630 | } |
| 2631 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2632 | xfrm_dst_set_child(xdst_prev, dst); |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 2633 | xdst0->path = dst; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2634 | |
| 2635 | err = -ENODEV; |
| 2636 | dev = dst->dev; |
| 2637 | if (!dev) |
| 2638 | goto free_dst; |
| 2639 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2640 | xfrm_init_path(xdst0, dst, nfheader_len); |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 2641 | xfrm_init_pmtu(bundle, nx); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2642 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2643 | for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst; |
| 2644 | xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) { |
| 2645 | err = xfrm_fill_dst(xdst_prev, dev, fl); |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2646 | if (err) |
| 2647 | goto free_dst; |
| 2648 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2649 | xdst_prev->u.dst.header_len = header_len; |
| 2650 | xdst_prev->u.dst.trailer_len = trailer_len; |
| 2651 | header_len -= xdst_prev->u.dst.xfrm->props.header_len; |
| 2652 | trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2655 | return &xdst0->u.dst; |
Herbert Xu | 25ee328 | 2007-12-11 09:32:34 -0800 | [diff] [blame] | 2656 | |
| 2657 | put_states: |
| 2658 | for (; i < nx; i++) |
| 2659 | xfrm_state_put(xfrm[i]); |
| 2660 | free_dst: |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2661 | if (xdst0) |
| 2662 | dst_release_immediate(&xdst0->u.dst); |
Steffen Klassert | 38369f5 | 2018-05-31 09:45:18 +0200 | [diff] [blame] | 2663 | |
| 2664 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
David S. Miller | 73ff93c | 2011-02-22 18:33:42 -0800 | [diff] [blame] | 2667 | static int xfrm_expand_policies(const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2668 | struct xfrm_policy **pols, |
| 2669 | int *num_pols, int *num_xfrms) |
| 2670 | { |
| 2671 | int i; |
| 2672 | |
| 2673 | if (*num_pols == 0 || !pols[0]) { |
| 2674 | *num_pols = 0; |
| 2675 | *num_xfrms = 0; |
| 2676 | return 0; |
| 2677 | } |
| 2678 | if (IS_ERR(pols[0])) |
| 2679 | return PTR_ERR(pols[0]); |
| 2680 | |
| 2681 | *num_xfrms = pols[0]->xfrm_nr; |
| 2682 | |
| 2683 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 2684 | if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW && |
| 2685 | pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
| 2686 | pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]), |
| 2687 | XFRM_POLICY_TYPE_MAIN, |
| 2688 | fl, family, |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2689 | XFRM_POLICY_OUT, |
| 2690 | pols[0]->if_id); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2691 | if (pols[1]) { |
| 2692 | if (IS_ERR(pols[1])) { |
| 2693 | xfrm_pols_put(pols, *num_pols); |
| 2694 | return PTR_ERR(pols[1]); |
| 2695 | } |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 2696 | (*num_pols)++; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2697 | (*num_xfrms) += pols[1]->xfrm_nr; |
| 2698 | } |
| 2699 | } |
| 2700 | #endif |
| 2701 | for (i = 0; i < *num_pols; i++) { |
| 2702 | if (pols[i]->action != XFRM_POLICY_ALLOW) { |
| 2703 | *num_xfrms = -1; |
| 2704 | break; |
| 2705 | } |
| 2706 | } |
| 2707 | |
| 2708 | return 0; |
| 2709 | |
| 2710 | } |
| 2711 | |
| 2712 | static struct xfrm_dst * |
| 2713 | 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] | 2714 | const struct flowi *fl, u16 family, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2715 | struct dst_entry *dst_orig) |
| 2716 | { |
| 2717 | struct net *net = xp_net(pols[0]); |
| 2718 | struct xfrm_state *xfrm[XFRM_MAX_DEPTH]; |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 2719 | struct xfrm_dst *bundle[XFRM_MAX_DEPTH]; |
Florian Westphal | e4db5b6 | 2018-06-25 17:26:02 +0200 | [diff] [blame] | 2720 | struct xfrm_dst *xdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2721 | struct dst_entry *dst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2722 | int err; |
| 2723 | |
| 2724 | /* Try to instantiate a bundle */ |
| 2725 | err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family); |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 2726 | if (err <= 0) { |
YueHaibing | 934ffce | 2018-07-25 16:54:33 +0800 | [diff] [blame] | 2727 | if (err == 0) |
| 2728 | return NULL; |
| 2729 | |
| 2730 | if (err != -EAGAIN) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2731 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 2732 | return ERR_PTR(err); |
| 2733 | } |
| 2734 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 2735 | dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2736 | if (IS_ERR(dst)) { |
| 2737 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR); |
| 2738 | return ERR_CAST(dst); |
| 2739 | } |
| 2740 | |
| 2741 | xdst = (struct xfrm_dst *)dst; |
| 2742 | xdst->num_xfrms = err; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2743 | xdst->num_pols = num_pols; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 2744 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2745 | xdst->policy_genid = atomic_read(&pols[0]->genid); |
| 2746 | |
| 2747 | return xdst; |
| 2748 | } |
| 2749 | |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 2750 | static void xfrm_policy_queue_process(struct timer_list *t) |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2751 | { |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2752 | struct sk_buff *skb; |
| 2753 | struct sock *sk; |
| 2754 | struct dst_entry *dst; |
Kees Cook | c3aed70 | 2017-10-16 17:28:56 -0700 | [diff] [blame] | 2755 | struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer); |
Eric W. Biederman | 3f5312a | 2015-10-07 16:48:34 -0500 | [diff] [blame] | 2756 | struct net *net = xp_net(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2757 | struct xfrm_policy_queue *pq = &pol->polq; |
| 2758 | struct flowi fl; |
| 2759 | struct sk_buff_head list; |
| 2760 | |
| 2761 | spin_lock(&pq->hold_queue.lock); |
| 2762 | skb = skb_peek(&pq->hold_queue); |
Steffen Klassert | 2bb53e2 | 2013-10-08 10:49:51 +0200 | [diff] [blame] | 2763 | if (!skb) { |
| 2764 | spin_unlock(&pq->hold_queue.lock); |
| 2765 | goto out; |
| 2766 | } |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2767 | dst = skb_dst(skb); |
| 2768 | sk = skb->sk; |
| 2769 | xfrm_decode_session(skb, &fl, dst->ops->family); |
| 2770 | spin_unlock(&pq->hold_queue.lock); |
| 2771 | |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 2772 | dst_hold(xfrm_dst_path(dst)); |
Steffen Klassert | 2471c98 | 2018-02-01 11:26:12 +0100 | [diff] [blame] | 2773 | dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2774 | if (IS_ERR(dst)) |
| 2775 | goto purge_queue; |
| 2776 | |
| 2777 | if (dst->flags & DST_XFRM_QUEUE) { |
| 2778 | dst_release(dst); |
| 2779 | |
| 2780 | if (pq->timeout >= XFRM_QUEUE_TMO_MAX) |
| 2781 | goto purge_queue; |
| 2782 | |
| 2783 | pq->timeout = pq->timeout << 1; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2784 | if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout)) |
| 2785 | xfrm_pol_hold(pol); |
Colin Ian King | 7759d6a | 2018-11-13 14:28:42 +0000 | [diff] [blame] | 2786 | goto out; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | dst_release(dst); |
| 2790 | |
| 2791 | __skb_queue_head_init(&list); |
| 2792 | |
| 2793 | spin_lock(&pq->hold_queue.lock); |
| 2794 | pq->timeout = 0; |
| 2795 | skb_queue_splice_init(&pq->hold_queue, &list); |
| 2796 | spin_unlock(&pq->hold_queue.lock); |
| 2797 | |
| 2798 | while (!skb_queue_empty(&list)) { |
| 2799 | skb = __skb_dequeue(&list); |
| 2800 | |
| 2801 | xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family); |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 2802 | dst_hold(xfrm_dst_path(skb_dst(skb))); |
| 2803 | dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2804 | if (IS_ERR(dst)) { |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2805 | kfree_skb(skb); |
| 2806 | continue; |
| 2807 | } |
| 2808 | |
| 2809 | nf_reset(skb); |
| 2810 | skb_dst_drop(skb); |
| 2811 | skb_dst_set(skb, dst); |
| 2812 | |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 2813 | dst_output(net, skb->sk, skb); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2814 | } |
| 2815 | |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2816 | out: |
| 2817 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2818 | return; |
| 2819 | |
| 2820 | purge_queue: |
| 2821 | pq->timeout = 0; |
Li RongQing | 1ee5e66 | 2015-04-22 15:51:16 +0800 | [diff] [blame] | 2822 | skb_queue_purge(&pq->hold_queue); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2823 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 2826 | static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2827 | { |
| 2828 | unsigned long sched_next; |
| 2829 | struct dst_entry *dst = skb_dst(skb); |
| 2830 | struct xfrm_dst *xdst = (struct xfrm_dst *) dst; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2831 | struct xfrm_policy *pol = xdst->pols[0]; |
| 2832 | struct xfrm_policy_queue *pq = &pol->polq; |
Steffen Klassert | 4d53eff | 2013-10-16 13:42:46 +0200 | [diff] [blame] | 2833 | |
Eric Dumazet | 39bb5e6 | 2014-10-30 10:32:34 -0700 | [diff] [blame] | 2834 | if (unlikely(skb_fclone_busy(sk, skb))) { |
Steffen Klassert | 4d53eff | 2013-10-16 13:42:46 +0200 | [diff] [blame] | 2835 | kfree_skb(skb); |
| 2836 | return 0; |
| 2837 | } |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2838 | |
| 2839 | if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) { |
| 2840 | kfree_skb(skb); |
| 2841 | return -EAGAIN; |
| 2842 | } |
| 2843 | |
| 2844 | skb_dst_force(skb); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2845 | |
| 2846 | spin_lock_bh(&pq->hold_queue.lock); |
| 2847 | |
| 2848 | if (!pq->timeout) |
| 2849 | pq->timeout = XFRM_QUEUE_TMO_MIN; |
| 2850 | |
| 2851 | sched_next = jiffies + pq->timeout; |
| 2852 | |
| 2853 | if (del_timer(&pq->hold_timer)) { |
| 2854 | if (time_before(pq->hold_timer.expires, sched_next)) |
| 2855 | sched_next = pq->hold_timer.expires; |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2856 | xfrm_pol_put(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | __skb_queue_tail(&pq->hold_queue, skb); |
Steffen Klassert | e7d8f6c | 2013-10-08 10:49:45 +0200 | [diff] [blame] | 2860 | if (!mod_timer(&pq->hold_timer, sched_next)) |
| 2861 | xfrm_pol_hold(pol); |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2862 | |
| 2863 | spin_unlock_bh(&pq->hold_queue.lock); |
| 2864 | |
| 2865 | return 0; |
| 2866 | } |
| 2867 | |
| 2868 | static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net, |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2869 | struct xfrm_flo *xflo, |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2870 | const struct flowi *fl, |
| 2871 | int num_xfrms, |
| 2872 | u16 family) |
| 2873 | { |
| 2874 | int err; |
| 2875 | struct net_device *dev; |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2876 | struct dst_entry *dst; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2877 | struct dst_entry *dst1; |
| 2878 | struct xfrm_dst *xdst; |
| 2879 | |
| 2880 | xdst = xfrm_alloc_dst(net, family); |
| 2881 | if (IS_ERR(xdst)) |
| 2882 | return xdst; |
| 2883 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2884 | if (!(xflo->flags & XFRM_LOOKUP_QUEUE) || |
| 2885 | net->xfrm.sysctl_larval_drop || |
| 2886 | num_xfrms <= 0) |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2887 | return xdst; |
| 2888 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2889 | dst = xflo->dst_orig; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2890 | dst1 = &xdst->u.dst; |
| 2891 | dst_hold(dst); |
| 2892 | xdst->route = dst; |
| 2893 | |
| 2894 | dst_copy_metrics(dst1, dst); |
| 2895 | |
| 2896 | dst1->obsolete = DST_OBSOLETE_FORCE_CHK; |
| 2897 | dst1->flags |= DST_HOST | DST_XFRM_QUEUE; |
| 2898 | dst1->lastuse = jiffies; |
| 2899 | |
| 2900 | dst1->input = dst_discard; |
| 2901 | dst1->output = xdst_queue_output; |
| 2902 | |
| 2903 | dst_hold(dst); |
David Miller | 45b018be | 2017-11-28 15:40:28 -0500 | [diff] [blame] | 2904 | xfrm_dst_set_child(xdst, dst); |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 2905 | xdst->path = dst; |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 2906 | |
| 2907 | xfrm_init_path((struct xfrm_dst *)dst1, dst, 0); |
| 2908 | |
| 2909 | err = -ENODEV; |
| 2910 | dev = dst->dev; |
| 2911 | if (!dev) |
| 2912 | goto free_dst; |
| 2913 | |
| 2914 | err = xfrm_fill_dst(xdst, dev, fl); |
| 2915 | if (err) |
| 2916 | goto free_dst; |
| 2917 | |
| 2918 | out: |
| 2919 | return xdst; |
| 2920 | |
| 2921 | free_dst: |
| 2922 | dst_release(dst1); |
| 2923 | xdst = ERR_PTR(err); |
| 2924 | goto out; |
| 2925 | } |
| 2926 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2927 | static struct xfrm_dst *xfrm_bundle_lookup(struct net *net, |
| 2928 | const struct flowi *fl, |
| 2929 | u16 family, u8 dir, |
| 2930 | struct xfrm_flo *xflo, u32 if_id) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2931 | { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2932 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2933 | int num_pols = 0, num_xfrms = 0, err; |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2934 | struct xfrm_dst *xdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2935 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2936 | /* Resolve policies to use if we couldn't get them from |
| 2937 | * previous cache entry */ |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2938 | num_pols = 1; |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 2939 | pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id); |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2940 | err = xfrm_expand_policies(fl, family, pols, |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2941 | &num_pols, &num_xfrms); |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2942 | if (err < 0) |
| 2943 | goto inc_error; |
| 2944 | if (num_pols == 0) |
| 2945 | return NULL; |
| 2946 | if (num_xfrms <= 0) |
| 2947 | goto make_dummy_bundle; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2948 | |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2949 | xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, |
Steffen Klassert | 76a4201 | 2018-01-10 12:14:28 +0100 | [diff] [blame] | 2950 | xflo->dst_orig); |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2951 | if (IS_ERR(xdst)) { |
| 2952 | err = PTR_ERR(xdst); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 2953 | if (err == -EREMOTE) { |
| 2954 | xfrm_pols_put(pols, num_pols); |
| 2955 | return NULL; |
| 2956 | } |
| 2957 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2958 | if (err != -EAGAIN) |
| 2959 | goto error; |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2960 | goto make_dummy_bundle; |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2961 | } else if (xdst == NULL) { |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 2962 | num_xfrms = 0; |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2963 | goto make_dummy_bundle; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2966 | return xdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2967 | |
| 2968 | make_dummy_bundle: |
| 2969 | /* We found policies, but there's no bundles to instantiate: |
| 2970 | * either because the policy blocks, has no transformations or |
| 2971 | * we could not build template (no xfrm_states).*/ |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 2972 | xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2973 | if (IS_ERR(xdst)) { |
| 2974 | xfrm_pols_put(pols, num_pols); |
| 2975 | return ERR_CAST(xdst); |
| 2976 | } |
| 2977 | xdst->num_pols = num_pols; |
| 2978 | xdst->num_xfrms = num_xfrms; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 2979 | memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2980 | |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 2981 | return xdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2982 | |
| 2983 | inc_error: |
| 2984 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR); |
| 2985 | error: |
Florian Westphal | 855dad9 | 2017-07-17 13:57:22 +0200 | [diff] [blame] | 2986 | xfrm_pols_put(pols, num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 2987 | return ERR_PTR(err); |
| 2988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2990 | static struct dst_entry *make_blackhole(struct net *net, u16 family, |
| 2991 | struct dst_entry *dst_orig) |
| 2992 | { |
Florian Westphal | 37b1038 | 2017-02-07 15:00:19 +0100 | [diff] [blame] | 2993 | const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2994 | struct dst_entry *ret; |
| 2995 | |
| 2996 | if (!afinfo) { |
| 2997 | dst_release(dst_orig); |
Li RongQing | 433a195 | 2012-09-17 22:40:10 +0000 | [diff] [blame] | 2998 | return ERR_PTR(-EINVAL); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 2999 | } else { |
| 3000 | ret = afinfo->blackhole_route(net, dst_orig); |
| 3001 | } |
Florian Westphal | bdba9fe | 2017-02-07 15:00:18 +0100 | [diff] [blame] | 3002 | rcu_read_unlock(); |
David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 3003 | |
| 3004 | return ret; |
| 3005 | } |
| 3006 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3007 | /* Finds/creates a bundle for given flow and if_id |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | * |
| 3009 | * At the moment we eat a raw IP route. Mostly to speed up lookups |
| 3010 | * on interfaces with disabled IPsec. |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3011 | * |
| 3012 | * xfrm_lookup uses an if_id of 0 by default, and is provided for |
| 3013 | * compatibility |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | */ |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3015 | struct dst_entry *xfrm_lookup_with_ifid(struct net *net, |
| 3016 | struct dst_entry *dst_orig, |
| 3017 | const struct flowi *fl, |
| 3018 | const struct sock *sk, |
| 3019 | int flags, u32 if_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | { |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3021 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3022 | struct xfrm_dst *xdst; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3023 | struct dst_entry *dst, *route; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3024 | u16 family = dst_orig->ops->family; |
Florian Westphal | aff669b | 2017-07-17 13:57:23 +0200 | [diff] [blame] | 3025 | u8 dir = XFRM_POLICY_OUT; |
Changli Gao | 4b02162 | 2010-04-27 21:20:22 +0000 | [diff] [blame] | 3026 | int i, err, num_pols, num_xfrms = 0, drop_pols = 0; |
Venkat Yekkirala | e0d1caa | 2006-07-24 23:29:07 -0700 | [diff] [blame] | 3027 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3028 | dst = NULL; |
| 3029 | xdst = NULL; |
| 3030 | route = NULL; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3031 | |
Eric Dumazet | bd5eb35 | 2015-12-07 08:53:17 -0800 | [diff] [blame] | 3032 | sk = sk_const_to_full_sk(sk); |
Thomas Graf | f7944fb | 2007-08-25 13:46:55 -0700 | [diff] [blame] | 3033 | if (sk && sk->sk_policy[XFRM_POLICY_OUT]) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3034 | num_pols = 1; |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3035 | pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family, |
| 3036 | if_id); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3037 | err = xfrm_expand_policies(fl, family, pols, |
| 3038 | &num_pols, &num_xfrms); |
| 3039 | if (err < 0) |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 3040 | goto dropdst; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3041 | |
| 3042 | if (num_pols) { |
| 3043 | if (num_xfrms <= 0) { |
| 3044 | drop_pols = num_pols; |
| 3045 | goto no_transform; |
| 3046 | } |
| 3047 | |
| 3048 | xdst = xfrm_resolve_and_create_bundle( |
| 3049 | pols, num_pols, fl, |
| 3050 | family, dst_orig); |
Steffen Klassert | 76a4201 | 2018-01-10 12:14:28 +0100 | [diff] [blame] | 3051 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3052 | if (IS_ERR(xdst)) { |
| 3053 | xfrm_pols_put(pols, num_pols); |
| 3054 | err = PTR_ERR(xdst); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 3055 | if (err == -EREMOTE) |
| 3056 | goto nopol; |
| 3057 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3058 | goto dropdst; |
Timo Teräs | d809ec8 | 2010-07-12 21:29:42 +0000 | [diff] [blame] | 3059 | } else if (xdst == NULL) { |
| 3060 | num_xfrms = 0; |
| 3061 | drop_pols = num_pols; |
| 3062 | goto no_transform; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3065 | route = xdst->route; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3066 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 3067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3069 | if (xdst == NULL) { |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 3070 | struct xfrm_flo xflo; |
| 3071 | |
| 3072 | xflo.dst_orig = dst_orig; |
| 3073 | xflo.flags = flags; |
| 3074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | /* To accelerate a bit... */ |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 3076 | if ((dst_orig->flags & DST_NOXFRM) || |
Alexey Dobriyan | 52479b6 | 2008-11-25 17:35:18 -0800 | [diff] [blame] | 3077 | !net->xfrm.policy_count[XFRM_POLICY_OUT]) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 3078 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3080 | xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id); |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 3081 | if (xdst == NULL) |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3082 | goto nopol; |
Florian Westphal | bd45c53 | 2017-07-17 13:57:25 +0200 | [diff] [blame] | 3083 | if (IS_ERR(xdst)) { |
| 3084 | err = PTR_ERR(xdst); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 3085 | goto dropdst; |
Masahide NAKAMURA | d66e37a | 2008-01-07 21:46:15 -0800 | [diff] [blame] | 3086 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3087 | |
| 3088 | num_pols = xdst->num_pols; |
| 3089 | num_xfrms = xdst->num_xfrms; |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 3090 | memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3091 | route = xdst->route; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3094 | dst = &xdst->u.dst; |
| 3095 | if (route == NULL && num_xfrms > 0) { |
| 3096 | /* The only case when xfrm_bundle_lookup() returns a |
| 3097 | * bundle with null route, is when the template could |
| 3098 | * not be resolved. It means policies are there, but |
| 3099 | * bundle could not be created, since we don't yet |
| 3100 | * have the xfrm_state's. We need to wait for KM to |
| 3101 | * negotiate new SA's or bail out with error.*/ |
| 3102 | if (net->xfrm.sysctl_larval_drop) { |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3103 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 3104 | err = -EREMOTE; |
| 3105 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3106 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3107 | |
Steffen Klassert | 5b8ef34 | 2013-08-27 13:43:30 +0200 | [diff] [blame] | 3108 | err = -EAGAIN; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3109 | |
| 3110 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES); |
| 3111 | goto error; |
| 3112 | } |
| 3113 | |
| 3114 | no_transform: |
| 3115 | if (num_pols == 0) |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 3116 | goto nopol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3118 | if ((flags & XFRM_LOOKUP_ICMP) && |
| 3119 | !(pols[0]->flags & XFRM_POLICY_ICMP)) { |
| 3120 | err = -ENOENT; |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 3121 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3122 | } |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 3123 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3124 | for (i = 0; i < num_pols; i++) |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 3125 | pols[i]->curlft.use_time = ktime_get_real_seconds(); |
Herbert Xu | 8b7817f | 2007-12-12 10:44:43 -0800 | [diff] [blame] | 3126 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3127 | if (num_xfrms < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | /* Prohibit the flow */ |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3129 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK); |
Patrick McHardy | e104411b | 2005-09-08 15:11:55 -0700 | [diff] [blame] | 3130 | err = -EPERM; |
| 3131 | goto error; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3132 | } else if (num_xfrms > 0) { |
| 3133 | /* Flow transformed */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3134 | dst_release(dst_orig); |
| 3135 | } else { |
| 3136 | /* Flow passes untransformed */ |
| 3137 | dst_release(dst); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3138 | dst = dst_orig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3140 | ok: |
| 3141 | xfrm_pols_put(pols, drop_pols); |
Gao feng | 0c18337 | 2012-05-26 01:30:53 +0000 | [diff] [blame] | 3142 | if (dst && dst->xfrm && |
| 3143 | dst->xfrm->props.mode == XFRM_MODE_TUNNEL) |
| 3144 | dst->flags |= DST_XFRM_TUNNEL; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3145 | return dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3147 | nopol: |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3148 | if (!(flags & XFRM_LOOKUP_ICMP)) { |
| 3149 | dst = dst_orig; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3150 | goto ok; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3151 | } |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3152 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | error: |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3154 | dst_release(dst); |
Herbert Xu | 75b8c13 | 2007-12-11 04:38:08 -0800 | [diff] [blame] | 3155 | dropdst: |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 3156 | if (!(flags & XFRM_LOOKUP_KEEP_DST_REF)) |
| 3157 | dst_release(dst_orig); |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3158 | xfrm_pols_put(pols, drop_pols); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3159 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3161 | EXPORT_SYMBOL(xfrm_lookup_with_ifid); |
| 3162 | |
| 3163 | /* Main function: finds/creates a bundle for given flow. |
| 3164 | * |
| 3165 | * At the moment we eat a raw IP route. Mostly to speed up lookups |
| 3166 | * on interfaces with disabled IPsec. |
| 3167 | */ |
| 3168 | struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig, |
| 3169 | const struct flowi *fl, const struct sock *sk, |
| 3170 | int flags) |
| 3171 | { |
| 3172 | return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0); |
| 3173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | EXPORT_SYMBOL(xfrm_lookup); |
| 3175 | |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 3176 | /* Callers of xfrm_lookup_route() must ensure a call to dst_output(). |
| 3177 | * Otherwise we may send out blackholed packets. |
| 3178 | */ |
| 3179 | struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig, |
| 3180 | const struct flowi *fl, |
Eric Dumazet | 6f9c961 | 2015-09-25 07:39:10 -0700 | [diff] [blame] | 3181 | const struct sock *sk, int flags) |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 3182 | { |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 3183 | struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk, |
huaibin Wang | ac37e25 | 2015-02-11 18:10:36 +0100 | [diff] [blame] | 3184 | flags | XFRM_LOOKUP_QUEUE | |
| 3185 | XFRM_LOOKUP_KEEP_DST_REF); |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 3186 | |
| 3187 | if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE) |
| 3188 | return make_blackhole(net, dst_orig->ops->family, dst_orig); |
| 3189 | |
Tommi Rantala | 8cc8877 | 2018-06-21 09:30:47 +0300 | [diff] [blame] | 3190 | if (IS_ERR(dst)) |
| 3191 | dst_release(dst_orig); |
| 3192 | |
Steffen Klassert | f92ee61 | 2014-09-16 10:08:40 +0200 | [diff] [blame] | 3193 | return dst; |
| 3194 | } |
| 3195 | EXPORT_SYMBOL(xfrm_lookup_route); |
| 3196 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3197 | static inline int |
David S. Miller | 8f029de | 2011-02-22 17:59:59 -0800 | [diff] [blame] | 3198 | 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] | 3199 | { |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3200 | struct sec_path *sp = skb_sec_path(skb); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3201 | struct xfrm_state *x; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3202 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3203 | if (!sp || idx < 0 || idx >= sp->len) |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3204 | return 0; |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3205 | x = sp->xvec[idx]; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3206 | if (!x->type->reject) |
| 3207 | return 0; |
Herbert Xu | 1ecafed | 2007-10-09 13:24:07 -0700 | [diff] [blame] | 3208 | return x->type->reject(x, skb, fl); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3209 | } |
| 3210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | /* When skb is transformed back to its "native" form, we have to |
| 3212 | * check policy restrictions. At the moment we make this in maximally |
| 3213 | * stupid way. Shame on me. :-) Of course, connected sockets must |
| 3214 | * have policy cached at them. |
| 3215 | */ |
| 3216 | |
| 3217 | static inline int |
David S. Miller | 7db454b | 2011-02-24 01:43:01 -0500 | [diff] [blame] | 3218 | 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] | 3219 | unsigned short family) |
| 3220 | { |
| 3221 | if (xfrm_state_kern(x)) |
Kazunori MIYAZAWA | 928ba41 | 2007-02-13 12:57:16 -0800 | [diff] [blame] | 3222 | return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | return x->id.proto == tmpl->id.proto && |
| 3224 | (x->id.spi == tmpl->id.spi || !tmpl->id.spi) && |
| 3225 | (x->props.reqid == tmpl->reqid || !tmpl->reqid) && |
| 3226 | x->props.mode == tmpl->mode && |
Herbert Xu | c5d18e9 | 2008-04-22 00:46:42 -0700 | [diff] [blame] | 3227 | (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) || |
Masahide NAKAMURA | f3bd484 | 2006-08-23 18:00:48 -0700 | [diff] [blame] | 3228 | !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) && |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 3229 | !(x->props.mode != XFRM_MODE_TRANSPORT && |
| 3230 | xfrm_state_addr_cmp(tmpl, x, family)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | } |
| 3232 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3233 | /* |
| 3234 | * 0 or more than 0 is returned when validation is succeeded (either bypass |
| 3235 | * because of optional transport mode, or next index of the mathced secpath |
| 3236 | * state with the template. |
| 3237 | * -1 is returned when no matching template is found. |
| 3238 | * Otherwise "-2 - errored_index" is returned. |
| 3239 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | static inline int |
David S. Miller | 22cccb7 | 2011-02-24 01:43:33 -0500 | [diff] [blame] | 3241 | 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] | 3242 | unsigned short family) |
| 3243 | { |
| 3244 | int idx = start; |
| 3245 | |
| 3246 | if (tmpl->optional) { |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 3247 | if (tmpl->mode == XFRM_MODE_TRANSPORT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | return start; |
| 3249 | } else |
| 3250 | start = -1; |
| 3251 | for (; idx < sp->len; idx++) { |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 3252 | if (xfrm_state_ok(tmpl, sp->xvec[idx], family)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | return ++idx; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3254 | if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) { |
| 3255 | if (start == -1) |
| 3256 | start = -2-idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | break; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | } |
| 3260 | return start; |
| 3261 | } |
| 3262 | |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3263 | static void |
| 3264 | decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse) |
| 3265 | { |
| 3266 | const struct iphdr *iph = ip_hdr(skb); |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3267 | int ihl = iph->ihl; |
| 3268 | u8 *xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3269 | struct flowi4 *fl4 = &fl->u.ip4; |
| 3270 | int oif = 0; |
| 3271 | |
| 3272 | if (skb_dst(skb)) |
| 3273 | oif = skb_dst(skb)->dev->ifindex; |
| 3274 | |
| 3275 | memset(fl4, 0, sizeof(struct flowi4)); |
| 3276 | fl4->flowi4_mark = skb->mark; |
| 3277 | fl4->flowi4_oif = reverse ? skb->skb_iif : oif; |
| 3278 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3279 | fl4->flowi4_proto = iph->protocol; |
| 3280 | fl4->daddr = reverse ? iph->saddr : iph->daddr; |
| 3281 | fl4->saddr = reverse ? iph->daddr : iph->saddr; |
| 3282 | fl4->flowi4_tos = iph->tos; |
| 3283 | |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3284 | if (!ip_is_fragment(iph)) { |
| 3285 | switch (iph->protocol) { |
| 3286 | case IPPROTO_UDP: |
| 3287 | case IPPROTO_UDPLITE: |
| 3288 | case IPPROTO_TCP: |
| 3289 | case IPPROTO_SCTP: |
| 3290 | case IPPROTO_DCCP: |
| 3291 | if (xprth + 4 < skb->data || |
| 3292 | pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 3293 | __be16 *ports; |
| 3294 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3295 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3296 | ports = (__be16 *)xprth; |
| 3297 | |
| 3298 | fl4->fl4_sport = ports[!!reverse]; |
| 3299 | fl4->fl4_dport = ports[!reverse]; |
| 3300 | } |
| 3301 | break; |
| 3302 | case IPPROTO_ICMP: |
| 3303 | if (xprth + 2 < skb->data || |
| 3304 | pskb_may_pull(skb, xprth + 2 - skb->data)) { |
| 3305 | u8 *icmp; |
| 3306 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3307 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3308 | icmp = xprth; |
| 3309 | |
| 3310 | fl4->fl4_icmp_type = icmp[0]; |
| 3311 | fl4->fl4_icmp_code = icmp[1]; |
| 3312 | } |
| 3313 | break; |
| 3314 | case IPPROTO_ESP: |
| 3315 | if (xprth + 4 < skb->data || |
| 3316 | pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 3317 | __be32 *ehdr; |
| 3318 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3319 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3320 | ehdr = (__be32 *)xprth; |
| 3321 | |
| 3322 | fl4->fl4_ipsec_spi = ehdr[0]; |
| 3323 | } |
| 3324 | break; |
| 3325 | case IPPROTO_AH: |
| 3326 | if (xprth + 8 < skb->data || |
| 3327 | pskb_may_pull(skb, xprth + 8 - skb->data)) { |
| 3328 | __be32 *ah_hdr; |
| 3329 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3330 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3331 | ah_hdr = (__be32 *)xprth; |
| 3332 | |
| 3333 | fl4->fl4_ipsec_spi = ah_hdr[1]; |
| 3334 | } |
| 3335 | break; |
| 3336 | case IPPROTO_COMP: |
| 3337 | if (xprth + 4 < skb->data || |
| 3338 | pskb_may_pull(skb, xprth + 4 - skb->data)) { |
| 3339 | __be16 *ipcomp_hdr; |
| 3340 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3341 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3342 | ipcomp_hdr = (__be16 *)xprth; |
| 3343 | |
| 3344 | fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1])); |
| 3345 | } |
| 3346 | break; |
| 3347 | case IPPROTO_GRE: |
| 3348 | if (xprth + 12 < skb->data || |
| 3349 | pskb_may_pull(skb, xprth + 12 - skb->data)) { |
| 3350 | __be16 *greflags; |
| 3351 | __be32 *gre_hdr; |
| 3352 | |
Florian Westphal | 858e540 | 2019-05-16 11:28:16 +0200 | [diff] [blame^] | 3353 | xprth = skb_network_header(skb) + ihl * 4; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3354 | greflags = (__be16 *)xprth; |
| 3355 | gre_hdr = (__be32 *)xprth; |
| 3356 | |
| 3357 | if (greflags[0] & GRE_KEY) { |
| 3358 | if (greflags[0] & GRE_CSUM) |
| 3359 | gre_hdr++; |
| 3360 | fl4->fl4_gre_key = gre_hdr[1]; |
| 3361 | } |
| 3362 | } |
| 3363 | break; |
| 3364 | default: |
| 3365 | fl4->fl4_ipsec_spi = 0; |
| 3366 | break; |
| 3367 | } |
| 3368 | } |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | #if IS_ENABLED(CONFIG_IPV6) |
| 3372 | static void |
| 3373 | decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse) |
| 3374 | { |
| 3375 | struct flowi6 *fl6 = &fl->u.ip6; |
| 3376 | int onlyproto = 0; |
| 3377 | const struct ipv6hdr *hdr = ipv6_hdr(skb); |
| 3378 | u32 offset = sizeof(*hdr); |
| 3379 | struct ipv6_opt_hdr *exthdr; |
| 3380 | const unsigned char *nh = skb_network_header(skb); |
| 3381 | u16 nhoff = IP6CB(skb)->nhoff; |
| 3382 | int oif = 0; |
| 3383 | u8 nexthdr; |
| 3384 | |
| 3385 | if (!nhoff) |
| 3386 | nhoff = offsetof(struct ipv6hdr, nexthdr); |
| 3387 | |
| 3388 | nexthdr = nh[nhoff]; |
| 3389 | |
| 3390 | if (skb_dst(skb)) |
| 3391 | oif = skb_dst(skb)->dev->ifindex; |
| 3392 | |
| 3393 | memset(fl6, 0, sizeof(struct flowi6)); |
| 3394 | fl6->flowi6_mark = skb->mark; |
| 3395 | fl6->flowi6_oif = reverse ? skb->skb_iif : oif; |
| 3396 | |
| 3397 | fl6->daddr = reverse ? hdr->saddr : hdr->daddr; |
| 3398 | fl6->saddr = reverse ? hdr->daddr : hdr->saddr; |
| 3399 | |
| 3400 | while (nh + offset + sizeof(*exthdr) < skb->data || |
| 3401 | pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) { |
| 3402 | nh = skb_network_header(skb); |
| 3403 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
| 3404 | |
| 3405 | switch (nexthdr) { |
| 3406 | case NEXTHDR_FRAGMENT: |
| 3407 | onlyproto = 1; |
| 3408 | /* fall through */ |
| 3409 | case NEXTHDR_ROUTING: |
| 3410 | case NEXTHDR_HOP: |
| 3411 | case NEXTHDR_DEST: |
| 3412 | offset += ipv6_optlen(exthdr); |
| 3413 | nexthdr = exthdr->nexthdr; |
| 3414 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
| 3415 | break; |
| 3416 | case IPPROTO_UDP: |
| 3417 | case IPPROTO_UDPLITE: |
| 3418 | case IPPROTO_TCP: |
| 3419 | case IPPROTO_SCTP: |
| 3420 | case IPPROTO_DCCP: |
| 3421 | if (!onlyproto && (nh + offset + 4 < skb->data || |
| 3422 | pskb_may_pull(skb, nh + offset + 4 - skb->data))) { |
| 3423 | __be16 *ports; |
| 3424 | |
| 3425 | nh = skb_network_header(skb); |
| 3426 | ports = (__be16 *)(nh + offset); |
| 3427 | fl6->fl6_sport = ports[!!reverse]; |
| 3428 | fl6->fl6_dport = ports[!reverse]; |
| 3429 | } |
| 3430 | fl6->flowi6_proto = nexthdr; |
| 3431 | return; |
| 3432 | case IPPROTO_ICMPV6: |
| 3433 | if (!onlyproto && (nh + offset + 2 < skb->data || |
| 3434 | pskb_may_pull(skb, nh + offset + 2 - skb->data))) { |
| 3435 | u8 *icmp; |
| 3436 | |
| 3437 | nh = skb_network_header(skb); |
| 3438 | icmp = (u8 *)(nh + offset); |
| 3439 | fl6->fl6_icmp_type = icmp[0]; |
| 3440 | fl6->fl6_icmp_code = icmp[1]; |
| 3441 | } |
| 3442 | fl6->flowi6_proto = nexthdr; |
| 3443 | return; |
| 3444 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 3445 | case IPPROTO_MH: |
| 3446 | offset += ipv6_optlen(exthdr); |
| 3447 | if (!onlyproto && (nh + offset + 3 < skb->data || |
| 3448 | pskb_may_pull(skb, nh + offset + 3 - skb->data))) { |
| 3449 | struct ip6_mh *mh; |
| 3450 | |
| 3451 | nh = skb_network_header(skb); |
| 3452 | mh = (struct ip6_mh *)(nh + offset); |
| 3453 | fl6->fl6_mh_type = mh->ip6mh_type; |
| 3454 | } |
| 3455 | fl6->flowi6_proto = nexthdr; |
| 3456 | return; |
| 3457 | #endif |
| 3458 | /* XXX Why are there these headers? */ |
| 3459 | case IPPROTO_AH: |
| 3460 | case IPPROTO_ESP: |
| 3461 | case IPPROTO_COMP: |
| 3462 | default: |
| 3463 | fl6->fl6_ipsec_spi = 0; |
| 3464 | fl6->flowi6_proto = nexthdr; |
| 3465 | return; |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | #endif |
| 3470 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 3471 | int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, |
| 3472 | unsigned int family, int reverse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | { |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3474 | switch (family) { |
| 3475 | case AF_INET: |
| 3476 | decode_session4(skb, fl, reverse); |
| 3477 | break; |
| 3478 | #if IS_ENABLED(CONFIG_IPV6) |
| 3479 | case AF_INET6: |
| 3480 | decode_session6(skb, fl, reverse); |
| 3481 | break; |
| 3482 | #endif |
| 3483 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | return -EAFNOSUPPORT; |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | |
Florian Westphal | c53ac41 | 2019-04-16 16:44:39 +0200 | [diff] [blame] | 3487 | return security_xfrm_decode_session(skb, &fl->flowi_secid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | } |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 3489 | EXPORT_SYMBOL(__xfrm_decode_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | |
David S. Miller | 9a7386e | 2011-02-24 01:44:12 -0500 | [diff] [blame] | 3491 | 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] | 3492 | { |
| 3493 | for (; k < sp->len; k++) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3494 | if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 3495 | *idxp = k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | return 1; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3497 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | return 0; |
| 3501 | } |
| 3502 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 3503 | 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] | 3504 | unsigned short family) |
| 3505 | { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 3506 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | struct xfrm_policy *pol; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3508 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
| 3509 | int npols = 0; |
| 3510 | int xfrm_nr; |
| 3511 | int pi; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 3512 | int reverse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | struct flowi fl; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3514 | int xerr_idx = -1; |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3515 | const struct xfrm_if_cb *ifcb; |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3516 | struct sec_path *sp; |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3517 | struct xfrm_if *xi; |
| 3518 | u32 if_id = 0; |
| 3519 | |
| 3520 | rcu_read_lock(); |
| 3521 | ifcb = xfrm_if_get_cb(); |
| 3522 | |
| 3523 | if (ifcb) { |
Martin Willi | 025c65e | 2019-03-26 13:20:43 +0100 | [diff] [blame] | 3524 | xi = ifcb->decode_session(skb, family); |
Tobias Brunner | 660899d | 2019-02-18 10:49:39 +0100 | [diff] [blame] | 3525 | if (xi) { |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3526 | if_id = xi->p.if_id; |
Tobias Brunner | 660899d | 2019-02-18 10:49:39 +0100 | [diff] [blame] | 3527 | net = xi->net; |
| 3528 | } |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3529 | } |
| 3530 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 3532 | reverse = dir & ~XFRM_POLICY_MASK; |
| 3533 | dir &= XFRM_POLICY_MASK; |
Herbert Xu | d5422ef | 2007-12-12 10:44:16 -0800 | [diff] [blame] | 3534 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3535 | if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3536 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3538 | } |
| 3539 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 3540 | nf_nat_decode_session(skb, &fl, family); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | |
| 3542 | /* First, check used SA against their selectors. */ |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3543 | sp = skb_sec_path(skb); |
| 3544 | if (sp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | int i; |
| 3546 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3547 | for (i = sp->len - 1; i >= 0; i--) { |
| 3548 | struct xfrm_state *x = sp->xvec[i]; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3549 | if (!xfrm_selector_match(&x->sel, &fl, family)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3550 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | pol = NULL; |
Eric Dumazet | bd5eb35 | 2015-12-07 08:53:17 -0800 | [diff] [blame] | 3557 | sk = sk_to_full_sk(sk); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 3558 | if (sk && sk->sk_policy[dir]) { |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3559 | pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id); |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3560 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3561 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 3562 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3563 | } |
Venkat Yekkirala | 3bccfbc | 2006-10-05 15:42:35 -0500 | [diff] [blame] | 3564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | |
Florian Westphal | 86dc8ee | 2017-07-17 13:57:24 +0200 | [diff] [blame] | 3566 | if (!pol) |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3567 | pol = xfrm_policy_lookup(net, &fl, family, dir, if_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3569 | if (IS_ERR(pol)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3570 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 3571 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3572 | } |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 3573 | |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3574 | if (!pol) { |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3575 | if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) { |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3576 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3577 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3578 | return 0; |
| 3579 | } |
| 3580 | return 1; |
| 3581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 3583 | pol->curlft.use_time = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3585 | pols[0] = pol; |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 3586 | npols++; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3587 | #ifdef CONFIG_XFRM_SUB_POLICY |
| 3588 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { |
Alexey Dobriyan | f6e1e25 | 2008-11-25 17:35:44 -0800 | [diff] [blame] | 3589 | pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3590 | &fl, family, |
Benedict Wong | bc56b33 | 2018-07-19 10:50:44 -0700 | [diff] [blame] | 3591 | XFRM_POLICY_IN, if_id); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3592 | if (pols[1]) { |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3593 | if (IS_ERR(pols[1])) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3594 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR); |
James Morris | 134b0fc | 2006-10-05 15:42:27 -0500 | [diff] [blame] | 3595 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3596 | } |
Arnd Bergmann | 386c568 | 2018-07-11 12:19:13 +0200 | [diff] [blame] | 3597 | pols[1]->curlft.use_time = ktime_get_real_seconds(); |
Weilong Chen | 02d0892 | 2013-12-24 09:43:48 +0800 | [diff] [blame] | 3598 | npols++; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | #endif |
| 3602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | if (pol->action == XFRM_POLICY_ALLOW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | static struct sec_path dummy; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3605 | struct xfrm_tmpl *tp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 3606 | struct xfrm_tmpl *stp[XFRM_MAX_DEPTH]; |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3607 | struct xfrm_tmpl **tpp = tp; |
| 3608 | int ti = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | int i, k; |
| 3610 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 3611 | sp = skb_sec_path(skb); |
| 3612 | if (!sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | sp = &dummy; |
| 3614 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3615 | for (pi = 0; pi < npols; pi++) { |
| 3616 | if (pols[pi] != pol && |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3617 | pols[pi]->action != XFRM_POLICY_ALLOW) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3618 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3619 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3620 | } |
| 3621 | if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3622 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3623 | goto reject_error; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3624 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3625 | for (i = 0; i < pols[pi]->xfrm_nr; i++) |
| 3626 | tpp[ti++] = &pols[pi]->xfrm_vec[i]; |
| 3627 | } |
| 3628 | xfrm_nr = ti; |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 3629 | if (npols > 1) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 3630 | xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net); |
Masahide NAKAMURA | 41a49cc | 2006-08-23 22:48:31 -0700 | [diff] [blame] | 3631 | tpp = stp; |
| 3632 | } |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | /* For each tunnel xfrm, find the first matching tmpl. |
| 3635 | * For each tmpl before that, find corresponding xfrm. |
| 3636 | * Order is _important_. Later we will implement |
| 3637 | * some barriers, but at the moment barriers |
| 3638 | * are implied between each two transformations. |
| 3639 | */ |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3640 | for (i = xfrm_nr-1, k = 0; i >= 0; i--) { |
| 3641 | k = xfrm_policy_ok(tpp[i], sp, k, family); |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3642 | if (k < 0) { |
James Morris | d1d9fac | 2006-09-01 00:32:12 -0700 | [diff] [blame] | 3643 | if (k < -1) |
| 3644 | /* "-2 - errored_index" returned */ |
| 3645 | xerr_idx = -(2+k); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3646 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | goto reject; |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | } |
| 3650 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3651 | if (secpath_has_nontransport(sp, k, &xerr_idx)) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3652 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | goto reject; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3656 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | return 1; |
| 3658 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3659 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | |
| 3661 | reject: |
Masahide NAKAMURA | df0ba92 | 2006-08-23 20:41:00 -0700 | [diff] [blame] | 3662 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
Masahide NAKAMURA | 4e81bb8 | 2006-08-23 22:43:30 -0700 | [diff] [blame] | 3663 | reject_error: |
| 3664 | xfrm_pols_put(pols, npols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | return 0; |
| 3666 | } |
| 3667 | EXPORT_SYMBOL(__xfrm_policy_check); |
| 3668 | |
| 3669 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family) |
| 3670 | { |
Alexey Dobriyan | 99a6665 | 2008-11-25 17:36:13 -0800 | [diff] [blame] | 3671 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | struct flowi fl; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3673 | struct dst_entry *dst; |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 3674 | int res = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3676 | if (xfrm_decode_session(skb, &fl, family) < 0) { |
jamal | 72032fd | 2010-02-18 03:35:07 +0000 | [diff] [blame] | 3677 | XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | return 0; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 3679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | |
Eric Dumazet | fafeeb6 | 2010-06-01 10:04:49 +0000 | [diff] [blame] | 3681 | skb_dst_force(skb); |
Steffen Klassert | 9e14379 | 2018-09-11 10:31:15 +0200 | [diff] [blame] | 3682 | if (!skb_dst(skb)) { |
| 3683 | XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR); |
| 3684 | return 0; |
| 3685 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3686 | |
Steffen Klassert | b8c203b | 2014-09-16 10:08:49 +0200 | [diff] [blame] | 3687 | 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] | 3688 | if (IS_ERR(dst)) { |
Eric Dumazet | 7313714 | 2011-03-15 15:26:43 -0700 | [diff] [blame] | 3689 | res = 0; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 3690 | dst = NULL; |
| 3691 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 3692 | skb_dst_set(skb, dst); |
| 3693 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3694 | } |
| 3695 | EXPORT_SYMBOL(__xfrm_route_forward); |
| 3696 | |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 3697 | /* Optimize later using cookies and generation ids. */ |
| 3698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie) |
| 3700 | { |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 3701 | /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete |
David S. Miller | f5b0a87 | 2012-07-19 12:31:33 -0700 | [diff] [blame] | 3702 | * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to |
| 3703 | * get validated by dst_ops->check on every use. We do this |
| 3704 | * because when a normal route referenced by an XFRM dst is |
| 3705 | * obsoleted we do not go looking around for all parent |
| 3706 | * referencing XFRM dsts so that we can invalidate them. It |
| 3707 | * is just too much work. Instead we make the checks here on |
| 3708 | * every use. For example: |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 3709 | * |
| 3710 | * XFRM dst A --> IPv4 dst X |
| 3711 | * |
| 3712 | * X is the "xdst->route" of A (X is also the "dst->path" of A |
| 3713 | * in this example). If X is marked obsolete, "A" will not |
| 3714 | * notice. That's what we are validating here via the |
| 3715 | * stale_bundle() check. |
| 3716 | * |
Wei Wang | 52df157 | 2017-06-17 10:42:38 -0700 | [diff] [blame] | 3717 | * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will |
| 3718 | * be marked on it. |
Florian Westphal | 09c7570 | 2017-07-17 13:57:26 +0200 | [diff] [blame] | 3719 | * This will force stale_bundle() to fail on any xdst bundle with |
Wei Wang | 52df157 | 2017-06-17 10:42:38 -0700 | [diff] [blame] | 3720 | * this dst linked in it. |
David S. Miller | 399c180 | 2005-12-19 14:23:23 -0800 | [diff] [blame] | 3721 | */ |
David S. Miller | d49c73c | 2006-08-13 18:55:53 -0700 | [diff] [blame] | 3722 | if (dst->obsolete < 0 && !stale_bundle(dst)) |
| 3723 | return dst; |
| 3724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | return NULL; |
| 3726 | } |
| 3727 | |
| 3728 | static int stale_bundle(struct dst_entry *dst) |
| 3729 | { |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 3730 | return !xfrm_bundle_ok((struct xfrm_dst *)dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 3733 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | { |
David Miller | b92cf4a | 2017-11-28 15:40:22 -0500 | [diff] [blame] | 3735 | while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 3736 | dst->dev = dev_net(dev)->loopback_dev; |
Daniel Lezcano | de3cb74 | 2007-09-25 19:16:28 -0700 | [diff] [blame] | 3737 | dev_hold(dst->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | dev_put(dev); |
| 3739 | } |
| 3740 | } |
Herbert Xu | aabc976 | 2005-05-03 16:27:10 -0700 | [diff] [blame] | 3741 | EXPORT_SYMBOL(xfrm_dst_ifdown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | |
| 3743 | static void xfrm_link_failure(struct sk_buff *skb) |
| 3744 | { |
| 3745 | /* Impossible. Such dst must be popped before reaches point of failure. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst) |
| 3749 | { |
| 3750 | if (dst) { |
| 3751 | if (dst->obsolete) { |
| 3752 | dst_release(dst); |
| 3753 | dst = NULL; |
| 3754 | } |
| 3755 | } |
| 3756 | return dst; |
| 3757 | } |
| 3758 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3759 | static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | { |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3761 | while (nr--) { |
| 3762 | struct xfrm_dst *xdst = bundle[nr]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | u32 pmtu, route_mtu_cached; |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3764 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3766 | dst = &xdst->u.dst; |
David Miller | b92cf4a | 2017-11-28 15:40:22 -0500 | [diff] [blame] | 3767 | pmtu = dst_mtu(xfrm_dst_child(dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | xdst->child_mtu_cached = pmtu; |
| 3769 | |
| 3770 | pmtu = xfrm_state_mtu(dst->xfrm, pmtu); |
| 3771 | |
| 3772 | route_mtu_cached = dst_mtu(xdst->route); |
| 3773 | xdst->route_mtu_cached = route_mtu_cached; |
| 3774 | |
| 3775 | if (pmtu > route_mtu_cached) |
| 3776 | pmtu = route_mtu_cached; |
| 3777 | |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 3778 | dst_metric_set(dst, RTAX_MTU, pmtu); |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3782 | /* Check that the bundle accepts the flow and its components are |
| 3783 | * still valid. |
| 3784 | */ |
| 3785 | |
Steffen Klassert | 12fdb4d | 2011-06-29 23:18:20 +0000 | [diff] [blame] | 3786 | static int xfrm_bundle_ok(struct xfrm_dst *first) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | { |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3788 | struct xfrm_dst *bundle[XFRM_MAX_DEPTH]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | struct dst_entry *dst = &first->u.dst; |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3790 | struct xfrm_dst *xdst; |
| 3791 | int start_from, nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | u32 mtu; |
| 3793 | |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3794 | if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | (dst->dev && !netif_running(dst->dev))) |
| 3796 | return 0; |
| 3797 | |
Steffen Klassert | a0073fe | 2013-02-05 12:52:55 +0100 | [diff] [blame] | 3798 | if (dst->flags & DST_XFRM_QUEUE) |
| 3799 | return 1; |
| 3800 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3801 | start_from = nr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | do { |
| 3803 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 3804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | if (dst->xfrm->km.state != XFRM_STATE_VALID) |
| 3806 | return 0; |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 3807 | if (xdst->xfrm_genid != dst->xfrm->genid) |
| 3808 | return 0; |
Timo Teräs | b1312c8 | 2010-06-24 14:35:00 -0700 | [diff] [blame] | 3809 | if (xdst->num_pols > 0 && |
| 3810 | xdst->policy_genid != atomic_read(&xdst->pols[0]->genid)) |
David S. Miller | 9d4a706 | 2006-08-24 03:18:09 -0700 | [diff] [blame] | 3811 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3813 | bundle[nr++] = xdst; |
| 3814 | |
David Miller | b92cf4a | 2017-11-28 15:40:22 -0500 | [diff] [blame] | 3815 | mtu = dst_mtu(xfrm_dst_child(dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | if (xdst->child_mtu_cached != mtu) { |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3817 | start_from = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | xdst->child_mtu_cached = mtu; |
| 3819 | } |
| 3820 | |
Hideaki YOSHIFUJI | 92d63de | 2005-05-26 12:58:04 -0700 | [diff] [blame] | 3821 | if (!dst_check(xdst->route, xdst->route_cookie)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | return 0; |
| 3823 | mtu = dst_mtu(xdst->route); |
| 3824 | if (xdst->route_mtu_cached != mtu) { |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3825 | start_from = nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | xdst->route_mtu_cached = mtu; |
| 3827 | } |
| 3828 | |
David Miller | b92cf4a | 2017-11-28 15:40:22 -0500 | [diff] [blame] | 3829 | dst = xfrm_dst_child(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } while (dst->xfrm); |
| 3831 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3832 | if (likely(!start_from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | return 1; |
| 3834 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3835 | xdst = bundle[start_from - 1]; |
| 3836 | mtu = xdst->child_mtu_cached; |
| 3837 | while (start_from--) { |
| 3838 | dst = &xdst->u.dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | |
| 3840 | mtu = xfrm_state_mtu(dst->xfrm, mtu); |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3841 | if (mtu > xdst->route_mtu_cached) |
| 3842 | mtu = xdst->route_mtu_cached; |
David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 3843 | dst_metric_set(dst, RTAX_MTU, mtu); |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3844 | if (!start_from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | break; |
| 3846 | |
David Miller | 5492093 | 2017-11-28 15:41:01 -0500 | [diff] [blame] | 3847 | xdst = bundle[start_from - 1]; |
| 3848 | xdst->child_mtu_cached = mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | return 1; |
| 3852 | } |
| 3853 | |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3854 | static unsigned int xfrm_default_advmss(const struct dst_entry *dst) |
| 3855 | { |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3856 | return dst_metric_advmss(xfrm_dst_path(dst)); |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3857 | } |
| 3858 | |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 3859 | static unsigned int xfrm_mtu(const struct dst_entry *dst) |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3860 | { |
Steffen Klassert | 618f9bc | 2011-11-23 02:13:31 +0000 | [diff] [blame] | 3861 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
| 3862 | |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3863 | return mtu ? : dst_mtu(xfrm_dst_path(dst)); |
David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
Julian Anastasov | 1ecc9ad | 2017-02-25 17:57:43 +0200 | [diff] [blame] | 3866 | static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst, |
| 3867 | const void *daddr) |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 3868 | { |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3869 | while (dst->xfrm) { |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 3870 | const struct xfrm_state *xfrm = dst->xfrm; |
| 3871 | |
Steffen Klassert | 013cb81 | 2018-02-19 07:44:07 +0100 | [diff] [blame] | 3872 | dst = xfrm_dst_child(dst); |
| 3873 | |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 3874 | if (xfrm->props.mode == XFRM_MODE_TRANSPORT) |
| 3875 | continue; |
| 3876 | if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR) |
| 3877 | daddr = xfrm->coaddr; |
| 3878 | else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR)) |
| 3879 | daddr = &xfrm->id.daddr; |
| 3880 | } |
Julian Anastasov | 1ecc9ad | 2017-02-25 17:57:43 +0200 | [diff] [blame] | 3881 | return daddr; |
| 3882 | } |
| 3883 | |
| 3884 | static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst, |
| 3885 | struct sk_buff *skb, |
| 3886 | const void *daddr) |
| 3887 | { |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3888 | const struct dst_entry *path = xfrm_dst_path(dst); |
Julian Anastasov | 1ecc9ad | 2017-02-25 17:57:43 +0200 | [diff] [blame] | 3889 | |
| 3890 | if (!skb) |
| 3891 | daddr = xfrm_get_dst_nexthop(dst, daddr); |
| 3892 | return path->ops->neigh_lookup(path, skb, daddr); |
| 3893 | } |
| 3894 | |
| 3895 | static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr) |
| 3896 | { |
David Miller | 0f6c480 | 2017-11-28 15:40:46 -0500 | [diff] [blame] | 3897 | const struct dst_entry *path = xfrm_dst_path(dst); |
Julian Anastasov | 1ecc9ad | 2017-02-25 17:57:43 +0200 | [diff] [blame] | 3898 | |
| 3899 | daddr = xfrm_get_dst_nexthop(dst, daddr); |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 3900 | path->ops->confirm_neigh(path, daddr); |
| 3901 | } |
| 3902 | |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3903 | int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | { |
| 3905 | int err = 0; |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3906 | |
| 3907 | if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | return -EAFNOSUPPORT; |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3909 | |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 3910 | spin_lock(&xfrm_policy_afinfo_lock); |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3911 | if (unlikely(xfrm_policy_afinfo[family] != NULL)) |
Li RongQing | f31e8d4f | 2015-04-23 11:06:53 +0800 | [diff] [blame] | 3912 | err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | else { |
| 3914 | struct dst_ops *dst_ops = afinfo->dst_ops; |
| 3915 | if (likely(dst_ops->kmem_cachep == NULL)) |
| 3916 | dst_ops->kmem_cachep = xfrm_dst_cache; |
| 3917 | if (likely(dst_ops->check == NULL)) |
| 3918 | dst_ops->check = xfrm_dst_check; |
David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 3919 | if (likely(dst_ops->default_advmss == NULL)) |
| 3920 | dst_ops->default_advmss = xfrm_default_advmss; |
Steffen Klassert | ebb762f | 2011-11-23 02:12:51 +0000 | [diff] [blame] | 3921 | if (likely(dst_ops->mtu == NULL)) |
| 3922 | dst_ops->mtu = xfrm_mtu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | if (likely(dst_ops->negative_advice == NULL)) |
| 3924 | dst_ops->negative_advice = xfrm_negative_advice; |
| 3925 | if (likely(dst_ops->link_failure == NULL)) |
| 3926 | dst_ops->link_failure = xfrm_link_failure; |
David S. Miller | d3aaeb3 | 2011-07-18 00:40:17 -0700 | [diff] [blame] | 3927 | if (likely(dst_ops->neigh_lookup == NULL)) |
| 3928 | dst_ops->neigh_lookup = xfrm_neigh_lookup; |
Julian Anastasov | 63fca65 | 2017-02-06 23:14:15 +0200 | [diff] [blame] | 3929 | if (likely(!dst_ops->confirm_neigh)) |
| 3930 | dst_ops->confirm_neigh = xfrm_confirm_neigh; |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3931 | rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | } |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 3933 | spin_unlock(&xfrm_policy_afinfo_lock); |
Alexey Dobriyan | d7c7544 | 2010-01-24 22:47:53 -0800 | [diff] [blame] | 3934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | return err; |
| 3936 | } |
| 3937 | EXPORT_SYMBOL(xfrm_policy_register_afinfo); |
| 3938 | |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3939 | void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | { |
Florian Westphal | 2b61997 | 2017-02-07 15:00:15 +0100 | [diff] [blame] | 3941 | struct dst_ops *dst_ops = afinfo->dst_ops; |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3942 | int i; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 3943 | |
Florian Westphal | a2817d8 | 2017-02-07 15:00:17 +0100 | [diff] [blame] | 3944 | for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) { |
| 3945 | if (xfrm_policy_afinfo[i] != afinfo) |
| 3946 | continue; |
| 3947 | RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL); |
| 3948 | break; |
Eric Dumazet | ef8531b | 2012-08-19 12:31:48 +0200 | [diff] [blame] | 3949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | |
Florian Westphal | 2b61997 | 2017-02-07 15:00:15 +0100 | [diff] [blame] | 3951 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | |
Florian Westphal | 2b61997 | 2017-02-07 15:00:15 +0100 | [diff] [blame] | 3953 | dst_ops->kmem_cachep = NULL; |
| 3954 | dst_ops->check = NULL; |
| 3955 | dst_ops->negative_advice = NULL; |
| 3956 | dst_ops->link_failure = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | } |
| 3958 | EXPORT_SYMBOL(xfrm_policy_unregister_afinfo); |
| 3959 | |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 3960 | void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb) |
| 3961 | { |
| 3962 | spin_lock(&xfrm_if_cb_lock); |
| 3963 | rcu_assign_pointer(xfrm_if_cb, ifcb); |
| 3964 | spin_unlock(&xfrm_if_cb_lock); |
| 3965 | } |
| 3966 | EXPORT_SYMBOL(xfrm_if_register_cb); |
| 3967 | |
| 3968 | void xfrm_if_unregister_cb(void) |
| 3969 | { |
| 3970 | RCU_INIT_POINTER(xfrm_if_cb, NULL); |
| 3971 | synchronize_rcu(); |
| 3972 | } |
| 3973 | EXPORT_SYMBOL(xfrm_if_unregister_cb); |
| 3974 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 3975 | #ifdef CONFIG_XFRM_STATISTICS |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3976 | static int __net_init xfrm_statistics_init(struct net *net) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 3977 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 3978 | int rv; |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 3979 | net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib); |
| 3980 | if (!net->mib.xfrm_statistics) |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 3981 | return -ENOMEM; |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 3982 | rv = xfrm_proc_init(net); |
| 3983 | if (rv < 0) |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 3984 | free_percpu(net->mib.xfrm_statistics); |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 3985 | return rv; |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 3986 | } |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3987 | |
| 3988 | static void xfrm_statistics_fini(struct net *net) |
| 3989 | { |
Alexey Dobriyan | c68cd1a | 2008-11-25 18:00:14 -0800 | [diff] [blame] | 3990 | xfrm_proc_fini(net); |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 3991 | free_percpu(net->mib.xfrm_statistics); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 3992 | } |
| 3993 | #else |
| 3994 | static int __net_init xfrm_statistics_init(struct net *net) |
| 3995 | { |
| 3996 | return 0; |
| 3997 | } |
| 3998 | |
| 3999 | static void xfrm_statistics_fini(struct net *net) |
| 4000 | { |
| 4001 | } |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 4002 | #endif |
| 4003 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4004 | static int __net_init xfrm_policy_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4006 | unsigned int hmask, sz; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4007 | int dir, err; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4008 | |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4009 | if (net_eq(net, &init_net)) { |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4010 | xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | sizeof(struct xfrm_dst), |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 4012 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4013 | NULL); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4014 | err = rhashtable_init(&xfrm_policy_inexact_table, |
| 4015 | &xfrm_pol_inexact_params); |
| 4016 | BUG_ON(err); |
| 4017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4019 | hmask = 8 - 1; |
| 4020 | sz = (hmask+1) * sizeof(struct hlist_head); |
| 4021 | |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4022 | net->xfrm.policy_byidx = xfrm_hash_alloc(sz); |
| 4023 | if (!net->xfrm.policy_byidx) |
| 4024 | goto out_byidx; |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 4025 | net->xfrm.policy_idx_hmask = hmask; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4026 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 4027 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4028 | struct xfrm_policy_hash *htab; |
| 4029 | |
Alexey Dobriyan | dc2caba | 2008-11-25 17:24:15 -0800 | [diff] [blame] | 4030 | net->xfrm.policy_count[dir] = 0; |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 4031 | net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 4032 | INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4033 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4034 | htab = &net->xfrm.policy_bydst[dir]; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 4035 | htab->table = xfrm_hash_alloc(sz); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4036 | if (!htab->table) |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4037 | goto out_bydst; |
| 4038 | htab->hmask = hmask; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 4039 | htab->dbits4 = 32; |
| 4040 | htab->sbits4 = 32; |
| 4041 | htab->dbits6 = 128; |
| 4042 | htab->sbits6 = 128; |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4043 | } |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 4044 | net->xfrm.policy_hthresh.lbits4 = 32; |
| 4045 | net->xfrm.policy_hthresh.rbits4 = 32; |
| 4046 | net->xfrm.policy_hthresh.lbits6 = 128; |
| 4047 | net->xfrm.policy_hthresh.rbits6 = 128; |
| 4048 | |
| 4049 | seqlock_init(&net->xfrm.policy_hthresh.lock); |
David S. Miller | 2518c7c | 2006-08-24 04:45:07 -0700 | [diff] [blame] | 4050 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 4051 | INIT_LIST_HEAD(&net->xfrm.policy_all); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4052 | INIT_LIST_HEAD(&net->xfrm.inexact_bins); |
Alexey Dobriyan | 66caf62 | 2008-11-25 17:28:57 -0800 | [diff] [blame] | 4053 | INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize); |
Christophe Gouault | 880a6fa | 2014-08-29 16:16:05 +0200 | [diff] [blame] | 4054 | INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4055 | return 0; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4056 | |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4057 | out_bydst: |
| 4058 | for (dir--; dir >= 0; dir--) { |
| 4059 | struct xfrm_policy_hash *htab; |
| 4060 | |
| 4061 | htab = &net->xfrm.policy_bydst[dir]; |
| 4062 | xfrm_hash_free(htab->table, sz); |
| 4063 | } |
| 4064 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4065 | out_byidx: |
| 4066 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | } |
| 4068 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4069 | static void xfrm_policy_fini(struct net *net) |
| 4070 | { |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 4071 | struct xfrm_pol_inexact_bin *b, *t; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4072 | unsigned int sz; |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 4073 | int dir; |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4074 | |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 4075 | flush_work(&net->xfrm.policy_hash_work); |
| 4076 | #ifdef CONFIG_XFRM_SUB_POLICY |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4077 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 4078 | #endif |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4079 | xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false); |
Alexey Dobriyan | 7c2776e | 2008-11-25 17:57:44 -0800 | [diff] [blame] | 4080 | |
Alexey Dobriyan | adfcf0b | 2008-11-25 17:22:11 -0800 | [diff] [blame] | 4081 | WARN_ON(!list_empty(&net->xfrm.policy_all)); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4082 | |
Herbert Xu | 53c2e28 | 2014-11-13 17:09:49 +0800 | [diff] [blame] | 4083 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4084 | struct xfrm_policy_hash *htab; |
| 4085 | |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 4086 | WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir])); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4087 | |
| 4088 | htab = &net->xfrm.policy_bydst[dir]; |
Michal Kubecek | 5b653b2 | 2013-01-18 16:03:48 +0100 | [diff] [blame] | 4089 | sz = (htab->hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | a35f6c5 | 2008-11-25 17:23:48 -0800 | [diff] [blame] | 4090 | WARN_ON(!hlist_empty(htab->table)); |
| 4091 | xfrm_hash_free(htab->table, sz); |
Alexey Dobriyan | 8b18f8e | 2008-11-25 17:23:26 -0800 | [diff] [blame] | 4092 | } |
| 4093 | |
Alexey Dobriyan | 8100bea | 2008-11-25 17:22:58 -0800 | [diff] [blame] | 4094 | sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head); |
Alexey Dobriyan | 93b851c | 2008-11-25 17:22:35 -0800 | [diff] [blame] | 4095 | WARN_ON(!hlist_empty(net->xfrm.policy_byidx)); |
| 4096 | xfrm_hash_free(net->xfrm.policy_byidx, sz); |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4097 | |
Florian Westphal | 6be3b0d | 2018-11-07 23:00:37 +0100 | [diff] [blame] | 4098 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
| 4099 | list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins) |
| 4100 | __xfrm_policy_inexact_prune_bin(b, true); |
| 4101 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | static int __net_init xfrm_net_init(struct net *net) |
| 4105 | { |
| 4106 | int rv; |
| 4107 | |
Florian Westphal | c282222 | 2017-02-08 11:52:29 +0100 | [diff] [blame] | 4108 | /* Initialize the per-net locks here */ |
| 4109 | spin_lock_init(&net->xfrm.xfrm_state_lock); |
| 4110 | spin_lock_init(&net->xfrm.xfrm_policy_lock); |
| 4111 | mutex_init(&net->xfrm.xfrm_cfg_mutex); |
| 4112 | |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 4113 | rv = xfrm_statistics_init(net); |
| 4114 | if (rv < 0) |
| 4115 | goto out_statistics; |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4116 | rv = xfrm_state_init(net); |
| 4117 | if (rv < 0) |
| 4118 | goto out_state; |
| 4119 | rv = xfrm_policy_init(net); |
| 4120 | if (rv < 0) |
| 4121 | goto out_policy; |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 4122 | rv = xfrm_sysctl_init(net); |
| 4123 | if (rv < 0) |
| 4124 | goto out_sysctl; |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 4125 | |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4126 | return 0; |
| 4127 | |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 4128 | out_sysctl: |
| 4129 | xfrm_policy_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4130 | out_policy: |
| 4131 | xfrm_state_fini(net); |
| 4132 | out_state: |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 4133 | xfrm_statistics_fini(net); |
| 4134 | out_statistics: |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4135 | return rv; |
| 4136 | } |
| 4137 | |
| 4138 | static void __net_exit xfrm_net_exit(struct net *net) |
| 4139 | { |
Alexey Dobriyan | b27aead | 2008-11-25 18:00:48 -0800 | [diff] [blame] | 4140 | xfrm_sysctl_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4141 | xfrm_policy_fini(net); |
| 4142 | xfrm_state_fini(net); |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 4143 | xfrm_statistics_fini(net); |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | static struct pernet_operations __net_initdata xfrm_net_ops = { |
| 4147 | .init = xfrm_net_init, |
| 4148 | .exit = xfrm_net_exit, |
| 4149 | }; |
| 4150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | void __init xfrm_init(void) |
| 4152 | { |
Alexey Dobriyan | d62ddc2 | 2008-11-25 17:14:31 -0800 | [diff] [blame] | 4153 | register_pernet_subsys(&xfrm_net_ops); |
Kirill Tkhai | e9a441b | 2018-03-29 17:03:25 +0300 | [diff] [blame] | 4154 | xfrm_dev_init(); |
Florian Westphal | 3084609 | 2016-08-11 15:17:54 +0200 | [diff] [blame] | 4155 | seqcount_init(&xfrm_policy_hash_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | xfrm_input_init(); |
Steffen Klassert | f203b76 | 2018-06-12 14:07:12 +0200 | [diff] [blame] | 4157 | |
| 4158 | RCU_INIT_POINTER(xfrm_if_cb, NULL); |
| 4159 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | } |
| 4161 | |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4162 | #ifdef CONFIG_AUDITSYSCALL |
Ilpo Järvinen | 1486cbd7 | 2008-01-12 03:20:03 -0800 | [diff] [blame] | 4163 | static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp, |
| 4164 | struct audit_buffer *audit_buf) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4165 | { |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4166 | struct xfrm_sec_ctx *ctx = xp->security; |
| 4167 | struct xfrm_selector *sel = &xp->selector; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4168 | |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4169 | if (ctx) |
| 4170 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", |
| 4171 | ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); |
| 4172 | |
Weilong Chen | 9b7a787 | 2013-12-24 09:43:46 +0800 | [diff] [blame] | 4173 | switch (sel->family) { |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4174 | case AF_INET: |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 4175 | audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4176 | if (sel->prefixlen_s != 32) |
| 4177 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 4178 | sel->prefixlen_s); |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 4179 | audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4180 | if (sel->prefixlen_d != 32) |
| 4181 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 4182 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4183 | break; |
| 4184 | case AF_INET6: |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 4185 | audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4186 | if (sel->prefixlen_s != 128) |
| 4187 | audit_log_format(audit_buf, " src_prefixlen=%d", |
| 4188 | sel->prefixlen_s); |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 4189 | audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6); |
Paul Moore | 875179f | 2007-12-01 23:27:18 +1100 | [diff] [blame] | 4190 | if (sel->prefixlen_d != 128) |
| 4191 | audit_log_format(audit_buf, " dst_prefixlen=%d", |
| 4192 | sel->prefixlen_d); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4193 | break; |
| 4194 | } |
| 4195 | } |
| 4196 | |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4197 | 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] | 4198 | { |
| 4199 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4200 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 4201 | audit_buf = xfrm_audit_start("SPD-add"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4202 | if (audit_buf == NULL) |
| 4203 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4204 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 4205 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4206 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 4207 | audit_log_end(audit_buf); |
| 4208 | } |
| 4209 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_add); |
| 4210 | |
Paul Moore | 68277ac | 2007-12-20 20:49:33 -0800 | [diff] [blame] | 4211 | void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4212 | bool task_valid) |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4213 | { |
| 4214 | struct audit_buffer *audit_buf; |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4215 | |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 4216 | audit_buf = xfrm_audit_start("SPD-delete"); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4217 | if (audit_buf == NULL) |
| 4218 | return; |
Tetsuo Handa | 2e71029 | 2014-04-22 21:48:30 +0900 | [diff] [blame] | 4219 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 4220 | audit_log_format(audit_buf, " res=%u", result); |
Joy Latten | ab5f5e8 | 2007-09-17 11:51:22 -0700 | [diff] [blame] | 4221 | xfrm_audit_common_policyinfo(xp, audit_buf); |
| 4222 | audit_log_end(audit_buf); |
| 4223 | } |
| 4224 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete); |
| 4225 | #endif |
| 4226 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4227 | #ifdef CONFIG_XFRM_MIGRATE |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 4228 | static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp, |
| 4229 | const struct xfrm_selector *sel_tgt) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4230 | { |
| 4231 | if (sel_cmp->proto == IPSEC_ULPROTO_ANY) { |
| 4232 | if (sel_tgt->family == sel_cmp->family && |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 4233 | xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr, |
| 4234 | sel_cmp->family) && |
| 4235 | xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr, |
| 4236 | sel_cmp->family) && |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4237 | sel_tgt->prefixlen_d == sel_cmp->prefixlen_d && |
| 4238 | sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 4239 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4240 | } |
| 4241 | } else { |
| 4242 | if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) { |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 4243 | return true; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4244 | } |
| 4245 | } |
David S. Miller | bc9b35a | 2012-05-15 15:04:57 -0400 | [diff] [blame] | 4246 | return false; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4247 | } |
| 4248 | |
Weilong Chen | 3e94c2d | 2013-12-24 09:43:47 +0800 | [diff] [blame] | 4249 | static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel, |
| 4250 | u8 dir, u8 type, struct net *net) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4251 | { |
| 4252 | struct xfrm_policy *pol, *ret = NULL; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4253 | struct hlist_head *chain; |
| 4254 | u32 priority = ~0U; |
| 4255 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 4256 | spin_lock_bh(&net->xfrm.xfrm_policy_lock); |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 4257 | chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 4258 | hlist_for_each_entry(pol, chain, bydst) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4259 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
| 4260 | pol->type == type) { |
| 4261 | ret = pol; |
| 4262 | priority = ret->priority; |
| 4263 | break; |
| 4264 | } |
| 4265 | } |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 4266 | chain = &net->xfrm.policy_inexact[dir]; |
Florian Westphal | 24969fa | 2018-11-07 23:00:35 +0100 | [diff] [blame] | 4267 | hlist_for_each_entry(pol, chain, bydst_inexact_list) { |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 4268 | if ((pol->priority >= priority) && ret) |
| 4269 | break; |
| 4270 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4271 | if (xfrm_migrate_selector_match(sel, &pol->selector) && |
Li RongQing | 8faf491 | 2015-05-14 11:16:59 +0800 | [diff] [blame] | 4272 | pol->type == type) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4273 | ret = pol; |
| 4274 | break; |
| 4275 | } |
| 4276 | } |
| 4277 | |
Li RongQing | 586f2eb | 2015-04-30 17:13:41 +0800 | [diff] [blame] | 4278 | xfrm_pol_hold(ret); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4279 | |
Florian Westphal | 9d0380d | 2016-08-11 15:17:59 +0200 | [diff] [blame] | 4280 | spin_unlock_bh(&net->xfrm.xfrm_policy_lock); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4281 | |
| 4282 | return ret; |
| 4283 | } |
| 4284 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 4285 | 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] | 4286 | { |
| 4287 | int match = 0; |
| 4288 | |
| 4289 | if (t->mode == m->mode && t->id.proto == m->proto && |
| 4290 | (m->reqid == 0 || t->reqid == m->reqid)) { |
| 4291 | switch (t->mode) { |
| 4292 | case XFRM_MODE_TUNNEL: |
| 4293 | case XFRM_MODE_BEET: |
YOSHIFUJI Hideaki / 吉藤英明 | 70e94e6 | 2013-01-29 12:48:50 +0000 | [diff] [blame] | 4294 | if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr, |
| 4295 | m->old_family) && |
| 4296 | xfrm_addr_equal(&t->saddr, &m->old_saddr, |
| 4297 | m->old_family)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4298 | match = 1; |
| 4299 | } |
| 4300 | break; |
| 4301 | case XFRM_MODE_TRANSPORT: |
| 4302 | /* in case of transport mode, template does not store |
| 4303 | any IP addresses, hence we just compare mode and |
| 4304 | protocol */ |
| 4305 | match = 1; |
| 4306 | break; |
| 4307 | default: |
| 4308 | break; |
| 4309 | } |
| 4310 | } |
| 4311 | return match; |
| 4312 | } |
| 4313 | |
| 4314 | /* update endpoint address(es) of template(s) */ |
| 4315 | static int xfrm_policy_migrate(struct xfrm_policy *pol, |
| 4316 | struct xfrm_migrate *m, int num_migrate) |
| 4317 | { |
| 4318 | struct xfrm_migrate *mp; |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4319 | int i, j, n = 0; |
| 4320 | |
| 4321 | write_lock_bh(&pol->lock); |
Herbert Xu | 12a169e | 2008-10-01 07:03:24 -0700 | [diff] [blame] | 4322 | if (unlikely(pol->walk.dead)) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4323 | /* target policy has been deleted */ |
| 4324 | write_unlock_bh(&pol->lock); |
| 4325 | return -ENOENT; |
| 4326 | } |
| 4327 | |
| 4328 | for (i = 0; i < pol->xfrm_nr; i++) { |
| 4329 | for (j = 0, mp = m; j < num_migrate; j++, mp++) { |
| 4330 | if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i])) |
| 4331 | continue; |
| 4332 | n++; |
Herbert Xu | 1bfcb10 | 2007-10-17 21:31:50 -0700 | [diff] [blame] | 4333 | if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL && |
| 4334 | pol->xfrm_vec[i].mode != XFRM_MODE_BEET) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4335 | continue; |
| 4336 | /* update endpoints */ |
| 4337 | memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr, |
| 4338 | sizeof(pol->xfrm_vec[i].id.daddr)); |
| 4339 | memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr, |
| 4340 | sizeof(pol->xfrm_vec[i].saddr)); |
| 4341 | pol->xfrm_vec[i].encap_family = mp->new_family; |
| 4342 | /* flush bundles */ |
Timo Teräs | 80c802f | 2010-04-07 00:30:05 +0000 | [diff] [blame] | 4343 | atomic_inc(&pol->genid); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | write_unlock_bh(&pol->lock); |
| 4348 | |
| 4349 | if (!n) |
| 4350 | return -ENODATA; |
| 4351 | |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
David S. Miller | dd70175 | 2011-02-24 00:21:08 -0500 | [diff] [blame] | 4355 | 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] | 4356 | { |
| 4357 | int i, j; |
| 4358 | |
| 4359 | if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH) |
| 4360 | return -EINVAL; |
| 4361 | |
| 4362 | for (i = 0; i < num_migrate; i++) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4363 | if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) || |
| 4364 | xfrm_addr_any(&m[i].new_saddr, m[i].new_family)) |
| 4365 | return -EINVAL; |
| 4366 | |
| 4367 | /* check if there is any duplicated entry */ |
| 4368 | for (j = i + 1; j < num_migrate; j++) { |
| 4369 | if (!memcmp(&m[i].old_daddr, &m[j].old_daddr, |
| 4370 | sizeof(m[i].old_daddr)) && |
| 4371 | !memcmp(&m[i].old_saddr, &m[j].old_saddr, |
| 4372 | sizeof(m[i].old_saddr)) && |
| 4373 | m[i].proto == m[j].proto && |
| 4374 | m[i].mode == m[j].mode && |
| 4375 | m[i].reqid == m[j].reqid && |
| 4376 | m[i].old_family == m[j].old_family) |
| 4377 | return -EINVAL; |
| 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | return 0; |
| 4382 | } |
| 4383 | |
David S. Miller | b4b7c0b | 2011-02-24 00:35:06 -0500 | [diff] [blame] | 4384 | int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
Arnaud Ebalard | 13c1d18 | 2008-10-05 13:33:42 -0700 | [diff] [blame] | 4385 | struct xfrm_migrate *m, int num_migrate, |
Antony Antony | 4ab47d4 | 2017-06-06 12:12:13 +0200 | [diff] [blame] | 4386 | struct xfrm_kmaddress *k, struct net *net, |
| 4387 | struct xfrm_encap_tmpl *encap) |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4388 | { |
| 4389 | int i, err, nx_cur = 0, nx_new = 0; |
| 4390 | struct xfrm_policy *pol = NULL; |
| 4391 | struct xfrm_state *x, *xc; |
| 4392 | struct xfrm_state *x_cur[XFRM_MAX_DEPTH]; |
| 4393 | struct xfrm_state *x_new[XFRM_MAX_DEPTH]; |
| 4394 | struct xfrm_migrate *mp; |
| 4395 | |
Vladis Dronov | 7bab096 | 2017-08-02 19:50:14 +0200 | [diff] [blame] | 4396 | /* Stage 0 - sanity checks */ |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4397 | if ((err = xfrm_migrate_check(m, num_migrate)) < 0) |
| 4398 | goto out; |
| 4399 | |
Vladis Dronov | 7bab096 | 2017-08-02 19:50:14 +0200 | [diff] [blame] | 4400 | if (dir >= XFRM_POLICY_MAX) { |
| 4401 | err = -EINVAL; |
| 4402 | goto out; |
| 4403 | } |
| 4404 | |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4405 | /* Stage 1 - find policy */ |
Fan Du | 8d549c4 | 2013-11-07 17:47:49 +0800 | [diff] [blame] | 4406 | if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4407 | err = -ENOENT; |
| 4408 | goto out; |
| 4409 | } |
| 4410 | |
| 4411 | /* Stage 2 - find and update state(s) */ |
| 4412 | for (i = 0, mp = m; i < num_migrate; i++, mp++) { |
Fan Du | 283bc9f | 2013-11-07 17:47:50 +0800 | [diff] [blame] | 4413 | if ((x = xfrm_migrate_state_find(mp, net))) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4414 | x_cur[nx_cur] = x; |
| 4415 | nx_cur++; |
Antony Antony | 4ab47d4 | 2017-06-06 12:12:13 +0200 | [diff] [blame] | 4416 | xc = xfrm_state_migrate(x, mp, encap); |
| 4417 | if (xc) { |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4418 | x_new[nx_new] = xc; |
| 4419 | nx_new++; |
| 4420 | } else { |
| 4421 | err = -ENODATA; |
| 4422 | goto restore_state; |
| 4423 | } |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | /* Stage 3 - update policy */ |
| 4428 | if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0) |
| 4429 | goto restore_state; |
| 4430 | |
| 4431 | /* Stage 4 - delete old state(s) */ |
| 4432 | if (nx_cur) { |
| 4433 | xfrm_states_put(x_cur, nx_cur); |
| 4434 | xfrm_states_delete(x_cur, nx_cur); |
| 4435 | } |
| 4436 | |
| 4437 | /* Stage 5 - announce */ |
Antony Antony | 8bafd73 | 2017-06-06 12:12:14 +0200 | [diff] [blame] | 4438 | km_migrate(sel, dir, type, m, num_migrate, k, encap); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4439 | |
| 4440 | xfrm_pol_put(pol); |
| 4441 | |
| 4442 | return 0; |
| 4443 | out: |
| 4444 | return err; |
| 4445 | |
| 4446 | restore_state: |
| 4447 | if (pol) |
| 4448 | xfrm_pol_put(pol); |
| 4449 | if (nx_cur) |
| 4450 | xfrm_states_put(x_cur, nx_cur); |
| 4451 | if (nx_new) |
| 4452 | xfrm_states_delete(x_new, nx_new); |
| 4453 | |
| 4454 | return err; |
| 4455 | } |
David S. Miller | e610e67 | 2007-02-08 13:29:15 -0800 | [diff] [blame] | 4456 | EXPORT_SYMBOL(xfrm_migrate); |
Shinta Sugimoto | 80c9aba | 2007-02-08 13:11:42 -0800 | [diff] [blame] | 4457 | #endif |