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 |
#ifdef LINUX_IF_TUN_H |
34 |
#include LINUX_IF_TUN_H |
35 |
#else |
36 |
#include <linux/if_tun.h> |
37 |
#endif |
38 |
#define DEFAULT_DEVICE "/dev/net/tun" |
39 |
|
40 |
#include "gettext.h" |
41 |
|
42 |
#include "conf.h" |
43 |
|
44 |
tap_device::tap_device () |
45 |
{ |
46 |
struct ifreq ifr; |
47 |
|
48 |
device = DEFAULT_DEVICE; |
49 |
|
50 |
fd = open (device, O_RDWR); |
51 |
|
52 |
if (fd < 0) |
53 |
{ |
54 |
slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); |
55 |
exit (1); |
56 |
} |
57 |
|
58 |
memset (&ifr, 0, sizeof (ifr)); |
59 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
60 |
|
61 |
if (conf.ifname) |
62 |
strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); |
63 |
else |
64 |
ifr.ifr_name[0] = 0; |
65 |
|
66 |
if (!ioctl (fd, TUNSETIFF, &ifr)) |
67 |
{ |
68 |
strncpy (ifrname, ifr.ifr_name, IFNAMSIZ); |
69 |
ifrname [IFNAMSIZ] = 0; |
70 |
} |
71 |
else |
72 |
{ |
73 |
slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); |
74 |
exit (1); |
75 |
} |
76 |
|
77 |
if (conf.ifpersist) |
78 |
if (ioctl (fd, TUNSETPERSIST, 1)) |
79 |
slog (L_WARN, _("cannot set persistent mode for device %s: %s"), ifrname, strerror (errno)); |
80 |
|
81 |
slog (L_DEBUG, _("%s is a %s"), device, info ()); |
82 |
} |
83 |
|
84 |
tap_device::~tap_device () |
85 |
{ |
86 |
close (fd); |
87 |
} |
88 |
|
89 |
tap_packet * |
90 |
tap_device::recv () |
91 |
{ |
92 |
tap_packet *pkt = new tap_packet; |
93 |
|
94 |
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
95 |
|
96 |
if (pkt->len <= 0) |
97 |
{ |
98 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
99 |
info (), DEFAULT_DEVICE, strerror (errno)); |
100 |
free (pkt); |
101 |
return 0; |
102 |
} |
103 |
|
104 |
return pkt; |
105 |
} |
106 |
|
107 |
void |
108 |
tap_device::send (tap_packet *pkt) |
109 |
{ |
110 |
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
111 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
112 |
strerror (errno)); |
113 |
} |
114 |
|