summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-splash/root/etc/init.d/luci_splash
blob: 582c2e2f95c3974aa123efe626ac639fedcadc13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh /etc/rc.common
START=70
EXTRA_COMMANDS=clear_leases
SPLASH_INTERFACES=""
LIMIT_DOWN=0
LIMIT_DOWN_BURST=0
LIMIT_UP=0

silent() {
	"$@" 2>/dev/null
}

iface_add() {
	local cfg="$1"
	
	config_get zone "$cfg" zone
	[ -n "$zone" ] || return 0
	
	config_get net "$cfg" network
	[ -n "$net" ] || return 0

	config_get ifname "$net" ifname
	[ -n "$ifname" ] || return 0
	
	config_get ipaddr "$net" ipaddr
	[ -n "$ipaddr" ] || return 0
	
	config_get netmask "$net" netmask
	[ -n "$netmask" ] || return 0
	
	eval "$(ipcalc.sh $ipaddr $netmask)"

	iptables -t nat -A prerouting_${zone} -j luci_splash_prerouting
	iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -p ! tcp -j luci_splash_portal
	iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d ! "$ipaddr" -j luci_splash_portal
	iptables -t nat -A luci_splash_prerouting -s "$NETWORK/$PREFIX" -d "$ipaddr" -p tcp -m multiport ! --dport 22,80,443 -j luci_splash_portal

	qos_iface_add "$ifname"

	append SPLASH_INTERFACES "$ifname"
}

iface_del() {
	config_get zone "$1" zone                                                                
	[ -n "$zone" ] || return 0

	config_get ifname "$1" ifname
	[ -n "$ifname" ] || return 0

	while iptables -t nat -D prerouting_${zone} -j luci_splash_prerouting 2>&-; do :; done

	qos_iface_del "$ifname"
}

blacklist_add() {
	local cfg="$1"
	
	config_get mac "$cfg" mac
	[ -n "$mac" ] && {
		iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
		iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j DROP
	}
}

whitelist_add() {
	local cfg="$1"
	
	config_get mac "$cfg" mac
	[ -n "$mac" ] && {
		iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
		iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j RETURN
	}
}

lease_add() {
	local cfg="$1"
	
	config_get mac "$cfg" mac
	config_get ban "$cfg" kicked

	ban=${ban:+DROP}

	[ -n "$mac" ] && {
		local oIFS="$IFS"; IFS=":"
		set -- $mac
		IFS="$oIFS"; unset oIFS
	
		local mac_pre="$1$2"
		local mac_post="$3$4$5$6"
		local handle="$6"

		iptables -t filter -I luci_splash_counter -m mac --mac-source "$mac" -j RETURN
		iptables -t mangle -I luci_splash_mark    -m mac --mac-source "$mac" -j MARK --set-mark 79
		iptables -t nat    -I luci_splash_leases  -m mac --mac-source "$mac" -j "${ban:-RETURN}"

		for i in $SPLASH_INTERFACES; do
			tc filter add dev $i parent 77:0 protocol ip prio 2 handle ::$handle u32 \
				match u16 0x0800 0xFFFF at -2 match u32 0x$mac_post 0xFFFFFFFF at -12 \
				match u16 0x$mac_pre 0xFFFF at -14 flowid 77:10
		done
	}
}

qos_iface_add() {
	local iface="$1"

	# 77 -> download root qdisc
	# 78 -> upload root qdisc
	# 79 -> fwmark

	tc qdisc del dev "$iface" root handle 77:

	if [ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ]; then
		tc qdisc add dev "$iface" root handle 77: htb

		# assume maximum rate of 20.000 kilobit for wlan
		tc class add dev "$iface" parent 77: classid 77:1 htb rate 20000kbit

		# set download limit and burst
		tc class add dev "$iface" parent 77:1 classid 77:10 htb \
			rate ${LIMIT_DOWN}kbit ceil ${LIMIT_DOWN_BURST}kbit prio 2

		tc qdisc add dev "$iface" parent 77:10 handle 78: sfq perturb 10

		# adding ingress can result in "File exists" if qos-scripts are active
		silent tc qdisc add dev "$iface" ingress

		# set client upload speed
		tc filter add dev "$iface" parent ffff: protocol ip prio 1 \
			handle 79 fw police rate ${LIMIT_UP}kbit mtu 6k burst 6k drop
	fi	
}

qos_iface_del() {
	local iface="$1"

	tc qdisc del dev "$iface" root handle 77:
	tc qdisc del dev "$iface" root handle 78:
	tc filter del dev "$iface" parent ffff: protocol ip prio 1 handle 79 fw
}

