2007-08-28 02:18:50 +02:00
|
|
|
/****************************************************************************
|
2014-09-30 18:44:32 +02:00
|
|
|
* arch/sim/src/up_tapdev.c
|
2007-08-28 02:18:50 +02:00
|
|
|
*
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011, 2016 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-08-28 02:18:50 +02:00
|
|
|
*
|
|
|
|
* Based on code from uIP which also has a BSD-like license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-09-28 16:05:46 +02:00
|
|
|
#ifndef __CYGWIN__
|
2007-08-31 01:57:58 +02:00
|
|
|
|
2007-08-28 02:18:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
#include <time.h>
|
2007-08-28 02:18:50 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
#ifdef CONFIG_SIM_NET_HOST_ROUTE
|
|
|
|
# include <net/route.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <linux/sockios.h>
|
2007-08-31 01:57:58 +02:00
|
|
|
#include <linux/if_tun.h>
|
2007-11-01 00:27:55 +01:00
|
|
|
#include <linux/net.h>
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
#include <netinet/in.h>
|
2007-08-28 02:18:50 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-09-30 18:44:32 +02:00
|
|
|
* Pre-processor Definitions
|
2007-08-28 02:18:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
//#define TAPDEV_DEBUG 1
|
2007-08-31 01:57:58 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
#define DEVTAP "/dev/net/tun"
|
2007-08-28 02:18:50 +02:00
|
|
|
|
2014-10-08 20:48:47 +02:00
|
|
|
/* Syslog priority (must match definitions in nuttx/include/syslog.h) */
|
|
|
|
|
|
|
|
#define LOG_INFO 1 /* Informational message */
|
|
|
|
#define LOG_ERR 4 /* Error conditions */
|
|
|
|
|
2007-08-28 02:18:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
/* Warning: This is very much Linux version specific! */
|
|
|
|
|
|
|
|
struct sel_arg_struct
|
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
fd_set *inp;
|
|
|
|
fd_set *outp;
|
|
|
|
fd_set *exp;
|
|
|
|
struct timeval *tvp;
|
|
|
|
};
|
|
|
|
|
2014-10-08 20:48:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* NuttX Domain Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int syslog(int priority, const char *format, ...);
|
|
|
|
int netdriver_setmacaddr(unsigned char *macaddr);
|
|
|
|
|
2007-08-28 02:18:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef TAPDEV_DEBUG
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
static int gdrop = 0;
|
|
|
|
#endif
|
|
|
|
static int gtapdevfd;
|
|
|
|
static char gdevname[IFNAMSIZ];
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIM_NET_HOST_ROUTE
|
|
|
|
static struct rtentry ghostroute;
|
2007-08-28 02:18:50 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
#ifdef TAPDEV_DEBUG
|
|
|
|
static inline void dump_ethhdr(const char *msg, unsigned char *buf, int buflen)
|
|
|
|
{
|
2014-10-08 20:48:47 +02:00
|
|
|
syslog(LOG_INFO, "TAPDEV: %s %d bytes\n", msg, buflen);
|
|
|
|
syslog(LOG_INFO,
|
|
|
|
" %02x:%02x:%02x:%02x:%02x:%02x %02x:%02x:%02x:%02x:%02x:%02x %02x%02x\n",
|
2013-01-28 22:55:16 +01:00
|
|
|
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
|
|
|
|
buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
|
2007-11-20 00:09:39 +01:00
|
|
|
#ifdef CONFIG_ENDIAN_BIG
|
2013-01-28 22:55:16 +01:00
|
|
|
buf[13], buf[12]);
|
2007-11-20 00:09:39 +01:00
|
|
|
#else
|
2013-01-28 22:55:16 +01:00
|
|
|
buf[12], buf[13]);
|
2007-10-31 01:13:07 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define dump_ethhdr(m,b,l)
|
|
|
|
#endif
|
|
|
|
|
2011-01-23 18:40:10 +01:00
|
|
|
static int up_setmacaddr(void)
|
|
|
|
{
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
unsigned char mac[7];
|
2011-01-23 18:40:10 +01:00
|
|
|
int ret = -1;
|
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
/* Assign a random locally-created MAC address.
|
|
|
|
*
|
|
|
|
* This previously took the address from the TAP interface; that was
|
|
|
|
* incorrect, as that hardware address belongs to the host system. Packets
|
|
|
|
* destined for the application aren't guaranteed to reach it if you do
|
|
|
|
* that, as the host may handle them at its discretion.
|
|
|
|
*
|
|
|
|
* With a unique MAC address, we get ALL the packets.
|
|
|
|
*
|
|
|
|
* TODO: The generated MAC address should be checked to see if it
|
|
|
|
* conflicts with something else on the network.
|
|
|
|
*/
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
mac[0] = 0x42;
|
|
|
|
mac[1] = rand() % 256;
|
|
|
|
mac[2] = rand() % 256;
|
|
|
|
mac[3] = rand() % 256;
|
|
|
|
mac[4] = rand() % 256;
|
|
|
|
mac[5] = rand() % 256;
|
|
|
|
mac[6] = 0;
|
|
|
|
|
|
|
|
ret = netdriver_setmacaddr(mac);
|
2011-01-23 18:40:10 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-28 02:18:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tapdev_init(void)
|
|
|
|
{
|
2007-11-01 00:27:55 +01:00
|
|
|
struct ifreq ifr;
|
2007-09-15 01:53:29 +02:00
|
|
|
int ret;
|
2007-08-28 02:18:50 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
#ifdef CONFIG_SIM_NET_BRIDGE
|
|
|
|
int sockfd;
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
/* Open the tap device */
|
|
|
|
|
2008-06-01 15:18:51 +02:00
|
|
|
gtapdevfd = open(DEVTAP, O_RDWR, 0644);
|
2007-09-15 01:53:29 +02:00
|
|
|
if (gtapdevfd < 0)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
2015-10-14 15:14:28 +02:00
|
|
|
syslog(LOG_ERR, "TAPDEV: open failed: %d\n", -gtapdevfd);
|
2007-08-28 02:18:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
/* Configure the tap device */
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2015-10-14 15:14:28 +02:00
|
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
2008-06-01 15:18:51 +02:00
|
|
|
ret = ioctl(gtapdevfd, TUNSETIFF, (unsigned long) &ifr);
|
2007-11-01 00:27:55 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-10-14 15:14:28 +02:00
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed: %d\n", -ret);
|
2007-11-01 00:27:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
/* Save the tap device name */
|
|
|
|
|
|
|
|
strncpy(gdevname, ifr.ifr_name, IFNAMSIZ);
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIM_NET_BRIDGE
|
|
|
|
/* Get a socket with which to manipulate the tap device; the remaining
|
|
|
|
* ioctl calls unfortunately won't work on the tap device fd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: Can't open socket: %d\n", -sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign the tap device to a bridge */
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, CONFIG_SIM_NET_BRIDGE_DEVICE, IFNAMSIZ);
|
|
|
|
ifr.ifr_ifindex = if_nametoindex(gdevname);
|
|
|
|
|
|
|
|
ret = ioctl(sockfd, SIOCBRADDIF, &ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed (can't add interface %s to bridge %s): %d\n",
|
2016-05-22 01:09:50 +02:00
|
|
|
gdevname, CONFIG_SIM_NET_BRIDGE_DEVICE, -ret);
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
}
|
2007-08-28 02:18:50 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
close(sockfd);
|
|
|
|
#endif
|
2007-11-01 00:27:55 +01:00
|
|
|
|
2011-01-23 18:40:10 +01:00
|
|
|
/* Set the MAC address */
|
2007-11-01 00:27:55 +01:00
|
|
|
|
2011-01-23 18:40:10 +01:00
|
|
|
up_setmacaddr();
|
2007-11-01 00:27:55 +01:00
|
|
|
}
|
|
|
|
|
2007-09-15 01:53:29 +02:00
|
|
|
unsigned int tapdev_read(unsigned char *buf, unsigned int buflen)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
2007-10-31 01:13:07 +01:00
|
|
|
fd_set fdset;
|
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
2007-08-28 02:18:50 +02:00
|
|
|
|
2007-09-15 01:53:29 +02:00
|
|
|
/* We can't do anything if we failed to open the tap device */
|
2007-10-31 01:13:07 +01:00
|
|
|
|
2007-09-15 01:53:29 +02:00
|
|
|
if (gtapdevfd < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
/* Wait for data on the tap device (or a timeout) */
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
2007-08-28 02:18:50 +02:00
|
|
|
tv.tv_usec = 1000;
|
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
2007-08-28 03:30:19 +02:00
|
|
|
FD_SET(gtapdevfd, &fdset);
|
2007-08-28 02:18:50 +02:00
|
|
|
|
2008-06-01 15:18:51 +02:00
|
|
|
ret = select(gtapdevfd + 1, &fdset, NULL, NULL, &tv);
|
2014-04-12 20:53:19 +02:00
|
|
|
if (ret == 0)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 15:18:51 +02:00
|
|
|
ret = read(gtapdevfd, buf, buflen);
|
2007-09-15 01:53:29 +02:00
|
|
|
if (ret < 0)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
2014-10-08 20:48:47 +02:00
|
|
|
syslog(LOG_ERR, "TAPDEV: read failed: %d\n", -ret);
|
2007-09-15 01:53:29 +02:00
|
|
|
return 0;
|
2007-08-28 02:18:50 +02:00
|
|
|
}
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
dump_ethhdr("read", buf, ret);
|
2007-08-28 02:18:50 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
void tapdev_send(unsigned char *buf, unsigned int buflen)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
#ifdef TAPDEV_DEBUG
|
2014-10-08 20:48:47 +02:00
|
|
|
syslog(LOG_INFO, "tapdev_send: sending %d bytes\n", buflen);
|
2007-08-28 02:18:50 +02:00
|
|
|
|
2007-08-28 03:30:19 +02:00
|
|
|
gdrop++;
|
2014-04-12 20:53:19 +02:00
|
|
|
if (gdrop % 8 == 7)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
2014-10-08 20:48:47 +02:00
|
|
|
syslog(LOG_ERR, "TAPDEV: Dropped a packet!\n");
|
2007-08-28 02:18:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-01 15:18:51 +02:00
|
|
|
ret = write(gtapdevfd, buf, buflen);
|
2007-09-15 01:53:29 +02:00
|
|
|
if (ret < 0)
|
2007-08-28 02:18:50 +02:00
|
|
|
{
|
2014-10-08 20:48:47 +02:00
|
|
|
syslog(LOG_ERR, "TAPDEV: write failed: %d", -ret);
|
2007-08-28 02:18:50 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
dump_ethhdr("write", buf, buflen);
|
2007-08-28 02:18:50 +02:00
|
|
|
}
|
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
void tapdev_ifup(in_addr_t ifaddr)
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIM_NET_HOST_ROUTE
|
|
|
|
struct sockaddr_in *addr;
|
|
|
|
#endif
|
2007-09-18 05:00:26 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
/* Get a socket with which to manipulate the tap device */
|
2007-09-18 05:00:26 +02:00
|
|
|
|
SUMMARY
-------
This patch enhances networking support for the simulation under Linux.
Includes updated support for Linux TUN/TAP, and the addition of support for
Linux bridge devices.
CHANGES
-------
o Check to see if the d_txavail callback is present before calling it in
the arp send code. This prevents a segfault when simulating the telnetd
daemon with arp send enabled.
o Adjust the simulation's netdriver_loop() so it will detect and respond to
ARP requests.
o Do not attempt to take the tap device's hardware address for use by the
simulation. That hardware address belongs to the host end of the link,
not the simulation end. Generate a randomized MAC address instead.
o Do not assign an IP address to the interface on the host side of the TAP
link.
+ Provide two modes: "host route" and "bridge".
+ In host route mode, maintain a host route that points any traffic for the
simulation's IP address to the tap device. In this mode, so long as the
simulation's IP is a free address in the same subnet as the host, no
additional configuration will be required to talk to it from the host.
Note that address changes are handled automatically if they follow the
rule of if-down/set-address/if-up, which everything seems to.
+ In bridge mode, add the tap device to the specified bridge instance. See
configs/sim/NETWORK-LINUX.txt for information and usage examples. This
enables much more flexible configurations (with fewer headaches), such as
running multiple simulations on a single host, all of which can access
the network the host is connected to.
o Refresh configurations in configs/sim where CONFIG_NET=y. They default
to "host route" mode.
o Add configs/sim/NETWORK-LINUX.txt
CAVEATS
-------
- The MAC address generation code is extremely simplistic, and does not
check for potential conflicts on the network. Probably not an issue, but
something to be aware of.
- I was careful to leave it in a state where Cygwin/pcap should still work,
but I don't have a Windows environment to test in. This should be
checked.
- I don't know if this was ever intended to work with OS X. I didn't even
try to test it there.
NOTES
-----
- Was able to get telnetd working and simulate nsh over telnet, but only so
long as listen backlogs were disabled.
There appears to be a bug in the backlog code where sockets are being
returned in SYN_RCVD state instead of waiting until they're ESTABLISHED;
if you perform an immediate send after accepting the connection, it will
confuse the stack and the send will hang; additionally, the connection
will never reach ESTABLISHED state.
Can be worked around by adding a sleep(1) after the accept in telnetd. I
don't have the necessary knowledge of the IP stack to know what the
correct fix is.
2016-05-21 01:36:14 +02:00
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: Can't open socket: %d\n", -sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bring the TAP interface up */
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, gdevname, IFNAMSIZ);
|
|
|
|
|
|
|
|
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed (can't get interface flags): %d\n", -ret);
|
|
|
|
close(sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifr.ifr_flags |= IFF_UP;
|
|
|
|
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed (can't set interface flags): %d\n", -ret);
|
|
|
|
close(sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SIM_NET_HOST_ROUTE
|
|
|
|
/* Add host route */
|
|
|
|
|
|
|
|
memset(&ghostroute, 0, sizeof(ghostroute));
|
|
|
|
|
|
|
|
addr = (struct sockaddr_in *)&ghostroute.rt_dst;
|
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
addr->sin_addr.s_addr = ifaddr;
|
|
|
|
|
|
|
|
ghostroute.rt_dev = gdevname;
|
|
|
|
ghostroute.rt_flags = RTF_UP | RTF_HOST;
|
|
|
|
ghostroute.rt_metric = 0;
|
|
|
|
|
|
|
|
ret = ioctl(sockfd, SIOCADDRT, (unsigned long)&ghostroute);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed (can't add host route): %d\n", -ret);
|
|
|
|
close(sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tapdev_ifdown(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SIM_NET_HOST_ROUTE
|
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (((struct sockaddr_in *)&ghostroute.rt_dst)->sin_addr.s_addr != 0)
|
|
|
|
{
|
|
|
|
/* Get a socket with which to manipulate the tap device */
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: Can't open socket: %d\n", -sockfd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(sockfd, SIOCDELRT, (unsigned long)&ghostroute);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "TAPDEV: ioctl failed (can't delete host route): %d\n", -ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__CYGWIN__ */
|