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