1 | /* |
1 | /* |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
2 | vpn.C -- handle the protocol, encryption, handshaking etc. |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
24 | #include <cstring> |
25 | #include <cstring> |
25 | #include <cstdio> |
26 | #include <cstdio> |
26 | |
27 | |
27 | #include <sys/types.h> |
28 | #include <sys/types.h> |
28 | #include <sys/socket.h> |
29 | #include <sys/socket.h> |
29 | #include <sys/poll.h> |
|
|
30 | #include <sys/wait.h> |
30 | #include <sys/wait.h> |
31 | #include <errno.h> |
31 | #include <errno.h> |
32 | #include <time.h> |
32 | #include <time.h> |
33 | #include <unistd.h> |
33 | #include <unistd.h> |
34 | #include <fcntl.h> |
34 | #include <fcntl.h> |
35 | #include <arpa/inet.h> |
35 | #include <sys/socket.h> |
36 | #include <netinet/in.h> |
36 | |
37 | #include <netinet/ip_icmp.h> |
37 | #include "netcompat.h" |
38 | |
38 | |
39 | #include "pidfile.h" |
39 | #include "pidfile.h" |
40 | |
40 | |
41 | #include "connection.h" |
41 | #include "connection.h" |
42 | #include "util.h" |
42 | #include "util.h" |
… | |
… | |
84 | if (ipv4_fd < 0) |
84 | if (ipv4_fd < 0) |
85 | return -1; |
85 | return -1; |
86 | |
86 | |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
87 | fcntl (ipv4_fd, F_SETFL, O_NONBLOCK); |
88 | |
88 | |
89 | #ifdef IP_MTU_DISCOVER |
89 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
90 | // this I really consider a linux bug. I am neither connected |
90 | // this I really consider a linux bug. I am neither connected |
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
91 | // nor do I fragment myself. Linux still sets DF and doesn't |
92 | // fragment for me sometimes. |
92 | // fragment for me sometimes. |
93 | { |
93 | { |
94 | int oval = IP_PMTUDISC_DONT; |
94 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
122 | { |
122 | { |
123 | int oval = 1; |
123 | int oval = 1; |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
124 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
125 | } |
125 | } |
126 | |
126 | |
127 | #ifdef IP_MTU_DISCOVER |
127 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
128 | // this I really consider a linux bug. I am neither connected |
128 | // this I really consider a linux bug. I am neither connected |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
129 | // nor do I fragment myself. Linux still sets DF and doesn't |
130 | // fragment for me sometimes. |
130 | // fragment for me sometimes. |
131 | { |
131 | { |
132 | int oval = IP_PMTUDISC_DONT; |
132 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
166 | |
166 | |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
167 | setsockopt (icmpv4_fd, SOL_RAW, ICMP_FILTER, &oval, sizeof oval); |
168 | } |
168 | } |
169 | #endif |
169 | #endif |
170 | |
170 | |
171 | #ifdef IP_MTU_DISCOVER |
171 | #if defined(SOL_IP) && defined(IP_MTU_DISCOVER) |
172 | // this I really consider a linux bug. I am neither connected |
172 | // this I really consider a linux bug. I am neither connected |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
173 | // nor do I fragment myself. Linux still sets DF and doesn't |
174 | // fragment for me sometimes. |
174 | // fragment for me sometimes. |
175 | { |
175 | { |
176 | int oval = IP_PMTUDISC_DONT; |
176 | int oval = IP_PMTUDISC_DONT; |
… | |
… | |
271 | } |
271 | } |
272 | |
272 | |
273 | bool |
273 | bool |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
274 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
275 | { |
275 | { |
|
|
276 | #if defined(SOL_IP) && defined(IP_TOS) |
276 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
277 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
278 | #endif |
277 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
279 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
278 | |
280 | |
279 | return true; |
281 | return true; |
280 | } |
282 | } |
281 | |
283 | |
… | |
… | |
304 | |
306 | |
305 | #if ENABLE_ICMP |
307 | #if ENABLE_ICMP |
306 | bool |
308 | bool |
307 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
309 | vpn::send_icmpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
308 | { |
310 | { |
|
|
311 | #if defined(SOL_IP) && defined(IP_TOS) |
309 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
312 | setsockopt (icmpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
313 | #endif |
310 | |
314 | |
311 | pkt->unshift_hdr (4); |
315 | pkt->unshift_hdr (4); |
312 | |
316 | |
313 | icmphdr *hdr = (icmphdr *)&((*pkt)[0]); |
317 | icmp_header *hdr = (icmp_header *)&((*pkt)[0]); |
314 | hdr->type = ::conf.icmp_type; |
318 | hdr->type = ::conf.icmp_type; |
315 | hdr->code = 255; |
319 | hdr->code = 255; |
316 | hdr->checksum = 0; |
320 | hdr->checksum = 0; |
317 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
321 | hdr->checksum = ipv4_checksum ((u16 *)hdr, pkt->len); |
318 | |
322 | |
… | |
… | |
323 | #endif |
327 | #endif |
324 | |
328 | |
325 | bool |
329 | bool |
326 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
330 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
327 | { |
331 | { |
|
|
332 | #if defined(SOL_IP) && defined(IP_TOS) |
328 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
333 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
|
|
334 | #endif |
329 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
335 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
330 | |
336 | |
331 | return true; |
337 | return true; |
|
|
338 | } |
|
|
339 | |
|
|
340 | void |
|
|
341 | vpn::inject_data_packet (tap_packet *pkt, int dst) |
|
|
342 | { |
|
|
343 | if (dst) |
|
|
344 | { |
|
|
345 | // unicast |
|
|
346 | if (dst != THISNODE->id) |
|
|
347 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
348 | } |
|
|
349 | else |
|
|
350 | { |
|
|
351 | // broadcast, this is ugly, but due to the security policy |
|
|
352 | // we have to connect to all hosts... |
|
|
353 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
354 | if ((*c)->conf != THISNODE) |
|
|
355 | (*c)->inject_data_packet (pkt, true); |
|
|
356 | } |
332 | } |
357 | } |
333 | |
358 | |
334 | void |
359 | void |
335 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
360 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
336 | { |
361 | { |
… | |
… | |
353 | connection *c = conns[src - 1]; |
378 | connection *c = conns[src - 1]; |
354 | |
379 | |
355 | if (dst == 0) |
380 | if (dst == 0) |
356 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
381 | slog (L_WARN, _("%s(%s): received broadcast (protocol violation)"), |
357 | c->conf->nodename, (const char *)rsi); |
382 | c->conf->nodename, (const char *)rsi); |
358 | else if (dst != 0 && dst != THISNODE->id) |
383 | else if (dst != THISNODE->id) |
359 | { |
384 | { |
360 | if (THISNODE->routerprio) |
385 | if (THISNODE->routerprio) |
361 | // the tos setting gets lost here. who cares. |
386 | // the tos setting gets lost here. who cares. |
362 | conns[dst - 1]->inject_vpn_packet (pkt); |
387 | conns[dst - 1]->inject_vpn_packet (pkt); |
363 | else |
388 | else |
… | |
… | |
435 | |
460 | |
436 | if (len > 0) |
461 | if (len > 0) |
437 | { |
462 | { |
438 | pkt->len = len; |
463 | pkt->len = len; |
439 | |
464 | |
440 | icmphdr *hdr = (icmphdr *)&((*pkt)[IP_OVERHEAD]); |
465 | icmp_header *hdr = (icmp_header *)&((*pkt)[IP_OVERHEAD]); |
441 | |
466 | |
442 | if (hdr->type == ::conf.icmp_type |
467 | if (hdr->type == ::conf.icmp_type |
443 | && hdr->code == 255) |
468 | && hdr->code == 255) |
444 | { |
469 | { |
445 | // raw sockets deliver the ipv4, but don't expect it on sends |
470 | // raw sockets deliver the ipv4, but don't expect it on sends |
… | |
… | |
524 | /* process data */ |
549 | /* process data */ |
525 | tap_packet *pkt; |
550 | tap_packet *pkt; |
526 | |
551 | |
527 | pkt = tap->recv (); |
552 | pkt = tap->recv (); |
528 | |
553 | |
|
|
554 | if (!pkt) |
|
|
555 | return; |
|
|
556 | |
|
|
557 | if (pkt->len > 14) |
|
|
558 | { |
529 | int dst = mac2id (pkt->dst); |
559 | int dst = mac2id (pkt->dst); |
530 | int src = mac2id (pkt->src); |
560 | int src = mac2id (pkt->src); |
531 | |
561 | |
532 | if (src != THISNODE->id) |
562 | if (src != THISNODE->id) |
533 | { |
|
|
534 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
535 | exit (1); |
|
|
536 | } |
|
|
537 | |
|
|
538 | if (dst == THISNODE->id) |
|
|
539 | { |
|
|
540 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
541 | exit (1); |
|
|
542 | } |
|
|
543 | |
|
|
544 | if (dst > conns.size ()) |
|
|
545 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
546 | else |
|
|
547 | { |
|
|
548 | if (dst) |
|
|
549 | { |
563 | { |
550 | // unicast |
564 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, exiting.")); |
551 | if (dst != THISNODE->id) |
565 | exit (1); |
552 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
553 | } |
566 | } |
|
|
567 | |
|
|
568 | if (dst == THISNODE->id) |
|
|
569 | { |
|
|
570 | slog (L_ERR, _("FATAL: tap packet destined for current node received, exiting.")); |
|
|
571 | exit (1); |
|
|
572 | } |
|
|
573 | |
|
|
574 | if (dst > conns.size ()) |
|
|
575 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
554 | else |
576 | else |
555 | { |
|
|
556 | // broadcast, this is ugly, but due to the security policy |
|
|
557 | // we have to connect to all hosts... |
|
|
558 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
559 | if ((*c)->conf != THISNODE) |
|
|
560 | (*c)->inject_data_packet (pkt); |
577 | inject_data_packet (pkt, dst); |
561 | } |
|
|
562 | } |
578 | } |
563 | |
579 | |
564 | delete pkt; |
580 | delete pkt; |
565 | } |
581 | } |
566 | else if (revents & (POLLHUP | POLLERR)) |
582 | else if (revents & (POLLHUP | POLLERR)) |