Tagged: Squid
Raspberry Pi as an ethernet to wifi bridge
So, here’s my account of how to set up your Raspberry Pi box as an ethernet bridge.
First of all, for those wondering: what is a bridged connection? Essentially this is a “see through” way of extending your network – in a bridge software decides which packets get fowarded on to the next segment of the network. So for a wired to wireless bridge you can imagine it as an extension of the wired network into the wif-fi space. This is different from just being a router as we don’t need to, for instance, set up a new DHCP server or fix the routing table elsewhere in the network to ensure packets for the wireless connection are sent to your router and so on.
I was already using my Raspberry Pi box as Squid proxy server and I could have gone an extra step of making that “transparent” (ie forcing users to use that via the bridged connection) but that was a level of control too far! But it’s an option. To be honest the RasPi’s (this is a first series version, with less memory) performance as a Squid server seems to tend towards the mediocre, so while I think using a proxy is worth it for limiting the stress on a network like mine (we can have up to 10 devices at once with PCs and phones these days), it’s not fundamental.
Before you begin: The assumption here is that you have an ethernet connection already running and a wireless card (USB device) that supports “promiscuous” mode, meaning it can read packets that were not originally destined for it. I think most modern cards do that, but I am no expert. The assumption here is that you are running Raspian. If you have another distro adjust the suggestions appropriately.
First step:
sudo apt-get install aptitude – aptitude is just better than apt-get.
sudo aptitude install hostapd bridge-utils – you need these to get the wireless card and the bridge running
Then ensure that IP forwarding is on by editing /etc/sysctl.conf and removing the # from before the line that reads net.ipv4.ip_forward=1.
Second step:
Edit (i.e., create) /etc/hostapd/hostapd.conf – insert your SSID and probably don’t use WEP, which is close to useless…
interface=wlan0 bridge=br0 driver=nl80211 auth_algs=1 macaddr_acl=0 ignore_broadcast_ssid=0 logger_syslog=-1 logger_syslog_level=0 hw_mode=g ssid=YOUR_SSID channel=11 wep_default_key=0 wep_key0=YOUR_WEP_KEY wpa=0
Third step:
Edit your /etc/network/interfaces file … here is mine:
auto lo
iface lo inet loopback
iface eth0 inet static
address 0.0.0.0
auto br0
iface br0 inet dhcp
pre-up ip link set eth0 down
pre-up ip link set wlan0 down
pre-up brctl addbr br0
pre-up brctl addif br0 eth0 wlan0
pre-up ip addr flush dev eth0
pre-up ip addr flush dev wlan0
post-down ip link set eth0 down
post-down ip link set wlan0 down
post-down ip link set br0 down
post-down brctl delif br0 eth0 wlan0
post-down brctl delbr br0
(I had to force my eth0 to IP address 0.0.0.0 because of the behaviour of my DHCP server on the commodity router, you may not need to do that, but it is essential that neither your eth0 nor your wlan0 acquire an IP address.)
Fourth step:
Edit your /etc/rc.local file to ensure that the hostapd service is restarted after the bridge comes up. (For DHCP server reasons I also had to ensure my bridge was assigned an IP address – not sure you will need to do that, but it’s probably harmless)
# Setup bridge dhclient br0 /etc/init.d/hostapd restart
Fifth step:
Restart your box sudo shutdown -r now and if it doesn’t work, don’t blame me, blame the guy who gave me the answer here – http://superuser.com/questions/520172/wired-to-wireless-bridge-in-linux
But hopefully it will work…
Related articles
- Uses for a Raspberry Pi (cartesianproduct.wordpress.com)
- Bridging between two Ethernet cards (daniweb.com)
- Mesh networking with multiple Raspberry Pi boards (hackaday.com)
- RaspberryPi Secure VoIP access points with GNU SIP Witch (ubuntuwicohan.blogspot.com)
Some RegEx stuff
So, I was trying to match results from /proc/pid/stat which gives a line that looks like this:
2321 (squid) S 1 2321 2321 0 -1 4202752 4841 0 0 0 530 577 0 0 20 0 1 0 24716 36311040 4417 18446744073709551615 1 1 0 0 0 0 0 4096 85571 18446744073709551615 0 0 17 0 0 0 1945 0 0
And where the 10th entry is the number of minor faults and the 12th entry is the number of major faults (as you can see here, Squid has had 4841 minor faults and 0 major faults since it was restarted when I changed IP address).
So a RegEx seemed to be the way to go and my first attempt looked like this:
(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+\\s(\\S)+
But this did not work … well, it matched the line but it gave me bad results. [Note: \s matches whitespace, \S matches non-whitespace.]
I am sure you are all cleverer than me and saw the flaw straight away – but it took me some time to figure it out: (\\S)+ would treat only the first character of 4841 as a match - what I needed to use was (\\S+) which matched the group and not just the character.
And… further to my querying of the poorly written GNU RegEx documentation, the nmatch parameter should be one bigger than the number of groups expected to be matched – in the above case that means 13.
Related articles
- RegEx for Those Who Hunger for Regular Expressions (arnoldit.com)
- Testing Java Regular Expressions: A Useful Harness (singztechmusings.wordpress.com)
- RegName, Fast Regular Expression Based File Name Changer (ghacks.net)
- Unix Fight! – Sed, Grep, Awk, Cut and Pulling Groups out of a PowerShell Regular Expression Capture (hanselman.com)
- Regular Expression Library Website (ninetwentyoneblog.wordpress.com)
- Famous Perl One-Liners Explained, Part VII: Handy Regular Expressions (catonmat.net)
- Regex Toolkit, Prayer-Based Parsing, Bad Examples (chrisjwarwick.wordpress.com)
- XPath support for the Html Agility Pack on Windows Phone (socialebola.wordpress.com)
