diff --git a/Dockerfile b/Dockerfile index 5035573..7501696 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,8 @@ RUN apt-get update && apt-get install -y \ traceroute \ nmap \ tcpdump \ + util-linux \ + bsdmainutils \ vim \ && rm -rf /var/lib/apt/lists/* diff --git a/README.md b/README.md index 70925d5..e9983f8 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,12 @@ services-xbee_net/ │ ├── entrypoint.sh # Entrypoint script for Docker container │ ├── get_connected_wifi_info.sh # Script to fetch connected Wi-Fi device information │ ├── health_check.sh # Script to perform health checks on the container -│ ├── setup_end_device.sh # Script to set up the XBee end device -│ ├── setup_host_device.sh # Script to set up the XBee host device -│ ├── test.sh # Script to run tests +| ├── debug/ # Directory for testing-related scripts and files +│ | ├── create_tap_device.sh # Create a tap device directly on an OS +│ | ├── destroy_tap_device.sh # Destroy a tap device directly on an OS +│ | ├── enable_host_macvlan.sh # Destroy a tap device directly on an OS +│ | ├── host_setup.sh # Script to set up Masquerade bridge docker0 network with active wifi +│ | ├── test.sh # Script to run tests ├── tests/ # Directory for testing-related scripts and files ├── xbnet/ # The xbnet source code directory ├── .env # Environment variables for the Docker setup @@ -46,7 +49,7 @@ services-xbee_net/ ├── docker-compose.yml # Main Docker Compose file ├── Dockerfile # Dockerfile to build the Docker image ├── README.md # Project documentation -└── supervisord.conf # Supervisor configuration file +└── run-single.sh # Script to set up env vars and start a docker service with a single xbnet node ``` ## Prerequisites @@ -64,47 +67,48 @@ cd services-xbee_net #### 2. Run a single xbnet node using the following cmd: -```bash -- sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router -- sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway -``` +- Router mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router` +- Gateway mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway` + + +#### 3. OR Manually build and Start the Docker Container + +##### NOTE: This will require the config `.env` to be manually modified, based on user needs. --- +- Configure a single xbnet net service using: -##### OPTIONAL: Config `.env` (NOTE: run-single.sh will configure this for you, you don't need to modify this manually): -```bash -# Configuration for xbnet0 -XBEE_PORT=/dev/ttyUSB0 -XBEE_BAUDRATE=230400 -XBEE_NET_SRC_IP=192.168.1.100 # Ensure this IP matches the network range -XBEE_NET_IFACE_NAME=xbnet0 + ```bash + # Configuration for xbnet0 + XBEE_PORT=/dev/ttyUSB0 + XBEE_BAUDRATE=230400 + XBEE_NET_SRC_IP=192.168.1.100 # Ensure this IP matches the network range + XBEE_NET_IFACE_NAME=xbnet0 -# Default Gateway -DEFAULT_GATEWAY=192.168.1.1 -``` + # Default Gateway + DEFAULT_GATEWAY=192.168.1.1 + ``` -##### b. Sample `.env-run-multiple` config: -```bash -# Configuration for xbnet0 (REQUIRED: minimum config to run a single xbnet node) -XBEE0_PORT=/dev/ttyUSB0 # The serial port for the XBee device -XBEE0_BAUDRATE=230400 # Baud rate for the XBee device -XBEE0_NET_SRC_IP=192.168.1.100 # Source IP for the XBee network -XBEE0_NET_IFACE_NAME=xbnet0 # Interface name for the XBee network +- Configure multiple xbnet net services using: -# Configuration for xbnet1 (OPTIONAL: only required for multi node xbnets) -XBEE1_PORT=/dev/ttyUSB1 -XBEE1_BAUDRATE=230400 -XBEE1_NET_SRC_IP=192.168.1.101 -XBEE1_NET_IFACE_NAME=xbnet1 + ```bash + # Configuration for xbnet0 (REQUIRED: minimum config to run a single xbnet node) + XBEE0_PORT=/dev/ttyUSB0 # The serial port for the XBee device + XBEE0_BAUDRATE=230400 # Baud rate for the XBee device + XBEE0_NET_SRC_IP=192.168.1.100 # Source IP for the XBee network + XBEE0_NET_IFACE_NAME=xbnet0 # Interface name for the XBee network -# Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node) -DEFAULT_GATEWAY=192.168.1.1 -``` + # Configuration for xbnet1 (OPTIONAL: only required for multi node xbnets) + XBEE1_PORT=/dev/ttyUSB1 + XBEE1_BAUDRATE=230400 + XBEE1_NET_SRC_IP=192.168.1.101 + XBEE1_NET_IFACE_NAME=xbnet1 + + # Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node) + DEFAULT_GATEWAY=192.168.1.1 + ``` --- - -#### 3. Build and Start the Docker Container - To build the Docker image and start the container, run: ```bash diff --git a/scripts/create_tap_device.sh b/scripts/debug/create_tap_device.sh similarity index 100% rename from scripts/create_tap_device.sh rename to scripts/debug/create_tap_device.sh diff --git a/scripts/destroy_tap_device.sh b/scripts/debug/destroy_tap_device.sh similarity index 100% rename from scripts/destroy_tap_device.sh rename to scripts/debug/destroy_tap_device.sh diff --git a/scripts/enable_host_macvlan.sh b/scripts/debug/enable_host_macvlan.sh similarity index 100% rename from scripts/enable_host_macvlan.sh rename to scripts/debug/enable_host_macvlan.sh diff --git a/scripts/host_setup.sh b/scripts/debug/host_setup.sh similarity index 100% rename from scripts/host_setup.sh rename to scripts/debug/host_setup.sh diff --git a/scripts/test.sh b/scripts/debug/test.sh similarity index 100% rename from scripts/test.sh rename to scripts/debug/test.sh diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index fbbb664..4f35b74 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -12,6 +12,7 @@ echo "Starting entrypoint.sh" +# Main loop loop() { while true; do # Check if the XBee device is connected @@ -21,7 +22,6 @@ loop() { cleanup continue fi - sleep 0.5 # Check if the network interface is up @@ -30,18 +30,15 @@ loop() { echo "Network interface $XBEE_NET_IFACE_NAME not found. Creating TAP interface..." create_tap_interface fi - sleep 0.5 # Log messages sent and received over xbnet log_xbnet_messages - sleep 0.5 done } - -# Function to check if the XBee device is connected +# Check if the XBee device is connected check_device_port() { if [ -e "$XBEE_PORT" ]; then return 0 @@ -50,7 +47,7 @@ check_device_port() { fi } -# Function to check if the network interface is up +# Check if the network interface is up check_network_state() { if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then return 0 @@ -59,7 +56,7 @@ check_network_state() { fi } -# Function to create a TAP network interface +# Create a TAP network interface create_tap_interface() { # Start xbnet and run it in the background xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} & @@ -85,11 +82,10 @@ create_tap_interface() { return 0 } -# Function to log messages sent and received over xbnet using netstat +# Log incoming and outgoing messages from Xbees over xbnet log_xbnet_messages() { echo "Monitoring messages on $XBEE_NET_IFACE_NAME..." - # Continuously monitor the interface for packet statistics while true; do RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets) TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets) @@ -103,16 +99,54 @@ log_xbnet_messages() { TX_DIFF=$((TX_PACKETS_AFTER - TX_PACKETS_BEFORE)) if [[ $RX_DIFF -gt 0 ]]; then - echo "$(date +'%Y-%m-%d %H:%M:%S') - $RX_DIFF packets received on $XBEE_NET_IFACE_NAME" + for packet in $(seq 1 $RX_DIFF); do + SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address) + SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1) + + # Assuming destination info is available + DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1) + DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1) + + # Capture payload data (in hex format for simplicity) + PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1) + + echo "******************************************************************" + echo "$(date +'%Y-%m-%d %H:%M:%S')" + echo "$packet packets received on $XBEE_NET_IFACE_NAME" + echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }" + echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }" + echo "Length: $(echo "$PAYLOAD" | wc -c) bytes" + echo "Payload: $PAYLOAD" + echo "******************************************************************" + done fi if [[ $TX_DIFF -gt 0 ]]; then - echo "$(date +'%Y-%m-%d %H:%M:%S') - $TX_DIFF packets sent on $XBEE_NET_IFACE_NAME" + for packet in $(seq 1 $TX_DIFF); do + SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address) + SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1) + + # Assuming destination info is available + DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1) + DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1) + + # Capture payload data (in hex format for simplicity) + PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1) + + echo "******************************************************************" + echo "$(date +'%Y-%m-%d %H:%M:%S')" + echo "$packet packets sent on $XBEE_NET_IFACE_NAME" + echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }" + echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }" + echo "Length: $(echo "$PAYLOAD" | wc -c) bytes" + echo "Payload: $PAYLOAD" + echo "******************************************************************" + done fi done } -# Function to clean up resources and exit the script +# Clean up resources and exit the script cleanup() { echo "Cleaning up resources..." pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap" diff --git a/scripts/setup_end_device.sh b/scripts/setup_end_device.sh deleted file mode 100755 index 4147844..0000000 --- a/scripts/setup_end_device.sh +++ /dev/null @@ -1,16 +0,0 @@ -# Configure xbnet on remote device -sudo xbnet \ - /dev/ttyUSB0 \ # Port where the XBee device is connected \ - tun # Create a TUN (network tunnel) interface for routing IP packets - -sudo ip addr \ - add 192.168.100.2/24 \ # Assign the IP address 192.168.100.2 with a subnet mask of 255.255.255.0 \ - dev xbnet1 # to the xbnet1 interface - -sudo ip link \ - set dev xbnet1 up # Activate the xbnet1 interface - -# Add default route to use host machine as gateway -sudo ip route \ - add default \ # Add a default route for all outgoing traffic \ - via 192.168.100.1 # Use 192.168.100.1 (the host machine) as the gateway diff --git a/scripts/setup_host_device.sh b/scripts/setup_host_device.sh deleted file mode 100755 index dc30581..0000000 --- a/scripts/setup_host_device.sh +++ /dev/null @@ -1,45 +0,0 @@ -# Enable IP forwarding -sudo sysctl \ - -w # Write (or set) the value of a kernel parameter - net.ipv4.ip_forward=1 # Temporarily enable IP forwarding to forward network packets from one network interface to another - -# Configure NAT for the bridge -sudo iptables \ - -t nat # Specify the nat table used for Network Address Translation - -A POSTROUTING # Append this rule to the POSTROUTING chain, which alters packets as they are about to leave the network interface - -o wlp0s20f3 # Apply this rule to packets leaving the wlp0s20f3 interface (Wi-Fi interface) - -j MASQUERADE # Use the MASQUERADE target to change the source IP address to the IP address of the outgoing interface, enabling multiple devices on a private network to share a single public IP address - -# Configure xbnet and bridge interfaces -sudo xbnet \ - /dev/ttyUSB0 \ # Port where the XBee device is connected - tun # Create a TUN (network tunnel) interface for routing IP packets - -sudo ip addr \ - add 192.168.100.1/24 \ # Assign the IP address 192.168.100.1 with a subnet mask of 255.255.255.0 - dev xbnet0 # to the xbnet0 interface - -sudo ip link \ - set dev xbnet0 up # Activate the xbnet0 interface - -sudo brctl \ - addbr br0 # Create a new bridge interface named br0 - -sudo brctl \ - addif br0 wlp0s20f3 # Add the Wi-Fi interface wlp0s20f3 to the bridge br0 - -sudo brctl \ - addif br0 xbnet0 # Add the xbnet0 interface to the bridge br0 - -sudo ip link \ - set dev br0 up # Activate the bridge interface br0 - -sudo ip addr \ - flush dev wlp0s20f3 # Remove any IP addresses assigned to the wlp0s20f3 interface - -sudo ip addr \ - add 192.168.7.122/24 \ # Assign the IP address 192.168.7.122 with a subnet mask of 255.255.255.0 - dev br0 # to the bridge interface br0 - -sudo ip link \ - set dev wlp0s20f3 up # Reactivate the wlp0s20f3 interface