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