View Single Post
  #124  
Old 2011-04-03, 09:22
jschan jschan is offline
 
Join Date: May 2010
Posts: 166
Thanks!: 41
Thanked 74 Times in 45 Posts
Cool Re: [HOWTO] N900 Bluetooth-PAN Tethering (tested with a WiFi XOOM!)

I cleaned up my scripts a bit, added some documentation, and threw them into the attached tar file.

The version of the script here includes the following new features:
- Adds in support for multiple concurrent tethering clients (tested using a ThinkPad and a XOOM at the same time via bluetooth PAN)
- Adds in support for disabling tethering for a particular client
- Automatically picks the Internet interface (though it's pretty dumb about it)
- Works with included file to get dbus-scripts to automatically setup tethering on new connections
- Provides additional error checks and messages (though these could definitely use more work)

It's missing the pick-which-connection feature you have in the current version of your script, though I did try to make the entire script pretty verbose for easy modifications.

Outside of that, using the main script is very similar to your current script except that you have to specify whether you want to "start" or "stop" the tethering. The requirements and setup is pretty much what you have already written about in great detail in the first post.

Here's the script for each reference (it's also in the attached tar file):
Code:
#!/bin/sh
#
# /opt/tether/tether.sh
#
# Written by jschan @ http://talk.maemo.org
#
# Code snippets and inspiration from:
#   - http://wiki.maemo.org/DbusScripts
#   - http://talk.maemo.org/showthread.php?t=58570
#   - http://code.google.com/p/n900ipv6/source/browse/share/pan
#
# Enables tethering over USB network and Bluetooth PAN.
#
# usage: ./tether.sh start|stop [internal_interface]
#
# Examples:
#   - "./tether.sh start" - Enables tethering on a single PAN client when an Internet connection is active
#   - "./tether.sh stop" - Disables tethering on a single PAN client (assumes the client is on bnep0)
#   - "./tether.sh start bnep1" - Enables tethering on a 2nd PAN client when an Internet connection is active
#   - "./tether.sh stop bnep1" - Disables tethering on the 2nd PAN client, without interfering with the 1st
#
# Notes:
#   - Tested to work with multiple Bluetooth PAN clients including a WiFi-only XOOM
#   - Tested to work in conjuction with a dbus-scripts to automate tethering setups
#   - Doesn't currently have support for multiple concurrent Internet interfaces
#     such as an IPv4 grps connection and a seperate IPv6 grps connection.
#   - IPv6 isn't supported yet... (Waiting for T-Mobile IPv6 activation)
#

DEFAULT_INTERFACE="bnep0"
PAN_NETWORK_PREFIX="10.20"
USB_NETWORK_PREFIX="10.30"
INTERFACE_OCTET="88"
DHCP_MIN_IP_OCTET="100"
DHCP_MAX_IP_OCTET="127"
DHCP_MAX_LEASE_TIME="1h"
PRIMARY_DNS="8.8.8.8"
SECONDARY_DNS="8.8.4.4"

if [ $# -lt 1 ] || [ $# -gt 2 ]; then
	echo "usage: $0 start|stop [internal_interface]"
	exit 1
fi

if [ -z "$2" ]; then
	INTERFACE="$DEFAULT_INTERFACE"
else
	INTERFACE="$2"
fi

print_message () {
	echo "$(date) $*"
       	run-standalone.sh dbus-send \
		--type=method_call \
		--dest=org.freedesktop.Notifications \
		/org/freedesktop/Notifications \
		org.freedesktop.Notifications.SystemNoteInfoprint \
		string:"$*"
}
#set -x

INTERFACE_TYPE="$(echo $INTERFACE | sed 's/\([a-z]*\)[0-9]*/\1/')"
INTERFACE_NUMBER="$(echo $INTERFACE | sed 's/[a-z]*\([0-9]*\)/\1/')"

case $INTERFACE_TYPE in
	bnep )
		NETWORK_PREFIX="$PAN_NETWORK_PREFIX.$INTERFACE_NUMBER"
	;;
	usb )
		NETWORK_PREFIX="$USB_NETWORK_PREFIX.$INTERFACE_NUMBER"
	;;
	* )
		# Notify no active Internet connection
		print_message "Unsupported interface type: $INTERFACE_TYPE"
		exit 1
	;;
esac

INTERFACE_IP="$NETWORK_PREFIX.$INTERFACE_OCTET"
NETMASK="255.255.255.0"
EXTERNAL_INTERFACE="$(ifconfig | awk '/(gprs|wlan)/ {print $1}' | head -1)"

