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