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