• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3IFNAME=$1
4CMD=$2
5
6kill_daemon() {
7    NAME=$1
8    PF=$2
9
10    if [ ! -r $PF ]; then
11	return
12    fi
13
14    PID=`cat $PF`
15    if [ $PID -gt 0 ]; then
16	if ps $PID | grep -q $NAME; then
17	    kill $PID
18	fi
19    fi
20    rm $PF
21}
22
23if [ "$CMD" = "P2P-GROUP-STARTED" ]; then
24    GIFNAME=$3
25    if [ "$4" = "GO" ]; then
26	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
27	rm /var/run/dhclient.leases-$GIFNAME
28	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
29	ifconfig $GIFNAME 192.168.42.1 up
30	if ! dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
31	    -i $GIFNAME \
32	    -F192.168.42.11,192.168.42.99; then
33	    # another dnsmasq instance may be running and blocking us; try to
34	    # start with -z to avoid that
35	    dnsmasq -x /var/run/dnsmasq.pid-$GIFNAME \
36		-i $GIFNAME \
37		-F192.168.42.11,192.168.42.99 --listen-address 192.168.42.1 -z -p 0
38	fi
39    fi
40    if [ "$4" = "client" ]; then
41	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
42	rm /var/run/dhclient.leases-$GIFNAME
43	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
44	ipaddr=`echo "$*" | sed 's/.* ip_addr=\([^ ]*\).*/\1/'`
45	ipmask=`echo "$*" | sed 's/.* ip_mask=\([^ ]*\).*/\1/'`
46	goipaddr=`echo "$*" | sed 's/.* go_ip_addr=\([^ ]*\).*/\1/'`
47	if echo "$ipaddr$ipmask$goipaddr" | grep -q ' '; then
48	    ipaddr=""
49	    ipmask=""
50	    goipaddr=""
51	fi
52	if [ -n "$ipaddr" ]; then
53	    sudo ifconfig $GIFNAME "$ipaddr" netmask "$ipmask"
54	    sudo ip ro re default via "$goipaddr"
55	    exit 0
56	fi
57	dhclient -pf /var/run/dhclient-$GIFNAME.pid \
58	    -lf /var/run/dhclient.leases-$GIFNAME \
59	    -nw \
60	    $GIFNAME
61    fi
62fi
63
64if [ "$CMD" = "P2P-GROUP-REMOVED" ]; then
65    GIFNAME=$3
66    if [ "$4" = "GO" ]; then
67	kill_daemon dnsmasq /var/run/dnsmasq.pid-$GIFNAME
68	ifconfig $GIFNAME 0.0.0.0
69    fi
70    if [ "$4" = "client" ]; then
71	kill_daemon dhclient /var/run/dhclient-$GIFNAME.pid
72	rm /var/run/dhclient.leases-$GIFNAME
73	ifconfig $GIFNAME 0.0.0.0
74    fi
75fi
76
77if [ "$CMD" = "P2P-CROSS-CONNECT-ENABLE" ]; then
78    GIFNAME=$3
79    UPLINK=$4
80    # enable NAT/masquerade $GIFNAME -> $UPLINK
81    iptables -P FORWARD DROP
82    iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE
83    iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
84    iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
85    sysctl net.ipv4.ip_forward=1
86fi
87
88if [ "$CMD" = "P2P-CROSS-CONNECT-DISABLE" ]; then
89    GIFNAME=$3
90    UPLINK=$4
91    # disable NAT/masquerade $GIFNAME -> $UPLINK
92    sysctl net.ipv4.ip_forward=0
93    iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE
94    iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
95    iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
96fi
97