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