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-2005 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 |
5 | it under the terms of the GNU General Public License as published by |
8 | 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 |
9 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
10 | (at your 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, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | but 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 |
12 | GNU General Public License for more details. |
15 | GNU General 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 |
15 | along with this program; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ |
20 | */ |
18 | |
21 | |
19 | #include "config.h" |
22 | #include "config.h" |
20 | |
23 | |
… | |
… | |
41 | |
44 | |
42 | #include "gettext.h" |
45 | #include "gettext.h" |
43 | |
46 | |
44 | #include "conf.h" |
47 | #include "conf.h" |
45 | |
48 | |
|
|
49 | #if TEST_ETHEREMU |
|
|
50 | # define IF_istun |
|
|
51 | # include "ether_emu.C" |
|
|
52 | #endif |
|
|
53 | |
46 | const char * |
54 | const char * |
47 | tap_device::info () |
55 | tap_device::info () |
48 | { |
56 | { |
49 | return _("Linux tun/tap device"); |
57 | return _("Linux tun/tap device"); |
|
|
58 | } |
|
|
59 | |
|
|
60 | const char * |
|
|
61 | tap_device::if_up () |
|
|
62 | { |
|
|
63 | return "/sbin/ifconfig $IFNAME hw ether $MAC mtu $MTU"; |
50 | } |
64 | } |
51 | |
65 | |
52 | tap_device::tap_device () |
66 | tap_device::tap_device () |
53 | { |
67 | { |
54 | struct ifreq ifr; |
68 | struct ifreq ifr; |
… | |
… | |
58 | fd = open (device, O_RDWR); |
72 | fd = open (device, O_RDWR); |
59 | |
73 | |
60 | if (fd < 0) |
74 | if (fd < 0) |
61 | { |
75 | { |
62 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
76 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
63 | exit (1); |
77 | exit (EXIT_FAILURE); |
64 | } |
78 | } |
65 | |
79 | |
66 | memset (&ifr, 0, sizeof (ifr)); |
80 | memset (&ifr, 0, sizeof (ifr)); |
|
|
81 | #if TEST_ETHEREMU |
|
|
82 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
|
|
83 | #else |
67 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
84 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
|
85 | #endif |
68 | |
86 | |
69 | if (conf.ifname) |
87 | if (conf.ifname) |
70 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
88 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
71 | else |
89 | else |
72 | ifr.ifr_name[0] = 0; |
90 | ifr.ifr_name[0] = 0; |
… | |
… | |
76 | strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
94 | strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
77 | ifrname [IFNAMSIZ] = 0; |
95 | ifrname [IFNAMSIZ] = 0; |
78 | } |
96 | } |
79 | else |
97 | else |
80 | { |
98 | { |
81 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
99 | slog (L_CRIT, _("unable to configure tun/tap interface, exiting: %s"), strerror (errno)); |
82 | exit (1); |
100 | exit (EXIT_FAILURE); |
83 | } |
101 | } |
|
|
102 | |
|
|
103 | #if 0 |
|
|
104 | doesn't work |
|
|
105 | id2mac (THISNODE->id, &ifr.ifr_hwaddr.sa_data); |
|
|
106 | if (ioctl (fd, SIOCSIFHWADDR, &ifr)) |
|
|
107 | { |
|
|
108 | slog (L_ERR, _("cannot set MAC address for device %s, exiting: %s"), ifrname, strerror (errno)); |
|
|
109 | exit (EXIT_FAILURE); |
|
|
110 | } |
|
|
111 | #endif |
84 | |
112 | |
85 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
113 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
86 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
114 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
87 | |
115 | |
88 | slog (L_DEBUG, _("%s is a %s"), device, info ()); |
116 | slog (L_DEBUG, _("%s is a %s"), device, info ()); |
… | |
… | |
96 | tap_packet * |
124 | tap_packet * |
97 | tap_device::recv () |
125 | tap_device::recv () |
98 | { |
126 | { |
99 | tap_packet *pkt = new tap_packet; |
127 | tap_packet *pkt = new tap_packet; |
100 | |
128 | |
|
|
129 | #if TEST_ETHEREMU |
|
|
130 | pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
|
|
131 | #else |
101 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
132 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
|
|
133 | #endif |
102 | |
134 | |
103 | if (pkt->len <= 0) |
135 | if (pkt->len <= 0) |
104 | { |
136 | { |
105 | delete pkt; |
137 | delete pkt; |
106 | slog (L_ERR, _("error while reading from %s %s: %s"), |
138 | slog (L_ERR, _("error while reading from %s %s: %s"), |
107 | info (), DEFAULT_DEVICE, strerror (errno)); |
139 | info (), DEFAULT_DEVICE, strerror (errno)); |
108 | return 0; |
140 | return 0; |
109 | } |
141 | } |
110 | |
142 | |
|
|
143 | #if TEST_ETHEREMU |
|
|
144 | pkt->len += 14; |
|
|
145 | |
|
|
146 | // assume ipv4 |
|
|
147 | (*pkt)[12] = 0x08; |
|
|
148 | (*pkt)[13] = 0x00; |
|
|
149 | |
|
|
150 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
151 | { |
|
|
152 | delete pkt; |
|
|
153 | return 0; |
|
|
154 | } |
|
|
155 | #endif |
|
|
156 | |
111 | return pkt; |
157 | return pkt; |
112 | } |
158 | } |
113 | |
159 | |
114 | void |
160 | void |
115 | tap_device::send (tap_packet *pkt) |
161 | tap_device::send (tap_packet *pkt) |
116 | { |
162 | { |
|
|
163 | #if TEST_ETHEREMU |
|
|
164 | if (ether_emu.tap_to_tun (pkt) && |
|
|
165 | write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
|
|
166 | #else |
117 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
167 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
|
|
168 | #endif |
118 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
169 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
119 | strerror (errno)); |
170 | strerror (errno)); |
120 | } |
171 | } |
121 | |
172 | |