Friday, March 5, 2010

Linux Wireless "Bridge"

As pointed out by a visitor to this site, this is not a "bridge" in the true networking sense as a bridge is a layer 2 device that joins network segments and this is a layer 3 routing setup, but oddly it is still a common term for this sort of device. Technically this is a simple iptables based router, one of the interfaces just happens to be wireless.

---

This keeps coming in handy every so often, it's a simple script to turn a Linux laptop into a router that manages traffic between it's wired and wireless interfaces. Today I ended up using it to get networking to a machine in an environment where there were no available ports on the switch to plug into; previously it's been used to provide access to a LAN which had no access to the WAN on its own, but there was a local wireless network which did.

Run this after establishing a wireless connection and connecting to another machine or switch, it will setup the private Ethernet network, iptables rules, forwarding, and the dhcp server.

#!/bin/bash
echo "router build for public interface ath0, private interface eth0"
echo "--------------------------------------------------------------"
echo "setting up the wire"
ifconfig eth0 192.168.0.1 netmask 255.255.255.0
echo "building nat for 192.168.0.X network"

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o ath0 -j MASQUERADE

echo "setting routes between ethernet and wireless"
iptables -A FORWARD -s 192.168.0.0/24 -o ath0 -j ACCEPT
iptables -A FORWARD -d 192.168.0.0/24 -m state --state ESTABLISHED,RELATED -i ath0 -j ACCEPT

echo "saving"
sh -c "iptables-save > /etc/iptables.rules"

echo 1 > /proc/sys/net/ipv4/ip_forward

echo "success!"
echo "---------------------------------------"
echo "setting up dhcp for eth0"

echo "option domain-name-servers 4.2.2.2;" > /etc/dhcpd.conf

echo "default-lease-time 60;" >> /etc/dhcpd.conf
echo "max-lease-time 72;" >> /etc/dhcpd.conf

echo "ddns-update-style none;" >> /etc/dhcpd.conf
echo "authoritative;" >> /etc/dhcpd.conf
echo "log-facility local7;" >> /etc/dhcpd.conf

echo "subnet 192.168.0.0 netmask 255.255.255.0 {" >> /etc/dhcpd.conf
echo "  range 192.168.0.100 192.168.0.254;" >> /etc/dhcpd.conf
echo "  option routers 192.168.0.1;" >> /etc/dhcpd.conf
echo "  option domain-name-servers 4.2.2.2;" >> /etc/dhcpd.conf
echo "}" >> /etc/dhcpd.conf

dhcpd

echo "dhcpd server running for eth0 network"


So far this has been tested to setup a wireless bridge on Ubuntu 9.10 as well as  multiple Gentoo installs.

2 comments:

  1. Hey Bro:

    That's not actually a bridge you have there, its IP masquerading (NAT) on your eth0 and your wlan interface is the gateway....you"re doing it at layer 3. A bridge works at layer 2.

    Cheers,
    jim
    ReplyDelete
  2. You are absolutely correct, it is not a bridge in standard networking terms. It's really a router, not sure why that term got stuck in my head for it... Next time I go through and revise old posts I'll have to fix my terminology here. Thanks for pointing it out :)
    ReplyDelete