1 | /* |
1 | /* |
2 | device-linux.C -- Interaction with Linux tun/tap device |
2 | device-linux.C -- Interaction with Linux tun/tap device |
|
|
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
3 | |
4 | |
|
|
5 | This file is part of GVPE. |
|
|
6 | |
4 | This program is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
5 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
6 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
7 | (at your option) any later version. |
10 | option) any later version. |
8 | |
11 | |
9 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
12 | GNU General Public License for more details. |
15 | Public License for more details. |
13 | |
16 | |
14 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
15 | along with this program; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
17 | */ |
30 | */ |
18 | |
31 | |
19 | #include "config.h" |
32 | #include "config.h" |
20 | |
33 | |
21 | #include <cstdio> |
34 | #include <cstdio> |
… | |
… | |
28 | #include <fcntl.h> |
41 | #include <fcntl.h> |
29 | #include <net/if.h> |
42 | #include <net/if.h> |
30 | #include <unistd.h> |
43 | #include <unistd.h> |
31 | #include <sys/ioctl.h> |
44 | #include <sys/ioctl.h> |
32 | |
45 | |
|
|
46 | #include <net/if.h> |
|
|
47 | |
33 | #ifdef LINUX_IF_TUN_H |
48 | #ifdef LINUX_IF_TUN_H |
34 | #include LINUX_IF_TUN_H |
49 | # include LINUX_IF_TUN_H |
35 | #else |
50 | #else |
36 | #include <linux/if_tun.h> |
51 | #include <linux/if_tun.h> |
37 | #endif |
52 | #endif |
38 | #define DEFAULT_DEVICE "/dev/net/tun" |
53 | #define DEFAULT_DEVICE "/dev/net/tun" |
39 | |
54 | |
40 | #include "gettext.h" |
55 | #include "gettext.h" |
41 | |
56 | |
42 | #include "conf.h" |
57 | #include "conf.h" |
43 | |
58 | |
|
|
59 | #if TEST_ETHEREMU |
|
|
60 | # define IF_istun |
|
|
61 | # include "ether_emu.C" |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | const char * |
|
|
65 | tap_device::info () |
|
|
66 | { |
|
|
67 | return _("Linux tun/tap device"); |
|
|
68 | } |
|
|
69 | |
|
|
70 | const char * |
|
|
71 | tap_device::if_up () |
|
|
72 | { |
|
|
73 | return "/sbin/ifconfig $IFNAME hw ether $MAC mtu $MTU"; |
|
|
74 | } |
|
|
75 | |
44 | tap_device::tap_device () |
76 | tap_device::tap_device () |
45 | { |
77 | { |
46 | struct ifreq ifr; |
78 | struct ifreq ifr; |
47 | |
79 | |
48 | device = DEFAULT_DEVICE; |
80 | device = (char *)DEFAULT_DEVICE; |
49 | |
81 | |
50 | fd = open (device, O_RDWR); |
82 | fd = open (device, O_RDWR); |
51 | |
83 | |
52 | if (fd < 0) |
84 | if (fd < 0) |
53 | { |
85 | { |
54 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
86 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
55 | exit (1); |
87 | exit (EXIT_FAILURE); |
56 | } |
88 | } |
57 | |
89 | |
58 | memset (&ifr, 0, sizeof (ifr)); |
90 | memset (&ifr, 0, sizeof (ifr)); |
|
|
91 | #if TEST_ETHEREMU |
|
|
92 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
|
|
93 | #else |
59 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
94 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
|
95 | #endif |
60 | |
96 | |
61 | if (conf.ifname) |
97 | if (conf.ifname) |
62 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
98 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
63 | else |
99 | else |
64 | ifr.ifr_name[0] = 0; |
100 | ifr.ifr_name[0] = 0; |
… | |
… | |
68 | strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
104 | strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
69 | ifrname [IFNAMSIZ] = 0; |
105 | ifrname [IFNAMSIZ] = 0; |
70 | } |
106 | } |
71 | else |
107 | else |
72 | { |
108 | { |
73 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
109 | slog (L_CRIT, _("unable to configure tun/tap interface, exiting: %s"), strerror (errno)); |
74 | exit (1); |
110 | exit (EXIT_FAILURE); |
75 | } |
111 | } |
|
|
112 | |
|
|
113 | #if 0 |
|
|
114 | does not work |
|
|
115 | id2mac (THISNODE->id, &ifr.ifr_hwaddr.sa_data); |
|
|
116 | if (ioctl (fd, SIOCSIFHWADDR, &ifr)) |
|
|
117 | { |
|
|
118 | slog (L_ERR, _("cannot set MAC address for device %s, exiting: %s"), ifrname, strerror (errno)); |
|
|
119 | exit (EXIT_FAILURE); |
|
|
120 | } |
|
|
121 | #endif |
76 | |
122 | |
77 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
123 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
78 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
124 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
79 | |
125 | |
80 | slog (L_DEBUG, _("%s is a %s"), device, info ()); |
126 | slog (L_DEBUG, _("%s is a %s"), device, info ()); |
… | |
… | |
88 | tap_packet * |
134 | tap_packet * |
89 | tap_device::recv () |
135 | tap_device::recv () |
90 | { |
136 | { |
91 | tap_packet *pkt = new tap_packet; |
137 | tap_packet *pkt = new tap_packet; |
92 | |
138 | |
|
|
139 | #if TEST_ETHEREMU |
|
|
140 | pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
|
|
141 | #else |
93 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
142 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
|
|
143 | #endif |
94 | |
144 | |
95 | if (pkt->len <= 0) |
145 | if (pkt->len <= 0) |
96 | { |
146 | { |
|
|
147 | delete pkt; |
97 | slog (L_ERR, _("error while reading from %s %s: %s"), |
148 | slog (L_ERR, _("error while reading from %s %s: %s"), |
98 | info (), DEFAULT_DEVICE, strerror (errno)); |
149 | info (), DEFAULT_DEVICE, strerror (errno)); |
99 | free (pkt); |
|
|
100 | return 0; |
150 | return 0; |
101 | } |
151 | } |
|
|
152 | |
|
|
153 | #if TEST_ETHEREMU |
|
|
154 | pkt->len += 14; |
|
|
155 | |
|
|
156 | // assume ipv4 |
|
|
157 | (*pkt)[12] = 0x08; |
|
|
158 | (*pkt)[13] = 0x00; |
|
|
159 | |
|
|
160 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
161 | { |
|
|
162 | delete pkt; |
|
|
163 | return 0; |
|
|
164 | } |
|
|
165 | #endif |
102 | |
166 | |
103 | return pkt; |
167 | return pkt; |
104 | } |
168 | } |
105 | |
169 | |
106 | void |
170 | void |
107 | tap_device::send (tap_packet *pkt) |
171 | tap_device::send (tap_packet *pkt) |
108 | { |
172 | { |
|
|
173 | #if TEST_ETHEREMU |
|
|
174 | if (ether_emu.tap_to_tun (pkt) && |
|
|
175 | write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
|
|
176 | #else |
109 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
177 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
|
|
178 | #endif |
110 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
179 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
111 | strerror (errno)); |
180 | strerror (errno)); |
112 | } |
181 | } |
113 | |
182 | |