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 require = require
11 local nxo = require "nixio"
12 local nfs = require "nixio.fs"
13 local ipc = require "luci.ip"
14 local sys = require "luci.sys"
15 local utl = require "luci.util"
16 local dsp = require "luci.dispatcher"
17 local uci = require "luci.model.uci"
18 local lng = require "luci.i18n"
19 local jsc = require "luci.jsonc"
21 module "luci.model.network"
24 IFACE_PATTERNS_VIRTUAL = { }
25 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$" }
26 IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
29 protocol = utl.class()
31 local _protocols = { }
33 local _interfaces, _bridge, _switch, _tunnel, _swtopo
34 local _ubusnetcache, _ubusdevcache, _ubuswificache
37 function _filter(c, s, o, r)
38 local val = _uci:get(c, s, o)
41 if type(val) == "string" then
42 for val in val:gmatch("%S+") do
48 _uci:set(c, s, o, table.concat(l, " "))
52 elseif type(val) == "table" then
53 for _, val in ipairs(val) do
67 function _append(c, s, o, a)
68 local val = _uci:get(c, s, o) or ""
69 if type(val) == "string" then
71 for val in val:gmatch("%S+") do
77 _uci:set(c, s, o, table.concat(l, " "))
78 elseif type(val) == "table" then
80 for _, val in ipairs(val) do
90 function _stror(s1, s2)
91 if not s1 or #s1 == 0 then
92 return s2 and #s2 > 0 and s2
98 function _get(c, s, o)
99 return _uci:get(c, s, o)
102 function _set(c, s, o, v)
104 if type(v) == "boolean" then v = v and "1" or "0" end
105 return _uci:set(c, s, o, v)
107 return _uci:delete(c, s, o)
111 function _wifi_iface(x)
113 for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
121 function _wifi_state(key, val, field)
122 local radio, radiostate, ifc, ifcstate
124 if not next(_ubuswificache) then
125 _ubuswificache = utl.ubus("network.wireless", "status", {}) or {}
127 -- workaround extended section format
128 for radio, radiostate in pairs(_ubuswificache) do
129 for ifc, ifcstate in pairs(radiostate.interfaces) do
130 if ifcstate.section and ifcstate.section:sub(1, 1) == '@' then
131 local s = _uci:get_all('wireless.%s' % ifcstate.section)
133 ifcstate.section = s['.name']
140 for radio, radiostate in pairs(_ubuswificache) do
141 for ifc, ifcstate in pairs(radiostate.interfaces) do
142 if ifcstate[key] == val then
143 return ifcstate[field]
149 function _wifi_lookup(ifn)
150 -- got a radio#.network# pseudo iface, locate the corresponding section
151 local radio, ifnidx = ifn:match("^(%w+)%.network(%d+)$")
152 if radio and ifnidx then
156 ifnidx = tonumber(ifnidx)
157 _uci:foreach("wireless", "wifi-iface",
159 if s.device == radio then
161 if num == ifnidx then
170 -- looks like wifi, try to locate the section via ubus state
171 elseif _wifi_iface(ifn) then
172 return _wifi_state("ifname", ifn, "section")
176 function _iface_virtual(x)
178 for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
186 function _iface_ignore(x)
188 for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
196 function init(cursor)
197 _uci = cursor or _uci or uci.cursor()
209 -- read interface information
211 for n, i in ipairs(nxo.getifaddrs()) do
212 local name = i.name:match("[^:]+")
214 if _iface_virtual(name) then
218 if _tunnel[name] or not (_iface_ignore(name) or _iface_virtual(name)) then
219 _interfaces[name] = _interfaces[name] or {
220 idx = i.ifindex or n,
228 if i.family == "packet" then
229 _interfaces[name].flags = i.flags
230 _interfaces[name].stats = i.data
231 _interfaces[name].macaddr = i.addr
232 elseif i.family == "inet" then
233 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
234 elseif i.family == "inet6" then
235 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
240 -- read bridge informaton
242 for l in utl.execi("brctl show") do
243 if not l:match("STP") then
244 local r = utl.split(l, "%s+", nil, true)
250 ifnames = { _interfaces[r[4]] }
253 b.ifnames[1].bridge = b
257 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
258 b.ifnames[#b.ifnames].bridge = b
263 -- read switch topology
264 local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
265 if type(boardinfo) == "table" and type(boardinfo.switch) == "table" then
267 for switch, layout in pairs(boardinfo.switch) do
268 if type(layout) == "table" and type(layout.ports) == "table" then
274 for _, port in ipairs(layout.ports) do
275 if type(port) == "table" and
276 type(port.num) == "number" and
277 (type(port.role) == "string" or
278 type(port.device) == "string")
282 role = port.role or "cpu",
283 index = port.index or port.num
287 spec.device = port.device
288 spec.tagged = port.need_tag
289 netdevs[tostring(port.num)] = port.device
292 ports[#ports+1] = spec
295 nports[port.role] = (nports[port.role] or 0) + 1
300 table.sort(ports, function(a, b)
301 if a.role ~= b.role then
302 return (a.role < b.role)
305 return (a.index < b.index)
309 for _, port in ipairs(ports) do
310 if port.role ~= role then
315 if role == "cpu" then
316 port.label = "CPU (%s)" % port.device
317 elseif nports[role] > 1 then
318 port.label = "%s %d" %{ role:upper(), pnum }
321 port.label = role:upper()
339 function save(self, ...)
344 function commit(self, ...)
349 function ifnameof(self, x)
350 if utl.instanceof(x, interface) then
352 elseif utl.instanceof(x, protocol) then
354 elseif type(x) == "string" then
355 return x:match("^[^:]+")
359 function get_protocol(self, protoname, netname)
360 local v = _protocols[protoname]
362 return v(netname or "__dummy__")
366 function get_protocols(self)
369 for _, v in ipairs(_protocols) do
370 p[#p+1] = v("__dummy__")
375 function register_protocol(self, protoname)
376 local proto = utl.class(protocol)
378 function proto.__init__(self, name)
382 function proto.proto(self)
386 _protocols[#_protocols+1] = proto
387 _protocols[protoname] = proto
392 function register_pattern_virtual(self, pat)
393 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
397 function has_ipv6(self)
398 return nfs.access("/proc/net/ipv6_route")
401 function add_network(self, n, options)
402 local oldnet = self:get_network(n)
403 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
404 if _uci:section("network", "interface", n, options) then
407 elseif oldnet and oldnet:is_empty() then
410 for k, v in pairs(options) do
418 function get_network(self, n)
419 if n and _uci:get("network", n) == "interface" then
424 function get_networks(self)
428 _uci:foreach("network", "interface",
430 nls[s['.name']] = network(s['.name'])
434 for n in utl.kspairs(nls) do
435 nets[#nets+1] = nls[n]
441 function del_network(self, n)
442 local r = _uci:delete("network", n)
444 _uci:delete_all("network", "alias",
445 function(s) return (s.interface == n) end)
447 _uci:delete_all("network", "route",
448 function(s) return (s.interface == n) end)
450 _uci:delete_all("network", "route6",
451 function(s) return (s.interface == n) end)
453 _uci:foreach("wireless", "wifi-iface",
457 for net in utl.imatch(s.network) do
463 _uci:set("wireless", s['.name'], "network",
464 table.concat(rest, " "))
466 _uci:delete("wireless", s['.name'], "network")
473 function rename_network(self, old, new)
475 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
476 r = _uci:section("network", "interface", new, _uci:get_all("network", old))
479 _uci:foreach("network", "alias",
481 if s.interface == old then
482 _uci:set("network", s['.name'], "interface", new)
486 _uci:foreach("network", "route",
488 if s.interface == old then
489 _uci:set("network", s['.name'], "interface", new)
493 _uci:foreach("network", "route6",
495 if s.interface == old then
496 _uci:set("network", s['.name'], "interface", new)
500 _uci:foreach("wireless", "wifi-iface",
504 for net in utl.imatch(s.network) do
512 _uci:set("wireless", s['.name'], "network",
513 table.concat(list, " "))
517 _uci:delete("network", old)
523 function get_interface(self, i)
524 if _interfaces[i] or _wifi_iface(i) then
529 _uci:foreach("wireless", "wifi-iface",
532 num[s.device] = num[s.device] and num[s.device] + 1 or 1
533 if s['.name'] == i then
535 "%s.network%d" %{s.device, num[s.device] })
544 function get_interfaces(self)
549 -- find normal interfaces
550 _uci:foreach("network", "interface",
552 for iface in utl.imatch(s.ifname) do
553 if not _iface_ignore(iface) and not _iface_virtual(iface) and not _wifi_iface(iface) then
554 nfs[iface] = interface(iface)
559 for iface in utl.kspairs(_interfaces) do
560 if not (nfs[iface] or _iface_ignore(iface) or _iface_virtual(iface) or _wifi_iface(iface)) then
561 nfs[iface] = interface(iface)
565 -- find vlan interfaces
566 _uci:foreach("network", "switch_vlan",
568 if type(s.ports) ~= "string" or
569 type(s.device) ~= "string" or
570 type(_swtopo[s.device]) ~= "table"
576 for pnum, ptag in s.ports:gmatch("(%d+)([tu]?)") do
577 local netdev = _swtopo[s.device].netdevs[pnum]
579 if not nfs[netdev] then
580 nfs[netdev] = interface(netdev)
582 _switch[netdev] = true
585 local vid = tonumber(s.vid or s.vlan)
586 if vid ~= nil and vid >= 0 and vid <= 4095 then
587 local iface = "%s.%d" %{ netdev, vid }
588 if not nfs[iface] then
589 nfs[iface] = interface(iface)
591 _switch[iface] = true
598 for iface in utl.kspairs(nfs) do
599 ifaces[#ifaces+1] = nfs[iface]
602 -- find wifi interfaces
605 _uci:foreach("wireless", "wifi-iface",
608 num[s.device] = num[s.device] and num[s.device] + 1 or 1
609 local i = "%s.network%d" %{ s.device, num[s.device] }
610 wfs[i] = interface(i)
614 for iface in utl.kspairs(wfs) do
615 ifaces[#ifaces+1] = wfs[iface]
621 function ignore_interface(self, x)
622 return _iface_ignore(x)
625 function get_wifidev(self, dev)
626 if _uci:get("wireless", dev) == "wifi-device" then
631 function get_wifidevs(self)
635 _uci:foreach("wireless", "wifi-device",
636 function(s) wfd[#wfd+1] = s['.name'] end)
639 for _, dev in utl.vspairs(wfd) do
640 devs[#devs+1] = wifidev(dev)
646 function get_wifinet(self, net)
647 local wnet = _wifi_lookup(net)
653 function add_wifinet(self, net, options)
654 if type(options) == "table" and options.device and
655 _uci:get("wireless", options.device) == "wifi-device"
657 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
662 function del_wifinet(self, net)
663 local wnet = _wifi_lookup(net)
665 _uci:delete("wireless", wnet)
671 function get_status_by_route(self, addr, mask)
673 for _, object in ipairs(utl.ubus()) do
674 local net = object:match("^network%.interface%.(.+)")
676 local s = utl.ubus(object, "status", {})
677 if s and s.route then
679 for _, rt in ipairs(s.route) do
680 if not rt.table and rt.target == addr and rt.mask == mask then
689 function get_status_by_address(self, addr)
691 for _, object in ipairs(utl.ubus()) do
692 local net = object:match("^network%.interface%.(.+)")
694 local s = utl.ubus(object, "status", {})
695 if s and s['ipv4-address'] then
697 for _, a in ipairs(s['ipv4-address']) do
698 if a.address == addr then
703 if s and s['ipv6-address'] then
705 for _, a in ipairs(s['ipv6-address']) do
706 if a.address == addr then
715 function get_wannet(self)
716 local net, stat = self:get_status_by_route("0.0.0.0", 0)
717 return net and network(net, stat.proto)
720 function get_wandev(self)
721 local _, stat = self:get_status_by_route("0.0.0.0", 0)
722 return stat and interface(stat.l3_device or stat.device)
725 function get_wan6net(self)
726 local net, stat = self:get_status_by_route("::", 0)
727 return net and network(net, stat.proto)
730 function get_wan6dev(self)
731 local _, stat = self:get_status_by_route("::", 0)
732 return stat and interface(stat.l3_device or stat.device)
735 function get_switch_topologies(self)
740 function network(name, proto)
742 local p = proto or _uci:get("network", name, "proto")
743 local c = p and _protocols[p] or protocol
748 function protocol.__init__(self, name)
752 function protocol._get(self, opt)
753 local v = _uci:get("network", self.sid, opt)
754 if type(v) == "table" then
755 return table.concat(v, " ")
760 function protocol._ubus(self, field)
761 if not _ubusnetcache[self.sid] then
762 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
765 if _ubusnetcache[self.sid] and field then
766 return _ubusnetcache[self.sid][field]
768 return _ubusnetcache[self.sid]
771 function protocol.get(self, opt)
772 return _get("network", self.sid, opt)
775 function protocol.set(self, opt, val)
776 return _set("network", self.sid, opt, val)
779 function protocol.ifname(self)
781 if self:is_floating() then
782 ifname = self:_ubus("l3_device")
784 ifname = self:_ubus("device")
788 _uci:foreach("wireless", "wifi-iface",
791 num[s.device] = num[s.device]
792 and num[s.device] + 1 or 1
795 for net in utl.imatch(s.network) do
796 if net == self.sid then
797 ifname = "%s.network%d" %{ s.device, num[s.device] }
807 function protocol.proto(self)
811 function protocol.get_i18n(self)
812 local p = self:proto()
814 return lng.translate("Unmanaged")
815 elseif p == "static" then
816 return lng.translate("Static address")
817 elseif p == "dhcp" then
818 return lng.translate("DHCP client")
820 return lng.translate("Unknown")
824 function protocol.type(self)
825 return self:_get("type")
828 function protocol.name(self)
832 function protocol.uptime(self)
833 return self:_ubus("uptime") or 0
836 function protocol.expires(self)
837 local u = self:_ubus("uptime")
838 local d = self:_ubus("data")
840 if type(u) == "number" and type(d) == "table" and
841 type(d.leasetime) == "number"
843 local r = (d.leasetime - (u % d.leasetime))
844 return r > 0 and r or 0
850 function protocol.metric(self)
851 return self:_ubus("metric") or 0
854 function protocol.ipaddr(self)
855 local addrs = self:_ubus("ipv4-address")
856 return addrs and #addrs > 0 and addrs[1].address
859 function protocol.ipaddrs(self)
860 local addrs = self:_ubus("ipv4-address")
863 if type(addrs) == "table" then
865 for n, addr in ipairs(addrs) do
866 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
873 function protocol.netmask(self)
874 local addrs = self:_ubus("ipv4-address")
875 return addrs and #addrs > 0 and
876 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
879 function protocol.gwaddr(self)
881 for _, route in ipairs(self:_ubus("route") or { }) do
882 if route.target == "0.0.0.0" and route.mask == 0 then
888 function protocol.dnsaddrs(self)
891 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
892 if not addr:match(":") then
899 function protocol.ip6addr(self)
900 local addrs = self:_ubus("ipv6-address")
901 if addrs and #addrs > 0 then
902 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
904 addrs = self:_ubus("ipv6-prefix-assignment")
905 if addrs and #addrs > 0 then
906 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
911 function protocol.ip6addrs(self)
912 local addrs = self:_ubus("ipv6-address")
916 if type(addrs) == "table" then
917 for n, addr in ipairs(addrs) do
918 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
922 addrs = self:_ubus("ipv6-prefix-assignment")
924 if type(addrs) == "table" then
925 for n, addr in ipairs(addrs) do
926 rv[#rv+1] = "%s1/%d" %{ addr.address, addr.mask }
933 function protocol.gw6addr(self)
935 for _, route in ipairs(self:_ubus("route") or { }) do
936 if route.target == "::" and route.mask == 0 then
937 return ipc.IPv6(route.nexthop):string()
942 function protocol.dns6addrs(self)
945 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
946 if addr:match(":") then
953 function protocol.is_bridge(self)
954 return (not self:is_virtual() and self:type() == "bridge")
957 function protocol.opkg_package(self)
961 function protocol.is_installed(self)
965 function protocol.is_virtual(self)
969 function protocol.is_floating(self)
973 function protocol.is_empty(self)
974 if self:is_floating() then
979 if (self:_get("ifname") or ""):match("%S+") then
983 _uci:foreach("wireless", "wifi-iface",
986 for n in utl.imatch(s.network) do
987 if n == self.sid then
998 function protocol.add_interface(self, ifname)
999 ifname = _M:ifnameof(ifname)
1000 if ifname and not self:is_floating() then
1001 -- if its a wifi interface, change its network option
1002 local wif = _wifi_lookup(ifname)
1004 _append("wireless", wif, "network", self.sid)
1006 -- add iface to our iface list
1008 _append("network", self.sid, "ifname", ifname)
1013 function protocol.del_interface(self, ifname)
1014 ifname = _M:ifnameof(ifname)
1015 if ifname and not self:is_floating() then
1016 -- if its a wireless interface, clear its network option
1017 local wif = _wifi_lookup(ifname)
1018 if wif then _filter("wireless", wif, "network", self.sid) end
1020 -- remove the interface
1021 _filter("network", self.sid, "ifname", ifname)
1025 function protocol.get_interface(self)
1026 if self:is_virtual() then
1027 _tunnel[self:proto() .. "-" .. self.sid] = true
1028 return interface(self:proto() .. "-" .. self.sid, self)
1029 elseif self:is_bridge() then
1030 _bridge["br-" .. self.sid] = true
1031 return interface("br-" .. self.sid, self)
1035 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
1036 ifn = ifn:match("^[^:/]+")
1037 return ifn and interface(ifn, self)
1040 _uci:foreach("wireless", "wifi-iface",
1043 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1046 for net in utl.imatch(s.network) do
1047 if net == self.sid then
1048 ifn = "%s.network%d" %{ s.device, num[s.device] }
1054 return ifn and interface(ifn, self)
1058 function protocol.get_interfaces(self)
1059 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
1064 for ifn in utl.imatch(self:get("ifname")) do
1065 ifn = ifn:match("^[^:/]+")
1066 nfs[ifn] = interface(ifn, self)
1069 for ifn in utl.kspairs(nfs) do
1070 ifaces[#ifaces+1] = nfs[ifn]
1075 _uci:foreach("wireless", "wifi-iface",
1078 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1081 for net in utl.imatch(s.network) do
1082 if net == self.sid then
1083 ifn = "%s.network%d" %{ s.device, num[s.device] }
1084 wfs[ifn] = interface(ifn, self)
1090 for ifn in utl.kspairs(wfs) do
1091 ifaces[#ifaces+1] = wfs[ifn]
1098 function protocol.contains_interface(self, ifname)
1099 ifname = _M:ifnameof(ifname)
1102 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1104 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1108 for ifn in utl.imatch(self:get("ifname")) do
1109 ifn = ifn:match("[^:]+")
1110 if ifn == ifname then
1115 local wif = _wifi_lookup(ifname)
1118 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1119 if n == self.sid then
1129 function protocol.adminlink(self)
1130 return dsp.build_url("admin", "network", "network", self.sid)
1134 interface = utl.class()
1136 function interface.__init__(self, ifname, network)
1137 local wif = _wifi_lookup(ifname)
1139 self.wif = wifinet(wif)
1140 self.ifname = _wifi_state("section", wif, "ifname")
1143 self.ifname = self.ifname or ifname
1144 self.dev = _interfaces[self.ifname]
1145 self.network = network
1148 function interface._ubus(self, field)
1149 if not _ubusdevcache[self.ifname] then
1150 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1151 { name = self.ifname })
1153 if _ubusdevcache[self.ifname] and field then
1154 return _ubusdevcache[self.ifname][field]
1156 return _ubusdevcache[self.ifname]
1159 function interface.name(self)
1160 return self.wif and self.wif:ifname() or self.ifname
1163 function interface.mac(self)
1164 local mac = self:_ubus("macaddr")
1165 return mac and mac:upper()
1168 function interface.ipaddrs(self)
1169 return self.dev and self.dev.ipaddrs or { }
1172 function interface.ip6addrs(self)
1173 return self.dev and self.dev.ip6addrs or { }
1176 function interface.type(self)
1177 if self.wif or _wifi_iface(self.ifname) then
1179 elseif _bridge[self.ifname] then
1181 elseif _tunnel[self.ifname] then
1183 elseif self.ifname:match("%.") then
1185 elseif _switch[self.ifname] then
1192 function interface.shortname(self)
1194 return self.wif:shortname()
1200 function interface.get_i18n(self)
1202 return "%s: %s %q" %{
1203 lng.translate("Wireless Network"),
1204 self.wif:active_mode(),
1205 self.wif:active_ssid() or self.wif:active_bssid() or self.wif:id()
1208 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1212 function interface.get_type_i18n(self)
1213 local x = self:type()
1215 return lng.translate("Wireless Adapter")
1216 elseif x == "bridge" then
1217 return lng.translate("Bridge")
1218 elseif x == "switch" then
1219 return lng.translate("Ethernet Switch")
1220 elseif x == "vlan" then
1221 if _switch[self.ifname] then
1222 return lng.translate("Switch VLAN")
1224 return lng.translate("Software VLAN")
1226 elseif x == "tunnel" then
1227 return lng.translate("Tunnel Interface")
1229 return lng.translate("Ethernet Adapter")
1233 function interface.adminlink(self)
1235 return self.wif:adminlink()
1239 function interface.ports(self)
1240 local members = self:_ubus("bridge-members")
1244 for _, iface in ipairs(members) do
1245 ifaces[#ifaces+1] = interface(iface)
1250 function interface.bridge_id(self)
1258 function interface.bridge_stp(self)
1266 function interface.is_up(self)
1267 return self:_ubus("up") or false
1270 function interface.is_bridge(self)
1271 return (self:type() == "bridge")
1274 function interface.is_bridgeport(self)
1275 return self.dev and self.dev.bridge and true or false
1278 function interface.tx_bytes(self)
1279 local stat = self:_ubus("statistics")
1280 return stat and stat.tx_bytes or 0
1283 function interface.rx_bytes(self)
1284 local stat = self:_ubus("statistics")
1285 return stat and stat.rx_bytes or 0
1288 function interface.tx_packets(self)
1289 local stat = self:_ubus("statistics")
1290 return stat and stat.tx_packets or 0
1293 function interface.rx_packets(self)
1294 local stat = self:_ubus("statistics")
1295 return stat and stat.rx_packets or 0
1298 function interface.get_network(self)
1299 return self:get_networks()[1]
1302 function interface.get_networks(self)
1303 if not self.networks then
1306 for _, net in ipairs(_M:get_networks()) do
1307 if net:contains_interface(self.ifname) or
1308 net:ifname() == self.ifname
1313 table.sort(nets, function(a, b) return a.sid < b.sid end)
1314 self.networks = nets
1317 return self.networks
1321 function interface.get_wifinet(self)
1326 wifidev = utl.class()
1328 function wifidev.__init__(self, dev)
1330 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1333 function wifidev.get(self, opt)
1334 return _get("wireless", self.sid, opt)
1337 function wifidev.set(self, opt, val)
1338 return _set("wireless", self.sid, opt, val)
1341 function wifidev.name(self)
1345 function wifidev.hwmodes(self)
1346 local l = self.iwinfo.hwmodelist
1347 if l and next(l) then
1350 return { b = true, g = true }
1354 function wifidev.get_i18n(self)
1356 if self.iwinfo.type == "wl" then
1358 elseif self.iwinfo.type == "madwifi" then
1363 local l = self:hwmodes()
1364 if l.a then m = m .. "a" end
1365 if l.b then m = m .. "b" end
1366 if l.g then m = m .. "g" end
1367 if l.n then m = m .. "n" end
1368 if l.ac then m = "ac" end
1370 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1373 function wifidev.is_up(self)
1374 if _ubuswificache[self.sid] then
1375 return (_ubuswificache[self.sid].up == true)
1381 function wifidev.get_wifinet(self, net)
1382 if _uci:get("wireless", net) == "wifi-iface" then
1385 local wnet = _wifi_lookup(net)
1387 return wifinet(wnet)
1392 function wifidev.get_wifinets(self)
1395 _uci:foreach("wireless", "wifi-iface",
1397 if s.device == self.sid then
1398 nets[#nets+1] = wifinet(s['.name'])
1405 function wifidev.add_wifinet(self, options)
1406 options = options or { }
1407 options.device = self.sid
1409 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1411 return wifinet(wnet, options)
1415 function wifidev.del_wifinet(self, net)
1416 if utl.instanceof(net, wifinet) then
1418 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1419 net = _wifi_lookup(net)
1422 if net and _uci:get("wireless", net, "device") == self.sid then
1423 _uci:delete("wireless", net)
1431 wifinet = utl.class()
1433 function wifinet.__init__(self, net, data)
1439 _uci:foreach("wireless", "wifi-iface",
1443 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1444 if s['.name'] == self.sid then
1445 sid = "@wifi-iface[%d]" % n
1446 netid = "%s.network%d" %{ s.device, num[s.device] }
1454 for k, r in pairs(_ubuswificache) do
1455 if type(r) == "table" and
1456 type(r.interfaces) == "table"
1458 for _, i in ipairs(r.interfaces) do
1459 if type(i) == "table" and i.section == sid then
1471 local dev = _wifi_state("section", self.sid, "ifname") or netid
1475 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1478 function wifinet.ubus(self, ...)
1479 local n, v = self._ubusdata
1480 for n = 1, select('#', ...) do
1481 if type(v) == "table" then
1482 v = v[select(n, ...)]
1490 function wifinet.get(self, opt)
1491 return _get("wireless", self.sid, opt)
1494 function wifinet.set(self, opt, val)
1495 return _set("wireless", self.sid, opt, val)
1498 function wifinet.mode(self)
1499 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1502 function wifinet.ssid(self)
1503 return self:ubus("net", "config", "ssid") or self:get("ssid")
1506 function wifinet.bssid(self)
1507 return self:ubus("net", "config", "bssid") or self:get("bssid")
1510 function wifinet.network(self)
1511 local net, networks = nil, { }
1512 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1513 networks[#networks+1] = net
1518 function wifinet.id(self)
1522 function wifinet.name(self)
1526 function wifinet.ifname(self)
1527 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1528 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1534 function wifinet.get_device(self)
1535 local dev = self:ubus("radio") or self:get("device")
1536 return dev and wifidev(dev) or nil
1539 function wifinet.is_up(self)
1540 local ifc = self:get_interface()
1541 return (ifc and ifc:is_up() or false)
1544 function wifinet.active_mode(self)
1545 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1547 if m == "ap" then m = "Master"
1548 elseif m == "sta" then m = "Client"
1549 elseif m == "adhoc" then m = "Ad-Hoc"
1550 elseif m == "mesh" then m = "Mesh"
1551 elseif m == "monitor" then m = "Monitor"
1557 function wifinet.active_mode_i18n(self)
1558 return lng.translate(self:active_mode())
1561 function wifinet.active_ssid(self)
1562 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1565 function wifinet.active_bssid(self)
1566 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1569 function wifinet.active_encryption(self)
1570 local enc = self.iwinfo and self.iwinfo.encryption
1571 return enc and enc.description or "-"
1574 function wifinet.assoclist(self)
1575 return self.iwinfo.assoclist or { }
1578 function wifinet.frequency(self)
1579 local freq = self.iwinfo.frequency
1580 if freq and freq > 0 then
1581 return "%.03f" % (freq / 1000)
1585 function wifinet.bitrate(self)
1586 local rate = self.iwinfo.bitrate
1587 if rate and rate > 0 then
1588 return (rate / 1000)
1592 function wifinet.channel(self)
1593 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1594 tonumber(self:get("channel"))
1597 function wifinet.signal(self)
1598 return self.iwinfo.signal or 0
1601 function wifinet.noise(self)
1602 return self.iwinfo.noise or 0
1605 function wifinet.country(self)
1606 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1609 function wifinet.txpower(self)
1610 local pwr = (self.iwinfo.txpower or 0)
1611 return pwr + self:txpower_offset()
1614 function wifinet.txpower_offset(self)
1615 return self.iwinfo.txpower_offset or 0
1618 function wifinet.signal_level(self, s, n)
1619 if self:active_bssid() ~= "00:00:00:00:00:00" then
1620 local signal = s or self:signal()
1621 local noise = n or self:noise()
1623 if signal < 0 and noise < 0 then
1624 local snr = -1 * (noise - signal)
1625 return math.floor(snr / 5)
1634 function wifinet.signal_percent(self)
1635 local qc = self.iwinfo.quality or 0
1636 local qm = self.iwinfo.quality_max or 0
1638 if qc > 0 and qm > 0 then
1639 return math.floor((100 / qm) * qc)
1645 function wifinet.shortname(self)
1647 lng.translate(self:active_mode()),
1648 self:active_ssid() or self:active_bssid() or self:id()
1652 function wifinet.get_i18n(self)
1653 return "%s: %s %q (%s)" %{
1654 lng.translate("Wireless Network"),
1655 lng.translate(self:active_mode()),
1656 self:active_ssid() or self:active_bssid() or self:id(),
1661 function wifinet.adminlink(self)
1662 return dsp.build_url("admin", "network", "wireless", self.netid)
1665 function wifinet.get_network(self)
1666 return self:get_networks()[1]
1669 function wifinet.get_networks(self)
1672 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1673 if _uci:get("network", net) == "interface" then
1674 nets[#nets+1] = network(net)
1677 table.sort(nets, function(a, b) return a.sid < b.sid end)
1681 function wifinet.get_interface(self)
1682 return interface(self:ifname())
1686 -- setup base protocols
1687 _M:register_protocol("static")
1688 _M:register_protocol("dhcp")
1689 _M:register_protocol("none")
1691 -- load protocol extensions
1692 local exts = nfs.dir(utl.libpath() .. "/model/network")
1696 if ext:match("%.lua$") then
1697 require("luci.model.network." .. ext:gsub("%.lua$", ""))