… | |
… | |
24 | #include <cstring> |
24 | #include <cstring> |
25 | #include <cstdio> |
25 | #include <cstdio> |
26 | |
26 | |
27 | #include <sys/types.h> |
27 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/poll.h> |
|
|
30 | #include <sys/wait.h> |
29 | #include <sys/wait.h> |
31 | #include <netinet/in.h> |
|
|
32 | #include <arpa/inet.h> |
|
|
33 | #include <errno.h> |
30 | #include <errno.h> |
34 | #include <time.h> |
31 | #include <time.h> |
35 | #include <unistd.h> |
32 | #include <unistd.h> |
|
|
33 | #include <fcntl.h> |
|
|
34 | #include <sys/socket.h> |
|
|
35 | |
|
|
36 | #include "netcompat.h" |
36 | |
37 | |
37 | #include "pidfile.h" |
38 | #include "pidfile.h" |
38 | |
39 | |
39 | #include "connection.h" |
40 | #include "connection.h" |
40 | #include "util.h" |
41 | #include "util.h" |
41 | #include "vpn.h" |
42 | #include "vpn.h" |
42 | |
43 | |
43 | ///////////////////////////////////////////////////////////////////////////// |
44 | ///////////////////////////////////////////////////////////////////////////// |
44 | |
45 | |
45 | const char *vpn::script_if_up (int) |
46 | const char *vpn::script_if_up () |
46 | { |
47 | { |
47 | // the tunnel device mtu should be the physical mtu - overhead |
48 | // the tunnel device mtu should be the physical mtu - overhead |
48 | // the tricky part is rounding to the cipher key blocksize |
49 | // the tricky part is rounding to the cipher key blocksize |
49 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
50 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
50 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
51 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
… | |
… | |
71 | } |
72 | } |
72 | |
73 | |
73 | int |
74 | int |
74 | vpn::setup () |
75 | vpn::setup () |
75 | { |
76 | { |
76 | sockinfo si; |
77 | ipv4_fd = -1; |
77 | |
78 | |
78 | si.set (THISNODE); |
79 | if (THISNODE->protocols & PROT_IPv4 && ::conf.ip_proto) |
|
|
80 | { |
|
|
81 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
|
|
82 | |
|
|
83 | if (ipv4_fd < 0) |
|
|
84 | return -1; |
|
|
85 | |
|
|
86 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
|
|
87 | |
|
|
88 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
|
|
89 | // this I really consider a linux bug. I am neither connected |
|
|
90 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
91 | // fragment for me sometimes. |
|
|
92 | { |
|
|
93 | int oval = IP_PMTUDISC_DONT; |
|
|
94 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
95 | } |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | sockinfo si (THISNODE, PROT_IPv4); |
|
|
99 | |
|
|
100 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
101 | { |
|
|
102 | slog (L_ERR, _("can't bind ipv4 socket on %s: %s"), (const char *)si, strerror (errno)); |
|
|
103 | exit (1); |
|
|
104 | } |
|
|
105 | |
|
|
106 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
|
|
107 | } |
79 | |
108 | |
80 | udpv4_fd = -1; |
109 | udpv4_fd = -1; |
81 | |
110 | |
82 | if (THISNODE->protocols & PROT_UDPv4) |
111 | if (THISNODE->protocols & PROT_UDPv4 && THISNODE->udp_port) |
83 | { |
112 | { |
84 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
113 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
85 | |
114 | |
86 | if (udpv4_fd < 0) |
115 | if (udpv4_fd < 0) |
87 | return -1; |
116 | return -1; |
88 | |
117 | |
89 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
118 | fcntl (udpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
119 | |
|
|
120 | // standard daemon practise... |
90 | { |
121 | { |
91 | slog (L_ERR, _("can't bind udpv4 to %s: %s"), (const char *)si, strerror (errno)); |
122 | int oval = 1; |
92 | exit (1); |
123 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
93 | } |
124 | } |
94 | |
125 | |
95 | #ifdef IP_MTU_DISCOVER |
126 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
96 | // this I really consider a linux bug. I am neither connected |
127 | // this I really consider a linux bug. I am neither connected |
97 | // nor do I fragment myself. Linux still sets DF and doesn't |
128 | // nor do I fragment myself. Linux still sets DF and doesn't |
98 | // fragment for me sometimes. |
129 | // fragment for me sometimes. |
99 | { |
130 | { |
100 | int oval = IP_PMTUDISC_DONT; |
131 | int oval = IP_PMTUDISC_DONT; |
101 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
132 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
102 | } |
133 | } |
103 | #endif |
134 | #endif |
104 | |
135 | |
105 | // standard daemon practise... |
136 | sockinfo si (THISNODE, PROT_UDPv4); |
|
|
137 | |
|
|
138 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
139 | { |
|
|
140 | slog (L_ERR, _("can't bind udpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
141 | exit (1); |
|
|
142 | } |
|
|
143 | |
|
|
144 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
|
|
145 | } |
|
|
146 | |
|
|
147 | icmpv4_fd = -1; |
|
|
148 | |
|
|
149 | #if ENABLE_ICMP |
|
|
150 | if (THISNODE->protocols & PROT_ICMPv4) |
|
|
151 | { |
|
|
152 | icmpv4_fd = socket (PF_INET, SOCK_RAW, IPPROTO_ICMP); |
|
|
153 | |
|
|
154 | if (icmpv4_fd < 0) |
|
|
155 | return -1; |
|
|
156 | |
|
|
157 | fcntl (icmpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
158 | |
|
|
159 | #ifdef ICMP_FILTER |
106 | { |
160 | { |
107 | int oval = 1; |
161 | icmp_filter oval; |
|
|
162 | oval.data = 0xffffffff; |
|
|
163 | if (::conf.icmp_type < 32) |
|
|
164 | oval.data &= ~(1 << ::conf.icmp_type); |
|
|
165 | |
108 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
166 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
109 | } |
167 | } |
|
|
168 | #endif |
110 | |
169 | |
111 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
170 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
112 | } |
|
|
113 | |
|
|
114 | ipv4_fd = -1; |
|
|
115 | if (THISNODE->protocols & PROT_IPv4) |
|
|
116 | { |
|
|
117 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
|
|
118 | |
|
|
119 | if (ipv4_fd < 0) |
|
|
120 | return -1; |
|
|
121 | |
|
|
122 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
123 | { |
|
|
124 | slog (L_ERR, _("can't bind ipv4 socket to %s: %s"), (const char *)si, strerror (errno)); |
|
|
125 | exit (1); |
|
|
126 | } |
|
|
127 | |
|
|
128 | #ifdef IP_MTU_DISCOVER |
|
|
129 | // this I really consider a linux bug. I am neither connected |
171 | // this I really consider a linux bug. I am neither connected |
130 | // nor do I fragment myself. Linux still sets DF and doesn't |
172 | // nor do I fragment myself. Linux still sets DF and doesn't |
131 | // fragment for me sometimes. |
173 | // fragment for me sometimes. |
132 | { |
174 | { |
133 | int oval = IP_PMTUDISC_DONT; |
175 | int oval = IP_PMTUDISC_DONT; |
134 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
176 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
135 | } |
177 | } |
136 | #endif |
178 | #endif |
137 | |
179 | |
|
|
180 | sockinfo si (THISNODE, PROT_ICMPv4); |
|
|
181 | |
|
|
182 | if (bind (icmpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
183 | { |
|
|
184 | slog (L_ERR, _("can't bind icmpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
185 | exit (1); |
|
|
186 | } |
|
|
187 | |
138 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
188 | icmpv4_ev_watcher.start (icmpv4_fd, POLLIN); |
|
|
189 | } |
|
|
190 | #endif |
|
|
191 | |
|
|
192 | tcpv4_fd = -1; |
|
|
193 | |
|
|
194 | #if ENABLE_TCP |
|
|
195 | if (THISNODE->protocols & PROT_TCPv4 && THISNODE->tcp_port) |
|
|
196 | { |
|
|
197 | tcpv4_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
|
198 | |
|
|
199 | if (tcpv4_fd < 0) |
|
|
200 | return -1; |
|
|
201 | |
|
|
202 | fcntl (tcpv4_fd, F_SETFL, O_NONBLOCK); |
|
|
203 | |
|
|
204 | // standard daemon practise... |
|
|
205 | { |
|
|
206 | int oval = 1; |
|
|
207 | setsockopt (tcpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
139 | } |
208 | } |
|
|
209 | |
|
|
210 | sockinfo si (THISNODE, PROT_TCPv4); |
|
|
211 | |
|
|
212 | if (bind (tcpv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
213 | { |
|
|
214 | slog (L_ERR, _("can't bind tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
215 | exit (1); |
|
|
216 | } |
|
|
217 | |
|
|
218 | if (listen (tcpv4_fd, 5)) |
|
|
219 | { |
|
|
220 | slog (L_ERR, _("can't listen tcpv4 on %s: %s"), (const char *)si, strerror (errno)); |
|
|
221 | exit (1); |
|
|
222 | } |
|
|
223 | |
|
|
224 | tcpv4_ev_watcher.start (tcpv4_fd, POLLIN); |
|
|
225 | } |
|
|
226 | #endif |
140 | |
227 | |
141 | tap = new tap_device (); |
228 | tap = new tap_device (); |
142 | if (!tap) //D this, of course, never catches |
229 | if (!tap) //D this, of course, never catches |
143 | { |
230 | { |
144 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
231 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
… | |
… | |
152 | reconnect_all (); |
239 | reconnect_all (); |
153 | |
240 | |
154 | return 0; |
241 | return 0; |
155 | } |
242 | } |
156 | |
243 | |
157 | void |
244 | // send a vpn packet out to other hosts |
|
|
245 | bool |
|
|
246 | vpn::send_vpn_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
247 | { |
|
|
248 | switch (si.prot) |
|
|
249 | { |
|
|
250 | case PROT_IPv4: |
|
|
251 | return send_ipv4_packet (pkt, si, tos); |
|
|
252 | |
|
|
253 | case PROT_UDPv4: |
|
|
254 | return send_udpv4_packet (pkt, si, tos); |
|
|
255 | |
|
|
256 | #if ENABLE_TCP |
|
|
257 | case PROT_TCPv4: |
|
|
258 | return send_tcpv4_packet (pkt, si, tos); |
|
|
259 | #endif |
|
|
260 | |
|
|
261 | #if ENABLE_ICMP |
|
|
262 | case PROT_ICMPv4: |
|
|
263 | return send_icmpv4_packet (pkt, si, tos); |
|
|
264 | #endif |
|
|
265 | |
|
|
266 | default: |
|
|
267 | slog (L_CRIT, _("%s: FATAL: trying to send packet with unsupported protocol"), (const char *)si); |
|
|
268 | return false; |
|
|
269 | } |
|
|
270 | } |
|
|
271 | |
|
|
272 | bool |
158 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
273 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
159 | { |
274 | { |
|
|
275 | #if defined(SOL_IP) && defined(IP_TOS) |
160 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
277 | #endif |
161 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
278 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
162 | } |
|
|
163 | |
279 | |
164 | void |
280 | return true; |
|
|
281 | } |
|
|
282 | |
|
|
283 | static u16 |
|
|
284 | ipv4_checksum (u16 *data, unsigned int len) |
|
|
285 | { |
|
|
286 | // use 32 bit accumulator and fold back carry bits at the end |
|
|
287 | u32 sum = 0; |
|
|
288 | |
|
|
289 | while (len > 1) |
|
|
290 | { |
|
|
291 | sum += *data++; |
|
|
292 | len -= 2; |
|
|
293 | } |
|
|
294 | |
|
|
295 | // odd byte left? |
|
|
296 | if (len) |
|
|
297 | sum += *(u8 *)data; |
|
|
298 | |
|
|
299 | // add back carry bits |
|
|
300 | sum = (sum >> 16) + (sum & 0xffff); // lo += hi |
|
|
301 | sum += (sum >> 16); // carry |
|
|
302 | |
|
|
303 | return ~sum; |
|
|
304 | } |
|
|
305 | |
|
|
306 | #if ENABLE_ICMP |
|
|
307 | bool |
|
|
308 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
|
|
309 | { |
|
|
310 | #if defined(SOL_IP) && defined(IP_TOS) |
|
|
311 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
312 | #endif |
|
|
313 | |
|
|
314 | pkt->unshift_hdr (4); |
|
|
315 | |
|
|
316 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
|
|
317 | hdr->type = ::conf.icmp_type; |
|
|
318 | hdr->code = 255; |
|
|
319 | hdr->checksum = 0; |
|
|
320 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
|
|
321 | |
|
|
322 | sendto (icmpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
323 | |
|
|
324 | return true; |
|
|
325 | } |
|
|
326 | #endif |
|
|
327 | |
|
|
328 | bool |
165 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
329 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
166 | { |
330 | { |
|
|
331 | #if defined(SOL_IP) && defined(IP_TOS) |
167 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
332 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
333 | #endif |
168 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
334 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
|
|
335 | |
|
|
336 | return true; |
169 | } |
337 | } |
170 | |
338 | |
171 | void |
339 | void |
172 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
340 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
173 | { |
341 | { |
… | |
… | |
180 | if (src == 0 || src > conns.size () |
348 | if (src == 0 || src > conns.size () |
181 | || dst > conns.size () |
349 | || dst > conns.size () |
182 | || pkt->typ () >= vpn_packet::PT_MAX) |
350 | || pkt->typ () >= vpn_packet::PT_MAX) |
183 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
351 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
184 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
352 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
353 | else if (dst > conns.size ()) |
|
|
354 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
|
|
355 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
185 | else |
356 | else |
186 | { |
357 | { |
187 | connection *c = conns[src - 1]; |
358 | connection *c = conns[src - 1]; |
188 | |
359 | |
189 | if (dst == 0 && !THISNODE->routerprio) |
360 | if (dst == 0) |
190 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
361 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
191 | c->conf->nodename, (const char *)rsi); |
362 | c->conf->nodename, (const char *)rsi); |
192 | else if (dst != 0 && dst != THISNODE->id) |
363 | else if (dst != 0 && dst != THISNODE->id) |
193 | // FORWARDING NEEDED ;) |
364 | { |
|
|
365 | if (THISNODE->routerprio) |
|
|
366 | // the tos setting gets lost here. who cares. |
|
|
367 | conns[dst - 1]->inject_vpn_packet (pkt); |
|
|
368 | else |
194 | slog (L_WARN, |
369 | slog (L_WARN, |
195 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
370 | _("%s(%s): forwarding request (=> %s), but we are no router"), |
|
|
371 | c->conf->nodename, (const char *)rsi, |
196 | dst, conns[dst - 1]->conf->nodename, |
372 | conns[dst - 1]->conf->nodename); |
197 | (const char *)rsi, |
373 | } |
198 | THISNODE->id, THISNODE->nodename); |
|
|
199 | else |
374 | else |
200 | c->recv_vpn_packet (pkt, rsi); |
375 | c->recv_vpn_packet (pkt, rsi); |
201 | } |
376 | } |
202 | } |
377 | } |
203 | |
378 | |
204 | void |
379 | void |
205 | vpn::udpv4_ev (short revents) |
380 | vpn::ipv4_ev (io_watcher &w, short revents) |
206 | { |
381 | { |
207 | if (revents & (POLLIN | POLLERR)) |
382 | if (revents & (POLLIN | POLLERR)) |
208 | { |
383 | { |
209 | vpn_packet *pkt = new vpn_packet; |
384 | vpn_packet *pkt = new vpn_packet; |
210 | struct sockaddr_in sa; |
385 | struct sockaddr_in sa; |
211 | socklen_t sa_len = sizeof (sa); |
386 | socklen_t sa_len = sizeof (sa); |
212 | int len; |
387 | int len; |
213 | |
388 | |
214 | len = recvfrom (udpv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
389 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
215 | |
390 | |
216 | sockinfo si(sa); |
391 | sockinfo si(sa, PROT_IPv4); |
217 | |
392 | |
218 | if (len > 0) |
393 | if (len > 0) |
219 | { |
394 | { |
220 | pkt->len = len; |
395 | pkt->len = len; |
|
|
396 | |
|
|
397 | // raw sockets deliver the ipv4, but don't expect it on sends |
|
|
398 | // this is slow, but... |
|
|
399 | pkt->skip_hdr (IP_OVERHEAD); |
221 | |
400 | |
222 | recv_vpn_packet (pkt, si); |
401 | recv_vpn_packet (pkt, si); |
223 | } |
402 | } |
224 | else |
403 | else |
225 | { |
404 | { |
… | |
… | |
230 | delete pkt; |
409 | delete pkt; |
231 | } |
410 | } |
232 | else if (revents & POLLHUP) |
411 | else if (revents & POLLHUP) |
233 | { |
412 | { |
234 | // this cannot ;) happen on udp sockets |
413 | // this cannot ;) happen on udp sockets |
235 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
414 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
236 | exit (1); |
415 | exit (1); |
237 | } |
416 | } |
238 | else |
417 | else |
239 | { |
418 | { |
240 | slog (L_ERR, |
419 | slog (L_ERR, |
… | |
… | |
242 | revents); |
421 | revents); |
243 | exit (1); |
422 | exit (1); |
244 | } |
423 | } |
245 | } |
424 | } |
246 | |
425 | |
|
|
426 | #if ENABLE_ICMP |
247 | void |
427 | void |
248 | vpn::ipv4_ev (short revents) |
428 | vpn::icmpv4_ev (io_watcher &w, short revents) |
249 | { |
429 | { |
250 | if (revents & (POLLIN | POLLERR)) |
430 | if (revents & (POLLIN | POLLERR)) |
251 | { |
431 | { |
252 | vpn_packet *pkt = new vpn_packet; |
432 | vpn_packet *pkt = new vpn_packet; |
253 | struct sockaddr_in sa; |
433 | struct sockaddr_in sa; |
254 | socklen_t sa_len = sizeof (sa); |
434 | socklen_t sa_len = sizeof (sa); |
255 | int len; |
435 | int len; |
256 | |
436 | |
257 | len = recvfrom (ipv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
437 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
258 | |
438 | |
259 | sockinfo si(sa, PROT_IPv4); |
439 | sockinfo si(sa, PROT_ICMPv4); |
260 | |
440 | |
261 | if (len > 0) |
441 | if (len > 0) |
262 | { |
442 | { |
263 | pkt->len = len; |
443 | pkt->len = len; |
264 | |
444 | |
|
|
445 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
|
|
446 | |
|
|
447 | if (hdr->type == ::conf.icmp_type |
|
|
448 | && hdr->code == 255) |
|
|
449 | { |
265 | // raw sockets deliver the ipv4, but don't expect it on sends |
450 | // raw sockets deliver the ipv4, but don't expect it on sends |
266 | // this is slow, but... |
451 | // this is slow, but... |
267 | pkt->skip_hdr (IP_OVERHEAD); |
452 | pkt->skip_hdr (ICMP_OVERHEAD); |
268 | |
453 | |
269 | recv_vpn_packet (pkt, si); |
454 | recv_vpn_packet (pkt, si); |
|
|
455 | } |
270 | } |
456 | } |
271 | else |
457 | else |
272 | { |
458 | { |
273 | // probably ECONNRESET or somesuch |
459 | // probably ECONNRESET or somesuch |
274 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
460 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
… | |
… | |
277 | delete pkt; |
463 | delete pkt; |
278 | } |
464 | } |
279 | else if (revents & POLLHUP) |
465 | else if (revents & POLLHUP) |
280 | { |
466 | { |
281 | // this cannot ;) happen on udp sockets |
467 | // this cannot ;) happen on udp sockets |
282 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
468 | slog (L_ERR, _("FATAL: POLLHUP on icmpv4 fd, terminating.")); |
283 | exit (1); |
469 | exit (1); |
284 | } |
470 | } |
285 | else |
471 | else |
286 | { |
472 | { |
287 | slog (L_ERR, |
473 | slog (L_ERR, |
288 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
474 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
289 | revents); |
475 | revents); |
290 | exit (1); |
476 | exit (1); |
291 | } |
477 | } |
292 | } |
478 | } |
|
|
479 | #endif |
293 | |
480 | |
294 | void |
481 | void |
|
|
482 | vpn::udpv4_ev (io_watcher &w, short revents) |
|
|
483 | { |
|
|
484 | if (revents & (POLLIN | POLLERR)) |
|
|
485 | { |
|
|
486 | vpn_packet *pkt = new vpn_packet; |
|
|
487 | struct sockaddr_in sa; |
|
|
488 | socklen_t sa_len = sizeof (sa); |
|
|
489 | int len; |
|
|
490 | |
|
|
491 | len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
492 | |
|
|
493 | sockinfo si(sa, PROT_UDPv4); |
|
|
494 | |
|
|
495 | if (len > 0) |
|
|
496 | { |
|
|
497 | pkt->len = len; |
|
|
498 | |
|
|
499 | recv_vpn_packet (pkt, si); |
|
|
500 | } |
|
|
501 | else |
|
|
502 | { |
|
|
503 | // probably ECONNRESET or somesuch |
|
|
504 | slog (L_DEBUG, _("%s: fd %d, %s"), (const char *)si, w.fd, strerror (errno)); |
|
|
505 | } |
|
|
506 | |
|
|
507 | delete pkt; |
|
|
508 | } |
|
|
509 | else if (revents & POLLHUP) |
|
|
510 | { |
|
|
511 | // this cannot ;) happen on udp sockets |
|
|
512 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
|
|
513 | exit (1); |
|
|
514 | } |
|
|
515 | else |
|
|
516 | { |
|
|
517 | slog (L_ERR, |
|
|
518 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
519 | revents); |
|
|
520 | exit (1); |
|
|
521 | } |
|
|
522 | } |
|
|
523 | |
|
|
524 | void |
295 | vpn::tap_ev (short revents) |
525 | vpn::tap_ev (io_watcher &w, short revents) |
296 | { |
526 | { |
297 | if (revents & POLLIN) |
527 | if (revents & POLLIN) |
298 | { |
528 | { |
299 | /* process data */ |
529 | /* process data */ |
300 | tap_packet *pkt; |
530 | tap_packet *pkt; |
301 | |
531 | |
302 | pkt = tap->recv (); |
532 | pkt = tap->recv (); |
303 | |
533 | |
|
|
534 | if (!pkt) |
|
|
535 | return; |
|
|
536 | |
|
|
537 | if (pkt->len > 14) |
|
|
538 | { |
304 | int dst = mac2id (pkt->dst); |
539 | int dst = mac2id (pkt->dst); |
305 | int src = mac2id (pkt->src); |
540 | int src = mac2id (pkt->src); |
306 | |
541 | |
307 | if (src != THISNODE->id) |
542 | if (src != THISNODE->id) |
308 | { |
|
|
309 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
310 | exit (1); |
|
|
311 | } |
|
|
312 | |
|
|
313 | if (dst == THISNODE->id) |
|
|
314 | { |
|
|
315 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
316 | exit (1); |
|
|
317 | } |
|
|
318 | |
|
|
319 | if (dst > conns.size ()) |
|
|
320 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
321 | else |
|
|
322 | { |
|
|
323 | if (dst) |
|
|
324 | { |
543 | { |
325 | // unicast |
544 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
326 | if (dst != THISNODE->id) |
545 | exit (1); |
327 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
328 | } |
546 | } |
|
|
547 | |
|
|
548 | if (dst == THISNODE->id) |
|
|
549 | { |
|
|
550 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
551 | exit (1); |
|
|
552 | } |
|
|
553 | |
|
|
554 | if (dst > conns.size ()) |
|
|
555 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
329 | else |
556 | else |
330 | { |
557 | { |
331 | // broadcast, first check router, then self, then english |
|
|
332 | connection *router = find_router (); |
|
|
333 | |
|
|
334 | if (router) |
558 | if (dst) |
|
|
559 | { |
|
|
560 | // unicast |
|
|
561 | if (dst != THISNODE->id) |
335 | router->inject_data_packet (pkt, true); |
562 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
563 | } |
336 | else |
564 | else |
|
|
565 | { |
|
|
566 | // broadcast, this is ugly, but due to the security policy |
|
|
567 | // we have to connect to all hosts... |
337 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
568 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
338 | if ((*c)->conf != THISNODE) |
569 | if ((*c)->conf != THISNODE) |
339 | (*c)->inject_data_packet (pkt); |
570 | (*c)->inject_data_packet (pkt); |
|
|
571 | } |
340 | } |
572 | } |
341 | } |
573 | } |
342 | |
574 | |
343 | delete pkt; |
575 | delete pkt; |
344 | } |
576 | } |
… | |
… | |
350 | else |
582 | else |
351 | abort (); |
583 | abort (); |
352 | } |
584 | } |
353 | |
585 | |
354 | void |
586 | void |
355 | vpn::event_cb (tstamp &ts) |
587 | vpn::event_cb (time_watcher &w) |
356 | { |
588 | { |
357 | if (events) |
589 | if (events) |
358 | { |
590 | { |
359 | if (events & EVENT_SHUTDOWN) |
591 | if (events & EVENT_SHUTDOWN) |
360 | { |
592 | { |
… | |
… | |
377 | } |
609 | } |
378 | |
610 | |
379 | events = 0; |
611 | events = 0; |
380 | } |
612 | } |
381 | |
613 | |
382 | ts = TSTAMP_CANCEL; |
614 | w.at = TSTAMP_CANCEL; |
383 | } |
615 | } |
384 | |
616 | |
385 | void |
617 | void |
386 | vpn::shutdown_all () |
618 | vpn::shutdown_all () |
387 | { |
619 | { |
… | |
… | |
411 | } |
643 | } |
412 | } |
644 | } |
413 | |
645 | |
414 | connection *vpn::find_router () |
646 | connection *vpn::find_router () |
415 | { |
647 | { |
416 | u32 prio = 0; |
648 | u32 prio = 1; |
417 | connection *router = 0; |
649 | connection *router = 0; |
418 | |
650 | |
419 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
651 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
420 | { |
652 | { |
421 | connection *c = *i; |
653 | connection *c = *i; |
422 | |
654 | |
423 | if (c->conf->routerprio > prio |
655 | if (c->conf->routerprio > prio |
424 | && c->connectmode == conf_node::C_ALWAYS |
656 | && c->connectmode == conf_node::C_ALWAYS // so we don't drop the connection if in use |
425 | && c->conf != THISNODE |
|
|
426 | && c->ictx && c->octx) |
657 | && c->ictx && c->octx |
|
|
658 | && c->conf != THISNODE) // redundant, since ictx==octx==0 always on thisnode |
427 | { |
659 | { |
428 | prio = c->conf->routerprio; |
660 | prio = c->conf->routerprio; |
429 | router = c; |
661 | router = c; |
430 | } |
662 | } |
431 | } |
663 | } |
432 | |
664 | |
433 | return router; |
665 | return router; |
434 | } |
666 | } |
435 | |
667 | |
436 | void vpn::connect_request (int id) |
668 | void vpn::send_connect_request (int id) |
437 | { |
669 | { |
438 | connection *c = find_router (); |
670 | connection *c = find_router (); |
439 | |
671 | |
440 | if (c) |
672 | if (c) |
441 | c->connect_request (id); |
673 | c->send_connect_request (id); |
442 | //else // does not work, because all others must connect to the same router |
674 | else |
443 | // // no router found, aggressively connect to all routers |
675 | // no router found, aggressively connect to all routers |
444 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
676 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
445 | // if ((*i)->conf->routerprio) |
677 | if ((*i)->conf->routerprio && (*i)->conf != THISNODE) |
446 | // (*i)->establish_connection (); |
678 | (*i)->establish_connection (); |
447 | } |
679 | } |
448 | |
680 | |
449 | void |
681 | void |
450 | connection::dump_status () |
682 | connection::dump_status () |
451 | { |
683 | { |
… | |
… | |
468 | |
700 | |
469 | slog (L_NOTICE, _("END status dump")); |
701 | slog (L_NOTICE, _("END status dump")); |
470 | } |
702 | } |
471 | |
703 | |
472 | vpn::vpn (void) |
704 | vpn::vpn (void) |
|
|
705 | : event (this, &vpn::event_cb) |
473 | : udpv4_ev_watcher(this, &vpn::udpv4_ev) |
706 | , udpv4_ev_watcher (this, &vpn::udpv4_ev) |
474 | , ipv4_ev_watcher(this, &vpn::ipv4_ev) |
707 | , ipv4_ev_watcher (this, &vpn::ipv4_ev) |
|
|
708 | #if ENABLE_TCP |
|
|
709 | , tcpv4_ev_watcher (this, &vpn::tcpv4_ev) |
|
|
710 | #endif |
|
|
711 | #if ENABLE_ICMP |
|
|
712 | , icmpv4_ev_watcher(this, &vpn::icmpv4_ev) |
|
|
713 | #endif |
475 | , tap_ev_watcher(this, &vpn::tap_ev) |
714 | , tap_ev_watcher (this, &vpn::tap_ev) |
476 | , event(this, &vpn::event_cb) |
|
|
477 | { |
715 | { |
478 | } |
716 | } |
479 | |
717 | |
480 | vpn::~vpn () |
718 | vpn::~vpn () |
481 | { |
719 | { |