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