2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/usr.sbin/vknetd/vknetd.c,v 1.2 2008/06/02 20:03:22 dillon Exp $
37 * vknet [-cdU] [-b bridgeN] [-p socket_path] [-t tapN] [address/cidrbits]
39 * Create a named unix-domain socket which userland vkernels can open
40 * to gain access to a local network. All connections to the socket
41 * are bridged together and the local network can also be bridged onto
42 * a TAP interface by specifying the -t option.
46 static ioinfo_t vknet_tap(const char *tapName, const char *bridgeName);
47 static int vknet_listener(const char *pathName);
48 static void vknet_acceptor(int net_fd);
49 static void *vknet_io(void *arg);
50 static int vknet_connect(const char *pathName);
51 static void vknet_monitor(int net_fd);
52 static void usage(void);
54 pthread_mutex_t BridgeMutex;
59 struct in_addr NetAddress;
60 struct in_addr NetMask;
63 main(int ac, char **av)
65 const char *pathName = "/var/run/vknet";
66 const char *tapName = "auto";
67 const char *bridgeName = NULL;
74 while ((c = getopt(ac, av, "b:cdp:t:U")) != -1) {
104 * Special connect/debug mode
107 net_fd = vknet_connect(pathName);
112 vknet_monitor(net_fd);
117 * In secure mode (the default), a network address/mask must be
118 * specified. e.g. 10.1.0.0/16. Any traffic going out the TAP
119 * interface will be filtered.
121 * If non-secure mode the network address/mask is optional.
123 if (SecureOpt || SetAddrOpt) {
128 if (ac == 0 || strchr(av[0], '/') == NULL)
131 if (inet_pton(AF_INET, strtok(str, "/"), &NetAddress) <= 0)
133 masklen = strtoul(strtok(NULL, "/"), NULL, 10);
134 mask = (1 << (32 - masklen)) - 1;
135 NetMask.s_addr = htonl(~mask);
139 * Normal operation, create the tap/bridge and listener. This
140 * part is not threaded.
144 if ((tap_info = vknet_tap(tapName, bridgeName)) == NULL) {
148 if ((net_fd = vknet_listener(pathName)) < 0) {
149 perror("listener: ");
154 * Now make us a demon and start the threads going.
158 pthread_mutex_init(&BridgeMutex, NULL);
159 pthread_create(&dummy_td, NULL, vknet_io, tap_info);
160 vknet_acceptor(net_fd);
165 #define TAPDEV_MINOR(x) ((int)((x) & 0xffff00ff))
168 vknet_tap(const char *tapName, const char *bridgeName)
171 struct ifaliasreq ifra;
181 if (strcmp(tapName, "auto") == 0) {
183 asprintf(&buf, "/dev/tap%d", i);
184 tap_fd = open(buf, O_RDWR | O_NONBLOCK);
186 if (tap_fd >= 0 || errno == ENOENT)
189 } else if (strncmp(tapName, "tap", 3) == 0) {
190 asprintf(&buf, "/dev/%s", tapName);
191 tap_fd = open(buf, O_RDWR | O_NONBLOCK);
194 tap_fd = open(tapName, O_RDWR | O_NONBLOCK);
200 * Figure out the tap unit number
202 if (fstat(tap_fd, &st) < 0) {
206 tap_unit = TAPDEV_MINOR(st.st_rdev);
211 fcntl(tap_fd, F_SETFL, 0);
212 bzero(&ifr, sizeof(ifr));
213 bzero(&ifra, sizeof(ifra));
214 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tap%d", tap_unit);
215 snprintf(ifra.ifra_name, sizeof(ifra.ifra_name), "tap%d", tap_unit);
217 s = socket(AF_INET, SOCK_DGRAM, 0);
220 * Set the interface address if in Secure mode.
223 struct sockaddr_in *in;
225 in = (void *)&ifra.ifra_addr;
226 in->sin_family = AF_INET;
227 in->sin_len = sizeof(ifra.ifra_addr);
228 in->sin_addr = NetAddress;
229 in = (void *)&ifra.ifra_mask;
230 in->sin_family = AF_INET;
231 in->sin_len = sizeof(ifra.ifra_mask);
232 in->sin_addr = NetMask;
233 if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
234 perror("Unable to set address on tap interface");
240 * Turn up the interface
243 if (ioctl(s, SIOCGIFFLAGS, &ifr) >= 0) {
244 bzero(&ifr, sizeof(ifr));
245 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tap%d", tap_unit);
246 ifr.ifr_flags |= flags & 0xFFFF;
247 ifr.ifr_flagshigh |= flags >> 16;
248 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
249 perror("Unable to set IFF_UP on tap interface");
259 * Create the bridge if necessary.
261 bzero(&ifr, sizeof(ifr));
262 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", bridgeName);
263 if (ioctl(s, SIOCIFCREATE, &ifr) < 0) {
264 if (errno != EEXIST) {
265 perror("Unable to create bridge interface");
272 * Add the tap interface to the bridge
274 bzero(&ifbr, sizeof(ifbr));
275 snprintf(ifbr.ifbr_ifsname, sizeof(ifbr.ifbr_ifsname),
278 bzero(&ifd, sizeof(ifd));
279 snprintf(ifd.ifd_name, sizeof(ifd.ifd_name), "%s", bridgeName);
280 ifd.ifd_cmd = BRDGADD;
281 ifd.ifd_len = sizeof(ifbr);
282 ifd.ifd_data = &ifbr;
284 if (ioctl(s, SIOCSDRVSPEC, &ifd) < 0) {
285 if (errno != EEXIST) {
286 perror("Unable to add tap ifc to bridge!");
293 info = malloc(sizeof(*info));
294 bzero(info, sizeof(*info));
303 vknet_listener(const char *pathName)
305 struct sockaddr_un sunx;
312 * Group access to our named unix domain socket.
314 if ((grp = getgrnam("vknet")) == NULL) {
315 fprintf(stderr, "The 'vknet' group must exist\n");
324 snprintf(sunx.sun_path, sizeof(sunx.sun_path), "%s", pathName);
325 len = offsetof(struct sockaddr_un, sun_path[strlen(sunx.sun_path)]);
326 ++len; /* include nul */
327 sunx.sun_family = AF_UNIX;
330 net_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
334 if (bind(net_fd, (void *)&sunx, len) < 0) {
338 if (listen(net_fd, 1024) < 0) {
342 if (chown(pathName, (uid_t)-1, gid) < 0) {
346 if (chmod(pathName, 0660) < 0) {
355 vknet_acceptor(int net_fd)
357 struct sockaddr_un sunx;
364 sunx_len = sizeof(sunx);
365 rfd = accept(net_fd, (void *)&sunx, &sunx_len);
368 info = malloc(sizeof(*info));
369 bzero(info, sizeof(*info));
372 pthread_create(&dummy_td, NULL, vknet_io, info);
377 * This I/O thread implements the core of the bridging code.
388 pthread_detach(pthread_self());
391 * Assign as a bridge slot using our thread id.
393 pthread_mutex_lock(&BridgeMutex);
394 bridge = bridge_add(info);
395 pthread_mutex_unlock(&BridgeMutex);
398 * Read packet loop. Writing is handled by the bridge code.
400 pkt = malloc(MAXPKT);
401 while ((bytes = read(info->fd, pkt, MAXPKT)) > 0) {
402 pthread_mutex_lock(&BridgeMutex);
403 bridge_packet(bridge, pkt, bytes);
404 pthread_mutex_unlock(&BridgeMutex);
410 pthread_mutex_lock(&BridgeMutex);
412 pthread_mutex_unlock(&BridgeMutex);
423 vknet_connect(const char *pathName)
425 struct sockaddr_un sunx;
429 snprintf(sunx.sun_path, sizeof(sunx.sun_path), "%s", pathName);
430 len = offsetof(struct sockaddr_un, sun_path[strlen(sunx.sun_path)]);
431 ++len; /* include nul */
432 sunx.sun_family = AF_UNIX;
435 net_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
438 if (connect(net_fd, (void *)&sunx, len) < 0) {
446 vknet_monitor(int net_fd)
452 pkt = malloc(MAXPKT);
453 while ((bytes = read(net_fd, pkt, MAXPKT)) > 0) {
454 printf("%02x:%02x:%02x:%02x:%02x:%02x <- "
455 "%02x:%02x:%02x:%02x:%02x:%02x",
456 pkt[0], pkt[1], pkt[2], pkt[3], pkt[4], pkt[5],
457 pkt[6], pkt[7], pkt[8], pkt[9], pkt[10], pkt[11]);
458 for (i = 12; i < bytes; ++i) {
459 if (((i - 12) & 15) == 0) {
462 printf(" %02x", pkt[i]);
476 fprintf(stderr, "usage: vknet [-cdU] [-b bridgeN] [-p socket_path] [-t tapN] [address/cidrbits]\n");
477 fprintf(stderr, "address/cidrbits must be specified in default secure mode.\n");