1 |
pcg |
1.1 |
/* |
2 |
|
|
device-linux.C -- Interaction with Linux tun/tap device |
3 |
pcg |
1.9 |
Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
4 |
pcg |
1.1 |
|
5 |
pcg |
1.9 |
This file is part of GVPE. |
6 |
|
|
|
7 |
|
|
GVPE is free software; you can redistribute it and/or modify |
8 |
pcg |
1.1 |
it under the terms of the GNU General Public License as published by |
9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
(at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
pcg |
1.9 |
along with gvpe; if not, write to the Free Software |
19 |
pcg |
1.1 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
#include "config.h" |
23 |
|
|
|
24 |
|
|
#include <cstdio> |
25 |
|
|
#include <cstring> |
26 |
|
|
#include <cstdlib> |
27 |
|
|
|
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <sys/types.h> |
30 |
|
|
#include <sys/stat.h> |
31 |
|
|
#include <fcntl.h> |
32 |
|
|
#include <net/if.h> |
33 |
|
|
#include <unistd.h> |
34 |
|
|
#include <sys/ioctl.h> |
35 |
|
|
|
36 |
pcg |
1.3 |
#include <net/if.h> |
37 |
|
|
|
38 |
pcg |
1.1 |
#ifdef LINUX_IF_TUN_H |
39 |
pcg |
1.3 |
# include LINUX_IF_TUN_H |
40 |
pcg |
1.1 |
#else |
41 |
|
|
#include <linux/if_tun.h> |
42 |
|
|
#endif |
43 |
|
|
#define DEFAULT_DEVICE "/dev/net/tun" |
44 |
|
|
|
45 |
pcg |
1.11 |
#include "gettext.h" |
46 |
|
|
|
47 |
pcg |
1.1 |
#include "conf.h" |
48 |
|
|
|
49 |
pcg |
1.4 |
#if TEST_ETHEREMU |
50 |
pcg |
1.6 |
# define IF_istun |
51 |
pcg |
1.4 |
# include "ether_emu.C" |
52 |
|
|
#endif |
53 |
|
|
|
54 |
pcg |
1.3 |
const char * |
55 |
|
|
tap_device::info () |
56 |
|
|
{ |
57 |
|
|
return _("Linux tun/tap device"); |
58 |
|
|
} |
59 |
|
|
|
60 |
pcg |
1.12 |
const char * |
61 |
|
|
tap_device::if_up () |
62 |
|
|
{ |
63 |
|
|
return "/sbin/ifconfig $IFNAME hw ether $MAC mtu $MTU up"; |
64 |
|
|
} |
65 |
|
|
|
66 |
pcg |
1.1 |
tap_device::tap_device () |
67 |
|
|
{ |
68 |
|
|
struct ifreq ifr; |
69 |
|
|
|
70 |
|
|
device = DEFAULT_DEVICE; |
71 |
|
|
|
72 |
|
|
fd = open (device, O_RDWR); |
73 |
|
|
|
74 |
|
|
if (fd < 0) |
75 |
|
|
{ |
76 |
|
|
slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
77 |
pcg |
1.8 |
exit (EXIT_FAILURE); |
78 |
pcg |
1.1 |
} |
79 |
|
|
|
80 |
|
|
memset (&ifr, 0, sizeof (ifr)); |
81 |
pcg |
1.4 |
#if TEST_ETHEREMU |
82 |
|
|
ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
83 |
|
|
#else |
84 |
pcg |
1.1 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
85 |
pcg |
1.4 |
#endif |
86 |
pcg |
1.1 |
|
87 |
|
|
if (conf.ifname) |
88 |
|
|
strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
89 |
|
|
else |
90 |
|
|
ifr.ifr_name[0] = 0; |
91 |
|
|
|
92 |
|
|
if (!ioctl (fd, TUNSETIFF, &ifr)) |
93 |
|
|
{ |
94 |
|
|
strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
95 |
|
|
ifrname [IFNAMSIZ] = 0; |
96 |
|
|
} |
97 |
|
|
else |
98 |
|
|
{ |
99 |
|
|
slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
100 |
pcg |
1.8 |
exit (EXIT_FAILURE); |
101 |
pcg |
1.1 |
} |
102 |
|
|
|
103 |
pcg |
1.2 |
if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
104 |
|
|
slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
105 |
pcg |
1.1 |
|
106 |
|
|
slog (L_DEBUG, _("%s is a %s"), device, info ()); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
tap_device::~tap_device () |
110 |
|
|
{ |
111 |
|
|
close (fd); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
tap_packet * |
115 |
|
|
tap_device::recv () |
116 |
|
|
{ |
117 |
|
|
tap_packet *pkt = new tap_packet; |
118 |
|
|
|
119 |
pcg |
1.4 |
#if TEST_ETHEREMU |
120 |
|
|
pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
121 |
|
|
#else |
122 |
pcg |
1.1 |
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
123 |
pcg |
1.4 |
#endif |
124 |
pcg |
1.1 |
|
125 |
|
|
if (pkt->len <= 0) |
126 |
|
|
{ |
127 |
pcg |
1.3 |
delete pkt; |
128 |
pcg |
1.1 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
129 |
|
|
info (), DEFAULT_DEVICE, strerror (errno)); |
130 |
|
|
return 0; |
131 |
|
|
} |
132 |
|
|
|
133 |
pcg |
1.4 |
#if TEST_ETHEREMU |
134 |
|
|
pkt->len += 14; |
135 |
|
|
|
136 |
|
|
// assume ipv4 |
137 |
|
|
(*pkt)[12] = 0x08; |
138 |
|
|
(*pkt)[13] = 0x00; |
139 |
|
|
|
140 |
|
|
if (!ether_emu.tun_to_tap (pkt)) |
141 |
|
|
{ |
142 |
|
|
delete pkt; |
143 |
|
|
return 0; |
144 |
|
|
} |
145 |
|
|
#endif |
146 |
|
|
|
147 |
pcg |
1.1 |
return pkt; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
void |
151 |
|
|
tap_device::send (tap_packet *pkt) |
152 |
|
|
{ |
153 |
pcg |
1.4 |
#if TEST_ETHEREMU |
154 |
|
|
if (ether_emu.tap_to_tun (pkt) && |
155 |
|
|
write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
156 |
|
|
#else |
157 |
pcg |
1.1 |
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
158 |
pcg |
1.4 |
#endif |
159 |
pcg |
1.1 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
160 |
|
|
strerror (errno)); |
161 |
|
|
} |
162 |
|
|
|