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