1 |
pcg |
1.1 |
/* |
2 |
|
|
device-linux.C -- Interaction with Linux tun/tap device |
3 |
|
|
|
4 |
|
|
This program is free software; you can redistribute it and/or modify |
5 |
|
|
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 |
7 |
|
|
(at your option) any later version. |
8 |
|
|
|
9 |
|
|
This program is distributed in the hope that it will be useful, |
10 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
|
|
GNU General Public License for more details. |
13 |
|
|
|
14 |
|
|
You should have received a copy of the GNU General Public License |
15 |
|
|
along with this program; if not, write to the Free Software |
16 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include "config.h" |
20 |
|
|
|
21 |
|
|
#include <cstdio> |
22 |
|
|
#include <cstring> |
23 |
|
|
#include <cstdlib> |
24 |
|
|
|
25 |
|
|
#include <errno.h> |
26 |
|
|
#include <sys/types.h> |
27 |
|
|
#include <sys/stat.h> |
28 |
|
|
#include <fcntl.h> |
29 |
|
|
#include <net/if.h> |
30 |
|
|
#include <unistd.h> |
31 |
|
|
#include <sys/ioctl.h> |
32 |
|
|
|
33 |
pcg |
1.3 |
#include <net/if.h> |
34 |
|
|
|
35 |
pcg |
1.1 |
#ifdef LINUX_IF_TUN_H |
36 |
pcg |
1.3 |
# include LINUX_IF_TUN_H |
37 |
pcg |
1.1 |
#else |
38 |
|
|
#include <linux/if_tun.h> |
39 |
|
|
#endif |
40 |
|
|
#define DEFAULT_DEVICE "/dev/net/tun" |
41 |
|
|
|
42 |
|
|
#include "gettext.h" |
43 |
|
|
|
44 |
|
|
#include "conf.h" |
45 |
|
|
|
46 |
pcg |
1.3 |
const char * |
47 |
|
|
tap_device::info () |
48 |
|
|
{ |
49 |
|
|
return _("Linux tun/tap device"); |
50 |
|
|
} |
51 |
|
|
|
52 |
pcg |
1.1 |
tap_device::tap_device () |
53 |
|
|
{ |
54 |
|
|
struct ifreq ifr; |
55 |
|
|
|
56 |
|
|
device = DEFAULT_DEVICE; |
57 |
|
|
|
58 |
|
|
fd = open (device, O_RDWR); |
59 |
|
|
|
60 |
|
|
if (fd < 0) |
61 |
|
|
{ |
62 |
|
|
slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
63 |
|
|
exit (1); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
memset (&ifr, 0, sizeof (ifr)); |
67 |
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
68 |
|
|
|
69 |
|
|
if (conf.ifname) |
70 |
|
|
strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
71 |
|
|
else |
72 |
|
|
ifr.ifr_name[0] = 0; |
73 |
|
|
|
74 |
|
|
if (!ioctl (fd, TUNSETIFF, &ifr)) |
75 |
|
|
{ |
76 |
|
|
strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
77 |
|
|
ifrname [IFNAMSIZ] = 0; |
78 |
|
|
} |
79 |
|
|
else |
80 |
|
|
{ |
81 |
|
|
slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
82 |
|
|
exit (1); |
83 |
|
|
} |
84 |
|
|
|
85 |
pcg |
1.2 |
if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
86 |
|
|
slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
87 |
pcg |
1.1 |
|
88 |
|
|
slog (L_DEBUG, _("%s is a %s"), device, info ()); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
tap_device::~tap_device () |
92 |
|
|
{ |
93 |
|
|
close (fd); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
tap_packet * |
97 |
|
|
tap_device::recv () |
98 |
|
|
{ |
99 |
|
|
tap_packet *pkt = new tap_packet; |
100 |
|
|
|
101 |
|
|
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
102 |
|
|
|
103 |
|
|
if (pkt->len <= 0) |
104 |
|
|
{ |
105 |
pcg |
1.3 |
delete pkt; |
106 |
pcg |
1.1 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
107 |
|
|
info (), DEFAULT_DEVICE, strerror (errno)); |
108 |
|
|
return 0; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
return pkt; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
void |
115 |
|
|
tap_device::send (tap_packet *pkt) |
116 |
|
|
{ |
117 |
|
|
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
118 |
|
|
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
119 |
|
|
strerror (errno)); |
120 |
|
|
} |
121 |
|
|
|