| 1 |
pcg |
1.1 |
/* |
| 2 |
|
|
device-tincd.C -- include one of the tincd low level implementations. |
| 3 |
|
|
|
| 4 |
|
|
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 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 7 |
|
|
(at your option) any later version. |
| 8 |
|
|
|
| 9 |
|
|
This program is distributed in the hope that it will be useful, |
| 10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
|
|
GNU General Public License for more details. |
| 13 |
|
|
|
| 14 |
|
|
You should have received a copy of the GNU General Public License |
| 15 |
|
|
along with this program; if not, write to the Free Software |
| 16 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
|
|
*/ |
| 18 |
|
|
|
| 19 |
|
|
#include <cstdio> |
| 20 |
|
|
#include <cstring> |
| 21 |
|
|
#include <cerrno> |
| 22 |
|
|
|
| 23 |
|
|
#include <sys/types.h> |
| 24 |
|
|
#include <sys/stat.h> |
| 25 |
|
|
#include <sys/ioctl.h> |
| 26 |
|
|
#include <unistd.h> |
| 27 |
|
|
#include <syslog.h> |
| 28 |
|
|
#include <fcntl.h> |
| 29 |
|
|
|
| 30 |
|
|
#include "conf.h" |
| 31 |
|
|
|
| 32 |
pcg |
1.3 |
// following headers used by cygwin (maybe others) |
| 33 |
|
|
#include "netcompat.h" |
| 34 |
|
|
#include <signal.h> |
| 35 |
|
|
|
| 36 |
|
|
#define xstrdup(strd) strdup(str) |
| 37 |
|
|
|
| 38 |
pcg |
1.2 |
/* make the tincd sources feel comfortable in our environment. */ |
| 39 |
|
|
/* this was reasonably easy to do. */ |
| 40 |
pcg |
1.1 |
#define routing_mode 1 |
| 41 |
|
|
#define RMODE_ROUTER 0 |
| 42 |
|
|
|
| 43 |
pcg |
1.2 |
#define LOG_TO_L(level) \ |
| 44 |
pcg |
1.1 |
(level) == LOG_ERR ? L_ERR \ |
| 45 |
|
|
: (level) == LOG_DEBUG ? L_DEBUG \ |
| 46 |
|
|
: (level) == LOG_WARNING ? L_WARN \ |
| 47 |
|
|
: (level) == LOG_INFO ? L_INFO \ |
| 48 |
pcg |
1.2 |
: L_NOTICE |
| 49 |
|
|
|
| 50 |
|
|
#if __STDC_VERSION__ > 199900 |
| 51 |
|
|
# define logger(level, ...) slog (LOG_TO_L(level), __VA_ARGS__) |
| 52 |
|
|
#elif __GNUC__ |
| 53 |
|
|
# define logger(level, args...) slog (LOG_TO_L(level), ## args) |
| 54 |
|
|
#else |
| 55 |
|
|
# error either need ISO-C 99 compliant compiler or gcc. |
| 56 |
|
|
#endif |
| 57 |
pcg |
1.1 |
|
| 58 |
|
|
#define ifdebug(subsys) if (0) |
| 59 |
|
|
|
| 60 |
|
|
#define cp() |
| 61 |
|
|
#define lookup_config(config_tree,key) (key) |
| 62 |
|
|
|
| 63 |
|
|
#define MTU MAXSIZE |
| 64 |
|
|
|
| 65 |
|
|
// BIGGEST hack of 'em all |
| 66 |
|
|
// will be casted to data_packet, due to structural similarity |
| 67 |
|
|
struct vpn_packet_t : net_packet { |
| 68 |
|
|
u8 data[MAXSIZE]; |
| 69 |
|
|
}; |
| 70 |
|
|
|
| 71 |
|
|
static tap_device *self; |
| 72 |
|
|
|
| 73 |
|
|
static bool overwrite_mac; |
| 74 |
|
|
|
| 75 |
|
|
static bool |
| 76 |
|
|
get_config_string(const char *key, char **res) |
| 77 |
|
|
{ |
| 78 |
|
|
if (!strcmp (key, "Interface")) |
| 79 |
|
|
*res = conf.ifname; |
| 80 |
|
|
else if (!strcmp (key, "Device")) |
| 81 |
|
|
*res = 0; |
| 82 |
|
|
else |
| 83 |
|
|
{ |
| 84 |
|
|
slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key); |
| 85 |
|
|
*res = 0; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
return *res; |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
#define netname conf.ifname |
| 92 |
|
|
|
| 93 |
|
|
#if IF_linux |
| 94 |
|
|
# include "tincd/linux/device.c" |
| 95 |
|
|
#elif IF_freebsd |
| 96 |
|
|
# include "tincd/freebsd/device.c" |
| 97 |
|
|
#elif IF_netbsd |
| 98 |
|
|
# include "tincd/netbsd/device.c" |
| 99 |
|
|
#elif IF_solaris |
| 100 |
|
|
# include "tincd/solaris/device.c" |
| 101 |
|
|
#elif IF_cygwin |
| 102 |
|
|
# include "tincd/cygwin/device.c" |
| 103 |
|
|
#elif IF_mingw |
| 104 |
|
|
# include "tincd/mingw/device.c" |
| 105 |
|
|
#elif IF_darwin |
| 106 |
|
|
# include "tincd/darwin/device.c" |
| 107 |
|
|
#elif IF_raw_socket |
| 108 |
|
|
# include "tincd/raw_socket/device.c" |
| 109 |
|
|
#else |
| 110 |
|
|
# error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
| 111 |
|
|
#endif |
| 112 |
|
|
|
| 113 |
|
|
const char * |
| 114 |
|
|
tap_device::info () |
| 115 |
|
|
{ |
| 116 |
|
|
return _("tincd compatibility layer"); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
tap_device::tap_device () |
| 120 |
|
|
{ |
| 121 |
|
|
self = this; |
| 122 |
|
|
|
| 123 |
|
|
if (setup_device ()) |
| 124 |
|
|
{ |
| 125 |
|
|
//slog (L_DEBUG, _("%s is a %s"), device, info ()); |
| 126 |
|
|
fd = device_fd; |
| 127 |
|
|
strcpy (ifrname, iface); |
| 128 |
|
|
} |
| 129 |
|
|
else |
| 130 |
|
|
{ |
| 131 |
|
|
slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); |
| 132 |
|
|
exit (1); |
| 133 |
|
|
} |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
tap_device::~tap_device () |
| 137 |
|
|
{ |
| 138 |
|
|
close_device (); |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
tap_packet * |
| 142 |
|
|
tap_device::recv () |
| 143 |
|
|
{ |
| 144 |
|
|
tap_packet *pkt = new tap_packet; |
| 145 |
|
|
|
| 146 |
|
|
if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
| 147 |
|
|
{ |
| 148 |
|
|
delete pkt; |
| 149 |
|
|
slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
| 150 |
|
|
strerror (errno)); |
| 151 |
|
|
return 0; |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
return pkt; |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
void |
| 158 |
|
|
tap_device::send (tap_packet *pkt) |
| 159 |
|
|
{ |
| 160 |
|
|
if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
| 161 |
|
|
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
| 162 |
|
|
strerror (errno)); |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
|