Skip to content

Instantly share code, notes, and snippets.

@madmo
Created November 22, 2012 22:51
Show Gist options
  • Save madmo/4133214 to your computer and use it in GitHub Desktop.
Save madmo/4133214 to your computer and use it in GitHub Desktop.
n9 PAN Networking
1. Remove network from the DisabledPlugins option in /etc/bluetooth/main.conf
2. start test-nap.py
3. Connect to pan
4. run tether.sh start to setup ip/dns/dhcp
#!/usr/bin/python
from __future__ import absolute_import, print_function, unicode_literals
import sys
import time
import dbus
from optparse import OptionParser, make_option
bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("org.bluez", "/"),
"org.bluez.Manager")
option_list = [
make_option("-i", "--device", action="store",
type="string", dest="dev_id"),
]
parser = OptionParser(option_list=option_list)
(options, args) = parser.parse_args()
if options.dev_id:
adapter_path = manager.FindAdapter(options.dev_id)
else:
adapter_path = manager.DefaultAdapter()
server = dbus.Interface(bus.get_object("org.bluez", adapter_path),
"org.bluez.NetworkServer")
service = "nap"
if (len(args) < 1):
bridge = "tether"
else:
bridge = args[0]
server.Register(service, bridge)
print("Server for %s registered for %s" % (service, bridge))
print("Press CTRL-C to disconnect")
try:
time.sleep(1000)
print("Terminating connection")
except:
pass
server.Unregister(service)
#!/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
#--verbose \
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment