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