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