ipc: openbsd: switch to array ioctl interface

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-05-13 00:37:46 -06:00
parent 0cfde946b1
commit 56cb39fb22
2 changed files with 79 additions and 88 deletions

View file

@ -986,12 +986,12 @@ static int kernel_get_device(struct wgdevice **device, const char *iface)
goto out; goto out;
if (last_size >= wgdata.wgd_size) if (last_size >= wgdata.wgd_size)
break; break;
wgdata.wgd_mem = realloc(wgdata.wgd_mem, wgdata.wgd_size); wgdata.wgd_interface = realloc(wgdata.wgd_interface, wgdata.wgd_size);
if (!wgdata.wgd_mem) if (!wgdata.wgd_interface)
goto out; goto out;
} }
wg_iface = wgdata.wgd_mem; wg_iface = wgdata.wgd_interface;
dev = calloc(1, sizeof(*dev)); dev = calloc(1, sizeof(*dev));
if (!dev) if (!dev)
goto out; goto out;
@ -1017,7 +1017,8 @@ static int kernel_get_device(struct wgdevice **device, const char *iface)
dev->flags |= WGDEVICE_HAS_PRIVATE_KEY; dev->flags |= WGDEVICE_HAS_PRIVATE_KEY;
} }
for (wg_peer = wg_iface->i_peers; wg_peer; wg_peer = wg_peer->p_next) { wg_peer = &wg_iface->i_peers[0];
for (size_t i = 0; i < wg_iface->i_peers_count; ++i) {
peer = calloc(1, sizeof(*peer)); peer = calloc(1, sizeof(*peer));
if (!peer) if (!peer)
goto out; goto out;
@ -1052,7 +1053,8 @@ static int kernel_get_device(struct wgdevice **device, const char *iface)
peer->last_handshake_time.tv_sec = wg_peer->p_last_handshake.tv_sec; peer->last_handshake_time.tv_sec = wg_peer->p_last_handshake.tv_sec;
peer->last_handshake_time.tv_nsec = wg_peer->p_last_handshake.tv_nsec; peer->last_handshake_time.tv_nsec = wg_peer->p_last_handshake.tv_nsec;
for (wg_aip = wg_peer->p_aips; wg_aip; wg_aip = wg_aip->a_next) { wg_aip = &wg_peer->p_aips[0];
for (size_t j = 0; j < wg_peer->p_aips_count; ++j) {
aip = calloc(1, sizeof(*aip)); aip = calloc(1, sizeof(*aip));
if (!aip) if (!aip)
goto out; goto out;
@ -1071,55 +1073,63 @@ static int kernel_get_device(struct wgdevice **device, const char *iface)
memcpy(&aip->ip6, &wg_aip->a_ipv6, sizeof(aip->ip6)); memcpy(&aip->ip6, &wg_aip->a_ipv6, sizeof(aip->ip6));
aip->cidr = wg_aip->a_cidr; aip->cidr = wg_aip->a_cidr;
} }
++wg_aip;
} }
wg_peer = (struct wg_peer_io *)wg_aip;
} }
*device = dev; *device = dev;
errno = 0; errno = 0;
out: out:
ret = -errno; ret = -errno;
free(wgdata.wgd_mem); free(wgdata.wgd_interface);
return ret; return ret;
} }
static int kernel_set_device(struct wgdevice *dev) static int kernel_set_device(struct wgdevice *dev)
{ {
struct wg_data_io wgdata = { .wgd_size = 0 }; struct wg_data_io wgdata = { .wgd_size = sizeof(struct wg_interface_io) };
struct wg_interface_io wg_iface = { 0 }; struct wg_interface_io *wg_iface;
struct wg_peer_io *wg_peer, *wg_peer_next; struct wg_peer_io *wg_peer;
struct wg_aip_io *wg_aip, *wg_aip_next; struct wg_aip_io *wg_aip;
struct wgpeer *peer; struct wgpeer *peer;
struct wgallowedip *aip; struct wgallowedip *aip;
int s = get_dgram_socket(), ret; int s = get_dgram_socket(), ret;
size_t peer_count, aip_count;
if (s < 0) if (s < 0)
return -errno; return -errno;
for_each_wgpeer(dev, peer) {
wgdata.wgd_size += sizeof(struct wg_peer_io);
for_each_wgallowedip(peer, aip)
wgdata.wgd_size += sizeof(struct wg_aip_io);
}
wg_iface = wgdata.wgd_interface = calloc(1, wgdata.wgd_size);
if (!wgdata.wgd_interface)
return -errno;
strlcpy(wgdata.wgd_name, dev->name, sizeof(wgdata.wgd_name)); strlcpy(wgdata.wgd_name, dev->name, sizeof(wgdata.wgd_name));
wgdata.wgd_mem = &wg_iface;
if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY) { if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY) {
memcpy(wg_iface.i_private, dev->private_key, sizeof(wg_iface.i_private)); memcpy(wg_iface->i_private, dev->private_key, sizeof(wg_iface->i_private));
wg_iface.i_flags |= WG_INTERFACE_HAS_PRIVATE; wg_iface->i_flags |= WG_INTERFACE_HAS_PRIVATE;
} }
if (dev->flags & WGDEVICE_HAS_LISTEN_PORT) { if (dev->flags & WGDEVICE_HAS_LISTEN_PORT) {
wg_iface.i_port = dev->listen_port; wg_iface->i_port = dev->listen_port;
wg_iface.i_flags |= WG_INTERFACE_HAS_PORT; wg_iface->i_flags |= WG_INTERFACE_HAS_PORT;
} }
if (dev->flags & WGDEVICE_HAS_FWMARK) { if (dev->flags & WGDEVICE_HAS_FWMARK) {
wg_iface.i_rtable = dev->fwmark; wg_iface->i_rtable = dev->fwmark;
wg_iface.i_flags |= WG_INTERFACE_HAS_RTABLE; wg_iface->i_flags |= WG_INTERFACE_HAS_RTABLE;
} }
if (dev->flags & WGDEVICE_REPLACE_PEERS) if (dev->flags & WGDEVICE_REPLACE_PEERS)
wg_iface.i_flags |= WG_INTERFACE_REPLACE_PEERS; wg_iface->i_flags |= WG_INTERFACE_REPLACE_PEERS;
peer_count = 0;
wg_peer = &wg_iface->i_peers[0];
for_each_wgpeer(dev, peer) { for_each_wgpeer(dev, peer) {
wg_peer = calloc(1, sizeof(*wg_peer));
if (!wg_peer)
goto out;
wg_peer->p_flags = WG_PEER_HAS_PUBLIC; wg_peer->p_flags = WG_PEER_HAS_PUBLIC;
memcpy(wg_peer->p_public, peer->public_key, sizeof(wg_peer->p_public)); memcpy(wg_peer->p_public, peer->public_key, sizeof(wg_peer->p_public));
@ -1145,14 +1155,9 @@ static int kernel_set_device(struct wgdevice *dev)
if (peer->flags & WGPEER_REMOVE_ME) if (peer->flags & WGPEER_REMOVE_ME)
wg_peer->p_flags |= WG_PEER_REMOVE; wg_peer->p_flags |= WG_PEER_REMOVE;
wg_peer->p_next = wg_iface.i_peers; aip_count = 0;
wg_iface.i_peers = wg_peer; wg_aip = &wg_peer->p_aips[0];
for_each_wgallowedip(peer, aip) { for_each_wgallowedip(peer, aip) {
wg_aip = calloc(1, sizeof(*wg_aip));
if (!wg_aip)
goto out;
wg_aip->a_af = aip->family; wg_aip->a_af = aip->family;
wg_aip->a_cidr = aip->cidr; wg_aip->a_cidr = aip->cidr;
@ -1161,14 +1166,16 @@ static int kernel_set_device(struct wgdevice *dev)
} else if (aip->family == AF_INET6) { } else if (aip->family == AF_INET6) {
memcpy(&wg_aip->a_ipv6, &aip->ip6, sizeof(wg_aip->a_ipv6)); memcpy(&wg_aip->a_ipv6, &aip->ip6, sizeof(wg_aip->a_ipv6));
} else { } else {
free(wg_aip);
continue; continue;
} }
++aip_count;
wg_aip->a_next = wg_peer->p_aips; ++wg_aip;
wg_peer->p_aips = wg_aip;
} }
wg_peer->p_aips_count = aip_count;
++peer_count;
wg_peer = (struct wg_peer_io *)wg_aip;
} }
wg_iface->i_peers_count = peer_count;
if (ioctl(s, SIOCSWG, (caddr_t)&wgdata) < 0) if (ioctl(s, SIOCSWG, (caddr_t)&wgdata) < 0)
goto out; goto out;
@ -1176,14 +1183,7 @@ static int kernel_set_device(struct wgdevice *dev)
out: out:
ret = -errno; ret = -errno;
for (wg_peer = wg_iface.i_peers; wg_peer; wg_peer = wg_peer_next) { free(wgdata.wgd_interface);
for (wg_aip = wg_peer->p_aips; wg_aip; wg_aip = wg_aip_next) {
wg_aip_next = wg_aip->a_next;
free(wg_aip);
}
wg_peer_next = wg_peer->p_next;
free(wg_peer);
}
return ret; return ret;
} }
#endif #endif

