summaryrefslogblamecommitdiffstats
path: root/src/if_wg.h
blob: 743ffab4797ae1f8ecb0fe0941dd469cc2ee371a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                           

                   
 
                   
                       
 
  
                                                                   
  
                                                                    


                      

                               


                                                               
 

                                                            
 
         

                                                           

         










                                                          
 
             
                           

                                       
  
 








                                       
                

                               
               
                                     



                                     
                    
                                  
               

                                                  


                  
                    









                                                  
                                    
                              



                                  
 
                    









                                                   
  
 
                    




                                                   

  
                        
/*
 * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __IF_WG_H__
#define __IF_WG_H__

#include <net/if.h>
#include <netinet/in.h>

/*
 * This is the public interface to the WireGuard network interface.
 *
 * It is designed to be used by tools such as ifconfig(8) and wg(4).
 */

#define WG_KEY_SIZE 32
#define WG_PSK_SIZE WG_KEY_SIZE

static inline int _memchrcmp(uint8_t *b, uint8_t c, size_t l) {
	while (l--) if (*b++ != c) return 1; return 0;
}

#define IS_NULL_KEY(k) (!_memchrcmp(k, 0, WG_KEY_SIZE))
#define IS_MASKED_KEY(k) (!_memchrcmp(k, 0xff, WG_KEY_SIZE))

/* GET */
#define	SIOCGWGSERV	_IOWR('i', 200, struct wg_serv_get)
#define	SIOCGWGPEER	_IOWR('i', 201, struct wg_peer_get)

/* SET */
#define	SIOCSWGSERVPORT	_IOW('i', 202, struct wg_serv_set)
#define	SIOCSWGSERVKEY	_IOW('i', 203, struct wg_serv_set)
#define	SIOCCWGPEERS	_IOW('i', 204, struct wg_serv_set)

#define	SIOCSWGPEERIP	_IOW('i', 205, struct wg_peer_set)
#define	SIOCSWGPEERAIP	_IOW('i', 206, struct wg_peer_set)
#define	SIOCSWGPEERPSK	_IOW('i', 207, struct wg_peer_set)
#define	SIOCSWGPEERPKA	_IOW('i', 208, struct wg_peer_set)
#define	SIOCDWGPEER	_IOW('i', 209, struct wg_peer_set)
#define	SIOCDWGPEERAIP	_IOW('i', 210, struct wg_peer_set)
#define	SIOCCWGPEERAIP	_IOW('i', 211, struct wg_peer_set)

union wg_ip {
	struct sockaddr	sa;
	struct sockaddr_in	ip_in;
	struct sockaddr_in6	ip_in6;
};

struct wg_keypair {
	struct wg_pubkey {
		uint8_t k[WG_KEY_SIZE];
	} pub;
	struct wg_privkey {
		uint8_t k[WG_KEY_SIZE];
	} priv;
};

struct wg_cidr {
	sa_family_t	c_af;
	uint8_t		c_mask;
	union {
		struct in_addr	ipv4;
		struct in6_addr	ipv6;
	} c_ip;
};

struct wg_serv_set {
	char	ss_name[IFNAMSIZ];
	union {
		struct wg_privkey	d_privkey;
		in_port_t		d_port;
	} ss_data;
};

struct wg_peer_set {
	char			sp_name[IFNAMSIZ];
	struct wg_pubkey	sp_pubkey;
	union {
		struct wg_privkey	d_shared;
		struct	wg_cidr		d_route;
		union	wg_ip		d_ip;
		uint16_t		d_pka;
	} sp_data;
};

#define ss_privkey ss_data.d_privkey
#define ss_port ss_data.d_port
#define sp_shared sp_data.d_shared
#define sp_route sp_data.d_route
#define sp_pka sp_data.d_pka
#define sp_ip sp_data.d_ip

struct wg_peer_get {
	char			 gp_name[IFNAMSIZ];
	struct wg_pubkey 	 gp_pubkey;
	struct wg_privkey 	 gp_shared;
	union wg_ip		 gp_ip;
	uint16_t		 gp_pka;
	uint64_t 		 gp_tx_bytes;
	uint64_t		 gp_rx_bytes;
	struct timespec		 gp_last_handshake;
	struct wg_cidr		*gp_routes;
	size_t			 gp_num_routes;
};

struct wg_serv_get {
	char			 gs_name[IFNAMSIZ];
	struct wg_keypair	 gs_keypair;
	in_port_t		 gs_port;
	struct wg_pubkey	*gs_peers;
	size_t			 gs_num_peers;
};

#endif /* __IF_WG_H__ */