A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
emu-sock-creator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 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 <unistd.h>
20 #include <string>
21 #include <iostream>
22 #include <iomanip>
23 #include <sstream>
24 #include <stdlib.h>
25 #include <errno.h>
26 
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/ioctl.h>
30 #include <net/ethernet.h>
31 #include <net/if.h>
32 #include <netinet/in.h>
33 #include <netpacket/packet.h>
34 #include <arpa/inet.h>
35 
36 #include "emu-encode-decode.h"
37 
38 #define EMU_MAGIC 65867
39 
40 static int gVerbose = 0;
41 
42 #define LOG(msg) \
43  if (gVerbose) \
44  { \
45  std::cout << __FUNCTION__ << "(): " << msg << std::endl; \
46  }
47 
48 #define ABORT(msg, printErrno) \
49  std::cout << __FILE__ << ": fatal error at line " << __LINE__ << ": " << __FUNCTION__ << "(): " << msg << std::endl; \
50  if (printErrno) \
51  { \
52  std::cout << " errno = " << errno << " (" << strerror (errno) << ")" << std::endl; \
53  } \
54  exit (-1);
55 
56 #define ABORT_IF(cond, msg, printErrno) \
57  if (cond) \
58  { \
59  ABORT (msg, printErrno); \
60  }
61 
70 static void
71 SendSocket (const char *path, int fd)
72 {
73  //
74  // Open a Unix (local interprocess) socket to call back to the emu net
75  // device.
76  //
77  LOG ("Create Unix socket");
78  int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
79  ABORT_IF (sock == -1, "Unable to open socket", 1);
80 
81  //
82  // We have this string called path, which is really a hex representation
83  // of the endpoint that the net device created. It used a forward encoding
84  // method (EmuBufferToString) to take the sockaddr_un it made and passed
85  // the resulting string to us. So we need to take the inverse method
86  // (EmuStringToBuffer) and build the same sockaddr_un over here.
87  //
88  socklen_t clientAddrLen;
89  struct sockaddr_un clientAddr;
90 
91  LOG ("Decode address " << path);
92  bool rc = ns3::EmuStringToBuffer (path, (uint8_t *)&clientAddr, &clientAddrLen);
93  ABORT_IF (rc == false, "Unable to decode path", 0);
94 
95  LOG ("Connect");
96  int status = connect (sock, (struct sockaddr*)&clientAddr, clientAddrLen);
97  ABORT_IF (status == -1, "Unable to connect to emu device", 1);
98 
99  LOG ("Connected");
100 
101  //
102  // This is arcane enough that a few words are worthwhile to explain what's
103  // going on here.
104  //
105  // The interesting information (the socket FD) is going to go back to the
106  // emu net device as an integer of ancillary data. Ancillary data is bits
107  // that are not a part a socket payload (out-of-band data). We're also
108  // going to send one integer back. It's just initialized to a magic number
109  // we use to make sure that the emu device is talking to the emu socket
110  // creator and not some other creator process.
111  //
112  // The struct iovec below is part of a scatter-gather list. It describes a
113  // buffer. In this case, it describes a buffer (an integer) containing the
114  // data that we're going to send back to the emu net device (that magic
115  // number).
116  //
117  struct iovec iov;
118  uint32_t magic = EMU_MAGIC;
119  iov.iov_base = &magic;
120  iov.iov_len = sizeof(magic);
121 
122  //
123  // The CMSG macros you'll see below are used to create and access control
124  // messages (which is another name for ancillary data). The ancillary
125  // data is made up of pairs of struct cmsghdr structures and associated
126  // data arrays.
127  //
128  // First, we're going to allocate a buffer on the stack to contain our
129  // data array (that contains the socket). Sometimes you'll see this called
130  // an "ancillary element" but the msghdr uses the control message termimology
131  // so we call it "control."
132  //
133  size_t msg_size = sizeof(int);
134  char control[CMSG_SPACE (msg_size)];
135 
136  //
137  // There is a msghdr that is used to minimize the number of parameters
138  // passed to sendmsg (which we will use to send our ancillary data). This
139  // structure uses terminology corresponding to control messages, so you'll
140  // see msg_control, which is the pointer to the ancillary data and controllen
141  // which is the size of the ancillary data array.
142  //
143  // So, initialize the message header that describes our ancillary/control data
144  // and point it to the control message/ancillary data we just allocated space
145  // for.
146  //
147  struct msghdr msg;
148  msg.msg_name = 0;
149  msg.msg_namelen = 0;
150  msg.msg_iov = &iov;
151  msg.msg_iovlen = 1;
152  msg.msg_control = control;
153  msg.msg_controllen = sizeof (control);
154  msg.msg_flags = 0;
155 
156  //
157  // A cmsghdr contains a length field that is the length of the header and
158  // the data. It has a cmsg_level field corresponding to the originating
159  // protocol. This takes values which are legal levels for getsockopt and
160  // setsockopt (here SOL_SOCKET). We're going to use the SCM_RIGHTS type of
161  // cmsg, that indicates that the ancillary data array contains access rights
162  // that we are sending back to the emu net device.
163  //
164  // We have to put together the first (and only) cmsghdr that will describe
165  // the whole package we're sending.
166  //
167  struct cmsghdr *cmsg;
168  cmsg = CMSG_FIRSTHDR (&msg);
169  cmsg->cmsg_level = SOL_SOCKET;
170  cmsg->cmsg_type = SCM_RIGHTS;
171  cmsg->cmsg_len = CMSG_LEN (msg_size);
172  //
173  // We also have to update the controllen in case other stuff is actually
174  // in there we may not be aware of (due to macros).
175  //
176  msg.msg_controllen = cmsg->cmsg_len;
177 
178  //
179  // Finally, we get a pointer to the start of the ancillary data array and
180  // put our file descriptor in.
181  //
182  int *fdptr = (int*)(CMSG_DATA (cmsg));
183  *fdptr = fd; //
184 
185  //
186  // Actually send the file descriptor back to the emulated net device.
187  //
188  ssize_t len = sendmsg (sock, &msg, 0);
189  ABORT_IF (len == -1, "Could not send socket back to emu net device", 1);
190 
191  LOG ("sendmsg complete");
192 }
193 
194 int
195 main (int argc, char *argv[])
196 {
197  int c;
198  char *path = NULL;
199 
200  opterr = 0;
201 
202  while ((c = getopt (argc, argv, "vp:")) != -1)
203  {
204  switch (c)
205  {
206  case 'v':
207  gVerbose = true;
208  break;
209  case 'p':
210  path = optarg;
211  break;
212  }
213  }
214 
215  //
216  // This program is spawned by an emu net device running in a simulation. It
217  // wants to create a raw socket as described below. We are going to do the
218  // work here since we're running suid root. Once we create the raw socket,
219  // we have to send it back to the emu net device. We do that over a Unix
220  // (local interprocess) socket. The emu net device created a socket to
221  // listen for our response on, and it is expected to have encoded the address
222  // information as a string and to have passed that string as an argument to
223  // us. We see it here as the "path" string. We can't do anything useful
224  // unless we have that string.
225  //
226  ABORT_IF (path == NULL, "path is a required argument", 0);
227  LOG ("Provided path is \"" << path << "\"");
228  //
229  // The whole reason for all of the hoops we went through to call out to this
230  // program will pay off here. We created this program to run as suid root
231  // in order to keep the main simulation program from having to be run with
232  // root privileges. We need root privileges to be able to open a raw socket
233  // though. So all of these hoops are to allow us to exeucte the following
234  // single line of code:
235  //
236  LOG ("Creating raw socket");
237  int sock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
238  ABORT_IF (sock == -1, "CreateSocket(): Unable to open raw socket", 1);
239 
240  //
241  // Send the socket back to the emu net device so it can go about its business
242  //
243  SendSocket (path, sock);
244 
245  return 0;
246 }