9271
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* Copyright (c) 2012 INRIA, 2012 University of Washington
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License version 2 as
|
|
7 |
* published by the Free Software Foundation;
|
|
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 "tap-fd-net-device-helper.h"
|
|
20 |
#include "encode-decode.h"
|
|
21 |
|
|
22 |
#include "ns3/abort.h"
|
|
23 |
#include "ns3/config.h"
|
|
24 |
#include "ns3/fd-net-device.h"
|
|
25 |
#include "ns3/log.h"
|
|
26 |
#include "ns3/names.h"
|
|
27 |
#include "ns3/object-factory.h"
|
|
28 |
#include "ns3/packet.h"
|
|
29 |
#include "ns3/simulator.h"
|
|
30 |
#include "ns3/trace-helper.h"
|
|
31 |
#include "ns3/internet-module.h"
|
|
32 |
|
|
33 |
#include <arpa/inet.h>
|
|
34 |
#include <errno.h>
|
|
35 |
#include <iostream>
|
|
36 |
#include <iomanip>
|
|
37 |
#include <limits>
|
|
38 |
#include <linux/if_tun.h>
|
|
39 |
#include <memory>
|
|
40 |
#include <net/ethernet.h>
|
|
41 |
#include <net/if.h>
|
|
42 |
#include <netinet/in.h>
|
|
43 |
#include <netpacket/packet.h>
|
|
44 |
|
|
45 |
#include <stdlib.h>
|
|
46 |
#include <string.h>
|
|
47 |
#include <sys/wait.h>
|
|
48 |
#include <sys/stat.h>
|
|
49 |
#include <sys/socket.h>
|
|
50 |
#include <sys/un.h>
|
|
51 |
#include <sys/ioctl.h>
|
|
52 |
#include <time.h>
|
|
53 |
#include <unistd.h>
|
|
54 |
|
|
55 |
#include <string>
|
|
56 |
|
|
57 |
NS_LOG_COMPONENT_DEFINE ("TapFdNetDeviceHelper");
|
|
58 |
|
|
59 |
namespace ns3 {
|
|
60 |
|
|
61 |
#define TAP_MAGIC 95549
|
|
62 |
|
|
63 |
TapFdNetDeviceHelper::TapFdNetDeviceHelper ()
|
|
64 |
{
|
|
65 |
m_deviceName = "";
|
|
66 |
m_modePi = false;
|
|
67 |
m_tapIp4 = "";
|
|
68 |
m_tapMask4 = "";
|
|
69 |
m_tapIp6 = "";
|
|
70 |
m_tapPrefix6 = 64;
|
|
71 |
m_tapMac = Mac48Address::Allocate ();
|
|
72 |
}
|
|
73 |
|
|
74 |
void
|
|
75 |
TapFdNetDeviceHelper::SetModePi (bool modePi)
|
|
76 |
{
|
|
77 |
m_modePi = modePi;
|
|
78 |
}
|
|
79 |
|
|
80 |
void
|
|
81 |
TapFdNetDeviceHelper::SetTapIpv4Address (Ipv4Address address)
|
|
82 |
{
|
|
83 |
m_tapIp4 = address;
|
|
84 |
}
|
|
85 |
|
|
86 |
void
|
|
87 |
TapFdNetDeviceHelper::SetTapIpv4Mask (Ipv4Mask mask)
|
|
88 |
{
|
|
89 |
m_tapMask4 = mask;
|
|
90 |
}
|
|
91 |
|
|
92 |
void
|
|
93 |
TapFdNetDeviceHelper::SetTapIpv6Address (Ipv6Address address)
|
|
94 |
{
|
|
95 |
m_tapIp6 = address;
|
|
96 |
}
|
|
97 |
|
|
98 |
void
|
|
99 |
TapFdNetDeviceHelper::SetTapIpv6Prefix (int prefix)
|
|
100 |
{
|
|
101 |
m_tapPrefix6 = prefix;
|
|
102 |
}
|
|
103 |
|
|
104 |
void
|
|
105 |
TapFdNetDeviceHelper::SetTapMacAddress (Mac48Address mac)
|
|
106 |
{
|
|
107 |
m_tapMac = mac;
|
|
108 |
}
|
|
109 |
|
|
110 |
Ptr<NetDevice>
|
|
111 |
TapFdNetDeviceHelper::InstallPriv (Ptr<Node> node) const
|
|
112 |
{
|
|
113 |
Ptr<NetDevice> d = FdNetDeviceHelper::InstallPriv (node);
|
|
114 |
Ptr<FdNetDevice> device = d->GetObject<FdNetDevice> ();
|
|
115 |
Ptr<FdNetDevice> fdnd = device->GetObject<FdNetDevice> ();
|
|
116 |
|
|
117 |
//
|
|
118 |
// We need to explicitly set the encapsulation mode for the traffic
|
|
119 |
// traversing the TAP device, so the FdNetDevice is able to know
|
|
120 |
// how to treat the traffic in a way that in compatible with the
|
|
121 |
// TAP device.
|
|
122 |
//
|
|
123 |
if (m_modePi)
|
|
124 |
{
|
|
125 |
fdnd->SetEncapsulationMode (FdNetDevice::DIXPI);
|
|
126 |
}
|
|
127 |
|
|
128 |
SetFileDescriptor (device);
|
|
129 |
return device;
|
|
130 |
}
|
|
131 |
|
|
132 |
void
|
|
133 |
TapFdNetDeviceHelper::SetFileDescriptor (Ptr<FdNetDevice> device) const
|
|
134 |
{
|
|
135 |
NS_LOG_LOGIC ("Creating TAP device");
|
|
136 |
|
|
137 |
//
|
|
138 |
// Call out to a separate process running as suid root in order to create a
|
|
139 |
// TAP device. We do this to avoid having the entire simulation running as root.
|
|
140 |
//
|
|
141 |
int fd = CreateFileDescriptor ();
|
|
142 |
device->SetFileDescriptor (fd);
|
|
143 |
}
|
|
144 |
|
|
145 |
int
|
|
146 |
TapFdNetDeviceHelper::CreateFileDescriptor (void) const
|
|
147 |
{
|
|
148 |
NS_LOG_FUNCTION (this);
|
|
149 |
|
|
150 |
#ifdef HAVE_TAP_CREATOR
|
|
151 |
//
|
|
152 |
// We're going to fork and exec that program soon, but first we need to have
|
|
153 |
// a socket to talk to it with. So we create a local interprocess (Unix)
|
|
154 |
// socket for that purpose.
|
|
155 |
//
|
|
156 |
int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
|
|
157 |
NS_ABORT_MSG_IF (sock == -1, "TapFdNetDeviceHelper::CreateFileDescriptor(): Unix socket creation error, errno = " << strerror (errno));
|
|
158 |
|
|
159 |
//
|
|
160 |
// Bind to that socket and let the kernel allocate an endpoint
|
|
161 |
//
|
|
162 |
struct sockaddr_un un;
|
|
163 |
memset (&un, 0, sizeof (un));
|
|
164 |
un.sun_family = AF_UNIX;
|
|
165 |
int status = bind (sock, (struct sockaddr*)&un, sizeof (sa_family_t));
|
|
166 |
NS_ABORT_MSG_IF (status == -1, "TapFdNetDeviceHelper::CreateFileDescriptor(): Could not bind(): errno = " << strerror (errno));
|
|
167 |
NS_LOG_INFO ("Created Unix socket");
|
|
168 |
NS_LOG_INFO ("sun_family = " << un.sun_family);
|
|
169 |
NS_LOG_INFO ("sun_path = " << un.sun_path);
|
|
170 |
|
|
171 |
//
|
|
172 |
// We have a socket here, but we want to get it there -- to the program we're
|
|
173 |
// going to exec. What we'll do is to do a getsockname and then encode the
|
|
174 |
// resulting address information as a string, and then send the string to the
|
|
175 |
// program as an argument. So we need to get the sock name.
|
|
176 |
//
|
|
177 |
socklen_t len = sizeof (un);
|
|
178 |
status = getsockname (sock, (struct sockaddr*)&un, &len);
|
|
179 |
NS_ABORT_MSG_IF (status == -1, "TapFdNetDeviceHelper::CreateFileDescriptor(): Could not getsockname(): errno = " << strerror (errno));
|
|
180 |
|
|
181 |
//
|
|
182 |
// Now encode that socket name (family and path) as a string of hex digits
|
|
183 |
//
|
|
184 |
std::string path = BufferToString ((uint8_t *)&un, len);
|
|
185 |
NS_LOG_INFO ("Encoded Unix socket as \"" << path << "\"");
|
|
186 |
|
|
187 |
//
|
|
188 |
// Fork and exec the process to create our socket. If we're us (the parent)
|
|
189 |
// we wait for the child (the creator) to complete and read the socket it
|
|
190 |
// created and passed back using the ancillary data mechanism.
|
|
191 |
//
|
|
192 |
pid_t pid = ::fork ();
|
|
193 |
if (pid == 0)
|
|
194 |
{
|
|
195 |
NS_LOG_DEBUG ("Child process");
|
|
196 |
|
|
197 |
//
|
|
198 |
// build a command line argument from the encoded endpoint string that
|
|
199 |
// the socket creation process will use to figure out how to respond to
|
|
200 |
// the (now) parent process. We're going to have to give this program
|
|
201 |
// quite a bit of information.
|
|
202 |
//
|
|
203 |
// -d<device-name> The name of the tap device we want to create;
|
|
204 |
// -m<MAC-address> The MAC-48 address to assign to the new tap device;
|
|
205 |
// -i<IPv4-address> The IP v4 address to assign to the new tap device;
|
|
206 |
// -I<IPv6-address> The IP v6 address to assign to the new tap device;
|
|
207 |
// -n<network-IPv4-mask> The network IPv4 mask to assign to the new tap device;
|
|
208 |
// -N<network-IPv6-mask> The network IPv6 mask to assign to the new tap device;
|
|
209 |
// -t Set teh IFF_TAP flag
|
|
210 |
// -h Set the IFF_NO_PI flag
|
|
211 |
// -p<path> the path to the unix socket described above.
|
|
212 |
//
|
|
213 |
// Example tap-creator -dnewdev -i1.2.3.1 -m08:00:2e:00:01:23 -n255.255.255.0 -t -h -pblah
|
|
214 |
//
|
|
215 |
|
|
216 |
//
|
|
217 |
// The device-name is something we may want the system to make up in
|
|
218 |
// every case. We also rely on it being configured via an Attribute
|
|
219 |
// through the helper. By default, it is set to the empty string
|
|
220 |
// which tells the system to make up a device name such as "tap123".
|
|
221 |
//
|
|
222 |
std::ostringstream ossDeviceName;
|
|
223 |
if (m_deviceName != "")
|
|
224 |
{
|
|
225 |
ossDeviceName << "-d" << m_deviceName;
|
|
226 |
}
|
|
227 |
|
|
228 |
std::ostringstream ossMac;
|
|
229 |
ossMac << "-m" << m_tapMac;
|
|
230 |
|
|
231 |
std::ostringstream ossIp4;
|
|
232 |
if (m_tapIp4 != "")
|
|
233 |
{
|
|
234 |
ossIp4 << "-i" << m_tapIp4;
|
|
235 |
}
|
|
236 |
|
|
237 |
std::ostringstream ossIp6;
|
|
238 |
if (m_tapIp6 != "")
|
|
239 |
{
|
|
240 |
ossIp6 << "-I" << m_tapIp6;
|
|
241 |
}
|
|
242 |
|
|
243 |
std::ostringstream ossNetmask4;
|
|
244 |
if (m_tapMask4 != "" )
|
|
245 |
{
|
|
246 |
ossNetmask4 << "-n" << m_tapMask4;
|
|
247 |
}
|
|
248 |
|
|
249 |
std::ostringstream ossPrefix6;
|
|
250 |
ossPrefix6 << "-P" << m_tapPrefix6;
|
|
251 |
|
|
252 |
std::ostringstream ossMode;
|
|
253 |
ossMode << "-t";
|
|
254 |
|
|
255 |
std::ostringstream ossPI;
|
|
256 |
if (m_modePi)
|
|
257 |
{
|
|
258 |
ossPI << "-h";
|
|
259 |
}
|
|
260 |
|
|
261 |
std::ostringstream ossPath;
|
|
262 |
ossPath << "-p" << path;
|
|
263 |
|
|
264 |
//
|
|
265 |
// Execute the socket creation process image.
|
|
266 |
//
|
|
267 |
status = ::execlp (TAP_CREATOR,
|
|
268 |
TAP_CREATOR, // argv[0] (filename)
|
|
269 |
ossDeviceName.str ().c_str (), // argv[1] (-d<device name>)
|
|
270 |
ossMac.str ().c_str (), // argv[2] (-m<MAC address>
|
|
271 |
ossIp4.str ().c_str (), // argv[3] (-i<IP v4 address>)
|
|
272 |
ossIp6.str ().c_str (), // argv[4] (-I<IP v6 address>)
|
|
273 |
ossNetmask4.str ().c_str (), // argv[5] (-n<IP v4 net mask>)
|
|
274 |
ossPrefix6.str ().c_str (), // argv[6] (-P<IP v6 prefix>)
|
|
275 |
ossMode.str ().c_str (), // argv[7] (-t <tap>)
|
|
276 |
ossPI.str ().c_str (), // argv[8] (-h <pi>)
|
|
277 |
ossPath.str ().c_str (), // argv[9] (-p<path>)
|
|
278 |
(char *)NULL);
|
|
279 |
|
|
280 |
//
|
|
281 |
// If the execlp successfully completes, it never returns. If it returns it failed or the OS is
|
|
282 |
// broken. In either case, we bail.
|
|
283 |
//
|
|
284 |
NS_FATAL_ERROR ("TapFdNetDeviceHelper::CreateFileDescriptor(): Back from execlp(), errno = " << ::strerror (errno));
|
|
285 |
}
|
|
286 |
else
|
|
287 |
{
|
|
288 |
NS_LOG_DEBUG ("Parent process");
|
|
289 |
//
|
|
290 |
// We're the process running the emu net device. We need to wait for the
|
|
291 |
// socket creator process to finish its job.
|
|
292 |
//
|
|
293 |
int st;
|
|
294 |
pid_t waited = waitpid (pid, &st, 0);
|
|
295 |
NS_ABORT_MSG_IF (waited == -1, "TapFdNetDeviceHelper::CreateFileDescriptor(): waitpid() fails, errno = " << strerror (errno));
|
|
296 |
NS_ASSERT_MSG (pid == waited, "TapFdNetDeviceHelper::CreateFileDescriptor(): pid mismatch");
|
|
297 |
|
|
298 |
//
|
|
299 |
// Check to see if the socket creator exited normally and then take a
|
|
300 |
// look at the exit code. If it bailed, so should we. If it didn't
|
|
301 |
// even exit normally, we bail too.
|
|
302 |
//
|
|
303 |
if (WIFEXITED (st))
|
|
304 |
{
|
|
305 |
int exitStatus = WEXITSTATUS (st);
|
|
306 |
NS_ABORT_MSG_IF (exitStatus != 0,
|
|
307 |
"TapFdNetDeviceHelper::CreateFileDescriptor(): socket creator exited normally with status " << exitStatus);
|
|
308 |
}
|
|
309 |
else
|
|
310 |
{
|
|
311 |
NS_FATAL_ERROR ("TapFdNetDeviceHelper::CreateFileDescriptor(): socket creator exited abnormally");
|
|
312 |
}
|
|
313 |
|
|
314 |
//
|
|
315 |
// At this point, the socket creator has run successfully and should
|
|
316 |
// have created our tap device, initialized it with the information we
|
|
317 |
// passed and sent it back to the socket address we provided. A socket
|
|
318 |
// (fd) we can use to talk to this tap device should be waiting on the
|
|
319 |
// Unix socket we set up to receive information back from the creator
|
|
320 |
// program. We've got to do a bunch of grunt work to get at it, though.
|
|
321 |
//
|
|
322 |
// The struct iovec below is part of a scatter-gather list. It describes a
|
|
323 |
// buffer. In this case, it describes a buffer (an integer) that will
|
|
324 |
// get the data that comes back from the socket creator process. It will
|
|
325 |
// be a magic number that we use as a consistency/sanity check.
|
|
326 |
//
|
|
327 |
struct iovec iov;
|
|
328 |
uint32_t magic;
|
|
329 |
iov.iov_base = &magic;
|
|
330 |
iov.iov_len = sizeof(magic);
|
|
331 |
|
|
332 |
//
|
|
333 |
// The CMSG macros you'll see below are used to create and access control
|
|
334 |
// messages (which is another name for ancillary data). The ancillary
|
|
335 |
// data is made up of pairs of struct cmsghdr structures and associated
|
|
336 |
// data arrays.
|
|
337 |
//
|
|
338 |
// First, we're going to allocate a buffer on the stack to receive our
|
|
339 |
// data array (that contains the socket). Sometimes you'll see this called
|
|
340 |
// an "ancillary element" but the msghdr uses the control message termimology
|
|
341 |
// so we call it "control."
|
|
342 |
//
|
|
343 |
size_t msg_size = sizeof(int);
|
|
344 |
char control[CMSG_SPACE (msg_size)];
|
|
345 |
|
|
346 |
//
|
|
347 |
// There is a msghdr that is used to minimize the number of parameters
|
|
348 |
// passed to recvmsg (which we will use to receive our ancillary data).
|
|
349 |
// This structure uses terminology corresponding to control messages, so
|
|
350 |
// you'll see msg_control, which is the pointer to the ancillary data and
|
|
351 |
// controllen which is the size of the ancillary data array.
|
|
352 |
//
|
|
353 |
// So, initialize the message header that describes the ancillary/control
|
|
354 |
// data we expect to receive and point it to buffer.
|
|
355 |
//
|
|
356 |
struct msghdr msg;
|
|
357 |
msg.msg_name = 0;
|
|
358 |
msg.msg_namelen = 0;
|
|
359 |
msg.msg_iov = &iov;
|
|
360 |
msg.msg_iovlen = 1;
|
|
361 |
msg.msg_control = control;
|
|
362 |
msg.msg_controllen = sizeof (control);
|
|
363 |
msg.msg_flags = 0;
|
|
364 |
|
|
365 |
//
|
|
366 |
// Now we can actually receive the interesting bits from the tap
|
|
367 |
// creator process. Lots of pain to get four bytes.
|
|
368 |
//
|
|
369 |
ssize_t bytesRead = recvmsg (sock, &msg, 0);
|
|
370 |
NS_ABORT_MSG_IF (bytesRead != sizeof(int), "TapFdNetDeviceHelper::CreateFileDescriptor(): Wrong byte count from socket creator");
|
|
371 |
|
|
372 |
//
|
|
373 |
// There may be a number of message headers/ancillary data arrays coming in.
|
|
374 |
// Let's look for the one with a type SCM_RIGHTS which indicates it's the
|
|
375 |
// one we're interested in.
|
|
376 |
//
|
|
377 |
struct cmsghdr *cmsg;
|
|
378 |
for (cmsg = CMSG_FIRSTHDR (&msg); cmsg != NULL; cmsg = CMSG_NXTHDR (&msg, cmsg))
|
|
379 |
{
|
|
380 |
if (cmsg->cmsg_level == SOL_SOCKET
|
|
381 |
&& cmsg->cmsg_type == SCM_RIGHTS)
|
|
382 |
{
|
|
383 |
//
|
|
384 |
// This is the type of message we want. Check to see if the magic
|
|
385 |
// number is correct and then pull out the socket we care about if
|
|
386 |
// it matches
|
|
387 |
//
|
|
388 |
if (magic == TAP_MAGIC)
|
|
389 |
{
|
|
390 |
NS_LOG_INFO ("Got SCM_RIGHTS with correct magic " << magic);
|
|
391 |
int *rawSocket = (int*)CMSG_DATA (cmsg);
|
|
392 |
NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket);
|
|
393 |
return *rawSocket;
|
|
394 |
}
|
|
395 |
else
|
|
396 |
{
|
|
397 |
NS_LOG_INFO ("Got SCM_RIGHTS, but with bad magic " << magic);
|
|
398 |
}
|
|
399 |
}
|
|
400 |
}
|
|
401 |
NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
|
|
402 |
}
|
|
403 |
|
|
404 |
#else
|
|
405 |
|
|
406 |
NS_FATAL_ERROR ("TAP_CREATOR is not defined in your system.");
|
|
407 |
|
|
408 |
#endif /* HAVE_TAP_CREATOR */
|
|
409 |
|
|
410 |
}
|
|
411 |
|
|
412 |
} // namespace ns3
|
|
413 |
|
|
414 |
|