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