applications/luci-splash: do not splash non-splash clients
[project/luci.git] / applications / luci-splash / root / etc / init.d / luci_splash
1 #!/bin/sh /etc/rc.common
2 START=70
3 EXTRA_COMMANDS=clear_leases
4 SPLASH_INTERFACES=""
5 LIMIT_DOWN=0
6 LIMIT_DOWN_BURST=0
7 LIMIT_UP=0
8
9 silent() {
10 "$@" 2>/dev/null
11 }
12
13 iface_add() {
14 local cfg="$1"
15
16 config_get zone "$cfg" zone
17 [ -n "$zone" ] || return 0
18
19 config_get net "$cfg" network
20 [ -n "$net" ] || return 0
21
22 config_get ifname "$net" ifname
23 [ -n "$ifname" ] || return 0
24
25 config_get ipaddr "$net" ipaddr
26 [ -n "$ipaddr" ] || return 0
27
28 config_get netmask "$net" netmask
29 [ -n "$netmask" ] || return 0
30
31 config_get parentiface "$net" interface
32 [ -n "$parentiface" ] && {
33 config_get parentproto "$parentiface" proto
34 config_get parentipaddr "$parentiface" ipaddr
35 config_get parentnetmask "$parentiface" netmask
36 }
37
38 eval "$(ipcalc.sh $ipaddr $netmask)"
39
40 iptables -t nat -A prerouting_${zone} -j luci_splash_prerouting
41 iptables -t nat -A luci_splash_prerouting -j luci_splash_portal
42
43 iptables -t filter -I luci_splash_filter -s ! "$NETWORK/$PREFIX" -j RETURN
44 iptables -t nat -I luci_splash_leases -s ! "$NETWORK/$PREFIX" -j RETURN
45
46 iptables -t filter -I luci_splash_filter -s "$NETWORK/$PREFIX" -d "$ipaddr/${netmask:-32}" -j RETURN
47 iptables -t nat -I luci_splash_leases -s "$NETWORK/$PREFIX" -d "$ipaddr/${netmask:-32}" -j RETURN
48
49 [ "$parentproto" = "static" -a -n "$parentipaddr" ] && {
50 iptables -t filter -I luci_splash_filter -s "$NETWORK/$PREFIX" -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
51 iptables -t nat -I luci_splash_leases -s "$NETWORK/$PREFIX" -d "$parentipaddr/${parentnetmask:-32}" -j RETURN
52 }
53
54 iptables -t filter -A luci_splash_filter -s "$NETWORK/$PREFIX" -p udp --dport 53 -j RETURN
55 iptables -t filter -A luci_splash_filter -s "$NETWORK/$PREFIX" -p tcp --dport 22 -j RETURN # XXX: ssh really needed?
56 iptables -t filter -A luci_splash_filter -s "$NETWORK/$PREFIX" -p tcp --dport 80 -j RETURN
57 iptables -t filter -A luci_splash_filter -s "$NETWORK/$PREFIX" -j REJECT --reject-with icmp-admin-prohibited
58
59 qos_iface_add "$ifname"
60
61 append SPLASH_INTERFACES "$ifname"
62 }
63
64 iface_del() {
65 config_get zone "$1" zone
66 [ -n "$zone" ] || return 0
67
68 while iptables -t nat -D prerouting_${zone} -j luci_splash_prerouting 2>&-; do :; done
69
70 config_get net "$1" network
71 [ -n "$net" ] || return 0
72
73 config_get ifname "$net" ifname
74 [ -n "$ifname" ] || return 0
75
76 qos_iface_del "$ifname"
77 }
78
79 blacklist_add() {
80 local cfg="$1"
81
82 config_get mac "$cfg" mac
83 [ -n "$mac" ] && {
84 iptables -t filter -I luci_splash_filter -m mac --mac-source "$mac" -j DROP
85 iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j DROP
86 }
87 }
88
89 whitelist_add() {
90 local cfg="$1"
91
92 config_get mac "$cfg" mac
93 [ -n "$mac" ] && {
94 iptables -t filter -I luci_splash_filter -m mac --mac-source "$mac" -j RETURN
95 iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j RETURN
96 }
97 }
98
99 lease_add() {
100 local cfg="$1"
101
102 config_get mac "$cfg" mac
103 config_get ban "$cfg" kicked
104
105 ban=${ban:+DROP}
106
107 [ -n "$mac" ] && {
108 local oIFS="$IFS"; IFS=":"
109 set -- $mac
110 IFS="$oIFS"; unset oIFS
111
112 local mac_pre="$1$2"
113 local mac_post="$3$4$5$6"
114 local handle="$6"
115
116 iptables -t filter -I luci_splash_filter -m mac --mac-source "$mac" -j RETURN
117 iptables -t nat -I luci_splash_leases -m mac --mac-source "$mac" -j "${ban:-RETURN}"
118
119 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
120 iptables -t mangle -I luci_splash_mark -m mac --mac-source "$mac" -j MARK --set-mark 79
121
122 for i in $SPLASH_INTERFACES; do
123 tc filter add dev $i parent 77:0 protocol ip prio 2 handle ::$handle u32 \
124 match u16 0x0800 0xFFFF at -2 match u32 0x$mac_post 0xFFFFFFFF at -12 \
125 match u16 0x$mac_pre 0xFFFF at -14 flowid 77:10
126 done
127 }
128 }
129 }
130
131 subnet_add() {
132 local cfg="$1"
133
134 config_get ipaddr "$cfg" ipaddr
135 config_get netmask "$cfg" netmask
136
137 [ -n "$ipaddr" ] && {
138 iptables -t filter -I luci_splash_filter -d "$ipaddr/${netmask:-32}" -j RETURN
139 iptables -t nat -I luci_splash_portal -d "$ipaddr/${netmask:-32}" -j RETURN
140 }
141 }
142
143 qos_iface_add() {
144 local iface="$1"
145
146 # 77 -> download root qdisc
147 # 78 -> upload root qdisc
148 # 79 -> fwmark
149
150 silent tc qdisc del dev "$iface" root handle 77:
151
152 if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
153 tc qdisc add dev "$iface" root handle 77: htb
154
155 # assume maximum rate of 20.000 kilobit for wlan
156 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
157
158 # set download limit and burst
159 tc class add dev "$iface" parent 77:1 classid 77:10 htb \
160 rate ${LIMIT_DOWN}kb ceil ${LIMIT_DOWN_BURST}kb prio 2
161
162 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
163
164 # adding ingress can result in "File exists" if qos-scripts are active
165 silent tc qdisc add dev "$iface" ingress
166
167 # set client upload speed
168 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
169 handle 79 fw police rate ${LIMIT_UP}kb mtu 6k burst 6k drop
170 fi
171 }
172
173 qos_iface_del() {
174 local iface="$1"
175
176 silent tc qdisc del dev "$iface" root handle 77:
177 silent tc qdisc del dev "$iface" root handle 78:
178 silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
179 }
180
181 boot() {
182 ### Setup splash-relay
183 uci get lucid.splashr || {
184 uci batch <<EOF
185 set lucid.splashr=daemon
186 set lucid.splashr.slave=httpd
187 add_list lucid.splashr.address=8082
188 add_list lucid.splashr.publisher=splashredir
189 set lucid.splashr.enabled=1
190
191 set lucid.splashredir=Redirector
192 set lucid.splashredir.name=Splashd
193 set lucid.splashredir.virtual='/'
194 set lucid.splashredir.physical=':80/luci/splash'
195
196 commit lucid
197 EOF
198 }
199
200 ### We are started by the firewall include
201 exit 0
202 }
203
204 start() {
205 ### Read chains from config
206 include /lib/network
207 scan_interfaces
208 config_load luci_splash
209
210 ### Find QoS limits
211 config_get LIMIT_UP general limit_up
212 config_get LIMIT_DOWN general limit_down
213 config_get LIMIT_DOWN_BURST general limit_down_burst
214
215 LIMIT_UP="${LIMIT_UP:-0}"
216 LIMIT_DOWN="${LIMIT_DOWN:-0}"
217 LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN * 2))}"
218
219 ### Load required modules
220 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
221 silent insmod cls_fw
222 silent insmod cls_u32
223 silent insmod sch_htb
224 silent insmod sch_ingress
225 }
226
227 ### Create subchains
228 iptables -t filter -N luci_splash_filter
229 iptables -t nat -N luci_splash_portal
230 iptables -t nat -N luci_splash_leases
231 iptables -t nat -N luci_splash_prerouting
232
233 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
234 iptables -t mangle -N luci_splash_mark
235
236 ### Build the main and portal rule
237 config_foreach iface_add iface
238 config_foreach subnet_add subnet
239 config_foreach blacklist_add blacklist
240 config_foreach whitelist_add whitelist
241 config_foreach lease_add lease
242
243 ### Build the portal rule
244 iptables -t filter -I INPUT -j luci_splash_filter
245 iptables -t filter -I FORWARD -j luci_splash_filter
246
247 [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
248 iptables -t mangle -I PREROUTING -j luci_splash_mark
249
250 ### Allow icmp, dns and traceroute
251 iptables -t nat -A luci_splash_portal -p udp --dport 33434:33523 -j RETURN
252 iptables -t nat -A luci_splash_portal -p icmp -j RETURN
253 iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
254
255 ### Redirect the rest into the lease chain
256 iptables -t nat -A luci_splash_portal -j luci_splash_leases
257
258 ### Build the leases rule
259 iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
260
261 ### Add crontab entry
262 test -f /etc/crontabs/root || touch /etc/crontabs/root
263 grep -q luci-splash /etc/crontabs/root || {
264 echo '*/5 * * * * /usr/sbin/luci-splash sync' >> /etc/crontabs/root
265 }
266 }
267
268 stop() {
269 ### Clear interface rules
270 include /lib/network
271 scan_interfaces
272 config_load luci_splash
273 config_foreach iface_del iface
274
275 silent iptables -t filter -D INPUT -j luci_splash_filter
276 silent iptables -t filter -D FORWARD -j luci_splash_filter
277 silent iptables -t mangle -D PREROUTING -j luci_splash_mark
278
279 ### Clear subchains
280 silent iptables -t nat -F luci_splash_leases
281 silent iptables -t nat -F luci_splash_portal
282 silent iptables -t nat -F luci_splash_prerouting
283 silent iptables -t filter -F luci_splash_filter
284 silent iptables -t mangle -F luci_splash_mark
285
286 ### Delete subchains
287 silent iptables -t nat -X luci_splash_leases
288 silent iptables -t nat -X luci_splash_portal
289 silent iptables -t nat -X luci_splash_prerouting
290 silent iptables -t filter -X luci_splash_filter
291 silent iptables -t mangle -X luci_splash_mark
292
293 sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
294 }
295
296
297 clear_leases() {
298 stop
299 while uci -P /var/state del luci_splash.@lease[0] 2>&-;do :; done
300 start
301 }
302