99f6140f18b620994aaa64692c438bb19ed1d975
[project/luci.git] / applications / luci-splash / root / etc / init.d / luci_splash
1 #!/bin/sh /etc/rc.common
2
3 set -x
4 START=70
5 EXTRA_COMMANDS=clear_leases
6 LIMIT_DOWN=0
7 LIMIT_DOWN_BURST=0
8 LIMIT_UP=0
9
10 IPT_REPLAY=/var/run/luci_splash.iptlog
11 LOCK=/var/run/luci_splash.lock
12 [ -x /usr/sbin/ip6tables ] && [ -f /proc/net/ipv6_route ] && HAS_IPV6=1
13
14 silent() {
15 "$@" 2>/dev/null
16 }
17
18 ipt_log() {
19 iptables -I "$@"
20 echo iptables -D "$@" >> $IPT_REPLAY
21 }
22
23 ipt6_log() {
24 [ "$HAS_IPV6" = 1 ] || return
25 ip6tables -I "$@"
26 echo ip6tables -D "$@" >> $IPT_REPLAY
27 }
28
29
30 iface_add() {
31 local cfg="$1"
32
33 config_get zone "$cfg" zone
34 [ -n "$zone" ] || return 0
35
36 config_get net "$cfg" network
37 [ -n "$net" ] || return 0
38
39 config_get ifname "$net" ifname
40 [ -n "$ifname" ] || return 0
41
42 config_get ipaddr "$net" ipaddr
43 [ -n "$ipaddr" ] || return 0
44
45 config_get ip6addr "$net" ip6addr
46 #[ -n "$ipaddr" ] || return 0
47
48 config_get netmask "$net" netmask
49 [ -n "$netmask" ] || return 0
50
51 config_get type "$net" type
52
53 parentiface="$(uci -q get network.${net}.ifname)"
54
55 [ -n "$parentiface" ] && [ ! "$type" = "bridge" ] && {
56 parentiface=${parentiface#@}
57 config_get parentproto "$parentiface" proto
58 config_get parentipaddr "$parentiface" ipaddr
59 config_get parentnetmask "$parentiface" netmask
60 }
61
62 eval "$(ipcalc.sh $ipaddr $netmask)"
63
64 ### Add interface specific chain entry rules
65 ipt_log "zone_${zone}_prerouting" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_prerouting -t nat
66 ipt_log "zone_${zone}_forward" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_forwarding -t filter
67
68 if [ "$HAS_IPV6" = 1 ] && [ -n "$ip6addr" ]; then
69 ipt6_log "zone_${zone}_forward" -i "${ifname%:*}" -s "$ip6addr" -j luci_splash_forwarding -t filter
70 fi
71
72 ### Allow traffic to the same subnet
73 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
74 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
75
76 ### Allow traffic to the mesh subnet
77 [ "$parentproto" = "static" -a -n "$parentipaddr" ] && {
78 iptables -t nat -I luci_splash_prerouting -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
79 iptables -t filter -I luci_splash_forwarding -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
80 }
81
82 qos_iface_add "$ifname"
83 }
84
85 iface_del() {
86 config_get zone "$1" zone
87 [ -n "$zone" ] || return 0
88
89 config_get net "$1" network
90 [ -n "$net" ] || return 0
91
92 config_get ifname "$net" ifname
93 [ -n "$ifname" ] || return 0
94
95 # Clear interface specific rules
96 [ -s $IPT_REPLAY ] && {
97 grep -- "-i ${ifname%:*}" $IPT_REPLAY | while read ln; do silent $ln; done
98 sed -ie "/-i ${ifname%:*}/d" $IPT_REPLAY
99 }
100
101 qos_iface_del "$ifname"
102 }
103
104 mac_add() {
105 config_get mac "$1" mac
106 append MACS "$mac"
107 }
108
109 subnet_add() {
110 local cfg="$1"
111
112 config_get ipaddr "$cfg" ipaddr
113 config_get netmask "$cfg" netmask
114
115 [ -n "$ipaddr" ] && {
116 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
117 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
118 }
119 }
120
121 qos_iface_add() {
122 local iface="$1"
123
124 # 77 -> download root qdisc
125 # 78 -> upload root qdisc
126 # 79 -> fwmark: client->inet
127 # 80 -> fwmark: inet->client
128
129 silent tc qdisc del dev "$iface" root handle 77:
130
131 if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
132 tc qdisc add dev "$iface" root handle 77: htb
133
134 # assume maximum rate of 20.000 kilobit for wlan
135 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
136
137 # set download limit and burst
138 tc class add dev "$iface" parent 77:1 classid 77:10 htb \
139 rate ${LIMIT_DOWN}kbit ceil ${LIMIT_DOWN_BURST}kbit prio 2
140
141 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
142
143 # adding ingress can result in "File exists" if qos-scripts are active
144 silent tc qdisc add dev "$iface" ingress
145
146 # set client download speed
147 tc filter add dev "$iface" parent 77: protocol ip prio 2 \
148 handle 80 fw flowid 77:10
149
150 # set client upload speed
151 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
152 handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
153 fi
154 }
155
156 qos_iface_del() {
157 local iface="$1"
158
159 silent tc qdisc del dev "$iface" root handle 77:
160 silent tc qdisc del dev "$iface" root handle 78:
161 silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
162 }
163
164 boot() {
165 ### Setup splash-relay
166 uci get uhttpd.splash 2>/dev/null || {
167 uci batch <<EOF
168 set uhttpd.splash=uhttpd
169 set uhttpd.splash.home="/www/cgi-bin/splash/"
170 set uhttpd.splash.interpreter=".sh=/bin/ash"
171 set uhttpd.splash.listen_http="8082"
172 set uhttpd.splash.index_page="splash.sh"
173 set uhttpd.splash.error_page="/splash.sh"
174
175 commit uhttpd
176 EOF
177 }
178
179 ### We are started by the firewall include
180 exit 0
181 }
182
183 start() {
184 lock $LOCK
185
186 include /lib/network
187 scan_interfaces
188 config_load luci_splash
189
190 ### Find QoS limits
191 config_get LIMIT_UP general limit_up
192 config_get LIMIT_DOWN general limit_down
193 config_get LIMIT_DOWN_BURST general limit_down_burst
194
195 LIMIT_UP="$((8*${LIMIT_UP:-0}))"
196 LIMIT_DOWN="$((8*${LIMIT_DOWN:-0}))"
197 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:+$((8*$LIMIT_DOWN_BURST))}"
198 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN / 5 * 6))}"
199
200 ### Load required modules
201 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
202 silent insmod act_police
203 silent insmod cls_fw
204 silent insmod cls_u32
205 silent insmod sch_htb
206 silent insmod sch_sfq
207 silent insmod sch_ingress
208 }
209
210 ### Create subchains
211 iptables -t nat -N luci_splash_prerouting
212 iptables -t nat -N luci_splash_leases
213 iptables -t filter -N luci_splash_forwarding
214 iptables -t filter -N luci_splash_filter
215
216 if [ "$HAS_IPV6" = 1 ]; then
217 ip6tables -t filter -N luci_splash_forwarding
218 ip6tables -t filter -N luci_splash_filter
219 fi
220
221 ### Clear iptables replay log
222 [ -s $IPT_REPLAY ] && . $IPT_REPLAY
223 echo -n > $IPT_REPLAY
224
225 ### Build the main and portal rule
226 config_foreach iface_add iface
227 config_foreach subnet_add subnet
228
229 ### Add interface independant prerouting rules
230 iptables -t nat -A luci_splash_prerouting -j luci_splash_leases
231 iptables -t nat -A luci_splash_leases -p udp --dport 53 -j REDIRECT --to-ports 53
232 iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
233
234 ### Add interface independant forwarding rules
235 iptables -t filter -A luci_splash_forwarding -j luci_splash_filter
236 iptables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
237 iptables -t filter -A luci_splash_filter -j REJECT --reject-with icmp-net-prohibited
238
239 if [ "$HAS_IPV6" = 1 ]; then
240 ip6tables -t filter -A luci_splash_forwarding -j luci_splash_filter
241 ip6tables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
242 ip6tables -t filter -A luci_splash_filter -j REJECT --reject-with adm-prohibited
243 fi
244
245 ### Add QoS chain
246 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
247 iptables -t mangle -N luci_splash_mark_out
248 iptables -t mangle -N luci_splash_mark_in
249 iptables -t mangle -I PREROUTING -j luci_splash_mark_out
250 iptables -t mangle -I POSTROUTING -j luci_splash_mark_in
251 if [ "$HAS_IPV6" = 1 ]; then
252 ip6tables -t mangle -N luci_splash_mark_out
253 ip6tables -t mangle -N luci_splash_mark_in
254 ip6tables -t mangle -I PREROUTING -j luci_splash_mark_out
255 ip6tables -t mangle -I POSTROUTING -j luci_splash_mark_in
256 fi
257 }
258
259 ### Find active mac addresses
260 MACS=""
261 config_foreach mac_add lease
262 config_foreach mac_add blacklist
263 config_foreach mac_add whitelist
264
265 ### Add crontab entry
266 test -f /etc/crontabs/root || touch /etc/crontabs/root
267 grep -q luci-splash /etc/crontabs/root || {
268 echo '*/5 * * * * /usr/sbin/luci-splash sync' >> /etc/crontabs/root
269 }
270
271 lock -u $LOCK
272
273 ### Populate iptables
274 [ -n "$MACS" ] && luci-splash add-rules $MACS
275 }
276
277 stop() {
278 lock $LOCK
279
280 include /lib/network
281 scan_interfaces
282 config_load luci_splash
283
284 ### Clear interface rules
285 config_foreach iface_del iface
286
287 silent iptables -t mangle -D PREROUTING -j luci_splash_mark_out
288 silent iptables -t mangle -D POSTROUTING -j luci_splash_mark_in
289
290 if [ "$HAS_IPV6" = 1 ]; then
291 silent ip6tables -t mangle -D PREROUTING -j luci_splash_mark_out
292 silent ip6tables -t mangle -D POSTROUTING -j luci_splash_mark_in
293 fi
294
295 ### Clear subchains
296 silent iptables -t nat -F luci_splash_prerouting
297 silent iptables -t nat -F luci_splash_leases
298 silent iptables -t filter -F luci_splash_forwarding
299 silent iptables -t filter -F luci_splash_filter
300 silent iptables -t mangle -F luci_splash_mark_out
301 silent iptables -t mangle -F luci_splash_mark_in
302
303 if [ "$HAS_IPV6" = 1 ]; then
304 ip6tables -t filter -F luci_splash_forwarding
305 ip6tables -t filter -F luci_splash_filter
306 ip6tables -t mangle -F luci_splash_mark_out
307 ip6tables -t mangle -F luci_splash_mark_in
308 fi
309
310 ### Delete subchains
311 silent iptables -t nat -X luci_splash_prerouting
312 silent iptables -t nat -X luci_splash_leases
313 silent iptables -t filter -X luci_splash_forwarding
314 silent iptables -t filter -X luci_splash_filter
315 silent iptables -t mangle -X luci_splash_mark_out
316 silent iptables -t mangle -X luci_splash_mark_in
317 if [ "$HAS_IPV6" = 1 ]; then
318 ip6tables -t filter -X luci_splash_forwarding
319 ip6tables -t filter -X luci_splash_filter
320 ip6tables -t mangle -X luci_splash_mark_out
321 ip6tables -t mangle -X luci_splash_mark_in
322 fi
323 sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
324
325 lock -u $LOCK
326 }
327
328 clear_leases() {
329 ### Find active mac addresses
330 MACS=""
331 config_foreach mac_add lease
332
333 ### Clear leases
334 [ -n "$MACS" ] && luci-splash remove $MACS
335 }
336