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 udhcpc /var/run/udhcpc-$GIFNAME.pid
27	ifconfig $GIFNAME 192.168.42.1 up
28	udhcpd /etc/udhcpd-p2p.conf
29    fi
30    if [ "$4" = "client" ]; then
31	kill_daemon udhcpc /var/run/udhcpc-$GIFNAME.pid
32	kill_daemon udhcpd /var/run/udhcpd-$GIFNAME.pid
33	udhcpc -i $GIFNAME -p /var/run/udhcpc-$GIFNAME.pid \
34		-s /etc/udhcpc.script
35    fi
36fi
37
38if [ "$CMD" = "P2P-GROUP-REMOVED" ]; then
39    GIFNAME=$3
40    if [ "$4" = "GO" ]; then
41	kill_daemon udhcpd /var/run/udhcpd-$GIFNAME.pid
42	ifconfig $GIFNAME 0.0.0.0
43    fi
44    if [ "$4" = "client" ]; then
45	kill_daemon udhcpc /var/run/udhcpc-$GIFNAME.pid
46	ifconfig $GIFNAME 0.0.0.0
47    fi
48fi
49
50if [ "$CMD" = "P2P-CROSS-CONNECT-ENABLE" ]; then
51    GIFNAME=$3
52    UPLINK=$4
53    # enable NAT/masquarade $GIFNAME -> $UPLINK
54    iptables -P FORWARD DROP
55    iptables -t nat -A POSTROUTING -o $UPLINK -j MASQUERADE
56    iptables -A FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
57    iptables -A FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
58    sysctl net.ipv4.ip_forward=1
59fi
60
61if [ "$CMD" = "P2P-CROSS-CONNECT-DISABLE" ]; then
62    GIFNAME=$3
63    UPLINK=$4
64    # disable NAT/masquarade $GIFNAME -> $UPLINK
65    sysctl net.ipv4.ip_forward=0
66    iptables -t nat -D POSTROUTING -o $UPLINK -j MASQUERADE
67    iptables -D FORWARD -i $UPLINK -o $GIFNAME -m state --state RELATED,ESTABLISHED -j ACCEPT
68    iptables -D FORWARD -i $GIFNAME -o $UPLINK -j ACCEPT
69fi
70