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