3 utl = require "luci.util"
4 sys = require "luci.sys"
6 require("luci.model.uci")
7 require("luci.sys.iptparser")
10 local uci = luci.model.uci.cursor_state()
11 local ipt = luci.sys.iptparser.IptParser()
13 local fs = require "luci.fs"
14 local ip = require "luci.ip"
18 local has_ipv6 = fs.access("/proc/net/ipv6_route") and fs.access("/usr/sbin/ip6tables")
21 os.execute("lock /var/run/luci_splash.lock")
25 os.execute("lock -u /var/run/luci_splash.lock")
29 local ret = sys.exec(cmd)
32 if ret and ret ~= "" then
39 local o3, o4 = ip:match("[0-9]+%.[0-9]+%.([0-9]+)%.([0-9]+)")
41 return string.format("%02X%s", tonumber(o3), "") .. string.format("%02X%s", tonumber(o4), "")
47 function update_stats(leased, whitelisted, whitelisttotal, blacklisted, blacklisttotal)
48 local leases = uci:get_all("luci_splash_leases", "stats")
49 uci:delete("luci_splash_leases", "stats")
50 uci:section("luci_splash_leases", "stats", "stats", {
51 leases = leased or (leases and leases.leases) or 0,
52 whitelisttotal = whitelisttotal or (leased and leases.whitelisttotal) or 0,
53 whitelistonline = whitelisted or (leases and leases.whitelistonline) or 0,
54 blacklisttotal = blacklisttotal or (leases and leases.blacklisttotal) or 0,
55 blacklistonline = blacklisted or (leases and leases.blacklistonline) or 0,
57 uci:save("luci_splash_leases")
60 function get_device_for_ip(ipaddr)
62 uci:foreach("network", "interface", function(s)
63 if s.ipaddr and s.netmask then
64 local network = ip.IPv4(s.ipaddr, s.netmask)
65 if network:contains(ip.IPv4(ipaddr)) then
66 -- this should be rewritten to luci functions if possible
67 dev = utl.trim(sys.exec(". /lib/functions/network.sh; network_get_device IFNAME '" .. s['.name'] .. "'; echo $IFNAME"))
74 function get_physdev(interface)
76 dev = utl.trim(sys.exec(". /lib/functions/network.sh; network_get_device IFNAME '" .. interface .. "'; echo $IFNAME"))
82 function get_filter_handle(parent, direction, device, mac)
83 local input = utl.split(sys.exec('/usr/sbin/tc filter show dev ' .. device .. ' parent ' .. parent) or {})
86 for k, v in pairs(input) do
87 handle = v:match('filter protocol ip pref %d+ u32 fh (%d*:%d*:%d*) order')
89 local mac, mac1, mac2, mac3, mac4, mac5, mac6
90 if direction == 'src' then
91 mac1, mac2, mac3, mac4 = input[k+1]:match('match ([%a%d][%a%d])([%a%d][%a%d])([%a%d][%a%d])([%a%d][%a%d])/ffffffff')
92 mac5, mac6 = input[k+2]:match('match ([%a%d][%a%d])([%a%d][%a%d])0000/ffff0000')
94 mac1, mac2 = input[k+1]:match('match 0000([%a%d][%a%d])([%a%d][%a%d])/0000ffff')
95 mac3, mac4, mac5, mac6 = input[k+2]:match('match ([%a%d][%a%d])([%a%d][%a%d])([%a%d][%a%d])([%a%d][%a%d])/ffffffff')
97 if mac1 and mac2 and mac3 and mac4 and mac5 and mac6 then
98 mac = "%s:%s:%s:%s:%s:%s" % { mac1, mac2, mac3, mac4, mac5, mac6 }
109 function macvalid(mac)
110 if mac and mac:match(
111 "^[a-fA-F0-9][a-fA-F0-9]:[a-fA-F0-9][a-fA-F0-9]:" ..
112 "[a-fA-F0-9][a-fA-F0-9]:[a-fA-F0-9][a-fA-F0-9]:" ..
113 "[a-fA-F0-9][a-fA-F0-9]:[a-fA-F0-9][a-fA-F0-9]$"
121 function ipvalid(ipaddr)
123 return ip.IPv4(ipaddr) and true or false
130 local cmd = table.remove(argv, 1)
133 limit_up = (tonumber(uci:get("luci_splash", "general", "limit_up")) or 0) * 8
134 limit_down = (tonumber(uci:get("luci_splash", "general", "limit_down")) or 0) * 8
136 if ( cmd == "lease" or cmd == "add-rules" or cmd == "remove" or
137 cmd == "whitelist" or cmd == "blacklist" or cmd == "status" ) and #argv > 0
139 if not (macvalid(arg) or ipvalid(arg)) then
140 print("Invalid argument. The second argument must " ..
141 "be a valid IPv4 or Mac Address.")
147 local arp_cache = net.arptable()
148 local leased_macs = get_known_macs("lease")
149 local blacklist_macs = get_known_macs("blacklist")
150 local whitelist_macs = get_known_macs("whitelist")
152 for i, adr in ipairs(argv) do
154 if adr:find(":") then
157 for _, e in ipairs(arp_cache) do
158 if e["IP address"] == adr then
159 mac = e["HW address"]:lower()
165 if mac and cmd == "add-rules" then
166 if leased_macs[mac] then
167 add_lease(mac, arp_cache, true)
168 elseif blacklist_macs[mac] then
169 add_blacklist_rule(mac)
170 elseif whitelist_macs[mac] then
171 add_whitelist_rule(mac)
173 elseif mac and cmd == "status" then
174 print(leased_macs[mac] and "lease"
175 or whitelist_macs[mac] and "whitelist"
176 or blacklist_macs[mac] and "blacklist"
178 elseif mac and ( cmd == "whitelist" or cmd == "blacklist" or cmd == "lease" ) then
179 if cmd ~= "lease" and leased_macs[mac] then
180 print("Removing %s from leases" % mac)
182 leased_macs[mac] = nil
185 if cmd ~= "whitelist" and whitelist_macs[mac] then
186 print("Removing %s from whitelist" % mac)
187 remove_whitelist(mac)
188 whitelist_macs[mac] = nil
191 if cmd ~= "blacklist" and blacklist_macs[mac] then
192 print("Removing %s from blacklist" % mac)
193 remove_blacklist(mac)
194 blacklist_macs[mac] = nil
197 if cmd == "lease" and not leased_macs[mac] then
198 print("Adding %s to leases" % mac)
200 leased_macs[mac] = true
201 elseif cmd == "whitelist" and not whitelist_macs[mac] then
202 print("Adding %s to whitelist" % mac)
204 whitelist_macs[mac] = true
205 elseif cmd == "blacklist" and not blacklist_macs[mac] then
206 print("Adding %s to blacklist" % mac)
208 blacklist_macs[mac] = true
210 print("The mac %s is already %sed" %{ mac, cmd })
212 elseif mac and cmd == "remove" then
213 if leased_macs[mac] then
214 print("Removing %s from leases" % mac)
216 leased_macs[mac] = nil
217 elseif whitelist_macs[mac] then
218 print("Removing %s from whitelist" % mac)
219 remove_whitelist(mac)
220 whitelist_macs[mac] = nil
221 elseif blacklist_macs[mac] then
222 print("Removing %s from blacklist" % mac)
223 remove_blacklist(mac)
224 blacklist_macs[mac] = nil
226 print("The mac %s is not known" % mac)
229 print("Can not find mac for ip %s" % argv[i])
235 elseif cmd == "sync" then
238 elseif cmd == "list" then
243 print("\n luci-splash list\n List connected, black- and whitelisted clients")
244 print("\n luci-splash sync\n Synchronize firewall rules and clear expired leases")
245 print("\n luci-splash lease <MAC-or-IP>\n Create a lease for the given address")
246 print("\n luci-splash blacklist <MAC-or-IP>\n Add given address to blacklist")
247 print("\n luci-splash whitelist <MAC-or-IP>\n Add given address to whitelist")
248 print("\n luci-splash remove <MAC-or-IP>\n Remove given address from the lease-, black- or whitelist")
255 -- Get current arp cache
256 function get_arpcache()
258 for _, entry in ipairs(net.arptable()) do
259 arpcache[entry["HW address"]:lower()] = { entry["Device"]:lower(), entry["IP address"]:lower() }
264 -- Get a list of known mac addresses
265 function get_known_macs(list)
266 local leased_macs = { }
268 if not list or list == "lease" then
269 uci:foreach("luci_splash_leases", "lease",
270 function(s) leased_macs[s.mac:lower()] = true end)
273 if not list or list == "whitelist" then
274 uci:foreach("luci_splash", "whitelist",
275 function(s) leased_macs[s.mac:lower()] = true end)
278 if not list or list == "blacklist" then
279 uci:foreach("luci_splash", "blacklist",
280 function(s) leased_macs[s.mac:lower()] = true end)
287 -- Helper to delete iptables rules
288 function ipt_delete_all(args, comp, off)
290 for i, r in ipairs(ipt:find(args)) do
291 if comp == nil or comp(r) then
292 off[r.table] = off[r.table] or { }
293 off[r.table][r.chain] = off[r.table][r.chain] or 0
295 exec("iptables -t %q -D %q %d 2>/dev/null"
296 %{ r.table, r.chain, r.index - off[r.table][r.chain] })
298 off[r.table][r.chain] = off[r.table][r.chain] + 1
303 function ipt6_delete_all(args, comp, off)
305 for i, r in ipairs(ipt:find(args)) do
306 if comp == nil or comp(r) then
307 off[r.table] = off[r.table] or { }
308 off[r.table][r.chain] = off[r.table][r.chain] or 0
310 exec("ip6tables -t %q -D %q %d 2>/dev/null"
311 %{ r.table, r.chain, r.index - off[r.table][r.chain] })
313 off[r.table][r.chain] = off[r.table][r.chain] + 1
319 -- Convert mac to uci-compatible section name
320 function convert_mac_to_secname(mac)
321 return string.gsub(mac, ":", "")
324 -- Add a lease to state and invoke add_rule
325 function add_lease(mac, arp, no_uci)
328 -- Get current ip address
330 for _, entry in ipairs(arp or net.arptable()) do
331 if entry["HW address"]:lower() == mac then
332 ipaddr = entry["IP address"]
337 -- Add lease if there is an ip addr
339 local device = get_device_for_ip(ipaddr)
341 local leased = uci:get("luci_splash_leases", "stats", "leases")
342 if type(tonumber(leased)) == "number" then
343 update_stats(leased + 1, nil, nil, nil, nil)
346 uci:section("luci_splash_leases", "lease", convert_mac_to_secname(mac), {
351 limit_down = limit_down,
354 uci:save("luci_splash_leases")
356 add_lease_rule(mac, ipaddr, device)
358 print("Found no active IP for %s, lease not added" % mac)
363 -- Remove a lease from state and invoke remove_rule
364 function remove_lease(mac)
367 uci:delete_all("luci_splash_leases", "lease",
369 if s.mac:lower() == mac then
370 remove_lease_rule(mac, s.ipaddr, s.device, tonumber(s.limit_up), tonumber(s.limit_down))
371 local leased = uci:get("luci_splash_leases", "stats", "leases")
372 if type(tonumber(leased)) == "number" and tonumber(leased) > 0 then
373 update_stats(leased - 1, nil, nil, nil, nil)
380 uci:save("luci_splash_leases")
384 -- Add a whitelist entry
385 function add_whitelist(mac)
386 uci:section("luci_splash", "whitelist", convert_mac_to_secname(mac), { mac = mac })
387 uci:save("luci_splash")
388 uci:commit("luci_splash")
389 add_whitelist_rule(mac)
393 -- Add a blacklist entry
394 function add_blacklist(mac)
395 uci:section("luci_splash", "blacklist", convert_mac_to_secname(mac), { mac = mac })
396 uci:save("luci_splash")
397 uci:commit("luci_splash")
398 add_blacklist_rule(mac)
402 -- Remove a whitelist entry
403 function remove_whitelist(mac)
405 uci:delete_all("luci_splash", "whitelist",
406 function(s) return not s.mac or s.mac:lower() == mac end)
407 uci:save("luci_splash")
408 uci:commit("luci_splash")
409 remove_lease_rule(mac)
410 remove_whitelist_tc(mac)
413 function remove_whitelist_tc(mac)
415 print("Removing whitelist filters for " .. mac)
417 uci:foreach("luci_splash", "iface", function(s)
418 local device = get_physdev(s['.name'])
419 if device and device ~= "" then
420 local handle = get_filter_handle('ffff:', 'src', device, mac)
421 exec('tc filter del dev "%s" parent ffff: protocol ip prio 1 handle %s u32' % { device, handle })
422 local handle = get_filter_handle('1:', 'dest', device, mac)
423 exec('tc filter del dev "%s" parent 1:0 protocol ip prio 1 handle %s u32' % { device, handle })
428 -- Remove a blacklist entry
429 function remove_blacklist(mac)
431 uci:delete_all("luci_splash", "blacklist",
432 function(s) return not s.mac or s.mac:lower() == mac end)
433 uci:save("luci_splash")
434 uci:commit("luci_splash")
435 remove_lease_rule(mac)
439 -- Add an iptables rule
440 function add_lease_rule(mac, ipaddr, device)
446 exec("iptables -t mangle -I luci_splash_mark_out -m mac --mac-source %q -j RETURN" % mac)
448 if id and device then
449 exec("iptables -t mangle -I luci_splash_mark_in -d %q -j MARK --set-mark 0x1%s -m comment --comment %s" % {ipaddr, id, mac:upper()})
453 exec("ip6tables -t mangle -I luci_splash_mark_out -m mac --mac-source %q -j MARK --set-mark 79" % mac)
454 -- not working yet, needs the ip6addr
455 --exec("ip6tables -t mangle -I luci_splash_mark_in -d %q -j MARK --set-mark 80 -m comment --comment %s" % {ipaddr, mac:upper()})
459 if device and tonumber(limit_up) > 0 then
460 exec('tc filter add dev "%s" parent ffff: protocol ip prio 2 u32 match ether src %s police rate %skbit mtu 6k burst 6k drop' % {device, mac, limit_up})
463 if id and device and tonumber(limit_down) > 0 then
464 exec("tc class add dev %s parent 1: classid 1:0x%s htb rate %skbit" % { device, id, limit_down })
465 exec("tc qdisc add dev %s parent 1:%s sfq perturb 10" % { device, id })
468 exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
469 exec("iptables -t nat -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
471 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
476 -- Remove lease, black- or whitelist rules
477 function remove_lease_rule(mac, ipaddr, device, limit_up, limit_down)
484 ipt_delete_all({table="mangle", chain="luci_splash_mark_in", options={"/*", mac:upper()}})
485 ipt_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", mac:upper()}})
486 ipt_delete_all({table="filter", chain="luci_splash_filter", options={"MAC", mac:upper()}})
487 ipt_delete_all({table="nat", chain="luci_splash_leases", options={"MAC", mac:upper()}})
489 ipt6_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", mac:upper()}})
490 ipt6_delete_all({table="filter", chain="luci_splash_filter", options={"MAC", mac:upper()}})
492 if device and tonumber(limit_up) > 0 then
493 local handle = get_filter_handle('ffff:', 'src', device, mac)
495 exec('tc filter del dev "%s" parent ffff: protocol ip prio 2 handle %s u32 police rate %skbit mtu 6k burst 6k drop' % {device, handle, limit_up})
499 -- remove clients class
500 if device and id then
501 exec('tc class del dev "%s" classid 1:%s' % {device, id})
502 exec('tc qdisc del dev "%s" parent 1:%s sfq perturb 10' % { device, id })
508 -- Add whitelist rules
509 function add_whitelist_rule(mac)
510 exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
511 exec("iptables -t nat -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
513 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
515 uci:foreach("luci_splash", "iface", function(s)
516 local device = get_physdev(s['.name'])
517 if device and device ~= "" then
518 exec('tc filter add dev "%s" parent ffff: protocol ip prio 1 u32 match ether src %s police pass' % { device, mac })
519 exec('tc filter add dev "%s" parent 1:0 protocol ip prio 1 u32 match ether dst %s classid 1:1' % { device, mac })
525 -- Add blacklist rules
526 function add_blacklist_rule(mac)
527 exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j DROP" % mac)
529 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j DROP" % mac)
534 -- Synchronise leases, remove abandoned rules
538 local time = os.time()
540 -- Current leases in state files
541 local leases = uci:get_all("luci_splash_leases")
543 -- Convert leasetime to seconds
544 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
547 uci:load("luci_splash_leases")
548 uci:revert("luci_splash_leases")
552 for k, v in pairs(leases) do
553 if v[".type"] == "lease" then
554 if os.difftime(time, tonumber(v.start)) > leasetime then
556 remove_lease_rule(v.mac, v.ipaddr, v.device, tonumber(v.limit_up), tonumber(v.limit_down))
558 leasecount = leasecount + 1
560 uci:section("luci_splash_leases", "lease", convert_mac_to_secname(v.mac), {
565 limit_down = limit_down,
572 -- Get the mac addresses of current leases
573 local macs = get_known_macs()
574 local arpcache = get_arpcache()
576 local blackwhitelist = uci:get_all("luci_splash")
577 local whitelist_total = 0
578 local whitelist_online = 0
579 local blacklist_total = 0
580 local blacklist_online = 0
582 -- Whitelist, Blacklist
583 for _, s in utl.spairs(blackwhitelist,
584 function(a,b) return blackwhitelist[a][".type"] > blackwhitelist[b][".type"] end
586 if (s[".type"] == "whitelist") then
587 whitelist_total = whitelist_total + 1
589 local mac = s.mac:lower()
590 if arpcache[mac] then
591 whitelist_online = whitelist_online + 1
595 if (s[".type"] == "blacklist") then
596 blacklist_total = blacklist_total + 1
598 local mac = s.mac:lower()
599 if arpcache[mac] then
600 blacklist_online = blacklist_online + 1
606 update_stats(leasecount, whitelist_online, whitelist_total, blacklist_online, blacklist_total)
608 uci:save("luci_splash_leases")
612 ipt_delete_all({table="filter", chain="luci_splash_filter", options={"MAC"}},
613 function(r) return not macs[r.options[2]:lower()] end)
614 ipt_delete_all({table="nat", chain="luci_splash_leases", options={"MAC"}},
615 function(r) return not macs[r.options[2]:lower()] end)
616 ipt_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", "MARK", "set"}},
617 function(r) return not macs[r.options[2]:lower()] end)
618 ipt_delete_all({table="mangle", chain="luci_splash_mark_in", options={"/*", "MARK", "set"}},
619 function(r) return not macs[r.options[2]:lower()] end)
623 ipt6_delete_all({table="filter", chain="luci_splash_filter", options={"MAC"}},
624 function(r) return not macs[r.options[2]:lower()] end)
625 ipt6_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", "MARK", "set"}},
626 function(r) return not macs[r.options[2]:lower()] end)
634 local arpcache = get_arpcache()
635 -- Find traffic usage
636 local function traffic(lease)
638 local traffic_out = 0
640 local rin = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=lease.ipaddr})
641 local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", lease.mac:upper()}})
643 if rin and #rin > 0 then traffic_in = math.floor( rin[1].bytes / 1024) end
644 if rout and #rout > 0 then traffic_out = math.floor(rout[1].bytes / 1024) end
646 return traffic_in, traffic_out
650 local leases = uci:get_all("luci_splash_leases")
651 local blackwhitelist = uci:get_all("luci_splash")
654 "%-17s %-15s %-9s %-4s %-7s %20s",
655 "MAC", "IP", "State", "Dur.", "Intf.", "Traffic down/up"
659 for _, s in pairs(leases) do
660 if s[".type"] == "lease" and s.mac then
661 local ti, to = traffic(s)
662 local mac = s.mac:lower()
663 local arp = arpcache[mac]
665 "%-17s %-15s %-9s %3dm %-7s %7dKB %7dKB",
666 mac, s.ipaddr, "leased",
667 math.floor(( os.time() - tonumber(s.start) ) / 60),
668 arp and arp[1] or "?", ti, to
673 -- Whitelist, Blacklist
674 for _, s in utl.spairs(blackwhitelist,
675 function(a,b) return blackwhitelist[a][".type"] > blackwhitelist[b][".type"] end
677 if (s[".type"] == "whitelist" or s[".type"] == "blacklist") and s.mac then
678 local mac = s.mac:lower()
679 local arp = arpcache[mac]
681 "%-17s %-15s %-9s %4s %-7s %9s %9s",
682 mac, arp and arp[2] or "?", s[".type"],
683 "- ", arp and arp[1] or "?", "-", "-"