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 (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0)) |
78 |
slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno)); |
79 |
|
80 |
slog (L_DEBUG, _("%s is a %s"), device, info ()); |
81 |
} |
82 |
|
83 |
tap_device::~tap_device () |
84 |
{ |
85 |
close (fd); |
86 |
} |
87 |
|
88 |
tap_packet * |
89 |
tap_device::recv () |
90 |
{ |
91 |
tap_packet *pkt = new tap_packet; |
92 |
|
93 |
pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); |
94 |
|
95 |
if (pkt->len <= 0) |
96 |
{ |
97 |
slog (L_ERR, _("error while reading from %s %s: %s"), |
98 |
info (), DEFAULT_DEVICE, strerror (errno)); |
99 |
free (pkt); |
100 |
return 0; |
101 |
} |
102 |
|
103 |
return pkt; |
104 |
} |
105 |
|
106 |
void |
107 |
tap_device::send (tap_packet *pkt) |
108 |
{ |
109 |
if (write (fd, &((*pkt)[0]), pkt->len) < 0) |
110 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
111 |
strerror (errno)); |
112 |
} |
113 |
|