Sunday, April 13, 2008

Script to turn a WRT54GL into a 5-port switch

#!/bin/sh
# Script: Turn a default WRT54GL (with openwrt) wap into a 5-port router.
# Author: Jason Miller
# Status: Not done yet. Routing needs to be added.
# End Configuration:
# WAN PORT: 10.0.0.23
# Port 1: 192.168.1.1
# Port 2: 192.168.2.1
# Port 3: 192.168.3.1
# Port 4: 192.168.4.1
# NOTES:
# - It is a VERY good idea to disable the IPTables init script first.


# It's good to have stuff written to non-volatile files in the event that something
# gets dorked up and you need to reboot to see what happened.
OUTPUT=/etc/out.out # For output messages
ERROR=/etc/out.err # For error messages
NET=/etc/out.net # For network config after all is done

(
# Don't need these
ifconfig eth1 down
ifconfig br0 down
brctl delbr br0
ifconfig vlan0 down
ifconfig vlan1 down

# Re-initialize the switch
robocfg switch disable
robocfg vlans enable reset

# For whatever reason, the port labels on the WRT case
# do not match the internal IDs of each port in the switch.
# We are also doing VLANs 11-15 instead of 1-5 because I
# really didn't want to take the extra time to debug
# the use of VLAN 1 (which is reserved by the default
# configuration).
robocfg vlan 15 ports "4 5t"
robocfg vlan 11 ports "3 5t"
robocfg vlan 12 ports "2 5t"
robocfg vlan 13 ports "1 5t"
robocfg vlan 14 ports "0 5t"

# Enable the switch with its new port/vlan mappings.
robocfg switch enable

# Make is so that we can run 'ifconfig' on the vlan interfaces.
vconfig add eth0 11
vconfig add eth0 12
vconfig add eth0 13
vconfig add eth0 14
vconfig add eth0 15

# Assign some MAC addresses. Don't be stupid and use a reserved
# MAC address (like broadcast or multicast addresses).
ifconfig vlan15 hw ether 00:00:00:00:00:05
ifconfig vlan11 hw ether 00:00:00:00:00:01
ifconfig vlan12 hw ether 00:00:00:00:00:02
ifconfig vlan13 hw ether 00:00:00:00:00:03
ifconfig vlan14 hw ether 00:00:00:00:00:04

# Turn everything on.
ifconfig vlan15 10.0.0.23 netmask 255.255.255.224 up
ifconfig vlan11 192.168.1.1 netmask 255.255.255.0 up
ifconfig vlan12 192.168.2.1 netmask 255.255.255.0 up
ifconfig vlan13 192.168.3.1 netmask 255.255.255.0 up
ifconfig vlan14 192.168.4.1 netmask 255.255.255.0 up

# Write the current network configuration to file.
ifconfig >${NET} 2>&1

) >${OUTPUT} 2>${ERROR}