blob: 89c731f4f0c7283e28fc7697c8729a5d1064da97 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
Trent Jaegerdf718372005-12-13 23:12:27 -080014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Jakub Kicinskib6459412021-12-28 16:49:13 -080017#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/workqueue.h>
19#include <net/xfrm.h>
20#include <linux/pfkeyv2.h>
21#include <linux/ipsec.h>
22#include <linux/module.h>
David S. Millerf034b5d2006-08-24 03:08:07 -070023#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080024#include <linux/audit.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080025#include <linux/uaccess.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080026#include <linux/ktime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -080028#include <linux/interrupt.h>
29#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Florian Westphalc7b37c72019-06-24 22:04:48 +020031#include <crypto/aead.h>
32
David S. Miller44e36b42006-08-24 04:50:50 -070033#include "xfrm_hash.h"
34
Florian Westphalc8406992016-08-09 12:16:08 +020035#define xfrm_state_deref_prot(table, net) \
36 rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
37
Florian Westphal35db57bb2016-08-23 16:00:12 +020038static void xfrm_state_gc_task(struct work_struct *work);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* 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)
David S. Millera624c102006-08-24 03:24:33 -070043 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 destination/tunnel endpoint. (output)
45 */
46
David S. Millerf034b5d2006-08-24 03:08:07 -070047static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
Mathias Krause565f0fa2018-05-03 10:55:07 +020048static struct kmem_cache *xfrm_state_cache __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Florian Westphal35db57bb2016-08-23 16:00:12 +020050static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
51static HLIST_HEAD(xfrm_state_gc_list);
52
Florian Westphal02efdff2016-08-09 12:16:05 +020053static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
54{
Reshetova, Elena88755e9c2017-07-04 15:53:21 +030055 return refcount_inc_not_zero(&x->refcnt);
Florian Westphal02efdff2016-08-09 12:16:05 +020056}
57
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080058static inline unsigned int xfrm_dst_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050059 const xfrm_address_t *daddr,
60 const xfrm_address_t *saddr,
David S. Millerc1969f22006-08-24 04:00:03 -070061 u32 reqid,
David S. Millera624c102006-08-24 03:24:33 -070062 unsigned short family)
63{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080064 return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
David S. Millera624c102006-08-24 03:24:33 -070065}
66
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080067static inline unsigned int xfrm_src_hash(struct net *net,
David S. Miller2ab38502011-02-24 01:47:16 -050068 const xfrm_address_t *daddr,
69 const xfrm_address_t *saddr,
David S. Miller44e36b42006-08-24 04:50:50 -070070 unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070071{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080072 return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070073}
74
David S. Miller2575b652006-08-24 03:26:44 -070075static inline unsigned int
David S. Miller2ab38502011-02-24 01:47:16 -050076xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
77 __be32 spi, u8 proto, unsigned short family)
David S. Millerf034b5d2006-08-24 03:08:07 -070078{
Alexey Dobriyan64d0cd02008-11-25 17:29:21 -080079 return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
David S. Millerf034b5d2006-08-24 03:08:07 -070080}
81
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +020082static unsigned int xfrm_seq_hash(struct net *net, u32 seq)
83{
84 return __xfrm_seq_hash(seq, net->xfrm.state_hmask);
85}
86
Leon Romanovsky3c611d42022-12-02 20:41:32 +020087#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
David S. Millerf034b5d2006-08-24 03:08:07 -0700106static void xfrm_hash_transfer(struct hlist_head *list,
107 struct hlist_head *ndsttable,
108 struct hlist_head *nsrctable,
109 struct hlist_head *nspitable,
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200110 struct hlist_head *nseqtable,
David S. Millerf034b5d2006-08-24 03:08:07 -0700111 unsigned int nhashmask)
112{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800113 struct hlist_node *tmp;
David S. Millerf034b5d2006-08-24 03:08:07 -0700114 struct xfrm_state *x;
115
Sasha Levinb67bfe02013-02-27 17:06:00 -0800116 hlist_for_each_entry_safe(x, tmp, list, bydst) {
David S. Millerf034b5d2006-08-24 03:08:07 -0700117 unsigned int h;
118
David S. Millerc1969f22006-08-24 04:00:03 -0700119 h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
120 x->props.reqid, x->props.family,
121 nhashmask);
Leon Romanovsky3c611d42022-12-02 20:41:32 +0200122 XFRM_STATE_INSERT(bydst, &x->bydst, ndsttable + h, x->xso.type);
David S. Millerf034b5d2006-08-24 03:08:07 -0700123
Masahide NAKAMURA667bbcb2006-10-03 15:56:09 -0700124 h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
125 x->props.family,
David S. Millerf034b5d2006-08-24 03:08:07 -0700126 nhashmask);
Leon Romanovsky3c611d42022-12-02 20:41:32 +0200127 XFRM_STATE_INSERT(bysrc, &x->bysrc, nsrctable + h, x->xso.type);
David S. Millerf034b5d2006-08-24 03:08:07 -0700128
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700129 if (x->id.spi) {
130 h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
131 x->id.proto, x->props.family,
132 nhashmask);
Leon Romanovsky3c611d42022-12-02 20:41:32 +0200133 XFRM_STATE_INSERT(byspi, &x->byspi, nspitable + h,
134 x->xso.type);
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -0700135 }
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200136
137 if (x->km.seq) {
138 h = __xfrm_seq_hash(x->km.seq, nhashmask);
Leon Romanovsky3c611d42022-12-02 20:41:32 +0200139 XFRM_STATE_INSERT(byseq, &x->byseq, nseqtable + h,
140 x->xso.type);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200141 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700142 }
143}
144
Alexey Dobriyan63082732008-11-25 17:19:07 -0800145static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
David S. Millerf034b5d2006-08-24 03:08:07 -0700146{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800147 return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
David S. Millerf034b5d2006-08-24 03:08:07 -0700148}
149
Alexey Dobriyan63082732008-11-25 17:19:07 -0800150static void xfrm_hash_resize(struct work_struct *work)
David S. Millerf034b5d2006-08-24 03:08:07 -0700151{
Alexey Dobriyan63082732008-11-25 17:19:07 -0800152 struct net *net = container_of(work, struct net, xfrm.state_hash_work);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200153 struct hlist_head *ndst, *nsrc, *nspi, *nseq, *odst, *osrc, *ospi, *oseq;
David S. Millerf034b5d2006-08-24 03:08:07 -0700154 unsigned long nsize, osize;
155 unsigned int nhashmask, ohashmask;
156 int i;
157
Alexey Dobriyan63082732008-11-25 17:19:07 -0800158 nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
David S. Miller44e36b42006-08-24 04:50:50 -0700159 ndst = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700160 if (!ndst)
Ying Xue02447902014-08-29 17:09:07 +0800161 return;
David S. Miller44e36b42006-08-24 04:50:50 -0700162 nsrc = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700163 if (!nsrc) {
David S. Miller44e36b42006-08-24 04:50:50 -0700164 xfrm_hash_free(ndst, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800165 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700166 }
David S. Miller44e36b42006-08-24 04:50:50 -0700167 nspi = xfrm_hash_alloc(nsize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700168 if (!nspi) {
David S. Miller44e36b42006-08-24 04:50:50 -0700169 xfrm_hash_free(ndst, nsize);
170 xfrm_hash_free(nsrc, nsize);
Ying Xue02447902014-08-29 17:09:07 +0800171 return;
David S. Millerf034b5d2006-08-24 03:08:07 -0700172 }
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200173 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 }
David S. Millerf034b5d2006-08-24 03:08:07 -0700180
Fan Du283bc9f2013-11-07 17:47:50 +0800181 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Ahmed S. Darwishe88add12021-03-16 11:56:29 +0100182 write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
David S. Millerf034b5d2006-08-24 03:08:07 -0700183
184 nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
Florian Westphalc8406992016-08-09 12:16:08 +0200185 odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800186 for (i = net->xfrm.state_hmask; i >= 0; i--)
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200187 xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nseq, nhashmask);
David S. Millerf034b5d2006-08-24 03:08:07 -0700188
Florian Westphalc8406992016-08-09 12:16:08 +0200189 osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
190 ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200191 oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800192 ohashmask = net->xfrm.state_hmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700193
Florian Westphalc8406992016-08-09 12:16:08 +0200194 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);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200197 rcu_assign_pointer(net->xfrm.state_byseq, nseq);
Alexey Dobriyan63082732008-11-25 17:19:07 -0800198 net->xfrm.state_hmask = nhashmask;
David S. Millerf034b5d2006-08-24 03:08:07 -0700199
Ahmed S. Darwishe88add12021-03-16 11:56:29 +0100200 write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800201 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerf034b5d2006-08-24 03:08:07 -0700202
203 osize = (ohashmask + 1) * sizeof(struct hlist_head);
Florian Westphaldf7274e2016-08-09 12:16:06 +0200204
205 synchronize_rcu();
206
David S. Miller44e36b42006-08-24 04:50:50 -0700207 xfrm_hash_free(odst, osize);
208 xfrm_hash_free(osrc, osize);
209 xfrm_hash_free(ospi, osize);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200210 xfrm_hash_free(oseq, osize);
David S. Millerf034b5d2006-08-24 03:08:07 -0700211}
212
Cong Wang44abdc32013-01-16 16:05:05 +0800213static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
214static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static DEFINE_SPINLOCK(xfrm_state_gc_lock);
217
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800218int __xfrm_state_delete(struct xfrm_state *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -0800220int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
Florian Westphalbb9cd072019-04-17 11:45:13 +0200221static bool km_is_alive(const struct km_event *c);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000222void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800224int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700225{
Cong Wang7a9885b2013-01-17 16:34:11 +0800226 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700227 int err = 0;
228
Florian Westphal4f518e82019-05-03 17:46:19 +0200229 if (!afinfo)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700230 return -EAFNOSUPPORT;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700231
Florian Westphal4f518e82019-05-03 17:46:19 +0200232#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
Florian Westphalaf5d27c42017-01-09 14:20:47 +0100265 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700266 return err;
267}
268EXPORT_SYMBOL(xfrm_register_type);
269
Florian Westphal4f518e82019-05-03 17:46:19 +0200270void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700271{
Cong Wang7a9885b2013-01-17 16:34:11 +0800272 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700273
274 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200275 return;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700276
Florian Westphal4f518e82019-05-03 17:46:19 +0200277#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
Florian Westphalaf5d27c42017-01-09 14:20:47 +0100309 rcu_read_unlock();
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700310}
311EXPORT_SYMBOL(xfrm_unregister_type);
312
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800313static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700314{
Florian Westphal4f518e82019-05-03 17:46:19 +0200315 const struct xfrm_type *type = NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700316 struct xfrm_state_afinfo *afinfo;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700317 int modload_attempted = 0;
318
319retry:
320 afinfo = xfrm_state_get_afinfo(family);
321 if (unlikely(afinfo == NULL))
322 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700323
Florian Westphal4f518e82019-05-03 17:46:19 +0200324 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
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700350 if (unlikely(type && !try_module_get(type->owner)))
351 type = NULL;
Florian Westphal75cda622017-01-09 14:20:49 +0100352
353 rcu_read_unlock();
354
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700355 if (!type && !modload_attempted) {
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700356 request_module("xfrm-type-%d-%d", family, proto);
357 modload_attempted = 1;
358 goto retry;
359 }
360
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700361 return type;
362}
363
Eric Dumazet533cb5b2008-01-30 19:11:50 -0800364static void xfrm_put_type(const struct xfrm_type *type)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700365{
366 module_put(type->owner);
367}
368
Steffen Klassert9d389d72017-04-14 10:05:44 +0200369int 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);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200373 int err = 0;
374
375 if (unlikely(afinfo == NULL))
376 return -EAFNOSUPPORT;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200377
Florian Westphal4f518e82019-05-03 17:46:19 +0200378 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
Steffen Klassert9d389d72017-04-14 10:05:44 +0200389 rcu_read_unlock();
390 return err;
391}
392EXPORT_SYMBOL(xfrm_register_type_offload);
393
Florian Westphal4f518e82019-05-03 17:46:19 +0200394void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
395 unsigned short family)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200396{
397 struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200398
399 if (unlikely(afinfo == NULL))
Florian Westphal4f518e82019-05-03 17:46:19 +0200400 return;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200401
Florian Westphal4f518e82019-05-03 17:46:19 +0200402 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 }
Steffen Klassert9d389d72017-04-14 10:05:44 +0200411 rcu_read_unlock();
Steffen Klassert9d389d72017-04-14 10:05:44 +0200412}
413EXPORT_SYMBOL(xfrm_unregister_type_offload);
414
Ilan Tayariffdb5212017-08-01 12:49:08 +0300415static const struct xfrm_type_offload *
416xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
Steffen Klassert9d389d72017-04-14 10:05:44 +0200417{
Florian Westphal4f518e82019-05-03 17:46:19 +0200418 const struct xfrm_type_offload *type = NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200419 struct xfrm_state_afinfo *afinfo;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200420
Ilan Tayariffdb5212017-08-01 12:49:08 +0300421retry:
Steffen Klassert9d389d72017-04-14 10:05:44 +0200422 afinfo = xfrm_state_get_afinfo(family);
423 if (unlikely(afinfo == NULL))
424 return NULL;
Steffen Klassert9d389d72017-04-14 10:05:44 +0200425
Florian Westphal4f518e82019-05-03 17:46:19 +0200426 switch (proto) {
427 case IPPROTO_ESP:
428 type = afinfo->type_offload_esp;
429 break;
430 default:
431 break;
432 }
433
Steffen Klassert9d389d72017-04-14 10:05:44 +0200434 if ((type && !try_module_get(type->owner)))
435 type = NULL;
436
Sabrina Dubroca2f10a612017-12-31 16:18:56 +0100437 rcu_read_unlock();
438
Ilan Tayariffdb5212017-08-01 12:49:08 +0300439 if (!type && try_load) {
440 request_module("xfrm-offload-%d-%d", family, proto);
Gustavo A. R. Silva545d8ae2018-01-22 16:34:09 -0600441 try_load = false;
Ilan Tayariffdb5212017-08-01 12:49:08 +0300442 goto retry;
443 }
444
Steffen Klassert9d389d72017-04-14 10:05:44 +0200445 return type;
446}
447
448static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
449{
450 module_put(type->owner);
451}
452
Florian Westphal4c145dc2019-03-29 21:16:31 +0100453static 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
470static 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
491static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700492{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100493 const struct xfrm_mode *mode;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700494
495 if (unlikely(encap >= XFRM_MODE_MAX))
496 return NULL;
497
Florian Westphal4c145dc2019-03-29 21:16:31 +0100498 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;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700511 }
512
Florian Westphal4c145dc2019-03-29 21:16:31 +0100513 return NULL;
Herbert Xuaa5d62c2007-10-17 21:31:12 -0700514}
515
Mathias Krause4a135e52018-11-21 21:09:23 +0100516void xfrm_state_free(struct xfrm_state *x)
517{
518 kmem_cache_free(xfrm_state_cache, x);
519}
520EXPORT_SYMBOL(xfrm_state_free);
521
Cong Wangf75a2802019-01-31 13:05:49 -0800522static void ___xfrm_state_destroy(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100524 hrtimer_cancel(&x->mtimer);
David S. Millera47f0ce2006-08-24 03:54:22 -0700525 del_timer_sync(&x->rtimer);
Ilan Tayarib5884792016-09-18 07:42:53 +0000526 kfree(x->aead);
Jesper Juhla51482b2005-11-08 09:41:34 -0800527 kfree(x->aalg);
528 kfree(x->ealg);
529 kfree(x->calg);
530 kfree(x->encap);
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700531 kfree(x->coaddr);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000532 kfree(x->replay_esn);
533 kfree(x->preplay_esn);
Steffen Klassert9d389d72017-04-14 10:05:44 +0200534 if (x->type_offload)
535 xfrm_put_type_offload(x->type_offload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (x->type) {
537 x->type->destructor(x);
538 xfrm_put_type(x->type);
539 }
Steffen Klassert86c67392019-11-06 08:13:49 +0100540 if (x->xfrag.page)
541 put_page(x->xfrag.page);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200542 xfrm_dev_state_free(x);
Trent Jaegerdf718372005-12-13 23:12:27 -0800543 security_xfrm_state_free(x);
Mathias Krause4a135e52018-11-21 21:09:23 +0100544 xfrm_state_free(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Alexey Dobriyanc7837142008-11-25 17:20:36 -0800547static void xfrm_state_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Herbert Xu12a169e2008-10-01 07:03:24 -0700549 struct xfrm_state *x;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800550 struct hlist_node *tmp;
Herbert Xu12a169e2008-10-01 07:03:24 -0700551 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock_bh(&xfrm_state_gc_lock);
Florian Westphal35db57bb2016-08-23 16:00:12 +0200554 hlist_move_list(&xfrm_state_gc_list, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 spin_unlock_bh(&xfrm_state_gc_lock);
556
Florian Westphaldf7274e2016-08-09 12:16:06 +0200557 synchronize_rcu();
558
Sasha Levinb67bfe02013-02-27 17:06:00 -0800559 hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
Cong Wangf75a2802019-01-31 13:05:49 -0800560 ___xfrm_state_destroy(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800563static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Thomas Gleixner671422b2019-03-01 23:48:20 +0100565 struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
566 enum hrtimer_restart ret = HRTIMER_NORESTART;
Arnd Bergmann386c5682018-07-11 12:19:13 +0200567 time64_t now = ktime_get_real_seconds();
568 time64_t next = TIME64_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int warn = 0;
Joy Latten161a09e2006-11-27 13:11:54 -0600570 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 spin_lock(&x->lock);
Leon Romanovskyf3da86d2022-12-02 20:41:33 +0200573 xfrm_dev_state_update_curlft(x);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 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) {
580 long tmo = x->lft.hard_add_expires_seconds +
581 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000582 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.
Alexander Alemayhu1365e547c2017-01-03 17:13:20 +0100586 * workaround: fix x->curflt.add_time by below:
Fan Due3c0d042012-07-30 21:43:54 +0000587 */
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (tmo < next)
594 next = tmo;
595 }
596 if (x->lft.hard_use_expires_seconds) {
597 long tmo = x->lft.hard_use_expires_seconds +
598 (x->curlft.use_time ? : now) - now;
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) {
607 long tmo = x->lft.soft_add_expires_seconds +
608 x->curlft.add_time - now;
Fan Due3c0d042012-07-30 21:43:54 +0000609 if (tmo <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 warn = 1;
Fan Due3c0d042012-07-30 21:43:54 +0000611 x->xflags &= ~XFRM_SOFT_EXPIRE;
612 } else if (tmo < next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 next = tmo;
Fan Due3c0d042012-07-30 21:43:54 +0000614 x->xflags |= XFRM_SOFT_EXPIRE;
615 x->saved_tmo = tmo;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 if (x->lft.soft_use_expires_seconds) {
619 long tmo = x->lft.soft_use_expires_seconds +
620 (x->curlft.use_time ? : now) - now;
621 if (tmo <= 0)
622 warn = 1;
623 else if (tmo < next)
624 next = tmo;
625 }
626
Herbert Xu4666faa2005-06-18 22:43:22 -0700627 x->km.dying = warn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (warn)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800629 km_state_expired(x, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630resched:
Arnd Bergmann386c5682018-07-11 12:19:13 +0200631 if (next != TIME64_MAX) {
Thomas Gleixner671422b2019-03-01 23:48:20 +0100632 hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
633 ret = HRTIMER_RESTART;
Yury Polyanskiy9e0d57f2009-11-08 20:58:41 -0800634 }
David S. Millera47f0ce2006-08-24 03:54:22 -0700635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto out;
637
638expired:
Steffen Klassert5b8ef342013-08-27 13:43:30 +0200639 if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 x->km.state = XFRM_STATE_EXPIRED;
Joy Latten161a09e2006-11-27 13:11:54 -0600641
642 err = __xfrm_state_delete(x);
Nicolas Dichtel0806ae42013-08-23 15:46:08 +0200643 if (!err)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800644 km_state_expired(x, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Tetsuo Handa2e710292014-04-22 21:48:30 +0900646 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648out:
649 spin_unlock(&x->lock);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Kees Cooke99e88a2017-10-16 14:43:17 -0700653static void xfrm_replay_timer_handler(struct timer_list *t);
David S. Miller0ac84752006-03-20 19:18:23 -0800654
Alexey Dobriyan673c09be2008-11-25 17:15:16 -0800655struct xfrm_state *xfrm_state_alloc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct xfrm_state *x;
658
Huang Zijianga4c278d12020-02-12 17:54:36 +0800659 x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (x) {
Alexey Dobriyan673c09be2008-11-25 17:15:16 -0800662 write_pnet(&x->xs_net, net);
Reshetova, Elena88755e9c2017-07-04 15:53:21 +0300663 refcount_set(&x->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 atomic_set(&x->tunnel_users, 0);
Herbert Xu12a169e2008-10-01 07:03:24 -0700665 INIT_LIST_HEAD(&x->km.all);
David S. Miller8f126e32006-08-24 02:45:07 -0700666 INIT_HLIST_NODE(&x->bydst);
667 INIT_HLIST_NODE(&x->bysrc);
668 INIT_HLIST_NODE(&x->byspi);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200669 INIT_HLIST_NODE(&x->byseq);
Thomas Gleixner671422b2019-03-01 23:48:20 +0100670 hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
671 x->mtimer.function = xfrm_timer_handler;
Kees Cooke99e88a2017-10-16 14:43:17 -0700672 timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
Arnd Bergmann386c5682018-07-11 12:19:13 +0200673 x->curlft.add_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 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;
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -0800678 x->replay_maxage = 0;
679 x->replay_maxdiff = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_lock_init(&x->lock);
681 }
682 return x;
683}
684EXPORT_SYMBOL(xfrm_state_alloc);
685
Cong Wangf75a2802019-01-31 13:05:49 -0800686void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700688 WARN_ON(x->km.state != XFRM_STATE_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Cong Wangf75a2802019-01-31 13:05:49 -0800690 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700EXPORT_SYMBOL(__xfrm_state_destroy);
701
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800702int __xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800704 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700705 int err = -ESRCH;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (x->km.state != XFRM_STATE_DEAD) {
708 x->km.state = XFRM_STATE_DEAD;
Fan Du283bc9f2013-11-07 17:47:50 +0800709 spin_lock(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700710 list_del(&x->km.all);
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200711 hlist_del_rcu(&x->bydst);
712 hlist_del_rcu(&x->bysrc);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +0200713 if (x->km.seq)
714 hlist_del_rcu(&x->byseq);
David S. Millera47f0ce2006-08-24 03:54:22 -0700715 if (x->id.spi)
Florian Westphalae3fb6d2016-08-09 12:16:04 +0200716 hlist_del_rcu(&x->byspi);
Alexey Dobriyan98806f72008-11-25 17:29:47 -0800717 net->xfrm.state_num--;
Fan Du283bc9f2013-11-07 17:47:50 +0800718 spin_unlock(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Sabrina Dubrocae27cca92019-11-25 14:49:02 +0100720 if (x->encap_sk)
721 sock_put(rcu_dereference_raw(x->encap_sk));
722
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200723 xfrm_dev_state_delete(x);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* 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 */
Patrick McHardy5dba4792007-11-27 11:10:07 +0800729 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700730 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700732
733 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -0800735EXPORT_SYMBOL(__xfrm_state_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700737int xfrm_state_delete(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700739 int err;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 spin_lock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700742 err = __xfrm_state_delete(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 spin_unlock_bh(&x->lock);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700744
745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747EXPORT_SYMBOL(xfrm_state_delete);
748
Joy Latten4aa2e622007-06-04 19:05:57 -0400749#ifdef CONFIG_SECURITY_NETWORK_XFRM
750static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900751xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Joy Latten4aa2e622007-06-04 19:05:57 -0400753 int i, err = 0;
754
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800755 for (i = 0; i <= net->xfrm.state_hmask; i++) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400756 struct xfrm_state *x;
757
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400759 if (xfrm_id_proto_match(x->id.proto, proto) &&
760 (err = security_xfrm_state_delete(x)) != 0) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900761 xfrm_audit_state_delete(x, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400762 return err;
763 }
764 }
765 }
766
767 return err;
768}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200769
770static inline int
771xfrm_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;
Leon Romanovsky87e0a942022-05-05 13:06:40 +0300777 struct xfrm_dev_offload *xso;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200778
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}
Joy Latten4aa2e622007-06-04 19:05:57 -0400792#else
793static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900794xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400795{
796 return 0;
797}
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200798
799static inline int
800xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
801{
802 return 0;
803}
Joy Latten4aa2e622007-06-04 19:05:57 -0400804#endif
805
Cong Wangf75a2802019-01-31 13:05:49 -0800806int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
Joy Latten4aa2e622007-06-04 19:05:57 -0400807{
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000808 int i, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Fan Du283bc9f2013-11-07 17:47:50 +0800810 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Tetsuo Handa2e710292014-04-22 21:48:30 +0900811 err = xfrm_state_flush_secctx_check(net, proto, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400812 if (err)
813 goto out;
814
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000815 err = -ESRCH;
Alexey Dobriyan0e602452008-11-25 17:30:18 -0800816 for (i = 0; i <= net->xfrm.state_hmask; i++) {
David S. Miller8f126e32006-08-24 02:45:07 -0700817 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800819 hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!xfrm_state_kern(x) &&
Masahide NAKAMURA57947082006-09-22 15:06:24 -0700821 xfrm_id_proto_match(x->id.proto, proto)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 xfrm_state_hold(x);
Fan Du283bc9f2013-11-07 17:47:50 +0800823 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Joy Latten161a09e2006-11-27 13:11:54 -0600825 err = xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -0700826 xfrm_audit_state_delete(x, err ? 0 : 1,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900827 task_valid);
Cong Wangf75a2802019-01-31 13:05:49 -0800828 if (sync)
829 xfrm_state_put_sync(x);
830 else
831 xfrm_state_put(x);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000832 if (!err)
833 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Fan Du283bc9f2013-11-07 17:47:50 +0800835 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto restart;
837 }
838 }
839 }
Artem Savkovdd269db2017-09-27 14:25:37 +0200840out:
841 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Florian Westphale4db5b62018-06-25 17:26:02 +0200842 if (cnt)
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +0000843 err = 0;
Florian Westphale4db5b62018-06-25 17:26:02 +0200844
Joy Latten4aa2e622007-06-04 19:05:57 -0400845 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847EXPORT_SYMBOL(xfrm_state_flush);
848
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200849int 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;
Leon Romanovsky87e0a942022-05-05 13:06:40 +0300861 struct xfrm_dev_offload *xso;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200862restart:
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
885out:
886 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
887 return err;
888}
889EXPORT_SYMBOL(xfrm_dev_state_flush);
890
Alexey Dobriyane0710412010-01-23 13:37:10 +0000891void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700892{
Fan Du283bc9f2013-11-07 17:47:50 +0800893 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000894 si->sadcnt = net->xfrm.state_num;
Benjamin Poirierca92e172018-11-05 17:00:53 +0900895 si->sadhcnt = net->xfrm.state_hmask + 1;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700896 si->sadhmcnt = xfrm_state_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800897 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700898}
899EXPORT_SYMBOL(xfrm_sad_getinfo);
900
Florian Westphal711059b2017-01-09 14:20:48 +0100901static void
Florian Westphalbac95932019-05-03 17:46:14 +0200902__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
919static 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
938static void
David S. Miller1a898592011-02-22 18:22:34 -0800939xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
David S. Miller04686012011-02-24 01:50:12 -0500940 const struct xfrm_tmpl *tmpl,
David S. Miller33765d02011-02-24 01:55:45 -0500941 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Thomas Egerer8444cf72010-09-20 11:11:38 -0700942 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Florian Westphalbac95932019-05-03 17:46:14 +0200944 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
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200953 x->id = tmpl->id;
Florian Westphalbac95932019-05-03 17:46:14 +0200954
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200955 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 }
Florian Westphal3819a352017-01-13 14:55:14 +0100971
Florian Westphal5c1b9ab2019-05-03 17:46:15 +0200972 x->props.mode = tmpl->mode;
973 x->props.reqid = tmpl->reqid;
974 x->props.family = tmpl->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Leon Romanovskyf8a70af2022-12-02 20:41:30 +0200977static 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
David S. Miller9aa60082011-02-24 01:51:36 -05001020static 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)
David S. Milleredcd5822006-08-24 00:42:45 -07001024{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001025 unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -07001026 struct xfrm_state *x;
1027
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001028 hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
David S. Milleredcd5822006-08-24 00:42:45 -07001029 if (x->props.family != family ||
1030 x->id.spi != spi ||
Wei Yongjun18025712009-06-28 18:42:53 +00001031 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001032 !xfrm_addr_equal(&x->id.daddr, daddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -07001033 continue;
1034
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001035 if ((mark & x->mark.m) != x->mark.v)
1036 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +02001037 if (!xfrm_state_hold_rcu(x))
1038 continue;
David S. Milleredcd5822006-08-24 00:42:45 -07001039 return x;
1040 }
1041
1042 return NULL;
1043}
1044
David S. Miller9aa60082011-02-24 01:51:36 -05001045static 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)
David S. Milleredcd5822006-08-24 00:42:45 -07001049{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001050 unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
David S. Milleredcd5822006-08-24 00:42:45 -07001051 struct xfrm_state *x;
1052
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001053 hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
David S. Milleredcd5822006-08-24 00:42:45 -07001054 if (x->props.family != family ||
Wei Yongjun18025712009-06-28 18:42:53 +00001055 x->id.proto != proto ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001056 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1057 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Milleredcd5822006-08-24 00:42:45 -07001058 continue;
1059
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001060 if ((mark & x->mark.m) != x->mark.v)
1061 continue;
Florian Westphal02efdff2016-08-09 12:16:05 +02001062 if (!xfrm_state_hold_rcu(x))
1063 continue;
David S. Milleredcd5822006-08-24 00:42:45 -07001064 return x;
1065 }
1066
1067 return NULL;
1068}
1069
1070static inline struct xfrm_state *
1071__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
1072{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001073 struct net *net = xs_net(x);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001074 u32 mark = x->mark.v & x->mark.m;
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08001075
David S. Milleredcd5822006-08-24 00:42:45 -07001076 if (use_spi)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001077 return __xfrm_state_lookup(net, mark, &x->id.daddr,
1078 x->id.spi, x->id.proto, family);
David S. Milleredcd5822006-08-24 00:42:45 -07001079 else
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001080 return __xfrm_state_lookup_byaddr(net, mark,
1081 &x->id.daddr,
David S. Milleredcd5822006-08-24 00:42:45 -07001082 &x->props.saddr,
1083 x->id.proto, family);
1084}
1085
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001086static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
Patrick McHardy2fab22f2006-10-24 15:34:00 -07001087{
1088 if (have_hash_collision &&
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001089 (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);
Patrick McHardy2fab22f2006-10-24 15:34:00 -07001092}
1093
David S. Miller08ec9af2009-03-13 14:22:40 -07001094static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
David S. Miller4a08ab02011-02-22 18:21:31 -08001095 const struct flowi *fl, unsigned short family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001096 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 &&
Herbert Xue94ee172020-09-25 14:42:56 +10001112 (x->sel.family != family ||
1113 !xfrm_selector_match(&x->sel, fl, family))) ||
Paul Moore3df98d72020-09-27 22:38:26 -04001114 !security_xfrm_state_pol_flow_match(x, pol,
1115 &fl->u.__fl_common))
David S. Miller08ec9af2009-03-13 14:22:40 -07001116 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) {
Herbert Xue94ee172020-09-25 14:42:56 +10001127 if ((!x->sel.family ||
1128 (x->sel.family == family &&
1129 xfrm_selector_match(&x->sel, fl, family))) &&
Paul Moore3df98d72020-09-27 22:38:26 -04001130 security_xfrm_state_pol_flow_match(x, pol,
1131 &fl->u.__fl_common))
David S. Miller08ec9af2009-03-13 14:22:40 -07001132 *error = -ESRCH;
1133 }
1134}
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136struct xfrm_state *
David S. Miller33765d02011-02-24 01:55:45 -05001137xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
David S. Millerb520e9f2011-02-22 18:24:19 -08001138 const struct flowi *fl, struct xfrm_tmpl *tmpl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct xfrm_policy *pol, int *err,
Benedict Wongbc56b332018-07-19 10:50:44 -07001140 unsigned short family, u32 if_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
David S. Miller08ec9af2009-03-13 14:22:40 -07001142 static xfrm_address_t saddr_wildcard = { };
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001143 struct net *net = xp_net(pol);
Nicolas Dichtel6a783c92009-04-27 02:58:59 -07001144 unsigned int h, h_wildcard;
David S. Miller37b08e32008-09-02 20:14:15 -07001145 struct xfrm_state *x, *x0, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int acquire_in_progress = 0;
1147 int error = 0;
1148 struct xfrm_state *best = NULL;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001149 u32 mark = pol->mark.v & pol->mark.m;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001150 unsigned short encap_family = tmpl->encap_family;
Florian Westphalb65e3d72016-08-09 12:16:07 +02001151 unsigned int sequence;
Horia Geanta0f245582014-02-12 16:20:06 +02001152 struct km_event c;
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001153
David S. Miller37b08e32008-09-02 20:14:15 -07001154 to_put = NULL;
1155
Ahmed S. Darwishe88add12021-03-16 11:56:29 +01001156 sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001157
Florian Westphald737a582016-08-09 12:16:09 +02001158 rcu_read_lock();
Thomas Egerer8444cf72010-09-20 11:11:38 -07001159 h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001160 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001161#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
Thomas Egerer8444cf72010-09-20 11:11:38 -07001178 if (x->props.family == encap_family &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001180 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001181 x->if_id == if_id &&
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -07001182 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Thomas Egerer8444cf72010-09-20 11:11:38 -07001183 xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 tmpl->mode == x->props.mode &&
1185 tmpl->id.proto == x->id.proto &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001186 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001187 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001188 &best, &acquire_in_progress, &error);
1189 }
Fan Du6f115632013-09-23 17:18:25 +08001190 if (best || acquire_in_progress)
David S. Miller08ec9af2009-03-13 14:22:40 -07001191 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Thomas Egerer8444cf72010-09-20 11:11:38 -07001193 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
Florian Westphalae3fb6d2016-08-09 12:16:04 +02001194 hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001195#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
Thomas Egerer8444cf72010-09-20 11:11:38 -07001212 if (x->props.family == encap_family &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001213 x->props.reqid == tmpl->reqid &&
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001214 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001215 x->if_id == if_id &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001216 !(x->props.flags & XFRM_STATE_WILDRECV) &&
Fan Duf59bbdf2013-09-27 16:32:50 +08001217 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
David S. Miller08ec9af2009-03-13 14:22:40 -07001218 tmpl->mode == x->props.mode &&
1219 tmpl->id.proto == x->id.proto &&
1220 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
Herbert Xue94ee172020-09-25 14:42:56 +10001221 xfrm_state_look_at(pol, x, fl, family,
David S. Miller08ec9af2009-03-13 14:22:40 -07001222 &best, &acquire_in_progress, &error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
David S. Miller08ec9af2009-03-13 14:22:40 -07001225found:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 x = best;
1227 if (!x && !error && !acquire_in_progress) {
Patrick McHardy5c5d2812005-04-21 20:12:32 -07001228 if (tmpl->id.spi &&
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001229 (x0 = __xfrm_state_lookup_all(net, mark, daddr,
1230 tmpl->id.spi, tmpl->id.proto,
1231 encap_family,
1232 &pol->xdo)) != NULL) {
David S. Miller37b08e32008-09-02 20:14:15 -07001233 to_put = x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 error = -EEXIST;
1235 goto out;
1236 }
Horia Geanta0f245582014-02-12 16:20:06 +02001237
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
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001248 x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (x == NULL) {
1250 error = -ENOMEM;
1251 goto out;
1252 }
Thomas Egerer8444cf72010-09-20 11:11:38 -07001253 /* Initialize temporary state matching only
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * to current session. */
Thomas Egerer8444cf72010-09-20 11:11:38 -07001255 xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001256 memcpy(&x->mark, &pol->mark, sizeof(x->mark));
Steffen Klassert7e652642018-06-12 14:07:07 +02001257 x->if_id = if_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
David S. Miller1d28f422011-03-12 00:29:39 -05001259 error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001260 if (error) {
1261 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001262 to_put = x;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001263 x = NULL;
1264 goto out;
1265 }
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001266#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;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001270
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001271 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (km_query(x, tmpl, pol) == 0) {
Florian Westphald737a582016-08-09 12:16:09 +02001292 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 x->km.state = XFRM_STATE_ACQ;
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001294 list_add(&x->km.all, &net->xfrm.state_all);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001295 XFRM_STATE_INSERT(bydst, &x->bydst,
1296 net->xfrm.state_bydst + h,
1297 x->xso.type);
Thomas Egerer8444cf72010-09-20 11:11:38 -07001298 h = xfrm_src_hash(net, daddr, saddr, encap_family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001299 XFRM_STATE_INSERT(bysrc, &x->bysrc,
1300 net->xfrm.state_bysrc + h,
1301 x->xso.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (x->id.spi) {
Thomas Egerer8444cf72010-09-20 11:11:38 -07001303 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001304 XFRM_STATE_INSERT(byspi, &x->byspi,
1305 net->xfrm.state_byspi + h,
1306 x->xso.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02001308 if (x->km.seq) {
1309 h = xfrm_seq_hash(net, x->km.seq);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001310 XFRM_STATE_INSERT(byseq, &x->byseq,
1311 net->xfrm.state_byseq + h,
1312 x->xso.type);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02001313 }
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001314 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001315 hrtimer_start(&x->mtimer,
1316 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1317 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001318 net->xfrm.state_num++;
1319 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Florian Westphald737a582016-08-09 12:16:09 +02001320 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 } else {
Leon Romanovskyf8a70af2022-12-02 20:41:30 +02001322#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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 x->km.state = XFRM_STATE_DEAD;
David S. Miller37b08e32008-09-02 20:14:15 -07001335 to_put = x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 x = NULL;
1337 error = -ESRCH;
1338 }
1339 }
1340out:
Florian Westphal02efdff2016-08-09 12:16:05 +02001341 if (x) {
1342 if (!xfrm_state_hold_rcu(x)) {
1343 *err = -EAGAIN;
1344 x = NULL;
1345 }
1346 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 *err = acquire_in_progress ? -EAGAIN : error;
Florian Westphal02efdff2016-08-09 12:16:05 +02001348 }
Florian Westphald737a582016-08-09 12:16:09 +02001349 rcu_read_unlock();
David S. Miller37b08e32008-09-02 20:14:15 -07001350 if (to_put)
1351 xfrm_state_put(to_put);
Florian Westphalb65e3d72016-08-09 12:16:07 +02001352
Ahmed S. Darwishe88add12021-03-16 11:56:29 +01001353 if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
Florian Westphalb65e3d72016-08-09 12:16:07 +02001354 *err = -EAGAIN;
1355 if (x) {
1356 xfrm_state_put(x);
1357 x = NULL;
1358 }
1359 }
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return x;
1362}
1363
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001364struct xfrm_state *
Steffen Klassert7e652642018-06-12 14:07:07 +02001365xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001366 xfrm_address_t *daddr, xfrm_address_t *saddr,
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001367 unsigned short family, u8 mode, u8 proto, u32 reqid)
1368{
Pavel Emelyanov4bda4f22007-12-14 11:38:04 -08001369 unsigned int h;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001370 struct xfrm_state *rx = NULL, *x = NULL;
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001371
Fan Du4ae770b2014-01-03 11:18:29 +08001372 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001373 h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001374 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001375 if (x->props.family == family &&
1376 x->props.reqid == reqid &&
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001377 (mark & x->mark.m) == x->mark.v &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001378 x->if_id == if_id &&
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001379 !(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);
Fan Du4ae770b2014-01-03 11:18:29 +08001391 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salim628529b2007-07-02 22:41:14 -07001392
1393
1394 return rx;
1395}
1396EXPORT_SYMBOL(xfrm_stateonly_find);
1397
Fan Duc4549972014-01-03 11:18:32 +08001398struct 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
Fan Duc4549972014-01-03 11:18:32 +08001411 xfrm_state_hold(x);
Li RongQingbdddbf62015-04-29 08:42:44 +08001412 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Fan Duc4549972014-01-03 11:18:32 +08001413 return x;
1414 }
1415 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1416 return NULL;
1417}
1418EXPORT_SYMBOL(xfrm_state_lookup_byspi);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static void __xfrm_state_insert(struct xfrm_state *x)
1421{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001422 struct net *net = xs_net(x);
David S. Millera624c102006-08-24 03:24:33 -07001423 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001425 list_add(&x->km.all, &net->xfrm.state_all);
Timo Teras4c563f72008-02-28 21:31:08 -08001426
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001427 h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
David S. Millerc1969f22006-08-24 04:00:03 -07001428 x->props.reqid, x->props.family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001429 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1430 x->xso.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001432 h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001433 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1434 x->xso.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Masahide NAKAMURA7b4dc3602006-09-27 22:21:52 -07001436 if (x->id.spi) {
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001437 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001438 x->props.family);
1439
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001440 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
1441 x->xso.type);
Masahide NAKAMURA6c44e6b2006-08-23 17:53:57 -07001442 }
1443
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02001444 if (x->km.seq) {
1445 h = xfrm_seq_hash(net, x->km.seq);
1446
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001447 XFRM_STATE_INSERT(byseq, &x->byseq, net->xfrm.state_byseq + h,
1448 x->xso.type);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02001449 }
1450
Thomas Gleixner671422b2019-03-01 23:48:20 +01001451 hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
David S. Millera47f0ce2006-08-24 03:54:22 -07001452 if (x->replay_maxage)
1453 mod_timer(&x->rtimer, jiffies + x->replay_maxage);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08001454
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001455 net->xfrm.state_num++;
David S. Millerf034b5d2006-08-24 03:08:07 -07001456
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001457 xfrm_hash_grow_check(net, x->bydst.next != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Fan Du283bc9f2013-11-07 17:47:50 +08001460/* net->xfrm.xfrm_state_lock is held */
David S. Millerc7f5ea32006-08-24 03:29:04 -07001461static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
1462{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001463 struct net *net = xs_net(xnew);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001464 unsigned short family = xnew->props.family;
1465 u32 reqid = xnew->props.reqid;
1466 struct xfrm_state *x;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001467 unsigned int h;
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001468 u32 mark = xnew->mark.v & xnew->mark.m;
Steffen Klassert7e652642018-06-12 14:07:07 +02001469 u32 if_id = xnew->if_id;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001470
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001471 h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001472 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Millerc7f5ea32006-08-24 03:29:04 -07001473 if (x->props.family == family &&
1474 x->props.reqid == reqid &&
Steffen Klassert7e652642018-06-12 14:07:07 +02001475 x->if_id == if_id &&
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001476 (mark & x->mark.m) == x->mark.v &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001477 xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
1478 xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
Herbert Xu34996cb2010-03-31 01:19:49 +00001479 x->genid++;
David S. Millerc7f5ea32006-08-24 03:29:04 -07001480 }
1481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483void xfrm_state_insert(struct xfrm_state *x)
1484{
Fan Du283bc9f2013-11-07 17:47:50 +08001485 struct net *net = xs_net(x);
1486
1487 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Millerc7f5ea32006-08-24 03:29:04 -07001488 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 __xfrm_state_insert(x);
Fan Du283bc9f2013-11-07 17:47:50 +08001490 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492EXPORT_SYMBOL(xfrm_state_insert);
1493
Fan Du283bc9f2013-11-07 17:47:50 +08001494/* net->xfrm.xfrm_state_lock is held */
Mathias Krausee473fcb2013-06-26 23:56:58 +02001495static struct xfrm_state *__find_acq_core(struct net *net,
1496 const struct xfrm_mark *m,
David S. Millera70486f2011-02-27 23:17:24 -08001497 unsigned short family, u8 mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001498 u32 reqid, u32 if_id, u8 proto,
David S. Millera70486f2011-02-27 23:17:24 -08001499 const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001500 const xfrm_address_t *saddr,
1501 int create)
David S. Miller27708342006-08-24 00:13:10 -07001502{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001503 unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
David S. Miller27708342006-08-24 00:13:10 -07001504 struct xfrm_state *x;
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001505 u32 mark = m->v & m->m;
David S. Miller27708342006-08-24 00:13:10 -07001506
Sasha Levinb67bfe02013-02-27 17:06:00 -08001507 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
David S. Miller27708342006-08-24 00:13:10 -07001508 if (x->props.reqid != reqid ||
1509 x->props.mode != mode ||
1510 x->props.family != family ||
1511 x->km.state != XFRM_STATE_ACQ ||
Joy Latten75e252d2007-03-12 17:14:07 -07001512 x->id.spi != 0 ||
Wei Yongjun18025712009-06-28 18:42:53 +00001513 x->id.proto != proto ||
Jamal Hadi Salim3d6acfa72010-02-22 11:32:56 +00001514 (mark & x->mark.m) != x->mark.v ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001515 !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
1516 !xfrm_addr_equal(&x->props.saddr, saddr, family))
David S. Miller27708342006-08-24 00:13:10 -07001517 continue;
1518
David S. Miller27708342006-08-24 00:13:10 -07001519 xfrm_state_hold(x);
1520 return x;
1521 }
1522
1523 if (!create)
1524 return NULL;
1525
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001526 x = xfrm_state_alloc(net);
David S. Miller27708342006-08-24 00:13:10 -07001527 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:
Jiri Benc15e318b2015-03-29 16:59:24 +02001539 x->sel.daddr.in6 = daddr->in6;
1540 x->sel.saddr.in6 = saddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001541 x->sel.prefixlen_d = 128;
1542 x->sel.prefixlen_s = 128;
Jiri Benc15e318b2015-03-29 16:59:24 +02001543 x->props.saddr.in6 = saddr->in6;
1544 x->id.daddr.in6 = daddr->in6;
David S. Miller27708342006-08-24 00:13:10 -07001545 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001546 }
David S. Miller27708342006-08-24 00:13:10 -07001547
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;
Steffen Klassert7e652642018-06-12 14:07:07 +02001553 x->if_id = if_id;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001554 x->mark.v = m->v;
1555 x->mark.m = m->m;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08001556 x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
David S. Miller27708342006-08-24 00:13:10 -07001557 xfrm_state_hold(x);
Thomas Gleixner671422b2019-03-01 23:48:20 +01001558 hrtimer_start(&x->mtimer,
1559 ktime_set(net->xfrm.sysctl_acq_expires, 0),
1560 HRTIMER_MODE_REL_SOFT);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001561 list_add(&x->km.all, &net->xfrm.state_all);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001562 XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
1563 x->xso.type);
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001564 h = xfrm_src_hash(net, daddr, saddr, family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02001565 XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
1566 x->xso.type);
David S. Miller918049f2006-10-12 22:03:24 -07001567
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001568 net->xfrm.state_num++;
David S. Miller918049f2006-10-12 22:03:24 -07001569
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001570 xfrm_hash_grow_check(net, x->bydst.next != NULL);
David S. Miller27708342006-08-24 00:13:10 -07001571 }
1572
1573 return x;
1574}
1575
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001576static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578int xfrm_state_add(struct xfrm_state *x)
1579{
Alexey Dobriyan5447c5e2008-11-25 17:31:51 -08001580 struct net *net = xs_net(x);
David S. Miller37b08e32008-09-02 20:14:15 -07001581 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int family;
1583 int err;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001584 u32 mark = x->mark.v & x->mark.m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001585 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 family = x->props.family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
David S. Miller37b08e32008-09-02 20:14:15 -07001589 to_put = NULL;
1590
Fan Du283bc9f2013-11-07 17:47:50 +08001591 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
David S. Milleredcd5822006-08-24 00:42:45 -07001593 x1 = __xfrm_state_locate(x, use_spi, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (x1) {
David S. Miller37b08e32008-09-02 20:14:15 -07001595 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 x1 = NULL;
1597 err = -EEXIST;
1598 goto out;
1599 }
1600
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001601 if (use_spi && x->km.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001602 x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
Joy Latten75e252d2007-03-12 17:14:07 -07001603 if (x1 && ((x1->id.proto != x->id.proto) ||
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001604 !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
David S. Miller37b08e32008-09-02 20:14:15 -07001605 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 x1 = NULL;
1607 }
1608 }
1609
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001610 if (use_spi && !x1)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001611 x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
Steffen Klassert7e652642018-06-12 14:07:07 +02001612 x->props.reqid, x->if_id, x->id.proto,
David S. Miller27708342006-08-24 00:13:10 -07001613 &x->id.daddr, &x->props.saddr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
David S. Millerc7f5ea32006-08-24 03:29:04 -07001615 __xfrm_state_bump_genids(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 __xfrm_state_insert(x);
1617 err = 0;
1618
1619out:
Fan Du283bc9f2013-11-07 17:47:50 +08001620 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (x1) {
1623 xfrm_state_delete(x1);
1624 xfrm_state_put(x1);
1625 }
1626
David S. Miller37b08e32008-09-02 20:14:15 -07001627 if (to_put)
1628 xfrm_state_put(to_put);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return err;
1631}
1632EXPORT_SYMBOL(xfrm_state_add);
1633
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001634#ifdef CONFIG_XFRM_MIGRATE
Antony Antony7aa05d32020-09-04 08:50:11 +02001635static 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
Antony Antony4ab47d42017-06-06 12:12:13 +02001659static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
1660 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001661{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001662 struct net *net = xs_net(orig);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001663 struct xfrm_state *x = xfrm_state_alloc(net);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001664 if (!x)
Herbert Xu553f9112010-02-15 20:00:51 +00001665 goto out;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001666
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) {
Martin Willi4447bb32009-11-25 00:29:52 +00001677 x->aalg = xfrm_algo_auth_clone(orig->aalg);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001678 if (!x->aalg)
1679 goto error;
1680 }
1681 x->props.aalgo = orig->props.aalgo;
1682
Steffen Klassertee5c2312014-02-19 13:33:24 +01001683 if (orig->aead) {
1684 x->aead = xfrm_algo_aead_clone(orig->aead);
Antony Antony75bf50f2017-12-07 21:54:27 +01001685 x->geniv = orig->geniv;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001686 if (!x->aead)
1687 goto error;
1688 }
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001689 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
Antony Antony4ab47d42017-06-06 12:12:13 +02001703 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
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001711 if (!x->encap)
1712 goto error;
1713 }
1714
Antony Antony7aa05d32020-09-04 08:50:11 +02001715 if (orig->security)
1716 if (clone_security(x, orig->security))
1717 goto error;
1718
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001719 if (orig->coaddr) {
1720 x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
1721 GFP_KERNEL);
1722 if (!x->coaddr)
1723 goto error;
1724 }
1725
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001726 if (orig->replay_esn) {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001727 if (xfrm_replay_clone(x, orig))
Steffen Klassertaf2f4642011-03-28 19:46:39 +00001728 goto error;
1729 }
1730
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001731 memcpy(&x->mark, &orig->mark, sizeof(x->mark));
Antony Antony545e5c52020-09-04 08:49:38 +02001732 memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001733
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001734 x->props.flags = orig->props.flags;
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01001735 x->props.extra_flags = orig->props.extra_flags;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001736
Steffen Klassert7e652642018-06-12 14:07:07 +02001737 x->if_id = orig->if_id;
Steffen Klassertee5c2312014-02-19 13:33:24 +01001738 x->tfcpad = orig->tfcpad;
1739 x->replay_maxdiff = orig->replay_maxdiff;
1740 x->replay_maxage = orig->replay_maxage;
Antony Antony83666852020-09-04 08:50:29 +02001741 memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001742 x->km.state = orig->km.state;
1743 x->km.seq = orig->km.seq;
Antony Antonya486cd22017-05-19 12:47:00 +02001744 x->replay = orig->replay;
1745 x->preplay = orig->preplay;
Antony Antony4e484b32021-12-22 14:11:18 +01001746 x->mapping_maxage = orig->mapping_maxage;
Antony Antony6aa811a2022-07-27 17:41:22 +02001747 x->lastused = orig->lastused;
Antony Antony4e484b32021-12-22 14:11:18 +01001748 x->new_mapping = 0;
1749 x->new_mapping_sport = 0;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001750
1751 return x;
1752
1753 error:
Herbert Xu553f9112010-02-15 20:00:51 +00001754 xfrm_state_put(x);
1755out:
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001756 return NULL;
1757}
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001758
Yan Yanc1aca302022-01-18 16:00:13 -08001759struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
1760 u32 if_id)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001761{
1762 unsigned int h;
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001763 struct xfrm_state *x = NULL;
1764
1765 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001766
1767 if (m->reqid) {
Fan Du283bc9f2013-11-07 17:47:50 +08001768 h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001769 m->reqid, m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001770 hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001771 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;
Yan Yanc1aca302022-01-18 16:00:13 -08001776 if (if_id != 0 && x->if_id != if_id)
1777 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001778 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))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001782 continue;
1783 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001784 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001785 }
1786 } else {
Fan Du283bc9f2013-11-07 17:47:50 +08001787 h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001788 m->old_family);
Fan Du283bc9f2013-11-07 17:47:50 +08001789 hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001790 if (x->props.mode != m->mode ||
1791 x->id.proto != m->proto)
1792 continue;
Yan Yanc1aca302022-01-18 16:00:13 -08001793 if (if_id != 0 && x->if_id != if_id)
1794 continue;
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001795 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))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001799 continue;
1800 xfrm_state_hold(x);
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001801 break;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001802 }
1803 }
1804
Steffen Klassert8c0cba22014-02-19 13:33:24 +01001805 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1806
1807 return x;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001808}
1809EXPORT_SYMBOL(xfrm_migrate_state_find);
1810
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001811struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
Antony Antony4ab47d42017-06-06 12:12:13 +02001812 struct xfrm_migrate *m,
1813 struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001814{
1815 struct xfrm_state *xc;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001816
Antony Antony4ab47d42017-06-06 12:12:13 +02001817 xc = xfrm_state_clone(x, encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001818 if (!xc)
1819 return NULL;
1820
Yan Yane03c3bb2022-01-18 16:00:14 -08001821 xc->props.family = m->new_family;
1822
1823 if (xfrm_init_state(xc) < 0)
1824 goto error;
1825
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001826 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 */
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001830 if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001831 /* 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 {
Steffen Klassertcc9ab602014-02-19 13:33:24 +01001835 if (xfrm_state_add(xc) < 0)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001836 goto error;
1837 }
1838
1839 return xc;
1840error:
Thomas Egerer78347c82010-12-06 23:28:56 +00001841 xfrm_state_put(xc);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08001842 return NULL;
1843}
1844EXPORT_SYMBOL(xfrm_state_migrate);
1845#endif
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847int xfrm_state_update(struct xfrm_state *x)
1848{
David S. Miller37b08e32008-09-02 20:14:15 -07001849 struct xfrm_state *x1, *to_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int err;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001851 int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
Fan Du283bc9f2013-11-07 17:47:50 +08001852 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
David S. Miller37b08e32008-09-02 20:14:15 -07001854 to_put = NULL;
1855
Fan Du283bc9f2013-11-07 17:47:50 +08001856 spin_lock_bh(&net->xfrm.xfrm_state_lock);
David S. Milleredcd5822006-08-24 00:42:45 -07001857 x1 = __xfrm_state_locate(x, use_spi, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 err = -ESRCH;
1860 if (!x1)
1861 goto out;
1862
1863 if (xfrm_state_kern(x1)) {
David S. Miller37b08e32008-09-02 20:14:15 -07001864 to_put = x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 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
1875out:
Fan Du283bc9f2013-11-07 17:47:50 +08001876 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
David S. Miller37b08e32008-09-02 20:14:15 -07001878 if (to_put)
1879 xfrm_state_put(to_put);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 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)) {
Herbert Xu257a4b02017-12-26 17:34:44 +11001893 if (x->encap && x1->encap &&
1894 x->encap->encap_type == x1->encap->encap_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 memcpy(x1->encap, x->encap, sizeof(*x1->encap));
Herbert Xu257a4b02017-12-26 17:34:44 +11001896 else if (x->encap || x1->encap)
1897 goto fail;
1898
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -07001899 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
1905 x1->km.dying = 0;
1906
Thomas Gleixner671422b2019-03-01 23:48:20 +01001907 hrtimer_start(&x1->mtimer, ktime_set(1, 0),
1908 HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (x1->curlft.use_time)
1910 xfrm_state_check_expire(x1);
1911
Nathan Harold5baf4f92018-07-19 19:07:47 -07001912 if (x->props.smark.m || x->props.smark.v || x->if_id) {
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001913 spin_lock_bh(&net->xfrm.xfrm_state_lock);
1914
Nathan Harold5baf4f92018-07-19 19:07:47 -07001915 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;
Nathan Harold6d8e85f2018-06-29 15:07:10 -07001920
1921 __xfrm_state_bump_genids(x1);
1922 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
1923 }
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 err = 0;
Tushar Gohad8fcbc632011-07-07 15:38:52 +00001926 x->km.state = XFRM_STATE_DEAD;
1927 __xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Herbert Xu257a4b02017-12-26 17:34:44 +11001929
1930fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 spin_unlock_bh(&x1->lock);
1932
1933 xfrm_state_put(x1);
1934
1935 return err;
1936}
1937EXPORT_SYMBOL(xfrm_state_update);
1938
1939int xfrm_state_check_expire(struct xfrm_state *x)
1940{
Leon Romanovskyf3da86d2022-12-02 20:41:33 +02001941 xfrm_dev_state_update_curlft(x);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (!x->curlft.use_time)
Arnd Bergmann386c5682018-07-11 12:19:13 +02001944 x->curlft.use_time = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (x->curlft.bytes >= x->lft.hard_byte_limit ||
1947 x->curlft.packets >= x->lft.hard_packet_limit) {
Herbert Xu4666faa2005-06-18 22:43:22 -07001948 x->km.state = XFRM_STATE_EXPIRED;
Thomas Gleixner671422b2019-03-01 23:48:20 +01001949 hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return -EINVAL;
1951 }
1952
1953 if (!x->km.dying &&
1954 (x->curlft.bytes >= x->lft.soft_byte_limit ||
Herbert Xu4666faa2005-06-18 22:43:22 -07001955 x->curlft.packets >= x->lft.soft_packet_limit)) {
1956 x->km.dying = 1;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08001957 km_state_expired(x, 0, 0);
Herbert Xu4666faa2005-06-18 22:43:22 -07001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return 0;
1960}
1961EXPORT_SYMBOL(xfrm_state_check_expire);
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963struct xfrm_state *
David S. Millera70486f2011-02-27 23:17:24 -08001964xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001965 u8 proto, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Florian Westphalc2f672f2016-09-20 15:45:26 +02001969 rcu_read_lock();
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001970 x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
Florian Westphalc2f672f2016-09-20 15:45:26 +02001971 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return x;
1973}
1974EXPORT_SYMBOL(xfrm_state_lookup);
1975
1976struct xfrm_state *
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001977xfrm_state_lookup_byaddr(struct net *net, u32 mark,
David S. Millera70486f2011-02-27 23:17:24 -08001978 const xfrm_address_t *daddr, const xfrm_address_t *saddr,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001979 u8 proto, unsigned short family)
1980{
1981 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001982
Fan Du283bc9f2013-11-07 17:47:50 +08001983 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001984 x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
Fan Du283bc9f2013-11-07 17:47:50 +08001985 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001986 return x;
1987}
1988EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
1989
1990struct xfrm_state *
Mathias Krausee473fcb2013-06-26 23:56:58 +02001991xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
Steffen Klassert7e652642018-06-12 14:07:07 +02001992 u32 if_id, u8 proto, const xfrm_address_t *daddr,
Mathias Krausee473fcb2013-06-26 23:56:58 +02001993 const xfrm_address_t *saddr, int create, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Fan Du283bc9f2013-11-07 17:47:50 +08001997 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Steffen Klassert7e652642018-06-12 14:07:07 +02001998 x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
Fan Du283bc9f2013-11-07 17:47:50 +08001999 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
David S. Miller27708342006-08-24 00:13:10 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return x;
2002}
2003EXPORT_SYMBOL(xfrm_find_acq);
2004
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002005#ifdef CONFIG_XFRM_SUB_POLICY
Florian Westphal3aaf3912019-05-03 17:46:17 +02002006#if IS_ENABLED(CONFIG_IPV6)
2007/* distribution counting sort function for xfrm_state and xfrm_tmpl */
2008static 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 */
2040static 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 */
2069static 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
2088static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
2089static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
2090
2091static inline void
2092__xfrm6_sort(void **dst, void **src, int n,
2093 int (*cmp)(const void *p), int maxclass)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002094{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09002095 int i;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002096
Florian Westphal3aaf3912019-05-03 17:46:17 +02002097 for (i = 0; i < n; i++)
2098 dst[i] = src[i];
2099}
2100#endif /* CONFIG_IPV6 */
2101
2102void
2103xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
2104 unsigned short family)
2105{
2106 int i;
2107
2108 if (family == AF_INET6)
2109 __xfrm6_sort((void **)dst, (void **)src, n,
2110 __xfrm6_tmpl_sort_cmp, 5);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09002111 else
2112 for (i = 0; i < n; i++)
2113 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002114}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002115
Florian Westphal3aaf3912019-05-03 17:46:17 +02002116void
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002117xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
2118 unsigned short family)
2119{
Koichiro Den3f5a95a2017-08-01 23:21:46 +09002120 int i;
Fan Du283bc9f2013-11-07 17:47:50 +08002121
Florian Westphal3aaf3912019-05-03 17:46:17 +02002122 if (family == AF_INET6)
2123 __xfrm6_sort((void **)dst, (void **)src, n,
2124 __xfrm6_state_sort_cmp, 6);
Koichiro Den3f5a95a2017-08-01 23:21:46 +09002125 else
2126 for (i = 0; i < n; i++)
2127 dst[i] = src[i];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002128}
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002129#endif
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/* Silly enough, but I'm lazy to build resolution list */
2132
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002133static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002135 unsigned int h = xfrm_seq_hash(net, seq);
2136 struct xfrm_state *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002138 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 }
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return NULL;
2148}
2149
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002150struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct xfrm_state *x;
2153
Fan Du283bc9f2013-11-07 17:47:50 +08002154 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002155 x = __xfrm_find_acq_byseq(net, mark, seq);
Fan Du283bc9f2013-11-07 17:47:50 +08002156 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return x;
2158}
2159EXPORT_SYMBOL(xfrm_find_acq_byseq);
2160
2161u32 xfrm_get_acqseq(void)
2162{
2163 u32 res;
jamal6836b9b2010-02-16 02:01:22 +00002164 static atomic_t acqseq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
jamal6836b9b2010-02-16 02:01:22 +00002166 do {
2167 res = atomic_inc_return(&acqseq);
2168 } while (!res);
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return res;
2171}
2172EXPORT_SYMBOL(xfrm_get_acqseq);
2173
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002174int verify_spi_info(u8 proto, u32 min, u32 max, struct netlink_ext_ack *extack)
Fan Du776e9dd2013-12-16 18:47:49 +08002175{
2176 switch (proto) {
2177 case IPPROTO_AH:
2178 case IPPROTO_ESP:
2179 break;
2180
2181 case IPPROTO_COMP:
2182 /* IPCOMP spi is 16-bits. */
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002183 if (max >= 0x10000) {
2184 NL_SET_ERR_MSG(extack, "IPCOMP SPI must be <= 65535");
Fan Du776e9dd2013-12-16 18:47:49 +08002185 return -EINVAL;
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002186 }
Fan Du776e9dd2013-12-16 18:47:49 +08002187 break;
2188
2189 default:
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002190 NL_SET_ERR_MSG(extack, "Invalid protocol, must be one of AH, ESP, IPCOMP");
Fan Du776e9dd2013-12-16 18:47:49 +08002191 return -EINVAL;
2192 }
2193
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002194 if (min > max) {
2195 NL_SET_ERR_MSG(extack, "Invalid SPI range: min > max");
Fan Du776e9dd2013-12-16 18:47:49 +08002196 return -EINVAL;
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002197 }
Fan Du776e9dd2013-12-16 18:47:49 +08002198
2199 return 0;
2200}
2201EXPORT_SYMBOL(verify_spi_info);
2202
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002203int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
2204 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Alexey Dobriyan221df1e2008-11-25 17:30:50 -08002206 struct net *net = xs_net(x);
David S. Millerf034b5d2006-08-24 03:08:07 -07002207 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct xfrm_state *x0;
Herbert Xu658b2192007-10-09 13:29:52 -07002209 int err = -ENOENT;
2210 __be32 minspi = htonl(low);
2211 __be32 maxspi = htonl(high);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002212 __be32 newspi = 0;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002213 u32 mark = x->mark.v & x->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Herbert Xu658b2192007-10-09 13:29:52 -07002215 spin_lock_bh(&x->lock);
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002216 if (x->km.state == XFRM_STATE_DEAD) {
2217 NL_SET_ERR_MSG(extack, "Target ACQUIRE is in DEAD state");
Herbert Xu658b2192007-10-09 13:29:52 -07002218 goto unlock;
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002219 }
Herbert Xu658b2192007-10-09 13:29:52 -07002220
2221 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (x->id.spi)
Herbert Xu658b2192007-10-09 13:29:52 -07002223 goto unlock;
2224
2225 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 if (minspi == maxspi) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002228 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (x0) {
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002230 NL_SET_ERR_MSG(extack, "Requested SPI is already in use");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 xfrm_state_put(x0);
Herbert Xu658b2192007-10-09 13:29:52 -07002232 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002234 newspi = minspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 } else {
2236 u32 spi = 0;
Weilong Chen9b7a7872013-12-24 09:43:46 +08002237 for (h = 0; h < high-low+1; h++) {
Jason A. Donenfelde8a533c2022-10-09 20:44:02 -06002238 spi = get_random_u32_inclusive(low, high);
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08002239 x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (x0 == NULL) {
zhuoliang zhanga779d912020-10-23 09:05:35 +02002241 newspi = htonl(spi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 }
2244 xfrm_state_put(x0);
2245 }
2246 }
zhuoliang zhanga779d912020-10-23 09:05:35 +02002247 if (newspi) {
Fan Du283bc9f2013-11-07 17:47:50 +08002248 spin_lock_bh(&net->xfrm.xfrm_state_lock);
zhuoliang zhanga779d912020-10-23 09:05:35 +02002249 x->id.spi = newspi;
Alexey Dobriyan12604d82008-11-25 17:31:18 -08002250 h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
Leon Romanovsky3c611d42022-12-02 20:41:32 +02002251 XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
2252 x->xso.type);
Fan Du283bc9f2013-11-07 17:47:50 +08002253 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu658b2192007-10-09 13:29:52 -07002254
2255 err = 0;
Sabrina Dubrocac2dad112022-11-24 15:43:43 +01002256 } else {
2257 NL_SET_ERR_MSG(extack, "No SPI available in the requested range");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Herbert Xu658b2192007-10-09 13:29:52 -07002259
2260unlock:
2261 spin_unlock_bh(&x->lock);
2262
2263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265EXPORT_SYMBOL(xfrm_alloc_spi);
2266
Nicolas Dichteld3623092014-02-14 15:30:36 +01002267static bool __xfrm_state_filter_match(struct xfrm_state *x,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002268 struct xfrm_address_filter *filter)
Nicolas Dichteld3623092014-02-14 15:30:36 +01002269{
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
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002284int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08002285 int (*func)(struct xfrm_state *, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 void *data)
2287{
Herbert Xu12a169e2008-10-01 07:03:24 -07002288 struct xfrm_state *state;
2289 struct xfrm_state_walk *x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 int err = 0;
2291
Herbert Xu12a169e2008-10-01 07:03:24 -07002292 if (walk->seq != 0 && list_empty(&walk->all))
Timo Teras4c563f72008-02-28 21:31:08 -08002293 return 0;
2294
Fan Du283bc9f2013-11-07 17:47:50 +08002295 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002296 if (list_empty(&walk->all))
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002297 x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07002298 else
Li RongQing80077702015-04-22 17:09:54 +08002299 x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
Alexey Dobriyan284fa7d2008-11-25 17:32:14 -08002300 list_for_each_entry_from(x, &net->xfrm.state_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07002301 if (x->state == XFRM_STATE_DEAD)
Timo Teras4c563f72008-02-28 21:31:08 -08002302 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002303 state = container_of(x, struct xfrm_state, km);
2304 if (!xfrm_id_proto_match(state->id.proto, walk->proto))
Timo Teras4c563f72008-02-28 21:31:08 -08002305 continue;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002306 if (!__xfrm_state_filter_match(state, walk->filter))
2307 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07002308 err = func(state, walk->seq, data);
2309 if (err) {
2310 list_move_tail(&walk->all, &x->all);
2311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002313 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002315 if (walk->seq == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 err = -ENOENT;
2317 goto out;
2318 }
Herbert Xu12a169e2008-10-01 07:03:24 -07002319 list_del_init(&walk->all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320out:
Fan Du283bc9f2013-11-07 17:47:50 +08002321 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return err;
2323}
2324EXPORT_SYMBOL(xfrm_state_walk);
2325
Nicolas Dichteld3623092014-02-14 15:30:36 +01002326void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002327 struct xfrm_address_filter *filter)
Herbert Xu5c182452008-09-22 19:48:19 -07002328{
Herbert Xu12a169e2008-10-01 07:03:24 -07002329 INIT_LIST_HEAD(&walk->all);
Herbert Xu5c182452008-09-22 19:48:19 -07002330 walk->proto = proto;
Herbert Xu12a169e2008-10-01 07:03:24 -07002331 walk->state = XFRM_STATE_DEAD;
2332 walk->seq = 0;
Nicolas Dichteld3623092014-02-14 15:30:36 +01002333 walk->filter = filter;
Herbert Xu5c182452008-09-22 19:48:19 -07002334}
2335EXPORT_SYMBOL(xfrm_state_walk_init);
2336
Fan Du283bc9f2013-11-07 17:47:50 +08002337void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
Herbert Xuabb81c42008-09-09 19:58:29 -07002338{
Nicolas Dichteld3623092014-02-14 15:30:36 +01002339 kfree(walk->filter);
2340
Herbert Xu12a169e2008-10-01 07:03:24 -07002341 if (list_empty(&walk->all))
Herbert Xu5c182452008-09-22 19:48:19 -07002342 return;
Herbert Xu5c182452008-09-22 19:48:19 -07002343
Fan Du283bc9f2013-11-07 17:47:50 +08002344 spin_lock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002345 list_del(&walk->all);
Fan Du283bc9f2013-11-07 17:47:50 +08002346 spin_unlock_bh(&net->xfrm.xfrm_state_lock);
Herbert Xuabb81c42008-09-09 19:58:29 -07002347}
2348EXPORT_SYMBOL(xfrm_state_walk_done);
2349
Kees Cooke99e88a2017-10-16 14:43:17 -07002350static void xfrm_replay_timer_handler(struct timer_list *t)
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002351{
Kees Cooke99e88a2017-10-16 14:43:17 -07002352 struct xfrm_state *x = from_timer(x, t, rtimer);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002353
2354 spin_lock(&x->lock);
2355
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002356 if (x->km.state == XFRM_STATE_VALID) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002357 if (xfrm_aevent_is_on(xs_net(x)))
Florian Westphalcfc61c52021-06-18 15:51:56 +02002358 xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
Jamal Hadi Salim27170962006-04-14 15:03:05 -07002359 else
2360 x->xflags |= XFRM_TIME_DEFER;
2361 }
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002362
2363 spin_unlock(&x->lock);
2364}
2365
Denis Chengdf018122007-12-07 00:51:11 -08002366static LIST_HEAD(xfrm_km_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
David S. Miller214e0052011-02-24 00:02:38 -05002368void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 struct xfrm_mgr *km;
2371
Cong Wang85168c02013-01-16 16:05:06 +08002372 rcu_read_lock();
2373 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374 if (km->notify_policy)
2375 km->notify_policy(xp, dir, c);
Cong Wang85168c02013-01-16 16:05:06 +08002376 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002377}
2378
David S. Miller214e0052011-02-24 00:02:38 -05002379void km_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380{
2381 struct xfrm_mgr *km;
Cong Wang85168c02013-01-16 16:05:06 +08002382 rcu_read_lock();
2383 list_for_each_entry_rcu(km, &xfrm_km_list, list)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384 if (km->notify)
2385 km->notify(x, c);
Cong Wang85168c02013-01-16 16:05:06 +08002386 rcu_read_unlock();
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002387}
2388
2389EXPORT_SYMBOL(km_policy_notify);
2390EXPORT_SYMBOL(km_state_notify);
2391
Eric W. Biederman15e47302012-09-07 20:12:54 +00002392void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002393{
2394 struct km_event c;
2395
Herbert Xubf08867f92005-06-18 22:44:00 -07002396 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002397 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002398 c.event = XFRM_MSG_EXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002402EXPORT_SYMBOL(km_state_expired);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403/*
2404 * We send to all registered managers regardless of failure
2405 * We are happy with one success
2406*/
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002407int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002409 int err = -EINVAL, acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct xfrm_mgr *km;
2411
Cong Wang85168c02013-01-16 16:05:06 +08002412 rcu_read_lock();
2413 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Fan Du65e07362012-08-15 10:13:47 +08002414 acqret = km->acquire(x, t, pol);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415 if (!acqret)
2416 err = acqret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Cong Wang85168c02013-01-16 16:05:06 +08002418 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return err;
2420}
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002421EXPORT_SYMBOL(km_query);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Antony Antony4e484b32021-12-22 14:11:18 +01002423static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 int err = -EINVAL;
2426 struct xfrm_mgr *km;
2427
Cong Wang85168c02013-01-16 16:05:06 +08002428 rcu_read_lock();
2429 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (km->new_mapping)
2431 err = km->new_mapping(x, ipaddr, sport);
2432 if (!err)
2433 break;
2434 }
Cong Wang85168c02013-01-16 16:05:06 +08002435 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return err;
2437}
Antony Antony4e484b32021-12-22 14:11:18 +01002438
2439int 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456EXPORT_SYMBOL(km_new_mapping);
2457
Eric W. Biederman15e47302012-09-07 20:12:54 +00002458void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Herbert Xubf08867f92005-06-18 22:44:00 -07002462 c.data.hard = hard;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002463 c.portid = portid;
Herbert Xuf60f6b82005-06-18 22:44:37 -07002464 c.event = XFRM_MSG_POLEXPIRE;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002465 km_policy_notify(pol, dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
David S. Millera70fcb02006-03-20 19:18:52 -08002467EXPORT_SYMBOL(km_policy_expired);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002469#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002470int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2471 const struct xfrm_migrate *m, int num_migrate,
Antony Antony8bafd732017-06-06 12:12:14 +02002472 const struct xfrm_kmaddress *k,
2473 const struct xfrm_encap_tmpl *encap)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002474{
2475 int err = -EINVAL;
2476 int ret;
2477 struct xfrm_mgr *km;
2478
Cong Wang85168c02013-01-16 16:05:06 +08002479 rcu_read_lock();
2480 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002481 if (km->migrate) {
Antony Antony8bafd732017-06-06 12:12:14 +02002482 ret = km->migrate(sel, dir, type, m, num_migrate, k,
2483 encap);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002484 if (!ret)
2485 err = ret;
2486 }
2487 }
Cong Wang85168c02013-01-16 16:05:06 +08002488 rcu_read_unlock();
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002489 return err;
2490}
2491EXPORT_SYMBOL(km_migrate);
Eric Dumazet2d60abc2008-01-03 20:43:21 -08002492#endif
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002493
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002494int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002495{
2496 int err = -EINVAL;
2497 int ret;
2498 struct xfrm_mgr *km;
2499
Cong Wang85168c02013-01-16 16:05:06 +08002500 rcu_read_lock();
2501 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002502 if (km->report) {
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002503 ret = km->report(net, proto, sel, addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002504 if (!ret)
2505 err = ret;
2506 }
2507 }
Cong Wang85168c02013-01-16 16:05:06 +08002508 rcu_read_unlock();
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002509 return err;
2510}
2511EXPORT_SYMBOL(km_report);
2512
Florian Westphalbb9cd072019-04-17 11:45:13 +02002513static bool km_is_alive(const struct km_event *c)
Horia Geanta0f245582014-02-12 16:20:06 +02002514{
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}
Horia Geanta0f245582014-02-12 16:20:06 +02002529
Dmitry Safonovc9e7c762020-09-21 15:36:51 +01002530#if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
2531static DEFINE_SPINLOCK(xfrm_translator_lock);
2532static struct xfrm_translator __rcu *xfrm_translator;
2533
2534struct 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;
2544out:
2545 rcu_read_unlock();
2546 return xtr;
2547}
2548EXPORT_SYMBOL_GPL(xfrm_get_translator);
2549
2550void xfrm_put_translator(struct xfrm_translator *xtr)
2551{
2552 module_put(xtr->owner);
2553}
2554EXPORT_SYMBOL_GPL(xfrm_put_translator);
2555
2556int 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}
2569EXPORT_SYMBOL_GPL(xfrm_register_translator);
2570
2571int 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}
2587EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
2588#endif
2589
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002590int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 int err;
2593 u8 *data;
2594 struct xfrm_mgr *km;
2595 struct xfrm_policy *pol = NULL;
2596
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002597 if (sockptr_is_null(optval) && !optlen) {
Lorenzo Colittibe8f8282017-11-20 19:26:02 +09002598 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (optlen <= 0 || optlen > PAGE_SIZE)
2605 return -EMSGSIZE;
2606
Christoph Hellwigc6d1b262020-07-23 08:08:51 +02002607 data = memdup_sockptr(optval, optlen);
Geliang Tanga133d932017-05-06 23:42:21 +08002608 if (IS_ERR(data))
2609 return PTR_ERR(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002611 if (in_compat_syscall()) {
2612 struct xfrm_translator *xtr = xfrm_get_translator();
2613
Yu Kuai48f486e2020-11-10 09:14:43 +08002614 if (!xtr) {
2615 kfree(data);
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002616 return -EOPNOTSUPP;
Yu Kuai48f486e2020-11-10 09:14:43 +08002617 }
Dmitry Safonov96392ee2020-09-21 15:36:56 +01002618
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 err = -EINVAL;
Cong Wang85168c02013-01-16 16:05:06 +08002628 rcu_read_lock();
2629 list_for_each_entry_rcu(km, &xfrm_km_list, list) {
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002630 pol = km->compile_policy(sk, optname, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 optlen, &err);
2632 if (err >= 0)
2633 break;
2634 }
Cong Wang85168c02013-01-16 16:05:06 +08002635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 if (err >= 0) {
2638 xfrm_sk_policy_insert(sk, err, pol);
2639 xfrm_pol_put(pol);
Jonathan Basseri2b06cdf2017-10-25 09:52:27 -07002640 __sk_dst_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 err = 0;
2642 }
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 kfree(data);
2645 return err;
2646}
2647EXPORT_SYMBOL(xfrm_user_policy);
2648
Cong Wang85168c02013-01-16 16:05:06 +08002649static DEFINE_SPINLOCK(xfrm_km_lock);
2650
Zhengchao Shaof41b2842022-06-15 09:55:19 +08002651void xfrm_register_km(struct xfrm_mgr *km)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Cong Wang85168c02013-01-16 16:05:06 +08002653 spin_lock_bh(&xfrm_km_lock);
2654 list_add_tail_rcu(&km->list, &xfrm_km_list);
2655 spin_unlock_bh(&xfrm_km_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657EXPORT_SYMBOL(xfrm_register_km);
2658
Zhengchao Shaof41b2842022-06-15 09:55:19 +08002659void xfrm_unregister_km(struct xfrm_mgr *km)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Cong Wang85168c02013-01-16 16:05:06 +08002661 spin_lock_bh(&xfrm_km_lock);
2662 list_del_rcu(&km->list);
2663 spin_unlock_bh(&xfrm_km_lock);
2664 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666EXPORT_SYMBOL(xfrm_unregister_km);
2667
2668int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
2669{
2670 int err = 0;
Florian Westphal423826a2017-01-09 14:20:46 +01002671
2672 if (WARN_ON(afinfo->family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002674
Cong Wang44abdc32013-01-16 16:05:05 +08002675 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002677 err = -EEXIST;
David S. Milleredcd5822006-08-24 00:42:45 -07002678 else
Cong Wang44abdc32013-01-16 16:05:05 +08002679 rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
2680 spin_unlock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return err;
2682}
2683EXPORT_SYMBOL(xfrm_state_register_afinfo);
2684
2685int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
2686{
Florian Westphal423826a2017-01-09 14:20:46 +01002687 int err = 0, family = afinfo->family;
2688
2689 if (WARN_ON(family >= NPROTO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return -EAFNOSUPPORT;
Florian Westphal423826a2017-01-09 14:20:46 +01002691
Cong Wang44abdc32013-01-16 16:05:05 +08002692 spin_lock_bh(&xfrm_state_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
Florian Westphal423826a2017-01-09 14:20:46 +01002694 if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 err = -EINVAL;
David S. Milleredcd5822006-08-24 00:42:45 -07002696 else
Cong Wang44abdc32013-01-16 16:05:05 +08002697 RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Cong Wang44abdc32013-01-16 16:05:05 +08002699 spin_unlock_bh(&xfrm_state_afinfo_lock);
2700 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return err;
2702}
2703EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
2704
Florian Westphal711059b2017-01-09 14:20:48 +01002705struct 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}
Florian Westphal733a5fa2019-03-29 21:16:30 +01002712EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
Florian Westphal711059b2017-01-09 14:20:48 +01002713
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +02002714struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 struct xfrm_state_afinfo *afinfo;
2717 if (unlikely(family >= NPROTO))
2718 return NULL;
Cong Wang44abdc32013-01-16 16:05:05 +08002719 rcu_read_lock();
2720 afinfo = rcu_dereference(xfrm_state_afinfo[family]);
Herbert Xu546be242006-05-27 23:03:58 -07002721 if (unlikely(!afinfo))
Cong Wang44abdc32013-01-16 16:05:05 +08002722 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return afinfo;
2724}
2725
Steffen Klassertb48c05a2018-04-16 07:50:09 +02002726void xfrm_flush_gc(void)
2727{
2728 flush_work(&xfrm_state_gc_work);
2729}
2730EXPORT_SYMBOL(xfrm_flush_gc);
2731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
2733void 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);
Cong Wangf75a2802019-01-31 13:05:49 -08002741 xfrm_state_put_sync(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 x->tunnel = NULL;
2743 }
2744}
2745EXPORT_SYMBOL(xfrm_state_delete_tunnel);
2746
Jiri Bohaca6d95c52022-01-26 16:00:18 +01002747u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Florian Westphalb3b73b82017-01-05 13:23:58 +01002749 const struct xfrm_type *type = READ_ONCE(x->type);
Florian Westphalc7b37c72019-06-24 22:04:48 +02002750 struct crypto_aead *aead;
2751 u32 blksize, net_adj = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Florian Westphalc7b37c72019-06-24 22:04:48 +02002753 if (x->km.state != XFRM_STATE_VALID ||
2754 !type || type->proto != IPPROTO_ESP)
2755 return mtu - x->props.header_len;
Florian Westphalb3b73b82017-01-05 13:23:58 +01002756
Florian Westphalc7b37c72019-06-24 22:04:48 +02002757 aead = x->data;
2758 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
2759
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 }
2774
2775 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
2776 net_adj) & ~(blksize - 1)) + net_adj - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
Jiri Bohaca6d95c52022-01-26 16:00:18 +01002778EXPORT_SYMBOL_GPL(xfrm_state_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002780int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
2781 struct netlink_ext_ack *extack)
Herbert Xu72cb6962005-06-20 13:18:08 -07002782{
Florian Westphalc9500d72019-03-29 21:16:32 +01002783 const struct xfrm_mode *inner_mode;
2784 const struct xfrm_mode *outer_mode;
Herbert Xud094cd82005-06-20 13:19:41 -07002785 int family = x->props.family;
Herbert Xu72cb6962005-06-20 13:18:08 -07002786 int err;
2787
Florian Westphale4681742019-05-03 17:46:16 +02002788 if (family == AF_INET &&
Kuniyuki Iwashima0968d2a2022-07-13 13:51:52 -07002789 READ_ONCE(xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc))
Florian Westphale4681742019-05-03 17:46:16 +02002790 x->props.flags |= XFRM_STATE_NOPMTUDISC;
Herbert Xud094cd82005-06-20 13:19:41 -07002791
2792 err = -EPROTONOSUPPORT;
Herbert Xu13996372007-10-17 21:35:51 -07002793
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002794 if (x->sel.family != AF_UNSPEC) {
2795 inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002796 if (inner_mode == NULL) {
2797 NL_SET_ERR_MSG(extack, "Requested mode not found");
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002798 goto error;
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002799 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002800
2801 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002802 family != x->sel.family) {
2803 NL_SET_ERR_MSG(extack, "Only tunnel modes can accommodate a change of family");
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002804 goto error;
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002805 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002806
Florian Westphalc9500d72019-03-29 21:16:32 +01002807 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002808 } else {
Florian Westphal4c145dc2019-03-29 21:16:31 +01002809 const struct xfrm_mode *inner_mode_iaf;
Martin Willid81d2282008-12-03 15:38:07 -08002810 int iafamily = AF_INET;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002811
Martin Willid81d2282008-12-03 15:38:07 -08002812 inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002813 if (inner_mode == NULL) {
2814 NL_SET_ERR_MSG(extack, "Requested mode not found");
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002815 goto error;
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002816 }
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002817
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002818 if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
2819 NL_SET_ERR_MSG(extack, "Only tunnel modes can accommodate an AF_UNSPEC selector");
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002820 goto error;
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002821 }
Florian Westphal4c145dc2019-03-29 21:16:31 +01002822
Florian Westphalc9500d72019-03-29 21:16:32 +01002823 x->inner_mode = *inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002824
Martin Willid81d2282008-12-03 15:38:07 -08002825 if (x->props.family == AF_INET)
2826 iafamily = AF_INET6;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002827
Martin Willid81d2282008-12-03 15:38:07 -08002828 inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
2829 if (inner_mode_iaf) {
2830 if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
Florian Westphalc9500d72019-03-29 21:16:32 +01002831 x->inner_mode_iaf = *inner_mode_iaf;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -07002832 }
2833 }
Herbert Xu13996372007-10-17 21:35:51 -07002834
Herbert Xud094cd82005-06-20 13:19:41 -07002835 x->type = xfrm_get_type(x->id.proto, family);
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002836 if (x->type == NULL) {
2837 NL_SET_ERR_MSG(extack, "Requested type not found");
Herbert Xu72cb6962005-06-20 13:18:08 -07002838 goto error;
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002839 }
Herbert Xu72cb6962005-06-20 13:18:08 -07002840
Ilan Tayariffdb5212017-08-01 12:49:08 +03002841 x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
Steffen Klassert9d389d72017-04-14 10:05:44 +02002842
Sabrina Dubrocae1e10b42022-09-27 17:45:29 +02002843 err = x->type->init_state(x, extack);
Herbert Xu72cb6962005-06-20 13:18:08 -07002844 if (err)
2845 goto error;
2846
Florian Westphalc9500d72019-03-29 21:16:32 +01002847 outer_mode = xfrm_get_mode(x->props.mode, family);
2848 if (!outer_mode) {
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002849 NL_SET_ERR_MSG(extack, "Requested mode not found");
Julia Lawall599901c2012-08-29 06:49:15 +00002850 err = -EPROTONOSUPPORT;
Herbert Xub59f45d2006-05-27 23:05:54 -07002851 goto error;
Julia Lawall599901c2012-08-29 06:49:15 +00002852 }
Herbert Xub59f45d2006-05-27 23:05:54 -07002853
Florian Westphalc9500d72019-03-29 21:16:32 +01002854 x->outer_mode = *outer_mode;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002855 if (init_replay) {
Sabrina Dubroca1cf9a3a2022-09-14 19:04:06 +02002856 err = xfrm_init_replay(x, extack);
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002857 if (err)
2858 goto error;
2859 }
2860
Herbert Xu72cb6962005-06-20 13:18:08 -07002861error:
2862 return err;
2863}
2864
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002865EXPORT_SYMBOL(__xfrm_init_state);
2866
2867int xfrm_init_state(struct xfrm_state *x)
2868{
Yossi Kupermancc015722018-01-17 15:52:41 +02002869 int err;
2870
Sabrina Dubroca741f9a12022-09-14 19:04:05 +02002871 err = __xfrm_init_state(x, true, false, NULL);
Yossi Kupermancc015722018-01-17 15:52:41 +02002872 if (!err)
2873 x->km.state = XFRM_STATE_VALID;
2874
2875 return err;
Wei Yongjuna454f0c2011-03-21 18:08:28 -07002876}
2877
Herbert Xu72cb6962005-06-20 13:18:08 -07002878EXPORT_SYMBOL(xfrm_init_state);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002879
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002880int __net_init xfrm_state_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
David S. Millerf034b5d2006-08-24 03:08:07 -07002882 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Mathias Krause565f0fa2018-05-03 10:55:07 +02002884 if (net_eq(net, &init_net))
2885 xfrm_state_cache = KMEM_CACHE(xfrm_state,
2886 SLAB_HWCACHE_ALIGN | SLAB_PANIC);
2887
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002888 INIT_LIST_HEAD(&net->xfrm.state_all);
2889
David S. Millerf034b5d2006-08-24 03:08:07 -07002890 sz = sizeof(struct hlist_head) * 8;
2891
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002892 net->xfrm.state_bydst = xfrm_hash_alloc(sz);
2893 if (!net->xfrm.state_bydst)
2894 goto out_bydst;
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002895 net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
2896 if (!net->xfrm.state_bysrc)
2897 goto out_bysrc;
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002898 net->xfrm.state_byspi = xfrm_hash_alloc(sz);
2899 if (!net->xfrm.state_byspi)
2900 goto out_byspi;
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002901 net->xfrm.state_byseq = xfrm_hash_alloc(sz);
2902 if (!net->xfrm.state_byseq)
2903 goto out_byseq;
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002904 net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
David S. Millerf034b5d2006-08-24 03:08:07 -07002905
Alexey Dobriyan0bf7c5b2008-11-25 17:18:39 -08002906 net->xfrm.state_num = 0;
Alexey Dobriyan63082732008-11-25 17:19:07 -08002907 INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
Fan Du283bc9f2013-11-07 17:47:50 +08002908 spin_lock_init(&net->xfrm.xfrm_state_lock);
Ahmed S. Darwishbc8e0ad2021-03-16 11:56:30 +01002909 seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
2910 &net->xfrm.xfrm_state_lock);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002911 return 0;
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002912
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002913out_byseq:
2914 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002915out_byspi:
2916 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002917out_bysrc:
2918 xfrm_hash_free(net->xfrm.state_bydst, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002919out_bydst:
2920 return -ENOMEM;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002921}
2922
2923void xfrm_state_fini(struct net *net)
2924{
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002925 unsigned int sz;
2926
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002927 flush_work(&net->xfrm.state_hash_work);
Florian Westphal35db57bb2016-08-23 16:00:12 +02002928 flush_work(&xfrm_state_gc_work);
Cong Wangdbb24832019-03-22 16:26:19 -07002929 xfrm_state_flush(net, 0, false, true);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002930
Alexey Dobriyan9d4139c2008-11-25 17:16:11 -08002931 WARN_ON(!list_empty(&net->xfrm.state_all));
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002932
Alexey Dobriyan529983e2008-11-25 17:18:12 -08002933 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
Sabrina Dubrocafe9f1d82021-04-25 21:47:12 +02002934 WARN_ON(!hlist_empty(net->xfrm.state_byseq));
2935 xfrm_hash_free(net->xfrm.state_byseq, sz);
Alexey Dobriyanb754a4f2008-11-25 17:17:47 -08002936 WARN_ON(!hlist_empty(net->xfrm.state_byspi));
2937 xfrm_hash_free(net->xfrm.state_byspi, sz);
Alexey Dobriyand320bbb2008-11-25 17:17:24 -08002938 WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
2939 xfrm_hash_free(net->xfrm.state_bysrc, sz);
Alexey Dobriyan73d189d2008-11-25 17:16:58 -08002940 WARN_ON(!hlist_empty(net->xfrm.state_bydst));
2941 xfrm_hash_free(net->xfrm.state_bydst, sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Joy Lattenab5f5e82007-09-17 11:51:22 -07002944#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002945static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
2946 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002947{
Paul Moore68277ac2007-12-20 20:49:33 -08002948 struct xfrm_sec_ctx *ctx = x->security;
2949 u32 spi = ntohl(x->id.spi);
2950
2951 if (ctx)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002952 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
Paul Moore68277ac2007-12-20 20:49:33 -08002953 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002954
Weilong Chen9b7a7872013-12-24 09:43:46 +08002955 switch (x->props.family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002956 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002957 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2958 &x->props.saddr.a4, &x->id.daddr.a4);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002959 break;
2960 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002961 audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07002962 x->props.saddr.a6, x->id.daddr.a6);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002963 break;
2964 }
Paul Moore68277ac2007-12-20 20:49:33 -08002965
2966 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002967}
2968
Ilpo Järvinencf35f432008-01-05 23:13:20 -08002969static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
2970 struct audit_buffer *audit_buf)
Paul Mooreafeb14b2007-12-21 14:58:11 -08002971{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002972 const struct iphdr *iph4;
2973 const struct ipv6hdr *iph6;
Paul Mooreafeb14b2007-12-21 14:58:11 -08002974
2975 switch (family) {
2976 case AF_INET:
2977 iph4 = ip_hdr(skb);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002978 audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
2979 &iph4->saddr, &iph4->daddr);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002980 break;
2981 case AF_INET6:
2982 iph6 = ipv6_hdr(skb);
2983 audit_log_format(audit_buf,
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002984 " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
Weilong Chen9b7a7872013-12-24 09:43:46 +08002985 &iph6->saddr, &iph6->daddr,
Paul Mooreafeb14b2007-12-21 14:58:11 -08002986 iph6->flow_lbl[0] & 0x0f,
2987 iph6->flow_lbl[1],
2988 iph6->flow_lbl[2]);
2989 break;
2990 }
2991}
2992
Tetsuo Handa2e710292014-04-22 21:48:30 +09002993void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002994{
2995 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002996
Paul Mooreafeb14b2007-12-21 14:58:11 -08002997 audit_buf = xfrm_audit_start("SAD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002998 if (audit_buf == NULL)
2999 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003000 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003001 xfrm_audit_helper_sainfo(x, audit_buf);
3002 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003003 audit_log_end(audit_buf);
3004}
3005EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
3006
Tetsuo Handa2e710292014-04-22 21:48:30 +09003007void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003008{
3009 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003010
Paul Mooreafeb14b2007-12-21 14:58:11 -08003011 audit_buf = xfrm_audit_start("SAD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003012 if (audit_buf == NULL)
3013 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003014 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003015 xfrm_audit_helper_sainfo(x, audit_buf);
3016 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003017 audit_log_end(audit_buf);
3018}
3019EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003020
3021void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
3022 struct sk_buff *skb)
3023{
3024 struct audit_buffer *audit_buf;
3025 u32 spi;
3026
3027 audit_buf = xfrm_audit_start("SA-replay-overflow");
3028 if (audit_buf == NULL)
3029 return;
3030 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3031 /* don't record the sequence number because it's inherent in this kind
3032 * of audit message */
3033 spi = ntohl(x->id.spi);
3034 audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
3035 audit_log_end(audit_buf);
3036}
3037EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
3038
Steffen Klassert9fdc4882011-03-08 00:08:32 +00003039void xfrm_audit_state_replay(struct xfrm_state *x,
Paul Mooreafeb14b2007-12-21 14:58:11 -08003040 struct sk_buff *skb, __be32 net_seq)
3041{
3042 struct audit_buffer *audit_buf;
3043 u32 spi;
3044
3045 audit_buf = xfrm_audit_start("SA-replayed-pkt");
3046 if (audit_buf == NULL)
3047 return;
3048 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3049 spi = ntohl(x->id.spi);
3050 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3051 spi, spi, ntohl(net_seq));
3052 audit_log_end(audit_buf);
3053}
Steffen Klassert9fdc4882011-03-08 00:08:32 +00003054EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003055
3056void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
3057{
3058 struct audit_buffer *audit_buf;
3059
3060 audit_buf = xfrm_audit_start("SA-notfound");
3061 if (audit_buf == NULL)
3062 return;
3063 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3064 audit_log_end(audit_buf);
3065}
3066EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
3067
3068void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
3069 __be32 net_spi, __be32 net_seq)
3070{
3071 struct audit_buffer *audit_buf;
3072 u32 spi;
3073
3074 audit_buf = xfrm_audit_start("SA-notfound");
3075 if (audit_buf == NULL)
3076 return;
3077 xfrm_audit_helper_pktinfo(skb, family, audit_buf);
3078 spi = ntohl(net_spi);
3079 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3080 spi, spi, ntohl(net_seq));
3081 audit_log_end(audit_buf);
3082}
3083EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
3084
3085void xfrm_audit_state_icvfail(struct xfrm_state *x,
3086 struct sk_buff *skb, u8 proto)
3087{
3088 struct audit_buffer *audit_buf;
3089 __be32 net_spi;
3090 __be32 net_seq;
3091
3092 audit_buf = xfrm_audit_start("SA-icv-failure");
3093 if (audit_buf == NULL)
3094 return;
3095 xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
3096 if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
3097 u32 spi = ntohl(net_spi);
3098 audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
3099 spi, spi, ntohl(net_seq));
3100 }
3101 audit_log_end(audit_buf);
3102}
3103EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003104#endif /* CONFIG_AUDITSYSCALL */