… | |
… | |
26 | #include <openssl/rsa.h> |
26 | #include <openssl/rsa.h> |
27 | |
27 | |
28 | #include "conf.h" |
28 | #include "conf.h" |
29 | #include "iom.h" |
29 | #include "iom.h" |
30 | #include "util.h" |
30 | #include "util.h" |
|
|
31 | #include "sockinfo.h" |
31 | #include "device.h" |
32 | #include "device.h" |
32 | |
33 | |
33 | /* Protocol version. Different versions are incompatible, |
34 | /* Protocol version. Different major versions are incompatible, |
34 | incompatible version have different protocols. |
35 | * different minor versions probably are compatible ;) |
35 | */ |
36 | */ |
36 | |
37 | |
37 | #define PROTOCOL_MAJOR 2 |
38 | #define PROTOCOL_MAJOR 0 |
38 | #define PROTOCOL_MINOR 0 |
39 | #define PROTOCOL_MINOR 0 |
39 | |
40 | |
40 | struct vpn; |
41 | struct vpn; |
41 | struct vpn_packet; |
42 | struct vpn_packet; |
42 | |
43 | |
|
|
44 | struct rsaid { |
|
|
45 | u8 id[RSA_IDLEN]; // the challenge id |
|
|
46 | }; |
|
|
47 | |
43 | typedef u8 rsachallenge[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data |
48 | typedef u8 rsachallenge[RSA_KEYLEN - RSA_OVERHEAD]; // challenge data; |
44 | typedef u8 rsaencrdata[RSA_KEYLEN]; // encrypted challenge |
49 | typedef u8 rsaencrdata[RSA_KEYLEN]; // encrypted challenge |
|
|
50 | typedef u8 rsaresponse[RSA_RESLEN]; // the encrypted ripemd160 hash |
45 | |
51 | |
46 | struct crypto_ctx; |
52 | struct crypto_ctx; |
47 | |
53 | |
48 | // a very simple fifo pkt-queue |
54 | // a very simple fifo pkt-queue |
49 | class pkt_queue |
55 | class pkt_queue |
… | |
… | |
58 | |
64 | |
59 | pkt_queue (); |
65 | pkt_queue (); |
60 | ~pkt_queue (); |
66 | ~pkt_queue (); |
61 | }; |
67 | }; |
62 | |
68 | |
63 | enum auth_subtype { AUTH_INIT, AUTH_INITREPLY, AUTH_REPLY }; |
|
|
64 | |
|
|
65 | struct connection |
69 | struct connection |
66 | { |
70 | { |
67 | conf_node *conf; |
71 | conf_node *conf; |
68 | struct vpn *vpn; |
72 | struct vpn *vpn; |
69 | |
73 | |
70 | SOCKADDR sa; |
74 | sockinfo si; // the current(!) destination ip to send packets to |
71 | int retry_cnt; |
75 | int retry_cnt; |
72 | |
76 | |
73 | tstamp last_activity; // time of last packet received |
77 | tstamp last_activity; // time of last packet received |
74 | |
78 | |
75 | u32 oseqno; |
79 | u32 oseqno; |
76 | sliding_window iseqno; |
80 | sliding_window iseqno; |
77 | |
81 | |
|
|
82 | u8 prot_send, prot_recv; |
|
|
83 | |
78 | pkt_queue queue; |
84 | pkt_queue queue; |
79 | |
85 | |
80 | crypto_ctx *octx, *ictx; |
86 | crypto_ctx *octx, *ictx; |
81 | |
87 | |
82 | enum conf_node::connectmode connectmode; |
88 | enum conf_node::connectmode connectmode; |
|
|
89 | u8 prot_minor; // minor number of other side |
83 | |
90 | |
84 | void reset_dstaddr (); |
91 | void reset_dstaddr (); |
85 | |
92 | |
86 | void shutdown (); |
93 | void shutdown (); |
87 | void reset_connection (); |
94 | void reset_connection (); |
88 | void establish_connection_cb (tstamp &ts); time_watcher establish_connection; |
95 | void establish_connection_cb (tstamp &ts); time_watcher establish_connection; |
89 | void rekey_cb (tstamp &ts); time_watcher rekey; // next rekying (actually current reset + reestablishing) |
96 | void rekey_cb (tstamp &ts); time_watcher rekey; // next rekying (actually current reset + reestablishing) |
90 | void keepalive_cb (tstamp &ts); time_watcher keepalive; // next keepalive probe |
97 | void keepalive_cb (tstamp &ts); time_watcher keepalive; // next keepalive probe |
91 | |
98 | |
92 | void send_auth (auth_subtype subtype, SOCKADDR *sa, rsachallenge *k = 0); |
99 | void send_auth_request (const sockinfo &si, bool initiate); |
93 | void send_reset (SOCKADDR *dsa); |
100 | void send_auth_response (const sockinfo &si, const rsaid &id, const rsachallenge &chg); |
94 | void send_ping (SOCKADDR *dss, u8 pong = 0); |
101 | void send_reset (const sockinfo &dsi); |
|
|
102 | void send_ping (const sockinfo &dsi, u8 pong = 0); |
95 | void send_data_packet (tap_packet *pkt, bool broadcast = false); |
103 | void send_data_packet (tap_packet *pkt, bool broadcast = false); |
96 | void inject_data_packet (tap_packet *pkt, bool broadcast = false); |
104 | void inject_data_packet (tap_packet *pkt, bool broadcast = false); |
97 | void connect_request (int id); |
105 | void connect_request (int id); |
98 | |
106 | |
|
|
107 | void send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos = IPTOS_RELIABILITY); |
99 | void recv_vpn_packet (vpn_packet *pkt, SOCKADDR *rsa); |
108 | void recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi); |
100 | |
109 | |
101 | void script_node (); |
110 | void script_node (); |
102 | const char *script_node_up (); |
111 | const char *script_node_up (int); |
103 | const char *script_node_down (); |
112 | const char *script_node_down (int); |
|
|
113 | |
|
|
114 | void dump_status (); |
104 | |
115 | |
105 | connection(struct vpn *vpn_); |
116 | connection(struct vpn *vpn_); |
106 | ~connection (); |
117 | ~connection (); |
107 | }; |
118 | }; |
108 | |
119 | |
109 | struct vpn |
120 | struct vpn |
110 | { |
121 | { |
111 | int socket_fd; |
122 | int udpv4_fd; |
|
|
123 | int ipv4_fd; |
|
|
124 | |
112 | int events; |
125 | int events; |
113 | |
126 | |
114 | enum { |
127 | enum { |
115 | EVENT_RECONNECT = 1, |
128 | EVENT_RECONNECT = 1, |
116 | EVENT_SHUTDOWN = 2, |
129 | EVENT_SHUTDOWN = 2, |
… | |
… | |
123 | typedef vector<connection *> conns_vector; |
136 | typedef vector<connection *> conns_vector; |
124 | conns_vector conns; |
137 | conns_vector conns; |
125 | |
138 | |
126 | connection *find_router (); |
139 | connection *find_router (); |
127 | |
140 | |
128 | void send_vpn_packet (vpn_packet *pkt, SOCKADDR *sa, int tos = IPTOS_RELIABILITY); |
|
|
129 | void reconnect_all (); |
141 | void reconnect_all (); |
130 | void shutdown_all (); |
142 | void shutdown_all (); |
131 | void connect_request (int id); |
143 | void connect_request (int id); |
132 | |
144 | |
133 | void vpn_ev (short revents); io_watcher vpn_ev_watcher; |
145 | void tap_ev (short revents); io_watcher tap_ev_watcher; |
|
|
146 | void ipv4_ev (short revents); io_watcher ipv4_ev_watcher; |
134 | void udp_ev (short revents); io_watcher udp_ev_watcher; |
147 | void udpv4_ev (short revents); io_watcher udpv4_ev_watcher; |
|
|
148 | |
|
|
149 | void recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi); |
|
|
150 | |
|
|
151 | void send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos = IPTOS_RELIABILITY); |
|
|
152 | void send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos = IPTOS_RELIABILITY); |
135 | |
153 | |
136 | vpn (); |
154 | vpn (); |
137 | ~vpn (); |
155 | ~vpn (); |
138 | |
156 | |
139 | int setup (); |
157 | int setup (); |
140 | |
158 | |
|
|
159 | void dump_status (); |
|
|
160 | |
141 | const char *script_if_up (); |
161 | const char *script_if_up (int); |
142 | }; |
162 | }; |
143 | |
163 | |
144 | #endif |
164 | #endif |
145 | |
165 | |