2018-09-19 19:49:47 -04:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/*
|
2019-01-01 19:58:10 -05:00
|
|
|
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 10:23:50 -05:00
|
|
|
*/
|
2017-09-24 22:22:09 -04:00
|
|
|
|
|
|
|
#ifndef CONTAINERS_H
|
|
|
|
#define CONTAINERS_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2017-11-22 10:49:56 -05:00
|
|
|
#include <time.h>
|
2018-05-21 00:10:54 -04:00
|
|
|
#include <sys/socket.h>
|
2017-09-24 22:22:09 -04:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include "../uapi/wireguard.h"
|
|
|
|
|
2019-01-22 07:38:52 -05:00
|
|
|
/* Cross platform __kernel_timespec */
|
|
|
|
struct timespec64 {
|
|
|
|
int64_t tv_sec;
|
|
|
|
int64_t tv_nsec;
|
|
|
|
};
|
|
|
|
|
2017-09-24 22:22:09 -04:00
|
|
|
struct wgallowedip {
|
|
|
|
uint16_t family;
|
|
|
|
union {
|
|
|
|
struct in_addr ip4;
|
|
|
|
struct in6_addr ip6;
|
|
|
|
};
|
|
|
|
uint8_t cidr;
|
|
|
|
struct wgallowedip *next_allowedip;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2017-10-03 00:18:45 -04:00
|
|
|
WGPEER_REMOVE_ME = 1U << 0,
|
|
|
|
WGPEER_REPLACE_ALLOWEDIPS = 1U << 1,
|
2017-11-10 22:30:21 -05:00
|
|
|
WGPEER_HAS_PUBLIC_KEY = 1U << 2,
|
|
|
|
WGPEER_HAS_PRESHARED_KEY = 1U << 3,
|
|
|
|
WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL = 1U << 4
|
2017-09-24 22:22:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wgpeer {
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
uint8_t public_key[WG_KEY_LEN];
|
|
|
|
uint8_t preshared_key[WG_KEY_LEN];
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct sockaddr addr;
|
|
|
|
struct sockaddr_in addr4;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
} endpoint;
|
|
|
|
|
2019-01-22 07:38:52 -05:00
|
|
|
struct timespec64 last_handshake_time;
|
2017-09-24 22:22:09 -04:00
|
|
|
uint64_t rx_bytes, tx_bytes;
|
|
|
|
uint16_t persistent_keepalive_interval;
|
|
|
|
|
2017-10-10 11:17:43 -04:00
|
|
|
struct wgallowedip *first_allowedip, *last_allowedip;
|
2017-09-24 22:22:09 -04:00
|
|
|
struct wgpeer *next_peer;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2017-10-03 00:18:45 -04:00
|
|
|
WGDEVICE_REPLACE_PEERS = 1U << 0,
|
|
|
|
WGDEVICE_HAS_PRIVATE_KEY = 1U << 1,
|
2017-11-10 22:30:21 -05:00
|
|
|
WGDEVICE_HAS_PUBLIC_KEY = 1U << 2,
|
|
|
|
WGDEVICE_HAS_LISTEN_PORT = 1U << 3,
|
|
|
|
WGDEVICE_HAS_FWMARK = 1U << 4
|
2017-09-24 22:22:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct wgdevice {
|
|
|
|
char name[IFNAMSIZ];
|
2017-10-09 07:27:00 -04:00
|
|
|
uint32_t ifindex;
|
2017-09-24 22:22:09 -04:00
|
|
|
|
|
|
|
uint32_t flags;
|
|
|
|
|
|
|
|
uint8_t public_key[WG_KEY_LEN];
|
|
|
|
uint8_t private_key[WG_KEY_LEN];
|
|
|
|
|
|
|
|
uint32_t fwmark;
|
|
|
|
uint16_t listen_port;
|
|
|
|
|
2017-10-10 11:17:43 -04:00
|
|
|
struct wgpeer *first_peer, *last_peer;
|
2017-09-24 22:22:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define for_each_wgpeer(__dev, __peer) for ((__peer) = (__dev)->first_peer; (__peer); (__peer) = (__peer)->next_peer)
|
|
|
|
#define for_each_wgallowedip(__peer, __allowedip) for ((__allowedip) = (__peer)->first_allowedip; (__allowedip); (__allowedip) = (__allowedip)->next_allowedip)
|
|
|
|
|
|
|
|
static inline void free_wgdevice(struct wgdevice *dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
for (struct wgpeer *peer = dev->first_peer, *np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
|
|
|
|
for (struct wgallowedip *allowedip = peer->first_allowedip, *na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
|
|
|
|
free(allowedip);
|
|
|
|
free(peer);
|
|
|
|
}
|
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|