1 #!/bin/sh /etc/rc.common
4 EXTRA_COMMANDS=clear_leases
9 IPT_REPLAY=/var/run/luci_splash.iptlog
10 LOCK=/var/run/luci_splash.lock
11 [ -x /usr/sbin/ip6tables ] && [ -f /proc/net/ipv6_route ] && HAS_IPV6=1
19 echo iptables -D "$@" >> $IPT_REPLAY
23 [ "$HAS_IPV6" = 1 ] || return
25 echo ip6tables -D "$@" >> $IPT_REPLAY
32 config_get zone "$cfg" zone
33 [ -n "$zone" ] || return 0
35 config_get net "$cfg" network
36 [ -n "$net" ] || return 0
38 config_get ifname "$net" ifname
39 [ -n "$ifname" ] || return 0
41 config_get ipaddr "$net" ipaddr
42 [ -n "$ipaddr" ] || return 0
44 config_get ip6addr "$net" ip6addr
45 #[ -n "$ipaddr" ] || return 0
47 config_get netmask "$net" netmask
48 [ -n "$netmask" ] || return 0
50 config_get type "$net" type
52 parentiface="$(uci -q get network.${net}.ifname)"
54 [ -n "$parentiface" ] && [ ! "$type" = "bridge" ] && {
55 parentiface=${parentiface#@}
56 config_get parentproto "$parentiface" proto
57 config_get parentipaddr "$parentiface" ipaddr
58 config_get parentnetmask "$parentiface" netmask
61 eval "$(ipcalc.sh $ipaddr $netmask)"
63 ### Add interface specific chain entry rules
64 ipt_log "zone_${zone}_prerouting" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_prerouting -t nat
65 ipt_log "zone_${zone}_forward" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_forwarding -t filter
67 if [ "$HAS_IPV6" = 1 ]; then
68 ipt6_log "zone_${zone}_forward" -i "${ifname%:*}" -s "$ip6addr" -j luci_splash_forwarding -t filter
71 ### Allow traffic to the same subnet
72 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
73 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
75 ### Allow traffic to the mesh subnet
76 [ "$parentproto" = "static" -a -n "$parentipaddr" ] && {
77 iptables -t nat -I luci_splash_prerouting -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
78 iptables -t filter -I luci_splash_forwarding -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
81 qos_iface_add "$ifname"
85 config_get zone "$1" zone
86 [ -n "$zone" ] || return 0
88 config_get net "$1" network
89 [ -n "$net" ] || return 0
91 config_get ifname "$net" ifname
92 [ -n "$ifname" ] || return 0
94 # Clear interface specific rules
95 [ -s $IPT_REPLAY ] && {
96 grep -- "-i ${ifname%:*}" $IPT_REPLAY | while read ln; do silent $ln; done
97 sed -ie "/-i ${ifname%:*}/d" $IPT_REPLAY
100 qos_iface_del "$ifname"
104 config_get mac "$1" mac
111 config_get ipaddr "$cfg" ipaddr
112 config_get netmask "$cfg" netmask
114 [ -n "$ipaddr" ] && {
115 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
116 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
123 # 77 -> download root qdisc
124 # 78 -> upload root qdisc
125 # 79 -> fwmark: client->inet
126 # 80 -> fwmark: inet->client
128 silent tc qdisc del dev "$iface" root handle 77:
130 if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
131 tc qdisc add dev "$iface" root handle 77: htb
133 # assume maximum rate of 20.000 kilobit for wlan
134 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
136 # set download limit and burst
137 tc class add dev "$iface" parent 77:1 classid 77:10 htb \
138 rate ${LIMIT_DOWN}kbit ceil ${LIMIT_DOWN_BURST}kbit prio 2
140 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
142 # adding ingress can result in "File exists" if qos-scripts are active
143 silent tc qdisc add dev "$iface" ingress
145 # set client download speed
146 tc filter add dev "$iface" parent 77: protocol ip prio 2 \
147 handle 80 fw flowid 77:10
149 # set client upload speed
150 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
151 handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
158 silent tc qdisc del dev "$iface" root handle 77:
159 silent tc qdisc del dev "$iface" root handle 78:
160 silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
164 ### Setup splash-relay
165 uci get uhttpd.splash 2>/dev/null || {
167 set uhttpd.splash=uhttpd
168 set uhttpd.splash.home="/www/cgi-bin/splash/"
169 set uhttpd.splash.interpreter=".sh=/bin/ash"
170 set uhttpd.splash.listen_http="8082"
171 set uhttpd.splash.index_page="splash.sh"
172 set uhttpd.splash.error_page="/splash.sh"
178 ### We are started by the firewall include
187 config_load luci_splash
190 config_get LIMIT_UP general limit_up
191 config_get LIMIT_DOWN general limit_down
192 config_get LIMIT_DOWN_BURST general limit_down_burst
194 LIMIT_UP="$((8*${LIMIT_UP:-0}))"
195 LIMIT_DOWN="$((8*${LIMIT_DOWN:-0}))"
196 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:+$((8*$LIMIT_DOWN_BURST))}"
197 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN / 5 * 6))}"
199 ### Load required modules
200 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
201 silent insmod act_police
203 silent insmod cls_u32
204 silent insmod sch_htb
205 silent insmod sch_sfq
206 silent insmod sch_ingress
210 iptables -t nat -N luci_splash_prerouting
211 iptables -t nat -N luci_splash_leases
212 iptables -t filter -N luci_splash_forwarding
213 iptables -t filter -N luci_splash_filter
215 if [ "$HAS_IPV6" = 1 ]; then
216 ip6tables -t filter -N luci_splash_forwarding
217 ip6tables -t filter -N luci_splash_filter
220 ### Clear iptables replay log
221 [ -s $IPT_REPLAY ] && . $IPT_REPLAY
222 echo -n > $IPT_REPLAY
224 ### Build the main and portal rule
225 config_foreach iface_add iface
226 config_foreach subnet_add subnet
228 ### Add interface independant prerouting rules
229 iptables -t nat -A luci_splash_prerouting -j luci_splash_leases
230 iptables -t nat -A luci_splash_leases -p udp --dport 53 -j REDIRECT --to-ports 53
231 iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
233 ### Add interface independant forwarding rules
234 iptables -t filter -A luci_splash_forwarding -j luci_splash_filter
235 iptables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
236 iptables -t filter -A luci_splash_filter -j REJECT --reject-with icmp-net-prohibited
238 if [ "$HAS_IPV6" = 1 ]; then
239 ip6tables -t filter -A luci_splash_forwarding -j luci_splash_filter
240 ip6tables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
241 ip6tables -t filter -A luci_splash_filter -j REJECT --reject-with adm-prohibited
245 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
246 iptables -t mangle -N luci_splash_mark_out
247 iptables -t mangle -N luci_splash_mark_in
248 iptables -t mangle -I PREROUTING -j luci_splash_mark_out
249 iptables -t mangle -I POSTROUTING -j luci_splash_mark_in
250 if [ "$HAS_IPV6" = 1 ]; then
251 ip6tables -t mangle -N luci_splash_mark_out
252 ip6tables -t mangle -N luci_splash_mark_in
253 ip6tables -t mangle -I PREROUTING -j luci_splash_mark_out
254 ip6tables -t mangle -I POSTROUTING -j luci_splash_mark_in
258 ### Find active mac addresses
260 config_foreach mac_add lease
261 config_foreach mac_add blacklist
262 config_foreach mac_add whitelist
264 ### Add crontab entry
265 test -f /etc/crontabs/root || touch /etc/crontabs/root
266 grep -q luci-splash /etc/crontabs/root || {
267 echo '*/5 * * * * /usr/sbin/luci-splash sync' >> /etc/crontabs/root
272 ### Populate iptables
273 [ -n "$MACS" ] && luci-splash add-rules $MACS
281 config_load luci_splash
283 ### Clear interface rules
284 config_foreach iface_del iface
286 silent iptables -t mangle -D PREROUTING -j luci_splash_mark_out
287 silent iptables -t mangle -D POSTROUTING -j luci_splash_mark_in
289 if [ "$HAS_IPV6" = 1 ]; then
290 silent ip6tables -t mangle -D PREROUTING -j luci_splash_mark_out
291 silent ip6tables -t mangle -D POSTROUTING -j luci_splash_mark_in
295 silent iptables -t nat -F luci_splash_prerouting
296 silent iptables -t nat -F luci_splash_leases
297 silent iptables -t filter -F luci_splash_forwarding
298 silent iptables -t filter -F luci_splash_filter
299 silent iptables -t mangle -F luci_splash_mark_out
300 silent iptables -t mangle -F luci_splash_mark_in
302 if [ "$HAS_IPV6" = 1 ]; then
303 ip6tables -t filter -F luci_splash_forwarding
304 ip6tables -t filter -F luci_splash_filter
305 ip6tables -t mangle -F luci_splash_mark_out
306 ip6tables -t mangle -F luci_splash_mark_in
310 silent iptables -t nat -X luci_splash_prerouting
311 silent iptables -t nat -X luci_splash_leases
312 silent iptables -t filter -X luci_splash_forwarding
313 silent iptables -t filter -X luci_splash_filter
314 silent iptables -t mangle -X luci_splash_mark_out
315 silent iptables -t mangle -X luci_splash_mark_in
316 if [ "$HAS_IPV6" = 1 ]; then
317 ip6tables -t filter -X luci_splash_forwarding
318 ip6tables -t filter -X luci_splash_filter
319 ip6tables -t mangle -X luci_splash_mark_out
320 ip6tables -t mangle -X luci_splash_mark_in
322 sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
328 ### Find active mac addresses
330 config_foreach mac_add lease
333 [ -n "$MACS" ] && luci-splash remove $MACS