1 -- Copyright 2009-2015 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
4 local type, next, pairs, ipairs, loadfile, table, select
5 = type, next, pairs, ipairs, loadfile, table, select
7 local tonumber, tostring, math = tonumber, tostring, math
9 local pcall, require, setmetatable = pcall, require, setmetatable
11 local nxo = require "nixio"
12 local nfs = require "nixio.fs"
13 local ipc = require "luci.ip"
14 local utl = require "luci.util"
15 local uci = require "luci.model.uci"
16 local lng = require "luci.i18n"
17 local jsc = require "luci.jsonc"
19 module "luci.model.network"
22 IFACE_PATTERNS_VIRTUAL = { }
23 IFACE_PATTERNS_IGNORE = { "^wmaster%d", "^wifi%d", "^hwsim%d", "^imq%d", "^ifb%d", "^mon%.wlan%d", "^sit%d", "^gre%d", "^gretap%d", "^ip6gre%d", "^ip6tnl%d", "^tunl%d", "^lo$" }
24 IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
27 protocol = utl.class()
29 local _protocols = { }
31 local _interfaces, _bridge, _switch, _tunnel, _swtopo
32 local _ubusnetcache, _ubusdevcache, _ubuswificache
35 function _filter(c, s, o, r)
36 local val = _uci:get(c, s, o)
39 if type(val) == "string" then
40 for val in val:gmatch("%S+") do
46 _uci:set(c, s, o, table.concat(l, " "))
50 elseif type(val) == "table" then
51 for _, val in ipairs(val) do
65 function _append(c, s, o, a)
66 local val = _uci:get(c, s, o) or ""
67 if type(val) == "string" then
69 for val in val:gmatch("%S+") do
75 _uci:set(c, s, o, table.concat(l, " "))
76 elseif type(val) == "table" then
78 for _, val in ipairs(val) do
88 function _stror(s1, s2)
89 if not s1 or #s1 == 0 then
90 return s2 and #s2 > 0 and s2
96 function _get(c, s, o)
97 return _uci:get(c, s, o)
100 function _set(c, s, o, v)
102 if type(v) == "boolean" then v = v and "1" or "0" end
103 return _uci:set(c, s, o, v)
105 return _uci:delete(c, s, o)
109 local function _wifi_state()
110 if not next(_ubuswificache) then
111 _ubuswificache = utl.ubus("network.wireless", "status", {}) or {}
113 return _ubuswificache
116 function _wifi_iface(x)
118 for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
126 local function _wifi_iwinfo_by_ifname(ifname, force_phy_only)
127 local stat, iwinfo = pcall(require, "iwinfo")
128 local iwtype = stat and type(ifname) == "string" and iwinfo.type(ifname)
129 local is_nonphy_op = {
141 -- if we got a type but no real netdev, we're referring to a phy
142 local phy_only = force_phy_only or (ipc.link(ifname).type ~= 1)
144 return setmetatable({}, {
145 __index = function(t, k)
146 if k == "ifname" then
148 elseif phy_only and is_nonphy_op[k] then
150 elseif iwinfo[iwtype][k] then
151 return iwinfo[iwtype][k](ifname)
158 local function _wifi_sid_by_netid(netid)
159 if type(netid) == "string" then
160 local radioname, netidx = netid:match("^(%w+)%.network(%d+)$")
161 if radioname and netidx then
164 netidx = tonumber(netidx)
165 _uci:foreach("wireless", "wifi-iface",
167 if s.device == radioname then
181 function _wifi_sid_by_ifname(ifn)
182 local sid = _wifi_sid_by_netid(ifn)
187 local _, _, netstate = _wifi_state_by_ifname(ifn)
188 if netstate and type(netstate.section) == "string" then
189 return netstate.section
193 local function _wifi_netid_by_sid(sid)
194 local t, n = _uci:get("wireless", sid)
195 if t == "wifi-iface" and n ~= nil then
196 local radioname = _uci:get("wireless", n, "device")
197 if type(radioname) == "string" then
198 local i, netid = 0, nil
200 _uci:foreach("wireless", "wifi-iface",
202 if s.device == radioname then
204 if s[".name"] == n then
205 netid = "%s.network%d" %{ radioname, i }
211 return netid, radioname
216 local function _wifi_netid_by_netname(name)
219 _uci:foreach("wireless", "wifi-iface",
222 for net in utl.imatch(s.network) do
224 netid = _wifi_netid_by_sid(s[".name"])
233 local function _wifi_state_by_sid(sid)
234 local t1, n1 = _uci:get("wireless", sid)
235 if t1 == "wifi-iface" and n1 ~= nil then
236 local radioname, radiostate
237 for radioname, radiostate in pairs(_wifi_state()) do
238 if type(radiostate) == "table" and
239 type(radiostate.interfaces) == "table"
241 local netidx, netstate
242 for netidx, netstate in ipairs(radiostate.interfaces) do
243 if type(netstate) == "table" and
244 type(netstate.section) == "string"
246 local t2, n2 = _uci:get("wireless", netstate.section)
247 if t1 == t2 and n1 == n2 then
248 return radioname, radiostate, netstate
257 local function _wifi_state_by_ifname(ifname)
258 if type(ifname) == "string" then
259 local radioname, radiostate
260 for radioname, radiostate in pairs(_wifi_state()) do
261 if type(radiostate) == "table" and
262 type(radiostate.interfaces) == "table"
264 local netidx, netstate
265 for netidx, netstate in ipairs(radiostate.interfaces) do
266 if type(netstate) == "table" and
267 type(netstate.ifname) == "string" and
268 netstate.ifname == ifname
270 return radioname, radiostate, netstate
278 function _iface_virtual(x)
280 for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
288 function _iface_ignore(x)
290 for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
298 function init(cursor)
299 _uci = cursor or _uci or uci.cursor()
311 -- read interface information
313 for n, i in ipairs(nxo.getifaddrs()) do
314 local name = i.name:match("[^:]+")
316 if _iface_virtual(name) then
320 if _tunnel[name] or not (_iface_ignore(name) or _iface_virtual(name)) then
321 _interfaces[name] = _interfaces[name] or {
322 idx = i.ifindex or n,
330 if i.family == "packet" then
331 _interfaces[name].flags = i.flags
332 _interfaces[name].stats = i.data
333 _interfaces[name].macaddr = i.addr
334 elseif i.family == "inet" then
335 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
336 elseif i.family == "inet6" then
337 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
342 -- read bridge informaton
344 for l in utl.execi("brctl show") do
345 if not l:match("STP") then
346 local r = utl.split(l, "%s+", nil, true)
352 ifnames = { _interfaces[r[4]] }
355 b.ifnames[1].bridge = b
359 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
360 b.ifnames[#b.ifnames].bridge = b
365 -- read switch topology
366 local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
367 if type(boardinfo) == "table" and type(boardinfo.switch) == "table" then
369 for switch, layout in pairs(boardinfo.switch) do
370 if type(layout) == "table" and type(layout.ports) == "table" then
376 for _, port in ipairs(layout.ports) do
377 if type(port) == "table" and
378 type(port.num) == "number" and
379 (type(port.role) == "string" or
380 type(port.device) == "string")
384 role = port.role or "cpu",
385 index = port.index or port.num
389 spec.device = port.device
390 spec.tagged = port.need_tag
391 netdevs[tostring(port.num)] = port.device
394 ports[#ports+1] = spec
397 nports[port.role] = (nports[port.role] or 0) + 1
402 table.sort(ports, function(a, b)
403 if a.role ~= b.role then
404 return (a.role < b.role)
407 return (a.index < b.index)
411 for _, port in ipairs(ports) do
412 if port.role ~= role then
417 if role == "cpu" then
418 port.label = "CPU (%s)" % port.device
419 elseif nports[role] > 1 then
420 port.label = "%s %d" %{ role:upper(), pnum }
423 port.label = role:upper()
441 function save(self, ...)
446 function commit(self, ...)
451 function ifnameof(self, x)
452 if utl.instanceof(x, interface) then
454 elseif utl.instanceof(x, protocol) then
456 elseif type(x) == "string" then
457 return x:match("^[^:]+")
461 function get_protocol(self, protoname, netname)
462 local v = _protocols[protoname]
464 return v(netname or "__dummy__")
468 function get_protocols(self)
471 for _, v in ipairs(_protocols) do
472 p[#p+1] = v("__dummy__")
477 function register_protocol(self, protoname)
478 local proto = utl.class(protocol)
480 function proto.__init__(self, name)
484 function proto.proto(self)
488 _protocols[#_protocols+1] = proto
489 _protocols[protoname] = proto
494 function register_pattern_virtual(self, pat)
495 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
499 function has_ipv6(self)
500 return nfs.access("/proc/net/ipv6_route")
503 function add_network(self, n, options)
504 local oldnet = self:get_network(n)
505 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
506 if _uci:section("network", "interface", n, options) then
509 elseif oldnet and oldnet:is_empty() then
512 for k, v in pairs(options) do
520 function get_network(self, n)
521 if n and _uci:get("network", n) == "interface" then
526 function get_networks(self)
530 _uci:foreach("network", "interface",
532 nls[s['.name']] = network(s['.name'])
536 for n in utl.kspairs(nls) do
537 nets[#nets+1] = nls[n]
543 function del_network(self, n)
544 local r = _uci:delete("network", n)
546 _uci:delete_all("network", "alias",
547 function(s) return (s.interface == n) end)
549 _uci:delete_all("network", "route",
550 function(s) return (s.interface == n) end)
552 _uci:delete_all("network", "route6",
553 function(s) return (s.interface == n) end)
555 _uci:foreach("wireless", "wifi-iface",
559 for net in utl.imatch(s.network) do
565 _uci:set("wireless", s['.name'], "network",
566 table.concat(rest, " "))
568 _uci:delete("wireless", s['.name'], "network")
575 function rename_network(self, old, new)
577 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
578 r = _uci:section("network", "interface", new, _uci:get_all("network", old))
581 _uci:foreach("network", "alias",
583 if s.interface == old then
584 _uci:set("network", s['.name'], "interface", new)
588 _uci:foreach("network", "route",
590 if s.interface == old then
591 _uci:set("network", s['.name'], "interface", new)
595 _uci:foreach("network", "route6",
597 if s.interface == old then
598 _uci:set("network", s['.name'], "interface", new)
602 _uci:foreach("wireless", "wifi-iface",
606 for net in utl.imatch(s.network) do
614 _uci:set("wireless", s['.name'], "network",
615 table.concat(list, " "))
619 _uci:delete("network", old)
625 function get_interface(self, i)
626 if _interfaces[i] or _wifi_iface(i) then
629 local netid = _wifi_netid_by_netname(i)
630 return netid and interface(netid)
634 function get_interfaces(self)
639 -- find normal interfaces
640 _uci:foreach("network", "interface",
642 for iface in utl.imatch(s.ifname) do
643 if not _iface_ignore(iface) and not _iface_virtual(iface) and not _wifi_iface(iface) then
644 nfs[iface] = interface(iface)
649 for iface in utl.kspairs(_interfaces) do
650 if not (nfs[iface] or _iface_ignore(iface) or _iface_virtual(iface) or _wifi_iface(iface)) then
651 nfs[iface] = interface(iface)
655 -- find vlan interfaces
656 _uci:foreach("network", "switch_vlan",
658 if type(s.ports) ~= "string" or
659 type(s.device) ~= "string" or
660 type(_swtopo[s.device]) ~= "table"
666 for pnum, ptag in s.ports:gmatch("(%d+)([tu]?)") do
667 local netdev = _swtopo[s.device].netdevs[pnum]
669 if not nfs[netdev] then
670 nfs[netdev] = interface(netdev)
672 _switch[netdev] = true
675 local vid = tonumber(s.vid or s.vlan)
676 if vid ~= nil and vid >= 0 and vid <= 4095 then
677 local iface = "%s.%d" %{ netdev, vid }
678 if not nfs[iface] then
679 nfs[iface] = interface(iface)
681 _switch[iface] = true
688 for iface in utl.kspairs(nfs) do
689 ifaces[#ifaces+1] = nfs[iface]
692 -- find wifi interfaces
695 _uci:foreach("wireless", "wifi-iface",
698 num[s.device] = num[s.device] and num[s.device] + 1 or 1
699 local i = "%s.network%d" %{ s.device, num[s.device] }
700 wfs[i] = interface(i)
704 for iface in utl.kspairs(wfs) do
705 ifaces[#ifaces+1] = wfs[iface]
711 function ignore_interface(self, x)
712 return _iface_ignore(x)
715 function get_wifidev(self, dev)
716 if _uci:get("wireless", dev) == "wifi-device" then
721 function get_wifidevs(self)
725 _uci:foreach("wireless", "wifi-device",
726 function(s) wfd[#wfd+1] = s['.name'] end)
729 for _, dev in utl.vspairs(wfd) do
730 devs[#devs+1] = wifidev(dev)
736 function get_wifinet(self, net)
737 local wnet = _wifi_sid_by_ifname(net)
743 function add_wifinet(self, net, options)
744 if type(options) == "table" and options.device and
745 _uci:get("wireless", options.device) == "wifi-device"
747 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
752 function del_wifinet(self, net)
753 local wnet = _wifi_sid_by_ifname(net)
755 _uci:delete("wireless", wnet)
761 function get_status_by_route(self, addr, mask)
763 for _, object in ipairs(utl.ubus()) do
764 local net = object:match("^network%.interface%.(.+)")
766 local s = utl.ubus(object, "status", {})
767 if s and s.route then
769 for _, rt in ipairs(s.route) do
770 if not rt.table and rt.target == addr and rt.mask == mask then
779 function get_status_by_address(self, addr)
781 for _, object in ipairs(utl.ubus()) do
782 local net = object:match("^network%.interface%.(.+)")
784 local s = utl.ubus(object, "status", {})
785 if s and s['ipv4-address'] then
787 for _, a in ipairs(s['ipv4-address']) do
788 if a.address == addr then
793 if s and s['ipv6-address'] then
795 for _, a in ipairs(s['ipv6-address']) do
796 if a.address == addr then
805 function get_wannet(self)
806 local net, stat = self:get_status_by_route("0.0.0.0", 0)
807 return net and network(net, stat.proto)
810 function get_wandev(self)
811 local _, stat = self:get_status_by_route("0.0.0.0", 0)
812 return stat and interface(stat.l3_device or stat.device)
815 function get_wan6net(self)
816 local net, stat = self:get_status_by_route("::", 0)
817 return net and network(net, stat.proto)
820 function get_wan6dev(self)
821 local _, stat = self:get_status_by_route("::", 0)
822 return stat and interface(stat.l3_device or stat.device)
825 function get_switch_topologies(self)
830 function network(name, proto)
832 local p = proto or _uci:get("network", name, "proto")
833 local c = p and _protocols[p] or protocol
838 function protocol.__init__(self, name)
842 function protocol._get(self, opt)
843 local v = _uci:get("network", self.sid, opt)
844 if type(v) == "table" then
845 return table.concat(v, " ")
850 function protocol._ubus(self, field)
851 if not _ubusnetcache[self.sid] then
852 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
855 if _ubusnetcache[self.sid] and field then
856 return _ubusnetcache[self.sid][field]
858 return _ubusnetcache[self.sid]
861 function protocol.get(self, opt)
862 return _get("network", self.sid, opt)
865 function protocol.set(self, opt, val)
866 return _set("network", self.sid, opt, val)
869 function protocol.ifname(self)
871 if self:is_floating() then
872 ifname = self:_ubus("l3_device")
874 ifname = self:_ubus("device")
877 ifname = _wifi_netid_by_netname(self.sid)
882 function protocol.proto(self)
886 function protocol.get_i18n(self)
887 local p = self:proto()
889 return lng.translate("Unmanaged")
890 elseif p == "static" then
891 return lng.translate("Static address")
892 elseif p == "dhcp" then
893 return lng.translate("DHCP client")
895 return lng.translate("Unknown")
899 function protocol.type(self)
900 return self:_get("type")
903 function protocol.name(self)
907 function protocol.uptime(self)
908 return self:_ubus("uptime") or 0
911 function protocol.expires(self)
912 local u = self:_ubus("uptime")
913 local d = self:_ubus("data")
915 if type(u) == "number" and type(d) == "table" and
916 type(d.leasetime) == "number"
918 local r = (d.leasetime - (u % d.leasetime))
919 return r > 0 and r or 0
925 function protocol.metric(self)
926 return self:_ubus("metric") or 0
929 function protocol.ipaddr(self)
930 local addrs = self:_ubus("ipv4-address")
931 return addrs and #addrs > 0 and addrs[1].address
934 function protocol.ipaddrs(self)
935 local addrs = self:_ubus("ipv4-address")
938 if type(addrs) == "table" then
940 for n, addr in ipairs(addrs) do
941 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
948 function protocol.netmask(self)
949 local addrs = self:_ubus("ipv4-address")
950 return addrs and #addrs > 0 and
951 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
954 function protocol.gwaddr(self)
956 for _, route in ipairs(self:_ubus("route") or { }) do
957 if route.target == "0.0.0.0" and route.mask == 0 then
963 function protocol.dnsaddrs(self)
966 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
967 if not addr:match(":") then
974 function protocol.ip6addr(self)
975 local addrs = self:_ubus("ipv6-address")
976 if addrs and #addrs > 0 then
977 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
979 addrs = self:_ubus("ipv6-prefix-assignment")
980 if addrs and #addrs > 0 then
981 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
986 function protocol.ip6addrs(self)
987 local addrs = self:_ubus("ipv6-address")
991 if type(addrs) == "table" then
992 for n, addr in ipairs(addrs) do
993 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
997 addrs = self:_ubus("ipv6-prefix-assignment")
999 if type(addrs) == "table" then
1000 for n, addr in ipairs(addrs) do
1001 rv[#rv+1] = "%s1/%d" %{ addr.address, addr.mask }
1008 function protocol.gw6addr(self)
1010 for _, route in ipairs(self:_ubus("route") or { }) do
1011 if route.target == "::" and route.mask == 0 then
1012 return ipc.IPv6(route.nexthop):string()
1017 function protocol.dns6addrs(self)
1020 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
1021 if addr:match(":") then
1028 function protocol.ip6prefix(self)
1029 local prefix = self:_ubus("ipv6-prefix")
1030 if prefix and #prefix > 0 then
1031 return "%s/%d" %{ prefix[1].address, prefix[1].mask }
1035 function protocol.is_bridge(self)
1036 return (not self:is_virtual() and self:type() == "bridge")
1039 function protocol.opkg_package(self)
1043 function protocol.is_installed(self)
1047 function protocol.is_virtual(self)
1051 function protocol.is_floating(self)
1055 function protocol.is_empty(self)
1056 if self:is_floating() then
1061 if (self:_get("ifname") or ""):match("%S+") then
1065 if empty and _wifi_netid_by_netname(self.sid) then
1073 function protocol.add_interface(self, ifname)
1074 ifname = _M:ifnameof(ifname)
1075 if ifname and not self:is_floating() then
1076 -- if its a wifi interface, change its network option
1077 local wif = _wifi_sid_by_ifname(ifname)
1079 _append("wireless", wif, "network", self.sid)
1081 -- add iface to our iface list
1083 _append("network", self.sid, "ifname", ifname)
1088 function protocol.del_interface(self, ifname)
1089 ifname = _M:ifnameof(ifname)
1090 if ifname and not self:is_floating() then
1091 -- if its a wireless interface, clear its network option
1092 local wif = _wifi_sid_by_ifname(ifname)
1093 if wif then _filter("wireless", wif, "network", self.sid) end
1095 -- remove the interface
1096 _filter("network", self.sid, "ifname", ifname)
1100 function protocol.get_interface(self)
1101 if self:is_virtual() then
1102 _tunnel[self:proto() .. "-" .. self.sid] = true
1103 return interface(self:proto() .. "-" .. self.sid, self)
1104 elseif self:is_bridge() then
1105 _bridge["br-" .. self.sid] = true
1106 return interface("br-" .. self.sid, self)
1110 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
1111 ifn = ifn:match("^[^:/]+")
1112 return ifn and interface(ifn, self)
1114 ifn = _wifi_netid_by_netname(self.sid)
1115 return ifn and interface(ifn, self)
1119 function protocol.get_interfaces(self)
1120 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
1125 for ifn in utl.imatch(self:get("ifname")) do
1126 ifn = ifn:match("^[^:/]+")
1127 nfs[ifn] = interface(ifn, self)
1130 for ifn in utl.kspairs(nfs) do
1131 ifaces[#ifaces+1] = nfs[ifn]
1135 _uci:foreach("wireless", "wifi-iface",
1139 for net in utl.imatch(s.network) do
1140 if net == self.sid then
1141 ifn = _wifi_netid_by_sid(s[".name"])
1143 wfs[ifn] = interface(ifn, self)
1150 for ifn in utl.kspairs(wfs) do
1151 ifaces[#ifaces+1] = wfs[ifn]
1158 function protocol.contains_interface(self, ifname)
1159 ifname = _M:ifnameof(ifname)
1162 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1164 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1168 for ifn in utl.imatch(self:get("ifname")) do
1169 ifn = ifn:match("[^:]+")
1170 if ifn == ifname then
1175 local wif = _wifi_sid_by_ifname(ifname)
1178 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1179 if n == self.sid then
1189 function protocol.adminlink(self)
1190 local stat, dsp = pcall(require, "luci.dispatcher")
1191 return stat and dsp.build_url("admin", "network", "network", self.sid)
1195 interface = utl.class()
1197 function interface.__init__(self, ifname, network)
1198 local wif = _wifi_sid_by_ifname(ifname)
1200 self.wif = wifinet(wif)
1201 self.ifname = self.wif:ifname()
1204 self.ifname = self.ifname or ifname
1205 self.dev = _interfaces[self.ifname]
1206 self.network = network
1209 function interface._ubus(self, field)
1210 if not _ubusdevcache[self.ifname] then
1211 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1212 { name = self.ifname })
1214 if _ubusdevcache[self.ifname] and field then
1215 return _ubusdevcache[self.ifname][field]
1217 return _ubusdevcache[self.ifname]
1220 function interface.name(self)
1221 return self.wif and self.wif:ifname() or self.ifname
1224 function interface.mac(self)
1225 local mac = self:_ubus("macaddr")
1226 return mac and mac:upper()
1229 function interface.ipaddrs(self)
1230 return self.dev and self.dev.ipaddrs or { }
1233 function interface.ip6addrs(self)
1234 return self.dev and self.dev.ip6addrs or { }
1237 function interface.type(self)
1238 if self.wif or _wifi_iface(self.ifname) then
1240 elseif _bridge[self.ifname] then
1242 elseif _tunnel[self.ifname] then
1244 elseif self.ifname:match("%.") then
1246 elseif _switch[self.ifname] then
1253 function interface.shortname(self)
1255 return self.wif:shortname()
1261 function interface.get_i18n(self)
1263 return "%s: %s %q" %{
1264 lng.translate("Wireless Network"),
1265 self.wif:active_mode(),
1266 self.wif:active_ssid() or self.wif:active_bssid() or self.wif:id()
1269 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1273 function interface.get_type_i18n(self)
1274 local x = self:type()
1276 return lng.translate("Wireless Adapter")
1277 elseif x == "bridge" then
1278 return lng.translate("Bridge")
1279 elseif x == "switch" then
1280 return lng.translate("Ethernet Switch")
1281 elseif x == "vlan" then
1282 if _switch[self.ifname] then
1283 return lng.translate("Switch VLAN")
1285 return lng.translate("Software VLAN")
1287 elseif x == "tunnel" then
1288 return lng.translate("Tunnel Interface")
1290 return lng.translate("Ethernet Adapter")
1294 function interface.adminlink(self)
1296 return self.wif:adminlink()
1300 function interface.ports(self)
1301 local members = self:_ubus("bridge-members")
1305 for _, iface in ipairs(members) do
1306 ifaces[#ifaces+1] = interface(iface)
1311 function interface.bridge_id(self)
1319 function interface.bridge_stp(self)
1327 function interface.is_up(self)
1328 return self:_ubus("up") or false
1331 function interface.is_bridge(self)
1332 return (self:type() == "bridge")
1335 function interface.is_bridgeport(self)
1336 return self.dev and self.dev.bridge and true or false
1339 function interface.tx_bytes(self)
1340 local stat = self:_ubus("statistics")
1341 return stat and stat.tx_bytes or 0
1344 function interface.rx_bytes(self)
1345 local stat = self:_ubus("statistics")
1346 return stat and stat.rx_bytes or 0
1349 function interface.tx_packets(self)
1350 local stat = self:_ubus("statistics")
1351 return stat and stat.tx_packets or 0
1354 function interface.rx_packets(self)
1355 local stat = self:_ubus("statistics")
1356 return stat and stat.rx_packets or 0
1359 function interface.get_network(self)
1360 return self:get_networks()[1]
1363 function interface.get_networks(self)
1364 if not self.networks then
1367 for _, net in ipairs(_M:get_networks()) do
1368 if net:contains_interface(self.ifname) or
1369 net:ifname() == self.ifname
1374 table.sort(nets, function(a, b) return a.sid < b.sid end)
1375 self.networks = nets
1378 return self.networks
1382 function interface.get_wifinet(self)
1387 wifidev = utl.class()
1389 function wifidev.__init__(self, name)
1390 local t, n = _uci:get("wireless", name)
1391 if t == "wifi-device" and n ~= nil then
1393 self.iwinfo = _wifi_iwinfo_by_ifname(self.sid, true)
1395 self.sid = self.sid or name
1396 self.iwinfo = self.iwinfo or { ifname = self.sid }
1399 function wifidev.get(self, opt)
1400 return _get("wireless", self.sid, opt)
1403 function wifidev.set(self, opt, val)
1404 return _set("wireless", self.sid, opt, val)
1407 function wifidev.name(self)
1411 function wifidev.hwmodes(self)
1412 local l = self.iwinfo.hwmodelist
1413 if l and next(l) then
1416 return { b = true, g = true }
1420 function wifidev.get_i18n(self)
1422 if self.iwinfo.type == "wl" then
1427 local l = self:hwmodes()
1428 if l.a then m = m .. "a" end
1429 if l.b then m = m .. "b" end
1430 if l.g then m = m .. "g" end
1431 if l.n then m = m .. "n" end
1432 if l.ac then m = "ac" end
1434 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1437 function wifidev.is_up(self)
1438 if _ubuswificache[self.sid] then
1439 return (_ubuswificache[self.sid].up == true)
1445 function wifidev.get_wifinet(self, net)
1446 if _uci:get("wireless", net) == "wifi-iface" then
1449 local wnet = _wifi_sid_by_ifname(net)
1451 return wifinet(wnet)
1456 function wifidev.get_wifinets(self)
1459 _uci:foreach("wireless", "wifi-iface",
1461 if s.device == self.sid then
1462 nets[#nets+1] = wifinet(s['.name'])
1469 function wifidev.add_wifinet(self, options)
1470 options = options or { }
1471 options.device = self.sid
1473 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1475 return wifinet(wnet, options)
1479 function wifidev.del_wifinet(self, net)
1480 if utl.instanceof(net, wifinet) then
1482 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1483 net = _wifi_sid_by_ifname(net)
1486 if net and _uci:get("wireless", net, "device") == self.sid then
1487 _uci:delete("wireless", net)
1495 wifinet = utl.class()
1497 function wifinet.__init__(self, name, data)
1498 local sid, netid, radioname, radiostate, netstate
1500 -- lookup state by radio#.network# notation
1501 sid = _wifi_sid_by_netid(name)
1504 radioname, radiostate, netstate = _wifi_state_by_sid(sid)
1506 -- lookup state by ifname (e.g. wlan0)
1507 radioname, radiostate, netstate = _wifi_state_by_ifname(name)
1508 if radioname and radiostate and netstate then
1509 sid = netstate.section
1510 netid = _wifi_netid_by_sid(sid)
1512 -- lookup state by uci section id (e.g. cfg053579)
1513 radioname, radiostate, netstate = _wifi_state_by_sid(name)
1514 if radioname and radiostate and netstate then
1516 netid = _wifi_netid_by_sid(sid)
1518 -- no state available, try to resolve from uci
1519 netid, radioname = _wifi_netid_by_sid(name)
1520 if netid and radioname then
1528 (netstate and _wifi_iwinfo_by_ifname(netstate.ifname)) or
1529 (radioname and _wifi_iwinfo_by_ifname(radioname)) or
1530 { ifname = (netid or sid or name) }
1532 self.sid = sid or name
1533 self.wdev = iwinfo.ifname
1534 self.iwinfo = iwinfo
1543 function wifinet.ubus(self, ...)
1544 local n, v = self._ubusdata
1545 for n = 1, select('#', ...) do
1546 if type(v) == "table" then
1547 v = v[select(n, ...)]
1555 function wifinet.get(self, opt)
1556 return _get("wireless", self.sid, opt)
1559 function wifinet.set(self, opt, val)
1560 return _set("wireless", self.sid, opt, val)
1563 function wifinet.mode(self)
1564 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1567 function wifinet.ssid(self)
1568 return self:ubus("net", "config", "ssid") or self:get("ssid")
1571 function wifinet.bssid(self)
1572 return self:ubus("net", "config", "bssid") or self:get("bssid")
1575 function wifinet.network(self)
1576 local net, networks = nil, { }
1577 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1578 networks[#networks+1] = net
1583 function wifinet.id(self)
1587 function wifinet.name(self)
1591 function wifinet.ifname(self)
1592 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1593 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1599 function wifinet.get_device(self)
1600 local dev = self:ubus("radio") or self:get("device")
1601 return dev and wifidev(dev) or nil
1604 function wifinet.is_up(self)
1605 local ifc = self:get_interface()
1606 return (ifc and ifc:is_up() or false)
1609 function wifinet.active_mode(self)
1610 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1612 if m == "ap" then m = "Master"
1613 elseif m == "sta" then m = "Client"
1614 elseif m == "adhoc" then m = "Ad-Hoc"
1615 elseif m == "mesh" then m = "Mesh"
1616 elseif m == "monitor" then m = "Monitor"
1622 function wifinet.active_mode_i18n(self)
1623 return lng.translate(self:active_mode())
1626 function wifinet.active_ssid(self)
1627 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1630 function wifinet.active_bssid(self)
1631 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1634 function wifinet.active_encryption(self)
1635 local enc = self.iwinfo and self.iwinfo.encryption
1636 return enc and enc.description or "-"
1639 function wifinet.assoclist(self)
1640 return self.iwinfo.assoclist or { }
1643 function wifinet.frequency(self)
1644 local freq = self.iwinfo.frequency
1645 if freq and freq > 0 then
1646 return "%.03f" % (freq / 1000)
1650 function wifinet.bitrate(self)
1651 local rate = self.iwinfo.bitrate
1652 if rate and rate > 0 then
1653 return (rate / 1000)
1657 function wifinet.channel(self)
1658 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1659 tonumber(self:get("channel"))
1662 function wifinet.signal(self)
1663 return self.iwinfo.signal or 0
1666 function wifinet.noise(self)
1667 return self.iwinfo.noise or 0
1670 function wifinet.country(self)
1671 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1674 function wifinet.txpower(self)
1675 local pwr = (self.iwinfo.txpower or 0)
1676 return pwr + self:txpower_offset()
1679 function wifinet.txpower_offset(self)
1680 return self.iwinfo.txpower_offset or 0
1683 function wifinet.signal_level(self, s, n)
1684 if self:active_bssid() ~= "00:00:00:00:00:00" then
1685 local signal = s or self:signal()
1686 local noise = n or self:noise()
1688 if signal < 0 and noise < 0 then
1689 local snr = -1 * (noise - signal)
1690 return math.floor(snr / 5)
1699 function wifinet.signal_percent(self)
1700 local qc = self.iwinfo.quality or 0
1701 local qm = self.iwinfo.quality_max or 0
1703 if qc > 0 and qm > 0 then
1704 return math.floor((100 / qm) * qc)
1710 function wifinet.shortname(self)
1712 lng.translate(self:active_mode()),
1713 self:active_ssid() or self:active_bssid() or self:id()
1717 function wifinet.get_i18n(self)
1718 return "%s: %s %q (%s)" %{
1719 lng.translate("Wireless Network"),
1720 lng.translate(self:active_mode()),
1721 self:active_ssid() or self:active_bssid() or self:id(),
1726 function wifinet.adminlink(self)
1727 local stat, dsp = pcall(require, "luci.dispatcher")
1728 return dsp and dsp.build_url("admin", "network", "wireless", self.netid)
1731 function wifinet.get_network(self)
1732 return self:get_networks()[1]
1735 function wifinet.get_networks(self)
1738 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1739 if _uci:get("network", net) == "interface" then
1740 nets[#nets+1] = network(net)
1743 table.sort(nets, function(a, b) return a.sid < b.sid end)
1747 function wifinet.get_interface(self)
1748 return interface(self:ifname())
1752 -- setup base protocols
1753 _M:register_protocol("static")
1754 _M:register_protocol("dhcp")
1755 _M:register_protocol("none")
1757 -- load protocol extensions
1758 local exts = nfs.dir(utl.libpath() .. "/model/network")
1762 if ext:match("%.lua$") then
1763 require("luci.model.network." .. ext:gsub("%.lua$", ""))