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