Sharing a 3G connection with Ubuntu

Seeing as I carry around a vast array of equipment, in my massive, 10Ton backpack, I normally have a 3G card at hand. If I'm visiting someone who doesn't have broadband themselves, or I'm sitting in a Coffee Shop with other laptop-lugging friends I might want to share my 3G connection with friends, via WiFi. (assuming I have a data bundle that month, or they understand the horrific 3G data pricing)

I wrote a little script to make this easy

  • It's clearly Atheros-specific, but I've included more generic commands in comments. Obviously interface names would need to be changed
  • I dial the 3G connection before I run this, and disconnect afterwards, but it would be trivial to change that...
  • My dnsmasq.conf contains only the line dhcp-range=10.42.42.10,10.42.42.254,12h
  • Dnsmasq is configured not to run on startup, via update-rc.d

/usr/local/sbin/3g-ap:

#!/bin/sh
/etc/dbus-1/event.d/25NetworkManager stop
# Atheros:
wlanconfig ath0 destroy
wlanconfig ath0 create wlandev wifi0 wlanmode ap
# Other:
#iwconfig ath1 mode master
iwconfig ath0 chan 3
iwconfig ath0 essid SR
iwconfig ath0 enc s:13Char-Passwd
ifconfig ath0 up 10.42.42.1 netmask 255.255.255.0
/etc/init.d/dnsmasq start
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -o ppp0 -s 10.42.42.0/24 -j MASQUERADE
sysctl -w net/ipv4/ip_forward=1
echo "Done - when finished, hit enter"
read ignoreme
/etc/init.d/dnsmasq stop
iptables -t nat -F POSTROUTING
sysctl -w net/ipv4/ip_forward=0
# Atheros:    
wlanconfig ath0 destroy
wlanconfig ath0 create wlandev wifi0 wlanmode sta
# Other:
#iwconfig ath0 mode managed
/etc/dbus-1/event.d/25NetworkManager start