ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-linux.C
Revision: 1.11
Committed: Fri Mar 18 02:32:20 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_8
Changes since 1.10: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 device-linux.C -- Interaction with Linux tun/tap device
3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4
5 This file is part of GVPE.
6
7 GVPE is free software; you can redistribute it and/or modify
8 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 along with gvpe; if not, write to the Free Software
19 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 #include <net/if.h>
37
38 #ifdef LINUX_IF_TUN_H
39 # include LINUX_IF_TUN_H
40 #else
41 #include <linux/if_tun.h>
42 #endif
43 #define DEFAULT_DEVICE "/dev/net/tun"
44
45 #include "gettext.h"
46
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 tap_device::tap_device ()
61 {
62 struct ifreq ifr;
63
64 device = DEFAULT_DEVICE;
65
66 fd = open (device, O_RDWR);
67
68 if (fd < 0)
69 {
70 slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno));
71 exit (EXIT_FAILURE);
72 }
73
74 memset (&ifr, 0, sizeof (ifr));
75 #if TEST_ETHEREMU
76 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
77 #else
78 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
79 #endif
80
81 if (conf.ifname)
82 strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ);
83 else
84 ifr.ifr_name[0] = 0;
85
86 if (!ioctl (fd, TUNSETIFF, &ifr))
87 {
88 strncpy (ifrname, ifr.ifr_name, IFNAMSIZ);
89 ifrname [IFNAMSIZ] = 0;
90 }
91 else
92 {
93 slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno));
94 exit (EXIT_FAILURE);
95 }
96
97 if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0))
98 slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno));
99
100 slog (L_DEBUG, _("%s is a %s"), device, info ());
101 }
102
103 tap_device::~tap_device ()
104 {
105 close (fd);
106 }
107
108 tap_packet *
109 tap_device::recv ()
110 {
111 tap_packet *pkt = new tap_packet;
112
113 #if TEST_ETHEREMU
114 pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14);
115 #else
116 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU);
117 #endif
118
119 if (pkt->len <= 0)
120 {
121 delete pkt;
122 slog (L_ERR, _("error while reading from %s %s: %s"),
123 info (), DEFAULT_DEVICE, strerror (errno));
124 return 0;
125 }
126
127 #if TEST_ETHEREMU
128 pkt->len += 14;
129
130 // assume ipv4
131 (*pkt)[12] = 0x08;
132 (*pkt)[13] = 0x00;
133
134 if (!ether_emu.tun_to_tap (pkt))
135 {
136 delete pkt;
137 return 0;
138 }
139 #endif
140
141 return pkt;
142 }
143
144 void
145 tap_device::send (tap_packet *pkt)
146 {
147 #if TEST_ETHEREMU
148 if (ether_emu.tap_to_tun (pkt) &&
149 write (fd, &((*pkt)[14]), pkt->len - 14) < 0)
150 #else
151 if (write (fd, &((*pkt)[0]), pkt->len) < 0)
152 #endif
153 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE,
154 strerror (errno));
155 }
156