View file

@ -25,26 +25,16 @@
#define SIOCSWG _IOWR('i', 210, struct wg_data_io) #define SIOCSWG _IOWR('i', 210, struct wg_data_io)
#define SIOCGWG _IOWR('i', 211, struct wg_data_io) #define SIOCGWG _IOWR('i', 211, struct wg_data_io)
struct wg_data_io { #define a_ipv4 a_addr.addr_ipv4
char wgd_name[IFNAMSIZ]; #define a_ipv6 a_addr.addr_ipv6
size_t wgd_size; /* size of the mem below */
void *wgd_mem; /* wg_interface_io{1},(wg_peer_io,wg_aip_io*)* */
};
#define WG_INTERFACE_HAS_PUBLIC (1 << 0) struct wg_aip_io {
#define WG_INTERFACE_HAS_PRIVATE (1 << 1) sa_family_t a_af;
#define WG_INTERFACE_HAS_PORT (1 << 2) int a_cidr;
#define WG_INTERFACE_HAS_RTABLE (1 << 3) union wg_aip_addr {
#define WG_INTERFACE_REPLACE_PEERS (1 << 4) struct in_addr addr_ipv4;
struct in6_addr addr_ipv6;
struct wg_interface_io { } a_addr;
uint8_t i_flags;
struct wg_peer_io *i_peers;
in_port_t i_port;
int i_rtable;
uint8_t i_public[WG_KEY_LEN];
uint8_t i_private[WG_KEY_LEN];
}; };
#define WG_PEER_HAS_PUBLIC (1 << 0) #define WG_PEER_HAS_PUBLIC (1 << 0)
@ -61,9 +51,6 @@ struct wg_interface_io {
struct wg_peer_io { struct wg_peer_io {
int p_flags; int p_flags;
struct wg_peer_io *p_next;
struct wg_aip_io *p_aips;
int p_protocol_version; int p_protocol_version;
uint8_t p_public[WG_KEY_LEN]; uint8_t p_public[WG_KEY_LEN];
uint8_t p_psk[WG_KEY_LEN]; uint8_t p_psk[WG_KEY_LEN];
@ -73,29 +60,33 @@ struct wg_peer_io {
struct sockaddr_in sa_sin; struct sockaddr_in sa_sin;
struct sockaddr_in6 sa_sin6; struct sockaddr_in6 sa_sin6;
} p_endpoint; } p_endpoint;
uint64_t p_txbytes; uint64_t p_txbytes;
uint64_t p_rxbytes; uint64_t p_rxbytes;
struct timespec p_last_handshake; /* nanotime */ struct timespec p_last_handshake; /* nanotime */
size_t p_aips_count;
struct wg_aip_io p_aips[];
}; };
#define a_af a_data.d_af #define WG_INTERFACE_HAS_PUBLIC (1 << 0)
#define a_cidr a_data.d_cidr #define WG_INTERFACE_HAS_PRIVATE (1 << 1)
#define a_addr a_data.d_addr #define WG_INTERFACE_HAS_PORT (1 << 2)
#define a_ipv4 a_addr.addr_ipv4 #define WG_INTERFACE_HAS_RTABLE (1 << 3)
#define a_ipv6 a_addr.addr_ipv6 #define WG_INTERFACE_REPLACE_PEERS (1 << 4)
struct wg_aip_io { struct wg_interface_io {
struct wg_aip_io *a_next; uint8_t i_flags;
in_port_t i_port;
int i_rtable;
uint8_t i_public[WG_KEY_LEN];
uint8_t i_private[WG_KEY_LEN];
size_t i_peers_count;
struct wg_peer_io i_peers[];
};
struct wg_aip_data { struct wg_data_io {
sa_family_t d_af; char wgd_name[IFNAMSIZ];
int d_cidr; size_t wgd_size; /* total size of the memory pointed to by wgd_interface */
union wg_aip_addr { struct wg_interface_io *wgd_interface;
struct in_addr addr_ipv4;
struct in6_addr addr_ipv6;
} d_addr;
} a_data;
}; };
#endif /* __IF_WG_H__ */ #endif /* __IF_WG_H__ */