DHCP_MIN_IP="$NETWORK_PREFIX.$DHCP_MIN_IP_OCTET"
DHCP_MAX_IP="$NETWORK_PREFIX.$DHCP_MAX_IP_OCTET"
DHCP_GATEWAY="$INTERFACE_IP"

MESSAGE="Tethering from $INTERFACE to $EXTERNAL_INTERFACE"
ACTIVATION_MSG="$MESSAGE activated"
DEACTIVATION_MSG="$MESSAGE deactivated"
DNSMASQ_PID_NAME="dnsmasq.$INTERFACE"
DNSMASQ_PID_FILE="/var/run/$DNSMASQ_PID_NAME.pid"

if [ "$1" == "start" ]; then
	INTERFACE_EXISTANCE_CHECK="$(ifconfig -a | grep $INTERFACE | awk '{print $1}')"
	if [ -z "$INTERFACE_EXISTANCE_CHECK" ]; then
		# Notify no active Internet connection
		print_message "$INTERFACE does not exist"
		exit 1
	fi

	if [ -z "$EXTERNAL_INTERFACE" ]; then
		# Notify no active Internet connection
		print_message "Tethering request from $INTERFACE recieved"
		exit 1
	fi

	# Setup interface
	ifconfig $INTERFACE down 2> /dev/null
	ifconfig $INTERFACE up
	ifconfig $INTERFACE $INTERFACE_IP netmask $NETMASK

	# Load modules
	modprobe ipt_MASQUERADE

	# flush old iptables rules
	iptables --flush
	iptables --table nat --flush

	# Setup NAT
	iptables --out-interface $EXTERNAL_INTERFACE \
		--table nat \
		--append POSTROUTING \
                --jump MASQUERADE

	# Enable IP forwarding
	#echo 1 > /proc/sys/net/ipv4/ip_forward
	echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
	echo 1 > /proc/sys/net/ipv4/conf/$EXTERNAL_INTERFACE/forwarding

	# Setup DNS and DHCP
	start-stop-daemon \
		--exec /usr/sbin/dnsmasq \
		--pidfile $DNSMASQ_PID_FILE \
		--start -- \
			--interface=$INTERFACE \
			--listen-address=$INTERFACE_IP \
			--except-interface=lo \
			--bind-interfaces \
			--pid-file=$DNSMASQ_PID_FILE \
			--dhcp-range=$DHCP_MIN_IP,$DHCP_MAX_IP,$DHCP_MAX_LEASE_TIME \
			--dhcp-option=1,$NETMASK \
			--dhcp-option=3,$DHCP_GATEWAY \
			--dhcp-option=6,$PRIMARY_DNS,$SECONDARY_DNS

	# Notify user bluetooth tethering is active
	print_message "$ACTIVATION_MSG"

elif [ "$1" == "stop" ]; then

	start-stop-daemon \
		--exec /usr/sbin/dnsmasq \
		--pidfile $DNSMASQ_PID_FILE \
		--stop

	# Bring down the interface
	ifconfig $INTERFACE down 2> /dev/null

	# Disable IP forwarding for tethered interface
	if [ -f "/proc/sys/net/ipv4/conf/$INTERFACE/forwarding" ]; then
		echo 0 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding 2> /dev/null
	fi

	# Notify user bluetooth tethering is inactive
	print_message "$DEACTIVATION_MSG"

	# Disable external interface IP forwarding if appropriate
	DNSMASQ_CHECK="$(ps -eaf | grep 'dnsmasq' | grep -v -e '127\.0\.0\.1' -e 'grep')"
	if [ -z "$DNSMASQ_CHECK" ]; then
		echo "$(date) No more clients tethered--disabling all forwarding"
		echo 0 > /proc/sys/net/ipv4/conf/$EXTERNAL_INTERFACE/forwarding
		echo 0 > /proc/sys/net/ipv4/ip_forward
	fi

else

	echo "unknown argument: $1"
	echo "usage: $0 start|stop [internal_interface]"
	exit 1

fi
If you want to install my scripts, just install dbus-scripts if you haven't already, untar the attached tar file into a new /opt/tether directory, copy the "tether-dbus-config" file into the /etc/dbus-scripts.d directory, and restart dbus-scripts.

Thanks again for all of you help getting me over the humps while I was working on this, 9000. I was thinking about returning this XOOM if I couldn't tether it to the n900, but I'm digging it now...
Attached Files
File Type: tar tether.tar (12.0 KB, 427 views)

Last edited by jschan; 2011-04-03 at 09:25.
Reply With Quote
The Following 9 Users Say Thank You to jschan For This Useful Post: