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