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 [-p path] [-t tapdev] network/mask
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
;
58 struct in_addr NetAddress
;
59 struct in_addr NetMask
;
62 main(int ac
, char **av
)
64 const char *pathName
= "/dev/vknet";
65 const char *tapName
= "auto";
66 const char *bridgeName
= NULL
;
73 while ((c
= getopt(ac
, av
, "b:cdp:t:U")) != -1) {
101 * Special connect/debug mode
104 net_fd
= vknet_connect(pathName
);
109 vknet_monitor(net_fd
);
114 * In secure mode (the default), a network address/mask must be
115 * specified. e.g. 10.1.0.0/16. Any traffic going out the TAP
116 * interface will be filtered.
123 if (ac
== 0 || strchr(av
[0], '/') == NULL
)
126 if (inet_pton(AF_INET
, strtok(str
, "/"), &NetAddress
) <= 0)
128 masklen
= strtoul(strtok(NULL
, "/"), NULL
, 10);
129 mask
= (1 << (32 - masklen
)) - 1;
130 NetMask
.s_addr
= htonl(~mask
);
134 * Normal operation, create the tap/bridge and listener. This
135 * part is not threaded.
139 if ((tap_info
= vknet_tap(tapName
, bridgeName
)) == NULL
) {
143 if ((net_fd
= vknet_listener(pathName
)) < 0) {
144 perror("listener: ");
149 * Now make us a demon and start the threads going.
153 pthread_mutex_init(&BridgeMutex
, NULL
);
154 pthread_create(&dummy_td
, NULL
, vknet_io
, tap_info
);
155 vknet_acceptor(net_fd
);
160 #define TAPDEV_MINOR(x) ((int)((x) & 0xffff00ff))
163 vknet_tap(const char *tapName
, const char *bridgeName
)
166 struct ifaliasreq ifra
;
176 if (strcmp(tapName
, "auto") == 0) {
178 asprintf(&buf
, "/dev/tap%d", i
);
179 tap_fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
181 if (tap_fd
>= 0 || errno
== ENOENT
)
184 } else if (strncmp(tapName
, "tap", 3) == 0) {
185 asprintf(&buf
, "/dev/%s", tapName
);
186 tap_fd
= open(buf
, O_RDWR
| O_NONBLOCK
);
189 tap_fd
= open(tapName
, O_RDWR
| O_NONBLOCK
);
195 * Figure out the tap unit number
197 if (fstat(tap_fd
, &st
) < 0) {
201 tap_unit
= TAPDEV_MINOR(st
.st_rdev
);
206 fcntl(tap_fd
, F_SETFL
, 0);
207 bzero(&ifr
, sizeof(ifr
));
208 bzero(&ifra
, sizeof(ifra
));
209 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "tap%d", tap_unit
);
210 snprintf(ifra
.ifra_name
, sizeof(ifra
.ifra_name
), "tap%d", tap_unit
);
212 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
215 * Set the interface address if in Secure mode.
218 struct sockaddr_in
*in
;
220 in
= (void *)&ifra
.ifra_addr
;
221 in
->sin_family
= AF_INET
;
222 in
->sin_len
= sizeof(ifra
.ifra_addr
);
223 in
->sin_addr
= NetAddress
;
224 in
= (void *)&ifra
.ifra_mask
;
225 in
->sin_family
= AF_INET
;
226 in
->sin_len
= sizeof(ifra
.ifra_mask
);
227 in
->sin_addr
= NetMask
;
228 if (ioctl(s
, SIOCAIFADDR
, &ifra
) < 0) {
229 perror("Unable to set address on tap interface");
235 * Turn up the interface
238 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) >= 0) {
239 bzero(&ifr
, sizeof(ifr
));
240 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "tap%d", tap_unit
);
241 ifr
.ifr_flags
|= flags
& 0xFFFF;
242 ifr
.ifr_flagshigh
|= flags
>> 16;
243 if (ioctl(s
, SIOCSIFFLAGS
, &ifr
) < 0) {
244 perror("Unable to set IFF_UP on tap interface");
254 * Create the bridge if necessary.
256 bzero(&ifr
, sizeof(ifr
));
257 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s", bridgeName
);
258 if (ioctl(s
, SIOCIFCREATE
, &ifr
) < 0) {
259 if (errno
!= EEXIST
) {
260 perror("Unable to create bridge interface");
267 * Add the tap interface to the bridge
269 bzero(&ifbr
, sizeof(ifbr
));
270 snprintf(ifbr
.ifbr_ifsname
, sizeof(ifbr
.ifbr_ifsname
),
273 bzero(&ifd
, sizeof(ifd
));
274 snprintf(ifd
.ifd_name
, sizeof(ifd
.ifd_name
), "%s", bridgeName
);
275 ifd
.ifd_cmd
= BRDGADD
;
276 ifd
.ifd_len
= sizeof(ifbr
);
277 ifd
.ifd_data
= &ifbr
;
279 if (ioctl(s
, SIOCSDRVSPEC
, &ifd
) < 0) {
280 if (errno
!= EEXIST
) {
281 perror("Unable to add tap ifc to bridge!");
288 info
= malloc(sizeof(*info
));
289 bzero(info
, sizeof(*info
));
298 vknet_listener(const char *pathName
)
300 struct sockaddr_un sunx
;
307 * Group access to our named unix domain socket.
309 if ((grp
= getgrnam("vknet")) == NULL
) {
310 fprintf(stderr
, "The 'vknet' group must exist\n");
319 snprintf(sunx
.sun_path
, sizeof(sunx
.sun_path
), "%s", pathName
);
320 len
= offsetof(struct sockaddr_un
, sun_path
[strlen(sunx
.sun_path
)]);
321 ++len
; /* include nul */
322 sunx
.sun_family
= AF_UNIX
;
325 net_fd
= socket(AF_UNIX
, SOCK_SEQPACKET
, 0);
329 if (bind(net_fd
, (void *)&sunx
, len
) < 0) {
333 if (listen(net_fd
, 1024) < 0) {
337 if (chown(pathName
, (uid_t
)-1, gid
) < 0) {
341 if (chmod(pathName
, 0660) < 0) {
350 vknet_acceptor(int net_fd
)
352 struct sockaddr_un sunx
;
359 sunx_len
= sizeof(sunx
);
360 rfd
= accept(net_fd
, (void *)&sunx
, &sunx_len
);
363 info
= malloc(sizeof(*info
));
364 bzero(info
, sizeof(*info
));
367 pthread_create(&dummy_td
, NULL
, vknet_io
, info
);
372 * This I/O thread implements the core of the bridging code.
383 pthread_detach(pthread_self());
386 * Assign as a bridge slot using our thread id.
388 pthread_mutex_lock(&BridgeMutex
);
389 bridge
= bridge_add(info
);
390 pthread_mutex_unlock(&BridgeMutex
);
393 * Read packet loop. Writing is handled by the bridge code.
395 pkt
= malloc(MAXPKT
);
396 while ((bytes
= read(info
->fd
, pkt
, MAXPKT
)) > 0) {
397 pthread_mutex_lock(&BridgeMutex
);
398 bridge_packet(bridge
, pkt
, bytes
);
399 pthread_mutex_unlock(&BridgeMutex
);
405 pthread_mutex_lock(&BridgeMutex
);
407 pthread_mutex_unlock(&BridgeMutex
);
418 vknet_connect(const char *pathName
)
420 struct sockaddr_un sunx
;
424 snprintf(sunx
.sun_path
, sizeof(sunx
.sun_path
), "%s", pathName
);
425 len
= offsetof(struct sockaddr_un
, sun_path
[strlen(sunx
.sun_path
)]);
426 ++len
; /* include nul */
427 sunx
.sun_family
= AF_UNIX
;
430 net_fd
= socket(AF_UNIX
, SOCK_SEQPACKET
, 0);
433 if (connect(net_fd
, (void *)&sunx
, len
) < 0) {
441 vknet_monitor(int net_fd
)
447 pkt
= malloc(MAXPKT
);
448 while ((bytes
= read(net_fd
, pkt
, MAXPKT
)) > 0) {
449 printf("%02x:%02x:%02x:%02x:%02x:%02x <- "
450 "%02x:%02x:%02x:%02x:%02x:%02x",
451 pkt
[0], pkt
[1], pkt
[2], pkt
[3], pkt
[4], pkt
[5],
452 pkt
[6], pkt
[7], pkt
[8], pkt
[9], pkt
[10], pkt
[11]);
453 for (i
= 12; i
< bytes
; ++i
) {
454 if (((i
- 12) & 15) == 0) {
457 printf(" %02x", pkt
[i
]);
471 fprintf(stderr
, "vknet [-p path] [-t tapdev] [-U] [network/bits]\n");
472 fprintf(stderr
, "network must be specified in default secure mode\n");