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 | |
… | |
… | |
28 | #include <fcntl.h> |
31 | #include <fcntl.h> |
29 | #include <net/if.h> |
32 | #include <net/if.h> |
30 | #include <unistd.h> |
33 | #include <unistd.h> |
31 | #include <sys/ioctl.h> |
34 | #include <sys/ioctl.h> |
32 | |
35 | |
|
|
36 | #include <net/if.h> |
|
|
37 | |
33 | #ifdef LINUX_IF_TUN_H |
38 | #ifdef LINUX_IF_TUN_H |
34 | #include LINUX_IF_TUN_H |
39 | # include LINUX_IF_TUN_H |
35 | #else |
40 | #else |
36 | #include <linux/if_tun.h> |
41 | #include <linux/if_tun.h> |
37 | #endif |
42 | #endif |
38 | #define DEFAULT_DEVICE "/dev/net/tun" |
43 | #define DEFAULT_DEVICE "/dev/net/tun" |
39 | |
44 | |
40 | #include "gettext.h" |
45 | #include "gettext.h" |
41 | |
46 | |
42 | #include "conf.h" |
47 | #include "conf.h" |
|
|
48 | |
|
|
49 | #if TEST_ETHEREMU |
|
|
50 | # define IF_istun |
|
|
51 | # include "ether_emu.C" |
|
|
52 | #endif |
|
|
53 | |
|
|
54 | const char * |
|
|
55 | tap_device::info () |
|
|
56 | { |
|
|
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 up"; |
|
|
64 | } |
43 | |
65 | |
44 | tap_device::tap_device () |
66 | tap_device::tap_device () |
45 | { |
67 | { |
46 | struct ifreq ifr; |
68 | struct ifreq ifr; |
47 | |
69 | |
… | |
… | |
50 | fd = open (device, O_RDWR); |
72 | fd = open (device, O_RDWR); |
51 | |
73 | |
52 | if (fd < 0) |
74 | if (fd < 0) |
53 | { |
75 | { |
54 | 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)); |
55 | exit (1); |
77 | exit (EXIT_FAILURE); |
56 | } |
78 | } |
57 | |
79 | |
58 | 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 |
59 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
84 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
|
85 | #endif |
60 | |
86 | |
61 | if (conf.ifname) |
87 | if (conf.ifname) |
62 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
88 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
63 | else |
89 | else |
64 | ifr.ifr_name[0] = 0; |
90 | ifr.ifr_name[0] = 0; |
… | |
… | |
69 | ifrname [IFNAMSIZ] = 0; |
95 | ifrname [IFNAMSIZ] = 0; |
70 | } |
96 | } |
71 | else |
97 | else |
72 | { |
98 | { |
73 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
99 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
74 | exit (1); |
100 | exit (EXIT_FAILURE); |
75 | } |
101 | } |
76 | |
102 | |
77 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
103 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
78 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
104 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
79 | |
105 | |
… | |
… | |
88 | tap_packet * |
114 | tap_packet * |
89 | tap_device::recv () |
115 | tap_device::recv () |
90 | { |
116 | { |
91 | tap_packet *pkt = new tap_packet; |
117 | tap_packet *pkt = new tap_packet; |
92 | |
118 | |
|
|
119 | #if TEST_ETHEREMU |
|
|
120 | pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
|
|
121 | #else |
93 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
122 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
|
|
123 | #endif |
94 | |
124 | |
95 | if (pkt->len <= 0) |
125 | if (pkt->len <= 0) |
96 | { |
126 | { |
|
|
127 | delete pkt; |
97 | slog (L_ERR, _("error while reading from %s %s: %s"), |
128 | slog (L_ERR, _("error while reading from %s %s: %s"), |
98 | info (), DEFAULT_DEVICE, strerror (errno)); |
129 | info (), DEFAULT_DEVICE, strerror (errno)); |
99 | free (pkt); |
|
|
100 | return 0; |
130 | return 0; |
101 | } |
131 | } |
|
|
132 | |
|
|
133 | #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 |
102 | |
146 | |
103 | return pkt; |
147 | return pkt; |
104 | } |
148 | } |
105 | |
149 | |
106 | void |
150 | void |
107 | tap_device::send (tap_packet *pkt) |
151 | tap_device::send (tap_packet *pkt) |
108 | { |
152 | { |
|
|
153 | #if TEST_ETHEREMU |
|
|
154 | if (ether_emu.tap_to_tun (pkt) && |
|
|
155 | write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
|
|
156 | #else |
109 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
157 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
|
|
158 | #endif |
110 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
159 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
111 | strerror (errno)); |
160 | strerror (errno)); |
112 | } |
161 | } |
113 | |
162 | |