# Router principale: VLAN e interfacce

Partiamo da un router con configurazione standard: `br-lan` contiene `lan1`–`lan4`, la rete `lan` ha IP `192.168.10.1`.

Obiettivo (vedi *Introduzione*):

| Porta | Ruolo |
|-------|-------|
| `lan1`, `lan2` | access Casa (10) |
| `lan3` | access Lavoro (20) |
| `lan4` | trunk verso l'AP: 10 untagged, 20/30/40 tagged |

## 1. VLAN sul bridge

```sh
# Casa (10)
uci add network bridge-vlan
uci set network.@bridge-vlan[-1].device='br-lan'
uci set network.@bridge-vlan[-1].vlan='10'
uci add_list network.@bridge-vlan[-1].ports='lan1:u*'
uci add_list network.@bridge-vlan[-1].ports='lan2:u*'
uci add_list network.@bridge-vlan[-1].ports='lan4:u*'

# Lavoro (20)
uci add network bridge-vlan
uci set network.@bridge-vlan[-1].device='br-lan'
uci set network.@bridge-vlan[-1].vlan='20'
uci add_list network.@bridge-vlan[-1].ports='lan3:u*'
uci add_list network.@bridge-vlan[-1].ports='lan4:t'

# IoT (30) e Ospiti (40): solo sul trunk
for v in 30 40; do
  uci add network bridge-vlan
  uci set network.@bridge-vlan[-1].device='br-lan'
  uci set network.@bridge-vlan[-1].vlan="$v"
  uci add_list network.@bridge-vlan[-1].ports='lan4:t'
done
```

## 2. Interfacce

```sh
# La LAN esistente passa sulla VLAN 10  ← FONDAMENTALE
uci set network.lan.device='br-lan.10'

# Nuove reti
for n in "work 20" "iot 30" "guest 40"; do
  set -- $n
  uci set network.$1=interface
  uci set network.$1.proto='static'
  uci set network.$1.device="br-lan.$2"
  uci set network.$1.ipaddr="192.168.$2.1"
  uci set network.$1.netmask='255.255.255.0'
done
```

## 3. DHCP

```sh
for n in work iot guest; do
  uci set dhcp.$n=dhcp
  uci set dhcp.$n.interface="$n"
  uci set dhcp.$n.start='100'
  uci set dhcp.$n.limit='100'
  uci set dhcp.$n.leasetime='12h'
done
uci set dhcp.guest.leasetime='2h'
```

## 4. Firewall

Senza zone le nuove reti non hanno né internet né accesso al DNS. Configura le zone come descritto nella pagina *Firewall tra le VLAN* **prima** di applicare, oppure subito dopo.

## 5. Controlla e applica

```sh
uci changes
```

Poi ripristino automatico (pagina *Prima di iniziare*) e:

```sh
uci commit
/etc/init.d/network restart
/etc/init.d/dnsmasq restart
/etc/init.d/firewall restart
```

> Al riavvio di dnsmasq possono comparire righe `udhcpc: no lease, failing`: dnsmasq verifica che sulle nuove reti non ci sia già un altro server DHCP. "No lease" significa che non ne ha trovati: va bene.

## 6. Verifica

```sh
bridge vlan show
ip -br addr | grep br-lan
```

Atteso:

```
port              vlan-id
lan4              10 PVID Egress Untagged
                  20
                  30
                  40
lan3              20 PVID Egress Untagged
lan2              10 PVID Egress Untagged
lan1              10 PVID Egress Untagged
br-lan            10
                  20
                  30
                  40
phy0-ap0          10 PVID Egress Untagged
```

Se tutto è a posto: `touch /tmp/ok`.

## Equivalente in LuCI

1. *Devices → br-lan → Configure → Bridge VLAN filtering*: abilita e compila la tabella porte/VLAN.
2. *Interfaces → lan → Edit*: Device = `br-lan.10`.
3. *Add new interface* per ogni rete: protocollo Static, device `br-lan.XX`, IP, scheda DHCP Server, scheda Firewall.
4. **Save** su ogni pagina, **Save & Apply** una sola volta alla fine.