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