]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * xfrm_state.c | |
4 | * | |
5 | * Changes: | |
6 | * Mitsuru KANDA @USAGI | |
7 | * Kazunori MIYAZAWA @USAGI | |
8 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> | |
9 | * IPv6 support | |
10 | * YOSHIFUJI Hideaki @USAGI | |
11 | * Split up af-specific functions | |
12 | * Derek Atkins <derek@ihtfp.com> | |
13 | * Add UDP Encapsulation | |
df71837d | 14 | * |
1da177e4 LT |
15 | */ |
16 | ||
17 | #include <linux/workqueue.h> | |
18 | #include <net/xfrm.h> | |
19 | #include <linux/pfkeyv2.h> | |
20 | #include <linux/ipsec.h> | |
21 | #include <linux/module.h> | |
f034b5d4 | 22 | #include <linux/cache.h> |
68277acc | 23 | #include <linux/audit.h> |
7c0f6ba6 | 24 | #include <linux/uaccess.h> |
9e0d57fd | 25 | #include <linux/ktime.h> |
5a0e3ad6 | 26 | #include <linux/slab.h> |
9e0d57fd YP |
27 | #include <linux/interrupt.h> |
28 | #include <linux/kernel.h> | |
1da177e4 | 29 | |
44e36b42 DM |
30 | #include "xfrm_hash.h" |
31 | ||
c8406998 FW |
32 | #define xfrm_state_deref_prot(table, net) \ |
33 | rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock)) | |
34 | ||
35db57bb FW |
35 | static void xfrm_state_gc_task(struct work_struct *work); |
36 | ||
1da177e4 LT |
37 | /* Each xfrm_state may be linked to two tables: |
38 | ||
39 | 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl) | |
a624c108 | 40 | 2. Hash table by (daddr,family,reqid) to find what SAs exist for given |
1da177e4 LT |
41 | destination/tunnel endpoint. (output) |
42 | */ | |
43 | ||
f034b5d4 | 44 | static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024; |
b65e3d7b | 45 | static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation); |
565f0fa9 | 46 | static struct kmem_cache *xfrm_state_cache __ro_after_init; |
f034b5d4 | 47 | |
35db57bb FW |
48 | static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task); |
49 | static HLIST_HEAD(xfrm_state_gc_list); | |
50 | ||
02efdff7 FW |
51 | static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x) |
52 | { | |
88755e9c | 53 | return refcount_inc_not_zero(&x->refcnt); |
02efdff7 FW |
54 | } |
55 | ||
64d0cd00 | 56 | static inline unsigned int xfrm_dst_hash(struct net *net, |
2ab38503 DM |
57 | const xfrm_address_t *daddr, |
58 | const xfrm_address_t *saddr, | |
c1969f29 | 59 | u32 reqid, |
a624c108 | 60 | unsigned short family) |
f034b5d4 | 61 | { |
64d0cd00 | 62 | return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask); |
f034b5d4 DM |
63 | } |
64 | ||
64d0cd00 | 65 | static inline unsigned int xfrm_src_hash(struct net *net, |
2ab38503 DM |
66 | const xfrm_address_t *daddr, |
67 | const xfrm_address_t *saddr, | |
44e36b42 | 68 | unsigned short family) |
f034b5d4 | 69 | { |
64d0cd00 | 70 | return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask); |
f034b5d4 DM |
71 | } |
72 | ||
f034b5d4 | 73 | static inline unsigned int |
2ab38503 DM |
74 | xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr, |
75 | __be32 spi, u8 proto, unsigned short family) | |
f034b5d4 | 76 | { |
64d0cd00 | 77 | return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask); |
f034b5d4 DM |
78 | } |
79 | ||
f034b5d4 DM |
80 | static void xfrm_hash_transfer(struct hlist_head *list, |
81 | struct hlist_head *ndsttable, | |
82 | struct hlist_head *nsrctable, | |
83 | struct hlist_head *nspitable, | |
84 | unsigned int nhashmask) | |
85 | { | |
b67bfe0d | 86 | struct hlist_node *tmp; |
f034b5d4 DM |
87 | struct xfrm_state *x; |
88 | ||
b67bfe0d | 89 | hlist_for_each_entry_safe(x, tmp, list, bydst) { |
f034b5d4 DM |
90 | unsigned int h; |
91 | ||
c1969f29 DM |
92 | h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr, |
93 | x->props.reqid, x->props.family, | |
94 | nhashmask); | |
ae3fb6d3 | 95 | hlist_add_head_rcu(&x->bydst, ndsttable + h); |
f034b5d4 | 96 | |
667bbcb6 MN |
97 | h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr, |
98 | x->props.family, | |
f034b5d4 | 99 | nhashmask); |
ae3fb6d3 | 100 | hlist_add_head_rcu(&x->bysrc, nsrctable + h); |
f034b5d4 | 101 | |
7b4dc360 MN |
102 | if (x->id.spi) { |
103 | h = __xfrm_spi_hash(&x->id.daddr, x->id.spi, | |
104 | x->id.proto, x->props.family, | |
105 | nhashmask); | |
ae3fb6d3 | 106 | hlist_add_head_rcu(&x->byspi, nspitable + h); |
7b4dc360 | 107 | } |
f034b5d4 DM |
108 | } |
109 | } | |
110 | ||
63082733 | 111 | static unsigned long xfrm_hash_new_size(unsigned int state_hmask) |
f034b5d4 | 112 | { |
63082733 | 113 | return ((state_hmask + 1) << 1) * sizeof(struct hlist_head); |
f034b5d4 DM |
114 | } |
115 | ||
63082733 | 116 | static void xfrm_hash_resize(struct work_struct *work) |
f034b5d4 | 117 | { |
63082733 | 118 | struct net *net = container_of(work, struct net, xfrm.state_hash_work); |
f034b5d4 DM |
119 | struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi; |
120 | unsigned long nsize, osize; | |
121 | unsigned int nhashmask, ohashmask; | |
122 | int i; | |
123 | ||
63082733 | 124 | nsize = xfrm_hash_new_size(net->xfrm.state_hmask); |
44e36b42 | 125 | ndst = xfrm_hash_alloc(nsize); |
f034b5d4 | 126 | if (!ndst) |
0244790c | 127 | return; |
44e36b42 | 128 | nsrc = xfrm_hash_alloc(nsize); |
f034b5d4 | 129 | if (!nsrc) { |
44e36b42 | 130 | xfrm_hash_free(ndst, nsize); |
0244790c | 131 | return; |
f034b5d4 | 132 | } |
44e36b42 | 133 | nspi = xfrm_hash_alloc(nsize); |
f034b5d4 | 134 | if (!nspi) { |
44e36b42 DM |
135 | xfrm_hash_free(ndst, nsize); |
136 | xfrm_hash_free(nsrc, nsize); | |
0244790c | 137 | return; |
f034b5d4 DM |
138 | } |
139 | ||
283bc9f3 | 140 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
b65e3d7b | 141 | write_seqcount_begin(&xfrm_state_hash_generation); |
f034b5d4 DM |
142 | |
143 | nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; | |
c8406998 | 144 | odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net); |
63082733 | 145 | for (i = net->xfrm.state_hmask; i >= 0; i--) |
c8406998 | 146 | xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask); |
f034b5d4 | 147 | |
c8406998 FW |
148 | osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net); |
149 | ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net); | |
63082733 | 150 | ohashmask = net->xfrm.state_hmask; |
f034b5d4 | 151 | |
c8406998 FW |
152 | rcu_assign_pointer(net->xfrm.state_bydst, ndst); |
153 | rcu_assign_pointer(net->xfrm.state_bysrc, nsrc); | |
154 | rcu_assign_pointer(net->xfrm.state_byspi, nspi); | |
63082733 | 155 | net->xfrm.state_hmask = nhashmask; |
f034b5d4 | 156 | |
b65e3d7b | 157 | write_seqcount_end(&xfrm_state_hash_generation); |
283bc9f3 | 158 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
f034b5d4 DM |
159 | |
160 | osize = (ohashmask + 1) * sizeof(struct hlist_head); | |
df7274eb FW |
161 | |
162 | synchronize_rcu(); | |
163 | ||
44e36b42 DM |
164 | xfrm_hash_free(odst, osize); |
165 | xfrm_hash_free(osrc, osize); | |
166 | xfrm_hash_free(ospi, osize); | |
f034b5d4 DM |
167 | } |
168 | ||
44abdc30 CW |
169 | static DEFINE_SPINLOCK(xfrm_state_afinfo_lock); |
170 | static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO]; | |
1da177e4 | 171 | |
1da177e4 LT |
172 | static DEFINE_SPINLOCK(xfrm_state_gc_lock); |
173 | ||
53bc6b4d | 174 | int __xfrm_state_delete(struct xfrm_state *x); |
1da177e4 | 175 | |
980ebd25 | 176 | int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol); |
bb9cd077 | 177 | static bool km_is_alive(const struct km_event *c); |
15e47304 | 178 | void km_state_expired(struct xfrm_state *x, int hard, u32 portid); |
1da177e4 | 179 | |
7a9885b9 | 180 | static DEFINE_SPINLOCK(xfrm_type_lock); |
533cb5b0 | 181 | int xfrm_register_type(const struct xfrm_type *type, unsigned short family) |
aa5d62cc | 182 | { |
7a9885b9 | 183 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
533cb5b0 | 184 | const struct xfrm_type **typemap; |
aa5d62cc HX |
185 | int err = 0; |
186 | ||
187 | if (unlikely(afinfo == NULL)) | |
188 | return -EAFNOSUPPORT; | |
189 | typemap = afinfo->type_map; | |
7a9885b9 | 190 | spin_lock_bh(&xfrm_type_lock); |
aa5d62cc HX |
191 | |
192 | if (likely(typemap[type->proto] == NULL)) | |
193 | typemap[type->proto] = type; | |
194 | else | |
195 | err = -EEXIST; | |
7a9885b9 | 196 | spin_unlock_bh(&xfrm_type_lock); |
af5d27c4 | 197 | rcu_read_unlock(); |
aa5d62cc HX |
198 | return err; |
199 | } | |
200 | EXPORT_SYMBOL(xfrm_register_type); | |
201 | ||
533cb5b0 | 202 | int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family) |
aa5d62cc | 203 | { |
7a9885b9 | 204 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); |
533cb5b0 | 205 | const struct xfrm_type **typemap; |
aa5d62cc HX |
206 | int err = 0; |
207 | ||
208 | if (unlikely(afinfo == NULL)) | |
209 | return -EAFNOSUPPORT; | |
210 | typemap = afinfo->type_map; | |
7a9885b9 | 211 | spin_lock_bh(&xfrm_type_lock); |
aa5d62cc HX |
212 | |
213 | if (unlikely(typemap[type->proto] != type)) | |
214 | err = -ENOENT; | |
215 | else | |
216 | typemap[type->proto] = NULL; | |
7a9885b9 | 217 | spin_unlock_bh(&xfrm_type_lock); |
af5d27c4 | 218 | rcu_read_unlock(); |
aa5d62cc HX |
219 | return err; |
220 | } | |
221 | EXPORT_SYMBOL(xfrm_unregister_type); | |
222 | ||
533cb5b0 | 223 | static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family) |
aa5d62cc HX |
224 | { |
225 | struct xfrm_state_afinfo *afinfo; | |
533cb5b0 ED |
226 | const struct xfrm_type **typemap; |
227 | const struct xfrm_type *type; | |
aa5d62cc HX |
228 | int modload_attempted = 0; |
229 | ||
230 | retry: | |
231 | afinfo = xfrm_state_get_afinfo(family); | |
232 | if (unlikely(afinfo == NULL)) | |
233 | return NULL; | |
234 | typemap = afinfo->type_map; | |
235 | ||
75cda62d | 236 | type = READ_ONCE(typemap[proto]); |
aa5d62cc HX |
237 | if (unlikely(type && !try_module_get(type->owner))) |
238 | type = NULL; | |
75cda62d FW |
239 | |
240 | rcu_read_unlock(); | |
241 | ||
aa5d62cc | 242 | if (!type && !modload_attempted) { |
aa5d62cc HX |
243 | request_module("xfrm-type-%d-%d", family, proto); |
244 | modload_attempted = 1; | |
245 | goto retry; | |
246 | } | |
247 | ||
aa5d62cc HX |
248 | return type; |
249 | } | |
250 | ||
533cb5b0 | 251 | static void xfrm_put_type(const struct xfrm_type *type) |
aa5d62cc HX |
252 | { |
253 | module_put(type->owner); | |
254 | } | |
255 | ||
9d389d7f SK |
256 | static DEFINE_SPINLOCK(xfrm_type_offload_lock); |
257 | int xfrm_register_type_offload(const struct xfrm_type_offload *type, | |
258 | unsigned short family) | |
259 | { | |
260 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); | |
261 | const struct xfrm_type_offload **typemap; | |
262 | int err = 0; | |
263 | ||
264 | if (unlikely(afinfo == NULL)) | |
265 | return -EAFNOSUPPORT; | |
266 | typemap = afinfo->type_offload_map; | |
267 | spin_lock_bh(&xfrm_type_offload_lock); | |
268 | ||
269 | if (likely(typemap[type->proto] == NULL)) | |
270 | typemap[type->proto] = type; | |
271 | else | |
272 | err = -EEXIST; | |
273 | spin_unlock_bh(&xfrm_type_offload_lock); | |
274 | rcu_read_unlock(); | |
275 | return err; | |
276 | } | |
277 | EXPORT_SYMBOL(xfrm_register_type_offload); | |
278 | ||
279 | int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, | |
280 | unsigned short family) | |
281 | { | |
282 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); | |
283 | const struct xfrm_type_offload **typemap; | |
284 | int err = 0; | |
285 | ||
286 | if (unlikely(afinfo == NULL)) | |
287 | return -EAFNOSUPPORT; | |
288 | typemap = afinfo->type_offload_map; | |
289 | spin_lock_bh(&xfrm_type_offload_lock); | |
290 | ||
291 | if (unlikely(typemap[type->proto] != type)) | |
292 | err = -ENOENT; | |
293 | else | |
294 | typemap[type->proto] = NULL; | |
295 | spin_unlock_bh(&xfrm_type_offload_lock); | |
296 | rcu_read_unlock(); | |
297 | return err; | |
298 | } | |
299 | EXPORT_SYMBOL(xfrm_unregister_type_offload); | |
300 | ||
ffdb5211 IT |
301 | static const struct xfrm_type_offload * |
302 | xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load) | |
9d389d7f SK |
303 | { |
304 | struct xfrm_state_afinfo *afinfo; | |
305 | const struct xfrm_type_offload **typemap; | |
306 | const struct xfrm_type_offload *type; | |
307 | ||
ffdb5211 | 308 | retry: |
9d389d7f SK |
309 | afinfo = xfrm_state_get_afinfo(family); |
310 | if (unlikely(afinfo == NULL)) | |
311 | return NULL; | |
312 | typemap = afinfo->type_offload_map; | |
313 | ||
314 | type = typemap[proto]; | |
315 | if ((type && !try_module_get(type->owner))) | |
316 | type = NULL; | |
317 | ||
2f10a61c SD |
318 | rcu_read_unlock(); |
319 | ||
ffdb5211 IT |
320 | if (!type && try_load) { |
321 | request_module("xfrm-offload-%d-%d", family, proto); | |
545d8ae7 | 322 | try_load = false; |
ffdb5211 IT |
323 | goto retry; |
324 | } | |
325 | ||
9d389d7f SK |
326 | return type; |
327 | } | |
328 | ||
329 | static void xfrm_put_type_offload(const struct xfrm_type_offload *type) | |
330 | { | |
331 | module_put(type->owner); | |
332 | } | |
333 | ||
4c145dce FW |
334 | static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = { |
335 | [XFRM_MODE_BEET] = { | |
336 | .encap = XFRM_MODE_BEET, | |
337 | .flags = XFRM_MODE_FLAG_TUNNEL, | |
338 | .family = AF_INET, | |
339 | }, | |
340 | [XFRM_MODE_TRANSPORT] = { | |
341 | .encap = XFRM_MODE_TRANSPORT, | |
342 | .family = AF_INET, | |
343 | }, | |
344 | [XFRM_MODE_TUNNEL] = { | |
345 | .encap = XFRM_MODE_TUNNEL, | |
346 | .flags = XFRM_MODE_FLAG_TUNNEL, | |
347 | .family = AF_INET, | |
348 | }, | |
349 | }; | |
350 | ||
351 | static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = { | |
352 | [XFRM_MODE_BEET] = { | |
353 | .encap = XFRM_MODE_BEET, | |
354 | .flags = XFRM_MODE_FLAG_TUNNEL, | |
355 | .family = AF_INET6, | |
356 | }, | |
357 | [XFRM_MODE_ROUTEOPTIMIZATION] = { | |
358 | .encap = XFRM_MODE_ROUTEOPTIMIZATION, | |
359 | .family = AF_INET6, | |
360 | }, | |
361 | [XFRM_MODE_TRANSPORT] = { | |
362 | .encap = XFRM_MODE_TRANSPORT, | |
363 | .family = AF_INET6, | |
364 | }, | |
365 | [XFRM_MODE_TUNNEL] = { | |
366 | .encap = XFRM_MODE_TUNNEL, | |
367 | .flags = XFRM_MODE_FLAG_TUNNEL, | |
368 | .family = AF_INET6, | |
369 | }, | |
370 | }; | |
371 | ||
372 | static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family) | |
373 | { | |
374 | const struct xfrm_mode *mode; | |
aa5d62cc HX |
375 | |
376 | if (unlikely(encap >= XFRM_MODE_MAX)) | |
377 | return NULL; | |
378 | ||
4c145dce FW |
379 | switch (family) { |
380 | case AF_INET: | |
381 | mode = &xfrm4_mode_map[encap]; | |
382 | if (mode->family == family) | |
383 | return mode; | |
384 | break; | |
385 | case AF_INET6: | |
386 | mode = &xfrm6_mode_map[encap]; | |
387 | if (mode->family == family) | |
388 | return mode; | |
389 | break; | |
390 | default: | |
391 | break; | |
aa5d62cc HX |
392 | } |
393 | ||
4c145dce | 394 | return NULL; |
aa5d62cc HX |
395 | } |
396 | ||
4a135e53 MK |
397 | void xfrm_state_free(struct xfrm_state *x) |
398 | { | |
399 | kmem_cache_free(xfrm_state_cache, x); | |
400 | } | |
401 | EXPORT_SYMBOL(xfrm_state_free); | |
402 | ||
f75a2804 | 403 | static void ___xfrm_state_destroy(struct xfrm_state *x) |
1da177e4 | 404 | { |
671422b2 | 405 | hrtimer_cancel(&x->mtimer); |
a47f0ce0 | 406 | del_timer_sync(&x->rtimer); |
b5884793 | 407 | kfree(x->aead); |
a51482bd JJ |
408 | kfree(x->aalg); |
409 | kfree(x->ealg); | |
410 | kfree(x->calg); | |
411 | kfree(x->encap); | |
060f02a3 | 412 | kfree(x->coaddr); |
d8647b79 SK |
413 | kfree(x->replay_esn); |
414 | kfree(x->preplay_esn); | |
9d389d7f SK |
415 | if (x->type_offload) |
416 | xfrm_put_type_offload(x->type_offload); | |
1da177e4 LT |
417 | if (x->type) { |
418 | x->type->destructor(x); | |
419 | xfrm_put_type(x->type); | |
420 | } | |
d77e38e6 | 421 | xfrm_dev_state_free(x); |
df71837d | 422 | security_xfrm_state_free(x); |
4a135e53 | 423 | xfrm_state_free(x); |
1da177e4 LT |
424 | } |
425 | ||
c7837144 | 426 | static void xfrm_state_gc_task(struct work_struct *work) |
1da177e4 | 427 | { |
12a169e7 | 428 | struct xfrm_state *x; |
b67bfe0d | 429 | struct hlist_node *tmp; |
12a169e7 | 430 | struct hlist_head gc_list; |
1da177e4 | 431 | |
1da177e4 | 432 | spin_lock_bh(&xfrm_state_gc_lock); |
35db57bb | 433 | hlist_move_list(&xfrm_state_gc_list, &gc_list); |
1da177e4 LT |
434 | spin_unlock_bh(&xfrm_state_gc_lock); |
435 | ||
df7274eb FW |
436 | synchronize_rcu(); |
437 | ||
b67bfe0d | 438 | hlist_for_each_entry_safe(x, tmp, &gc_list, gclist) |
f75a2804 | 439 | ___xfrm_state_destroy(x); |
1da177e4 LT |
440 | } |
441 | ||
3e94c2dc | 442 | static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me) |
1da177e4 | 443 | { |
671422b2 TG |
444 | struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer); |
445 | enum hrtimer_restart ret = HRTIMER_NORESTART; | |
386c5680 AB |
446 | time64_t now = ktime_get_real_seconds(); |
447 | time64_t next = TIME64_MAX; | |
1da177e4 | 448 | int warn = 0; |
161a09e7 | 449 | int err = 0; |
1da177e4 LT |
450 | |
451 | spin_lock(&x->lock); | |
452 | if (x->km.state == XFRM_STATE_DEAD) | |
453 | goto out; | |
454 | if (x->km.state == XFRM_STATE_EXPIRED) | |
455 | goto expired; | |
456 | if (x->lft.hard_add_expires_seconds) { | |
457 | long tmo = x->lft.hard_add_expires_seconds + | |
458 | x->curlft.add_time - now; | |
e3c0d047 FD |
459 | if (tmo <= 0) { |
460 | if (x->xflags & XFRM_SOFT_EXPIRE) { | |
461 | /* enter hard expire without soft expire first?! | |
462 | * setting a new date could trigger this. | |
1365e547 | 463 | * workaround: fix x->curflt.add_time by below: |
e3c0d047 FD |
464 | */ |
465 | x->curlft.add_time = now - x->saved_tmo - 1; | |
466 | tmo = x->lft.hard_add_expires_seconds - x->saved_tmo; | |
467 | } else | |
468 | goto expired; | |
469 | } | |
1da177e4 LT |
470 | if (tmo < next) |
471 | next = tmo; | |
472 | } | |
473 | if (x->lft.hard_use_expires_seconds) { | |
474 | long tmo = x->lft.hard_use_expires_seconds + | |
475 | (x->curlft.use_time ? : now) - now; | |
476 | if (tmo <= 0) | |
477 | goto expired; | |
478 | if (tmo < next) | |
479 | next = tmo; | |
480 | } | |
481 | if (x->km.dying) | |
482 | goto resched; | |
483 | if (x->lft.soft_add_expires_seconds) { | |
484 | long tmo = x->lft.soft_add_expires_seconds + | |
485 | x->curlft.add_time - now; | |
e3c0d047 | 486 | if (tmo <= 0) { |
1da177e4 | 487 | warn = 1; |
e3c0d047 FD |
488 | x->xflags &= ~XFRM_SOFT_EXPIRE; |
489 | } else if (tmo < next) { | |
1da177e4 | 490 | next = tmo; |
e3c0d047 FD |
491 | x->xflags |= XFRM_SOFT_EXPIRE; |
492 | x->saved_tmo = tmo; | |
493 | } | |
1da177e4 LT |
494 | } |
495 | if (x->lft.soft_use_expires_seconds) { | |
496 | long tmo = x->lft.soft_use_expires_seconds + | |
497 | (x->curlft.use_time ? : now) - now; | |
498 | if (tmo <= 0) | |
499 | warn = 1; | |
500 | else if (tmo < next) | |
501 | next = tmo; | |
502 | } | |
503 | ||
4666faab | 504 | x->km.dying = warn; |
1da177e4 | 505 | if (warn) |
53bc6b4d | 506 | km_state_expired(x, 0, 0); |
1da177e4 | 507 | resched: |
386c5680 | 508 | if (next != TIME64_MAX) { |
671422b2 TG |
509 | hrtimer_forward_now(&x->mtimer, ktime_set(next, 0)); |
510 | ret = HRTIMER_RESTART; | |
9e0d57fd | 511 | } |
a47f0ce0 | 512 | |
1da177e4 LT |
513 | goto out; |
514 | ||
515 | expired: | |
5b8ef341 | 516 | if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) |
1da177e4 | 517 | x->km.state = XFRM_STATE_EXPIRED; |
161a09e7 JL |
518 | |
519 | err = __xfrm_state_delete(x); | |
0806ae4c | 520 | if (!err) |
53bc6b4d | 521 | km_state_expired(x, 1, 0); |
1da177e4 | 522 | |
2e71029e | 523 | xfrm_audit_state_delete(x, err ? 0 : 1, true); |
161a09e7 | 524 | |
1da177e4 LT |
525 | out: |
526 | spin_unlock(&x->lock); | |
671422b2 | 527 | return ret; |
1da177e4 LT |
528 | } |
529 | ||
e99e88a9 | 530 | static void xfrm_replay_timer_handler(struct timer_list *t); |
0ac84752 | 531 | |
673c09be | 532 | struct xfrm_state *xfrm_state_alloc(struct net *net) |
1da177e4 LT |
533 | { |
534 | struct xfrm_state *x; | |
535 | ||
565f0fa9 | 536 | x = kmem_cache_alloc(xfrm_state_cache, GFP_ATOMIC | __GFP_ZERO); |
1da177e4 LT |
537 | |
538 | if (x) { | |
673c09be | 539 | write_pnet(&x->xs_net, net); |
88755e9c | 540 | refcount_set(&x->refcnt, 1); |
1da177e4 | 541 | atomic_set(&x->tunnel_users, 0); |
12a169e7 | 542 | INIT_LIST_HEAD(&x->km.all); |
8f126e37 DM |
543 | INIT_HLIST_NODE(&x->bydst); |
544 | INIT_HLIST_NODE(&x->bysrc); | |
545 | INIT_HLIST_NODE(&x->byspi); | |
671422b2 TG |
546 | hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT); |
547 | x->mtimer.function = xfrm_timer_handler; | |
e99e88a9 | 548 | timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0); |
386c5680 | 549 | x->curlft.add_time = ktime_get_real_seconds(); |
1da177e4 LT |
550 | x->lft.soft_byte_limit = XFRM_INF; |
551 | x->lft.soft_packet_limit = XFRM_INF; | |
552 | x->lft.hard_byte_limit = XFRM_INF; | |
553 | x->lft.hard_packet_limit = XFRM_INF; | |
f8cd5488 JHS |
554 | x->replay_maxage = 0; |
555 | x->replay_maxdiff = 0; | |
1da177e4 LT |
556 | spin_lock_init(&x->lock); |
557 | } | |
558 | return x; | |
559 | } | |
560 | EXPORT_SYMBOL(xfrm_state_alloc); | |
561 | ||
f75a2804 | 562 | void __xfrm_state_destroy(struct xfrm_state *x, bool sync) |
1da177e4 | 563 | { |
547b792c | 564 | WARN_ON(x->km.state != XFRM_STATE_DEAD); |
1da177e4 | 565 | |
f75a2804 CW |
566 | if (sync) { |
567 | synchronize_rcu(); | |
568 | ___xfrm_state_destroy(x); | |
569 | } else { | |
570 | spin_lock_bh(&xfrm_state_gc_lock); | |
571 | hlist_add_head(&x->gclist, &xfrm_state_gc_list); | |
572 | spin_unlock_bh(&xfrm_state_gc_lock); | |
573 | schedule_work(&xfrm_state_gc_work); | |
574 | } | |
1da177e4 LT |
575 | } |
576 | EXPORT_SYMBOL(__xfrm_state_destroy); | |
577 | ||
53bc6b4d | 578 | int __xfrm_state_delete(struct xfrm_state *x) |
1da177e4 | 579 | { |
98806f75 | 580 | struct net *net = xs_net(x); |
26b15dad JHS |
581 | int err = -ESRCH; |
582 | ||
1da177e4 LT |
583 | if (x->km.state != XFRM_STATE_DEAD) { |
584 | x->km.state = XFRM_STATE_DEAD; | |
283bc9f3 | 585 | spin_lock(&net->xfrm.xfrm_state_lock); |
12a169e7 | 586 | list_del(&x->km.all); |
ae3fb6d3 FW |
587 | hlist_del_rcu(&x->bydst); |
588 | hlist_del_rcu(&x->bysrc); | |
a47f0ce0 | 589 | if (x->id.spi) |
ae3fb6d3 | 590 | hlist_del_rcu(&x->byspi); |
98806f75 | 591 | net->xfrm.state_num--; |
283bc9f3 | 592 | spin_unlock(&net->xfrm.xfrm_state_lock); |
1da177e4 | 593 | |
d77e38e6 SK |
594 | xfrm_dev_state_delete(x); |
595 | ||
1da177e4 LT |
596 | /* All xfrm_state objects are created by xfrm_state_alloc. |
597 | * The xfrm_state_alloc call gives a reference, and that | |
598 | * is what we are dropping here. | |
599 | */ | |
5dba4797 | 600 | xfrm_state_put(x); |
26b15dad | 601 | err = 0; |
1da177e4 | 602 | } |
26b15dad JHS |
603 | |
604 | return err; | |
1da177e4 | 605 | } |
53bc6b4d | 606 | EXPORT_SYMBOL(__xfrm_state_delete); |
1da177e4 | 607 | |
26b15dad | 608 | int xfrm_state_delete(struct xfrm_state *x) |
1da177e4 | 609 | { |
26b15dad JHS |
610 | int err; |
611 | ||
1da177e4 | 612 | spin_lock_bh(&x->lock); |
26b15dad | 613 | err = __xfrm_state_delete(x); |
1da177e4 | 614 | spin_unlock_bh(&x->lock); |
26b15dad JHS |
615 | |
616 | return err; | |
1da177e4 LT |
617 | } |
618 | EXPORT_SYMBOL(xfrm_state_delete); | |
619 | ||
4aa2e62c JL |
620 | #ifdef CONFIG_SECURITY_NETWORK_XFRM |
621 | static inline int | |
2e71029e | 622 | xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) |
1da177e4 | 623 | { |
4aa2e62c JL |
624 | int i, err = 0; |
625 | ||
0e602451 | 626 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
4aa2e62c JL |
627 | struct xfrm_state *x; |
628 | ||
b67bfe0d | 629 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
4aa2e62c JL |
630 | if (xfrm_id_proto_match(x->id.proto, proto) && |
631 | (err = security_xfrm_state_delete(x)) != 0) { | |
2e71029e | 632 | xfrm_audit_state_delete(x, 0, task_valid); |
4aa2e62c JL |
633 | return err; |
634 | } | |
635 | } | |
636 | } | |
637 | ||
638 | return err; | |
639 | } | |
d77e38e6 SK |
640 | |
641 | static inline int | |
642 | xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid) | |
643 | { | |
644 | int i, err = 0; | |
645 | ||
646 | for (i = 0; i <= net->xfrm.state_hmask; i++) { | |
647 | struct xfrm_state *x; | |
648 | struct xfrm_state_offload *xso; | |
649 | ||
650 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { | |
651 | xso = &x->xso; | |
652 | ||
653 | if (xso->dev == dev && | |
654 | (err = security_xfrm_state_delete(x)) != 0) { | |
655 | xfrm_audit_state_delete(x, 0, task_valid); | |
656 | return err; | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
661 | return err; | |
662 | } | |
4aa2e62c JL |
663 | #else |
664 | static inline int | |
2e71029e | 665 | xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid) |
4aa2e62c JL |
666 | { |
667 | return 0; | |
668 | } | |
d77e38e6 SK |
669 | |
670 | static inline int | |
671 | xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid) | |
672 | { | |
673 | return 0; | |
674 | } | |
4aa2e62c JL |
675 | #endif |
676 | ||
f75a2804 | 677 | int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync) |
4aa2e62c | 678 | { |
9e64cc95 | 679 | int i, err = 0, cnt = 0; |
1da177e4 | 680 | |
283bc9f3 | 681 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
2e71029e | 682 | err = xfrm_state_flush_secctx_check(net, proto, task_valid); |
4aa2e62c JL |
683 | if (err) |
684 | goto out; | |
685 | ||
9e64cc95 | 686 | err = -ESRCH; |
0e602451 | 687 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
8f126e37 | 688 | struct xfrm_state *x; |
1da177e4 | 689 | restart: |
b67bfe0d | 690 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
1da177e4 | 691 | if (!xfrm_state_kern(x) && |
5794708f | 692 | xfrm_id_proto_match(x->id.proto, proto)) { |
1da177e4 | 693 | xfrm_state_hold(x); |
283bc9f3 | 694 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 | 695 | |
161a09e7 | 696 | err = xfrm_state_delete(x); |
ab5f5e8b | 697 | xfrm_audit_state_delete(x, err ? 0 : 1, |
2e71029e | 698 | task_valid); |
f75a2804 CW |
699 | if (sync) |
700 | xfrm_state_put_sync(x); | |
701 | else | |
702 | xfrm_state_put(x); | |
9e64cc95 JHS |
703 | if (!err) |
704 | cnt++; | |
1da177e4 | 705 | |
283bc9f3 | 706 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
707 | goto restart; |
708 | } | |
709 | } | |
710 | } | |
dd269db8 AS |
711 | out: |
712 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); | |
e4db5b61 | 713 | if (cnt) |
9e64cc95 | 714 | err = 0; |
e4db5b61 | 715 | |
4aa2e62c | 716 | return err; |
1da177e4 LT |
717 | } |
718 | EXPORT_SYMBOL(xfrm_state_flush); | |
719 | ||
d77e38e6 SK |
720 | int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid) |
721 | { | |
722 | int i, err = 0, cnt = 0; | |
723 | ||
724 | spin_lock_bh(&net->xfrm.xfrm_state_lock); | |
725 | err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid); | |
726 | if (err) | |
727 | goto out; | |
728 | ||
729 | err = -ESRCH; | |
730 | for (i = 0; i <= net->xfrm.state_hmask; i++) { | |
731 | struct xfrm_state *x; | |
732 | struct xfrm_state_offload *xso; | |
733 | restart: | |
734 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { | |
735 | xso = &x->xso; | |
736 | ||
737 | if (!xfrm_state_kern(x) && xso->dev == dev) { | |
738 | xfrm_state_hold(x); | |
739 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); | |
740 | ||
741 | err = xfrm_state_delete(x); | |
742 | xfrm_audit_state_delete(x, err ? 0 : 1, | |
743 | task_valid); | |
744 | xfrm_state_put(x); | |
745 | if (!err) | |
746 | cnt++; | |
747 | ||
748 | spin_lock_bh(&net->xfrm.xfrm_state_lock); | |
749 | goto restart; | |
750 | } | |
751 | } | |
752 | } | |
753 | if (cnt) | |
754 | err = 0; | |
755 | ||
756 | out: | |
757 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); | |
758 | return err; | |
759 | } | |
760 | EXPORT_SYMBOL(xfrm_dev_state_flush); | |
761 | ||
e071041b | 762 | void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si) |
28d8909b | 763 | { |
283bc9f3 | 764 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
e071041b | 765 | si->sadcnt = net->xfrm.state_num; |
ca92e173 | 766 | si->sadhcnt = net->xfrm.state_hmask + 1; |
28d8909b | 767 | si->sadhmcnt = xfrm_state_hashmax; |
283bc9f3 | 768 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
28d8909b JHS |
769 | } |
770 | EXPORT_SYMBOL(xfrm_sad_getinfo); | |
771 | ||
711059b9 | 772 | static void |
1a898592 | 773 | xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl, |
04686013 | 774 | const struct xfrm_tmpl *tmpl, |
33765d06 | 775 | const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
8444cf71 | 776 | unsigned short family) |
1da177e4 | 777 | { |
711059b9 FW |
778 | struct xfrm_state_afinfo *afinfo = xfrm_state_afinfo_get_rcu(family); |
779 | ||
3819a35f FW |
780 | if (!afinfo) |
781 | return; | |
782 | ||
783 | afinfo->init_tempsel(&x->sel, fl); | |
8444cf71 TE |
784 | |
785 | if (family != tmpl->encap_family) { | |
711059b9 | 786 | afinfo = xfrm_state_afinfo_get_rcu(tmpl->encap_family); |
8444cf71 | 787 | if (!afinfo) |
711059b9 | 788 | return; |
8444cf71 TE |
789 | } |
790 | afinfo->init_temprop(x, tmpl, daddr, saddr); | |
1da177e4 LT |
791 | } |
792 | ||
9aa60088 DM |
793 | static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark, |
794 | const xfrm_address_t *daddr, | |
795 | __be32 spi, u8 proto, | |
796 | unsigned short family) | |
edcd5821 | 797 | { |
221df1ed | 798 | unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family); |
edcd5821 DM |
799 | struct xfrm_state *x; |
800 | ||
ae3fb6d3 | 801 | hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) { |
edcd5821 DM |
802 | if (x->props.family != family || |
803 | x->id.spi != spi || | |
1802571b | 804 | x->id.proto != proto || |
70e94e66 | 805 | !xfrm_addr_equal(&x->id.daddr, daddr, family)) |
edcd5821 DM |
806 | continue; |
807 | ||
3d6acfa7 JHS |
808 | if ((mark & x->mark.m) != x->mark.v) |
809 | continue; | |
02efdff7 FW |
810 | if (!xfrm_state_hold_rcu(x)) |
811 | continue; | |
edcd5821 DM |
812 | return x; |
813 | } | |
814 | ||
815 | return NULL; | |
816 | } | |
817 | ||
9aa60088 DM |
818 | static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark, |
819 | const xfrm_address_t *daddr, | |
820 | const xfrm_address_t *saddr, | |
821 | u8 proto, unsigned short family) | |
edcd5821 | 822 | { |
221df1ed | 823 | unsigned int h = xfrm_src_hash(net, daddr, saddr, family); |
edcd5821 DM |
824 | struct xfrm_state *x; |
825 | ||
ae3fb6d3 | 826 | hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) { |
edcd5821 | 827 | if (x->props.family != family || |
1802571b | 828 | x->id.proto != proto || |
70e94e66 YH |
829 | !xfrm_addr_equal(&x->id.daddr, daddr, family) || |
830 | !xfrm_addr_equal(&x->props.saddr, saddr, family)) | |
edcd5821 DM |
831 | continue; |
832 | ||
3d6acfa7 JHS |
833 | if ((mark & x->mark.m) != x->mark.v) |
834 | continue; | |
02efdff7 FW |
835 | if (!xfrm_state_hold_rcu(x)) |
836 | continue; | |
edcd5821 DM |
837 | return x; |
838 | } | |
839 | ||
840 | return NULL; | |
841 | } | |
842 | ||
843 | static inline struct xfrm_state * | |
844 | __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family) | |
845 | { | |
221df1ed | 846 | struct net *net = xs_net(x); |
bd55775c | 847 | u32 mark = x->mark.v & x->mark.m; |
221df1ed | 848 | |
edcd5821 | 849 | if (use_spi) |
bd55775c JHS |
850 | return __xfrm_state_lookup(net, mark, &x->id.daddr, |
851 | x->id.spi, x->id.proto, family); | |
edcd5821 | 852 | else |
bd55775c JHS |
853 | return __xfrm_state_lookup_byaddr(net, mark, |
854 | &x->id.daddr, | |
edcd5821 DM |
855 | &x->props.saddr, |
856 | x->id.proto, family); | |
857 | } | |
858 | ||
98806f75 | 859 | static void xfrm_hash_grow_check(struct net *net, int have_hash_collision) |
2fab22f2 PM |
860 | { |
861 | if (have_hash_collision && | |
98806f75 AD |
862 | (net->xfrm.state_hmask + 1) < xfrm_state_hashmax && |
863 | net->xfrm.state_num > net->xfrm.state_hmask) | |
864 | schedule_work(&net->xfrm.state_hash_work); | |
2fab22f2 PM |
865 | } |
866 | ||
08ec9af1 | 867 | static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x, |
4a08ab0f | 868 | const struct flowi *fl, unsigned short family, |
08ec9af1 DM |
869 | struct xfrm_state **best, int *acq_in_progress, |
870 | int *error) | |
871 | { | |
872 | /* Resolution logic: | |
873 | * 1. There is a valid state with matching selector. Done. | |
874 | * 2. Valid state with inappropriate selector. Skip. | |
875 | * | |
876 | * Entering area of "sysdeps". | |
877 | * | |
878 | * 3. If state is not valid, selector is temporary, it selects | |
879 | * only session which triggered previous resolution. Key | |
880 | * manager will do something to install a state with proper | |
881 | * selector. | |
882 | */ | |
883 | if (x->km.state == XFRM_STATE_VALID) { | |
884 | if ((x->sel.family && | |
885 | !xfrm_selector_match(&x->sel, fl, x->sel.family)) || | |
886 | !security_xfrm_state_pol_flow_match(x, pol, fl)) | |
887 | return; | |
888 | ||
889 | if (!*best || | |
890 | (*best)->km.dying > x->km.dying || | |
891 | ((*best)->km.dying == x->km.dying && | |
892 | (*best)->curlft.add_time < x->curlft.add_time)) | |
893 | *best = x; | |
894 | } else if (x->km.state == XFRM_STATE_ACQ) { | |
895 | *acq_in_progress = 1; | |
896 | } else if (x->km.state == XFRM_STATE_ERROR || | |
897 | x->km.state == XFRM_STATE_EXPIRED) { | |
898 | if (xfrm_selector_match(&x->sel, fl, x->sel.family) && | |
899 | security_xfrm_state_pol_flow_match(x, pol, fl)) | |
900 | *error = -ESRCH; | |
901 | } | |
902 | } | |
903 | ||
1da177e4 | 904 | struct xfrm_state * |
33765d06 | 905 | xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
b520e9f6 | 906 | const struct flowi *fl, struct xfrm_tmpl *tmpl, |
1da177e4 | 907 | struct xfrm_policy *pol, int *err, |
bc56b334 | 908 | unsigned short family, u32 if_id) |
1da177e4 | 909 | { |
08ec9af1 | 910 | static xfrm_address_t saddr_wildcard = { }; |
5447c5e4 | 911 | struct net *net = xp_net(pol); |
6a783c90 | 912 | unsigned int h, h_wildcard; |
37b08e34 | 913 | struct xfrm_state *x, *x0, *to_put; |
1da177e4 LT |
914 | int acquire_in_progress = 0; |
915 | int error = 0; | |
916 | struct xfrm_state *best = NULL; | |
bd55775c | 917 | u32 mark = pol->mark.v & pol->mark.m; |
8444cf71 | 918 | unsigned short encap_family = tmpl->encap_family; |
b65e3d7b | 919 | unsigned int sequence; |
0f24558e | 920 | struct km_event c; |
a716c119 | 921 | |
37b08e34 DM |
922 | to_put = NULL; |
923 | ||
b65e3d7b FW |
924 | sequence = read_seqcount_begin(&xfrm_state_hash_generation); |
925 | ||
d737a580 | 926 | rcu_read_lock(); |
8444cf71 | 927 | h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family); |
ae3fb6d3 | 928 | hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) { |
8444cf71 | 929 | if (x->props.family == encap_family && |
1da177e4 | 930 | x->props.reqid == tmpl->reqid && |
3d6acfa7 | 931 | (mark & x->mark.m) == x->mark.v && |
7e652640 | 932 | x->if_id == if_id && |
fbd9a5b4 | 933 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
8444cf71 | 934 | xfrm_state_addr_check(x, daddr, saddr, encap_family) && |
1da177e4 LT |
935 | tmpl->mode == x->props.mode && |
936 | tmpl->id.proto == x->id.proto && | |
08ec9af1 | 937 | (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) |
1f673c5f | 938 | xfrm_state_look_at(pol, x, fl, encap_family, |
08ec9af1 DM |
939 | &best, &acquire_in_progress, &error); |
940 | } | |
6f115638 | 941 | if (best || acquire_in_progress) |
08ec9af1 DM |
942 | goto found; |
943 | ||
8444cf71 | 944 | h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); |
ae3fb6d3 | 945 | hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) { |
8444cf71 | 946 | if (x->props.family == encap_family && |
08ec9af1 | 947 | x->props.reqid == tmpl->reqid && |
3d6acfa7 | 948 | (mark & x->mark.m) == x->mark.v && |
7e652640 | 949 | x->if_id == if_id && |
08ec9af1 | 950 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
f59bbdfa | 951 | xfrm_addr_equal(&x->id.daddr, daddr, encap_family) && |
08ec9af1 DM |
952 | tmpl->mode == x->props.mode && |
953 | tmpl->id.proto == x->id.proto && | |
954 | (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) | |
1f673c5f | 955 | xfrm_state_look_at(pol, x, fl, encap_family, |
08ec9af1 | 956 | &best, &acquire_in_progress, &error); |
1da177e4 LT |
957 | } |
958 | ||
08ec9af1 | 959 | found: |
1da177e4 LT |
960 | x = best; |
961 | if (!x && !error && !acquire_in_progress) { | |
5c5d281a | 962 | if (tmpl->id.spi && |
bd55775c | 963 | (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi, |
8444cf71 | 964 | tmpl->id.proto, encap_family)) != NULL) { |
37b08e34 | 965 | to_put = x0; |
1da177e4 LT |
966 | error = -EEXIST; |
967 | goto out; | |
968 | } | |
0f24558e HG |
969 | |
970 | c.net = net; | |
971 | /* If the KMs have no listeners (yet...), avoid allocating an SA | |
972 | * for each and every packet - garbage collection might not | |
973 | * handle the flood. | |
974 | */ | |
975 | if (!km_is_alive(&c)) { | |
976 | error = -ESRCH; | |
977 | goto out; | |
978 | } | |
979 | ||
5447c5e4 | 980 | x = xfrm_state_alloc(net); |
1da177e4 LT |
981 | if (x == NULL) { |
982 | error = -ENOMEM; | |
983 | goto out; | |
984 | } | |
8444cf71 | 985 | /* Initialize temporary state matching only |
1da177e4 | 986 | * to current session. */ |
8444cf71 | 987 | xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family); |
bd55775c | 988 | memcpy(&x->mark, &pol->mark, sizeof(x->mark)); |
7e652640 | 989 | x->if_id = if_id; |
1da177e4 | 990 | |
1d28f42c | 991 | error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid); |
e0d1caa7 VY |
992 | if (error) { |
993 | x->km.state = XFRM_STATE_DEAD; | |
37b08e34 | 994 | to_put = x; |
e0d1caa7 VY |
995 | x = NULL; |
996 | goto out; | |
997 | } | |
998 | ||
1da177e4 | 999 | if (km_query(x, tmpl, pol) == 0) { |
d737a580 | 1000 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 | 1001 | x->km.state = XFRM_STATE_ACQ; |
5447c5e4 | 1002 | list_add(&x->km.all, &net->xfrm.state_all); |
ae3fb6d3 | 1003 | hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h); |
8444cf71 | 1004 | h = xfrm_src_hash(net, daddr, saddr, encap_family); |
ae3fb6d3 | 1005 | hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h); |
1da177e4 | 1006 | if (x->id.spi) { |
8444cf71 | 1007 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family); |
ae3fb6d3 | 1008 | hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h); |
1da177e4 | 1009 | } |
b27aeadb | 1010 | x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; |
671422b2 TG |
1011 | hrtimer_start(&x->mtimer, |
1012 | ktime_set(net->xfrm.sysctl_acq_expires, 0), | |
1013 | HRTIMER_MODE_REL_SOFT); | |
5447c5e4 AD |
1014 | net->xfrm.state_num++; |
1015 | xfrm_hash_grow_check(net, x->bydst.next != NULL); | |
d737a580 | 1016 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
1017 | } else { |
1018 | x->km.state = XFRM_STATE_DEAD; | |
37b08e34 | 1019 | to_put = x; |
1da177e4 LT |
1020 | x = NULL; |
1021 | error = -ESRCH; | |
1022 | } | |
1023 | } | |
1024 | out: | |
02efdff7 FW |
1025 | if (x) { |
1026 | if (!xfrm_state_hold_rcu(x)) { | |
1027 | *err = -EAGAIN; | |
1028 | x = NULL; | |
1029 | } | |
1030 | } else { | |
1da177e4 | 1031 | *err = acquire_in_progress ? -EAGAIN : error; |
02efdff7 | 1032 | } |
d737a580 | 1033 | rcu_read_unlock(); |
37b08e34 DM |
1034 | if (to_put) |
1035 | xfrm_state_put(to_put); | |
b65e3d7b FW |
1036 | |
1037 | if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) { | |
1038 | *err = -EAGAIN; | |
1039 | if (x) { | |
1040 | xfrm_state_put(x); | |
1041 | x = NULL; | |
1042 | } | |
1043 | } | |
1044 | ||
1da177e4 LT |
1045 | return x; |
1046 | } | |
1047 | ||
628529b6 | 1048 | struct xfrm_state * |
7e652640 | 1049 | xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id, |
5447c5e4 | 1050 | xfrm_address_t *daddr, xfrm_address_t *saddr, |
628529b6 JHS |
1051 | unsigned short family, u8 mode, u8 proto, u32 reqid) |
1052 | { | |
4bda4f25 | 1053 | unsigned int h; |
628529b6 | 1054 | struct xfrm_state *rx = NULL, *x = NULL; |
628529b6 | 1055 | |
4ae770bf | 1056 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
5447c5e4 | 1057 | h = xfrm_dst_hash(net, daddr, saddr, reqid, family); |
b67bfe0d | 1058 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
628529b6 JHS |
1059 | if (x->props.family == family && |
1060 | x->props.reqid == reqid && | |
3d6acfa7 | 1061 | (mark & x->mark.m) == x->mark.v && |
7e652640 | 1062 | x->if_id == if_id && |
628529b6 JHS |
1063 | !(x->props.flags & XFRM_STATE_WILDRECV) && |
1064 | xfrm_state_addr_check(x, daddr, saddr, family) && | |
1065 | mode == x->props.mode && | |
1066 | proto == x->id.proto && | |
1067 | x->km.state == XFRM_STATE_VALID) { | |
1068 | rx = x; | |
1069 | break; | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | if (rx) | |
1074 | xfrm_state_hold(rx); | |
4ae770bf | 1075 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
628529b6 JHS |
1076 | |
1077 | ||
1078 | return rx; | |
1079 | } | |
1080 | EXPORT_SYMBOL(xfrm_stateonly_find); | |
1081 | ||
c454997e FD |
1082 | struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi, |
1083 | unsigned short family) | |
1084 | { | |
1085 | struct xfrm_state *x; | |
1086 | struct xfrm_state_walk *w; | |
1087 | ||
1088 | spin_lock_bh(&net->xfrm.xfrm_state_lock); | |
1089 | list_for_each_entry(w, &net->xfrm.state_all, all) { | |
1090 | x = container_of(w, struct xfrm_state, km); | |
1091 | if (x->props.family != family || | |
1092 | x->id.spi != spi) | |
1093 | continue; | |
1094 | ||
c454997e | 1095 | xfrm_state_hold(x); |
bdddbf69 | 1096 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
c454997e FD |
1097 | return x; |
1098 | } | |
1099 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); | |
1100 | return NULL; | |
1101 | } | |
1102 | EXPORT_SYMBOL(xfrm_state_lookup_byspi); | |
1103 | ||
1da177e4 LT |
1104 | static void __xfrm_state_insert(struct xfrm_state *x) |
1105 | { | |
98806f75 | 1106 | struct net *net = xs_net(x); |
a624c108 | 1107 | unsigned int h; |
1da177e4 | 1108 | |
98806f75 | 1109 | list_add(&x->km.all, &net->xfrm.state_all); |
4c563f76 | 1110 | |
98806f75 | 1111 | h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr, |
c1969f29 | 1112 | x->props.reqid, x->props.family); |
ae3fb6d3 | 1113 | hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h); |
1da177e4 | 1114 | |
98806f75 | 1115 | h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family); |
ae3fb6d3 | 1116 | hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h); |
1da177e4 | 1117 | |
7b4dc360 | 1118 | if (x->id.spi) { |
98806f75 | 1119 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, |
6c44e6b7 MN |
1120 | x->props.family); |
1121 | ||
ae3fb6d3 | 1122 | hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h); |
6c44e6b7 MN |
1123 | } |
1124 | ||
671422b2 | 1125 | hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); |
a47f0ce0 DM |
1126 | if (x->replay_maxage) |
1127 | mod_timer(&x->rtimer, jiffies + x->replay_maxage); | |
f8cd5488 | 1128 | |
98806f75 | 1129 | net->xfrm.state_num++; |
f034b5d4 | 1130 | |
98806f75 | 1131 | xfrm_hash_grow_check(net, x->bydst.next != NULL); |
1da177e4 LT |
1132 | } |
1133 | ||
283bc9f3 | 1134 | /* net->xfrm.xfrm_state_lock is held */ |
c7f5ea3a DM |
1135 | static void __xfrm_state_bump_genids(struct xfrm_state *xnew) |
1136 | { | |
98806f75 | 1137 | struct net *net = xs_net(xnew); |
c7f5ea3a DM |
1138 | unsigned short family = xnew->props.family; |
1139 | u32 reqid = xnew->props.reqid; | |
1140 | struct xfrm_state *x; | |
c7f5ea3a | 1141 | unsigned int h; |
3d6acfa7 | 1142 | u32 mark = xnew->mark.v & xnew->mark.m; |
7e652640 | 1143 | u32 if_id = xnew->if_id; |
c7f5ea3a | 1144 | |
98806f75 | 1145 | h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family); |
b67bfe0d | 1146 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
c7f5ea3a DM |
1147 | if (x->props.family == family && |
1148 | x->props.reqid == reqid && | |
7e652640 | 1149 | x->if_id == if_id && |
3d6acfa7 | 1150 | (mark & x->mark.m) == x->mark.v && |
70e94e66 YH |
1151 | xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) && |
1152 | xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family)) | |
34996cb9 | 1153 | x->genid++; |
c7f5ea3a DM |
1154 | } |
1155 | } | |
1156 | ||
1da177e4 LT |
1157 | void xfrm_state_insert(struct xfrm_state *x) |
1158 | { | |
283bc9f3 FD |
1159 | struct net *net = xs_net(x); |
1160 | ||
1161 | spin_lock_bh(&net->xfrm.xfrm_state_lock); | |
c7f5ea3a | 1162 | __xfrm_state_bump_genids(x); |
1da177e4 | 1163 | __xfrm_state_insert(x); |
283bc9f3 | 1164 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
1165 | } |
1166 | EXPORT_SYMBOL(xfrm_state_insert); | |
1167 | ||
283bc9f3 | 1168 | /* net->xfrm.xfrm_state_lock is held */ |
e473fcb4 MK |
1169 | static struct xfrm_state *__find_acq_core(struct net *net, |
1170 | const struct xfrm_mark *m, | |
a70486f0 | 1171 | unsigned short family, u8 mode, |
7e652640 | 1172 | u32 reqid, u32 if_id, u8 proto, |
a70486f0 | 1173 | const xfrm_address_t *daddr, |
e473fcb4 MK |
1174 | const xfrm_address_t *saddr, |
1175 | int create) | |
2770834c | 1176 | { |
5447c5e4 | 1177 | unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family); |
2770834c | 1178 | struct xfrm_state *x; |
3d6acfa7 | 1179 | u32 mark = m->v & m->m; |
2770834c | 1180 | |
b67bfe0d | 1181 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
2770834c DM |
1182 | if (x->props.reqid != reqid || |
1183 | x->props.mode != mode || | |
1184 | x->props.family != family || | |
1185 | x->km.state != XFRM_STATE_ACQ || | |
75e252d9 | 1186 | x->id.spi != 0 || |
1802571b | 1187 | x->id.proto != proto || |
3d6acfa7 | 1188 | (mark & x->mark.m) != x->mark.v || |
70e94e66 YH |
1189 | !xfrm_addr_equal(&x->id.daddr, daddr, family) || |
1190 | !xfrm_addr_equal(&x->props.saddr, saddr, family)) | |
2770834c DM |
1191 | continue; |
1192 | ||
2770834c DM |
1193 | xfrm_state_hold(x); |
1194 | return x; | |
1195 | } | |
1196 | ||
1197 | if (!create) | |
1198 | return NULL; | |
1199 | ||
5447c5e4 | 1200 | x = xfrm_state_alloc(net); |
2770834c DM |
1201 | if (likely(x)) { |
1202 | switch (family) { | |
1203 | case AF_INET: | |
1204 | x->sel.daddr.a4 = daddr->a4; | |
1205 | x->sel.saddr.a4 = saddr->a4; | |
1206 | x->sel.prefixlen_d = 32; | |
1207 | x->sel.prefixlen_s = 32; | |
1208 | x->props.saddr.a4 = saddr->a4; | |
1209 | x->id.daddr.a4 = daddr->a4; | |
1210 | break; | |
1211 | ||
1212 | case AF_INET6: | |
15e318bd JB |
1213 | x->sel.daddr.in6 = daddr->in6; |
1214 | x->sel.saddr.in6 = saddr->in6; | |
2770834c DM |
1215 | x->sel.prefixlen_d = 128; |
1216 | x->sel.prefixlen_s = 128; | |
15e318bd JB |
1217 | x->props.saddr.in6 = saddr->in6; |
1218 | x->id.daddr.in6 = daddr->in6; | |
2770834c | 1219 | break; |
3ff50b79 | 1220 | } |
2770834c DM |
1221 | |
1222 | x->km.state = XFRM_STATE_ACQ; | |
1223 | x->id.proto = proto; | |
1224 | x->props.family = family; | |
1225 | x->props.mode = mode; | |
1226 | x->props.reqid = reqid; | |
7e652640 | 1227 | x->if_id = if_id; |
bd55775c JHS |
1228 | x->mark.v = m->v; |
1229 | x->mark.m = m->m; | |
b27aeadb | 1230 | x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires; |
2770834c | 1231 | xfrm_state_hold(x); |
671422b2 TG |
1232 | hrtimer_start(&x->mtimer, |
1233 | ktime_set(net->xfrm.sysctl_acq_expires, 0), | |
1234 | HRTIMER_MODE_REL_SOFT); | |
5447c5e4 | 1235 | list_add(&x->km.all, &net->xfrm.state_all); |
ae3fb6d3 | 1236 | hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h); |
5447c5e4 | 1237 | h = xfrm_src_hash(net, daddr, saddr, family); |
ae3fb6d3 | 1238 | hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h); |
918049f0 | 1239 | |
5447c5e4 | 1240 | net->xfrm.state_num++; |
918049f0 | 1241 | |
5447c5e4 | 1242 | xfrm_hash_grow_check(net, x->bydst.next != NULL); |
2770834c DM |
1243 | } |
1244 | ||
1245 | return x; | |
1246 | } | |
1247 | ||
bd55775c | 1248 | static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq); |
1da177e4 LT |
1249 | |
1250 | int xfrm_state_add(struct xfrm_state *x) | |
1251 | { | |
5447c5e4 | 1252 | struct net *net = xs_net(x); |
37b08e34 | 1253 | struct xfrm_state *x1, *to_put; |
1da177e4 LT |
1254 | int family; |
1255 | int err; | |
bd55775c | 1256 | u32 mark = x->mark.v & x->mark.m; |
eb2971b6 | 1257 | int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); |
1da177e4 LT |
1258 | |
1259 | family = x->props.family; | |
1da177e4 | 1260 | |
37b08e34 DM |
1261 | to_put = NULL; |
1262 | ||
283bc9f3 | 1263 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 | 1264 | |
edcd5821 | 1265 | x1 = __xfrm_state_locate(x, use_spi, family); |
1da177e4 | 1266 | if (x1) { |
37b08e34 | 1267 | to_put = x1; |
1da177e4 LT |
1268 | x1 = NULL; |
1269 | err = -EEXIST; | |
1270 | goto out; | |
1271 | } | |
1272 | ||
eb2971b6 | 1273 | if (use_spi && x->km.seq) { |
bd55775c | 1274 | x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq); |
75e252d9 | 1275 | if (x1 && ((x1->id.proto != x->id.proto) || |
70e94e66 | 1276 | !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) { |
37b08e34 | 1277 | to_put = x1; |
1da177e4 LT |
1278 | x1 = NULL; |
1279 | } | |
1280 | } | |
1281 | ||
eb2971b6 | 1282 | if (use_spi && !x1) |
bd55775c | 1283 | x1 = __find_acq_core(net, &x->mark, family, x->props.mode, |
7e652640 | 1284 | x->props.reqid, x->if_id, x->id.proto, |
2770834c | 1285 | &x->id.daddr, &x->props.saddr, 0); |
1da177e4 | 1286 | |
c7f5ea3a | 1287 | __xfrm_state_bump_genids(x); |
1da177e4 LT |
1288 | __xfrm_state_insert(x); |
1289 | err = 0; | |
1290 | ||
1291 | out: | |
283bc9f3 | 1292 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
1293 | |
1294 | if (x1) { | |
1295 | xfrm_state_delete(x1); | |
1296 | xfrm_state_put(x1); | |
1297 | } | |
1298 | ||
37b08e34 DM |
1299 | if (to_put) |
1300 | xfrm_state_put(to_put); | |
1301 | ||
1da177e4 LT |
1302 | return err; |
1303 | } | |
1304 | EXPORT_SYMBOL(xfrm_state_add); | |
1305 | ||
80c9abaa | 1306 | #ifdef CONFIG_XFRM_MIGRATE |
4ab47d47 AA |
1307 | static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, |
1308 | struct xfrm_encap_tmpl *encap) | |
80c9abaa | 1309 | { |
98806f75 | 1310 | struct net *net = xs_net(orig); |
98806f75 | 1311 | struct xfrm_state *x = xfrm_state_alloc(net); |
80c9abaa | 1312 | if (!x) |
553f9118 | 1313 | goto out; |
80c9abaa SS |
1314 | |
1315 | memcpy(&x->id, &orig->id, sizeof(x->id)); | |
1316 | memcpy(&x->sel, &orig->sel, sizeof(x->sel)); | |
1317 | memcpy(&x->lft, &orig->lft, sizeof(x->lft)); | |
1318 | x->props.mode = orig->props.mode; | |
1319 | x->props.replay_window = orig->props.replay_window; | |
1320 | x->props.reqid = orig->props.reqid; | |
1321 | x->props.family = orig->props.family; | |
1322 | x->props.saddr = orig->props.saddr; | |
1323 | ||
1324 | if (orig->aalg) { | |
4447bb33 | 1325 | x->aalg = xfrm_algo_auth_clone(orig->aalg); |
80c9abaa SS |
1326 | if (!x->aalg) |
1327 | goto error; | |
1328 | } | |
1329 | x->props.aalgo = orig->props.aalgo; | |
1330 | ||
ee5c2317 SK |
1331 | if (orig->aead) { |
1332 | x->aead = xfrm_algo_aead_clone(orig->aead); | |
75bf50f4 | 1333 | x->geniv = orig->geniv; |
ee5c2317 SK |
1334 | if (!x->aead) |
1335 | goto error; | |
1336 | } | |
80c9abaa SS |
1337 | if (orig->ealg) { |
1338 | x->ealg = xfrm_algo_clone(orig->ealg); | |
1339 | if (!x->ealg) | |
1340 | goto error; | |
1341 | } | |
1342 | x->props.ealgo = orig->props.ealgo; | |
1343 | ||
1344 | if (orig->calg) { | |
1345 | x->calg = xfrm_algo_clone(orig->calg); | |
1346 | if (!x->calg) | |
1347 | goto error; | |
1348 | } | |
1349 | x->props.calgo = orig->props.calgo; | |
1350 | ||
4ab47d47 AA |
1351 | if (encap || orig->encap) { |
1352 | if (encap) | |
1353 | x->encap = kmemdup(encap, sizeof(*x->encap), | |
1354 | GFP_KERNEL); | |
1355 | else | |
1356 | x->encap = kmemdup(orig->encap, sizeof(*x->encap), | |
1357 | GFP_KERNEL); | |
1358 | ||
80c9abaa SS |
1359 | if (!x->encap) |
1360 | goto error; | |
1361 | } | |
1362 | ||
1363 | if (orig->coaddr) { | |
1364 | x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr), | |
1365 | GFP_KERNEL); | |
1366 | if (!x->coaddr) | |
1367 | goto error; | |
1368 | } | |
1369 | ||
af2f464e | 1370 | if (orig->replay_esn) { |
cc9ab60e | 1371 | if (xfrm_replay_clone(x, orig)) |
af2f464e SK |
1372 | goto error; |
1373 | } | |
1374 | ||
bd55775c JHS |
1375 | memcpy(&x->mark, &orig->mark, sizeof(x->mark)); |
1376 | ||
cc9ab60e | 1377 | if (xfrm_init_state(x) < 0) |
80c9abaa SS |
1378 | goto error; |
1379 | ||
1380 | x->props.flags = orig->props.flags; | |
a947b0a9 | 1381 | x->props.extra_flags = orig->props.extra_flags; |
80c9abaa | 1382 | |
7e652640 | 1383 | x->if_id = orig->if_id; |
ee5c2317 SK |
1384 | x->tfcpad = orig->tfcpad; |
1385 | x->replay_maxdiff = orig->replay_maxdiff; | |
1386 | x->replay_maxage = orig->replay_maxage; | |
80c9abaa SS |
1387 | x->curlft.add_time = orig->curlft.add_time; |
1388 | x->km.state = orig->km.state; | |
1389 | x->km.seq = orig->km.seq; | |
a486cd23 AA |
1390 | x->replay = orig->replay; |
1391 | x->preplay = orig->preplay; | |
80c9abaa SS |
1392 | |
1393 | return x; | |
1394 | ||
1395 | error: | |
553f9118 HX |
1396 | xfrm_state_put(x); |
1397 | out: | |
80c9abaa SS |
1398 | return NULL; |
1399 | } | |
80c9abaa | 1400 | |
283bc9f3 | 1401 | struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net) |
80c9abaa SS |
1402 | { |
1403 | unsigned int h; | |
8c0cba22 SK |
1404 | struct xfrm_state *x = NULL; |
1405 | ||
1406 | spin_lock_bh(&net->xfrm.xfrm_state_lock); | |
80c9abaa SS |
1407 | |
1408 | if (m->reqid) { | |
283bc9f3 | 1409 | h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr, |
80c9abaa | 1410 | m->reqid, m->old_family); |
283bc9f3 | 1411 | hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) { |
80c9abaa SS |
1412 | if (x->props.mode != m->mode || |
1413 | x->id.proto != m->proto) | |
1414 | continue; | |
1415 | if (m->reqid && x->props.reqid != m->reqid) | |
1416 | continue; | |
70e94e66 YH |
1417 | if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, |
1418 | m->old_family) || | |
1419 | !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, | |
1420 | m->old_family)) | |
80c9abaa SS |
1421 | continue; |
1422 | xfrm_state_hold(x); | |
8c0cba22 | 1423 | break; |
80c9abaa SS |
1424 | } |
1425 | } else { | |
283bc9f3 | 1426 | h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr, |
80c9abaa | 1427 | m->old_family); |
283bc9f3 | 1428 | hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) { |
80c9abaa SS |
1429 | if (x->props.mode != m->mode || |
1430 | x->id.proto != m->proto) | |
1431 | continue; | |
70e94e66 YH |
1432 | if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr, |
1433 | m->old_family) || | |
1434 | !xfrm_addr_equal(&x->props.saddr, &m->old_saddr, | |
1435 | m->old_family)) | |
80c9abaa SS |
1436 | continue; |
1437 | xfrm_state_hold(x); | |
8c0cba22 | 1438 | break; |
80c9abaa SS |
1439 | } |
1440 | } | |
1441 | ||
8c0cba22 SK |
1442 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1443 | ||
1444 | return x; | |
80c9abaa SS |
1445 | } |
1446 | EXPORT_SYMBOL(xfrm_migrate_state_find); | |
1447 | ||
3e94c2dc | 1448 | struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x, |
4ab47d47 AA |
1449 | struct xfrm_migrate *m, |
1450 | struct xfrm_encap_tmpl *encap) | |
80c9abaa SS |
1451 | { |
1452 | struct xfrm_state *xc; | |
80c9abaa | 1453 | |
4ab47d47 | 1454 | xc = xfrm_state_clone(x, encap); |
80c9abaa SS |
1455 | if (!xc) |
1456 | return NULL; | |
1457 | ||
1458 | memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr)); | |
1459 | memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr)); | |
1460 | ||
1461 | /* add state */ | |
70e94e66 | 1462 | if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) { |
80c9abaa SS |
1463 | /* a care is needed when the destination address of the |
1464 | state is to be updated as it is a part of triplet */ | |
1465 | xfrm_state_insert(xc); | |
1466 | } else { | |
cc9ab60e | 1467 | if (xfrm_state_add(xc) < 0) |
80c9abaa SS |
1468 | goto error; |
1469 | } | |
1470 | ||
1471 | return xc; | |
1472 | error: | |
78347c8c | 1473 | xfrm_state_put(xc); |
80c9abaa SS |
1474 | return NULL; |
1475 | } | |
1476 | EXPORT_SYMBOL(xfrm_state_migrate); | |
1477 | #endif | |
1478 | ||
1da177e4 LT |
1479 | int xfrm_state_update(struct xfrm_state *x) |
1480 | { | |
37b08e34 | 1481 | struct xfrm_state *x1, *to_put; |
1da177e4 | 1482 | int err; |
eb2971b6 | 1483 | int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY); |
283bc9f3 | 1484 | struct net *net = xs_net(x); |
1da177e4 | 1485 | |
37b08e34 DM |
1486 | to_put = NULL; |
1487 | ||
283bc9f3 | 1488 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
edcd5821 | 1489 | x1 = __xfrm_state_locate(x, use_spi, x->props.family); |
1da177e4 LT |
1490 | |
1491 | err = -ESRCH; | |
1492 | if (!x1) | |
1493 | goto out; | |
1494 | ||
1495 | if (xfrm_state_kern(x1)) { | |
37b08e34 | 1496 | to_put = x1; |
1da177e4 LT |
1497 | err = -EEXIST; |
1498 | goto out; | |
1499 | } | |
1500 | ||
1501 | if (x1->km.state == XFRM_STATE_ACQ) { | |
1502 | __xfrm_state_insert(x); | |
1503 | x = NULL; | |
1504 | } | |
1505 | err = 0; | |
1506 | ||
1507 | out: | |
283bc9f3 | 1508 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 | 1509 | |
37b08e34 DM |
1510 | if (to_put) |
1511 | xfrm_state_put(to_put); | |
1512 | ||
1da177e4 LT |
1513 | if (err) |
1514 | return err; | |
1515 | ||
1516 | if (!x) { | |
1517 | xfrm_state_delete(x1); | |
1518 | xfrm_state_put(x1); | |
1519 | return 0; | |
1520 | } | |
1521 | ||
1522 | err = -EINVAL; | |
1523 | spin_lock_bh(&x1->lock); | |
1524 | if (likely(x1->km.state == XFRM_STATE_VALID)) { | |
257a4b01 HX |
1525 | if (x->encap && x1->encap && |
1526 | x->encap->encap_type == x1->encap->encap_type) | |
1da177e4 | 1527 | memcpy(x1->encap, x->encap, sizeof(*x1->encap)); |
257a4b01 HX |
1528 | else if (x->encap || x1->encap) |
1529 | goto fail; | |
1530 | ||
060f02a3 NT |
1531 | if (x->coaddr && x1->coaddr) { |
1532 | memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr)); | |
1533 | } | |
1534 | if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel))) | |
1535 | memcpy(&x1->sel, &x->sel, sizeof(x1->sel)); | |
1da177e4 LT |
1536 | memcpy(&x1->lft, &x->lft, sizeof(x1->lft)); |
1537 | x1->km.dying = 0; | |
1538 | ||
671422b2 TG |
1539 | hrtimer_start(&x1->mtimer, ktime_set(1, 0), |
1540 | HRTIMER_MODE_REL_SOFT); | |
1da177e4 LT |
1541 | if (x1->curlft.use_time) |
1542 | xfrm_state_check_expire(x1); | |
1543 | ||
5baf4f9c | 1544 | if (x->props.smark.m || x->props.smark.v || x->if_id) { |
6d8e85ff NH |
1545 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
1546 | ||
5baf4f9c NH |
1547 | if (x->props.smark.m || x->props.smark.v) |
1548 | x1->props.smark = x->props.smark; | |
1549 | ||
1550 | if (x->if_id) | |
1551 | x1->if_id = x->if_id; | |
6d8e85ff NH |
1552 | |
1553 | __xfrm_state_bump_genids(x1); | |
1554 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); | |
1555 | } | |
1556 | ||
1da177e4 | 1557 | err = 0; |
8fcbc637 TG |
1558 | x->km.state = XFRM_STATE_DEAD; |
1559 | __xfrm_state_put(x); | |
1da177e4 | 1560 | } |
257a4b01 HX |
1561 | |
1562 | fail: | |
1da177e4 LT |
1563 | spin_unlock_bh(&x1->lock); |
1564 | ||
1565 | xfrm_state_put(x1); | |
1566 | ||
1567 | return err; | |
1568 | } | |
1569 | EXPORT_SYMBOL(xfrm_state_update); | |
1570 | ||
1571 | int xfrm_state_check_expire(struct xfrm_state *x) | |
1572 | { | |
1573 | if (!x->curlft.use_time) | |
386c5680 | 1574 | x->curlft.use_time = ktime_get_real_seconds(); |
1da177e4 | 1575 | |
1da177e4 LT |
1576 | if (x->curlft.bytes >= x->lft.hard_byte_limit || |
1577 | x->curlft.packets >= x->lft.hard_packet_limit) { | |
4666faab | 1578 | x->km.state = XFRM_STATE_EXPIRED; |
671422b2 | 1579 | hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT); |
1da177e4 LT |
1580 | return -EINVAL; |
1581 | } | |
1582 | ||
1583 | if (!x->km.dying && | |
1584 | (x->curlft.bytes >= x->lft.soft_byte_limit || | |
4666faab HX |
1585 | x->curlft.packets >= x->lft.soft_packet_limit)) { |
1586 | x->km.dying = 1; | |
53bc6b4d | 1587 | km_state_expired(x, 0, 0); |
4666faab | 1588 | } |
1da177e4 LT |
1589 | return 0; |
1590 | } | |
1591 | EXPORT_SYMBOL(xfrm_state_check_expire); | |
1592 | ||
1da177e4 | 1593 | struct xfrm_state * |
a70486f0 | 1594 | xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi, |
bd55775c | 1595 | u8 proto, unsigned short family) |
1da177e4 LT |
1596 | { |
1597 | struct xfrm_state *x; | |
1da177e4 | 1598 | |
c2f672fc | 1599 | rcu_read_lock(); |
bd55775c | 1600 | x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family); |
c2f672fc | 1601 | rcu_read_unlock(); |
1da177e4 LT |
1602 | return x; |
1603 | } | |
1604 | EXPORT_SYMBOL(xfrm_state_lookup); | |
1605 | ||
1606 | struct xfrm_state * | |
bd55775c | 1607 | xfrm_state_lookup_byaddr(struct net *net, u32 mark, |
a70486f0 | 1608 | const xfrm_address_t *daddr, const xfrm_address_t *saddr, |
eb2971b6 MN |
1609 | u8 proto, unsigned short family) |
1610 | { | |
1611 | struct xfrm_state *x; | |
eb2971b6 | 1612 | |
283bc9f3 | 1613 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
bd55775c | 1614 | x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family); |
283bc9f3 | 1615 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
eb2971b6 MN |
1616 | return x; |
1617 | } | |
1618 | EXPORT_SYMBOL(xfrm_state_lookup_byaddr); | |
1619 | ||
1620 | struct xfrm_state * | |
e473fcb4 | 1621 | xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid, |
7e652640 | 1622 | u32 if_id, u8 proto, const xfrm_address_t *daddr, |
e473fcb4 | 1623 | const xfrm_address_t *saddr, int create, unsigned short family) |
1da177e4 LT |
1624 | { |
1625 | struct xfrm_state *x; | |
1da177e4 | 1626 | |
283bc9f3 | 1627 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
7e652640 | 1628 | x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create); |
283bc9f3 | 1629 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
2770834c | 1630 | |
1da177e4 LT |
1631 | return x; |
1632 | } | |
1633 | EXPORT_SYMBOL(xfrm_find_acq); | |
1634 | ||
41a49cc3 MN |
1635 | #ifdef CONFIG_XFRM_SUB_POLICY |
1636 | int | |
1637 | xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n, | |
283bc9f3 | 1638 | unsigned short family, struct net *net) |
41a49cc3 | 1639 | { |
3f5a95ad | 1640 | int i; |
41a49cc3 MN |
1641 | int err = 0; |
1642 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); | |
1643 | if (!afinfo) | |
1644 | return -EAFNOSUPPORT; | |
1645 | ||
283bc9f3 | 1646 | spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/ |
41a49cc3 MN |
1647 | if (afinfo->tmpl_sort) |
1648 | err = afinfo->tmpl_sort(dst, src, n); | |
3f5a95ad KD |
1649 | else |
1650 | for (i = 0; i < n; i++) | |
1651 | dst[i] = src[i]; | |
283bc9f3 | 1652 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
af5d27c4 | 1653 | rcu_read_unlock(); |
41a49cc3 MN |
1654 | return err; |
1655 | } | |
1656 | EXPORT_SYMBOL(xfrm_tmpl_sort); | |
1657 | ||
1658 | int | |
1659 | xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n, | |
1660 | unsigned short family) | |
1661 | { | |
3f5a95ad | 1662 | int i; |
41a49cc3 MN |
1663 | int err = 0; |
1664 | struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family); | |
35ea790d | 1665 | struct net *net = xs_net(*src); |
283bc9f3 | 1666 | |
41a49cc3 MN |
1667 | if (!afinfo) |
1668 | return -EAFNOSUPPORT; | |
1669 | ||
283bc9f3 | 1670 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
41a49cc3 MN |
1671 | if (afinfo->state_sort) |
1672 | err = afinfo->state_sort(dst, src, n); | |
3f5a95ad KD |
1673 | else |
1674 | for (i = 0; i < n; i++) | |
1675 | dst[i] = src[i]; | |
283bc9f3 | 1676 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
af5d27c4 | 1677 | rcu_read_unlock(); |
41a49cc3 MN |
1678 | return err; |
1679 | } | |
1680 | EXPORT_SYMBOL(xfrm_state_sort); | |
1681 | #endif | |
1682 | ||
1da177e4 LT |
1683 | /* Silly enough, but I'm lazy to build resolution list */ |
1684 | ||
bd55775c | 1685 | static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) |
1da177e4 LT |
1686 | { |
1687 | int i; | |
1da177e4 | 1688 | |
5447c5e4 | 1689 | for (i = 0; i <= net->xfrm.state_hmask; i++) { |
8f126e37 DM |
1690 | struct xfrm_state *x; |
1691 | ||
b67bfe0d | 1692 | hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) { |
8f126e37 | 1693 | if (x->km.seq == seq && |
3d6acfa7 | 1694 | (mark & x->mark.m) == x->mark.v && |
8f126e37 | 1695 | x->km.state == XFRM_STATE_ACQ) { |
1da177e4 LT |
1696 | xfrm_state_hold(x); |
1697 | return x; | |
1698 | } | |
1699 | } | |
1700 | } | |
1701 | return NULL; | |
1702 | } | |
1703 | ||
bd55775c | 1704 | struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq) |
1da177e4 LT |
1705 | { |
1706 | struct xfrm_state *x; | |
1707 | ||
283bc9f3 | 1708 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
bd55775c | 1709 | x = __xfrm_find_acq_byseq(net, mark, seq); |
283bc9f3 | 1710 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
1711 | return x; |
1712 | } | |
1713 | EXPORT_SYMBOL(xfrm_find_acq_byseq); | |
1714 | ||
1715 | u32 xfrm_get_acqseq(void) | |
1716 | { | |
1717 | u32 res; | |
6836b9bd | 1718 | static atomic_t acqseq; |
1719 | ||
1720 | do { | |
1721 | res = atomic_inc_return(&acqseq); | |
1722 | } while (!res); | |
1da177e4 | 1723 | |
1da177e4 LT |
1724 | return res; |
1725 | } | |
1726 | EXPORT_SYMBOL(xfrm_get_acqseq); | |
1727 | ||
776e9dd9 FD |
1728 | int verify_spi_info(u8 proto, u32 min, u32 max) |
1729 | { | |
1730 | switch (proto) { | |
1731 | case IPPROTO_AH: | |
1732 | case IPPROTO_ESP: | |
1733 | break; | |
1734 | ||
1735 | case IPPROTO_COMP: | |
1736 | /* IPCOMP spi is 16-bits. */ | |
1737 | if (max >= 0x10000) | |
1738 | return -EINVAL; | |
1739 | break; | |
1740 | ||
1741 | default: | |
1742 | return -EINVAL; | |
1743 | } | |
1744 | ||
1745 | if (min > max) | |
1746 | return -EINVAL; | |
1747 | ||
1748 | return 0; | |
1749 | } | |
1750 | EXPORT_SYMBOL(verify_spi_info); | |
1751 | ||
658b219e | 1752 | int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) |
1da177e4 | 1753 | { |
221df1ed | 1754 | struct net *net = xs_net(x); |
f034b5d4 | 1755 | unsigned int h; |
1da177e4 | 1756 | struct xfrm_state *x0; |
658b219e HX |
1757 | int err = -ENOENT; |
1758 | __be32 minspi = htonl(low); | |
1759 | __be32 maxspi = htonl(high); | |
bd55775c | 1760 | u32 mark = x->mark.v & x->mark.m; |
1da177e4 | 1761 | |
658b219e HX |
1762 | spin_lock_bh(&x->lock); |
1763 | if (x->km.state == XFRM_STATE_DEAD) | |
1764 | goto unlock; | |
1765 | ||
1766 | err = 0; | |
1da177e4 | 1767 | if (x->id.spi) |
658b219e HX |
1768 | goto unlock; |
1769 | ||
1770 | err = -ENOENT; | |
1da177e4 LT |
1771 | |
1772 | if (minspi == maxspi) { | |
bd55775c | 1773 | x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); |
1da177e4 LT |
1774 | if (x0) { |
1775 | xfrm_state_put(x0); | |
658b219e | 1776 | goto unlock; |
1da177e4 LT |
1777 | } |
1778 | x->id.spi = minspi; | |
1779 | } else { | |
1780 | u32 spi = 0; | |
9b7a787d | 1781 | for (h = 0; h < high-low+1; h++) { |
63862b5b | 1782 | spi = low + prandom_u32()%(high-low+1); |
bd55775c | 1783 | x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); |
1da177e4 LT |
1784 | if (x0 == NULL) { |
1785 | x->id.spi = htonl(spi); | |
1786 | break; | |
1787 | } | |
1788 | xfrm_state_put(x0); | |
1789 | } | |
1790 | } | |
1791 | if (x->id.spi) { | |
283bc9f3 | 1792 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
12604d8a | 1793 | h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); |
ae3fb6d3 | 1794 | hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h); |
283bc9f3 | 1795 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
658b219e HX |
1796 | |
1797 | err = 0; | |
1da177e4 | 1798 | } |
658b219e HX |
1799 | |
1800 | unlock: | |
1801 | spin_unlock_bh(&x->lock); | |
1802 | ||
1803 | return err; | |
1da177e4 LT |
1804 | } |
1805 | EXPORT_SYMBOL(xfrm_alloc_spi); | |
1806 | ||
d3623099 | 1807 | static bool __xfrm_state_filter_match(struct xfrm_state *x, |
870a2df4 | 1808 | struct xfrm_address_filter *filter) |
d3623099 ND |
1809 | { |
1810 | if (filter) { | |
1811 | if ((filter->family == AF_INET || | |
1812 | filter->family == AF_INET6) && | |
1813 | x->props.family != filter->family) | |
1814 | return false; | |
1815 | ||
1816 | return addr_match(&x->props.saddr, &filter->saddr, | |
1817 | filter->splen) && | |
1818 | addr_match(&x->id.daddr, &filter->daddr, | |
1819 | filter->dplen); | |
1820 | } | |
1821 | return true; | |
1822 | } | |
1823 | ||
284fa7da | 1824 | int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk, |
4c563f76 | 1825 | int (*func)(struct xfrm_state *, int, void*), |
1da177e4 LT |
1826 | void *data) |
1827 | { | |
12a169e7 HX |
1828 | struct xfrm_state *state; |
1829 | struct xfrm_state_walk *x; | |
1da177e4 LT |
1830 | int err = 0; |
1831 | ||
12a169e7 | 1832 | if (walk->seq != 0 && list_empty(&walk->all)) |
4c563f76 TT |
1833 | return 0; |
1834 | ||
283bc9f3 | 1835 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
12a169e7 | 1836 | if (list_empty(&walk->all)) |
284fa7da | 1837 | x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all); |
12a169e7 | 1838 | else |
80077702 | 1839 | x = list_first_entry(&walk->all, struct xfrm_state_walk, all); |
284fa7da | 1840 | list_for_each_entry_from(x, &net->xfrm.state_all, all) { |
12a169e7 | 1841 | if (x->state == XFRM_STATE_DEAD) |
4c563f76 | 1842 | continue; |
12a169e7 HX |
1843 | state = container_of(x, struct xfrm_state, km); |
1844 | if (!xfrm_id_proto_match(state->id.proto, walk->proto)) | |
4c563f76 | 1845 | continue; |
d3623099 ND |
1846 | if (!__xfrm_state_filter_match(state, walk->filter)) |
1847 | continue; | |
12a169e7 HX |
1848 | err = func(state, walk->seq, data); |
1849 | if (err) { | |
1850 | list_move_tail(&walk->all, &x->all); | |
1851 | goto out; | |
1da177e4 | 1852 | } |
12a169e7 | 1853 | walk->seq++; |
1da177e4 | 1854 | } |
12a169e7 | 1855 | if (walk->seq == 0) { |
1da177e4 LT |
1856 | err = -ENOENT; |
1857 | goto out; | |
1858 | } | |
12a169e7 | 1859 | list_del_init(&walk->all); |
1da177e4 | 1860 | out: |
283bc9f3 | 1861 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
1da177e4 LT |
1862 | return err; |
1863 | } | |
1864 | EXPORT_SYMBOL(xfrm_state_walk); | |
1865 | ||
d3623099 | 1866 | void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto, |
870a2df4 | 1867 | struct xfrm_address_filter *filter) |
5c182458 | 1868 | { |
12a169e7 | 1869 | INIT_LIST_HEAD(&walk->all); |
5c182458 | 1870 | walk->proto = proto; |
12a169e7 HX |
1871 | walk->state = XFRM_STATE_DEAD; |
1872 | walk->seq = 0; | |
d3623099 | 1873 | walk->filter = filter; |
5c182458 HX |
1874 | } |
1875 | EXPORT_SYMBOL(xfrm_state_walk_init); | |
1876 | ||
283bc9f3 | 1877 | void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net) |
abb81c4f | 1878 | { |
d3623099 ND |
1879 | kfree(walk->filter); |
1880 | ||
12a169e7 | 1881 | if (list_empty(&walk->all)) |
5c182458 | 1882 | return; |
5c182458 | 1883 | |
283bc9f3 | 1884 | spin_lock_bh(&net->xfrm.xfrm_state_lock); |
12a169e7 | 1885 | list_del(&walk->all); |
283bc9f3 | 1886 | spin_unlock_bh(&net->xfrm.xfrm_state_lock); |
abb81c4f HX |
1887 | } |
1888 | EXPORT_SYMBOL(xfrm_state_walk_done); | |
1889 | ||
e99e88a9 | 1890 | static void xfrm_replay_timer_handler(struct timer_list *t) |
f8cd5488 | 1891 | { |
e99e88a9 | 1892 | struct xfrm_state *x = from_timer(x, t, rtimer); |
f8cd5488 JHS |
1893 | |
1894 | spin_lock(&x->lock); | |
1895 | ||
2717096a | 1896 | if (x->km.state == XFRM_STATE_VALID) { |
a6483b79 | 1897 | if (xfrm_aevent_is_on(xs_net(x))) |
9fdc4883 | 1898 | x->repl->notify(x, XFRM_REPLAY_TIMEOUT); |
2717096a JHS |
1899 | else |
1900 | x->xflags |= XFRM_TIME_DEFER; | |
1901 | } | |
f8cd5488 JHS |
1902 | |
1903 | spin_unlock(&x->lock); | |
1904 | } | |
1905 | ||
df01812e | 1906 | static LIST_HEAD(xfrm_km_list); |
1da177e4 | 1907 | |
214e005b | 1908 | void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c) |
1da177e4 LT |
1909 | { |
1910 | struct xfrm_mgr *km; | |
1911 | ||
85168c00 CW |
1912 | rcu_read_lock(); |
1913 | list_for_each_entry_rcu(km, &xfrm_km_list, list) | |
26b15dad JHS |
1914 | if (km->notify_policy) |
1915 | km->notify_policy(xp, dir, c); | |
85168c00 | 1916 | rcu_read_unlock(); |
26b15dad | 1917 | } |
1da177e4 | 1918 | |
214e005b | 1919 | void km_state_notify(struct xfrm_state *x, const struct km_event *c) |
26b15dad JHS |
1920 | { |
1921 | struct xfrm_mgr *km; | |
85168c00 CW |
1922 | rcu_read_lock(); |
1923 | list_for_each_entry_rcu(km, &xfrm_km_list, list) | |
26b15dad JHS |
1924 | if (km->notify) |
1925 | km->notify(x, c); | |
85168c00 | 1926 | rcu_read_unlock(); |
26b15dad JHS |
1927 | } |
1928 | ||
1929 | EXPORT_SYMBOL(km_policy_notify); | |
1930 | EXPORT_SYMBOL(km_state_notify); | |
1931 | ||
15e47304 | 1932 | void km_state_expired(struct xfrm_state *x, int hard, u32 portid) |
26b15dad JHS |
1933 | { |
1934 | struct km_event c; | |
1935 | ||
bf08867f | 1936 | c.data.hard = hard; |
15e47304 | 1937 | c.portid = portid; |
f60f6b8f | 1938 | c.event = XFRM_MSG_EXPIRE; |
26b15dad | 1939 | km_state_notify(x, &c); |
1da177e4 LT |
1940 | } |
1941 | ||
53bc6b4d | 1942 | EXPORT_SYMBOL(km_state_expired); |
26b15dad JHS |
1943 | /* |
1944 | * We send to all registered managers regardless of failure | |
1945 | * We are happy with one success | |
1946 | */ | |
980ebd25 | 1947 | int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol) |
1da177e4 | 1948 | { |
26b15dad | 1949 | int err = -EINVAL, acqret; |
1da177e4 LT |
1950 | struct xfrm_mgr *km; |
1951 | ||
85168c00 CW |
1952 | rcu_read_lock(); |
1953 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
65e0736b | 1954 | acqret = km->acquire(x, t, pol); |
26b15dad JHS |
1955 | if (!acqret) |
1956 | err = acqret; | |
1da177e4 | 1957 | } |
85168c00 | 1958 | rcu_read_unlock(); |
1da177e4 LT |
1959 | return err; |
1960 | } | |
980ebd25 | 1961 | EXPORT_SYMBOL(km_query); |
1da177e4 | 1962 | |
5d36b180 | 1963 | int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport) |
1da177e4 LT |
1964 | { |
1965 | int err = -EINVAL; | |
1966 | struct xfrm_mgr *km; | |
1967 | ||
85168c00 CW |
1968 | rcu_read_lock(); |
1969 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
1da177e4 LT |
1970 | if (km->new_mapping) |
1971 | err = km->new_mapping(x, ipaddr, sport); | |
1972 | if (!err) | |
1973 | break; | |
1974 | } | |
85168c00 | 1975 | rcu_read_unlock(); |
1da177e4 LT |
1976 | return err; |
1977 | } | |
1978 | EXPORT_SYMBOL(km_new_mapping); | |
1979 | ||
15e47304 | 1980 | void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid) |
1da177e4 | 1981 | { |
26b15dad | 1982 | struct km_event c; |
1da177e4 | 1983 | |
bf08867f | 1984 | c.data.hard = hard; |
15e47304 | 1985 | c.portid = portid; |
f60f6b8f | 1986 | c.event = XFRM_MSG_POLEXPIRE; |
26b15dad | 1987 | km_policy_notify(pol, dir, &c); |
1da177e4 | 1988 | } |
a70fcb0b | 1989 | EXPORT_SYMBOL(km_policy_expired); |
1da177e4 | 1990 | |
2d60abc2 | 1991 | #ifdef CONFIG_XFRM_MIGRATE |
183cad12 DM |
1992 | int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type, |
1993 | const struct xfrm_migrate *m, int num_migrate, | |
8bafd730 AA |
1994 | const struct xfrm_kmaddress *k, |
1995 | const struct xfrm_encap_tmpl *encap) | |
80c9abaa SS |
1996 | { |
1997 | int err = -EINVAL; | |
1998 | int ret; | |
1999 | struct xfrm_mgr *km; | |
2000 | ||
85168c00 CW |
2001 | rcu_read_lock(); |
2002 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
80c9abaa | 2003 | if (km->migrate) { |
8bafd730 AA |
2004 | ret = km->migrate(sel, dir, type, m, num_migrate, k, |
2005 | encap); | |
80c9abaa SS |
2006 | if (!ret) |
2007 | err = ret; | |
2008 | } | |
2009 | } | |
85168c00 | 2010 | rcu_read_unlock(); |
80c9abaa SS |
2011 | return err; |
2012 | } | |
2013 | EXPORT_SYMBOL(km_migrate); | |
2d60abc2 | 2014 | #endif |
80c9abaa | 2015 | |
db983c11 | 2016 | int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr) |
97a64b45 MN |
2017 | { |
2018 | int err = -EINVAL; | |
2019 | int ret; | |
2020 | struct xfrm_mgr *km; | |
2021 | ||
85168c00 CW |
2022 | rcu_read_lock(); |
2023 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
97a64b45 | 2024 | if (km->report) { |
db983c11 | 2025 | ret = km->report(net, proto, sel, addr); |
97a64b45 MN |
2026 | if (!ret) |
2027 | err = ret; | |
2028 | } | |
2029 | } | |
85168c00 | 2030 | rcu_read_unlock(); |
97a64b45 MN |
2031 | return err; |
2032 | } | |
2033 | EXPORT_SYMBOL(km_report); | |
2034 | ||
bb9cd077 | 2035 | static bool km_is_alive(const struct km_event *c) |
0f24558e HG |
2036 | { |
2037 | struct xfrm_mgr *km; | |
2038 | bool is_alive = false; | |
2039 | ||
2040 | rcu_read_lock(); | |
2041 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
2042 | if (km->is_alive && km->is_alive(c)) { | |
2043 | is_alive = true; | |
2044 | break; | |
2045 | } | |
2046 | } | |
2047 | rcu_read_unlock(); | |
2048 | ||
2049 | return is_alive; | |
2050 | } | |
0f24558e | 2051 | |
1da177e4 LT |
2052 | int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen) |
2053 | { | |
2054 | int err; | |
2055 | u8 *data; | |
2056 | struct xfrm_mgr *km; | |
2057 | struct xfrm_policy *pol = NULL; | |
2058 | ||
19d7df69 SK |
2059 | if (in_compat_syscall()) |
2060 | return -EOPNOTSUPP; | |
19d7df69 | 2061 | |
be8f8284 LC |
2062 | if (!optval && !optlen) { |
2063 | xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL); | |
2064 | xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL); | |
2065 | __sk_dst_reset(sk); | |
2066 | return 0; | |
2067 | } | |
2068 | ||
1da177e4 LT |
2069 | if (optlen <= 0 || optlen > PAGE_SIZE) |
2070 | return -EMSGSIZE; | |
2071 | ||
a133d930 GT |
2072 | data = memdup_user(optval, optlen); |
2073 | if (IS_ERR(data)) | |
2074 | return PTR_ERR(data); | |
1da177e4 LT |
2075 | |
2076 | err = -EINVAL; | |
85168c00 CW |
2077 | rcu_read_lock(); |
2078 | list_for_each_entry_rcu(km, &xfrm_km_list, list) { | |
cb969f07 | 2079 | pol = km->compile_policy(sk, optname, data, |
1da177e4 LT |
2080 | optlen, &err); |
2081 | if (err >= 0) | |
2082 | break; | |
2083 | } | |
85168c00 | 2084 | rcu_read_unlock(); |
1da177e4 LT |
2085 | |
2086 | if (err >= 0) { | |
2087 | xfrm_sk_policy_insert(sk, err, pol); | |
2088 | xfrm_pol_put(pol); | |
2b06cdf3 | 2089 | __sk_dst_reset(sk); |
1da177e4 LT |
2090 | err = 0; |
2091 | } | |
2092 | ||
1da177e4 LT |
2093 | kfree(data); |
2094 | return err; | |
2095 | } | |
2096 | EXPORT_SYMBOL(xfrm_user_policy); | |
2097 | ||
85168c00 CW |
2098 | static DEFINE_SPINLOCK(xfrm_km_lock); |
2099 | ||
1da177e4 LT |
2100 | int xfrm_register_km(struct xfrm_mgr *km) |
2101 | { | |
85168c00 CW |
2102 | spin_lock_bh(&xfrm_km_lock); |
2103 | list_add_tail_rcu(&km->list, &xfrm_km_list); | |
2104 | spin_unlock_bh(&xfrm_km_lock); | |
1da177e4 LT |
2105 | return 0; |
2106 | } | |
2107 | EXPORT_SYMBOL(xfrm_register_km); | |
2108 | ||
2109 | int xfrm_unregister_km(struct xfrm_mgr *km) | |
2110 | { | |
85168c00 CW |
2111 | spin_lock_bh(&xfrm_km_lock); |
2112 | list_del_rcu(&km->list); | |
2113 | spin_unlock_bh(&xfrm_km_lock); | |
2114 | synchronize_rcu(); | |
1da177e4 LT |
2115 | return 0; |
2116 | } | |
2117 | EXPORT_SYMBOL(xfrm_unregister_km); | |
2118 | ||
2119 | int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo) | |
2120 | { | |
2121 | int err = 0; | |
423826a7 FW |
2122 | |
2123 | if (WARN_ON(afinfo->family >= NPROTO)) | |
1da177e4 | 2124 | return -EAFNOSUPPORT; |
423826a7 | 2125 | |
44abdc30 | 2126 | spin_lock_bh(&xfrm_state_afinfo_lock); |
1da177e4 | 2127 | if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL)) |
f31e8d4f | 2128 | err = -EEXIST; |
edcd5821 | 2129 | else |
44abdc30 CW |
2130 | rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo); |
2131 | spin_unlock_bh(&xfrm_state_afinfo_lock); | |
1da177e4 LT |
2132 | return err; |
2133 | } | |
2134 | EXPORT_SYMBOL(xfrm_state_register_afinfo); | |
2135 | ||
2136 | int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo) | |
2137 | { | |
423826a7 FW |
2138 | int err = 0, family = afinfo->family; |
2139 | ||
2140 | if (WARN_ON(family >= NPROTO)) | |
1da177e4 | 2141 | return -EAFNOSUPPORT; |
423826a7 | 2142 | |
44abdc30 | 2143 | spin_lock_bh(&xfrm_state_afinfo_lock); |
1da177e4 | 2144 | if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) { |
423826a7 | 2145 | if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo) |
1da177e4 | 2146 | err = -EINVAL; |
edcd5821 | 2147 | else |
44abdc30 | 2148 | RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL); |
1da177e4 | 2149 | } |
44abdc30 CW |
2150 | spin_unlock_bh(&xfrm_state_afinfo_lock); |
2151 | synchronize_rcu(); | |
1da177e4 LT |
2152 | return err; |
2153 | } | |
2154 | EXPORT_SYMBOL(xfrm_state_unregister_afinfo); | |
2155 | ||
711059b9 FW |
2156 | struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family) |
2157 | { | |
2158 | if (unlikely(family >= NPROTO)) | |
2159 | return NULL; | |
2160 | ||
2161 | return rcu_dereference(xfrm_state_afinfo[family]); | |
2162 | } | |
733a5fac | 2163 | EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu); |
711059b9 | 2164 | |
628e341f | 2165 | struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family) |
1da177e4 LT |
2166 | { |
2167 | struct xfrm_state_afinfo *afinfo; | |
2168 | if (unlikely(family >= NPROTO)) | |
2169 | return NULL; | |
44abdc30 CW |
2170 | rcu_read_lock(); |
2171 | afinfo = rcu_dereference(xfrm_state_afinfo[family]); | |
546be240 | 2172 | if (unlikely(!afinfo)) |
44abdc30 | 2173 | rcu_read_unlock(); |
1da177e4 LT |
2174 | return afinfo; |
2175 | } | |
2176 | ||
b48c05ab SK |
2177 | void xfrm_flush_gc(void) |
2178 | { | |
2179 | flush_work(&xfrm_state_gc_work); | |
2180 | } | |
2181 | EXPORT_SYMBOL(xfrm_flush_gc); | |
2182 | ||
1da177e4 LT |
2183 | /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */ |
2184 | void xfrm_state_delete_tunnel(struct xfrm_state *x) | |
2185 | { | |
2186 | if (x->tunnel) { | |
2187 | struct xfrm_state *t = x->tunnel; | |
2188 | ||
2189 | if (atomic_read(&t->tunnel_users) == 2) | |
2190 | xfrm_state_delete(t); | |
2191 | atomic_dec(&t->tunnel_users); | |
f75a2804 | 2192 | xfrm_state_put_sync(t); |
1da177e4 LT |
2193 | x->tunnel = NULL; |
2194 | } | |
2195 | } | |
2196 | EXPORT_SYMBOL(xfrm_state_delete_tunnel); | |
2197 | ||
2198 | int xfrm_state_mtu(struct xfrm_state *x, int mtu) | |
2199 | { | |
b3b73b8e | 2200 | const struct xfrm_type *type = READ_ONCE(x->type); |
1da177e4 | 2201 | |
c5c25238 | 2202 | if (x->km.state == XFRM_STATE_VALID && |
b3b73b8e FW |
2203 | type && type->get_mtu) |
2204 | return type->get_mtu(x, mtu); | |
2205 | ||
2206 | return mtu - x->props.header_len; | |
1da177e4 LT |
2207 | } |
2208 | ||
ffdb5211 | 2209 | int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload) |
72cb6962 | 2210 | { |
4c145dce | 2211 | const struct xfrm_state_afinfo *afinfo; |
c9500d7b FW |
2212 | const struct xfrm_mode *inner_mode; |
2213 | const struct xfrm_mode *outer_mode; | |
d094cd83 | 2214 | int family = x->props.family; |
72cb6962 HX |
2215 | int err; |
2216 | ||
d094cd83 HX |
2217 | err = -EAFNOSUPPORT; |
2218 | afinfo = xfrm_state_get_afinfo(family); | |
2219 | if (!afinfo) | |
2220 | goto error; | |
2221 | ||
2222 | err = 0; | |
2223 | if (afinfo->init_flags) | |
2224 | err = afinfo->init_flags(x); | |
2225 | ||
af5d27c4 | 2226 | rcu_read_unlock(); |
d094cd83 HX |
2227 | |
2228 | if (err) | |
2229 | goto error; | |
2230 | ||
2231 | err = -EPROTONOSUPPORT; | |
13996378 | 2232 | |
df9dcb45 KM |
2233 | if (x->sel.family != AF_UNSPEC) { |
2234 | inner_mode = xfrm_get_mode(x->props.mode, x->sel.family); | |
2235 | if (inner_mode == NULL) | |
2236 | goto error; | |
2237 | ||
2238 | if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) && | |
4c145dce | 2239 | family != x->sel.family) |
df9dcb45 | 2240 | goto error; |
df9dcb45 | 2241 | |
c9500d7b | 2242 | x->inner_mode = *inner_mode; |
df9dcb45 | 2243 | } else { |
4c145dce | 2244 | const struct xfrm_mode *inner_mode_iaf; |
d81d2285 | 2245 | int iafamily = AF_INET; |
df9dcb45 | 2246 | |
d81d2285 | 2247 | inner_mode = xfrm_get_mode(x->props.mode, x->props.family); |
df9dcb45 KM |
2248 | if (inner_mode == NULL) |
2249 | goto error; | |
2250 | ||
4c145dce | 2251 | if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) |
df9dcb45 | 2252 | goto error; |
4c145dce | 2253 | |
c9500d7b | 2254 | x->inner_mode = *inner_mode; |
df9dcb45 | 2255 | |
d81d2285 MW |
2256 | if (x->props.family == AF_INET) |
2257 | iafamily = AF_INET6; | |
df9dcb45 | 2258 | |
d81d2285 MW |
2259 | inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily); |
2260 | if (inner_mode_iaf) { | |
2261 | if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL) | |
c9500d7b | 2262 | x->inner_mode_iaf = *inner_mode_iaf; |
df9dcb45 KM |
2263 | } |
2264 | } | |
13996378 | 2265 | |
d094cd83 | 2266 | x->type = xfrm_get_type(x->id.proto, family); |
72cb6962 HX |
2267 | if (x->type == NULL) |
2268 | goto error; | |
2269 | ||
ffdb5211 | 2270 | x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload); |
9d389d7f | 2271 | |
72cb6962 HX |
2272 | err = x->type->init_state(x); |
2273 | if (err) | |
2274 | goto error; | |
2275 | ||
c9500d7b FW |
2276 | outer_mode = xfrm_get_mode(x->props.mode, family); |
2277 | if (!outer_mode) { | |
599901c3 | 2278 | err = -EPROTONOSUPPORT; |
b59f45d0 | 2279 | goto error; |
599901c3 | 2280 | } |
b59f45d0 | 2281 | |
c9500d7b | 2282 | x->outer_mode = *outer_mode; |
a454f0cc WY |
2283 | if (init_replay) { |
2284 | err = xfrm_init_replay(x); | |
2285 | if (err) | |
2286 | goto error; | |
2287 | } | |
2288 | ||
72cb6962 HX |
2289 | error: |
2290 | return err; | |
2291 | } | |
2292 | ||
a454f0cc WY |
2293 | EXPORT_SYMBOL(__xfrm_init_state); |
2294 | ||
2295 | int xfrm_init_state(struct xfrm_state *x) | |
2296 | { | |
cc01572e YK |
2297 | int err; |
2298 | ||
2299 | err = __xfrm_init_state(x, true, false); | |
2300 | if (!err) | |
2301 | x->km.state = XFRM_STATE_VALID; | |
2302 | ||
2303 | return err; | |
a454f0cc WY |
2304 | } |
2305 | ||
72cb6962 | 2306 | EXPORT_SYMBOL(xfrm_init_state); |
a716c119 | 2307 | |
d62ddc21 | 2308 | int __net_init xfrm_state_init(struct net *net) |
1da177e4 | 2309 | { |
f034b5d4 DM |
2310 | unsigned int sz; |
2311 | ||
565f0fa9 MK |
2312 | if (net_eq(net, &init_net)) |
2313 | xfrm_state_cache = KMEM_CACHE(xfrm_state, | |
2314 | SLAB_HWCACHE_ALIGN | SLAB_PANIC); | |
2315 | ||
9d4139c7 AD |
2316 | INIT_LIST_HEAD(&net->xfrm.state_all); |
2317 | ||
f034b5d4 DM |
2318 | sz = sizeof(struct hlist_head) * 8; |
2319 | ||
73d189dc AD |
2320 | net->xfrm.state_bydst = xfrm_hash_alloc(sz); |
2321 | if (!net->xfrm.state_bydst) | |
2322 | goto out_bydst; | |
d320bbb3 AD |
2323 | net->xfrm.state_bysrc = xfrm_hash_alloc(sz); |
2324 | if (!net->xfrm.state_bysrc) | |
2325 | goto out_bysrc; | |
b754a4fd AD |
2326 | net->xfrm.state_byspi = xfrm_hash_alloc(sz); |
2327 | if (!net->xfrm.state_byspi) | |
2328 | goto out_byspi; | |
529983ec | 2329 | net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1); |
1da177e4 | 2330 | |
0bf7c5b0 | 2331 | net->xfrm.state_num = 0; |
63082733 | 2332 | INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize); |
283bc9f3 | 2333 | spin_lock_init(&net->xfrm.xfrm_state_lock); |
d62ddc21 | 2334 | return 0; |
73d189dc | 2335 | |
b754a4fd AD |
2336 | out_byspi: |
2337 | xfrm_hash_free(net->xfrm.state_bysrc, sz); | |
d320bbb3 AD |
2338 | out_bysrc: |
2339 | xfrm_hash_free(net->xfrm.state_bydst, sz); | |
73d189dc AD |
2340 | out_bydst: |
2341 | return -ENOMEM; | |
d62ddc21 AD |
2342 | } |
2343 | ||
2344 | void xfrm_state_fini(struct net *net) | |
2345 | { | |
73d189dc AD |
2346 | unsigned int sz; |
2347 | ||
7c2776ee | 2348 | flush_work(&net->xfrm.state_hash_work); |
35db57bb | 2349 | flush_work(&xfrm_state_gc_work); |
dbb2483b | 2350 | xfrm_state_flush(net, 0, false, true); |
7c2776ee | 2351 | |
9d4139c7 | 2352 | WARN_ON(!list_empty(&net->xfrm.state_all)); |
73d189dc | 2353 | |
529983ec | 2354 | sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head); |
b754a4fd AD |
2355 | WARN_ON(!hlist_empty(net->xfrm.state_byspi)); |
2356 | xfrm_hash_free(net->xfrm.state_byspi, sz); | |
d320bbb3 AD |
2357 | WARN_ON(!hlist_empty(net->xfrm.state_bysrc)); |
2358 | xfrm_hash_free(net->xfrm.state_bysrc, sz); | |
73d189dc AD |
2359 | WARN_ON(!hlist_empty(net->xfrm.state_bydst)); |
2360 | xfrm_hash_free(net->xfrm.state_bydst, sz); | |
1da177e4 LT |
2361 | } |
2362 | ||
ab5f5e8b | 2363 | #ifdef CONFIG_AUDITSYSCALL |
cf35f43e IJ |
2364 | static void xfrm_audit_helper_sainfo(struct xfrm_state *x, |
2365 | struct audit_buffer *audit_buf) | |
ab5f5e8b | 2366 | { |
68277acc PM |
2367 | struct xfrm_sec_ctx *ctx = x->security; |
2368 | u32 spi = ntohl(x->id.spi); | |
2369 | ||
2370 | if (ctx) | |
ab5f5e8b | 2371 | audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s", |
68277acc | 2372 | ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str); |
ab5f5e8b | 2373 | |
9b7a787d | 2374 | switch (x->props.family) { |
ab5f5e8b | 2375 | case AF_INET: |
21454aaa HH |
2376 | audit_log_format(audit_buf, " src=%pI4 dst=%pI4", |
2377 | &x->props.saddr.a4, &x->id.daddr.a4); | |
ab5f5e8b JL |
2378 | break; |
2379 | case AF_INET6: | |
5b095d98 | 2380 | audit_log_format(audit_buf, " src=%pI6 dst=%pI6", |
fdb46ee7 | 2381 | x->props.saddr.a6, x->id.daddr.a6); |
ab5f5e8b JL |
2382 | break; |
2383 | } | |
68277acc PM |
2384 | |
2385 | audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); | |
ab5f5e8b JL |
2386 | } |
2387 | ||
cf35f43e IJ |
2388 | static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family, |
2389 | struct audit_buffer *audit_buf) | |
afeb14b4 | 2390 | { |
b71d1d42 ED |
2391 | const struct iphdr *iph4; |
2392 | const struct ipv6hdr *iph6; | |
afeb14b4 PM |
2393 | |
2394 | switch (family) { | |
2395 | case AF_INET: | |
2396 | iph4 = ip_hdr(skb); | |
21454aaa HH |
2397 | audit_log_format(audit_buf, " src=%pI4 dst=%pI4", |
2398 | &iph4->saddr, &iph4->daddr); | |
afeb14b4 PM |
2399 | break; |
2400 | case AF_INET6: | |
2401 | iph6 = ipv6_hdr(skb); | |
2402 | audit_log_format(audit_buf, | |
5b095d98 | 2403 | " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x", |
9b7a787d | 2404 | &iph6->saddr, &iph6->daddr, |
afeb14b4 PM |
2405 | iph6->flow_lbl[0] & 0x0f, |
2406 | iph6->flow_lbl[1], | |
2407 | iph6->flow_lbl[2]); | |
2408 | break; | |
2409 | } | |
2410 | } | |
2411 | ||
2e71029e | 2412 | void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid) |
ab5f5e8b JL |
2413 | { |
2414 | struct audit_buffer *audit_buf; | |
ab5f5e8b | 2415 | |
afeb14b4 | 2416 | audit_buf = xfrm_audit_start("SAD-add"); |
ab5f5e8b JL |
2417 | if (audit_buf == NULL) |
2418 | return; | |
2e71029e | 2419 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
afeb14b4 PM |
2420 | xfrm_audit_helper_sainfo(x, audit_buf); |
2421 | audit_log_format(audit_buf, " res=%u", result); | |
ab5f5e8b JL |
2422 | audit_log_end(audit_buf); |
2423 | } | |
2424 | EXPORT_SYMBOL_GPL(xfrm_audit_state_add); | |
2425 | ||
2e71029e | 2426 | void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid) |
ab5f5e8b JL |
2427 | { |
2428 | struct audit_buffer *audit_buf; | |
ab5f5e8b | 2429 | |
afeb14b4 | 2430 | audit_buf = xfrm_audit_start("SAD-delete"); |
ab5f5e8b JL |
2431 | if (audit_buf == NULL) |
2432 | return; | |
2e71029e | 2433 | xfrm_audit_helper_usrinfo(task_valid, audit_buf); |
afeb14b4 PM |
2434 | xfrm_audit_helper_sainfo(x, audit_buf); |
2435 | audit_log_format(audit_buf, " res=%u", result); | |
ab5f5e8b JL |
2436 | audit_log_end(audit_buf); |
2437 | } | |
2438 | EXPORT_SYMBOL_GPL(xfrm_audit_state_delete); | |
afeb14b4 PM |
2439 | |
2440 | void xfrm_audit_state_replay_overflow(struct xfrm_state *x, | |
2441 | struct sk_buff *skb) | |
2442 | { | |
2443 | struct audit_buffer *audit_buf; | |
2444 | u32 spi; | |
2445 | ||
2446 | audit_buf = xfrm_audit_start("SA-replay-overflow"); | |
2447 | if (audit_buf == NULL) | |
2448 | return; | |
2449 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); | |
2450 | /* don't record the sequence number because it's inherent in this kind | |
2451 | * of audit message */ | |
2452 | spi = ntohl(x->id.spi); | |
2453 | audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi); | |
2454 | audit_log_end(audit_buf); | |
2455 | } | |
2456 | EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow); | |
2457 | ||
9fdc4883 | 2458 | void xfrm_audit_state_replay(struct xfrm_state *x, |
afeb14b4 PM |
2459 | struct sk_buff *skb, __be32 net_seq) |
2460 | { | |
2461 | struct audit_buffer *audit_buf; | |
2462 | u32 spi; | |
2463 | ||
2464 | audit_buf = xfrm_audit_start("SA-replayed-pkt"); | |
2465 | if (audit_buf == NULL) | |
2466 | return; | |
2467 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); | |
2468 | spi = ntohl(x->id.spi); | |
2469 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", | |
2470 | spi, spi, ntohl(net_seq)); | |
2471 | audit_log_end(audit_buf); | |
2472 | } | |
9fdc4883 | 2473 | EXPORT_SYMBOL_GPL(xfrm_audit_state_replay); |
afeb14b4 PM |
2474 | |
2475 | void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family) | |
2476 | { | |
2477 | struct audit_buffer *audit_buf; | |
2478 | ||
2479 | audit_buf = xfrm_audit_start("SA-notfound"); | |
2480 | if (audit_buf == NULL) | |
2481 | return; | |
2482 | xfrm_audit_helper_pktinfo(skb, family, audit_buf); | |
2483 | audit_log_end(audit_buf); | |
2484 | } | |
2485 | EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple); | |
2486 | ||
2487 | void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, | |
2488 | __be32 net_spi, __be32 net_seq) | |
2489 | { | |
2490 | struct audit_buffer *audit_buf; | |
2491 | u32 spi; | |
2492 | ||
2493 | audit_buf = xfrm_audit_start("SA-notfound"); | |
2494 | if (audit_buf == NULL) | |
2495 | return; | |
2496 | xfrm_audit_helper_pktinfo(skb, family, audit_buf); | |
2497 | spi = ntohl(net_spi); | |
2498 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", | |
2499 | spi, spi, ntohl(net_seq)); | |
2500 | audit_log_end(audit_buf); | |
2501 | } | |
2502 | EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound); | |
2503 | ||
2504 | void xfrm_audit_state_icvfail(struct xfrm_state *x, | |
2505 | struct sk_buff *skb, u8 proto) | |
2506 | { | |
2507 | struct audit_buffer *audit_buf; | |
2508 | __be32 net_spi; | |
2509 | __be32 net_seq; | |
2510 | ||
2511 | audit_buf = xfrm_audit_start("SA-icv-failure"); | |
2512 | if (audit_buf == NULL) | |
2513 | return; | |
2514 | xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf); | |
2515 | if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) { | |
2516 | u32 spi = ntohl(net_spi); | |
2517 | audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u", | |
2518 | spi, spi, ntohl(net_seq)); | |
2519 | } | |
2520 | audit_log_end(audit_buf); | |
2521 | } | |
2522 | EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail); | |
ab5f5e8b | 2523 | #endif /* CONFIG_AUDITSYSCALL */ |