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