aboutsummaryrefslogtreecommitdiffstats
path: root/src/support.h
blob: 5eff4fb768acda37b677c6ada66ea308f569a269 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* SPDX-License-Identifier: ISC
 *
 * Copyright (C) 2021 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * Copyright (C) 2021 Matt Dunwoodie <ncon@noconroy.net>
 *
 * support.h contains functions that are either not _yet_ upstream in FreeBSD 14, or are shimmed
 * from OpenBSD. It is different from compat.h, which is strictly for backports.
 */

#ifndef _WG_SUPPORT
#define _WG_SUPPORT

#include <sys/types.h>
#include <sys/limits.h>
#include <sys/endian.h>
#include <sys/socket.h>
#include <sys/libkern.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <net/vnet.h>
#include <vm/uma.h>

/* TODO the following is openbsd compat defines to allow us to copy the wg_*
 * files from openbsd (almost) verbatim. this will greatly increase maintenance
 * across the platforms. it should be moved to it's own file. the only thing
 * we're missing from this is struct pool (freebsd: uma_zone_t), which isn't a
 * show stopper, but is something worth considering in the future.
 *  - md */

#define rw_assert_wrlock(x) rw_assert(x, RA_WLOCKED)
#define rw_enter_write rw_wlock
#define rw_exit_write rw_wunlock
#define rw_enter_read rw_rlock
#define rw_exit_read rw_runlock
#define rw_exit rw_unlock

#define RW_DOWNGRADE 1
#define rw_enter(x, y) do {		\
	CTASSERT(y == RW_DOWNGRADE);	\
	rw_downgrade(x);		\
} while (0)

MALLOC_DECLARE(M_WG);

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

#ifndef PRIV_NET_WG
#define PRIV_NET_WG PRIV_NET_HWIOCTL
#endif

#ifndef IFT_WIREGUARD
#define IFT_WIREGUARD IFT_PPP
#endif

#ifndef ck_pr_store_bool
#define ck_pr_store_bool(dst, val) ck_pr_store_8((uint8_t *)(dst), (uint8_t)(val))
#endif

#ifndef ck_pr_load_bool
#define ck_pr_load_bool(src) ((bool)ck_pr_load_8((uint8_t *)(src)))
#endif


static inline int
sogetsockaddr(struct socket *so, struct sockaddr **nam)
{
	int error;

	CURVNET_SET(so->so_vnet);
	error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, nam);
	CURVNET_RESTORE();
	return (error);
}

#endif