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
14 config_load luci_splash
24 echo iptables -D "$@" >> $IPT_REPLAY
30 config_get zone "$cfg" zone
31 [ -n "$zone" ] || return 0
33 config_get net "$cfg" network
34 [ -n "$net" ] || return 0
36 config_get ifname "$net" ifname
37 [ -n "$ifname" ] || return 0
39 config_get ipaddr "$net" ipaddr
40 [ -n "$ipaddr" ] || return 0
42 config_get netmask "$net" netmask
43 [ -n "$netmask" ] || return 0
45 config_get parentiface "$net" interface
46 [ -n "$parentiface" ] && {
47 config_get parentproto "$parentiface" proto
48 config_get parentipaddr "$parentiface" ipaddr
49 config_get parentnetmask "$parentiface" netmask
52 eval "$(ipcalc.sh $ipaddr $netmask)"
54 ### Add interface specific chain entry rules
55 ipt_log "zone_${zone}_prerouting" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_prerouting -t nat
56 ipt_log "zone_${zone}_forward" -i "${ifname%:*}" -s "$NETWORK/$PREFIX" -j luci_splash_forwarding -t filter
58 ### Allow traffic to the same subnet
59 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
60 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
62 ### Allow traffic to the mesh subnet
63 [ "$parentproto" = "static" -a -n "$parentipaddr" ] && {
64 iptables -t nat -I luci_splash_prerouting -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
65 iptables -t filter -I luci_splash_forwarding -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
68 qos_iface_add "$ifname"
72 config_get zone "$1" zone
73 [ -n "$zone" ] || return 0
75 config_get net "$1" network
76 [ -n "$net" ] || return 0
78 config_get ifname "$net" ifname
79 [ -n "$ifname" ] || return 0
81 # Clear interface specific rules
82 [ -s $IPT_REPLAY ] && {
83 grep -- "-i ${ifname%:*}" $IPT_REPLAY | while read ln; do silent $ln; done
84 sed -ie "/-i ${ifname%:*}/d" $IPT_REPLAY
87 qos_iface_del "$ifname"
91 config_get mac "$1" mac
98 config_get ipaddr "$cfg" ipaddr
99 config_get netmask "$cfg" netmask
101 [ -n "$ipaddr" ] && {
102 iptables -t nat -I luci_splash_prerouting -d "$ipaddr/${netmask:-32}" -j RETURN
103 iptables -t filter -I luci_splash_forwarding -d "$ipaddr/${netmask:-32}" -j RETURN
110 # 77 -> download root qdisc
111 # 78 -> upload root qdisc
112 # 79 -> fwmark: client->inet
113 # 80 -> fwmark: inet->client
115 silent tc qdisc del dev "$iface" root handle 77:
117 if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
118 tc qdisc add dev "$iface" root handle 77: htb
120 # assume maximum rate of 20.000 kilobit for wlan
121 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
123 # set download limit and burst
124 tc class add dev "$iface" parent 77:1 classid 77:10 htb \
125 rate ${LIMIT_DOWN}kbit ceil ${LIMIT_DOWN_BURST}kbit prio 2
127 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
129 # adding ingress can result in "File exists" if qos-scripts are active
130 silent tc qdisc add dev "$iface" ingress
132 # set client download speed
133 tc filter add dev "$iface" parent 77: protocol ip prio 2 \
134 handle 80 fw flowid 77:10
136 # set client upload speed
137 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
138 handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
145 silent tc qdisc del dev "$iface" root handle 77:
146 silent tc qdisc del dev "$iface" root handle 78:
147 silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
151 ### Setup splash-relay
152 uci get lucid.splashr 2>/dev/null || {
154 set lucid.splashr=daemon
155 set lucid.splashr.slave=httpd
156 add_list lucid.splashr.address=8082
157 add_list lucid.splashr.publisher=splashredir
158 set lucid.splashr.enabled=1
160 set lucid.splashredir=Redirector
161 set lucid.splashredir.name=Splashd
162 set lucid.splashredir.virtual='/'
163 set lucid.splashredir.physical=':80/luci/splash'
169 ### We are started by the firewall include
174 lock -w $LOCK && lock $LOCK
177 config_get LIMIT_UP general limit_up
178 config_get LIMIT_DOWN general limit_down
179 config_get LIMIT_DOWN_BURST general limit_down_burst
181 LIMIT_UP="$((8*${LIMIT_UP:-0}))"
182 LIMIT_DOWN="$((8*${LIMIT_DOWN:-0}))"
183 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:+$((8*$LIMIT_DOWN_BURST))}"
184 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN / 5 * 6))}"
186 ### Load required modules
187 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
189 silent insmod cls_u32
190 silent insmod sch_htb
191 silent insmod sch_sfq
192 silent insmod sch_ingress
196 iptables -t nat -N luci_splash_prerouting
197 iptables -t nat -N luci_splash_leases
198 iptables -t filter -N luci_splash_forwarding
199 iptables -t filter -N luci_splash_filter
201 ### Clear iptables replay log
202 [ -s $IPT_REPLAY ] && . $IPT_REPLAY
203 echo -n > $IPT_REPLAY
205 ### Build the main and portal rule
206 config_foreach iface_add iface
207 config_foreach subnet_add subnet
209 ### Add interface independant prerouting rules
210 iptables -t nat -A luci_splash_prerouting -j luci_splash_leases
211 iptables -t nat -A luci_splash_leases -p udp --dport 53 -j REDIRECT --to-ports 53
212 iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
214 ### Add interface independant forwarding rules
215 iptables -t filter -A luci_splash_forwarding -j luci_splash_filter
216 iptables -t filter -A luci_splash_filter -p tcp -j REJECT --reject-with tcp-reset
217 iptables -t filter -A luci_splash_filter -j REJECT --reject-with icmp-net-prohibited
220 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
221 iptables -t mangle -N luci_splash_mark_out
222 iptables -t mangle -N luci_splash_mark_in
223 iptables -t mangle -I PREROUTING -j luci_splash_mark_out
224 iptables -t mangle -I POSTROUTING -j luci_splash_mark_in
227 ### Find active mac addresses
229 config_foreach mac_add lease
230 config_foreach mac_add blacklist
231 config_foreach mac_add whitelist
233 ### Add crontab entry
234 test -f /etc/crontabs/root || touch /etc/crontabs/root
235 grep -q luci-splash /etc/crontabs/root || {
236 echo '*/5 * * * * /usr/sbin/luci-splash sync' >> /etc/crontabs/root
241 ### Populate iptables
242 [ -n "$MACS" ] && luci-splash add-rules $MACS
246 lock -w $LOCK && lock $LOCK
248 ### Clear interface rules
249 config_foreach iface_del iface
251 silent iptables -t mangle -D PREROUTING -j luci_splash_mark_out
252 silent iptables -t mangle -D POSTROUTING -j luci_splash_mark_in
255 silent iptables -t nat -F luci_splash_prerouting
256 silent iptables -t nat -F luci_splash_leases
257 silent iptables -t filter -F luci_splash_forwarding
258 silent iptables -t filter -F luci_splash_filter
259 silent iptables -t mangle -F luci_splash_mark_out
260 silent iptables -t mangle -F luci_splash_mark_in
263 silent iptables -t nat -X luci_splash_prerouting
264 silent iptables -t nat -X luci_splash_leases
265 silent iptables -t filter -X luci_splash_forwarding
266 silent iptables -t filter -X luci_splash_filter
267 silent iptables -t mangle -X luci_splash_mark_out
268 silent iptables -t mangle -X luci_splash_mark_in
270 sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
276 ### Find active mac addresses
278 config_foreach mac_add lease
281 [ -n "$MACS" ] && luci-splash remove $MACS