GNU/Linux AccesPoint Wifi

Objectiu: Volem crear una xarxa WIFI per interconnectar dispositius.

Ens caldran poques coses per poder crear una xarxa Wifi amb GNU/Linux.

El més important és que la interfícies que fem servir es pugui posar en mode MASTER i que estigui suportada pel paquet hostapd.

Sabrem si la nostra tarja wireless la podrem posar perquè sigui un punt d’accés.

iw list | grep "Supported interface modes" -A 8
 Supported interface modes:
                 * IBSS
                 * managed
                 * AP
                 * AP/VLAN
                 * WDS
                 * monitor
                 * P2P-client
                 * P2P-GO

Ha de poder suportar els modes AP i/o AP/VLAN.

(no confondre amb la comanda ‘iwlist’ que serveix per altres tasques’)

Després necessitarem configurar el paquet ‘hostapd’ en el fitxer hostapd.conf

interface=wlan0
ssid=wififree
channel=4
hw_mode=g
wpa=2
wpa_passphrase=wififree
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
ieee80211n=1
wmm_enabled=1
preamble=1

Només caldrà executar-lo:

hostapd -ddd hostapd.conf

Ara configurarem un petit servei de DHCP per poder atorgar IPs als nostres dispositius.
(Opcional)

Utilitzarem el ‘dnsmasq’ (hi ha altres opcions dependrà de la distro).

Fitxer dnsmasq.conf:

interface=wlan0
dhcp-range=192.168.0.100,192.168.0.150,1h

L’executarem amb:

dnsmasq -d -C dnsmasq.conf

Després ho podem complicar tant com vulguem (enrutaments, firewalls, etc…)

iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE

Exemple funcional RPI 3B+ amb Raspbian Bullseye

#!/bin/bash

cat >/etc/hostapd/hostapd.conf <<EOF
interface=wlan0
country_code=ES
ssid=nitcat-test
driver=nl80211

#Enable 802.11n
ieee80211n=1
require_ht=1
#ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40]

#2.4GHZ
channel=6
#hw_mode=g

#5GHZ
#hw_mode=a
#channel=36

#AC
ieee80211ac=1
require_vht=1
ieee80211d=0
ieee80211h=0

wpa=2
wpa_passphrase=.f4m1l14.C0ll.-
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
wme_enabled=1
wmm_enabled=1
max_num_sta=2
preamble=1
macaddr_acl=0
EOF

cat >/etc/udhcpd.conf <<EOF
start		192.168.14.100
end		192.168.14.199
interface	wlan0
opt	dns	8.8.8.8
opt	dns	8.8.4.4
option	subnet	255.255.255.0
opt	router	192.168.14.1
opt	wins	192.168.14.1
option	domain	tdf.nit.cat
option	lease	3600
EOF

case "$1" in
        start)
           echo "Iniciant: $0"
	   ip link set wlan0 down
	   systemctl stop wpa_supplicant
	   pkill -9 wpa_supplicant
	   ip link set wlan0 up
	   iwconfig wlan0 txpower 31
	   iw wlan0 set power_save off 
	   echo 1 > /proc/sys/net/ipv4/ip_forward

	   ip address add 192.168.14.1/24 dev wlan0

	   #Bridge Wifi + Eth (normalment no funciona per restriccions de firmware de les nics)
	   #ip link add br0 type bridge
           #ip link set br0 type bridge stp_state 0
	   #ip link set wlan0 master $BRIDGE
	   #ip link set eth0  master $BRIDGE
           #ip address add 192.168.14.1/24 dev br0
           #ip link set br0 up
   
	   udhcpd -S -I 192.168.14.1 /etc/udhcpd.conf
	   hostapd -d -B /etc/hostapd/hostapd.conf
           ;;
        stop)
           echo "Aturant: $0"
	   pkill hostapd
	   pkill udhcpd
	   ip address delete 192.168.14.1/24 dev wlan0

	   #Bridge
	   #ip link set wlan0 nomaster
	   #ip link set eth0  nomaster
	   #ip link delete br0 type bridge

	   ip link set wlan0 down
           ;;
        status)
           echo -n "Estat: $0 "
           ;;
        restart)
           $0 stop
           $0 start
           ;;
         *)
           echo "$0"
           echo "$0 {start|stop|restart|status}"
           exit 1
esac