4 require("luci.model.uci")
5 require("luci.sys.iptparser")
8 local uci = luci.model.uci.cursor_state()
9 local ipt = luci.sys.iptparser.IptParser()
11 local splash_interfaces = { }
14 local limit_down_burst = 0
20 limit_up = tonumber(uci:get("luci_splash", "general", "limit_up") or 0)
21 limit_down = tonumber(uci:get("luci_splash", "general", "limit_down") or 0)
22 limit_down_burst = tonumber(uci:get("luci_splash", "general", "limit_down_burst") or limit_down * 2)
24 uci:foreach("luci_splash", "iface", function(s)
26 splash_interfaces[#splash_interfaces+1] = uci:get("network", s.network, "ifname")
30 if cmd == "status" and arg then
31 if islisted("whitelist", arg) then
33 elseif islisted("blacklist", arg) then
36 local lease = haslease(arg)
37 if lease and lease.kicked then
46 elseif cmd == "add" and arg then
47 if not haslease(arg) then
50 print("already leased!")
54 elseif cmd == "remove" and arg then
57 elseif cmd == "sync" then
61 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
66 -- Add a lease to state and invoke add_rule
67 function add_lease(mac)
68 uci:section("luci_splash", "lease", nil, {
74 uci:save("luci_splash")
78 -- Remove a lease from state and invoke remove_rule
79 function remove_lease(mac)
83 uci:delete_all("luci_splash", "lease",
84 function(s) return ( s.mac:lower() == mac ) end)
86 uci:save("luci_splash")
90 -- Add an iptables rule
91 function add_rule(mac)
92 local a, b, c, d, e, f = mac:match("(%w+):(%w+):(%w+):(%w+):(%w+):(%w+)")
93 local mac_pre = "%s%s" %{ a, b }
94 local mac_post = "%s%s%s%s" %{ c, d, e, f }
97 if limit_up > 0 and limit_down > 0 then
98 os.execute("iptables -t mangle -I luci_splash_mark -m mac --mac-source %q -j MARK --set-mark 79" % mac)
100 for _, i in ipairs(splash_interfaces) do
101 os.execute("tc filter add dev %q parent 77:0 protocol ip prio 2 " % i ..
102 "handle ::%q u32 " % handle ..
103 "match u16 0x0800 0xFFFF at -2 match u32 0x%q 0xFFFFFFFF at -12 " % mac_post ..
104 "match u16 0x%q 0xFFFF at -14 flowid 77:10" % mac_pre)
108 os.execute("iptables -t filter -I luci_splash_counter -m mac --mac-source %q -j RETURN" % mac)
109 return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
113 -- Remove an iptables rule
114 function remove_rule(mac)
115 local handle = mac:match("%w+:%w+:%w+:%w+:%w+:(%w+)")
117 local function ipt_delete_foreach(args)
118 for _, r in ipairs(ipt:find(args)) do
119 if r.options and #r.options >= 2 and r.options[1] == "MAC" and
120 r.options[2]:lower() == mac:lower()
122 os.execute("iptables -t %q -D %q -m mac --mac-source %q %s 2>/dev/null"
123 %{ r.table, r.chain, mac,
124 r.target == "MARK" and "-j MARK --set-mark 79" or
125 r.target and "-j %q" % r.target or "" })
130 ipt_delete_foreach({table="filter", chain="luci_splash_counter"})
131 ipt_delete_foreach({table="mangle", chain="luci_splash_mark"})
132 ipt_delete_foreach({table="nat", chain="luci_splash_leases"})
134 for _, i in ipairs(splash_interfaces) do
135 os.execute("tc filter del dev %q parent 77:0 protocol ip prio 2 " % i ..
136 "handle 800::%q u32 2>/dev/null" % handle)
143 -- Check whether a MAC-Address is listed in the lease state list
144 function haslease(mac)
148 uci:foreach("luci_splash", "lease",
150 if section.mac:lower() == mac then
159 -- Check whether a MAC-Address is in given list
160 function islisted(what, mac)
163 uci:foreach("luci_splash", what,
165 if section.mac:lower() == mac then
175 -- Returns a list of MAC-Addresses for which a rule is existing
178 for i, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
179 if r.options and #r.options >= 2 and r.options[1] == "MAC" then
180 macs[r.options[2]:lower()] = true
183 return luci.util.keys(macs)
187 -- Synchronise leases, remove abandoned rules
190 local time = os.time()
192 -- Current leases in state files
193 local leases = uci:get_all("luci_splash")
195 -- Convert leasetime to seconds
196 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
199 uci:load("luci_splash")
200 uci:revert("luci_splash")
204 for k, v in pairs(leases) do
205 if v[".type"] == "lease" then
206 if os.difftime(time, tonumber(v.start)) > leasetime then
208 remove_rule(v.mac, v.kicked)
211 uci:section("luci_splash", "lease", nil, {
216 written[v.mac:lower()] = 1
218 elseif v[".type"] == "whitelist" or v[".type"] == "blacklist" then
219 written[v.mac:lower()] = 1
224 -- Delete rules without state
225 for i, r in ipairs(listrules()) do
226 if #r > 0 and not written[r:lower()] then
231 uci:save("luci_splash")