boot() {
	### Setup splash-relay
	uci get lucid.splashr || {
uci batch <<EOF
	set lucid.splashr=daemon
	set lucid.splashr.slave=httpd
	add_list lucid.splashr.address=8082
	add_list lucid.splashr.publisher=splashredir
	set lucid.splashr.enabled=1

	set lucid.splashredir=Redirector
	set lucid.splashredir.name=Splashd
	set lucid.splashredir.virtual='/'
	set lucid.splashredir.physical=':80/luci/splash'

	commit lucid
EOF
	}

	### We are started by the firewall include
	exit 0
}

start() {
	### Read chains from config
	include /lib/network
	scan_interfaces
	config_load luci_splash
	
	### Find QoS limits
	config_get LIMIT_UP general limit_up
	config_get LIMIT_DOWN general limit_down
	config_get LIMIT_DOWN_BURST general limit_down_burst

	LIMIT_UP="${LIMIT_UP:-0}"
	LIMIT_DOWN="${LIMIT_DOWN:-0}"
	LIMIT_DOWN_BURST="${LIMIT_DOWN_BURST:-$(($LIMIT_DOWN * 2))}"

	### Load required modules
	[ "$LIMIT_IP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && {
		silent insmod cls_fw
		silent insmod cls_route
		silent insmod cls_rsvp
		silent insmod cls_rsvp6
		silent insmod cls_tcindex
		silent insmod cls_u32
		silent insmod sch_cbq
		silent insmod sch_csz
		silent insmod sch_dsmark
		silent insmod sch_gred
		silent insmod sch_hfsc
		silent insmod sch_htb
		silent insmod sch_ingress
		silent insmod sch_red
		silent insmod sch_tbf
		silent insmod sch_teql
	}

	### Create subchains
	iptables -t filter -N luci_splash_counter
	iptables -t nat    -N luci_splash_portal
	iptables -t nat    -N luci_splash_leases
	iptables -t nat    -N luci_splash_prerouting

	[ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
		iptables -t mangle -N luci_splash_mark

	### Build the main and portal rule
	config_foreach iface_add iface
	config_foreach blacklist_add blacklist
	config_foreach whitelist_add whitelist
	config_foreach lease_add lease
	
	### Build the portal rule
	iptables -t filter -I INPUT      -j luci_splash_counter
	iptables -t filter -I FORWARD    -j luci_splash_counter

	[ "$LIMIT_UP" -gt 0 -a "$LIMIT_DOWN" -gt 0 ] && \
		iptables -t mangle -I PREROUTING -j luci_splash_mark
	
	iptables -t nat -A luci_splash_portal -p udp --dport 33434:33523 -j RETURN
	iptables -t nat -A luci_splash_portal -p icmp -j RETURN
	iptables -t nat -A luci_splash_portal -p udp --dport 53 -j RETURN
	iptables -t nat -A luci_splash_portal -j luci_splash_leases
	
	### Build the leases rule
	iptables -t nat -A luci_splash_leases -p tcp --dport 80 -j REDIRECT --to-ports 8082
	iptables -t nat -A luci_splash_leases -j DROP
	
	### Add crontab entry
	test -f /etc/crontabs/root || touch /etc/crontabs/root
	grep -q luci-splash /etc/crontabs/root || {
		echo '*/5 * * * * 	/usr/sbin/luci-splash sync' >> /etc/crontabs/root
	}

	### Start the splash httpd
	start-stop-daemon -S -m -p /var/run/luci-splashd.pid -b -q -x /usr/bin/luci-splashd
}

stop() {
	### Clear interface rules
	config_load luci_splash
	config_foreach iface_del iface

	silent iptables -t filter -D INPUT      -j luci_splash_counter
	silent iptables -t filter -D FORWARD    -j luci_splash_counter
	silent iptables -t mangle -D PREROUTING -j luci_splash_mark
        
	### Clear subchains
	silent iptables -t nat    -F luci_splash_leases
	silent iptables -t nat    -F luci_splash_portal
	silent iptables -t nat    -F luci_splash_prerouting
	silent iptables -t filter -F luci_splash_counter
	silent iptables -t mangle -F luci_splash_mark
	
	### Delete subchains
	silent iptables -t nat    -X luci_splash_leases
	silent iptables -t nat    -X luci_splash_portal
	silent iptables -t nat    -X luci_splash_prerouting
	silent iptables -t filter -X luci_splash_counter
	silent iptables -t mangle -X luci_splash_mark

	### Stop the splash httpd
	start-stop-daemon -K -p /var/run/luci-splashd.pid -s KILL -q
	
	sed -ie '/\/usr\/sbin\/luci-splash sync/d' /var/spool/cron/crontabs/root
}

	
clear_leases() {
	stop
	while uci -P /var/state del luci_splash.@lease[0] 2>&-;do :; done
	start
}