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