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.ip6prefix(self)
954 local prefix = self:_ubus("ipv6-prefix")
955 if prefix and #prefix > 0 then
956 return "%s/%d" %{ prefix[1].address, prefix[1].mask }
960 function protocol.is_bridge(self)
961 return (not self:is_virtual() and self:type() == "bridge")
964 function protocol.opkg_package(self)
968 function protocol.is_installed(self)
972 function protocol.is_virtual(self)
976 function protocol.is_floating(self)
980 function protocol.is_empty(self)
981 if self:is_floating() then
986 if (self:_get("ifname") or ""):match("%S+") then
990 _uci:foreach("wireless", "wifi-iface",
993 for n in utl.imatch(s.network) do
994 if n == self.sid then
1005 function protocol.add_interface(self, ifname)
1006 ifname = _M:ifnameof(ifname)
1007 if ifname and not self:is_floating() then
1008 -- if its a wifi interface, change its network option
1009 local wif = _wifi_lookup(ifname)
1011 _append("wireless", wif, "network", self.sid)
1013 -- add iface to our iface list
1015 _append("network", self.sid, "ifname", ifname)
1020 function protocol.del_interface(self, ifname)
1021 ifname = _M:ifnameof(ifname)
1022 if ifname and not self:is_floating() then
1023 -- if its a wireless interface, clear its network option
1024 local wif = _wifi_lookup(ifname)
1025 if wif then _filter("wireless", wif, "network", self.sid) end
1027 -- remove the interface
1028 _filter("network", self.sid, "ifname", ifname)
1032 function protocol.get_interface(self)
1033 if self:is_virtual() then
1034 _tunnel[self:proto() .. "-" .. self.sid] = true
1035 return interface(self:proto() .. "-" .. self.sid, self)
1036 elseif self:is_bridge() then
1037 _bridge["br-" .. self.sid] = true
1038 return interface("br-" .. self.sid, self)
1042 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
1043 ifn = ifn:match("^[^:/]+")
1044 return ifn and interface(ifn, self)
1047 _uci:foreach("wireless", "wifi-iface",
1050 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1053 for net in utl.imatch(s.network) do
1054 if net == self.sid then
1055 ifn = "%s.network%d" %{ s.device, num[s.device] }
1061 return ifn and interface(ifn, self)
1065 function protocol.get_interfaces(self)
1066 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
1071 for ifn in utl.imatch(self:get("ifname")) do
1072 ifn = ifn:match("^[^:/]+")
1073 nfs[ifn] = interface(ifn, self)
1076 for ifn in utl.kspairs(nfs) do
1077 ifaces[#ifaces+1] = nfs[ifn]
1082 _uci:foreach("wireless", "wifi-iface",
1085 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1088 for net in utl.imatch(s.network) do
1089 if net == self.sid then
1090 ifn = "%s.network%d" %{ s.device, num[s.device] }
1091 wfs[ifn] = interface(ifn, self)
1097 for ifn in utl.kspairs(wfs) do
1098 ifaces[#ifaces+1] = wfs[ifn]
1105 function protocol.contains_interface(self, ifname)
1106 ifname = _M:ifnameof(ifname)
1109 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1111 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1115 for ifn in utl.imatch(self:get("ifname")) do
1116 ifn = ifn:match("[^:]+")
1117 if ifn == ifname then
1122 local wif = _wifi_lookup(ifname)
1125 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1126 if n == self.sid then
1136 function protocol.adminlink(self)
1137 return dsp.build_url("admin", "network", "network", self.sid)
1141 interface = utl.class()
1143 function interface.__init__(self, ifname, network)
1144 local wif = _wifi_lookup(ifname)
1146 self.wif = wifinet(wif)
1147 self.ifname = _wifi_state("section", wif, "ifname")
1150 self.ifname = self.ifname or ifname
1151 self.dev = _interfaces[self.ifname]
1152 self.network = network
1155 function interface._ubus(self, field)
1156 if not _ubusdevcache[self.ifname] then
1157 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1158 { name = self.ifname })
1160 if _ubusdevcache[self.ifname] and field then
1161 return _ubusdevcache[self.ifname][field]
1163 return _ubusdevcache[self.ifname]
1166 function interface.name(self)
1167 return self.wif and self.wif:ifname() or self.ifname
1170 function interface.mac(self)
1171 local mac = self:_ubus("macaddr")
1172 return mac and mac:upper()
1175 function interface.ipaddrs(self)
1176 return self.dev and self.dev.ipaddrs or { }
1179 function interface.ip6addrs(self)
1180 return self.dev and self.dev.ip6addrs or { }
1183 function interface.type(self)
1184 if self.wif or _wifi_iface(self.ifname) then
1186 elseif _bridge[self.ifname] then
1188 elseif _tunnel[self.ifname] then
1190 elseif self.ifname:match("%.") then
1192 elseif _switch[self.ifname] then
1199 function interface.shortname(self)
1201 return self.wif:shortname()
1207 function interface.get_i18n(self)
1209 return "%s: %s %q" %{
1210 lng.translate("Wireless Network"),
1211 self.wif:active_mode(),
1212 self.wif:active_ssid() or self.wif:active_bssid() or self.wif:id()
1215 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1219 function interface.get_type_i18n(self)
1220 local x = self:type()
1222 return lng.translate("Wireless Adapter")
1223 elseif x == "bridge" then
1224 return lng.translate("Bridge")
1225 elseif x == "switch" then
1226 return lng.translate("Ethernet Switch")
1227 elseif x == "vlan" then
1228 if _switch[self.ifname] then
1229 return lng.translate("Switch VLAN")
1231 return lng.translate("Software VLAN")
1233 elseif x == "tunnel" then
1234 return lng.translate("Tunnel Interface")
1236 return lng.translate("Ethernet Adapter")
1240 function interface.adminlink(self)
1242 return self.wif:adminlink()
1246 function interface.ports(self)
1247 local members = self:_ubus("bridge-members")
1251 for _, iface in ipairs(members) do
1252 ifaces[#ifaces+1] = interface(iface)
1257 function interface.bridge_id(self)
1265 function interface.bridge_stp(self)
1273 function interface.is_up(self)
1274 return self:_ubus("up") or false
1277 function interface.is_bridge(self)
1278 return (self:type() == "bridge")
1281 function interface.is_bridgeport(self)
1282 return self.dev and self.dev.bridge and true or false
1285 function interface.tx_bytes(self)
1286 local stat = self:_ubus("statistics")
1287 return stat and stat.tx_bytes or 0
1290 function interface.rx_bytes(self)
1291 local stat = self:_ubus("statistics")
1292 return stat and stat.rx_bytes or 0
1295 function interface.tx_packets(self)
1296 local stat = self:_ubus("statistics")
1297 return stat and stat.tx_packets or 0
1300 function interface.rx_packets(self)
1301 local stat = self:_ubus("statistics")
1302 return stat and stat.rx_packets or 0
1305 function interface.get_network(self)
1306 return self:get_networks()[1]
1309 function interface.get_networks(self)
1310 if not self.networks then
1313 for _, net in ipairs(_M:get_networks()) do
1314 if net:contains_interface(self.ifname) or
1315 net:ifname() == self.ifname
1320 table.sort(nets, function(a, b) return a.sid < b.sid end)
1321 self.networks = nets
1324 return self.networks
1328 function interface.get_wifinet(self)
1333 wifidev = utl.class()
1335 function wifidev.__init__(self, dev)
1337 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1340 function wifidev.get(self, opt)
1341 return _get("wireless", self.sid, opt)
1344 function wifidev.set(self, opt, val)
1345 return _set("wireless", self.sid, opt, val)
1348 function wifidev.name(self)
1352 function wifidev.hwmodes(self)
1353 local l = self.iwinfo.hwmodelist
1354 if l and next(l) then
1357 return { b = true, g = true }
1361 function wifidev.get_i18n(self)
1363 if self.iwinfo.type == "wl" then
1368 local l = self:hwmodes()
1369 if l.a then m = m .. "a" end
1370 if l.b then m = m .. "b" end
1371 if l.g then m = m .. "g" end
1372 if l.n then m = m .. "n" end
1373 if l.ac then m = "ac" end
1375 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1378 function wifidev.is_up(self)
1379 if _ubuswificache[self.sid] then
1380 return (_ubuswificache[self.sid].up == true)
1386 function wifidev.get_wifinet(self, net)
1387 if _uci:get("wireless", net) == "wifi-iface" then
1390 local wnet = _wifi_lookup(net)
1392 return wifinet(wnet)
1397 function wifidev.get_wifinets(self)
1400 _uci:foreach("wireless", "wifi-iface",
1402 if s.device == self.sid then
1403 nets[#nets+1] = wifinet(s['.name'])
1410 function wifidev.add_wifinet(self, options)
1411 options = options or { }
1412 options.device = self.sid
1414 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1416 return wifinet(wnet, options)
1420 function wifidev.del_wifinet(self, net)
1421 if utl.instanceof(net, wifinet) then
1423 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1424 net = _wifi_lookup(net)
1427 if net and _uci:get("wireless", net, "device") == self.sid then
1428 _uci:delete("wireless", net)
1436 wifinet = utl.class()
1438 function wifinet.__init__(self, net, data)
1444 _uci:foreach("wireless", "wifi-iface",
1448 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1449 if s['.name'] == self.sid then
1450 sid = "@wifi-iface[%d]" % n
1451 netid = "%s.network%d" %{ s.device, num[s.device] }
1459 for k, r in pairs(_ubuswificache) do
1460 if type(r) == "table" and
1461 type(r.interfaces) == "table"
1463 for _, i in ipairs(r.interfaces) do
1464 if type(i) == "table" and i.section == sid then
1476 local dev = _wifi_state("section", self.sid, "ifname") or netid
1480 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1483 function wifinet.ubus(self, ...)
1484 local n, v = self._ubusdata
1485 for n = 1, select('#', ...) do
1486 if type(v) == "table" then
1487 v = v[select(n, ...)]
1495 function wifinet.get(self, opt)
1496 return _get("wireless", self.sid, opt)
1499 function wifinet.set(self, opt, val)
1500 return _set("wireless", self.sid, opt, val)
1503 function wifinet.mode(self)
1504 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1507 function wifinet.ssid(self)
1508 return self:ubus("net", "config", "ssid") or self:get("ssid")
1511 function wifinet.bssid(self)
1512 return self:ubus("net", "config", "bssid") or self:get("bssid")
1515 function wifinet.network(self)
1516 local net, networks = nil, { }
1517 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1518 networks[#networks+1] = net
1523 function wifinet.id(self)
1527 function wifinet.name(self)
1531 function wifinet.ifname(self)
1532 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1533 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1539 function wifinet.get_device(self)
1540 local dev = self:ubus("radio") or self:get("device")
1541 return dev and wifidev(dev) or nil
1544 function wifinet.is_up(self)
1545 local ifc = self:get_interface()
1546 return (ifc and ifc:is_up() or false)
1549 function wifinet.active_mode(self)
1550 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1552 if m == "ap" then m = "Master"
1553 elseif m == "sta" then m = "Client"
1554 elseif m == "adhoc" then m = "Ad-Hoc"
1555 elseif m == "mesh" then m = "Mesh"
1556 elseif m == "monitor" then m = "Monitor"
1562 function wifinet.active_mode_i18n(self)
1563 return lng.translate(self:active_mode())
1566 function wifinet.active_ssid(self)
1567 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1570 function wifinet.active_bssid(self)
1571 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1574 function wifinet.active_encryption(self)
1575 local enc = self.iwinfo and self.iwinfo.encryption
1576 return enc and enc.description or "-"
1579 function wifinet.assoclist(self)
1580 return self.iwinfo.assoclist or { }
1583 function wifinet.frequency(self)
1584 local freq = self.iwinfo.frequency
1585 if freq and freq > 0 then
1586 return "%.03f" % (freq / 1000)
1590 function wifinet.bitrate(self)
1591 local rate = self.iwinfo.bitrate
1592 if rate and rate > 0 then
1593 return (rate / 1000)
1597 function wifinet.channel(self)
1598 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1599 tonumber(self:get("channel"))
1602 function wifinet.signal(self)
1603 return self.iwinfo.signal or 0
1606 function wifinet.noise(self)
1607 return self.iwinfo.noise or 0
1610 function wifinet.country(self)
1611 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1614 function wifinet.txpower(self)
1615 local pwr = (self.iwinfo.txpower or 0)
1616 return pwr + self:txpower_offset()
1619 function wifinet.txpower_offset(self)
1620 return self.iwinfo.txpower_offset or 0
1623 function wifinet.signal_level(self, s, n)
1624 if self:active_bssid() ~= "00:00:00:00:00:00" then
1625 local signal = s or self:signal()
1626 local noise = n or self:noise()
1628 if signal < 0 and noise < 0 then
1629 local snr = -1 * (noise - signal)
1630 return math.floor(snr / 5)
1639 function wifinet.signal_percent(self)
1640 local qc = self.iwinfo.quality or 0
1641 local qm = self.iwinfo.quality_max or 0
1643 if qc > 0 and qm > 0 then
1644 return math.floor((100 / qm) * qc)
1650 function wifinet.shortname(self)
1652 lng.translate(self:active_mode()),
1653 self:active_ssid() or self:active_bssid() or self:id()
1657 function wifinet.get_i18n(self)
1658 return "%s: %s %q (%s)" %{
1659 lng.translate("Wireless Network"),
1660 lng.translate(self:active_mode()),
1661 self:active_ssid() or self:active_bssid() or self:id(),
1666 function wifinet.adminlink(self)
1667 return dsp.build_url("admin", "network", "wireless", self.netid)
1670 function wifinet.get_network(self)
1671 return self:get_networks()[1]
1674 function wifinet.get_networks(self)
1677 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1678 if _uci:get("network", net) == "interface" then
1679 nets[#nets+1] = network(net)
1682 table.sort(nets, function(a, b) return a.sid < b.sid end)
1686 function wifinet.get_interface(self)
1687 return interface(self:ifname())
1691 -- setup base protocols
1692 _M:register_protocol("static")
1693 _M:register_protocol("dhcp")
1694 _M:register_protocol("none")
1696 -- load protocol extensions
1697 local exts = nfs.dir(utl.libpath() .. "/model/network")
1701 if ext:match("%.lua$") then
1702 require("luci.model.network." .. ext:gsub("%.lua$", ""))