summaryrefslogblamecommitdiffstats
path: root/src/if_wg.h
blob: 122a06570081cf9f1cf80dc1910304587d7aefbd (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>

/*
 * 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_get_serv)
#define	SIOCGWGPEER	_IOWR('i', 201, struct wg_get_peer)

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

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

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

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_set_peer {
	char	sp_name[IFNAMSIZ];
	uint8_t 	sp_pubkey[WG_KEY_SIZE];
	union {
		uint8_t	d_psk[WG_PSK_SIZE];
		uint16_t	d_pka;
		union	wg_ip	d_ip;
		struct	wg_cidr	d_aip;
	} sp_data;
};

struct wg_set_serv {
	char	ss_name[IFNAMSIZ];
	union {
		uint8_t	d_privkey[WG_KEY_SIZE];
		in_port_t	d_port;
	} ss_data;
};

#define sp_psk sp_data.d_psk
#define sp_pka sp_data.d_pka
#define sp_ip sp_data.d_ip
#define sp_aip sp_data.d_aip
#define ss_privkey ss_data.d_privkey
#define ss_port ss_data.d_port

struct wg_get_peer {
	char	gp_name[IFNAMSIZ];
	uint8_t 	gp_pubkey[WG_KEY_SIZE];
	uint8_t 	gp_psk   [WG_PSK_SIZE];
	uint64_t 	gp_tx_bytes, gp_rx_bytes;
	uint16_t	gp_pka;
	union wg_ip	gp_ip;
	struct timespec	gp_last_handshake;
	size_t		gp_num_aip;
	struct wg_cidr	*gp_aip;
};

struct wg_get_serv {
	char	gs_name[IFNAMSIZ];
	uint8_t 	gs_pubkey[WG_KEY_SIZE];
	uint8_t 	gs_privkey[WG_KEY_SIZE];
	in_port_t	gs_port;
	size_t 		gs_num_peers;
	uint8_t	(*gs_peers)[WG_KEY_SIZE];
};

#endif /* __IF_WG_H__ */