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