1 #ifndef _MLVPN_H
2 #define _MLVPN_H
3 
4 #include "includes.h"
5 
6 #include <unistd.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <sys/queue.h>
11 #include <sys/un.h>
12 #include <netinet/in.h>
13 #include <netinet/tcp.h>
14 #include <time.h>
15 #include <math.h>
16 #include <ev.h>
17 
18 /* Many thanks Fabien Dupont! */
19 #ifdef HAVE_LINUX
20  /* Absolutely essential to have it there for IFNAMSIZ */
21  #include <sys/types.h>
22  #include <netdb.h>
23  #include <linux/if.h>
24 #endif
25 
26 #include <arpa/inet.h>
27 
28 #ifdef HAVE_VALGRIND_VALGRIND_H
29  #include <valgrind/valgrind.h>
30 #else
31  #define RUNNING_ON_VALGRIND 0
32 #endif
33 
34 #ifdef HAVE_DECL_RES_INIT
35  #include <netinet/in.h>
36  #include <arpa/nameser.h>
37  #include <resolv.h>
38 #endif
39 
40 #ifdef HAVE_FILTERS
41  #include <pcap/pcap.h>
42 #endif
43 
44 #include "pkt.h"
45 #include "buffer.h"
46 #include "reorder.h"
47 #include "timestamp.h"
48 
49 #define MLVPN_MAXHNAMSTR 256
50 #define MLVPN_MAXPORTSTR 5
51 
52 /* Number of packets in the queue. Each pkt is ~ 1520 */
53 /* 1520 * 128 ~= 24 KBytes of data maximum per channel VMSize */
54 #define PKTBUFSIZE 128
55 
56 /* tuntap interface name size */
57 #ifndef IFNAMSIZ
58  #define IFNAMSIZ 16
59 #endif
60 #define MLVPN_IFNAMSIZ IFNAMSIZ
61 
62 /* How frequently we check tunnels */
63 #define MLVPN_IO_TIMEOUT_DEFAULT 1.0
64 /* What is the maximum retry timeout */
65 #define MLVPN_IO_TIMEOUT_MAXIMUM 60.0
66 /* In case we can't open the tunnel, retry every time with previous
67  * timeout multiplied by the increment.
68  * Example:
69  * 1st try t+0: bind error
70  * 2nd try t+1: bind error
71  * 3rd try t+2: bind error
72  * 4rd try t+4: dns error
73  * ...
74  * n try t+60
75  * n+1 try t+60
76  */
77 #define MLVPN_IO_TIMEOUT_INCREMENT 2
78 
79 #define NEXT_KEEPALIVE(now, t) (now + 2)
80 /* Protocol version of mlvpn
81  * version 0: mlvpn 2.0 to 2.1
82  * version 1: mlvpn 2.2+ (add reorder field in mlvpn_proto_t)
83  */
84 #define MLVPN_PROTOCOL_VERSION 1
85 
86 struct mlvpn_options_s
87 {
88     /* use ps_status or not ? */
89     int change_process_title;
90     /* process name if set */
91     char process_name[1024];
92     /* where is the config file */
93     char control_unix_path[MAXPATHLEN];
94     char control_bind_host[MLVPN_MAXHNAMSTR];
95     char control_bind_port[MLVPN_MAXHNAMSTR];
96     char config_path[MAXPATHLEN];
97     /* tunnel configuration for the status command script */
98     char ip4[24];
99     char ip6[128]; /* Should not exceed 45 + 3 + 1 bytes */
100     char ip4_gateway[16];
101     char ip6_gateway[128];
102     char ip4_routes[4096]; /* Allow about 200 routes minimum */
103     char ip6_routes[8192]; /* Allow about 80 routes minimum */
104     int mtu;
105     int config_fd;
106     /* log verbosity */
107     int verbose;
108     int debug;
109     /* User change if running as root */
110     char unpriv_user[128];
111     int cleartext_data;
112     int root_allowed;
113     uint32_t reorder_buffer_size;
114     uint32_t fallback_available;
115 };
116 
117 struct mlvpn_status_s
118 {
119     int fallback_mode;
120     int connected;
121     int initialized;
122     time_t start_time;
123     time_t last_reload;
124 };
125 
126 enum chap_status {
127     MLVPN_DISCONNECTED,
128     MLVPN_AUTHSENT,
129     MLVPN_AUTHOK,
130     MLVPN_LOSSY
131 };
132 
133 LIST_HEAD(rtunhead, mlvpn_tunnel_s) rtuns;
134 
135 typedef struct mlvpn_tunnel_s
136 {
137     LIST_ENTRY(mlvpn_tunnel_s) entries;
138     char *name;           /* tunnel name */
139     char bindaddr[MLVPN_MAXHNAMSTR]; /* packets source */
140     char bindport[MLVPN_MAXPORTSTR]; /* packets port source (or NULL) */
141 	uint32_t bindfib;     /* FIB number to use */
142     char destaddr[MLVPN_MAXHNAMSTR]; /* remote server ip (can be hostname) */
143     char destport[MLVPN_MAXPORTSTR]; /* remote server port */
144     int fd;               /* socket file descriptor */
145     int server_mode;      /* server or client */
146     int disconnects;      /* is it stable ? */
147     int conn_attempts;    /* connection attempts */
148     int fallback_only;    /* if set, this link will be used when all others are down */
149     uint32_t loss_tolerence; /* How much loss is acceptable before the link is discarded */
150     uint64_t seq;
151     uint64_t expected_receiver_seq;
152     uint64_t saved_timestamp;
153     uint64_t saved_timestamp_received_at;
154     uint64_t seq_last;
155     uint64_t seq_vect;
156     int rtt_hit;
157     double srtt;
158     double rttvar;
159     double weight;        /* For weight round robin */
160     uint32_t flow_id;
161     uint64_t sentpackets; /* 64bit packets sent counter */
162     uint64_t recvpackets; /* 64bit packets recv counter */
163     uint64_t sentbytes;   /* 64bit bytes sent counter */
164     uint64_t recvbytes;   /* 64bit bytes recv counter */
165     uint32_t timeout;     /* configured timeout in seconds */
166     uint32_t bandwidth;   /* bandwidth in bytes per second */
167     circular_buffer_t *sbuf;    /* send buffer */
168     circular_buffer_t *hpsbuf;  /* high priority buffer */
169     struct addrinfo *addrinfo;
170     enum chap_status status;    /* Auth status */
171     ev_tstamp last_activity;
172     ev_tstamp last_connection_attempt;
173     ev_tstamp next_keepalive;
174     ev_tstamp last_keepalive_ack;
175     ev_tstamp last_keepalive_ack_sent;
176     ev_io io_read;
177     ev_io io_write;
178     ev_timer io_timeout;
179 } mlvpn_tunnel_t;
180 
181 #ifdef HAVE_FILTERS
182 struct mlvpn_filters_s {
183     uint8_t count;
184     struct bpf_program filter[255];
185     mlvpn_tunnel_t *tun[255];
186 };
187 #endif
188 
189 int mlvpn_config(int config_file_fd, int first_time);
190 int mlvpn_sock_set_nonblocking(int fd);
191 
192 int mlvpn_loss_ratio(mlvpn_tunnel_t *tun);
193 int mlvpn_rtun_wrr_reset(struct rtunhead *head, int use_fallbacks);
194 mlvpn_tunnel_t *mlvpn_rtun_wrr_choose();
195 mlvpn_tunnel_t *mlvpn_rtun_choose();
196 mlvpn_tunnel_t *mlvpn_rtun_new(const char *name,
197     const char *bindaddr, const char *bindport, uint32_t bindfib,
198     const char *destaddr, const char *destport,
199     int server_mode, uint32_t timeout,
200     int fallback_only, uint32_t bandwidth,
201     uint32_t loss_tolerence);
202 void mlvpn_rtun_drop(mlvpn_tunnel_t *t);
203 void mlvpn_rtun_status_down(mlvpn_tunnel_t *t);
204 #ifdef HAVE_FILTERS
205 int mlvpn_filters_add(const struct bpf_program *filter, mlvpn_tunnel_t *tun);
206 mlvpn_tunnel_t *mlvpn_filters_choose(uint32_t pktlen, const u_char *pktdata);
207 #endif
208 
209 #include "privsep.h"
210 #include "log.h"
211 
212 #endif
213