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