Access point SliTaz 5.1

Sharing the connection of a SliTaz machine with other devices: over Wi-Fi, with the machine acting as an access point, or over a cable. The machine routes their traffic to its own connection and hides them behind its address (NAT). Everything here is done as root, with three tools: hostapd makes the access point, the BusyBox udhcpd gives addresses to the devices, and iptables does the NAT.

What you need

$ iw list | grep -A8 'Supported interface modes'
# spk-add hostapd iptables

iptables brings its kernel modules with it, in linux-lts-net. udhcpd is part of BusyBox. On SliTaz Base i486, use tazpkg get-install instead of spk-add.

In the examples, the connection to share is eth0, the access point is wlan0, and the devices get addresses in 192.168.50.x. Change the names to yours (ip link lists them), and pick a network that differs from the one of the connection you share.

Wi-Fi access point

Keep network.sh away from the card

/etc/init.d/network.sh must not use the Wi-Fi card at the same time. Use the cable for the machine's own connection:

# trame network config interface eth0
# trame network restart

This sets WIFI="no" in /etc/network.conf. When the wireless_tools package is installed (SliTaz Base has it), the restart also turns the Wi-Fi radio off with iwconfig; turn it on again:

# iwconfig wlan0 txpower on

hostapd

The package installs the full example as /etc/hostapd/hostapd.conf, with every option explained. Keep it aside and write a short one:

# mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.example

The new /etc/hostapd/hostapd.conf:

interface=wlan0
driver=nl80211
ctrl_interface=/var/run/hostapd
ssid=SliTaz
country_code=FR
hw_mode=g
channel=6
ieee80211n=1
wpa=2
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa_passphrase=a long key of your own

Make the file readable by root only, since it holds the key:

# chmod 600 /etc/hostapd/hostapd.conf

Address and DHCP

Give the card its address, then write /etc/udhcpd.conf for the devices. Its router and dns options tell them to go through this machine and which DNS server to use: here a public one, or the one in your /etc/resolv.conf.

# ip addr add 192.168.50.1/24 dev wlan0

/etc/udhcpd.conf:

start		192.168.50.20
end		192.168.50.200
interface	wlan0
lease_file	/var/lib/udhcpd.leases
pidfile		/var/run/udhcpd.pid
option	subnet	255.255.255.0
option	router	192.168.50.1
option	dns	9.9.9.9
option	lease	86400

NAT

Let the kernel forward the traffic, and hide the devices behind the address of eth0:

# echo 1 > /proc/sys/net/ipv4/ip_forward
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Start

# /etc/init.d/hostapd start
# /etc/init.d/udhcpd start

Connect a phone or a laptop to the SliTaz network. Check from this machine:

# hostapd_cli all_sta
# cat /var/lib/udhcpd.leases

hostapd_cli all_sta lists the connected devices; the lease file is binary, dumpleases -f /var/lib/udhcpd.leases prints it.

At every boot

The address and the NAT go at the end of /etc/init.d/local.sh:

ip addr add 192.168.50.1/24 dev wlan0
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

hostapd and udhcpd are started as services, in this order:

# trame service enable hostapd
# trame service enable udhcpd

local.sh runs before the services, so the address is there when udhcpd starts.

Sharing over a cable

The same, without hostapd: the machine is online by Wi-Fi (or by a USB stick) and shares it with a computer or a switch on its Ethernet port. Use eth0 where the Wi-Fi example has wlan0, and the Wi-Fi interface (or usb0, ppp0) in the iptables line:

# ip addr add 192.168.50.1/24 dev eth0
# ip link set eth0 up
# echo 1 > /proc/sys/net/ipv4/ip_forward
# iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

In /etc/udhcpd.conf, set interface eth0, then start udhcpd. At boot, the four commands go in local.sh and udhcpd in the services, as above.

dnsmasq instead of udhcpd

The dnsmasq package gives the addresses and also answers the DNS requests of the devices, with a cache, forwarding them to the servers of /etc/resolv.conf. Use it instead of udhcpd, not with it. Install it, keep its example file aside and write a short /etc/dnsmasq.conf:

# spk-add dnsmasq
# mv /etc/dnsmasq.conf /etc/dnsmasq.conf.example
interface=wlan0
dhcp-range=192.168.50.20,192.168.50.200,24h
dhcp-leasefile=/var/lib/dnsmasq.leases

The devices then use this machine as their gateway and DNS server. Start it with /etc/init.d/dnsmasq start, or trame service enable dnsmasq for every boot.

Stop sharing

# /etc/init.d/udhcpd stop
# /etc/init.d/hostapd stop
# iptables -t nat -F POSTROUTING
# echo 0 > /proc/sys/net/ipv4/ip_forward
# ip addr del 192.168.50.1/24 dev wlan0

and trame service disable for the services started at boot.

Troubleshooting

hostapd does not start: run it in the terminal to see why:

# hostapd -d /etc/hostapd/hostapd.conf

nl80211: Could not configure driver mode: wpa_supplicant still holds the card; check WIFI="no" in /etc/network.conf, then killall wpa_supplicant. rfkill: WLAN soft blocked: the radio is off, see Wi-Fi from the console. A channel or a mode refused: try another channel of the 2.4 GHz band, or remove the ieee80211n line.

Devices connect, but get no address: udhcpd started before the card had its address, or on the wrong interface. Check ip addr show wlan0, then restart it.

An address, but no Internet: check the forwarding (cat /proc/sys/net/ipv4/ip_forward gives 1) and the NAT rule:

# iptables -t nat -L POSTROUTING -n -v

Its counters grow when devices go out. If the SliTaz firewall is on (IPTABLES_RULES="yes" in /etc/slitaz/firewall.conf), its rules drop the forwarded traffic and the DHCP requests: add your own rules to /etc/slitaz/firewall.sh, or keep it off.

Everything stops after a network change: network.sh restart, trame network restart or trame wifi connect take the other interfaces down, and may turn the Wi-Fi radio off (see above). Turn the radio on, give the card its address again, restart hostapd and udhcpd; or reboot.