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