applications/luci-splash:
[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 include /lib/network
13 scan_interfaces
14 config_load luci_splash
15
16 set -x
17
18 silent() {
19         "$@" 2>/dev/null
20 }
21
22 ipt_log() {
23         iptables -I "$@"
24         echo iptables -D "$@" >> $IPT_REPLAY
25 }
26
27 iface_add() {
28         local cfg="$1"
29         
30         config_get zone "$cfg" zone
31         [ -n "$zone" ] || return 0
32         
33         config_get net "$cfg" network
34         [ -n "$net" ] || return 0
35
36         config_get ifname "$net" ifname
37         [ -n "$ifname" ] || return 0
38         
39         config_get ipaddr "$net" ipaddr
40         [ -n "$ipaddr" ] || return 0
41         
42         config_get netmask "$net" netmask
43         [ -n "$netmask" ] || return 0
44         
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
50         }
51         
52         eval "$(ipcalc.sh $ipaddr $netmask)"
53
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
57
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
61
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
66         }
67
68         qos_iface_add "$ifname"
69 }
70
71 iface_del() {
72         config_get zone "$1" zone                                                                
73         [ -n "$zone" ] || return 0
74
75         config_get net "$1" network
76         [ -n "$net" ] || return 0
77
78         config_get ifname "$net" ifname
79         [ -n "$ifname" ] || return 0
80
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
85         }
86
87         qos_iface_del "$ifname"
88 }
89
90 mac_add() {
91         config_get mac "$1" mac
92         append MACS "$mac"
93 }
94
95 subnet_add() {
96         local cfg="$1"
97         
98         config_get ipaddr  "$cfg" ipaddr
99         config_get netmask "$cfg" netmask
100         
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
104         }
105 }
106
107 qos_iface_add() {
108         local iface="$1"
109
110         # 77 -> download root qdisc
111         # 78 -> upload root qdisc
112         # 79 -> fwmark: client->inet
113         # 80 -> fwmark: inet->client
114
115         silent tc qdisc del dev "$iface" root handle 77:
116
117         if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
118                 tc qdisc add dev "$iface" root handle 77: htb
119
120                 # assume maximum rate of 20.000 kilobit for wlan
121                 tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit
122
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
126
127                 tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10
128
129                 # adding ingress can result in "File exists" if qos-scripts are active
130                 silent tc qdisc add dev "$iface" ingress
131
132                 # set client download speed
133                 tc filter add dev "$iface" parent 77: protocol ip prio 2 \
134                         handle 80 fw flowid 77:10
135
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
139         fi      
140 }
141
142 qos_iface_del() {
143         local iface="$1"
144
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
148 }
149
150 boot() {
151         ### Setup splash-relay
152         uci get lucid.splashr 2>/dev/null || {
153 uci batch <<EOF
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
159
160         set lucid.splashredir=Redirector
161         set lucid.splashredir.name=Splashd
162         set lucid.splashredir.virtual='/'
163         set lucid.splashredir.physical=':80/luci/splash'
164
165         commit lucid
166 EOF
167         }
168
169         ### We are started by the firewall include
170         exit 0
171 }
172
173 start() {
174         lock -w $LOCK && lock $LOCK
175
176         ### Find QoS limits
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
180
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))}"
185
186         ### Load required modules
187         [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
188                 silent insmod cls_fw
189                 silent insmod cls_u32
190                 silent insmod sch_htb
191                 silent insmod sch_sfq
192                 silent insmod sch_ingress
193         }
194
195         ### Create subchains
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
200
201         ### Clear iptables replay log
202         [ -s $IPT_REPLAY ] && . $IPT_REPLAY
203         echo -n > $IPT_REPLAY
204
205         ### Build the main and portal rule
206         config_foreach iface_add iface
207         config_foreach subnet_add subnet
208
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
213
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
218
219         ### Add QoS chain
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
225         }
226
227         ### Find active mac addresses
228         MACS=""
229         config_foreach mac_add lease
230         config_foreach mac_add blacklist
231         config_foreach mac_add whitelist
232
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
237         }
238
239         lock -u $LOCK
240
241         ### Populate iptables
242         [ -n "$MACS" ] && luci-splash add-rules $MACS
243 }
244
245 stop() {
246         lock -w $LOCK && lock $LOCK
247
248         ### Clear interface rules
249         config_foreach iface_del iface
250
251         silent iptables -t mangle -D PREROUTING  -j luci_splash_mark_out
252         silent iptables -t mangle -D POSTROUTING -j luci_splash_mark_in
253         
254         ### Clear subchains
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
261         
262         ### Delete subchains
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
269         
270         sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
271
272         lock -u $LOCK
273 }
274
275 clear_leases() {
276         ### Find active mac addresses
277         MACS=""
278         config_foreach mac_add lease
279
280         ### Clear leases
281         [ -n "$MACS" ] && luci-splash remove $MACS
282 }
283