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 = { }
19 limit_up = tonumber(uci:get("luci_splash", "general", "limit_up")) or 0
20 limit_down = tonumber(uci:get("luci_splash", "general", "limit_down")) or 0
22 uci:foreach("luci_splash", "iface", function(s)
24 splash_interfaces[#splash_interfaces+1] = uci:get("network", s.network, "ifname")
28 if cmd == "status" and arg then
29 if islisted("whitelist", arg) then
31 elseif islisted("blacklist", arg) then
34 local lease = haslease(arg)
35 if lease and lease.kicked then
44 elseif cmd == "add" and arg then
45 if not haslease(arg) then
48 print("already leased!")
52 elseif cmd == "remove" and arg then
55 elseif cmd == "sync" then
59 print("Usage: " .. argv[0] .. " <status|add|remove|sync> [MAC]")
64 -- Add a lease to state and invoke add_rule
65 function add_lease(mac)
66 uci:section("luci_splash", "lease", nil, {
72 uci:save("luci_splash")
76 -- Remove a lease from state and invoke remove_rule
77 function remove_lease(mac)
81 uci:delete_all("luci_splash", "lease",
82 function(s) return ( s.mac:lower() == mac ) end)
84 uci:save("luci_splash")
88 -- Add an iptables rule
89 function add_rule(mac)
90 local a, b, c, d, e, f = mac:match("(%w+):(%w+):(%w+):(%w+):(%w+):(%w+)")
91 local mac_pre = "%s%s" %{ a, b }
92 local mac_post = "%s%s%s%s" %{ c, d, e, f }
95 if limit_up > 0 and limit_down > 0 then
96 os.execute("iptables -t mangle -I luci_splash_mark -m mac --mac-source %q -j MARK --set-mark 79" % mac)
98 for _, i in ipairs(splash_interfaces) do
99 os.execute("tc filter add dev %q parent 77:0 protocol ip prio 2 " % i ..
100 "handle ::%q u32 " % handle ..
101 "match u16 0x0800 0xFFFF at -2 match u32 0x%q 0xFFFFFFFF at -12 " % mac_post ..
102 "match u16 0x%q 0xFFFF at -14 flowid 77:10" % mac_pre)
106 os.execute("iptables -t filter -I luci_splash_counter -m mac --mac-source %q -j RETURN" % mac)
107 return os.execute("iptables -t nat -I luci_splash_leases -m mac --mac-source %q -j RETURN" % mac)
111 -- Remove an iptables rule
112 function remove_rule(mac)
113 local handle = mac:match("%w+:%w+:%w+:%w+:%w+:(%w+)")
115 local function ipt_delete_foreach(args)
116 for _, r in ipairs(ipt:find(args)) do
117 if r.options and #r.options >= 2 and r.options[1] == "MAC" and
118 r.options[2]:lower() == mac:lower()
120 os.execute("iptables -t %q -D %q -m mac --mac-source %q %s 2>/dev/null"
121 %{ r.table, r.chain, mac,
122 r.target == "MARK" and "-j MARK --set-mark 79" or
123 r.target and "-j %q" % r.target or "" })
128 ipt_delete_foreach({table="filter", chain="luci_splash_counter"})
129 ipt_delete_foreach({table="mangle", chain="luci_splash_mark"})
130 ipt_delete_foreach({table="nat", chain="luci_splash_leases"})
132 for _, i in ipairs(splash_interfaces) do
133 os.execute("tc filter del dev %q parent 77:0 protocol ip prio 2 " % i ..
134 "handle 800::%q u32 2>/dev/null" % handle)
141 -- Check whether a MAC-Address is listed in the lease state list
142 function haslease(mac)
146 uci:foreach("luci_splash", "lease",
148 if section.mac:lower() == mac then
157 -- Check whether a MAC-Address is in given list
158 function islisted(what, mac)
161 uci:foreach("luci_splash", what,
163 if section.mac:lower() == mac then
173 -- Returns a list of MAC-Addresses for which a rule is existing
176 for i, r in ipairs(ipt:find({table="nat", chain="luci_splash_leases"})) do
177 if r.options and #r.options >= 2 and r.options[1] == "MAC" then
178 macs[r.options[2]:lower()] = true
181 return luci.util.keys(macs)
185 -- Synchronise leases, remove abandoned rules
188 local time = os.time()
190 -- Current leases in state files
191 local leases = uci:get_all("luci_splash")
193 -- Convert leasetime to seconds
194 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
197 uci:load("luci_splash")
198 uci:revert("luci_splash")
202 for k, v in pairs(leases) do
203 if v[".type"] == "lease" then
204 if os.difftime(time, tonumber(v.start)) > leasetime then
209 uci:section("luci_splash", "lease", nil, {
214 written[v.mac:lower()] = 1
216 elseif v[".type"] == "whitelist" or v[".type"] == "blacklist" then
217 written[v.mac:lower()] = 1
222 -- Delete rules without state
223 for i, r in ipairs(listrules()) do
224 if #r > 0 and not written[r:lower()] then
229 uci:save("luci_splash")