2ca565189f22cd5163383f51b8b86b05c9219c35
[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         eval "$(ipcalc.sh $ipaddr $netmask)"
32
33         iptables -t nat -A prerouting_${zone} -j luci_splash_prerouting
34         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -p ! tcp -j luci_splash_portal
35         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d ! "$ipaddr" -j luci_splash_portal
36         iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d "$ipaddr" -p tcp -m multiport ! --dport 22,80,443 -j luci_splash_portal
37
38         qos_iface_add "$ifname"
39
40         append SPLASH_INTERFACES "$ifname"
41 }
42
43 iface_del() {
44         config_get zone "$1" zone                                                                
45         [ -n "$zone" ] || return 0
46
47         config_get ifname "$1" ifname
48         [ -n "$ifname" ] || return 0
49
50         while iptables -t nat -D prerouting_${zone} -j luci_splash_prerouting 2>&-; do :; done
51
52         qos_iface_del "$ifname"
53 }
54
55 blacklist_add() {
56         local cfg="$1"
57         
58         config_get mac "$cfg" mac
59         [ -n "$mac" ] && {
60                 iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
61                 iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j DROP
62         }
63 }
64
65 whitelist_add() {
66         local cfg="$1"
67         
68         config_get mac "$cfg" mac
69         [ -n "$mac" ] && {
70                 iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
71                 iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j RETURN
72         }
73 }
74
75 lease_add() {
76         local cfg="$1"
77         
78         config_get mac "$cfg" mac
79         config_get ban "$cfg" kicked
80
81         ban=${ban:+DROP}
82
83         [ -n "$mac" ] && {
84                 local oIFS="$IFS"; IFS=":"
85                 set -- $mac
86                 IFS="$oIFS"; unset oIFS
87         
88                 local mac_pre="$1$2"
89                 local mac_post="$3$4$5$6"
90                 local handle="$6"
91
92                 iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
93                 iptables -t mangle -I luci_splash_mark    -m mac --mac-source "$mac" -j MARK --set-mark 79
94                 iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j "${ban:-RETURN}"
95
96                 for i in $SPLASH_INTERFACES; do
97                         tc filter add dev $i parent 77:0 protocol ip prio 2 handle ::$handle u32 \
98                                 match u16 0x0800 0xFFFF at -2 match u32 0x$mac_post 0xFFFFFFFF at -12 \
99                                 match u16 0x$mac_pre 0xFFFF at -14 flowid 77:10
100                 done
101         }
102 }
103
104 qos_iface_add() {
105         local iface="$1"
106
107         # 77 -> download root qdisc
108         # 78 -> upload root qdisc
109         # 79 -> fwmark
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 upload speed
129                 tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
130                         handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
131         fi      
132 }
133
134 qos_iface_del() {
135         local iface="$1"
136
137         silent tc qdisc del dev "$iface" root handle 77:
138         silent tc qdisc del dev "$iface" root handle 78:
139         silent tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
140 }
141
142 boot() {
143         ### Setup splash-relay
144         uci get lucid.splashr || {
145 uci batch <<EOF
146         set lucid.splashr=daemon
147         set lucid.splashr.slave=httpd
148         add_list lucid.splashr.address=8082
149         add_list lucid.splashr.publisher=splashredir
150         set lucid.splashr.enabled=1
151
152         set lucid.splashredir=Redirector
153         set lucid.splashredir.name=Splashd
154         set lucid.splashredir.virtual='/'
155         set lucid.splashredir.physical=':80/luci/splash'
156
157         commit lucid
158 EOF
159         }
160
161         ### We are started by the firewall include
162         exit 0
163 }
164
165 start() {
166         ### Read chains from config
167         include /lib/network
168         scan_interfaces
169         config_load luci_splash
170         
171         ### Find QoS limits
172         config_get LIMIT_UP general limit_up
173         config_get LIMIT_DOWN general limit_down
174         config_get LIMIT_DOWN_BURST general limit_down_burst
175
176         LIMIT_UP="${LIMIT_UP:-0}"
177         LIMIT_DOWN="${LIMIT_DOWN:-0}"
178         LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN * 2))}"
179
180         ### Load required modules
181         [ "$LIMIT_IP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
182                 silent insmod cls_fw
183                 silent insmod cls_route
184                 silent insmod cls_rsvp
185                 silent insmod cls_rsvp6
186                 silent insmod cls_tcindex
187                 silent insmod cls_u32
188                 silent insmod sch_cbq
189                 silent insmod sch_csz
190                 silent insmod sch_dsmark
191                 silent insmod sch_gred
192                 silent insmod sch_hfsc
193                 silent insmod sch_htb
194                 silent insmod sch_ingress
195                 silent insmod sch_red
196                 silent insmod sch_tbf
197                 silent insmod sch_teql
198         }
199
200         ### Create subchains
201         iptables -t filter -N luci_splash_counter
202         iptables -t nat    -N luci_splash_portal
203         iptables -t nat    -N luci_splash_leases
204         iptables -t nat    -N luci_splash_prerouting
205
206         [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
207                 iptables -t mangle -N luci_splash_mark
208
209         ### Build the main and portal rule
210         config_foreach iface_add iface
211         config_foreach blacklist_add blacklist
212         config_foreach whitelist_add whitelist
213         config_foreach lease_add lease
214         
215         ### Build the portal rule
216         iptables -t filter -I INPUT      -j luci_splash_counter
217         iptables -t filter -I FORWARD    -j luci_splash_counter
218
219         [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
220                 iptables -t mangle -I PREROUTING -j luci_splash_mark
221         
222         iptables -t nat -A luci_splash_portal -p udp --dport 33434:33523 -j RETURN
223         iptables -t nat -A luci_splash_portal -p icmp -j RETURN
224         iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
225         iptables -t nat -A luci_splash_portal -j luci_splash_leases
226         
227         ### Build the leases rule
228         iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
229         iptables -t nat -A luci_splash_leases -j DROP
230         
231         ### Add crontab entry
232         test -f /etc/crontabs/root || touch /etc/crontabs/root
233         grep -q luci-splash /etc/crontabs/root || {
234                 echo '*/5 * * * *       /usr/sbin/luci-splash sync' >> /etc/crontabs/root
235         }
236
237         ### Start the splash httpd
238         start-stop-daemon -S -m -p /var/run/luci-splashd.pid -b -q -x /usr/bin/luci-splashd
239 }
240
241 stop() {
242         ### Clear interface rules
243         config_load luci_splash
244         config_foreach iface_del iface
245
246         silent iptables -t filter -D INPUT      -j luci_splash_counter
247         silent iptables -t filter -D FORWARD    -j luci_splash_counter
248         silent iptables -t mangle -D PREROUTING -j luci_splash_mark
249         
250         ### Clear subchains
251         silent iptables -t nat    -F luci_splash_leases
252         silent iptables -t nat    -F luci_splash_portal
253         silent iptables -t nat    -F luci_splash_prerouting
254         silent iptables -t filter -F luci_splash_counter
255         silent iptables -t mangle -F luci_splash_mark
256         
257         ### Delete subchains
258         silent iptables -t nat    -X luci_splash_leases
259         silent iptables -t nat    -X luci_splash_portal
260         silent iptables -t nat    -X luci_splash_prerouting
261         silent iptables -t filter -X luci_splash_counter
262         silent iptables -t mangle -X luci_splash_mark
263
264         ### Stop the splash httpd
265         start-stop-daemon -K -p /var/run/luci-splashd.pid -s KILL -q
266         
267         sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
268 }
269
270         
271 clear_leases() {
272         stop
273         while uci -P /var/state del luci_splash.@lease[0] 2>&-;do :; done
274         start
275 }
276