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