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 | ||
1da177e4 LT |
16 | #include <linux/slab.h> |
17 | #include <linux/kmod.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/spinlock.h> | |
20 | #include <linux/workqueue.h> | |
21 | #include <linux/notifier.h> | |
22 | #include <linux/netdevice.h> | |
eb9c7ebe | 23 | #include <linux/netfilter.h> |
1da177e4 | 24 | #include <linux/module.h> |
2518c7c2 | 25 | #include <linux/cache.h> |
1da177e4 LT |
26 | #include <net/xfrm.h> |
27 | #include <net/ip.h> | |
28 | ||
44e36b42 DM |
29 | #include "xfrm_hash.h" |
30 | ||
aad0e0b9 | 31 | int sysctl_xfrm_larval_drop __read_mostly; |
14e50e57 | 32 | |
4a3e2f71 AV |
33 | DEFINE_MUTEX(xfrm_cfg_mutex); |
34 | EXPORT_SYMBOL(xfrm_cfg_mutex); | |
1da177e4 LT |
35 | |
36 | static DEFINE_RWLOCK(xfrm_policy_lock); | |
37 | ||
2518c7c2 DM |
38 | unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2]; |
39 | EXPORT_SYMBOL(xfrm_policy_count); | |
1da177e4 LT |
40 | |
41 | static DEFINE_RWLOCK(xfrm_policy_afinfo_lock); | |
42 | static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO]; | |
43 | ||
e18b890b | 44 | static struct kmem_cache *xfrm_dst_cache __read_mostly; |
1da177e4 LT |
45 | |
46 | static struct work_struct xfrm_policy_gc_work; | |
2518c7c2 | 47 | static HLIST_HEAD(xfrm_policy_gc_list); |
1da177e4 LT |
48 | static DEFINE_SPINLOCK(xfrm_policy_gc_lock); |
49 | ||
50 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family); | |
51 | static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo); | |
52 | ||
77681021 AM |
53 | static inline int |
54 | __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl) | |
55 | { | |
56 | return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) && | |
57 | addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) && | |
58 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && | |
59 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && | |
60 | (fl->proto == sel->proto || !sel->proto) && | |
61 | (fl->oif == sel->ifindex || !sel->ifindex); | |
62 | } | |
63 | ||
64 | static inline int | |
65 | __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl) | |
66 | { | |
67 | return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) && | |
68 | addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) && | |
69 | !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) && | |
70 | !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) && | |
71 | (fl->proto == sel->proto || !sel->proto) && | |
72 | (fl->oif == sel->ifindex || !sel->ifindex); | |
73 | } | |
74 | ||
75 | int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl, | |
76 | unsigned short family) | |
77 | { | |
78 | switch (family) { | |
79 | case AF_INET: | |
80 | return __xfrm4_selector_match(sel, fl); | |
81 | case AF_INET6: | |
82 | return __xfrm6_selector_match(sel, fl); | |
83 | } | |
84 | return 0; | |
85 | } | |
86 | ||
a716c119 | 87 | int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl, |
1da177e4 LT |
88 | unsigned short family) |
89 | { | |
90 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); | |
91 | int err = 0; | |
92 | ||
93 | if (unlikely(afinfo == NULL)) | |
94 | return -EAFNOSUPPORT; | |
95 | ||
96 | if (likely(afinfo->dst_lookup != NULL)) | |
97 | err = afinfo->dst_lookup(dst, fl); | |
98 | else | |
99 | err = -EINVAL; | |
100 | xfrm_policy_put_afinfo(afinfo); | |
101 | return err; | |
102 | } | |
103 | EXPORT_SYMBOL(xfrm_dst_lookup); | |
104 | ||
1da177e4 LT |
105 | static inline unsigned long make_jiffies(long secs) |
106 | { | |
107 | if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ) | |
108 | return MAX_SCHEDULE_TIMEOUT-1; | |
109 | else | |
a716c119 | 110 | return secs*HZ; |
1da177e4 LT |
111 | } |
112 | ||
113 | static void xfrm_policy_timer(unsigned long data) | |
114 | { | |
115 | struct xfrm_policy *xp = (struct xfrm_policy*)data; | |
9d729f72 | 116 | unsigned long now = get_seconds(); |
1da177e4 LT |
117 | long next = LONG_MAX; |
118 | int warn = 0; | |
119 | int dir; | |
120 | ||
121 | read_lock(&xp->lock); | |
122 | ||
123 | if (xp->dead) | |
124 | goto out; | |
125 | ||
77d8d7a6 | 126 | dir = xfrm_policy_id2dir(xp->index); |
1da177e4 LT |
127 | |
128 | if (xp->lft.hard_add_expires_seconds) { | |
129 | long tmo = xp->lft.hard_add_expires_seconds + | |
130 | xp->curlft.add_time - now; | |
131 | if (tmo <= 0) | |
132 | goto expired; | |
133 | if (tmo < next) | |
134 | next = tmo; | |
135 | } | |
136 | if (xp->lft.hard_use_expires_seconds) { | |
137 | long tmo = xp->lft.hard_use_expires_seconds + | |
138 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; | |
139 | if (tmo <= 0) | |
140 | goto expired; | |
141 | if (tmo < next) | |
142 | next = tmo; | |
143 | } | |
144 | if (xp->lft.soft_add_expires_seconds) { | |
145 | long tmo = xp->lft.soft_add_expires_seconds + | |
146 | xp->curlft.add_time - now; | |
147 | if (tmo <= 0) { | |
148 | warn = 1; | |
149 | tmo = XFRM_KM_TIMEOUT; | |
150 | } | |
151 | if (tmo < next) | |
152 | next = tmo; | |
153 | } | |
154 | if (xp->lft.soft_use_expires_seconds) { | |
155 | long tmo = xp->lft.soft_use_expires_seconds + | |
156 | (xp->curlft.use_time ? : xp->curlft.add_time) - now; | |
157 | if (tmo <= 0) { | |
158 | warn = 1; | |
159 | tmo = XFRM_KM_TIMEOUT; | |
160 | } | |
161 | if (tmo < next) | |
162 | next = tmo; | |
163 | } | |
164 | ||
165 | if (warn) | |
6c5c8ca7 | 166 | km_policy_expired(xp, dir, 0, 0); |
1da177e4 LT |
167 | if (next != LONG_MAX && |
168 | !mod_timer(&xp->timer, jiffies + make_jiffies(next))) | |
169 | xfrm_pol_hold(xp); | |
170 | ||
171 | out: | |
172 | read_unlock(&xp->lock); | |
173 | xfrm_pol_put(xp); | |
174 | return; | |
175 | ||
176 | expired: | |
177 | read_unlock(&xp->lock); | |
4666faab | 178 | if (!xfrm_policy_delete(xp, dir)) |
6c5c8ca7 | 179 | km_policy_expired(xp, dir, 1, 0); |
1da177e4 LT |
180 | xfrm_pol_put(xp); |
181 | } | |
182 | ||
183 | ||
184 | /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2 | |
185 | * SPD calls. | |
186 | */ | |
187 | ||
dd0fc66f | 188 | struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp) |
1da177e4 LT |
189 | { |
190 | struct xfrm_policy *policy; | |
191 | ||
0da974f4 | 192 | policy = kzalloc(sizeof(struct xfrm_policy), gfp); |
1da177e4 LT |
193 | |
194 | if (policy) { | |
2518c7c2 DM |
195 | INIT_HLIST_NODE(&policy->bydst); |
196 | INIT_HLIST_NODE(&policy->byidx); | |
1da177e4 | 197 | rwlock_init(&policy->lock); |
2518c7c2 | 198 | atomic_set(&policy->refcnt, 1); |
1da177e4 LT |
199 | init_timer(&policy->timer); |
200 | policy->timer.data = (unsigned long)policy; | |
201 | policy->timer.function = xfrm_policy_timer; | |
202 | } | |
203 | return policy; | |
204 | } | |
205 | EXPORT_SYMBOL(xfrm_policy_alloc); | |
206 | ||
207 | /* Destroy xfrm_policy: descendant resources must be released to this moment. */ | |
208 | ||
209 | void __xfrm_policy_destroy(struct xfrm_policy *policy) | |
210 | { | |
09a62660 | 211 | BUG_ON(!policy->dead); |
1da177e4 | 212 | |
09a62660 | 213 | BUG_ON(policy->bundles); |
1da177e4 LT |
214 | |
215 | if (del_timer(&policy->timer)) | |
216 | BUG(); | |
217 | ||
df71837d | 218 | security_xfrm_policy_free(policy); |
1da177e4 LT |
219 | kfree(policy); |
220 | } | |
221 | EXPORT_SYMBOL(__xfrm_policy_destroy); | |
222 | ||
223 | static void xfrm_policy_gc_kill(struct xfrm_policy *policy) | |
224 | { | |
225 | struct dst_entry *dst; | |
226 | ||
227 | while ((dst = policy->bundles) != NULL) { | |
228 | policy->bundles = dst->next; | |
229 | dst_free(dst); | |
230 | } | |
231 | ||
232 | if (del_timer(&policy->timer)) | |
233 | atomic_dec(&policy->refcnt); | |
234 | ||
235 | if (atomic_read(&policy->refcnt) > 1) | |
236 | flow_cache_flush(); | |
237 | ||
238 | xfrm_pol_put(policy); | |
239 | } | |
240 | ||
c4028958 | 241 | static void xfrm_policy_gc_task(struct work_struct *work) |
1da177e4 LT |
242 | { |
243 | struct xfrm_policy *policy; | |
2518c7c2 DM |
244 | struct hlist_node *entry, *tmp; |
245 | struct hlist_head gc_list; | |
1da177e4 LT |
246 | |
247 | spin_lock_bh(&xfrm_policy_gc_lock); | |
2518c7c2 DM |
248 | gc_list.first = xfrm_policy_gc_list.first; |
249 | INIT_HLIST_HEAD(&xfrm_policy_gc_list); | |
1da177e4 LT |
250 | spin_unlock_bh(&xfrm_policy_gc_lock); |
251 | ||
2518c7c2 | 252 | hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst) |
1da177e4 | 253 | xfrm_policy_gc_kill(policy); |
1da177e4 LT |
254 | } |
255 | ||
256 | /* Rule must be locked. Release descentant resources, announce | |
257 | * entry dead. The rule must be unlinked from lists to the moment. | |
258 | */ | |
259 | ||
260 | static void xfrm_policy_kill(struct xfrm_policy *policy) | |
261 | { | |
262 | int dead; | |
263 | ||
264 | write_lock_bh(&policy->lock); | |
265 | dead = policy->dead; | |
266 | policy->dead = 1; | |
267 | write_unlock_bh(&policy->lock); | |
268 | ||
269 | if (unlikely(dead)) { | |
270 | WARN_ON(1); | |
271 | return; | |
272 | } | |
273 | ||
274 | spin_lock(&xfrm_policy_gc_lock); | |
2518c7c2 | 275 | hlist_add_head(&policy->bydst, &xfrm_policy_gc_list); |
1da177e4 LT |
276 | spin_unlock(&xfrm_policy_gc_lock); |
277 | ||
278 | schedule_work(&xfrm_policy_gc_work); | |
279 | } | |
280 | ||
2518c7c2 DM |
281 | struct xfrm_policy_hash { |
282 | struct hlist_head *table; | |
283 | unsigned int hmask; | |
284 | }; | |
285 | ||
286 | static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2]; | |
287 | static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly; | |
288 | static struct hlist_head *xfrm_policy_byidx __read_mostly; | |
289 | static unsigned int xfrm_idx_hmask __read_mostly; | |
290 | static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024; | |
291 | ||
2518c7c2 DM |
292 | static inline unsigned int idx_hash(u32 index) |
293 | { | |
294 | return __idx_hash(index, xfrm_idx_hmask); | |
295 | } | |
296 | ||
2518c7c2 DM |
297 | static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir) |
298 | { | |
299 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; | |
300 | unsigned int hash = __sel_hash(sel, family, hmask); | |
301 | ||
302 | return (hash == hmask + 1 ? | |
303 | &xfrm_policy_inexact[dir] : | |
304 | xfrm_policy_bydst[dir].table + hash); | |
305 | } | |
306 | ||
307 | static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir) | |
308 | { | |
309 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; | |
310 | unsigned int hash = __addr_hash(daddr, saddr, family, hmask); | |
311 | ||
312 | return xfrm_policy_bydst[dir].table + hash; | |
313 | } | |
314 | ||
2518c7c2 DM |
315 | static void xfrm_dst_hash_transfer(struct hlist_head *list, |
316 | struct hlist_head *ndsttable, | |
317 | unsigned int nhashmask) | |
318 | { | |
319 | struct hlist_node *entry, *tmp; | |
320 | struct xfrm_policy *pol; | |
321 | ||
322 | hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) { | |
323 | unsigned int h; | |
324 | ||
325 | h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr, | |
326 | pol->family, nhashmask); | |
327 | hlist_add_head(&pol->bydst, ndsttable+h); | |
328 | } | |
329 | } | |
330 | ||
331 | static void xfrm_idx_hash_transfer(struct hlist_head *list, | |
332 | struct hlist_head *nidxtable, | |
333 | unsigned int nhashmask) | |
334 | { | |
335 | struct hlist_node *entry, *tmp; | |
336 | struct xfrm_policy *pol; | |
337 | ||
338 | hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) { | |
339 | unsigned int h; | |
340 | ||
341 | h = __idx_hash(pol->index, nhashmask); | |
342 | hlist_add_head(&pol->byidx, nidxtable+h); | |
343 | } | |
344 | } | |
345 | ||
346 | static unsigned long xfrm_new_hash_mask(unsigned int old_hmask) | |
347 | { | |
348 | return ((old_hmask + 1) << 1) - 1; | |
349 | } | |
350 | ||
351 | static void xfrm_bydst_resize(int dir) | |
352 | { | |
353 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; | |
354 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); | |
355 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); | |
356 | struct hlist_head *odst = xfrm_policy_bydst[dir].table; | |
44e36b42 | 357 | struct hlist_head *ndst = xfrm_hash_alloc(nsize); |
2518c7c2 DM |
358 | int i; |
359 | ||
360 | if (!ndst) | |
361 | return; | |
362 | ||
363 | write_lock_bh(&xfrm_policy_lock); | |
364 | ||
365 | for (i = hmask; i >= 0; i--) | |
366 | xfrm_dst_hash_transfer(odst + i, ndst, nhashmask); | |
367 | ||
368 | xfrm_policy_bydst[dir].table = ndst; | |
369 | xfrm_policy_bydst[dir].hmask = nhashmask; | |
370 | ||
371 | write_unlock_bh(&xfrm_policy_lock); | |
372 | ||
44e36b42 | 373 | xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head)); |
2518c7c2 DM |
374 | } |
375 | ||
376 | static void xfrm_byidx_resize(int total) | |
377 | { | |
378 | unsigned int hmask = xfrm_idx_hmask; | |
379 | unsigned int nhashmask = xfrm_new_hash_mask(hmask); | |
380 | unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head); | |
381 | struct hlist_head *oidx = xfrm_policy_byidx; | |
44e36b42 | 382 | struct hlist_head *nidx = xfrm_hash_alloc(nsize); |
2518c7c2 DM |
383 | int i; |
384 | ||
385 | if (!nidx) | |
386 | return; | |
387 | ||
388 | write_lock_bh(&xfrm_policy_lock); | |
389 | ||
390 | for (i = hmask; i >= 0; i--) | |
391 | xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask); | |
392 | ||
393 | xfrm_policy_byidx = nidx; | |
394 | xfrm_idx_hmask = nhashmask; | |
395 | ||
396 | write_unlock_bh(&xfrm_policy_lock); | |
397 | ||
44e36b42 | 398 | xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head)); |
2518c7c2 DM |
399 | } |
400 | ||
401 | static inline int xfrm_bydst_should_resize(int dir, int *total) | |
402 | { | |
403 | unsigned int cnt = xfrm_policy_count[dir]; | |
404 | unsigned int hmask = xfrm_policy_bydst[dir].hmask; | |
405 | ||
406 | if (total) | |
407 | *total += cnt; | |
408 | ||
409 | if ((hmask + 1) < xfrm_policy_hashmax && | |
410 | cnt > hmask) | |
411 | return 1; | |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | static inline int xfrm_byidx_should_resize(int total) | |
417 | { | |
418 | unsigned int hmask = xfrm_idx_hmask; | |
419 | ||
420 | if ((hmask + 1) < xfrm_policy_hashmax && | |
421 | total > hmask) | |
422 | return 1; | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
5a6d3416 | 427 | void xfrm_spd_getinfo(struct xfrmk_spdinfo *si) |
ecfd6b18 JHS |
428 | { |
429 | read_lock_bh(&xfrm_policy_lock); | |
430 | si->incnt = xfrm_policy_count[XFRM_POLICY_IN]; | |
431 | si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT]; | |
432 | si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD]; | |
433 | si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX]; | |
434 | si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX]; | |
435 | si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX]; | |
436 | si->spdhcnt = xfrm_idx_hmask; | |
437 | si->spdhmcnt = xfrm_policy_hashmax; | |
438 | read_unlock_bh(&xfrm_policy_lock); | |
439 | } | |
440 | EXPORT_SYMBOL(xfrm_spd_getinfo); | |
2518c7c2 | 441 | |
ecfd6b18 | 442 | static DEFINE_MUTEX(hash_resize_mutex); |
c4028958 | 443 | static void xfrm_hash_resize(struct work_struct *__unused) |
2518c7c2 DM |
444 | { |
445 | int dir, total; | |
446 | ||
447 | mutex_lock(&hash_resize_mutex); | |
448 | ||
449 | total = 0; | |
450 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { | |
451 | if (xfrm_bydst_should_resize(dir, &total)) | |
452 | xfrm_bydst_resize(dir); | |
453 | } | |
454 | if (xfrm_byidx_should_resize(total)) | |
455 | xfrm_byidx_resize(total); | |
456 | ||
457 | mutex_unlock(&hash_resize_mutex); | |
458 | } | |
459 | ||
c4028958 | 460 | static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize); |
2518c7c2 | 461 | |
1da177e4 LT |
462 | /* Generate new index... KAME seems to generate them ordered by cost |
463 | * of an absolute inpredictability of ordering of rules. This will not pass. */ | |
4e81bb83 | 464 | static u32 xfrm_gen_index(u8 type, int dir) |
1da177e4 | 465 | { |
1da177e4 LT |
466 | static u32 idx_generator; |
467 | ||
468 | for (;;) { | |
2518c7c2 DM |
469 | struct hlist_node *entry; |
470 | struct hlist_head *list; | |
471 | struct xfrm_policy *p; | |
472 | u32 idx; | |
473 | int found; | |
474 | ||
1da177e4 LT |
475 | idx = (idx_generator | dir); |
476 | idx_generator += 8; | |
477 | if (idx == 0) | |
478 | idx = 8; | |
2518c7c2 DM |
479 | list = xfrm_policy_byidx + idx_hash(idx); |
480 | found = 0; | |
481 | hlist_for_each_entry(p, entry, list, byidx) { | |
482 | if (p->index == idx) { | |
483 | found = 1; | |
1da177e4 | 484 | break; |
2518c7c2 | 485 | } |
1da177e4 | 486 | } |
2518c7c2 | 487 | if (!found) |
1da177e4 LT |
488 | return idx; |
489 | } | |
490 | } | |
491 | ||
2518c7c2 DM |
492 | static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2) |
493 | { | |
494 | u32 *p1 = (u32 *) s1; | |
495 | u32 *p2 = (u32 *) s2; | |
496 | int len = sizeof(struct xfrm_selector) / sizeof(u32); | |
497 | int i; | |
498 | ||
499 | for (i = 0; i < len; i++) { | |
500 | if (p1[i] != p2[i]) | |
501 | return 1; | |
502 | } | |
503 | ||
504 | return 0; | |
505 | } | |
506 | ||
1da177e4 LT |
507 | int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl) |
508 | { | |
2518c7c2 DM |
509 | struct xfrm_policy *pol; |
510 | struct xfrm_policy *delpol; | |
511 | struct hlist_head *chain; | |
a6c7ab55 | 512 | struct hlist_node *entry, *newpos; |
9b78a82c | 513 | struct dst_entry *gc_list; |
1da177e4 LT |
514 | |
515 | write_lock_bh(&xfrm_policy_lock); | |
2518c7c2 DM |
516 | chain = policy_hash_bysel(&policy->selector, policy->family, dir); |
517 | delpol = NULL; | |
518 | newpos = NULL; | |
2518c7c2 | 519 | hlist_for_each_entry(pol, entry, chain, bydst) { |
a6c7ab55 | 520 | if (pol->type == policy->type && |
2518c7c2 | 521 | !selector_cmp(&pol->selector, &policy->selector) && |
a6c7ab55 HX |
522 | xfrm_sec_ctx_match(pol->security, policy->security) && |
523 | !WARN_ON(delpol)) { | |
1da177e4 LT |
524 | if (excl) { |
525 | write_unlock_bh(&xfrm_policy_lock); | |
526 | return -EEXIST; | |
527 | } | |
1da177e4 LT |
528 | delpol = pol; |
529 | if (policy->priority > pol->priority) | |
530 | continue; | |
531 | } else if (policy->priority >= pol->priority) { | |
a6c7ab55 | 532 | newpos = &pol->bydst; |
1da177e4 LT |
533 | continue; |
534 | } | |
1da177e4 LT |
535 | if (delpol) |
536 | break; | |
1da177e4 LT |
537 | } |
538 | if (newpos) | |
2518c7c2 DM |
539 | hlist_add_after(newpos, &policy->bydst); |
540 | else | |
541 | hlist_add_head(&policy->bydst, chain); | |
1da177e4 | 542 | xfrm_pol_hold(policy); |
2518c7c2 | 543 | xfrm_policy_count[dir]++; |
1da177e4 | 544 | atomic_inc(&flow_cache_genid); |
2518c7c2 DM |
545 | if (delpol) { |
546 | hlist_del(&delpol->bydst); | |
547 | hlist_del(&delpol->byidx); | |
548 | xfrm_policy_count[dir]--; | |
549 | } | |
4e81bb83 | 550 | policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir); |
2518c7c2 | 551 | hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index)); |
9d729f72 | 552 | policy->curlft.add_time = get_seconds(); |
1da177e4 LT |
553 | policy->curlft.use_time = 0; |
554 | if (!mod_timer(&policy->timer, jiffies + HZ)) | |
555 | xfrm_pol_hold(policy); | |
556 | write_unlock_bh(&xfrm_policy_lock); | |
557 | ||
9b78a82c | 558 | if (delpol) |
1da177e4 | 559 | xfrm_policy_kill(delpol); |
2518c7c2 DM |
560 | else if (xfrm_bydst_should_resize(dir, NULL)) |
561 | schedule_work(&xfrm_hash_work); | |
9b78a82c DM |
562 | |
563 | read_lock_bh(&xfrm_policy_lock); | |
564 | gc_list = NULL; | |
2518c7c2 DM |
565 | entry = &policy->bydst; |
566 | hlist_for_each_entry_continue(policy, entry, bydst) { | |
9b78a82c DM |
567 | struct dst_entry *dst; |
568 | ||
569 | write_lock(&policy->lock); | |
570 | dst = policy->bundles; | |
571 | if (dst) { | |
572 | struct dst_entry *tail = dst; | |
573 | while (tail->next) | |
574 | tail = tail->next; | |
575 | tail->next = gc_list; | |
576 | gc_list = dst; | |
577 | ||
578 | policy->bundles = NULL; | |
579 | } | |
580 | write_unlock(&policy->lock); | |
1da177e4 | 581 | } |
9b78a82c DM |
582 | read_unlock_bh(&xfrm_policy_lock); |
583 | ||
584 | while (gc_list) { | |
585 | struct dst_entry *dst = gc_list; | |
586 | ||
587 | gc_list = dst->next; | |
588 | dst_free(dst); | |
589 | } | |
590 | ||
1da177e4 LT |
591 | return 0; |
592 | } | |
593 | EXPORT_SYMBOL(xfrm_policy_insert); | |
594 | ||
4e81bb83 MN |
595 | struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir, |
596 | struct xfrm_selector *sel, | |
ef41aaa0 EP |
597 | struct xfrm_sec_ctx *ctx, int delete, |
598 | int *err) | |
1da177e4 | 599 | { |
2518c7c2 DM |
600 | struct xfrm_policy *pol, *ret; |
601 | struct hlist_head *chain; | |
602 | struct hlist_node *entry; | |
1da177e4 | 603 | |
ef41aaa0 | 604 | *err = 0; |
1da177e4 | 605 | write_lock_bh(&xfrm_policy_lock); |
2518c7c2 DM |
606 | chain = policy_hash_bysel(sel, sel->family, dir); |
607 | ret = NULL; | |
608 | hlist_for_each_entry(pol, entry, chain, bydst) { | |
609 | if (pol->type == type && | |
610 | !selector_cmp(sel, &pol->selector) && | |
611 | xfrm_sec_ctx_match(ctx, pol->security)) { | |
1da177e4 | 612 | xfrm_pol_hold(pol); |
2518c7c2 | 613 | if (delete) { |
ef41aaa0 EP |
614 | *err = security_xfrm_policy_delete(pol); |
615 | if (*err) { | |
616 | write_unlock_bh(&xfrm_policy_lock); | |
617 | return pol; | |
618 | } | |
2518c7c2 DM |
619 | hlist_del(&pol->bydst); |
620 | hlist_del(&pol->byidx); | |
621 | xfrm_policy_count[dir]--; | |
622 | } | |
623 | ret = pol; | |
1da177e4 LT |
624 | break; |
625 | } | |
626 | } | |
627 | write_unlock_bh(&xfrm_policy_lock); | |
628 | ||
2518c7c2 | 629 | if (ret && delete) { |
1da177e4 | 630 | atomic_inc(&flow_cache_genid); |
2518c7c2 | 631 | xfrm_policy_kill(ret); |
1da177e4 | 632 | } |
2518c7c2 | 633 | return ret; |
1da177e4 | 634 | } |
df71837d | 635 | EXPORT_SYMBOL(xfrm_policy_bysel_ctx); |
1da177e4 | 636 | |
ef41aaa0 EP |
637 | struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete, |
638 | int *err) | |
1da177e4 | 639 | { |
2518c7c2 DM |
640 | struct xfrm_policy *pol, *ret; |
641 | struct hlist_head *chain; | |
642 | struct hlist_node *entry; | |
1da177e4 | 643 | |
b5505c6e HX |
644 | *err = -ENOENT; |
645 | if (xfrm_policy_id2dir(id) != dir) | |
646 | return NULL; | |
647 | ||
ef41aaa0 | 648 | *err = 0; |
1da177e4 | 649 | write_lock_bh(&xfrm_policy_lock); |
2518c7c2 DM |
650 | chain = xfrm_policy_byidx + idx_hash(id); |
651 | ret = NULL; | |
652 | hlist_for_each_entry(pol, entry, chain, byidx) { | |
653 | if (pol->type == type && pol->index == id) { | |
1da177e4 | 654 | xfrm_pol_hold(pol); |
2518c7c2 | 655 | if (delete) { |
ef41aaa0 EP |
656 | *err = security_xfrm_policy_delete(pol); |
657 | if (*err) { | |
658 | write_unlock_bh(&xfrm_policy_lock); | |
659 | return pol; | |
660 | } | |
2518c7c2 DM |
661 | hlist_del(&pol->bydst); |
662 | hlist_del(&pol->byidx); | |
663 | xfrm_policy_count[dir]--; | |
664 | } | |
665 | ret = pol; | |
1da177e4 LT |
666 | break; |
667 | } | |
668 | } | |
669 | write_unlock_bh(&xfrm_policy_lock); | |
670 | ||
2518c7c2 | 671 | if (ret && delete) { |
1da177e4 | 672 | atomic_inc(&flow_cache_genid); |
2518c7c2 | 673 | xfrm_policy_kill(ret); |
1da177e4 | 674 | } |
2518c7c2 | 675 | return ret; |
1da177e4 LT |
676 | } |
677 | EXPORT_SYMBOL(xfrm_policy_byid); | |
678 | ||
4aa2e62c JL |
679 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
680 | static inline int | |
681 | xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info) | |
1da177e4 | 682 | { |
4aa2e62c JL |
683 | int dir, err = 0; |
684 | ||
685 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { | |
686 | struct xfrm_policy *pol; | |
687 | struct hlist_node *entry; | |
688 | int i; | |
689 | ||
690 | hlist_for_each_entry(pol, entry, | |
691 | &xfrm_policy_inexact[dir], bydst) { | |
692 | if (pol->type != type) | |
693 | continue; | |
694 | err = security_xfrm_policy_delete(pol); | |
695 | if (err) { | |
ab5f5e8b JL |
696 | xfrm_audit_policy_delete(pol, 0, |
697 | audit_info->loginuid, | |
698 | audit_info->secid); | |
4aa2e62c JL |
699 | return err; |
700 | } | |
7dc12d6d | 701 | } |
4aa2e62c JL |
702 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
703 | hlist_for_each_entry(pol, entry, | |
704 | xfrm_policy_bydst[dir].table + i, | |
705 | bydst) { | |
706 | if (pol->type != type) | |
707 | continue; | |
708 | err = security_xfrm_policy_delete(pol); | |
709 | if (err) { | |
ab5f5e8b JL |
710 | xfrm_audit_policy_delete(pol, 0, |
711 | audit_info->loginuid, | |
712 | audit_info->secid); | |
4aa2e62c JL |
713 | return err; |
714 | } | |
715 | } | |
716 | } | |
717 | } | |
718 | return err; | |
719 | } | |
720 | #else | |
721 | static inline int | |
722 | xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info) | |
723 | { | |
724 | return 0; | |
725 | } | |
726 | #endif | |
727 | ||
728 | int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info) | |
729 | { | |
730 | int dir, err = 0; | |
1da177e4 LT |
731 | |
732 | write_lock_bh(&xfrm_policy_lock); | |
4aa2e62c JL |
733 | |
734 | err = xfrm_policy_flush_secctx_check(type, audit_info); | |
735 | if (err) | |
736 | goto out; | |
737 | ||
1da177e4 | 738 | for (dir = 0; dir < XFRM_POLICY_MAX; dir++) { |
2518c7c2 DM |
739 | struct xfrm_policy *pol; |
740 | struct hlist_node *entry; | |
ae8c0577 | 741 | int i, killed; |
2518c7c2 | 742 | |
ae8c0577 | 743 | killed = 0; |
2518c7c2 DM |
744 | again1: |
745 | hlist_for_each_entry(pol, entry, | |
746 | &xfrm_policy_inexact[dir], bydst) { | |
747 | if (pol->type != type) | |
748 | continue; | |
749 | hlist_del(&pol->bydst); | |
750 | hlist_del(&pol->byidx); | |
1da177e4 LT |
751 | write_unlock_bh(&xfrm_policy_lock); |
752 | ||
ab5f5e8b JL |
753 | xfrm_audit_policy_delete(pol, 1, audit_info->loginuid, |
754 | audit_info->secid); | |
161a09e7 | 755 | |
2518c7c2 | 756 | xfrm_policy_kill(pol); |
ae8c0577 | 757 | killed++; |
1da177e4 LT |
758 | |
759 | write_lock_bh(&xfrm_policy_lock); | |
2518c7c2 DM |
760 | goto again1; |
761 | } | |
762 | ||
763 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { | |
764 | again2: | |
765 | hlist_for_each_entry(pol, entry, | |
766 | xfrm_policy_bydst[dir].table + i, | |
767 | bydst) { | |
768 | if (pol->type != type) | |
769 | continue; | |
770 | hlist_del(&pol->bydst); | |
771 | hlist_del(&pol->byidx); | |
772 | write_unlock_bh(&xfrm_policy_lock); | |
773 | ||
ab5f5e8b JL |
774 | xfrm_audit_policy_delete(pol, 1, |
775 | audit_info->loginuid, | |
776 | audit_info->secid); | |
2518c7c2 | 777 | xfrm_policy_kill(pol); |
ae8c0577 | 778 | killed++; |
2518c7c2 DM |
779 | |
780 | write_lock_bh(&xfrm_policy_lock); | |
781 | goto again2; | |
782 | } | |
1da177e4 | 783 | } |
2518c7c2 | 784 | |
ae8c0577 | 785 | xfrm_policy_count[dir] -= killed; |
1da177e4 LT |
786 | } |
787 | atomic_inc(&flow_cache_genid); | |
4aa2e62c | 788 | out: |
1da177e4 | 789 | write_unlock_bh(&xfrm_policy_lock); |
4aa2e62c | 790 | return err; |
1da177e4 LT |
791 | } |
792 | EXPORT_SYMBOL(xfrm_policy_flush); | |
793 | ||
4e81bb83 | 794 | int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*), |
1da177e4 LT |
795 | void *data) |
796 | { | |
baf5d743 | 797 | struct xfrm_policy *pol, *last = NULL; |
2518c7c2 | 798 | struct hlist_node *entry; |
baf5d743 | 799 | int dir, last_dir = 0, count, error; |
1da177e4 LT |
800 | |
801 | read_lock_bh(&xfrm_policy_lock); | |
2518c7c2 | 802 | count = 0; |
1da177e4 LT |
803 | |
804 | for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) { | |
2518c7c2 DM |
805 | struct hlist_head *table = xfrm_policy_bydst[dir].table; |
806 | int i; | |
807 | ||
808 | hlist_for_each_entry(pol, entry, | |
809 | &xfrm_policy_inexact[dir], bydst) { | |
810 | if (pol->type != type) | |
811 | continue; | |
baf5d743 JHS |
812 | if (last) { |
813 | error = func(last, last_dir % XFRM_POLICY_MAX, | |
814 | count, data); | |
815 | if (error) | |
816 | goto out; | |
817 | } | |
818 | last = pol; | |
819 | last_dir = dir; | |
820 | count++; | |
1da177e4 | 821 | } |
2518c7c2 DM |
822 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { |
823 | hlist_for_each_entry(pol, entry, table + i, bydst) { | |
824 | if (pol->type != type) | |
825 | continue; | |
baf5d743 JHS |
826 | if (last) { |
827 | error = func(last, last_dir % XFRM_POLICY_MAX, | |
828 | count, data); | |
829 | if (error) | |
830 | goto out; | |
831 | } | |
832 | last = pol; | |
833 | last_dir = dir; | |
834 | count++; | |
2518c7c2 DM |
835 | } |
836 | } | |
1da177e4 | 837 | } |
baf5d743 JHS |
838 | if (count == 0) { |
839 | error = -ENOENT; | |
840 | goto out; | |
841 | } | |
842 | error = func(last, last_dir % XFRM_POLICY_MAX, 0, data); | |
1da177e4 LT |
843 | out: |
844 | read_unlock_bh(&xfrm_policy_lock); | |
845 | return error; | |
846 | } | |
847 | EXPORT_SYMBOL(xfrm_policy_walk); | |
848 | ||
134b0fc5 JM |
849 | /* |
850 | * Find policy to apply to this flow. | |
851 | * | |
852 | * Returns 0 if policy found, else an -errno. | |
853 | */ | |
2518c7c2 DM |
854 | static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl, |
855 | u8 type, u16 family, int dir) | |
1da177e4 | 856 | { |
2518c7c2 | 857 | struct xfrm_selector *sel = &pol->selector; |
134b0fc5 | 858 | int match, ret = -ESRCH; |
1da177e4 | 859 | |
2518c7c2 DM |
860 | if (pol->family != family || |
861 | pol->type != type) | |
134b0fc5 | 862 | return ret; |
1da177e4 | 863 | |
2518c7c2 | 864 | match = xfrm_selector_match(sel, fl, family); |
134b0fc5 JM |
865 | if (match) |
866 | ret = security_xfrm_policy_lookup(pol, fl->secid, dir); | |
2518c7c2 | 867 | |
134b0fc5 | 868 | return ret; |
2518c7c2 | 869 | } |
1da177e4 | 870 | |
2518c7c2 DM |
871 | static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl, |
872 | u16 family, u8 dir) | |
873 | { | |
134b0fc5 | 874 | int err; |
2518c7c2 DM |
875 | struct xfrm_policy *pol, *ret; |
876 | xfrm_address_t *daddr, *saddr; | |
877 | struct hlist_node *entry; | |
878 | struct hlist_head *chain; | |
acba48e1 | 879 | u32 priority = ~0U; |
df71837d | 880 | |
2518c7c2 DM |
881 | daddr = xfrm_flowi_daddr(fl, family); |
882 | saddr = xfrm_flowi_saddr(fl, family); | |
883 | if (unlikely(!daddr || !saddr)) | |
884 | return NULL; | |
885 | ||
886 | read_lock_bh(&xfrm_policy_lock); | |
887 | chain = policy_hash_direct(daddr, saddr, family, dir); | |
888 | ret = NULL; | |
889 | hlist_for_each_entry(pol, entry, chain, bydst) { | |
134b0fc5 JM |
890 | err = xfrm_policy_match(pol, fl, type, family, dir); |
891 | if (err) { | |
892 | if (err == -ESRCH) | |
893 | continue; | |
894 | else { | |
895 | ret = ERR_PTR(err); | |
896 | goto fail; | |
897 | } | |
898 | } else { | |
2518c7c2 | 899 | ret = pol; |
acba48e1 | 900 | priority = ret->priority; |
2518c7c2 DM |
901 | break; |
902 | } | |
903 | } | |
acba48e1 DM |
904 | chain = &xfrm_policy_inexact[dir]; |
905 | hlist_for_each_entry(pol, entry, chain, bydst) { | |
134b0fc5 JM |
906 | err = xfrm_policy_match(pol, fl, type, family, dir); |
907 | if (err) { | |
908 | if (err == -ESRCH) | |
909 | continue; | |
910 | else { | |
911 | ret = ERR_PTR(err); | |
912 | goto fail; | |
913 | } | |
914 | } else if (pol->priority < priority) { | |
acba48e1 DM |
915 | ret = pol; |
916 | break; | |
1da177e4 LT |
917 | } |
918 | } | |
acba48e1 DM |
919 | if (ret) |
920 | xfrm_pol_hold(ret); | |
134b0fc5 | 921 | fail: |
1da177e4 | 922 | read_unlock_bh(&xfrm_policy_lock); |
4e81bb83 | 923 | |
2518c7c2 | 924 | return ret; |
4e81bb83 MN |
925 | } |
926 | ||
134b0fc5 | 927 | static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir, |
4e81bb83 MN |
928 | void **objp, atomic_t **obj_refp) |
929 | { | |
930 | struct xfrm_policy *pol; | |
134b0fc5 | 931 | int err = 0; |
4e81bb83 MN |
932 | |
933 | #ifdef CONFIG_XFRM_SUB_POLICY | |
934 | pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir); | |
134b0fc5 JM |
935 | if (IS_ERR(pol)) { |
936 | err = PTR_ERR(pol); | |
937 | pol = NULL; | |
938 | } | |
939 | if (pol || err) | |
4e81bb83 MN |
940 | goto end; |
941 | #endif | |
942 | pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir); | |
134b0fc5 JM |
943 | if (IS_ERR(pol)) { |
944 | err = PTR_ERR(pol); | |
945 | pol = NULL; | |
946 | } | |
4e81bb83 | 947 | #ifdef CONFIG_XFRM_SUB_POLICY |
2518c7c2 | 948 | end: |
4e81bb83 | 949 | #endif |
1da177e4 LT |
950 | if ((*objp = (void *) pol) != NULL) |
951 | *obj_refp = &pol->refcnt; | |
134b0fc5 | 952 | return err; |
1da177e4 LT |
953 | } |
954 | ||
df71837d TJ |
955 | static inline int policy_to_flow_dir(int dir) |
956 | { | |
957 | if (XFRM_POLICY_IN == FLOW_DIR_IN && | |
a716c119 YH |
958 | XFRM_POLICY_OUT == FLOW_DIR_OUT && |
959 | XFRM_POLICY_FWD == FLOW_DIR_FWD) | |
960 | return dir; | |
961 | switch (dir) { | |
962 | default: | |
963 | case XFRM_POLICY_IN: | |
964 | return FLOW_DIR_IN; | |
965 | case XFRM_POLICY_OUT: | |
966 | return FLOW_DIR_OUT; | |
967 | case XFRM_POLICY_FWD: | |
968 | return FLOW_DIR_FWD; | |
3ff50b79 | 969 | } |
df71837d TJ |
970 | } |
971 | ||
e0d1caa7 | 972 | static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl) |
1da177e4 LT |
973 | { |
974 | struct xfrm_policy *pol; | |
975 | ||
976 | read_lock_bh(&xfrm_policy_lock); | |
977 | if ((pol = sk->sk_policy[dir]) != NULL) { | |
a716c119 | 978 | int match = xfrm_selector_match(&pol->selector, fl, |
1da177e4 | 979 | sk->sk_family); |
a716c119 | 980 | int err = 0; |
df71837d | 981 | |
3bccfbc7 VY |
982 | if (match) { |
983 | err = security_xfrm_policy_lookup(pol, fl->secid, | |
984 | policy_to_flow_dir(dir)); | |
985 | if (!err) | |
986 | xfrm_pol_hold(pol); | |
987 | else if (err == -ESRCH) | |
988 | pol = NULL; | |
989 | else | |
990 | pol = ERR_PTR(err); | |
991 | } else | |
1da177e4 LT |
992 | pol = NULL; |
993 | } | |
994 | read_unlock_bh(&xfrm_policy_lock); | |
995 | return pol; | |
996 | } | |
997 | ||
998 | static void __xfrm_policy_link(struct xfrm_policy *pol, int dir) | |
999 | { | |
2518c7c2 DM |
1000 | struct hlist_head *chain = policy_hash_bysel(&pol->selector, |
1001 | pol->family, dir); | |
4e81bb83 | 1002 | |
2518c7c2 DM |
1003 | hlist_add_head(&pol->bydst, chain); |
1004 | hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index)); | |
1005 | xfrm_policy_count[dir]++; | |
1da177e4 | 1006 | xfrm_pol_hold(pol); |
2518c7c2 DM |
1007 | |
1008 | if (xfrm_bydst_should_resize(dir, NULL)) | |
1009 | schedule_work(&xfrm_hash_work); | |
1da177e4 LT |
1010 | } |
1011 | ||
1012 | static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol, | |
1013 | int dir) | |
1014 | { | |
2518c7c2 DM |
1015 | if (hlist_unhashed(&pol->bydst)) |
1016 | return NULL; | |
1da177e4 | 1017 | |
2518c7c2 DM |
1018 | hlist_del(&pol->bydst); |
1019 | hlist_del(&pol->byidx); | |
1020 | xfrm_policy_count[dir]--; | |
1021 | ||
1022 | return pol; | |
1da177e4 LT |
1023 | } |
1024 | ||
4666faab | 1025 | int xfrm_policy_delete(struct xfrm_policy *pol, int dir) |
1da177e4 LT |
1026 | { |
1027 | write_lock_bh(&xfrm_policy_lock); | |
1028 | pol = __xfrm_policy_unlink(pol, dir); | |
1029 | write_unlock_bh(&xfrm_policy_lock); | |
1030 | if (pol) { | |
1031 | if (dir < XFRM_POLICY_MAX) | |
1032 | atomic_inc(&flow_cache_genid); | |
1033 | xfrm_policy_kill(pol); | |
4666faab | 1034 | return 0; |
1da177e4 | 1035 | } |
4666faab | 1036 | return -ENOENT; |
1da177e4 | 1037 | } |
a70fcb0b | 1038 | EXPORT_SYMBOL(xfrm_policy_delete); |
1da177e4 LT |
1039 | |
1040 | int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol) | |
1041 | { | |
1042 | struct xfrm_policy *old_pol; | |
1043 | ||
4e81bb83 MN |
1044 | #ifdef CONFIG_XFRM_SUB_POLICY |
1045 | if (pol && pol->type != XFRM_POLICY_TYPE_MAIN) | |
1046 | return -EINVAL; | |
1047 | #endif | |
1048 | ||
1da177e4 LT |
1049 | write_lock_bh(&xfrm_policy_lock); |
1050 | old_pol = sk->sk_policy[dir]; | |
1051 | sk->sk_policy[dir] = pol; | |
1052 | if (pol) { | |
9d729f72 | 1053 | pol->curlft.add_time = get_seconds(); |
4e81bb83 | 1054 | pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir); |
1da177e4 LT |
1055 | __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir); |
1056 | } | |
1057 | if (old_pol) | |
1058 | __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir); | |
1059 | write_unlock_bh(&xfrm_policy_lock); | |
1060 | ||
1061 | if (old_pol) { | |
1062 | xfrm_policy_kill(old_pol); | |
1063 | } | |
1064 | return 0; | |
1065 | } | |
1066 | ||
1067 | static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir) | |
1068 | { | |
1069 | struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC); | |
1070 | ||
1071 | if (newp) { | |
1072 | newp->selector = old->selector; | |
df71837d TJ |
1073 | if (security_xfrm_policy_clone(old, newp)) { |
1074 | kfree(newp); | |
1075 | return NULL; /* ENOMEM */ | |
1076 | } | |
1da177e4 LT |
1077 | newp->lft = old->lft; |
1078 | newp->curlft = old->curlft; | |
1079 | newp->action = old->action; | |
1080 | newp->flags = old->flags; | |
1081 | newp->xfrm_nr = old->xfrm_nr; | |
1082 | newp->index = old->index; | |
4e81bb83 | 1083 | newp->type = old->type; |
1da177e4 LT |
1084 | memcpy(newp->xfrm_vec, old->xfrm_vec, |
1085 | newp->xfrm_nr*sizeof(struct xfrm_tmpl)); | |
1086 | write_lock_bh(&xfrm_policy_lock); | |
1087 | __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir); | |
1088 | write_unlock_bh(&xfrm_policy_lock); | |
1089 | xfrm_pol_put(newp); | |
1090 | } | |
1091 | return newp; | |
1092 | } | |
1093 | ||
1094 | int __xfrm_sk_clone_policy(struct sock *sk) | |
1095 | { | |
1096 | struct xfrm_policy *p0 = sk->sk_policy[0], | |
1097 | *p1 = sk->sk_policy[1]; | |
1098 | ||
1099 | sk->sk_policy[0] = sk->sk_policy[1] = NULL; | |
1100 | if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL) | |
1101 | return -ENOMEM; | |
1102 | if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL) | |
1103 | return -ENOMEM; | |
1104 | return 0; | |
1105 | } | |
1106 | ||
a1e59abf PM |
1107 | static int |
1108 | xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote, | |
1109 | unsigned short family) | |
1110 | { | |
1111 | int err; | |
1112 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); | |
1113 | ||
1114 | if (unlikely(afinfo == NULL)) | |
1115 | return -EINVAL; | |
1116 | err = afinfo->get_saddr(local, remote); | |
1117 | xfrm_policy_put_afinfo(afinfo); | |
1118 | return err; | |
1119 | } | |
1120 | ||
1da177e4 LT |
1121 | /* Resolve list of templates for the flow, given policy. */ |
1122 | ||
1123 | static int | |
4e81bb83 MN |
1124 | xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl, |
1125 | struct xfrm_state **xfrm, | |
1126 | unsigned short family) | |
1da177e4 LT |
1127 | { |
1128 | int nx; | |
1129 | int i, error; | |
1130 | xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family); | |
1131 | xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family); | |
a1e59abf | 1132 | xfrm_address_t tmp; |
1da177e4 LT |
1133 | |
1134 | for (nx=0, i = 0; i < policy->xfrm_nr; i++) { | |
1135 | struct xfrm_state *x; | |
1136 | xfrm_address_t *remote = daddr; | |
1137 | xfrm_address_t *local = saddr; | |
1138 | struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i]; | |
1139 | ||
48b8d783 JK |
1140 | if (tmpl->mode == XFRM_MODE_TUNNEL || |
1141 | tmpl->mode == XFRM_MODE_BEET) { | |
1da177e4 LT |
1142 | remote = &tmpl->id.daddr; |
1143 | local = &tmpl->saddr; | |
76b3f055 | 1144 | family = tmpl->encap_family; |
a1e59abf PM |
1145 | if (xfrm_addr_any(local, family)) { |
1146 | error = xfrm_get_saddr(&tmp, remote, family); | |
1147 | if (error) | |
1148 | goto fail; | |
1149 | local = &tmp; | |
1150 | } | |
1da177e4 LT |
1151 | } |
1152 | ||
1153 | x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family); | |
1154 | ||
1155 | if (x && x->km.state == XFRM_STATE_VALID) { | |
1156 | xfrm[nx++] = x; | |
1157 | daddr = remote; | |
1158 | saddr = local; | |
1159 | continue; | |
1160 | } | |
1161 | if (x) { | |
1162 | error = (x->km.state == XFRM_STATE_ERROR ? | |
1163 | -EINVAL : -EAGAIN); | |
1164 | xfrm_state_put(x); | |
1165 | } | |
1166 | ||
1167 | if (!tmpl->optional) | |
1168 | goto fail; | |
1169 | } | |
1170 | return nx; | |
1171 | ||
1172 | fail: | |
1173 | for (nx--; nx>=0; nx--) | |
1174 | xfrm_state_put(xfrm[nx]); | |
1175 | return error; | |
1176 | } | |
1177 | ||
4e81bb83 MN |
1178 | static int |
1179 | xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl, | |
1180 | struct xfrm_state **xfrm, | |
1181 | unsigned short family) | |
1182 | { | |
41a49cc3 MN |
1183 | struct xfrm_state *tp[XFRM_MAX_DEPTH]; |
1184 | struct xfrm_state **tpp = (npols > 1) ? tp : xfrm; | |
4e81bb83 MN |
1185 | int cnx = 0; |
1186 | int error; | |
1187 | int ret; | |
1188 | int i; | |
1189 | ||
1190 | for (i = 0; i < npols; i++) { | |
1191 | if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) { | |
1192 | error = -ENOBUFS; | |
1193 | goto fail; | |
1194 | } | |
41a49cc3 MN |
1195 | |
1196 | ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family); | |
4e81bb83 MN |
1197 | if (ret < 0) { |
1198 | error = ret; | |
1199 | goto fail; | |
1200 | } else | |
1201 | cnx += ret; | |
1202 | } | |
1203 | ||
41a49cc3 MN |
1204 | /* found states are sorted for outbound processing */ |
1205 | if (npols > 1) | |
1206 | xfrm_state_sort(xfrm, tpp, cnx, family); | |
1207 | ||
4e81bb83 MN |
1208 | return cnx; |
1209 | ||
1210 | fail: | |
1211 | for (cnx--; cnx>=0; cnx--) | |
41a49cc3 | 1212 | xfrm_state_put(tpp[cnx]); |
4e81bb83 MN |
1213 | return error; |
1214 | ||
1215 | } | |
1216 | ||
1da177e4 LT |
1217 | /* Check that the bundle accepts the flow and its components are |
1218 | * still valid. | |
1219 | */ | |
1220 | ||
1221 | static struct dst_entry * | |
1222 | xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family) | |
1223 | { | |
1224 | struct dst_entry *x; | |
1225 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); | |
1226 | if (unlikely(afinfo == NULL)) | |
1227 | return ERR_PTR(-EINVAL); | |
1228 | x = afinfo->find_bundle(fl, policy); | |
1229 | xfrm_policy_put_afinfo(afinfo); | |
1230 | return x; | |
1231 | } | |
1232 | ||
1233 | /* Allocate chain of dst_entry's, attach known xfrm's, calculate | |
1234 | * all the metrics... Shortly, bundle a bundle. | |
1235 | */ | |
1236 | ||
1237 | static int | |
1238 | xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx, | |
1239 | struct flowi *fl, struct dst_entry **dst_p, | |
1240 | unsigned short family) | |
1241 | { | |
1242 | int err; | |
1243 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); | |
1244 | if (unlikely(afinfo == NULL)) | |
1245 | return -EINVAL; | |
1246 | err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p); | |
1247 | xfrm_policy_put_afinfo(afinfo); | |
1248 | return err; | |
1249 | } | |
1250 | ||
157bfc25 MN |
1251 | static int inline |
1252 | xfrm_dst_alloc_copy(void **target, void *src, int size) | |
1253 | { | |
1254 | if (!*target) { | |
1255 | *target = kmalloc(size, GFP_ATOMIC); | |
1256 | if (!*target) | |
1257 | return -ENOMEM; | |
1258 | } | |
1259 | memcpy(*target, src, size); | |
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | static int inline | |
1264 | xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel) | |
1265 | { | |
1266 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1267 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
1268 | return xfrm_dst_alloc_copy((void **)&(xdst->partner), | |
1269 | sel, sizeof(*sel)); | |
1270 | #else | |
1271 | return 0; | |
1272 | #endif | |
1273 | } | |
1274 | ||
1275 | static int inline | |
1276 | xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl) | |
1277 | { | |
1278 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1279 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
1280 | return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl)); | |
1281 | #else | |
1282 | return 0; | |
1283 | #endif | |
1284 | } | |
1da177e4 LT |
1285 | |
1286 | static int stale_bundle(struct dst_entry *dst); | |
1287 | ||
1288 | /* Main function: finds/creates a bundle for given flow. | |
1289 | * | |
1290 | * At the moment we eat a raw IP route. Mostly to speed up lookups | |
1291 | * on interfaces with disabled IPsec. | |
1292 | */ | |
14e50e57 DM |
1293 | int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl, |
1294 | struct sock *sk, int flags) | |
1da177e4 LT |
1295 | { |
1296 | struct xfrm_policy *policy; | |
4e81bb83 MN |
1297 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
1298 | int npols; | |
1299 | int pol_dead; | |
1300 | int xfrm_nr; | |
1301 | int pi; | |
1da177e4 LT |
1302 | struct xfrm_state *xfrm[XFRM_MAX_DEPTH]; |
1303 | struct dst_entry *dst, *dst_orig = *dst_p; | |
1304 | int nx = 0; | |
1305 | int err; | |
1306 | u32 genid; | |
42cf93cd | 1307 | u16 family; |
df71837d | 1308 | u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT); |
e0d1caa7 | 1309 | |
1da177e4 LT |
1310 | restart: |
1311 | genid = atomic_read(&flow_cache_genid); | |
1312 | policy = NULL; | |
4e81bb83 MN |
1313 | for (pi = 0; pi < ARRAY_SIZE(pols); pi++) |
1314 | pols[pi] = NULL; | |
1315 | npols = 0; | |
1316 | pol_dead = 0; | |
1317 | xfrm_nr = 0; | |
1318 | ||
f7944fb1 | 1319 | if (sk && sk->sk_policy[XFRM_POLICY_OUT]) { |
e0d1caa7 | 1320 | policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl); |
3bccfbc7 VY |
1321 | if (IS_ERR(policy)) |
1322 | return PTR_ERR(policy); | |
1323 | } | |
1da177e4 LT |
1324 | |
1325 | if (!policy) { | |
1326 | /* To accelerate a bit... */ | |
2518c7c2 DM |
1327 | if ((dst_orig->flags & DST_NOXFRM) || |
1328 | !xfrm_policy_count[XFRM_POLICY_OUT]) | |
1da177e4 LT |
1329 | return 0; |
1330 | ||
e0d1caa7 | 1331 | policy = flow_cache_lookup(fl, dst_orig->ops->family, |
42cf93cd | 1332 | dir, xfrm_policy_lookup); |
134b0fc5 JM |
1333 | if (IS_ERR(policy)) |
1334 | return PTR_ERR(policy); | |
1da177e4 LT |
1335 | } |
1336 | ||
1337 | if (!policy) | |
1338 | return 0; | |
1339 | ||
42cf93cd | 1340 | family = dst_orig->ops->family; |
9d729f72 | 1341 | policy->curlft.use_time = get_seconds(); |
4e81bb83 MN |
1342 | pols[0] = policy; |
1343 | npols ++; | |
1344 | xfrm_nr += pols[0]->xfrm_nr; | |
1da177e4 LT |
1345 | |
1346 | switch (policy->action) { | |
1347 | case XFRM_POLICY_BLOCK: | |
1348 | /* Prohibit the flow */ | |
e104411b PM |
1349 | err = -EPERM; |
1350 | goto error; | |
1da177e4 LT |
1351 | |
1352 | case XFRM_POLICY_ALLOW: | |
4e81bb83 | 1353 | #ifndef CONFIG_XFRM_SUB_POLICY |
1da177e4 LT |
1354 | if (policy->xfrm_nr == 0) { |
1355 | /* Flow passes not transformed. */ | |
1356 | xfrm_pol_put(policy); | |
1357 | return 0; | |
1358 | } | |
4e81bb83 | 1359 | #endif |
1da177e4 LT |
1360 | |
1361 | /* Try to find matching bundle. | |
1362 | * | |
1363 | * LATER: help from flow cache. It is optional, this | |
1364 | * is required only for output policy. | |
1365 | */ | |
1366 | dst = xfrm_find_bundle(fl, policy, family); | |
1367 | if (IS_ERR(dst)) { | |
e104411b PM |
1368 | err = PTR_ERR(dst); |
1369 | goto error; | |
1da177e4 LT |
1370 | } |
1371 | ||
1372 | if (dst) | |
1373 | break; | |
1374 | ||
4e81bb83 MN |
1375 | #ifdef CONFIG_XFRM_SUB_POLICY |
1376 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { | |
1377 | pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, | |
1378 | fl, family, | |
1379 | XFRM_POLICY_OUT); | |
1380 | if (pols[1]) { | |
134b0fc5 JM |
1381 | if (IS_ERR(pols[1])) { |
1382 | err = PTR_ERR(pols[1]); | |
1383 | goto error; | |
1384 | } | |
4e81bb83 MN |
1385 | if (pols[1]->action == XFRM_POLICY_BLOCK) { |
1386 | err = -EPERM; | |
1387 | goto error; | |
1388 | } | |
1389 | npols ++; | |
1390 | xfrm_nr += pols[1]->xfrm_nr; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | /* | |
1395 | * Because neither flowi nor bundle information knows about | |
1396 | * transformation template size. On more than one policy usage | |
1397 | * we can realize whether all of them is bypass or not after | |
1398 | * they are searched. See above not-transformed bypass | |
1399 | * is surrounded by non-sub policy configuration, too. | |
1400 | */ | |
1401 | if (xfrm_nr == 0) { | |
1402 | /* Flow passes not transformed. */ | |
1403 | xfrm_pols_put(pols, npols); | |
1404 | return 0; | |
1405 | } | |
1406 | ||
1407 | #endif | |
1408 | nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family); | |
1da177e4 LT |
1409 | |
1410 | if (unlikely(nx<0)) { | |
1411 | err = nx; | |
14e50e57 DM |
1412 | if (err == -EAGAIN && sysctl_xfrm_larval_drop) { |
1413 | /* EREMOTE tells the caller to generate | |
1414 | * a one-shot blackhole route. | |
1415 | */ | |
1416 | xfrm_pol_put(policy); | |
1417 | return -EREMOTE; | |
1418 | } | |
1da177e4 LT |
1419 | if (err == -EAGAIN && flags) { |
1420 | DECLARE_WAITQUEUE(wait, current); | |
1421 | ||
1422 | add_wait_queue(&km_waitq, &wait); | |
1423 | set_current_state(TASK_INTERRUPTIBLE); | |
1424 | schedule(); | |
1425 | set_current_state(TASK_RUNNING); | |
1426 | remove_wait_queue(&km_waitq, &wait); | |
1427 | ||
4e81bb83 | 1428 | nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family); |
1da177e4 LT |
1429 | |
1430 | if (nx == -EAGAIN && signal_pending(current)) { | |
1431 | err = -ERESTART; | |
1432 | goto error; | |
1433 | } | |
1434 | if (nx == -EAGAIN || | |
1435 | genid != atomic_read(&flow_cache_genid)) { | |
4e81bb83 | 1436 | xfrm_pols_put(pols, npols); |
1da177e4 LT |
1437 | goto restart; |
1438 | } | |
1439 | err = nx; | |
1440 | } | |
1441 | if (err < 0) | |
1442 | goto error; | |
1443 | } | |
1444 | if (nx == 0) { | |
1445 | /* Flow passes not transformed. */ | |
4e81bb83 | 1446 | xfrm_pols_put(pols, npols); |
1da177e4 LT |
1447 | return 0; |
1448 | } | |
1449 | ||
1450 | dst = dst_orig; | |
1451 | err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family); | |
1452 | ||
1453 | if (unlikely(err)) { | |
1454 | int i; | |
1455 | for (i=0; i<nx; i++) | |
1456 | xfrm_state_put(xfrm[i]); | |
1457 | goto error; | |
1458 | } | |
1459 | ||
4e81bb83 MN |
1460 | for (pi = 0; pi < npols; pi++) { |
1461 | read_lock_bh(&pols[pi]->lock); | |
1462 | pol_dead |= pols[pi]->dead; | |
1463 | read_unlock_bh(&pols[pi]->lock); | |
1464 | } | |
1465 | ||
1da177e4 | 1466 | write_lock_bh(&policy->lock); |
4e81bb83 | 1467 | if (unlikely(pol_dead || stale_bundle(dst))) { |
1da177e4 LT |
1468 | /* Wow! While we worked on resolving, this |
1469 | * policy has gone. Retry. It is not paranoia, | |
1470 | * we just cannot enlist new bundle to dead object. | |
1471 | * We can't enlist stable bundles either. | |
1472 | */ | |
1473 | write_unlock_bh(&policy->lock); | |
1da177e4 LT |
1474 | if (dst) |
1475 | dst_free(dst); | |
00de651d HX |
1476 | |
1477 | err = -EHOSTUNREACH; | |
1478 | goto error; | |
1da177e4 | 1479 | } |
157bfc25 MN |
1480 | |
1481 | if (npols > 1) | |
1482 | err = xfrm_dst_update_parent(dst, &pols[1]->selector); | |
1483 | else | |
1484 | err = xfrm_dst_update_origin(dst, fl); | |
1485 | if (unlikely(err)) { | |
1486 | write_unlock_bh(&policy->lock); | |
1487 | if (dst) | |
1488 | dst_free(dst); | |
1489 | goto error; | |
1490 | } | |
1491 | ||
1da177e4 LT |
1492 | dst->next = policy->bundles; |
1493 | policy->bundles = dst; | |
1494 | dst_hold(dst); | |
1495 | write_unlock_bh(&policy->lock); | |
1496 | } | |
1497 | *dst_p = dst; | |
1498 | dst_release(dst_orig); | |
a716c119 | 1499 | xfrm_pols_put(pols, npols); |
1da177e4 LT |
1500 | return 0; |
1501 | ||
1502 | error: | |
1503 | dst_release(dst_orig); | |
4e81bb83 | 1504 | xfrm_pols_put(pols, npols); |
1da177e4 LT |
1505 | *dst_p = NULL; |
1506 | return err; | |
1507 | } | |
14e50e57 DM |
1508 | EXPORT_SYMBOL(__xfrm_lookup); |
1509 | ||
1510 | int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl, | |
1511 | struct sock *sk, int flags) | |
1512 | { | |
1513 | int err = __xfrm_lookup(dst_p, fl, sk, flags); | |
1514 | ||
1515 | if (err == -EREMOTE) { | |
1516 | dst_release(*dst_p); | |
1517 | *dst_p = NULL; | |
1518 | err = -EAGAIN; | |
1519 | } | |
1520 | ||
1521 | return err; | |
1522 | } | |
1da177e4 LT |
1523 | EXPORT_SYMBOL(xfrm_lookup); |
1524 | ||
df0ba92a MN |
1525 | static inline int |
1526 | xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl) | |
1527 | { | |
1528 | struct xfrm_state *x; | |
df0ba92a MN |
1529 | |
1530 | if (!skb->sp || idx < 0 || idx >= skb->sp->len) | |
1531 | return 0; | |
1532 | x = skb->sp->xvec[idx]; | |
1533 | if (!x->type->reject) | |
1534 | return 0; | |
1ecafede | 1535 | return x->type->reject(x, skb, fl); |
df0ba92a MN |
1536 | } |
1537 | ||
1da177e4 LT |
1538 | /* When skb is transformed back to its "native" form, we have to |
1539 | * check policy restrictions. At the moment we make this in maximally | |
1540 | * stupid way. Shame on me. :-) Of course, connected sockets must | |
1541 | * have policy cached at them. | |
1542 | */ | |
1543 | ||
1544 | static inline int | |
a716c119 | 1545 | xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x, |
1da177e4 LT |
1546 | unsigned short family) |
1547 | { | |
1548 | if (xfrm_state_kern(x)) | |
928ba416 | 1549 | return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family); |
1da177e4 LT |
1550 | return x->id.proto == tmpl->id.proto && |
1551 | (x->id.spi == tmpl->id.spi || !tmpl->id.spi) && | |
1552 | (x->props.reqid == tmpl->reqid || !tmpl->reqid) && | |
1553 | x->props.mode == tmpl->mode && | |
f3bd4840 MN |
1554 | ((tmpl->aalgos & (1<<x->props.aalgo)) || |
1555 | !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) && | |
7e49e6de MN |
1556 | !(x->props.mode != XFRM_MODE_TRANSPORT && |
1557 | xfrm_state_addr_cmp(tmpl, x, family)); | |
1da177e4 LT |
1558 | } |
1559 | ||
df0ba92a MN |
1560 | /* |
1561 | * 0 or more than 0 is returned when validation is succeeded (either bypass | |
1562 | * because of optional transport mode, or next index of the mathced secpath | |
1563 | * state with the template. | |
1564 | * -1 is returned when no matching template is found. | |
1565 | * Otherwise "-2 - errored_index" is returned. | |
1566 | */ | |
1da177e4 LT |
1567 | static inline int |
1568 | xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start, | |
1569 | unsigned short family) | |
1570 | { | |
1571 | int idx = start; | |
1572 | ||
1573 | if (tmpl->optional) { | |
7e49e6de | 1574 | if (tmpl->mode == XFRM_MODE_TRANSPORT) |
1da177e4 LT |
1575 | return start; |
1576 | } else | |
1577 | start = -1; | |
1578 | for (; idx < sp->len; idx++) { | |
dbe5b4aa | 1579 | if (xfrm_state_ok(tmpl, sp->xvec[idx], family)) |
1da177e4 | 1580 | return ++idx; |
df0ba92a MN |
1581 | if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) { |
1582 | if (start == -1) | |
1583 | start = -2-idx; | |
1da177e4 | 1584 | break; |
df0ba92a | 1585 | } |
1da177e4 LT |
1586 | } |
1587 | return start; | |
1588 | } | |
1589 | ||
3e3850e9 PM |
1590 | int |
1591 | xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family) | |
1da177e4 LT |
1592 | { |
1593 | struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family); | |
e0d1caa7 | 1594 | int err; |
1da177e4 LT |
1595 | |
1596 | if (unlikely(afinfo == NULL)) | |
1597 | return -EAFNOSUPPORT; | |
1598 | ||
1599 | afinfo->decode_session(skb, fl); | |
beb8d13b | 1600 | err = security_xfrm_decode_session(skb, &fl->secid); |
1da177e4 | 1601 | xfrm_policy_put_afinfo(afinfo); |
e0d1caa7 | 1602 | return err; |
1da177e4 | 1603 | } |
3e3850e9 | 1604 | EXPORT_SYMBOL(xfrm_decode_session); |
1da177e4 | 1605 | |
df0ba92a | 1606 | static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp) |
1da177e4 LT |
1607 | { |
1608 | for (; k < sp->len; k++) { | |
df0ba92a | 1609 | if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) { |
d1d9facf | 1610 | *idxp = k; |
1da177e4 | 1611 | return 1; |
df0ba92a | 1612 | } |
1da177e4 LT |
1613 | } |
1614 | ||
1615 | return 0; | |
1616 | } | |
1617 | ||
a716c119 | 1618 | int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, |
1da177e4 LT |
1619 | unsigned short family) |
1620 | { | |
1621 | struct xfrm_policy *pol; | |
4e81bb83 MN |
1622 | struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX]; |
1623 | int npols = 0; | |
1624 | int xfrm_nr; | |
1625 | int pi; | |
1da177e4 | 1626 | struct flowi fl; |
df71837d | 1627 | u8 fl_dir = policy_to_flow_dir(dir); |
df0ba92a | 1628 | int xerr_idx = -1; |
1da177e4 | 1629 | |
3e3850e9 | 1630 | if (xfrm_decode_session(skb, &fl, family) < 0) |
1da177e4 | 1631 | return 0; |
eb9c7ebe | 1632 | nf_nat_decode_session(skb, &fl, family); |
1da177e4 LT |
1633 | |
1634 | /* First, check used SA against their selectors. */ | |
1635 | if (skb->sp) { | |
1636 | int i; | |
1637 | ||
1638 | for (i=skb->sp->len-1; i>=0; i--) { | |
dbe5b4aa HX |
1639 | struct xfrm_state *x = skb->sp->xvec[i]; |
1640 | if (!xfrm_selector_match(&x->sel, &fl, family)) | |
1da177e4 | 1641 | return 0; |
1da177e4 LT |
1642 | } |
1643 | } | |
1644 | ||
1645 | pol = NULL; | |
3bccfbc7 | 1646 | if (sk && sk->sk_policy[dir]) { |
e0d1caa7 | 1647 | pol = xfrm_sk_policy_lookup(sk, dir, &fl); |
3bccfbc7 VY |
1648 | if (IS_ERR(pol)) |
1649 | return 0; | |
1650 | } | |
1da177e4 LT |
1651 | |
1652 | if (!pol) | |
e0d1caa7 | 1653 | pol = flow_cache_lookup(&fl, family, fl_dir, |
1da177e4 LT |
1654 | xfrm_policy_lookup); |
1655 | ||
134b0fc5 JM |
1656 | if (IS_ERR(pol)) |
1657 | return 0; | |
1658 | ||
df0ba92a | 1659 | if (!pol) { |
d1d9facf | 1660 | if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) { |
df0ba92a MN |
1661 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
1662 | return 0; | |
1663 | } | |
1664 | return 1; | |
1665 | } | |
1da177e4 | 1666 | |
9d729f72 | 1667 | pol->curlft.use_time = get_seconds(); |
1da177e4 | 1668 | |
4e81bb83 MN |
1669 | pols[0] = pol; |
1670 | npols ++; | |
1671 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1672 | if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) { | |
1673 | pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, | |
1674 | &fl, family, | |
1675 | XFRM_POLICY_IN); | |
1676 | if (pols[1]) { | |
134b0fc5 JM |
1677 | if (IS_ERR(pols[1])) |
1678 | return 0; | |
9d729f72 | 1679 | pols[1]->curlft.use_time = get_seconds(); |
4e81bb83 MN |
1680 | npols ++; |
1681 | } | |
1682 | } | |
1683 | #endif | |
1684 | ||
1da177e4 LT |
1685 | if (pol->action == XFRM_POLICY_ALLOW) { |
1686 | struct sec_path *sp; | |
1687 | static struct sec_path dummy; | |
4e81bb83 | 1688 | struct xfrm_tmpl *tp[XFRM_MAX_DEPTH]; |
41a49cc3 | 1689 | struct xfrm_tmpl *stp[XFRM_MAX_DEPTH]; |
4e81bb83 MN |
1690 | struct xfrm_tmpl **tpp = tp; |
1691 | int ti = 0; | |
1da177e4 LT |
1692 | int i, k; |
1693 | ||
1694 | if ((sp = skb->sp) == NULL) | |
1695 | sp = &dummy; | |
1696 | ||
4e81bb83 MN |
1697 | for (pi = 0; pi < npols; pi++) { |
1698 | if (pols[pi] != pol && | |
1699 | pols[pi]->action != XFRM_POLICY_ALLOW) | |
1700 | goto reject; | |
1701 | if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) | |
1702 | goto reject_error; | |
1703 | for (i = 0; i < pols[pi]->xfrm_nr; i++) | |
1704 | tpp[ti++] = &pols[pi]->xfrm_vec[i]; | |
1705 | } | |
1706 | xfrm_nr = ti; | |
41a49cc3 MN |
1707 | if (npols > 1) { |
1708 | xfrm_tmpl_sort(stp, tpp, xfrm_nr, family); | |
1709 | tpp = stp; | |
1710 | } | |
4e81bb83 | 1711 | |
1da177e4 LT |
1712 | /* For each tunnel xfrm, find the first matching tmpl. |
1713 | * For each tmpl before that, find corresponding xfrm. | |
1714 | * Order is _important_. Later we will implement | |
1715 | * some barriers, but at the moment barriers | |
1716 | * are implied between each two transformations. | |
1717 | */ | |
4e81bb83 MN |
1718 | for (i = xfrm_nr-1, k = 0; i >= 0; i--) { |
1719 | k = xfrm_policy_ok(tpp[i], sp, k, family); | |
df0ba92a | 1720 | if (k < 0) { |
d1d9facf JM |
1721 | if (k < -1) |
1722 | /* "-2 - errored_index" returned */ | |
1723 | xerr_idx = -(2+k); | |
1da177e4 | 1724 | goto reject; |
df0ba92a | 1725 | } |
1da177e4 LT |
1726 | } |
1727 | ||
d1d9facf | 1728 | if (secpath_has_nontransport(sp, k, &xerr_idx)) |
1da177e4 LT |
1729 | goto reject; |
1730 | ||
4e81bb83 | 1731 | xfrm_pols_put(pols, npols); |
1da177e4 LT |
1732 | return 1; |
1733 | } | |
1734 | ||
1735 | reject: | |
df0ba92a | 1736 | xfrm_secpath_reject(xerr_idx, skb, &fl); |
4e81bb83 MN |
1737 | reject_error: |
1738 | xfrm_pols_put(pols, npols); | |
1da177e4 LT |
1739 | return 0; |
1740 | } | |
1741 | EXPORT_SYMBOL(__xfrm_policy_check); | |
1742 | ||
1743 | int __xfrm_route_forward(struct sk_buff *skb, unsigned short family) | |
1744 | { | |
1745 | struct flowi fl; | |
1746 | ||
3e3850e9 | 1747 | if (xfrm_decode_session(skb, &fl, family) < 0) |
1da177e4 LT |
1748 | return 0; |
1749 | ||
1750 | return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0; | |
1751 | } | |
1752 | EXPORT_SYMBOL(__xfrm_route_forward); | |
1753 | ||
d49c73c7 DM |
1754 | /* Optimize later using cookies and generation ids. */ |
1755 | ||
1da177e4 LT |
1756 | static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie) |
1757 | { | |
d49c73c7 DM |
1758 | /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete |
1759 | * to "-1" to force all XFRM destinations to get validated by | |
1760 | * dst_ops->check on every use. We do this because when a | |
1761 | * normal route referenced by an XFRM dst is obsoleted we do | |
1762 | * not go looking around for all parent referencing XFRM dsts | |
1763 | * so that we can invalidate them. It is just too much work. | |
1764 | * Instead we make the checks here on every use. For example: | |
1765 | * | |
1766 | * XFRM dst A --> IPv4 dst X | |
1767 | * | |
1768 | * X is the "xdst->route" of A (X is also the "dst->path" of A | |
1769 | * in this example). If X is marked obsolete, "A" will not | |
1770 | * notice. That's what we are validating here via the | |
1771 | * stale_bundle() check. | |
1772 | * | |
1773 | * When a policy's bundle is pruned, we dst_free() the XFRM | |
1774 | * dst which causes it's ->obsolete field to be set to a | |
1775 | * positive non-zero integer. If an XFRM dst has been pruned | |
1776 | * like this, we want to force a new route lookup. | |
399c180a | 1777 | */ |
d49c73c7 DM |
1778 | if (dst->obsolete < 0 && !stale_bundle(dst)) |
1779 | return dst; | |
1780 | ||
1da177e4 LT |
1781 | return NULL; |
1782 | } | |
1783 | ||
1784 | static int stale_bundle(struct dst_entry *dst) | |
1785 | { | |
5b368e61 | 1786 | return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0); |
1da177e4 LT |
1787 | } |
1788 | ||
aabc9761 | 1789 | void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev) |
1da177e4 | 1790 | { |
1da177e4 | 1791 | while ((dst = dst->child) && dst->xfrm && dst->dev == dev) { |
2774c7ab | 1792 | dst->dev = init_net.loopback_dev; |
de3cb747 | 1793 | dev_hold(dst->dev); |
1da177e4 LT |
1794 | dev_put(dev); |
1795 | } | |
1796 | } | |
aabc9761 | 1797 | EXPORT_SYMBOL(xfrm_dst_ifdown); |
1da177e4 LT |
1798 | |
1799 | static void xfrm_link_failure(struct sk_buff *skb) | |
1800 | { | |
1801 | /* Impossible. Such dst must be popped before reaches point of failure. */ | |
1802 | return; | |
1803 | } | |
1804 | ||
1805 | static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst) | |
1806 | { | |
1807 | if (dst) { | |
1808 | if (dst->obsolete) { | |
1809 | dst_release(dst); | |
1810 | dst = NULL; | |
1811 | } | |
1812 | } | |
1813 | return dst; | |
1814 | } | |
1815 | ||
2518c7c2 DM |
1816 | static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p) |
1817 | { | |
1818 | struct dst_entry *dst, **dstp; | |
1819 | ||
1820 | write_lock(&pol->lock); | |
1821 | dstp = &pol->bundles; | |
1822 | while ((dst=*dstp) != NULL) { | |
1823 | if (func(dst)) { | |
1824 | *dstp = dst->next; | |
1825 | dst->next = *gc_list_p; | |
1826 | *gc_list_p = dst; | |
1827 | } else { | |
1828 | dstp = &dst->next; | |
1829 | } | |
1830 | } | |
1831 | write_unlock(&pol->lock); | |
1832 | } | |
1833 | ||
1da177e4 LT |
1834 | static void xfrm_prune_bundles(int (*func)(struct dst_entry *)) |
1835 | { | |
2518c7c2 DM |
1836 | struct dst_entry *gc_list = NULL; |
1837 | int dir; | |
1da177e4 LT |
1838 | |
1839 | read_lock_bh(&xfrm_policy_lock); | |
2518c7c2 DM |
1840 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { |
1841 | struct xfrm_policy *pol; | |
1842 | struct hlist_node *entry; | |
1843 | struct hlist_head *table; | |
1844 | int i; | |
4e81bb83 | 1845 | |
2518c7c2 DM |
1846 | hlist_for_each_entry(pol, entry, |
1847 | &xfrm_policy_inexact[dir], bydst) | |
1848 | prune_one_bundle(pol, func, &gc_list); | |
1849 | ||
1850 | table = xfrm_policy_bydst[dir].table; | |
1851 | for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) { | |
1852 | hlist_for_each_entry(pol, entry, table + i, bydst) | |
1853 | prune_one_bundle(pol, func, &gc_list); | |
1da177e4 LT |
1854 | } |
1855 | } | |
1856 | read_unlock_bh(&xfrm_policy_lock); | |
1857 | ||
1858 | while (gc_list) { | |
2518c7c2 | 1859 | struct dst_entry *dst = gc_list; |
1da177e4 LT |
1860 | gc_list = dst->next; |
1861 | dst_free(dst); | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | static int unused_bundle(struct dst_entry *dst) | |
1866 | { | |
1867 | return !atomic_read(&dst->__refcnt); | |
1868 | } | |
1869 | ||
1870 | static void __xfrm_garbage_collect(void) | |
1871 | { | |
1872 | xfrm_prune_bundles(unused_bundle); | |
1873 | } | |
1874 | ||
1c095399 | 1875 | static int xfrm_flush_bundles(void) |
1da177e4 LT |
1876 | { |
1877 | xfrm_prune_bundles(stale_bundle); | |
1878 | return 0; | |
1879 | } | |
1880 | ||
1881 | void xfrm_init_pmtu(struct dst_entry *dst) | |
1882 | { | |
1883 | do { | |
1884 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
1885 | u32 pmtu, route_mtu_cached; | |
1886 | ||
1887 | pmtu = dst_mtu(dst->child); | |
1888 | xdst->child_mtu_cached = pmtu; | |
1889 | ||
1890 | pmtu = xfrm_state_mtu(dst->xfrm, pmtu); | |
1891 | ||
1892 | route_mtu_cached = dst_mtu(xdst->route); | |
1893 | xdst->route_mtu_cached = route_mtu_cached; | |
1894 | ||
1895 | if (pmtu > route_mtu_cached) | |
1896 | pmtu = route_mtu_cached; | |
1897 | ||
1898 | dst->metrics[RTAX_MTU-1] = pmtu; | |
1899 | } while ((dst = dst->next)); | |
1900 | } | |
1901 | ||
1902 | EXPORT_SYMBOL(xfrm_init_pmtu); | |
1903 | ||
1904 | /* Check that the bundle accepts the flow and its components are | |
1905 | * still valid. | |
1906 | */ | |
1907 | ||
5b368e61 VY |
1908 | int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first, |
1909 | struct flowi *fl, int family, int strict) | |
1da177e4 LT |
1910 | { |
1911 | struct dst_entry *dst = &first->u.dst; | |
1912 | struct xfrm_dst *last; | |
1913 | u32 mtu; | |
1914 | ||
92d63dec | 1915 | if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) || |
1da177e4 LT |
1916 | (dst->dev && !netif_running(dst->dev))) |
1917 | return 0; | |
157bfc25 MN |
1918 | #ifdef CONFIG_XFRM_SUB_POLICY |
1919 | if (fl) { | |
1920 | if (first->origin && !flow_cache_uli_match(first->origin, fl)) | |
1921 | return 0; | |
1922 | if (first->partner && | |
1923 | !xfrm_selector_match(first->partner, fl, family)) | |
1924 | return 0; | |
1925 | } | |
1926 | #endif | |
1da177e4 LT |
1927 | |
1928 | last = NULL; | |
1929 | ||
1930 | do { | |
1931 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; | |
1932 | ||
1933 | if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family)) | |
1934 | return 0; | |
67f83cbf VY |
1935 | if (fl && pol && |
1936 | !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl)) | |
e0d1caa7 | 1937 | return 0; |
1da177e4 LT |
1938 | if (dst->xfrm->km.state != XFRM_STATE_VALID) |
1939 | return 0; | |
9d4a706d DM |
1940 | if (xdst->genid != dst->xfrm->genid) |
1941 | return 0; | |
e53820de MN |
1942 | |
1943 | if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL && | |
1944 | !xfrm_state_addr_flow_check(dst->xfrm, fl, family)) | |
1945 | return 0; | |
1da177e4 LT |
1946 | |
1947 | mtu = dst_mtu(dst->child); | |
1948 | if (xdst->child_mtu_cached != mtu) { | |
1949 | last = xdst; | |
1950 | xdst->child_mtu_cached = mtu; | |
1951 | } | |
1952 | ||
92d63dec | 1953 | if (!dst_check(xdst->route, xdst->route_cookie)) |
1da177e4 LT |
1954 | return 0; |
1955 | mtu = dst_mtu(xdst->route); | |
1956 | if (xdst->route_mtu_cached != mtu) { | |
1957 | last = xdst; | |
1958 | xdst->route_mtu_cached = mtu; | |
1959 | } | |
1960 | ||
1961 | dst = dst->child; | |
1962 | } while (dst->xfrm); | |
1963 | ||
1964 | if (likely(!last)) | |
1965 | return 1; | |
1966 | ||
1967 | mtu = last->child_mtu_cached; | |
1968 | for (;;) { | |
1969 | dst = &last->u.dst; | |
1970 | ||
1971 | mtu = xfrm_state_mtu(dst->xfrm, mtu); | |
1972 | if (mtu > last->route_mtu_cached) | |
1973 | mtu = last->route_mtu_cached; | |
1974 | dst->metrics[RTAX_MTU-1] = mtu; | |
1975 | ||
1976 | if (last == first) | |
1977 | break; | |
1978 | ||
bd0bf076 | 1979 | last = (struct xfrm_dst *)last->u.dst.next; |
1da177e4 LT |
1980 | last->child_mtu_cached = mtu; |
1981 | } | |
1982 | ||
1983 | return 1; | |
1984 | } | |
1985 | ||
1986 | EXPORT_SYMBOL(xfrm_bundle_ok); | |
1987 | ||
1da177e4 LT |
1988 | int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo) |
1989 | { | |
1990 | int err = 0; | |
1991 | if (unlikely(afinfo == NULL)) | |
1992 | return -EINVAL; | |
1993 | if (unlikely(afinfo->family >= NPROTO)) | |
1994 | return -EAFNOSUPPORT; | |
e959d812 | 1995 | write_lock_bh(&xfrm_policy_afinfo_lock); |
1da177e4 LT |
1996 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL)) |
1997 | err = -ENOBUFS; | |
1998 | else { | |
1999 | struct dst_ops *dst_ops = afinfo->dst_ops; | |
2000 | if (likely(dst_ops->kmem_cachep == NULL)) | |
2001 | dst_ops->kmem_cachep = xfrm_dst_cache; | |
2002 | if (likely(dst_ops->check == NULL)) | |
2003 | dst_ops->check = xfrm_dst_check; | |
1da177e4 LT |
2004 | if (likely(dst_ops->negative_advice == NULL)) |
2005 | dst_ops->negative_advice = xfrm_negative_advice; | |
2006 | if (likely(dst_ops->link_failure == NULL)) | |
2007 | dst_ops->link_failure = xfrm_link_failure; | |
1da177e4 LT |
2008 | if (likely(afinfo->garbage_collect == NULL)) |
2009 | afinfo->garbage_collect = __xfrm_garbage_collect; | |
2010 | xfrm_policy_afinfo[afinfo->family] = afinfo; | |
2011 | } | |
e959d812 | 2012 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
1da177e4 LT |
2013 | return err; |
2014 | } | |
2015 | EXPORT_SYMBOL(xfrm_policy_register_afinfo); | |
2016 | ||
2017 | int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) | |
2018 | { | |
2019 | int err = 0; | |
2020 | if (unlikely(afinfo == NULL)) | |
2021 | return -EINVAL; | |
2022 | if (unlikely(afinfo->family >= NPROTO)) | |
2023 | return -EAFNOSUPPORT; | |
e959d812 | 2024 | write_lock_bh(&xfrm_policy_afinfo_lock); |
1da177e4 LT |
2025 | if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) { |
2026 | if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo)) | |
2027 | err = -EINVAL; | |
2028 | else { | |
2029 | struct dst_ops *dst_ops = afinfo->dst_ops; | |
2030 | xfrm_policy_afinfo[afinfo->family] = NULL; | |
2031 | dst_ops->kmem_cachep = NULL; | |
2032 | dst_ops->check = NULL; | |
1da177e4 LT |
2033 | dst_ops->negative_advice = NULL; |
2034 | dst_ops->link_failure = NULL; | |
1da177e4 LT |
2035 | afinfo->garbage_collect = NULL; |
2036 | } | |
2037 | } | |
e959d812 | 2038 | write_unlock_bh(&xfrm_policy_afinfo_lock); |
1da177e4 LT |
2039 | return err; |
2040 | } | |
2041 | EXPORT_SYMBOL(xfrm_policy_unregister_afinfo); | |
2042 | ||
2043 | static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family) | |
2044 | { | |
2045 | struct xfrm_policy_afinfo *afinfo; | |
2046 | if (unlikely(family >= NPROTO)) | |
2047 | return NULL; | |
2048 | read_lock(&xfrm_policy_afinfo_lock); | |
2049 | afinfo = xfrm_policy_afinfo[family]; | |
546be240 HX |
2050 | if (unlikely(!afinfo)) |
2051 | read_unlock(&xfrm_policy_afinfo_lock); | |
1da177e4 LT |
2052 | return afinfo; |
2053 | } | |
2054 | ||
2055 | static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo) | |
2056 | { | |
546be240 HX |
2057 | read_unlock(&xfrm_policy_afinfo_lock); |
2058 | } | |
2059 | ||
1da177e4 LT |
2060 | static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr) |
2061 | { | |
e9dc8653 EB |
2062 | struct net_device *dev = ptr; |
2063 | ||
2064 | if (dev->nd_net != &init_net) | |
2065 | return NOTIFY_DONE; | |
2066 | ||
1da177e4 LT |
2067 | switch (event) { |
2068 | case NETDEV_DOWN: | |
2069 | xfrm_flush_bundles(); | |
2070 | } | |
2071 | return NOTIFY_DONE; | |
2072 | } | |
2073 | ||
2074 | static struct notifier_block xfrm_dev_notifier = { | |
2075 | xfrm_dev_event, | |
2076 | NULL, | |
2077 | 0 | |
2078 | }; | |
2079 | ||
2080 | static void __init xfrm_policy_init(void) | |
2081 | { | |
2518c7c2 DM |
2082 | unsigned int hmask, sz; |
2083 | int dir; | |
2084 | ||
1da177e4 LT |
2085 | xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache", |
2086 | sizeof(struct xfrm_dst), | |
e5d679f3 | 2087 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
20c2df83 | 2088 | NULL); |
1da177e4 | 2089 | |
2518c7c2 DM |
2090 | hmask = 8 - 1; |
2091 | sz = (hmask+1) * sizeof(struct hlist_head); | |
2092 | ||
44e36b42 | 2093 | xfrm_policy_byidx = xfrm_hash_alloc(sz); |
2518c7c2 DM |
2094 | xfrm_idx_hmask = hmask; |
2095 | if (!xfrm_policy_byidx) | |
2096 | panic("XFRM: failed to allocate byidx hash\n"); | |
2097 | ||
2098 | for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) { | |
2099 | struct xfrm_policy_hash *htab; | |
2100 | ||
2101 | INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]); | |
2102 | ||
2103 | htab = &xfrm_policy_bydst[dir]; | |
44e36b42 | 2104 | htab->table = xfrm_hash_alloc(sz); |
2518c7c2 DM |
2105 | htab->hmask = hmask; |
2106 | if (!htab->table) | |
2107 | panic("XFRM: failed to allocate bydst hash\n"); | |
2108 | } | |
2109 | ||
c4028958 | 2110 | INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task); |
1da177e4 LT |
2111 | register_netdevice_notifier(&xfrm_dev_notifier); |
2112 | } | |
2113 | ||
2114 | void __init xfrm_init(void) | |
2115 | { | |
2116 | xfrm_state_init(); | |
2117 | xfrm_policy_init(); | |
2118 | xfrm_input_init(); | |
2119 | } | |
2120 | ||
ab5f5e8b JL |
2121 | #ifdef CONFIG_AUDITSYSCALL |
2122 | static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp, | |
2123 | struct audit_buffer *audit_buf) | |
2124 | { | |
2125 | if (xp->security) | |
2126 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", | |
2127 | xp->security->ctx_alg, xp->security->ctx_doi, | |
2128 | xp->security->ctx_str); | |
2129 | ||
2130 | switch(xp->selector.family) { | |
2131 | case AF_INET: | |
2132 | audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u", | |
2133 | NIPQUAD(xp->selector.saddr.a4), | |
2134 | NIPQUAD(xp->selector.daddr.a4)); | |
2135 | break; | |
2136 | case AF_INET6: | |
2137 | { | |
2138 | struct in6_addr saddr6, daddr6; | |
2139 | ||
2140 | memcpy(&saddr6, xp->selector.saddr.a6, | |
2141 | sizeof(struct in6_addr)); | |
2142 | memcpy(&daddr6, xp->selector.daddr.a6, | |
2143 | sizeof(struct in6_addr)); | |
2144 | audit_log_format(audit_buf, | |
2145 | " src=" NIP6_FMT " dst=" NIP6_FMT, | |
2146 | NIP6(saddr6), NIP6(daddr6)); | |
2147 | } | |
2148 | break; | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | void | |
2153 | xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid) | |
2154 | { | |
2155 | struct audit_buffer *audit_buf; | |
2156 | extern int audit_enabled; | |
2157 | ||
2158 | if (audit_enabled == 0) | |
2159 | return; | |
2160 | audit_buf = xfrm_audit_start(sid, auid); | |
2161 | if (audit_buf == NULL) | |
2162 | return; | |
2163 | audit_log_format(audit_buf, " op=SPD-add res=%u", result); | |
2164 | xfrm_audit_common_policyinfo(xp, audit_buf); | |
2165 | audit_log_end(audit_buf); | |
2166 | } | |
2167 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_add); | |
2168 | ||
2169 | void | |
2170 | xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid) | |
2171 | { | |
2172 | struct audit_buffer *audit_buf; | |
2173 | extern int audit_enabled; | |
2174 | ||
2175 | if (audit_enabled == 0) | |
2176 | return; | |
2177 | audit_buf = xfrm_audit_start(sid, auid); | |
2178 | if (audit_buf == NULL) | |
2179 | return; | |
2180 | audit_log_format(audit_buf, " op=SPD-delete res=%u", result); | |
2181 | xfrm_audit_common_policyinfo(xp, audit_buf); | |
2182 | audit_log_end(audit_buf); | |
2183 | } | |
2184 | EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete); | |
2185 | #endif | |
2186 | ||
80c9abaa SS |
2187 | #ifdef CONFIG_XFRM_MIGRATE |
2188 | static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp, | |
2189 | struct xfrm_selector *sel_tgt) | |
2190 | { | |
2191 | if (sel_cmp->proto == IPSEC_ULPROTO_ANY) { | |
2192 | if (sel_tgt->family == sel_cmp->family && | |
2193 | xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr, | |
a716c119 | 2194 | sel_cmp->family) == 0 && |
80c9abaa SS |
2195 | xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr, |
2196 | sel_cmp->family) == 0 && | |
2197 | sel_tgt->prefixlen_d == sel_cmp->prefixlen_d && | |
2198 | sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) { | |
2199 | return 1; | |
2200 | } | |
2201 | } else { | |
2202 | if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) { | |
2203 | return 1; | |
2204 | } | |
2205 | } | |
2206 | return 0; | |
2207 | } | |
2208 | ||
2209 | static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel, | |
2210 | u8 dir, u8 type) | |
2211 | { | |
2212 | struct xfrm_policy *pol, *ret = NULL; | |
2213 | struct hlist_node *entry; | |
2214 | struct hlist_head *chain; | |
2215 | u32 priority = ~0U; | |
2216 | ||
2217 | read_lock_bh(&xfrm_policy_lock); | |
2218 | chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir); | |
2219 | hlist_for_each_entry(pol, entry, chain, bydst) { | |
2220 | if (xfrm_migrate_selector_match(sel, &pol->selector) && | |
2221 | pol->type == type) { | |
2222 | ret = pol; | |
2223 | priority = ret->priority; | |
2224 | break; | |
2225 | } | |
2226 | } | |
2227 | chain = &xfrm_policy_inexact[dir]; | |
2228 | hlist_for_each_entry(pol, entry, chain, bydst) { | |
2229 | if (xfrm_migrate_selector_match(sel, &pol->selector) && | |
2230 | pol->type == type && | |
2231 | pol->priority < priority) { | |
2232 | ret = pol; | |
2233 | break; | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | if (ret) | |
2238 | xfrm_pol_hold(ret); | |
2239 | ||
2240 | read_unlock_bh(&xfrm_policy_lock); | |
2241 | ||
2242 | return ret; | |
2243 | } | |
2244 | ||
2245 | static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t) | |
2246 | { | |
2247 | int match = 0; | |
2248 | ||
2249 | if (t->mode == m->mode && t->id.proto == m->proto && | |
2250 | (m->reqid == 0 || t->reqid == m->reqid)) { | |
2251 | switch (t->mode) { | |
2252 | case XFRM_MODE_TUNNEL: | |
2253 | case XFRM_MODE_BEET: | |
2254 | if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr, | |
2255 | m->old_family) == 0 && | |
2256 | xfrm_addr_cmp(&t->saddr, &m->old_saddr, | |
2257 | m->old_family) == 0) { | |
2258 | match = 1; | |
2259 | } | |
2260 | break; | |
2261 | case XFRM_MODE_TRANSPORT: | |
2262 | /* in case of transport mode, template does not store | |
2263 | any IP addresses, hence we just compare mode and | |
2264 | protocol */ | |
2265 | match = 1; | |
2266 | break; | |
2267 | default: | |
2268 | break; | |
2269 | } | |
2270 | } | |
2271 | return match; | |
2272 | } | |
2273 | ||
2274 | /* update endpoint address(es) of template(s) */ | |
2275 | static int xfrm_policy_migrate(struct xfrm_policy *pol, | |
2276 | struct xfrm_migrate *m, int num_migrate) | |
2277 | { | |
2278 | struct xfrm_migrate *mp; | |
2279 | struct dst_entry *dst; | |
2280 | int i, j, n = 0; | |
2281 | ||
2282 | write_lock_bh(&pol->lock); | |
2283 | if (unlikely(pol->dead)) { | |
2284 | /* target policy has been deleted */ | |
2285 | write_unlock_bh(&pol->lock); | |
2286 | return -ENOENT; | |
2287 | } | |
2288 | ||
2289 | for (i = 0; i < pol->xfrm_nr; i++) { | |
2290 | for (j = 0, mp = m; j < num_migrate; j++, mp++) { | |
2291 | if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i])) | |
2292 | continue; | |
2293 | n++; | |
2294 | if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL) | |
2295 | continue; | |
2296 | /* update endpoints */ | |
2297 | memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr, | |
2298 | sizeof(pol->xfrm_vec[i].id.daddr)); | |
2299 | memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr, | |
2300 | sizeof(pol->xfrm_vec[i].saddr)); | |
2301 | pol->xfrm_vec[i].encap_family = mp->new_family; | |
2302 | /* flush bundles */ | |
2303 | while ((dst = pol->bundles) != NULL) { | |
2304 | pol->bundles = dst->next; | |
2305 | dst_free(dst); | |
2306 | } | |
2307 | } | |
2308 | } | |
2309 | ||
2310 | write_unlock_bh(&pol->lock); | |
2311 | ||
2312 | if (!n) | |
2313 | return -ENODATA; | |
2314 | ||
2315 | return 0; | |
2316 | } | |
2317 | ||
2318 | static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate) | |
2319 | { | |
2320 | int i, j; | |
2321 | ||
2322 | if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH) | |
2323 | return -EINVAL; | |
2324 | ||
2325 | for (i = 0; i < num_migrate; i++) { | |
2326 | if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr, | |
2327 | m[i].old_family) == 0) && | |
2328 | (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr, | |
2329 | m[i].old_family) == 0)) | |
2330 | return -EINVAL; | |
2331 | if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) || | |
2332 | xfrm_addr_any(&m[i].new_saddr, m[i].new_family)) | |
2333 | return -EINVAL; | |
2334 | ||
2335 | /* check if there is any duplicated entry */ | |
2336 | for (j = i + 1; j < num_migrate; j++) { | |
2337 | if (!memcmp(&m[i].old_daddr, &m[j].old_daddr, | |
2338 | sizeof(m[i].old_daddr)) && | |
2339 | !memcmp(&m[i].old_saddr, &m[j].old_saddr, | |
2340 | sizeof(m[i].old_saddr)) && | |
2341 | m[i].proto == m[j].proto && | |
2342 | m[i].mode == m[j].mode && | |
2343 | m[i].reqid == m[j].reqid && | |
2344 | m[i].old_family == m[j].old_family) | |
2345 | return -EINVAL; | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | return 0; | |
2350 | } | |
2351 | ||
2352 | int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
2353 | struct xfrm_migrate *m, int num_migrate) | |
2354 | { | |
2355 | int i, err, nx_cur = 0, nx_new = 0; | |
2356 | struct xfrm_policy *pol = NULL; | |
2357 | struct xfrm_state *x, *xc; | |
2358 | struct xfrm_state *x_cur[XFRM_MAX_DEPTH]; | |
2359 | struct xfrm_state *x_new[XFRM_MAX_DEPTH]; | |
2360 | struct xfrm_migrate *mp; | |
2361 | ||
2362 | if ((err = xfrm_migrate_check(m, num_migrate)) < 0) | |
2363 | goto out; | |
2364 | ||
2365 | /* Stage 1 - find policy */ | |
2366 | if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) { | |
2367 | err = -ENOENT; | |
2368 | goto out; | |
2369 | } | |
2370 | ||
2371 | /* Stage 2 - find and update state(s) */ | |
2372 | for (i = 0, mp = m; i < num_migrate; i++, mp++) { | |
2373 | if ((x = xfrm_migrate_state_find(mp))) { | |
2374 | x_cur[nx_cur] = x; | |
2375 | nx_cur++; | |
2376 | if ((xc = xfrm_state_migrate(x, mp))) { | |
2377 | x_new[nx_new] = xc; | |
2378 | nx_new++; | |
2379 | } else { | |
2380 | err = -ENODATA; | |
2381 | goto restore_state; | |
2382 | } | |
2383 | } | |
2384 | } | |
2385 | ||
2386 | /* Stage 3 - update policy */ | |
2387 | if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0) | |
2388 | goto restore_state; | |
2389 | ||
2390 | /* Stage 4 - delete old state(s) */ | |
2391 | if (nx_cur) { | |
2392 | xfrm_states_put(x_cur, nx_cur); | |
2393 | xfrm_states_delete(x_cur, nx_cur); | |
2394 | } | |
2395 | ||
2396 | /* Stage 5 - announce */ | |
2397 | km_migrate(sel, dir, type, m, num_migrate); | |
2398 | ||
2399 | xfrm_pol_put(pol); | |
2400 | ||
2401 | return 0; | |
2402 | out: | |
2403 | return err; | |
2404 | ||
2405 | restore_state: | |
2406 | if (pol) | |
2407 | xfrm_pol_put(pol); | |
2408 | if (nx_cur) | |
2409 | xfrm_states_put(x_cur, nx_cur); | |
2410 | if (nx_new) | |
2411 | xfrm_states_delete(x_new, nx_new); | |
2412 | ||
2413 | return err; | |
2414 | } | |
e610e679 | 2415 | EXPORT_SYMBOL(xfrm_migrate); |
80c9abaa | 2416 | #endif |