xref: /freebsd/sys/dev/wg/compat.h (revision 744bfb213144c63cbaf38d91a1c4f7aebb9b9fbc)
1 /* SPDX-License-Identifier: MIT
2  *
3  * Copyright (C) 2021 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  * Copyright (c) 2022 The FreeBSD Foundation
5  *
6  * compat.h contains code that is backported from FreeBSD's main branch.
7  * It is different from support.h, which is for code that is not _yet_ upstream.
8  */
9 
10 #include <sys/param.h>
11 
12 #if (__FreeBSD_version < 1400036 && __FreeBSD_version >= 1400000) || __FreeBSD_version < 1300519
13 #define COMPAT_NEED_CHACHA20POLY1305_MBUF
14 #endif
15 
16 #if __FreeBSD_version < 1400048
17 #define COMPAT_NEED_CHACHA20POLY1305
18 #endif
19 
20 #if __FreeBSD_version < 1400049
21 #define COMPAT_NEED_CURVE25519
22 #endif
23 
24 #if __FreeBSD_version < 0x7fffffff /* TODO: update this when implemented */
25 #define COMPAT_NEED_BLAKE2S
26 #endif
27 
28 #if __FreeBSD_version < 1400059
29 #include <sys/sockbuf.h>
30 #define sbcreatecontrol(a, b, c, d, e) sbcreatecontrol(a, b, c, d)
31 #endif
32 
33 #if __FreeBSD_version < 1300507
34 #include <sys/smp.h>
35 #include <sys/gtaskqueue.h>
36 
37 struct taskqgroup_cpu {
38 	LIST_HEAD(, grouptask)  tgc_tasks;
39 	struct gtaskqueue       *tgc_taskq;
40 	int     tgc_cnt;
41 	int     tgc_cpu;
42 };
43 
44 struct taskqgroup {
45 	struct taskqgroup_cpu tqg_queue[MAXCPU];
46 	/* Other members trimmed from compat. */
47 };
48 
49 static inline void taskqgroup_drain_all(struct taskqgroup *tqg)
50 {
51 	struct gtaskqueue *q;
52 
53 	for (int i = 0; i < mp_ncpus; i++) {
54 		q = tqg->tqg_queue[i].tgc_taskq;
55 		if (q == NULL)
56 			continue;
57 		gtaskqueue_drain_all(q);
58 	}
59 }
60 #endif
61 
62 #if __FreeBSD_version < 1300000
63 #define VIMAGE
64 
65 #include <sys/types.h>
66 #include <sys/limits.h>
67 #include <sys/endian.h>
68 #include <sys/socket.h>
69 #include <sys/libkern.h>
70 #include <sys/malloc.h>
71 #include <sys/proc.h>
72 #include <sys/lock.h>
73 #include <sys/socketvar.h>
74 #include <sys/protosw.h>
75 #include <net/vnet.h>
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <vm/uma.h>
79 
80 #define taskqgroup_attach(a, b, c, d, e, f) taskqgroup_attach((a), (b), (c), -1, (f))
81 #define taskqgroup_attach_cpu(a, b, c, d, e, f, g) taskqgroup_attach_cpu((a), (b), (c), (d), -1, (g))
82 
83 #undef NET_EPOCH_ENTER
84 #define NET_EPOCH_ENTER(et) NET_EPOCH_ENTER_ET(et)
85 #undef NET_EPOCH_EXIT
86 #define NET_EPOCH_EXIT(et) NET_EPOCH_EXIT_ET(et)
87 #define NET_EPOCH_CALL(f, c) epoch_call(net_epoch_preempt, (c), (f))
88 #define NET_EPOCH_ASSERT() MPASS(in_epoch(net_epoch_preempt))
89 
90 #undef atomic_load_ptr
91 #define atomic_load_ptr(p) (*(volatile __typeof(*p) *)(p))
92 
93 #endif
94 
95 #if __FreeBSD_version < 1202000
96 static inline uint32_t arc4random_uniform(uint32_t bound)
97 {
98 	uint32_t ret, max_mod_bound;
99 
100 	if (bound < 2)
101 		return 0;
102 
103 	max_mod_bound = (1 + ~bound) % bound;
104 
105 	do {
106 		ret = arc4random();
107 	} while (ret < max_mod_bound);
108 
109 	return ret % bound;
110 }
111 
112 typedef void callout_func_t(void *);
113 
114 #ifndef CSUM_SND_TAG
115 #define CSUM_SND_TAG 0x80000000
116 #endif
117 
118 #endif
119