Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.wifi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Install these tools

* iw
* wpa_supplicant
* hostapd

32-bit versions of these packages
* pkg-config libnl-3-dev libnl-genl-3-dev

In Ubuntu

sudo apt install iw hostapd pkg-config:i386 libnl-3-dev:i386 libnl-genl-3-dev:i386
sudo systemctl disable hostapd

Create wifi interface

cd net-tools
./net-setup.sh -c zwifi.conf -i zwifi

9 changes: 9 additions & 0 deletions hostapd-open.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generic settings
ieee80211d=1
wmm_enabled=1
country_code=FI
utf8_ssid=1

# Open AP
ssid=zephyr-open
channel=1
14 changes: 14 additions & 0 deletions hostapd-wpa2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generic settings
ieee80211n=1
wmm_enabled=1
country_code=FI
utf8_ssid=1
hw_mode=g

# WPA2 AP
ssid=zephyr-wpa2
channel=6
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_passphrase=password
2 changes: 2 additions & 0 deletions zwifi-dnsmasq-open.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
domain-needed
dhcp-range=192.0.2.32,192.0.2.64,255.255.255.0,1h
2 changes: 2 additions & 0 deletions zwifi-dnsmasq-wpa2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
domain-needed
dhcp-range=198.51.100.32,198.51.100.64,255.255.255.0,1h
89 changes: 89 additions & 0 deletions zwifi.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Configuration file for setting IP addresses for a wifi network interface.

INTERFACE="$1"
INTERFACE_AP="${INTERFACE}"

PID_FILE_WPA_SUPPLICANT=/var/run/zephyr-wpa_supplicant.pid
LOG_FILE_WPA_SUPPLICANT=/var/log/zephyr-wpa_supplicant.log

NUM_RADIOS=4

HWADDR="00:00:5e:00:53:00"

IPV6_ADDR_1="2001:db8::2"
IPV6_ROUTE_1="2001:db8::/64"

IPV4_ADDR_1="192.0.2.2/24"
IPV4_ROUTE_1="192.0.2.0/24"

# We do not want the default tun device as the hwsim will create one for us.
# As the tun device is created by net-setup.sh automatically, delete it here.
# Create it back in zwifi.conf.stop script in order to avoid error prints.
ip tuntap del "$INTERFACE" mode tap

modprobe mac80211_hwsim radios=${NUM_RADIOS}
if [ $? -ne 0 ]; then
exit 1
fi

# Rename wlan0 and wlan1 to something Zephyr specific for easier debugging
ip link set dev wlan0 down

# wlan0 is for station
ip link set dev wlan0 name "${INTERFACE}"
ip link set dev "${INTERFACE}" up
ip link set dev "${INTERFACE}" address ${HWADDR}

# Create a namespace that hostapd is running at
AP_NS=zwifi-ap

ip netns add $AP_NS

COUNT=0

# Then setup different modes of access points
for i in open wpa2
do
IFACE="${INTERFACE_AP}-$i"
WLAN="wlan$(($COUNT + 1))"

ip link set dev "$WLAN" down
ip link set dev "$WLAN" name "${IFACE}"
ip link set dev "${IFACE}" up

#HWADDR_AP="00:00:5e:00:53:$(printf '%02x' $((256 - $NUM_RADIOS + $COUNT)))"
#ip link set dev "${IFACE}" address ${HWADDR_AP}

PHY_AP=$(iw dev "${IFACE}" info | grep wiphy | awk '{print "phy"$2}')
if [ -z "$PHY_AP" ]; then
echo "Invalid phy $PHY_AP"
exit 1
fi

# Bind phy to our namespace
iw phy "$PHY_AP" set netns name $AP_NS

PID_FILE_HOSTAPD="/var/run/zephyr-hostapd-${i}.pid"
LOG_FILE_HOSTAPD="/var/log/zephyr-hostapd-${i}.log"
CONF_FILE_HOSTAPD="hostapd-${i}.conf"

ip netns exec $AP_NS hostapd -B -f "$LOG_FILE_HOSTAPD" \
-P "$PID_FILE_HOSTAPD" -t -i "${IFACE}" "$CONF_FILE_HOSTAPD"

PID_FILE_DNSMASQ="/var/run/zephyr-dnsmasq-${i}.pid"
CONF_FILE_DNSMASQ="zwifi-dnsmasq-${i}.conf"
DNSMASQ_PORT=$((5353 + $COUNT))

ip netns exec $AP_NS dnsmasq -C "$CONF_FILE_DNSMASQ" \
-z -x "$PID_FILE_DNSMASQ" -i "${IFACE}"

COUNT=$((COUNT + 1))
done

#ip netns exec $AP_NS ip -6 address add $IPV6_ADDR_1 dev "${INTERFACE_AP}" nodad
#ip netns exec $AP_NS ip -6 route add $IPV6_ROUTE_1 dev "${INTERFACE_AP}"
#ip netns exec $AP_NS ip address add $IPV4_ADDR_1 dev "${INTERFACE_AP}"
#ip netns exec $AP_NS ip route add $IPV4_ROUTE_1 dev "${INTERFACE_AP}" > /dev/null 2>&1

# Now everything is ready so that Zephyr wpa_supplicant can connect to the
# simulated wifi network.
23 changes: 23 additions & 0 deletions zwifi.conf.stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Configuration file for setting IP addresses for a wifi network interface.

INTERFACE="$1"

# Rename wlan0 to something we want
ip link set dev "$INTERFACE" down

rmmod mac80211_hwsim

for i in open wpa2
do
PID_FILE_HOSTAPD=/var/run/zephyr-hostapd-${i}.pid
PID_FILE_DNSMASQ="/var/run/zephyr-dnsmasq-${i}.pid"

kill $(cat "$PID_FILE_HOSTAPD")
kill $(cat "$PID_FILE_DNSMASQ")
done

AP_NS=zwifi-ap
ip netns delete $AP_NS

# The net-tools script expects this so create it
ip tuntap add "$INTERFACE" mode tap
1 change: 1 addition & 0 deletions zwifi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./net-setup.sh -c zwifi.conf -i zwifi