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"
20 module "luci.model.network"
23 IFACE_PATTERNS_VIRTUAL = { }
24 IFACE_PATTERNS_IGNORE = { "^wmaster%d", "^wifi%d", "^hwsim%d", "^imq%d", "^ifb%d", "^mon%.wlan%d", "^sit%d", "^gre%d", "^lo$" }
25 IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
28 protocol = utl.class()
30 local _protocols = { }
32 local _interfaces, _bridge, _switch, _tunnel
33 local _ubusnetcache, _ubusdevcache, _ubuswificache
36 function _filter(c, s, o, r)
37 local val = _uci:get(c, s, o)
40 if type(val) == "string" then
41 for val in val:gmatch("%S+") do
47 _uci:set(c, s, o, table.concat(l, " "))
51 elseif type(val) == "table" then
52 for _, val in ipairs(val) do
66 function _append(c, s, o, a)
67 local val = _uci:get(c, s, o) or ""
68 if type(val) == "string" then
70 for val in val:gmatch("%S+") do
76 _uci:set(c, s, o, table.concat(l, " "))
77 elseif type(val) == "table" then
79 for _, val in ipairs(val) do
89 function _stror(s1, s2)
90 if not s1 or #s1 == 0 then
91 return s2 and #s2 > 0 and s2
97 function _get(c, s, o)
98 return _uci:get(c, s, o)
101 function _set(c, s, o, v)
103 if type(v) == "boolean" then v = v and "1" or "0" end
104 return _uci:set(c, s, o, v)
106 return _uci:delete(c, s, o)
110 function _wifi_iface(x)
112 for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
120 function _wifi_state(key, val, field)
121 local radio, radiostate, ifc, ifcstate
123 if not next(_ubuswificache) then
124 _ubuswificache = utl.ubus("network.wireless", "status", {}) or {}
126 -- workaround extended section format
127 for radio, radiostate in pairs(_ubuswificache) do
128 for ifc, ifcstate in pairs(radiostate.interfaces) do
129 if ifcstate.section and ifcstate.section:sub(1, 1) == '@' then
130 local s = _uci:get_all('wireless.%s' % ifcstate.section)
132 ifcstate.section = s['.name']
139 for radio, radiostate in pairs(_ubuswificache) do
140 for ifc, ifcstate in pairs(radiostate.interfaces) do
141 if ifcstate[key] == val then
142 return ifcstate[field]
148 function _wifi_lookup(ifn)
149 -- got a radio#.network# pseudo iface, locate the corresponding section
150 local radio, ifnidx = ifn:match("^(%w+)%.network(%d+)$")
151 if radio and ifnidx then
155 ifnidx = tonumber(ifnidx)
156 _uci:foreach("wireless", "wifi-iface",
158 if s.device == radio then
160 if num == ifnidx then
169 -- looks like wifi, try to locate the section via ubus state
170 elseif _wifi_iface(ifn) then
171 return _wifi_state("ifname", ifn, "section")
175 function _iface_virtual(x)
177 for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
185 function _iface_ignore(x)
187 for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
192 return _iface_virtual(x)
196 function init(cursor)
197 _uci = cursor or _uci or uci.cursor()
208 -- read interface information
210 for n, i in ipairs(nxo.getifaddrs()) do
211 local name = i.name:match("[^:]+")
212 local prnt = name:match("^([^%.]+)%.")
214 if _iface_virtual(name) then
218 if _tunnel[name] or not _iface_ignore(name) then
219 _interfaces[name] = _interfaces[name] or {
220 idx = i.ifindex or n,
233 if i.family == "packet" then
234 _interfaces[name].flags = i.flags
235 _interfaces[name].stats = i.data
236 _interfaces[name].macaddr = i.addr
237 elseif i.family == "inet" then
238 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
239 elseif i.family == "inet6" then
240 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
245 -- read bridge informaton
247 for l in utl.execi("brctl show") do
248 if not l:match("STP") then
249 local r = utl.split(l, "%s+", nil, true)
255 ifnames = { _interfaces[r[4]] }
258 b.ifnames[1].bridge = b
262 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
263 b.ifnames[#b.ifnames].bridge = b
271 function save(self, ...)
276 function commit(self, ...)
281 function ifnameof(self, x)
282 if utl.instanceof(x, interface) then
284 elseif utl.instanceof(x, protocol) then
286 elseif type(x) == "string" then
287 return x:match("^[^:]+")
291 function get_protocol(self, protoname, netname)
292 local v = _protocols[protoname]
294 return v(netname or "__dummy__")
298 function get_protocols(self)
301 for _, v in ipairs(_protocols) do
302 p[#p+1] = v("__dummy__")
307 function register_protocol(self, protoname)
308 local proto = utl.class(protocol)
310 function proto.__init__(self, name)
314 function proto.proto(self)
318 _protocols[#_protocols+1] = proto
319 _protocols[protoname] = proto
324 function register_pattern_virtual(self, pat)
325 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
329 function has_ipv6(self)
330 return nfs.access("/proc/net/ipv6_route")
333 function add_network(self, n, options)
334 local oldnet = self:get_network(n)
335 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
336 if _uci:section("network", "interface", n, options) then
339 elseif oldnet and oldnet:is_empty() then
342 for k, v in pairs(options) do
350 function get_network(self, n)
351 if n and _uci:get("network", n) == "interface" then
356 function get_networks(self)
360 _uci:foreach("network", "interface",
362 nls[s['.name']] = network(s['.name'])
366 for n in utl.kspairs(nls) do
367 nets[#nets+1] = nls[n]
373 function del_network(self, n)
374 local r = _uci:delete("network", n)
376 _uci:delete_all("network", "alias",
377 function(s) return (s.interface == n) end)
379 _uci:delete_all("network", "route",
380 function(s) return (s.interface == n) end)
382 _uci:delete_all("network", "route6",
383 function(s) return (s.interface == n) end)
385 _uci:foreach("wireless", "wifi-iface",
389 for net in utl.imatch(s.network) do
395 _uci:set("wireless", s['.name'], "network",
396 table.concat(rest, " "))
398 _uci:delete("wireless", s['.name'], "network")
405 function rename_network(self, old, new)
407 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
408 r = _uci:section("network", "interface", new, _uci:get_all("network", old))
411 _uci:foreach("network", "alias",
413 if s.interface == old then
414 _uci:set("network", s['.name'], "interface", new)
418 _uci:foreach("network", "route",
420 if s.interface == old then
421 _uci:set("network", s['.name'], "interface", new)
425 _uci:foreach("network", "route6",
427 if s.interface == old then
428 _uci:set("network", s['.name'], "interface", new)
432 _uci:foreach("wireless", "wifi-iface",
436 for net in utl.imatch(s.network) do
444 _uci:set("wireless", s['.name'], "network",
445 table.concat(list, " "))
449 _uci:delete("network", old)
455 function get_interface(self, i)
456 if _interfaces[i] or _wifi_iface(i) then
461 _uci:foreach("wireless", "wifi-iface",
464 num[s.device] = num[s.device] and num[s.device] + 1 or 1
465 if s['.name'] == i then
467 "%s.network%d" %{s.device, num[s.device] })
476 function get_interfaces(self)
483 -- find normal interfaces
484 _uci:foreach("network", "interface",
486 for iface in utl.imatch(s.ifname) do
487 if not _iface_ignore(iface) and not _wifi_iface(iface) then
489 nfs[iface] = interface(iface)
494 for iface in utl.kspairs(_interfaces) do
495 if not (seen[iface] or _iface_ignore(iface) or _wifi_iface(iface)) then
496 nfs[iface] = interface(iface)
500 -- find vlan interfaces
501 _uci:foreach("network", "switch_vlan",
507 local base = baseof[s.device]
509 if not s.device:match("^eth%d") then
511 for l in utl.execi("swconfig dev %q help 2>/dev/null" % s.device) do
513 base = l:match("^%w+: (%w+)")
516 if not base or not base:match("^eth%d") then
522 baseof[s.device] = base
525 local vid = tonumber(s.vid or s.vlan)
526 if vid ~= nil and vid >= 0 and vid <= 4095 then
527 local iface = "%s.%d" %{ base, vid }
528 if not seen[iface] then
530 nfs[iface] = interface(iface)
535 for iface in utl.kspairs(nfs) do
536 ifaces[#ifaces+1] = nfs[iface]
539 -- find wifi interfaces
542 _uci:foreach("wireless", "wifi-iface",
545 num[s.device] = num[s.device] and num[s.device] + 1 or 1
546 local i = "%s.network%d" %{ s.device, num[s.device] }
547 wfs[i] = interface(i)
551 for iface in utl.kspairs(wfs) do
552 ifaces[#ifaces+1] = wfs[iface]
558 function ignore_interface(self, x)
559 return _iface_ignore(x)
562 function get_wifidev(self, dev)
563 if _uci:get("wireless", dev) == "wifi-device" then
568 function get_wifidevs(self)
572 _uci:foreach("wireless", "wifi-device",
573 function(s) wfd[#wfd+1] = s['.name'] end)
576 for _, dev in utl.vspairs(wfd) do
577 devs[#devs+1] = wifidev(dev)
583 function get_wifinet(self, net)
584 local wnet = _wifi_lookup(net)
590 function add_wifinet(self, net, options)
591 if type(options) == "table" and options.device and
592 _uci:get("wireless", options.device) == "wifi-device"
594 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
599 function del_wifinet(self, net)
600 local wnet = _wifi_lookup(net)
602 _uci:delete("wireless", wnet)
608 function get_status_by_route(self, addr, mask)
610 for _, object in ipairs(utl.ubus()) do
611 local net = object:match("^network%.interface%.(.+)")
613 local s = utl.ubus(object, "status", {})
614 if s and s.route then
616 for _, rt in ipairs(s.route) do
617 if not rt.table and rt.target == addr and rt.mask == mask then
626 function get_status_by_address(self, addr)
628 for _, object in ipairs(utl.ubus()) do
629 local net = object:match("^network%.interface%.(.+)")
631 local s = utl.ubus(object, "status", {})
632 if s and s['ipv4-address'] then
634 for _, a in ipairs(s['ipv4-address']) do
635 if a.address == addr then
640 if s and s['ipv6-address'] then
642 for _, a in ipairs(s['ipv6-address']) do
643 if a.address == addr then
652 function get_wannet(self)
653 local net = self:get_status_by_route("0.0.0.0", 0)
654 return net and network(net)
657 function get_wandev(self)
658 local _, stat = self:get_status_by_route("0.0.0.0", 0)
659 return stat and interface(stat.l3_device or stat.device)
662 function get_wan6net(self)
663 local net = self:get_status_by_route("::", 0)
664 return net and network(net)
667 function get_wan6dev(self)
668 local _, stat = self:get_status_by_route("::", 0)
669 return stat and interface(stat.l3_device or stat.device)
673 function network(name, proto)
675 local p = proto or _uci:get("network", name, "proto")
676 local c = p and _protocols[p] or protocol
681 function protocol.__init__(self, name)
685 function protocol._get(self, opt)
686 local v = _uci:get("network", self.sid, opt)
687 if type(v) == "table" then
688 return table.concat(v, " ")
693 function protocol._ubus(self, field)
694 if not _ubusnetcache[self.sid] then
695 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
698 if _ubusnetcache[self.sid] and field then
699 return _ubusnetcache[self.sid][field]
701 return _ubusnetcache[self.sid]
704 function protocol.get(self, opt)
705 return _get("network", self.sid, opt)
708 function protocol.set(self, opt, val)
709 return _set("network", self.sid, opt, val)
712 function protocol.ifname(self)
714 if self:is_floating() then
715 ifname = self:_ubus("l3_device")
717 ifname = self:_ubus("device")
721 _uci:foreach("wireless", "wifi-iface",
724 num[s.device] = num[s.device]
725 and num[s.device] + 1 or 1
728 for net in utl.imatch(s.network) do
729 if net == self.sid then
730 ifname = "%s.network%d" %{ s.device, num[s.device] }
740 function protocol.proto(self)
744 function protocol.get_i18n(self)
745 local p = self:proto()
747 return lng.translate("Unmanaged")
748 elseif p == "static" then
749 return lng.translate("Static address")
750 elseif p == "dhcp" then
751 return lng.translate("DHCP client")
753 return lng.translate("Unknown")
757 function protocol.type(self)
758 return self:_get("type")
761 function protocol.name(self)
765 function protocol.uptime(self)
766 return self:_ubus("uptime") or 0
769 function protocol.expires(self)
770 local u = self:_ubus("uptime")
771 local d = self:_ubus("data")
773 if type(u) == "number" and type(d) == "table" and
774 type(d.leasetime) == "number"
776 local r = (d.leasetime - (u % d.leasetime))
777 return r > 0 and r or 0
783 function protocol.metric(self)
784 return self:_ubus("metric") or 0
787 function protocol.ipaddr(self)
788 local addrs = self:_ubus("ipv4-address")
789 return addrs and #addrs > 0 and addrs[1].address
792 function protocol.ipaddrs(self)
793 local addrs = self:_ubus("ipv4-address")
796 if type(addrs) == "table" then
798 for n, addr in ipairs(addrs) do
799 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
806 function protocol.netmask(self)
807 local addrs = self:_ubus("ipv4-address")
808 return addrs and #addrs > 0 and
809 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
812 function protocol.gwaddr(self)
814 for _, route in ipairs(self:_ubus("route") or { }) do
815 if route.target == "0.0.0.0" and route.mask == 0 then
821 function protocol.dnsaddrs(self)
824 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
825 if not addr:match(":") then
832 function protocol.ip6addr(self)
833 local addrs = self:_ubus("ipv6-address")
834 if addrs and #addrs > 0 then
835 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
837 addrs = self:_ubus("ipv6-prefix-assignment")
838 if addrs and #addrs > 0 then
839 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
844 function protocol.ip6addrs(self)
845 local addrs = self:_ubus("ipv6-address")
849 if type(addrs) == "table" then
850 for n, addr in ipairs(addrs) do
851 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
855 addrs = self:_ubus("ipv6-prefix-assignment")
857 if type(addrs) == "table" then
858 for n, addr in ipairs(addrs) do
859 rv[#rv+1] = "%s1/%d" %{ addr.address, addr.mask }
866 function protocol.gw6addr(self)
868 for _, route in ipairs(self:_ubus("route") or { }) do
869 if route.target == "::" and route.mask == 0 then
870 return ipc.IPv6(route.nexthop):string()
875 function protocol.dns6addrs(self)
878 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
879 if addr:match(":") then
886 function protocol.is_bridge(self)
887 return (not self:is_virtual() and self:type() == "bridge")
890 function protocol.opkg_package(self)
894 function protocol.is_installed(self)
898 function protocol.is_virtual(self)
902 function protocol.is_floating(self)
906 function protocol.is_empty(self)
907 if self:is_floating() then
912 if (self:_get("ifname") or ""):match("%S+") then
916 _uci:foreach("wireless", "wifi-iface",
919 for n in utl.imatch(s.network) do
920 if n == self.sid then
931 function protocol.add_interface(self, ifname)
932 ifname = _M:ifnameof(ifname)
933 if ifname and not self:is_floating() then
934 -- if its a wifi interface, change its network option
935 local wif = _wifi_lookup(ifname)
937 _append("wireless", wif, "network", self.sid)
939 -- add iface to our iface list
941 _append("network", self.sid, "ifname", ifname)
946 function protocol.del_interface(self, ifname)
947 ifname = _M:ifnameof(ifname)
948 if ifname and not self:is_floating() then
949 -- if its a wireless interface, clear its network option
950 local wif = _wifi_lookup(ifname)
951 if wif then _filter("wireless", wif, "network", self.sid) end
953 -- remove the interface
954 _filter("network", self.sid, "ifname", ifname)
958 function protocol.get_interface(self)
959 if self:is_virtual() then
960 _tunnel[self:proto() .. "-" .. self.sid] = true
961 return interface(self:proto() .. "-" .. self.sid, self)
962 elseif self:is_bridge() then
963 _bridge["br-" .. self.sid] = true
964 return interface("br-" .. self.sid, self)
968 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
969 ifn = ifn:match("^[^:/]+")
970 return ifn and interface(ifn, self)
973 _uci:foreach("wireless", "wifi-iface",
976 num[s.device] = num[s.device] and num[s.device] + 1 or 1
979 for net in utl.imatch(s.network) do
980 if net == self.sid then
981 ifn = "%s.network%d" %{ s.device, num[s.device] }
987 return ifn and interface(ifn, self)
991 function protocol.get_interfaces(self)
992 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
997 for ifn in utl.imatch(self:get("ifname")) do
998 ifn = ifn:match("^[^:/]+")
999 nfs[ifn] = interface(ifn, self)
1002 for ifn in utl.kspairs(nfs) do
1003 ifaces[#ifaces+1] = nfs[ifn]
1008 _uci:foreach("wireless", "wifi-iface",
1011 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1014 for net in utl.imatch(s.network) do
1015 if net == self.sid then
1016 ifn = "%s.network%d" %{ s.device, num[s.device] }
1017 wfs[ifn] = interface(ifn, self)
1023 for ifn in utl.kspairs(wfs) do
1024 ifaces[#ifaces+1] = wfs[ifn]
1031 function protocol.contains_interface(self, ifname)
1032 ifname = _M:ifnameof(ifname)
1035 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1037 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1041 for ifn in utl.imatch(self:get("ifname")) do
1042 ifn = ifn:match("[^:]+")
1043 if ifn == ifname then
1048 local wif = _wifi_lookup(ifname)
1051 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1052 if n == self.sid then
1062 function protocol.adminlink(self)
1063 return dsp.build_url("admin", "network", "network", self.sid)
1067 interface = utl.class()
1069 function interface.__init__(self, ifname, network)
1070 local wif = _wifi_lookup(ifname)
1072 self.wif = wifinet(wif)
1073 self.ifname = _wifi_state("section", wif, "ifname")
1076 self.ifname = self.ifname or ifname
1077 self.dev = _interfaces[self.ifname]
1078 self.network = network
1081 function interface._ubus(self, field)
1082 if not _ubusdevcache[self.ifname] then
1083 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1084 { name = self.ifname })
1086 if _ubusdevcache[self.ifname] and field then
1087 return _ubusdevcache[self.ifname][field]
1089 return _ubusdevcache[self.ifname]
1092 function interface.name(self)
1093 return self.wif and self.wif:ifname() or self.ifname
1096 function interface.mac(self)
1097 local mac = self:_ubus("macaddr")
1098 return mac and mac:upper()
1101 function interface.ipaddrs(self)
1102 return self.dev and self.dev.ipaddrs or { }
1105 function interface.ip6addrs(self)
1106 return self.dev and self.dev.ip6addrs or { }
1109 function interface.type(self)
1110 if self.wif or _wifi_iface(self.ifname) then
1112 elseif _bridge[self.ifname] then
1114 elseif _tunnel[self.ifname] then
1116 elseif self.ifname:match("%.") then
1118 elseif _switch[self.ifname] then
1125 function interface.shortname(self)
1128 self.wif:active_mode(),
1129 self.wif:active_ssid() or self.wif:active_bssid()
1136 function interface.get_i18n(self)
1138 return "%s: %s %q" %{
1139 lng.translate("Wireless Network"),
1140 self.wif:active_mode(),
1141 self.wif:active_ssid() or self.wif:active_bssid()
1144 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1148 function interface.get_type_i18n(self)
1149 local x = self:type()
1151 return lng.translate("Wireless Adapter")
1152 elseif x == "bridge" then
1153 return lng.translate("Bridge")
1154 elseif x == "switch" then
1155 return lng.translate("Ethernet Switch")
1156 elseif x == "vlan" then
1157 return lng.translate("VLAN Interface")
1158 elseif x == "tunnel" then
1159 return lng.translate("Tunnel Interface")
1161 return lng.translate("Ethernet Adapter")
1165 function interface.adminlink(self)
1167 return self.wif:adminlink()
1171 function interface.ports(self)
1172 local members = self:_ubus("bridge-members")
1176 for _, iface in ipairs(members) do
1177 ifaces[#ifaces+1] = interface(iface)
1182 function interface.bridge_id(self)
1190 function interface.bridge_stp(self)
1198 function interface.is_up(self)
1199 return self:_ubus("up") or false
1202 function interface.is_bridge(self)
1203 return (self:type() == "bridge")
1206 function interface.is_bridgeport(self)
1207 return self.dev and self.dev.bridge and true or false
1210 function interface.tx_bytes(self)
1211 local stat = self:_ubus("statistics")
1212 return stat and stat.tx_bytes or 0
1215 function interface.rx_bytes(self)
1216 local stat = self:_ubus("statistics")
1217 return stat and stat.rx_bytes or 0
1220 function interface.tx_packets(self)
1221 local stat = self:_ubus("statistics")
1222 return stat and stat.tx_packets or 0
1225 function interface.rx_packets(self)
1226 local stat = self:_ubus("statistics")
1227 return stat and stat.rx_packets or 0
1230 function interface.get_network(self)
1231 return self:get_networks()[1]
1234 function interface.get_networks(self)
1235 if not self.networks then
1238 for _, net in ipairs(_M:get_networks()) do
1239 if net:contains_interface(self.ifname) or
1240 net:ifname() == self.ifname
1245 table.sort(nets, function(a, b) return a.sid < b.sid end)
1246 self.networks = nets
1249 return self.networks
1253 function interface.get_wifinet(self)
1258 wifidev = utl.class()
1260 function wifidev.__init__(self, dev)
1262 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1265 function wifidev.get(self, opt)
1266 return _get("wireless", self.sid, opt)
1269 function wifidev.set(self, opt, val)
1270 return _set("wireless", self.sid, opt, val)
1273 function wifidev.name(self)
1277 function wifidev.hwmodes(self)
1278 local l = self.iwinfo.hwmodelist
1279 if l and next(l) then
1282 return { b = true, g = true }
1286 function wifidev.get_i18n(self)
1288 if self.iwinfo.type == "wl" then
1290 elseif self.iwinfo.type == "madwifi" then
1295 local l = self:hwmodes()
1296 if l.a then m = m .. "a" end
1297 if l.b then m = m .. "b" end
1298 if l.g then m = m .. "g" end
1299 if l.n then m = m .. "n" end
1300 if l.ac then m = "ac" end
1302 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1305 function wifidev.is_up(self)
1306 if _ubuswificache[self.sid] then
1307 return (_ubuswificache[self.sid].up == true)
1313 function wifidev.get_wifinet(self, net)
1314 if _uci:get("wireless", net) == "wifi-iface" then
1317 local wnet = _wifi_lookup(net)
1319 return wifinet(wnet)
1324 function wifidev.get_wifinets(self)
1327 _uci:foreach("wireless", "wifi-iface",
1329 if s.device == self.sid then
1330 nets[#nets+1] = wifinet(s['.name'])
1337 function wifidev.add_wifinet(self, options)
1338 options = options or { }
1339 options.device = self.sid
1341 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1343 return wifinet(wnet, options)
1347 function wifidev.del_wifinet(self, net)
1348 if utl.instanceof(net, wifinet) then
1350 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1351 net = _wifi_lookup(net)
1354 if net and _uci:get("wireless", net, "device") == self.sid then
1355 _uci:delete("wireless", net)
1363 wifinet = utl.class()
1365 function wifinet.__init__(self, net, data)
1371 _uci:foreach("wireless", "wifi-iface",
1375 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1376 if s['.name'] == self.sid then
1377 sid = "@wifi-iface[%d]" % n
1378 netid = "%s.network%d" %{ s.device, num[s.device] }
1386 for k, r in pairs(_ubuswificache) do
1387 if type(r) == "table" and
1388 type(r.interfaces) == "table"
1390 for _, i in ipairs(r.interfaces) do
1391 if type(i) == "table" and i.section == sid then
1403 local dev = _wifi_state("section", self.sid, "ifname") or netid
1407 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1410 function wifinet.ubus(self, ...)
1411 local n, v = self._ubusdata
1412 for n = 1, select('#', ...) do
1413 if type(v) == "table" then
1414 v = v[select(n, ...)]
1422 function wifinet.get(self, opt)
1423 return _get("wireless", self.sid, opt)
1426 function wifinet.set(self, opt, val)
1427 return _set("wireless", self.sid, opt, val)
1430 function wifinet.mode(self)
1431 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1434 function wifinet.ssid(self)
1435 return self:ubus("net", "config", "ssid") or self:get("ssid")
1438 function wifinet.bssid(self)
1439 return self:ubus("net", "config", "bssid") or self:get("bssid")
1442 function wifinet.network(self)
1443 local net, networks = nil, { }
1444 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1445 networks[#networks+1] = net
1450 function wifinet.id(self)
1454 function wifinet.name(self)
1458 function wifinet.ifname(self)
1459 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1460 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1466 function wifinet.get_device(self)
1467 local dev = self:ubus("radio") or self:get("device")
1468 return dev and wifidev(dev) or nil
1471 function wifinet.is_up(self)
1472 local ifc = self:get_interface()
1473 return (ifc and ifc:is_up() or false)
1476 function wifinet.active_mode(self)
1477 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1479 if m == "ap" then m = "Master"
1480 elseif m == "sta" then m = "Client"
1481 elseif m == "adhoc" then m = "Ad-Hoc"
1482 elseif m == "mesh" then m = "Mesh"
1483 elseif m == "monitor" then m = "Monitor"
1489 function wifinet.active_mode_i18n(self)
1490 return lng.translate(self:active_mode())
1493 function wifinet.active_ssid(self)
1494 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1497 function wifinet.active_bssid(self)
1498 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1501 function wifinet.active_encryption(self)
1502 local enc = self.iwinfo and self.iwinfo.encryption
1503 return enc and enc.description or "-"
1506 function wifinet.assoclist(self)
1507 return self.iwinfo.assoclist or { }
1510 function wifinet.frequency(self)
1511 local freq = self.iwinfo.frequency
1512 if freq and freq > 0 then
1513 return "%.03f" % (freq / 1000)
1517 function wifinet.bitrate(self)
1518 local rate = self.iwinfo.bitrate
1519 if rate and rate > 0 then
1520 return (rate / 1000)
1524 function wifinet.channel(self)
1525 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1526 tonumber(self:get("channel"))
1529 function wifinet.signal(self)
1530 return self.iwinfo.signal or 0
1533 function wifinet.noise(self)
1534 return self.iwinfo.noise or 0
1537 function wifinet.country(self)
1538 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1541 function wifinet.txpower(self)
1542 local pwr = (self.iwinfo.txpower or 0)
1543 return pwr + self:txpower_offset()
1546 function wifinet.txpower_offset(self)
1547 return self.iwinfo.txpower_offset or 0
1550 function wifinet.signal_level(self, s, n)
1551 if self:active_bssid() ~= "00:00:00:00:00:00" then
1552 local signal = s or self:signal()
1553 local noise = n or self:noise()
1555 if signal < 0 and noise < 0 then
1556 local snr = -1 * (noise - signal)
1557 return math.floor(snr / 5)
1566 function wifinet.signal_percent(self)
1567 local qc = self.iwinfo.quality or 0
1568 local qm = self.iwinfo.quality_max or 0
1570 if qc > 0 and qm > 0 then
1571 return math.floor((100 / qm) * qc)
1577 function wifinet.shortname(self)
1579 lng.translate(self:active_mode()),
1580 self:active_ssid() or self:active_bssid()
1584 function wifinet.get_i18n(self)
1585 return "%s: %s %q (%s)" %{
1586 lng.translate("Wireless Network"),
1587 lng.translate(self:active_mode()),
1588 self:active_ssid() or self:active_bssid(),
1593 function wifinet.adminlink(self)
1594 return dsp.build_url("admin", "network", "wireless", self.netid)
1597 function wifinet.get_network(self)
1598 return self:get_networks()[1]
1601 function wifinet.get_networks(self)
1604 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1605 if _uci:get("network", net) == "interface" then
1606 nets[#nets+1] = network(net)
1609 table.sort(nets, function(a, b) return a.sid < b.sid end)
1613 function wifinet.get_interface(self)
1614 return interface(self:ifname())
1618 -- setup base protocols
1619 _M:register_protocol("static")
1620 _M:register_protocol("dhcp")
1621 _M:register_protocol("none")
1623 -- load protocol extensions
1624 local exts = nfs.dir(utl.libpath() .. "/model/network")
1628 if ext:match("%.lua$") then
1629 require("luci.model.network." .. ext:gsub("%.lua$", ""))