A Discrete-Event Network Simulator
API
tap-creator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
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 version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "tap-encode-decode.h"
19 
20 #include "ns3/mac48-address.h"
21 
22 #include <cerrno>
23 #include <cstdlib>
24 #include <cstring> // for strerror
25 #include <fcntl.h>
26 #include <iomanip>
27 #include <iostream>
28 #include <linux/if_tun.h>
29 #include <net/if.h>
30 #include <net/route.h>
31 #include <netinet/in.h>
32 #include <sstream>
33 #include <stdint.h>
34 #include <string>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/un.h>
39 #include <unistd.h>
40 
41 #define TAP_MAGIC 95549
42 
43 static bool gVerbose = false; // Set to true to turn on logging messages.
44 
45 #define LOG(msg) \
46  if (gVerbose) \
47  { \
48  std::cout << __FUNCTION__ << "(): " << msg << std::endl; \
49  }
50 
51 #define ABORT(msg, printErrno) \
52  std::cout << __FILE__ << ": fatal error at line " << __LINE__ << ": " << __FUNCTION__ \
53  << "(): " << msg << std::endl; \
54  if (printErrno) \
55  { \
56  std::cout << " errno = " << errno << " (" << std::strerror(errno) << ")" << std::endl; \
57  } \
58  std::exit(-1);
59 
60 #define ABORT_IF(cond, msg, printErrno) \
61  if (cond) \
62  { \
63  ABORT(msg, printErrno); \
64  }
65 
66 static sockaddr
67 CreateInetAddress(uint32_t networkOrder)
68 {
69  union {
70  struct sockaddr any_socket;
71  struct sockaddr_in si;
72  } s;
73 
74  s.si.sin_family = AF_INET;
75  s.si.sin_port = 0; // unused
76  s.si.sin_addr.s_addr = htonl(networkOrder);
77  return s.any_socket;
78 }
79 
80 static void
81 SendSocket(const char* path, int fd)
82 {
83  //
84  // Open a Unix (local interprocess) socket to call back to the tap bridge
85  //
86  LOG("Create Unix socket");
87  int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
88  ABORT_IF(sock == -1, "Unable to open socket", 1);
89 
90  //
91  // We have this string called path, which is really a hex representation
92  // of the endpoint that the tap bridge created. It used a forward encoding
93  // method (TapBufferToString) to take the sockaddr_un it made and passed
94  // the resulting string to us. So we need to take the inverse method
95  // (TapStringToBuffer) and build the same sockaddr_un over here.
96  //
97  socklen_t clientAddrLen;
98  struct sockaddr_un clientAddr;
99 
100  LOG("Decode address " << path);
101  bool rc = ns3::TapStringToBuffer(path, (uint8_t*)&clientAddr, &clientAddrLen);
102  ABORT_IF(rc == false, "Unable to decode path", 0);
103 
104  LOG("Connect");
105  int status = connect(sock, (struct sockaddr*)&clientAddr, clientAddrLen);
106  ABORT_IF(status == -1, "Unable to connect to tap bridge", 1);
107 
108  LOG("Connected");
109 
110  //
111  // This is arcane enough that a few words are worthwhile to explain what's
112  // going on here.
113  //
114  // The interesting information (the socket FD) is going to go back to the
115  // tap bridge as an integer of ancillary data. Ancillary data is bits
116  // that are not a part a socket payload (out-of-band data). We're also
117  // going to send one integer back. It's just initialized to a magic number
118  // we use to make sure that the tap bridge is talking to the tap socket
119  // creator and not some other creator process (emu, specifically)
120  //
121  // The struct iovec below is part of a scatter-gather list. It describes a
122  // buffer. In this case, it describes a buffer (an integer) containing the
123  // data that we're going to send back to the tap bridge (that magic number).
124  //
125  struct iovec iov;
126  uint32_t magic = TAP_MAGIC;
127  iov.iov_base = &magic;
128  iov.iov_len = sizeof(magic);
129 
130  //
131  // The CMSG macros you'll see below are used to create and access control
132  // messages (which is another name for ancillary data). The ancillary
133  // data is made up of pairs of struct cmsghdr structures and associated
134  // data arrays.
135  //
136  // First, we're going to allocate a buffer on the stack to contain our
137  // data array (that contains the socket). Sometimes you'll see this called
138  // an "ancillary element" but the msghdr uses the control message termimology
139  // so we call it "control."
140  //
141  size_t msg_size = sizeof(int);
142  char control[CMSG_SPACE(msg_size)];
143 
144  //
145  // There is a msghdr that is used to minimize the number of parameters
146  // passed to sendmsg (which we will use to send our ancillary data). This
147  // structure uses terminology corresponding to control messages, so you'll
148  // see msg_control, which is the pointer to the ancillary data and controllen
149  // which is the size of the ancillary data array.
150  //
151  // So, initialize the message header that describes our ancillary/control data
152  // and point it to the control message/ancillary data we just allocated space
153  // for.
154  //
155  struct msghdr msg;
156  msg.msg_name = nullptr;
157  msg.msg_namelen = 0;
158  msg.msg_iov = &iov;
159  msg.msg_iovlen = 1;
160  msg.msg_control = control;
161  msg.msg_controllen = sizeof(control);
162  msg.msg_flags = 0;
163 
164  //
165  // A cmsghdr contains a length field that is the length of the header and
166  // the data. It has a cmsg_level field corresponding to the originating
167  // protocol. This takes values which are legal levels for getsockopt and
168  // setsockopt (here SOL_SOCKET). We're going to use the SCM_RIGHTS type of
169  // cmsg, that indicates that the ancillary data array contains access rights
170  // that we are sending back to the tap bridge.
171  //
172  // We have to put together the first (and only) cmsghdr that will describe
173  // the whole package we're sending.
174  //
175  struct cmsghdr* cmsg;
176  cmsg = CMSG_FIRSTHDR(&msg);
177  cmsg->cmsg_level = SOL_SOCKET;
178  cmsg->cmsg_type = SCM_RIGHTS;
179  cmsg->cmsg_len = CMSG_LEN(msg_size);
180  //
181  // We also have to update the controllen in case other stuff is actually
182  // in there we may not be aware of (due to macros).
183  //
184  msg.msg_controllen = cmsg->cmsg_len;
185 
186  //
187  // Finally, we get a pointer to the start of the ancillary data array and
188  // put our file descriptor in.
189  //
190  int* fdptr = (int*)(CMSG_DATA(cmsg));
191  *fdptr = fd; //
192 
193  //
194  // Actually send the file descriptor back to the tap bridge.
195  //
196  ssize_t len = sendmsg(sock, &msg, 0);
197  ABORT_IF(len == -1, "Could not send socket back to tap bridge", 1);
198 
199  LOG("sendmsg complete");
200 }
201 
202 static int
203 CreateTap(const char* dev,
204  const char* gw,
205  const char* ip,
206  const char* mac,
207  const char* mode,
208  const char* netmask)
209 {
210  //
211  // Creation and management of Tap devices is done via the tun device
212  //
213  int tap = open("/dev/net/tun", O_RDWR);
214  ABORT_IF(tap == -1, "Could not open /dev/net/tun", true);
215 
216  //
217  // Allocate a tap device, making sure that it will not send the tun_pi header.
218  // If we provide a null name to the ifr.ifr_name, we tell the kernel to pick
219  // a name for us (i.e., tapn where n = 0..255.
220  //
221  // If the device does not already exist, the system will create one.
222  //
223  struct ifreq ifr;
224  ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
225  strcpy(ifr.ifr_name, dev);
226  int status = ioctl(tap, TUNSETIFF, (void*)&ifr);
227  ABORT_IF(status == -1, "Could not allocate tap device", true);
228 
229  std::string tapDeviceName = (char*)ifr.ifr_name;
230  LOG("Allocated TAP device " << tapDeviceName);
231 
232  //
233  // Operating mode "2" corresponds to USE_LOCAL and "3" to USE_BRIDGE mode.
234  // This means that we expect that the user will have named, created and
235  // configured a network tap that we are just going to use. So don't mess
236  // up his hard work by changing anything, just return the tap fd.
237  //
238  if (std::string(mode) == "2" || std::string(mode) == "3")
239  {
240  LOG("Returning precreated tap ");
241  return tap;
242  }
243 
244  //
245  // Set the hardware (MAC) address of the new device
246  //
247  ifr.ifr_hwaddr.sa_family = 1; // this is ARPHRD_ETHER from if_arp.h
248  ns3::Mac48Address(mac).CopyTo((uint8_t*)ifr.ifr_hwaddr.sa_data);
249  status = ioctl(tap, SIOCSIFHWADDR, &ifr);
250  ABORT_IF(status == -1, "Could not set MAC address", true);
251  LOG("Set device MAC address to " << mac);
252 
253  int fd = socket(AF_INET, SOCK_DGRAM, 0);
254 
255  //
256  // Bring the interface up.
257  //
258  status = ioctl(fd, SIOCGIFFLAGS, &ifr);
259  ABORT_IF(status == -1, "Could not get flags for interface", true);
260  ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
261  status = ioctl(fd, SIOCSIFFLAGS, &ifr);
262  ABORT_IF(status == -1, "Could not bring interface up", true);
263  LOG("Device is up");
264 
265  //
266  // Set the IP address of the new interface/device.
267  //
268  ifr.ifr_addr = CreateInetAddress(ns3::Ipv4Address(ip).Get());
269  status = ioctl(fd, SIOCSIFADDR, &ifr);
270  ABORT_IF(status == -1, "Could not set IP address", true);
271  LOG("Set device IP address to " << ip);
272 
273  //
274  // Set the net mask of the new interface/device
275  //
276  ifr.ifr_netmask = CreateInetAddress(ns3::Ipv4Mask(netmask).Get());
277  status = ioctl(fd, SIOCSIFNETMASK, &ifr);
278  ABORT_IF(status == -1, "Could not set net mask", true);
279  LOG("Set device Net Mask to " << netmask);
280 
281  return tap;
282 }
283 
284 int
285 main(int argc, char* argv[])
286 {
287  int c;
288  char* dev = (char*)"";
289  char* gw = nullptr;
290  char* ip = nullptr;
291  char* mac = nullptr;
292  char* netmask = nullptr;
293  char* operatingMode = nullptr;
294  char* path = nullptr;
295 
296  opterr = 0;
297 
298  while ((c = getopt(argc, argv, "vd:g:i:m:n:o:p:")) != -1)
299  {
300  switch (c)
301  {
302  case 'd':
303  dev = optarg; // name of the new tap device
304  break;
305  case 'g':
306  gw = optarg; // gateway address for the new device
307  break;
308  case 'i':
309  ip = optarg; // ip address of the new device
310  break;
311  case 'm':
312  mac = optarg; // mac address of the new device
313  break;
314  case 'n':
315  netmask = optarg; // net mask for the new device
316  break;
317  case 'o':
318  operatingMode = optarg; // operating mode of tap bridge
319  break;
320  case 'p':
321  path = optarg; // path back to the tap bridge
322  break;
323  case 'v':
324  gVerbose = true;
325  break;
326  }
327  }
328 
329  //
330  // We have got to be able to coordinate the name of the tap device we are
331  // going to create and or open with the device that an external Linux host
332  // will use. If this name is provided we use it. If not we let the system
333  // create the device for us. This name is given in dev
334  //
335  LOG("Provided Device Name is \"" << dev << "\"");
336 
337  //
338  // We have got to be able to provide a gateway to the external Linux host
339  // so it can talk to the ns-3 network. This ip address is provided in
340  // gw.
341  //
342  ABORT_IF(gw == nullptr, "Gateway Address is a required argument", 0);
343  LOG("Provided Gateway Address is \"" << gw << "\"");
344 
345  //
346  // We have got to be able to assign an IP address to the tap device we are
347  // allocating. This address is allocated in the simulation and assigned to
348  // the tap bridge. This address is given in ip.
349  //
350  ABORT_IF(ip == nullptr, "IP Address is a required argument", 0);
351  LOG("Provided IP Address is \"" << ip << "\"");
352 
353  //
354  // We have got to be able to assign a Mac address to the tap device we are
355  // allocating. This address is allocated in the simulation and assigned to
356  // the bridged device. This allows packets addressed to the bridged device
357  // to appear in the Linux host as if they were received there.
358  //
359  ABORT_IF(mac == nullptr, "MAC Address is a required argument", 0);
360  LOG("Provided MAC Address is \"" << mac << "\"");
361 
362  //
363  // We have got to be able to assign a net mask to the tap device we are
364  // allocating. This mask is allocated in the simulation and given to
365  // the bridged device.
366  //
367  ABORT_IF(netmask == nullptr, "Net Mask is a required argument", 0);
368  LOG("Provided Net Mask is \"" << netmask << "\"");
369 
370  //
371  // We have got to know whether or not to create the TAP.
372  //
373  ABORT_IF(operatingMode == nullptr, "Operating Mode is a required argument", 0);
374  LOG("Provided Operating Mode is \"" << operatingMode << "\"");
375 
376  //
377  // This program is spawned by a tap bridge running in a simulation. It
378  // wants to create a socket as described below. We are going to do the
379  // work here since we're running suid root. Once we create the socket,
380  // we have to send it back to the tap bridge. We do that over a Unix
381  // (local interprocess) socket. The tap bridge created a socket to
382  // listen for our response on, and it is expected to have encoded the address
383  // information as a string and to have passed that string as an argument to
384  // us. We see it here as the "path" string. We can't do anything useful
385  // unless we have that string.
386  //
387  ABORT_IF(path == nullptr, "path is a required argument", 0);
388  LOG("Provided path is \"" << path << "\"");
389 
390  //
391  // The whole reason for all of the hoops we went through to call out to this
392  // program will pay off here. We created this program to run as suid root
393  // in order to keep the main simulation program from having to be run with
394  // root privileges. We need root privileges to be able to futz with the
395  // Tap device underlying all of this. So all of these hoops are to allow
396  // us to execute the following code:
397  //
398  LOG("Creating Tap");
399  int sock = CreateTap(dev, gw, ip, mac, operatingMode, netmask);
400  ABORT_IF(sock == -1, "main(): Unable to create tap socket", 1);
401 
402  //
403  // Send the socket back to the tap net device so it can go about its business
404  //
405  SendSocket(path, sock);
406 
407  return 0;
408 }
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
an EUI-48 address
Definition: mac48-address.h:46
void CopyTo(uint8_t buffer[6]) const
bool TapStringToBuffer(std::string s, uint8_t *buffer, uint32_t *len)
Convert string encoded by the inverse function (TapBufferToString) back into a byte buffer.
mac
Definition: third.py:92
static void SendSocket(const char *path, int fd)
Definition: tap-creator.cc:81
#define LOG(msg)
Definition: tap-creator.cc:45
static sockaddr CreateInetAddress(uint32_t networkOrder)
Definition: tap-creator.cc:67
static bool gVerbose
Definition: tap-creator.cc:43
#define ABORT_IF(cond, msg, printErrno)
Definition: tap-creator.cc:60
static int CreateTap(const char *dev, const char *gw, const char *ip, const char *mac, const char *mode, const char *netmask)
Definition: tap-creator.cc:203
#define TAP_MAGIC
Definition: tap-creator.cc:41