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 | |
… | |
… | |
37 | #else |
40 | #else |
38 | #include <linux/if_tun.h> |
41 | #include <linux/if_tun.h> |
39 | #endif |
42 | #endif |
40 | #define DEFAULT_DEVICE "/dev/net/tun" |
43 | #define DEFAULT_DEVICE "/dev/net/tun" |
41 | |
44 | |
42 | #include "gettext.h" |
45 | #include "conf.h" |
43 | |
46 | |
44 | #include "conf.h" |
47 | #if TEST_ETHEREMU |
|
|
48 | # define IF_istun |
|
|
49 | # include "ether_emu.C" |
|
|
50 | #endif |
45 | |
51 | |
46 | const char * |
52 | const char * |
47 | tap_device::info () |
53 | tap_device::info () |
48 | { |
54 | { |
49 | return _("Linux tun/tap device"); |
55 | return _("Linux tun/tap device"); |
… | |
… | |
58 | fd = open (device, O_RDWR); |
64 | fd = open (device, O_RDWR); |
59 | |
65 | |
60 | if (fd < 0) |
66 | if (fd < 0) |
61 | { |
67 | { |
62 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
68 | slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
63 | exit (1); |
69 | exit (EXIT_FAILURE); |
64 | } |
70 | } |
65 | |
71 | |
66 | memset (&ifr, 0, sizeof (ifr)); |
72 | memset (&ifr, 0, sizeof (ifr)); |
|
|
73 | #if TEST_ETHEREMU |
|
|
74 | ifr.ifr_flags = IFF_TUN | IFF_NO_PI; |
|
|
75 | #else |
67 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
76 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
|
77 | #endif |
68 | |
78 | |
69 | if (conf.ifname) |
79 | if (conf.ifname) |
70 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
80 | strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
71 | else |
81 | else |
72 | ifr.ifr_name[0] = 0; |
82 | ifr.ifr_name[0] = 0; |
… | |
… | |
77 | ifrname [IFNAMSIZ] = 0; |
87 | ifrname [IFNAMSIZ] = 0; |
78 | } |
88 | } |
79 | else |
89 | else |
80 | { |
90 | { |
81 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
91 | slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
82 | exit (1); |
92 | exit (EXIT_FAILURE); |
83 | } |
93 | } |
84 | |
94 | |
85 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
95 | if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
86 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
96 | slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
87 | |
97 | |
… | |
… | |
96 | tap_packet * |
106 | tap_packet * |
97 | tap_device::recv () |
107 | tap_device::recv () |
98 | { |
108 | { |
99 | tap_packet *pkt = new tap_packet; |
109 | tap_packet *pkt = new tap_packet; |
100 | |
110 | |
|
|
111 | #if TEST_ETHEREMU |
|
|
112 | pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14); |
|
|
113 | #else |
101 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
114 | pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
|
|
115 | #endif |
102 | |
116 | |
103 | if (pkt->len <= 0) |
117 | if (pkt->len <= 0) |
104 | { |
118 | { |
105 | delete pkt; |
119 | delete pkt; |
106 | slog (L_ERR, _("error while reading from %s %s: %s"), |
120 | slog (L_ERR, _("error while reading from %s %s: %s"), |
107 | info (), DEFAULT_DEVICE, strerror (errno)); |
121 | info (), DEFAULT_DEVICE, strerror (errno)); |
108 | return 0; |
122 | return 0; |
109 | } |
123 | } |
110 | |
124 | |
|
|
125 | #if TEST_ETHEREMU |
|
|
126 | pkt->len += 14; |
|
|
127 | |
|
|
128 | // assume ipv4 |
|
|
129 | (*pkt)[12] = 0x08; |
|
|
130 | (*pkt)[13] = 0x00; |
|
|
131 | |
|
|
132 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
133 | { |
|
|
134 | delete pkt; |
|
|
135 | return 0; |
|
|
136 | } |
|
|
137 | #endif |
|
|
138 | |
111 | return pkt; |
139 | return pkt; |
112 | } |
140 | } |
113 | |
141 | |
114 | void |
142 | void |
115 | tap_device::send (tap_packet *pkt) |
143 | tap_device::send (tap_packet *pkt) |
116 | { |
144 | { |
|
|
145 | #if TEST_ETHEREMU |
|
|
146 | if (ether_emu.tap_to_tun (pkt) && |
|
|
147 | write (fd, &((*pkt)[14]), pkt->len - 14) < 0) |
|
|
148 | #else |
117 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
149 | if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
|
|
150 | #endif |
118 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
151 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
119 | strerror (errno)); |
152 | strerror (errno)); |
120 | } |
153 | } |
121 | |
154 | |