de5332330b2af50566784718bc6bd5ac238fa866
[project/luci.git] / applications / luci-splash / root / usr / sbin / luci-splash
1 #!/usr/bin/lua
2
3 utl = require "luci.util"
4 sys = require "luci.sys"
5
6 require("luci.model.uci")
7 require("luci.sys.iptparser")
8
9 -- Init state session
10 local uci = luci.model.uci.cursor_state()
11 local ipt = luci.sys.iptparser.IptParser()
12 local net = sys.net
13 local fs = require "luci.fs"
14 local ip = require "luci.ip"
15
16 local debug = false
17
18 local has_ipv6 = fs.access("/proc/net/ipv6_route") and fs.access("/usr/sbin/ip6tables")
19
20 function lock()
21         os.execute("lock /var/run/luci_splash.lock")
22 end
23
24 function unlock()
25         os.execute("lock -u /var/run/luci_splash.lock")
26 end
27
28 function exec(cmd)
29         local ret = sys.exec(cmd)
30         if debug then
31                 print('+ ' .. cmd)
32                 if ret and ret ~= "" then
33                         print(ret)
34                 end
35         end
36 end
37
38 function get_id(ip)
39         local o3, o4 = ip:match("[0-9]+%.[0-9]+%.([0-9]+)%.([0-9]+)")
40         if o3 and 04 then
41                 return string.format("%02X%s", tonumber(o3), "") .. string.format("%02X%s", tonumber(o4), "")
42         else
43                 return false
44         end
45 end
46
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,
56         })
57         uci:save("luci_splash_leases")
58 end
59
60 function get_device_for_ip(ipaddr)
61         local dev
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"))
68                         end
69                 end
70         end)
71         return dev
72 end
73
74 function get_physdev(interface)
75         local dev
76         dev = utl.trim(sys.exec(". /lib/functions/network.sh; network_get_device IFNAME '" ..  interface .. "'; echo $IFNAME"))
77         return dev
78 end
79
80
81
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 {})
84         local tbl = {}
85         local handle
86         for k, v in pairs(input) do
87                 handle = v:match('filter protocol ip pref %d+ u32 fh (%d*:%d*:%d*) order')
88                 if handle then
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')
93                         else
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')
96                         end
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 }
99                                 tbl[mac] = handle
100                         end
101                 end
102         end
103         if tbl[mac] then
104                 handle = tbl[mac]
105         end
106         return handle
107 end
108
109 function main(argv)
110         local cmd = table.remove(argv, 1)
111         local arg = argv[1]
112
113         limit_up = (tonumber(uci:get("luci_splash", "general", "limit_up")) or 0) * 8
114         limit_down = (tonumber(uci:get("luci_splash", "general", "limit_down")) or 0) * 8
115
116         if ( cmd == "lease" or cmd == "add-rules" or cmd == "remove" or
117              cmd == "whitelist" or cmd == "blacklist" or cmd == "status" ) and #argv > 0
118         then
119                 lock()
120
121                 local arp_cache      = net.arptable()
122                 local leased_macs    = get_known_macs("lease")
123                 local blacklist_macs = get_known_macs("blacklist")
124                 local whitelist_macs = get_known_macs("whitelist")
125
126                 for i, adr in ipairs(argv) do
127                         local mac = nil
128                         if adr:find(":") then
129                                 mac = adr:lower()
130                         else
131                                 for _, e in ipairs(arp_cache) do
132                                         if e["IP address"] == adr then
133                                                 mac = e["HW address"]:lower()
134                                                 break
135                                         end
136                                 end
137                         end
138
139                         if mac and cmd == "add-rules" then
140                                 if leased_macs[mac] then
141                                         add_lease(mac, arp_cache, true)
142                                 elseif blacklist_macs[mac] then
143                                         add_blacklist_rule(mac)
144                                 elseif whitelist_macs[mac] then
145                                         add_whitelist_rule(mac)
146                                 end
147                         elseif mac and cmd == "status" then
148                                 print(leased_macs[mac] and "lease"
149                                         or whitelist_macs[mac] and "whitelist"
150                                         or blacklist_macs[mac] and "blacklist"
151                                         or "new")
152                         elseif mac and ( cmd == "whitelist" or cmd == "blacklist" or cmd == "lease" ) then
153                                 if cmd ~= "lease" and leased_macs[mac] then
154                                         print("Removing %s from leases" % mac)
155                                         remove_lease(mac)
156                                         leased_macs[mac] = nil
157                                 end
158
159                                 if cmd ~= "whitelist" and whitelist_macs[mac] then
160                                         print("Removing %s from whitelist" % mac)
161                                         remove_whitelist(mac)
162                                         whitelist_macs[mac] = nil                                       
163                                 end
164
165                                 if cmd ~= "blacklist" and blacklist_macs[mac] then
166                                         print("Removing %s from blacklist" % mac)
167                                         remove_blacklist(mac)
168                                         blacklist_macs[mac] = nil
169                                 end
170
171                                 if cmd == "lease" and not leased_macs[mac] then
172                                         print("Adding %s to leases" % mac)
173                                         add_lease(mac)
174                                         leased_macs[mac] = true
175                                 elseif cmd == "whitelist" and not whitelist_macs[mac] then
176                                         print("Adding %s to whitelist" % mac)
177                                         add_whitelist(mac)
178                                         whitelist_macs[mac] = true
179                                 elseif cmd == "blacklist" and not blacklist_macs[mac] then
180                                         print("Adding %s to blacklist" % mac)
181                                         add_blacklist(mac)
182                                         blacklist_macs[mac] = true
183                                 else
184                                         print("The mac %s is already %sed" %{ mac, cmd })
185                                 end
186                         elseif mac and cmd == "remove" then
187                                 if leased_macs[mac] then
188                                         print("Removing %s from leases" % mac)
189                                         remove_lease(mac)
190                                         leased_macs[mac] = nil
191                                 elseif whitelist_macs[mac] then
192                                         print("Removing %s from whitelist" % mac)
193                                         remove_whitelist(mac)
194                                         whitelist_macs[mac] = nil                                       
195                                 elseif blacklist_macs[mac] then
196                                         print("Removing %s from blacklist" % mac)
197                                         remove_blacklist(mac)
198                                         blacklist_macs[mac] = nil
199                                 else
200                                         print("The mac %s is not known" % mac)
201                                 end
202                         else
203                                 print("Can not find mac for ip %s" % argv[i])
204                         end
205                 end
206
207                 unlock()
208                 os.exit(0)      
209         elseif cmd == "sync" then
210                 sync()
211                 os.exit(0)
212         elseif cmd == "list" then
213                 list()
214                 os.exit(0)
215         else
216                 print("Usage:")
217                 print("\n  luci-splash list\n    List connected, black- and whitelisted clients")
218                 print("\n  luci-splash sync\n    Synchronize firewall rules and clear expired leases")
219                 print("\n  luci-splash lease <MAC-or-IP>\n    Create a lease for the given address")
220                 print("\n  luci-splash blacklist <MAC-or-IP>\n    Add given address to blacklist")
221                 print("\n  luci-splash whitelist <MAC-or-IP>\n    Add given address to whitelist")
222                 print("\n  luci-splash remove <MAC-or-IP>\n    Remove given address from the lease-, black- or whitelist")
223                 print("")
224
225                 os.exit(1)      
226         end
227 end
228
229 -- Get current arp cache
230 function get_arpcache()
231         local arpcache = { }
232         for _, entry in ipairs(net.arptable()) do
233                 arpcache[entry["HW address"]:lower()] = { entry["Device"]:lower(), entry["IP address"]:lower() }
234         end
235         return arpcache
236 end
237
238 -- Get a list of known mac addresses
239 function get_known_macs(list)
240         local leased_macs = { }
241
242         if not list or list == "lease" then
243                 uci:foreach("luci_splash_leases", "lease",
244                         function(s) leased_macs[s.mac:lower()] = true end)
245         end
246
247         if not list or list == "whitelist" then
248                 uci:foreach("luci_splash", "whitelist",
249                         function(s) leased_macs[s.mac:lower()] = true end)
250         end
251
252         if not list or list == "blacklist" then
253                 uci:foreach("luci_splash", "blacklist",
254                         function(s) leased_macs[s.mac:lower()] = true end)
255         end
256
257         return leased_macs
258 end
259
260
261 -- Helper to delete iptables rules
262 function ipt_delete_all(args, comp, off)
263         off = off or { }
264         for i, r in ipairs(ipt:find(args)) do
265                 if comp == nil or comp(r) then
266                         off[r.table] = off[r.table] or { }
267                         off[r.table][r.chain] = off[r.table][r.chain] or 0
268
269                         exec("iptables -t %q -D %q %d 2>/dev/null"
270                                 %{ r.table, r.chain, r.index - off[r.table][r.chain] })
271
272                         off[r.table][r.chain] = off[r.table][r.chain] + 1
273                 end
274         end
275 end
276
277 function ipt6_delete_all(args, comp, off)
278         off = off or { }
279         for i, r in ipairs(ipt:find(args)) do
280                 if comp == nil or comp(r) then
281                         off[r.table] = off[r.table] or { }
282                         off[r.table][r.chain] = off[r.table][r.chain] or 0
283
284                         exec("ip6tables -t %q -D %q %d 2>/dev/null"
285                                 %{ r.table, r.chain, r.index - off[r.table][r.chain] })
286
287                         off[r.table][r.chain] = off[r.table][r.chain] + 1
288                 end
289         end
290 end
291
292
293 -- Convert mac to uci-compatible section name
294 function convert_mac_to_secname(mac)
295         return string.gsub(mac, ":", "")
296 end
297
298 -- Add a lease to state and invoke add_rule
299 function add_lease(mac, arp, no_uci)
300         mac = mac:lower()
301
302         -- Get current ip address
303         local ipaddr
304         for _, entry in ipairs(arp or net.arptable()) do
305                 if entry["HW address"]:lower() == mac then
306                         ipaddr = entry["IP address"]
307                         break
308                 end
309         end
310
311         -- Add lease if there is an ip addr
312         if ipaddr then
313                 local device = get_device_for_ip(ipaddr)
314                 if not no_uci then
315                         local leased = uci:get("luci_splash_leases", "stats", "leases")
316                         if type(tonumber(leased)) == "number" then
317                                 update_stats(leased + 1, nil, nil, nil, nil)
318                         end
319
320                         uci:section("luci_splash_leases", "lease", convert_mac_to_secname(mac), {
321                                 mac    = mac,
322                                 ipaddr = ipaddr,
323                                 device = device,
324                                 limit_up = limit_up,
325                                 limit_down = limit_down,
326                                 start  = os.time()
327                         })
328                         uci:save("luci_splash_leases")
329                 end
330                 add_lease_rule(mac, ipaddr, device)
331         else
332                 print("Found no active IP for %s, lease not added" % mac)
333         end
334 end
335
336
337 -- Remove a lease from state and invoke remove_rule
338 function remove_lease(mac)
339         mac = mac:lower()
340
341         uci:delete_all("luci_splash_leases", "lease",
342                 function(s)
343                         if s.mac:lower() == mac then
344                                 remove_lease_rule(mac, s.ipaddr, s.device, tonumber(s.limit_up), tonumber(s.limit_down))
345                                 local leased = uci:get("luci_splash_leases", "stats", "leases")
346                                 if type(tonumber(leased)) == "number" and tonumber(leased) > 0 then
347                                         update_stats(leased - 1, nil, nil, nil, nil)
348                                 end
349                                 return true
350                         end
351                         return false
352                 end)
353                 
354         uci:save("luci_splash_leases")
355 end
356
357
358 -- Add a whitelist entry
359 function add_whitelist(mac)
360         uci:section("luci_splash", "whitelist", convert_mac_to_secname(mac), { mac = mac })
361         uci:save("luci_splash")
362         uci:commit("luci_splash")
363         add_whitelist_rule(mac)
364 end
365
366
367 -- Add a blacklist entry
368 function add_blacklist(mac)
369         uci:section("luci_splash", "blacklist", convert_mac_to_secname(mac), { mac = mac })
370         uci:save("luci_splash")
371         uci:commit("luci_splash")
372         add_blacklist_rule(mac)
373 end
374
375
376 -- Remove a whitelist entry
377 function remove_whitelist(mac)
378         mac = mac:lower()
379         uci:delete_all("luci_splash", "whitelist",
380                 function(s) return not s.mac or s.mac:lower() == mac end)
381         uci:save("luci_splash")
382         uci:commit("luci_splash")
383         remove_lease_rule(mac)
384         remove_whitelist_tc(mac)
385 end
386
387 function remove_whitelist_tc(mac)
388         if debug then
389                 print("Removing whitelist filters for " .. mac)
390         end
391         uci:foreach("luci_splash", "iface", function(s)
392                 local device = get_physdev(s['.name'])
393                 if device and device ~= "" then
394                         local handle = get_filter_handle('ffff:', 'src', device, mac)
395                         exec('tc filter del dev "%s" parent ffff: protocol ip prio 1 handle %s u32' % { device, handle })
396                         local handle = get_filter_handle('1:', 'dest', device, mac)
397                         exec('tc filter del dev "%s" parent 1:0 protocol ip prio 1 handle %s u32' % { device, handle })
398                 end
399         end)
400 end
401
402 -- Remove a blacklist entry
403 function remove_blacklist(mac)
404         mac = mac:lower()
405         uci:delete_all("luci_splash", "blacklist",
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 end
411
412
413 -- Add an iptables rule
414 function add_lease_rule(mac, ipaddr, device)
415         local id
416         if ipaddr then
417                 id = get_id(ipaddr)
418         end
419
420         exec("iptables -t mangle -I luci_splash_mark_out -m mac --mac-source %q -j RETURN" % mac)
421
422         if id and device then
423                 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()})
424         end
425
426         if has_ipv6 then
427                 exec("ip6tables -t mangle -I luci_splash_mark_out -m mac --mac-source %q -j MARK --set-mark 79" % mac)
428                 -- not working yet, needs the ip6addr
429                 --exec("ip6tables -t mangle -I luci_splash_mark_in -d %q -j MARK --set-mark 80 -m comment --comment %s" % {ipaddr, mac:upper()})
430         end
431
432
433         if device and tonumber(limit_up) > 0 then
434                 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})
435         end
436
437         if id and device and tonumber(limit_down) > 0 then
438                 exec("tc class add dev %s parent 1: classid 1:0x%s htb rate %skbit" % { device, id, limit_down })
439                 exec("tc qdisc add dev %s parent 1:%s sfq perturb 10" % { device, id })
440         end
441
442         exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
443         exec("iptables -t nat    -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
444         if has_ipv6 then
445                 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
446         end
447 end
448
449
450 -- Remove lease, black- or whitelist rules
451 function remove_lease_rule(mac, ipaddr, device, limit_up, limit_down)
452         local id
453         if ipaddr then
454                 id = get_id(ipaddr)
455         end
456
457         ipt:resync()
458         ipt_delete_all({table="mangle", chain="luci_splash_mark_in", options={"/*", mac:upper()}})
459         ipt_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", mac:upper()}})
460         ipt_delete_all({table="filter", chain="luci_splash_filter",   options={"MAC", mac:upper()}})
461         ipt_delete_all({table="nat",    chain="luci_splash_leases",   options={"MAC", mac:upper()}})
462         if has_ipv6 then
463                 ipt6_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", mac:upper()}})
464                 ipt6_delete_all({table="filter", chain="luci_splash_filter",   options={"MAC", mac:upper()}})
465         end
466         if device and tonumber(limit_up) > 0 then
467                 local handle = get_filter_handle('ffff:', 'src', device, mac)
468                 if handle then
469                         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})
470                 end
471         end
472
473         -- remove clients class
474         if device and id then
475                 exec('tc class del dev "%s" classid 1:%s' % {device, id})
476                 exec('tc qdisc del dev "%s" parent 1:%s sfq perturb 10' % { device, id })
477         end
478
479 end
480
481
482 -- Add whitelist rules
483 function add_whitelist_rule(mac)
484         exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
485         exec("iptables -t nat    -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
486         if has_ipv6 then
487                 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j RETURN" % mac)
488         end
489         uci:foreach("luci_splash", "iface", function(s)
490                 local device = get_physdev(s['.name'])
491                 if device and device ~= "" then
492                         exec('tc filter add dev "%s" parent ffff: protocol ip prio 1 u32 match ether src %s police pass' % { device, mac })
493                         exec('tc filter add dev "%s" parent 1:0 protocol ip prio 1 u32 match ether dst %s classid 1:1' % { device, mac })
494                 end
495         end)
496 end
497
498
499 -- Add blacklist rules
500 function add_blacklist_rule(mac)
501         exec("iptables -t filter -I luci_splash_filter -m mac --mac-source %q -j DROP" % mac)
502         if has_ipv6 then
503                 exec("ip6tables -t filter -I luci_splash_filter -m mac --mac-source %q -j DROP" % mac)
504         end
505 end
506
507
508 -- Synchronise leases, remove abandoned rules
509 function sync()
510         lock()
511
512         local time = os.time()
513
514         -- Current leases in state files
515         local leases = uci:get_all("luci_splash_leases")
516         
517         -- Convert leasetime to seconds
518         local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
519         
520         -- Clean state file
521         uci:load("luci_splash_leases")
522         uci:revert("luci_splash_leases")
523         
524         -- For all leases
525         local leasecount = 0
526         for k, v in pairs(leases) do
527                 if v[".type"] == "lease" then
528                         if os.difftime(time, tonumber(v.start)) > leasetime then
529                                 -- Remove expired
530                                 remove_lease_rule(v.mac, v.ipaddr, v.device, tonumber(v.limit_up), tonumber(v.limit_down))
531                         else
532                                 leasecount = leasecount + 1
533                                 -- Rewrite state
534                                 uci:section("luci_splash_leases", "lease", convert_mac_to_secname(v.mac), {             
535                                         mac    = v.mac,
536                                         ipaddr = v.ipaddr,
537                                         device = v.device,
538                                         limit_up = limit_up,
539                                         limit_down = limit_down,
540                                         start  = v.start
541                                 })
542                         end
543                 end
544         end
545         
546         -- Get the mac addresses of current leases
547         local macs = get_known_macs()
548         local arpcache = get_arpcache()
549
550         local blackwhitelist = uci:get_all("luci_splash")
551         local whitelist_total = 0
552         local whitelist_online = 0
553         local blacklist_total = 0
554         local blacklist_online = 0
555
556         -- Whitelist, Blacklist
557         for _, s in utl.spairs(blackwhitelist,
558                 function(a,b) return blackwhitelist[a][".type"] > blackwhitelist[b][".type"] end
559         ) do
560                 if (s[".type"] == "whitelist") then
561                         whitelist_total = whitelist_total + 1
562                         if s.mac then
563                                 local mac = s.mac:lower()
564                                 if arpcache[mac] then
565                                         whitelist_online = whitelist_online + 1
566                                 end
567                         end
568                 end
569                 if (s[".type"] == "blacklist") then
570                         blacklist_total = blacklist_total + 1
571                         if s.mac then
572                                 local mac = s.mac:lower()
573                                 if arpcache[mac] then
574                                         blacklist_online = blacklist_online + 1
575                                 end
576                         end
577                 end
578         end
579
580         update_stats(leasecount, whitelist_online, whitelist_total, blacklist_online, blacklist_total)
581
582         uci:save("luci_splash_leases")
583
584         ipt:resync()
585
586         ipt_delete_all({table="filter", chain="luci_splash_filter", options={"MAC"}},
587                 function(r) return not macs[r.options[2]:lower()] end)
588         ipt_delete_all({table="nat", chain="luci_splash_leases", options={"MAC"}},
589                 function(r) return not macs[r.options[2]:lower()] end)
590         ipt_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", "MARK", "set"}},
591                 function(r) return not macs[r.options[2]:lower()] end)
592         ipt_delete_all({table="mangle", chain="luci_splash_mark_in", options={"/*", "MARK", "set"}},
593                 function(r) return not macs[r.options[2]:lower()] end)
594
595
596         if has_ipv6 then
597                 ipt6_delete_all({table="filter", chain="luci_splash_filter", options={"MAC"}},
598                         function(r) return not macs[r.options[2]:lower()] end)
599                 ipt6_delete_all({table="mangle", chain="luci_splash_mark_out", options={"MAC", "MARK", "set"}},
600                         function(r) return not macs[r.options[2]:lower()] end)
601         end
602
603         unlock()
604 end
605
606 -- Show client info
607 function list()
608         local arpcache = get_arpcache()
609         -- Find traffic usage
610         local function traffic(lease)
611                 local traffic_in  = 0
612                 local traffic_out = 0
613
614                 local rin  = ipt:find({table="mangle", chain="luci_splash_mark_in", destination=lease.ipaddr})
615                 local rout = ipt:find({table="mangle", chain="luci_splash_mark_out", options={"MAC", lease.mac:upper()}})
616
617                 if rin  and #rin  > 0 then traffic_in  = math.floor( rin[1].bytes / 1024) end
618                 if rout and #rout > 0 then traffic_out = math.floor(rout[1].bytes / 1024) end
619
620                 return traffic_in, traffic_out
621         end
622
623         -- Print listings
624         local leases = uci:get_all("luci_splash_leases")
625         local blackwhitelist = uci:get_all("luci_splash")
626
627         print(string.format(
628                 "%-17s  %-15s  %-9s  %-4s  %-7s  %20s",
629                 "MAC", "IP", "State", "Dur.", "Intf.", "Traffic down/up"
630         ))
631
632         -- Leases
633         for _, s in pairs(leases) do
634                 if s[".type"] == "lease" and s.mac then
635                         local ti, to = traffic(s)
636                         local mac = s.mac:lower()
637                         local arp = arpcache[mac]
638                         print(string.format(
639                                 "%-17s  %-15s  %-9s  %3dm  %-7s  %7dKB  %7dKB",
640                                 mac, s.ipaddr, "leased",
641                                 math.floor(( os.time() - tonumber(s.start) ) / 60),
642                                 arp and arp[1] or "?", ti, to
643                         ))
644                 end
645         end
646
647         -- Whitelist, Blacklist
648         for _, s in utl.spairs(blackwhitelist,
649                 function(a,b) return blackwhitelist[a][".type"] > blackwhitelist[b][".type"] end
650         ) do
651                 if (s[".type"] == "whitelist" or s[".type"] == "blacklist") and s.mac then
652                         local mac = s.mac:lower()
653                         local arp = arpcache[mac]
654                         print(string.format(
655                                 "%-17s  %-15s  %-9s  %4s  %-7s  %9s  %9s",
656                                 mac, arp and arp[2] or "?", s[".type"],
657                                 "- ", arp and arp[1] or "?", "-", "-"
658                         ))
659                 end
660         end
661 end
662
663 main(arg)