-
Notifications
You must be signed in to change notification settings - Fork 66
/
wlanstart.sh
executable file
·150 lines (114 loc) · 4.13 KB
/
wlanstart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# Check if running in privileged mode
if [ ! -w "/sys" ] ; then
echo "[Error] Not running in privileged mode."
exit 1
fi
# Check environment variables
if [ ! "${INTERFACE}" ] ; then
echo "[Error] An interface must be specified."
exit 1
fi
# Default values
true ${SUBNET:=192.168.254.0}
true ${AP_ADDR:=192.168.254.1}
true ${PRI_DNS:=8.8.8.8}
true ${SEC_DNS:=8.8.4.4}
true ${SSID:=raspberry}
true ${CHANNEL:=11}
true ${WPA_PASSPHRASE:=passw0rd}
true ${HW_MODE:=g}
if [ ! -f "/etc/hostapd.conf" ] ; then
cat > "/etc/hostapd.conf" <<EOF
interface=${INTERFACE}
${DRIVER+"driver=${DRIVER}"}
ssid=${SSID}
hw_mode=${HW_MODE}
channel=${CHANNEL}
wpa=2
wpa_passphrase=${WPA_PASSPHRASE}
wpa_key_mgmt=WPA-PSK
# TKIP is no secure anymore
#wpa_pairwise=TKIP CCMP
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_ptk_rekey=600
wmm_enabled=1
# Activate channel selection for HT High Througput (802.11an)
${HT_ENABLED+"ieee80211n=1"}
${HT_CAPAB+"ht_capab=${HT_CAPAB}"}
# Activate channel selection for VHT Very High Througput (802.11ac)
${VHT_ENABLED+"ieee80211ac=1"}
${VHT_CAPAB+"vht_capab=${VHT_CAPAB}"}
EOF
fi
# Setup interface and restart DHCP service
ip link set ${INTERFACE} up
ip addr flush dev ${INTERFACE}
ip addr add ${AP_ADDR}/24 dev ${INTERFACE}
# NAT settings
echo "NAT settings ip_dynaddr, ip_forward"
for i in ip_dynaddr ip_forward ; do
if [ $(cat /proc/sys/net/ipv4/$i) -eq 1 ] ; then
echo $i already 1
else
echo "1" > /proc/sys/net/ipv4/$i
fi
done
cat /proc/sys/net/ipv4/ip_dynaddr
cat /proc/sys/net/ipv4/ip_forward
if [ "${OUTGOINGS}" ] ; then
ints="$(sed 's/,\+/ /g' <<<"${OUTGOINGS}")"
for int in ${ints}
do
echo "Setting iptables for outgoing traffics on ${int}..."
iptables -t nat -D POSTROUTING -s ${SUBNET}/24 -o ${int} -j MASQUERADE > /dev/null 2>&1 || true
iptables -t nat -A POSTROUTING -s ${SUBNET}/24 -o ${int} -j MASQUERADE
iptables -D FORWARD -i ${int} -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT > /dev/null 2>&1 || true
iptables -A FORWARD -i ${int} -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i ${INTERFACE} -o ${int} -j ACCEPT > /dev/null 2>&1 || true
iptables -A FORWARD -i ${INTERFACE} -o ${int} -j ACCEPT
done
else
echo "Setting iptables for outgoing traffics on all interfaces..."
iptables -t nat -D POSTROUTING -s ${SUBNET}/24 -j MASQUERADE > /dev/null 2>&1 || true
iptables -t nat -A POSTROUTING -s ${SUBNET}/24 -j MASQUERADE
iptables -D FORWARD -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT > /dev/null 2>&1 || true
iptables -A FORWARD -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i ${INTERFACE} -j ACCEPT > /dev/null 2>&1 || true
iptables -A FORWARD -i ${INTERFACE} -j ACCEPT
fi
echo "Configuring DHCP server .."
cat > "/etc/dhcpd.conf" <<EOF
option domain-name-servers ${PRI_DNS}, ${SEC_DNS};
option subnet-mask 255.255.255.0;
option routers ${AP_ADDR};
subnet ${SUBNET} netmask 255.255.255.0 {
range ${SUBNET::-1}100 ${SUBNET::-1}200;
}
EOF
echo "Starting DHCP server .."
dhcpd ${INTERFACE}
# Capture external docker signals
trap 'true' SIGINT
trap 'true' SIGTERM
trap 'true' SIGHUP
echo "Starting HostAP daemon ..."
/usr/sbin/hostapd /etc/hostapd.conf &
wait $!
echo "Removing iptables rules..."
if [ "${OUTGOINGS}" ] ; then
ints="$(sed 's/,\+/ /g' <<<"${OUTGOINGS}")"
for int in ${ints}
do
echo "Removing iptables for outgoing traffics on ${int}..."
iptables -t nat -D POSTROUTING -s ${SUBNET}/24 -o ${int} -j MASQUERADE > /dev/null 2>&1 || true
iptables -D FORWARD -i ${int} -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT > /dev/null 2>&1 || true
iptables -D FORWARD -i ${INTERFACE} -o ${int} -j ACCEPT > /dev/null 2>&1 || true
done
else
echo "Setting iptables for outgoing traffics on all interfaces..."
iptables -t nat -D POSTROUTING -s ${SUBNET}/24 -j MASQUERADE > /dev/null 2>&1 || true
iptables -D FORWARD -o ${INTERFACE} -m state --state RELATED,ESTABLISHED -j ACCEPT > /dev/null 2>&1 || true
iptables -D FORWARD -i ${INTERFACE} -j ACCEPT > /dev/null 2>&1 || true
fi