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", "^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
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
193 return _iface_virtual(x)
197 function init(cursor)
198 _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("[^:]+")
213 local prnt = name:match("^([^%.]+)%.")
215 if _iface_virtual(name) then
219 if _tunnel[name] or not _iface_ignore(name) then
220 _interfaces[name] = _interfaces[name] or {
221 idx = i.ifindex or n,
234 if i.family == "packet" then
235 _interfaces[name].flags = i.flags
236 _interfaces[name].stats = i.data
237 _interfaces[name].macaddr = i.addr
238 elseif i.family == "inet" then
239 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
240 elseif i.family == "inet6" then
241 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
246 -- read bridge informaton
248 for l in utl.execi("brctl show") do
249 if not l:match("STP") then
250 local r = utl.split(l, "%s+", nil, true)
256 ifnames = { _interfaces[r[4]] }
259 b.ifnames[1].bridge = b
263 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
264 b.ifnames[#b.ifnames].bridge = b
272 function save(self, ...)
277 function commit(self, ...)
282 function ifnameof(self, x)
283 if utl.instanceof(x, interface) then
285 elseif utl.instanceof(x, protocol) then
287 elseif type(x) == "string" then
288 return x:match("^[^:]+")
292 function get_protocol(self, protoname, netname)
293 local v = _protocols[protoname]
295 return v(netname or "__dummy__")
299 function get_protocols(self)
302 for _, v in ipairs(_protocols) do
303 p[#p+1] = v("__dummy__")
308 function register_protocol(self, protoname)
309 local proto = utl.class(protocol)
311 function proto.__init__(self, name)
315 function proto.proto(self)
319 _protocols[#_protocols+1] = proto
320 _protocols[protoname] = proto
325 function register_pattern_virtual(self, pat)
326 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
330 function has_ipv6(self)
331 return nfs.access("/proc/net/ipv6_route")
334 function add_network(self, n, options)
335 local oldnet = self:get_network(n)
336 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
337 if _uci:section("network", "interface", n, options) then
340 elseif oldnet and oldnet:is_empty() then
343 for k, v in pairs(options) do
351 function get_network(self, n)
352 if n and _uci:get("network", n) == "interface" then
357 function get_networks(self)
361 _uci:foreach("network", "interface",
363 nls[s['.name']] = network(s['.name'])
367 for n in utl.kspairs(nls) do
368 nets[#nets+1] = nls[n]
374 function del_network(self, n)
375 local r = _uci:delete("network", n)
377 _uci:delete_all("network", "alias",
378 function(s) return (s.interface == n) end)
380 _uci:delete_all("network", "route",
381 function(s) return (s.interface == n) end)
383 _uci:delete_all("network", "route6",
384 function(s) return (s.interface == n) end)
386 _uci:foreach("wireless", "wifi-iface",
390 for net in utl.imatch(s.network) do
396 _uci:set("wireless", s['.name'], "network",
397 table.concat(rest, " "))
399 _uci:delete("wireless", s['.name'], "network")
406 function rename_network(self, old, new)
408 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
409 r = _uci:section("network", "interface", new, _uci:get_all("network", old))
412 _uci:foreach("network", "alias",
414 if s.interface == old then
415 _uci:set("network", s['.name'], "interface", new)
419 _uci:foreach("network", "route",
421 if s.interface == old then
422 _uci:set("network", s['.name'], "interface", new)
426 _uci:foreach("network", "route6",
428 if s.interface == old then
429 _uci:set("network", s['.name'], "interface", new)
433 _uci:foreach("wireless", "wifi-iface",
437 for net in utl.imatch(s.network) do
445 _uci:set("wireless", s['.name'], "network",
446 table.concat(list, " "))
450 _uci:delete("network", old)
456 function get_interface(self, i)
457 if _interfaces[i] or _wifi_iface(i) then
462 _uci:foreach("wireless", "wifi-iface",
465 num[s.device] = num[s.device] and num[s.device] + 1 or 1
466 if s['.name'] == i then
468 "%s.network%d" %{s.device, num[s.device] })
477 local function swdev_from_board_json()
478 local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
479 if type(boardinfo) == "table" and type(boardinfo.network) == "table" then
481 for net, val in pairs(boardinfo.network) do
482 if type(val) == "table" and type(val.ifname) == "string" and
483 val.create_vlan == true
492 function get_interfaces(self)
499 -- find normal interfaces
500 _uci:foreach("network", "interface",
502 for iface in utl.imatch(s.ifname) do
503 if not _iface_ignore(iface) and not _wifi_iface(iface) then
505 nfs[iface] = interface(iface)
510 for iface in utl.kspairs(_interfaces) do
511 if not (seen[iface] or _iface_ignore(iface) or _wifi_iface(iface)) then
512 nfs[iface] = interface(iface)
516 -- find vlan interfaces
517 _uci:foreach("network", "switch_vlan",
523 local base = baseof[s.device]
525 if not s.device:match("^eth%d") then
527 for l in utl.execi("swconfig dev %q help 2>/dev/null" % s.device) do
529 base = l:match("^%w+: (%w+)")
532 if not base or not base:match("^eth%d") then
533 base = swdev_from_board_json() or "eth0"
538 baseof[s.device] = base
541 local vid = tonumber(s.vid or s.vlan)
542 if vid ~= nil and vid >= 0 and vid <= 4095 then
543 local iface = "%s.%d" %{ base, vid }
544 if not seen[iface] then
546 nfs[iface] = interface(iface)
551 for iface in utl.kspairs(nfs) do
552 ifaces[#ifaces+1] = nfs[iface]
555 -- find wifi interfaces
558 _uci:foreach("wireless", "wifi-iface",
561 num[s.device] = num[s.device] and num[s.device] + 1 or 1
562 local i = "%s.network%d" %{ s.device, num[s.device] }
563 wfs[i] = interface(i)
567 for iface in utl.kspairs(wfs) do
568 ifaces[#ifaces+1] = wfs[iface]
574 function ignore_interface(self, x)
575 return _iface_ignore(x)
578 function get_wifidev(self, dev)
579 if _uci:get("wireless", dev) == "wifi-device" then
584 function get_wifidevs(self)
588 _uci:foreach("wireless", "wifi-device",
589 function(s) wfd[#wfd+1] = s['.name'] end)
592 for _, dev in utl.vspairs(wfd) do
593 devs[#devs+1] = wifidev(dev)
599 function get_wifinet(self, net)
600 local wnet = _wifi_lookup(net)
606 function add_wifinet(self, net, options)
607 if type(options) == "table" and options.device and
608 _uci:get("wireless", options.device) == "wifi-device"
610 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
615 function del_wifinet(self, net)
616 local wnet = _wifi_lookup(net)
618 _uci:delete("wireless", wnet)
624 function get_status_by_route(self, addr, mask)
626 for _, object in ipairs(utl.ubus()) do
627 local net = object:match("^network%.interface%.(.+)")
629 local s = utl.ubus(object, "status", {})
630 if s and s.route then
632 for _, rt in ipairs(s.route) do
633 if not rt.table and rt.target == addr and rt.mask == mask then
642 function get_status_by_address(self, addr)
644 for _, object in ipairs(utl.ubus()) do
645 local net = object:match("^network%.interface%.(.+)")
647 local s = utl.ubus(object, "status", {})
648 if s and s['ipv4-address'] then
650 for _, a in ipairs(s['ipv4-address']) do
651 if a.address == addr then
656 if s and s['ipv6-address'] then
658 for _, a in ipairs(s['ipv6-address']) do
659 if a.address == addr then
668 function get_wannet(self)
669 local net, stat = self:get_status_by_route("0.0.0.0", 0)
670 return net and network(net, stat.proto)
673 function get_wandev(self)
674 local _, stat = self:get_status_by_route("0.0.0.0", 0)
675 return stat and interface(stat.l3_device or stat.device)
678 function get_wan6net(self)
679 local net, stat = self:get_status_by_route("::", 0)
680 return net and network(net, stat.proto)
683 function get_wan6dev(self)
684 local _, stat = self:get_status_by_route("::", 0)
685 return stat and interface(stat.l3_device or stat.device)
689 function network(name, proto)
691 local p = proto or _uci:get("network", name, "proto")
692 local c = p and _protocols[p] or protocol
697 function protocol.__init__(self, name)
701 function protocol._get(self, opt)
702 local v = _uci:get("network", self.sid, opt)
703 if type(v) == "table" then
704 return table.concat(v, " ")
709 function protocol._ubus(self, field)
710 if not _ubusnetcache[self.sid] then
711 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
714 if _ubusnetcache[self.sid] and field then
715 return _ubusnetcache[self.sid][field]
717 return _ubusnetcache[self.sid]
720 function protocol.get(self, opt)
721 return _get("network", self.sid, opt)
724 function protocol.set(self, opt, val)
725 return _set("network", self.sid, opt, val)
728 function protocol.ifname(self)
730 if self:is_floating() then
731 ifname = self:_ubus("l3_device")
733 ifname = self:_ubus("device")
737 _uci:foreach("wireless", "wifi-iface",
740 num[s.device] = num[s.device]
741 and num[s.device] + 1 or 1
744 for net in utl.imatch(s.network) do
745 if net == self.sid then
746 ifname = "%s.network%d" %{ s.device, num[s.device] }
756 function protocol.proto(self)
760 function protocol.get_i18n(self)
761 local p = self:proto()
763 return lng.translate("Unmanaged")
764 elseif p == "static" then
765 return lng.translate("Static address")
766 elseif p == "dhcp" then
767 return lng.translate("DHCP client")
769 return lng.translate("Unknown")
773 function protocol.type(self)
774 return self:_get("type")
777 function protocol.name(self)
781 function protocol.uptime(self)
782 return self:_ubus("uptime") or 0
785 function protocol.expires(self)
786 local u = self:_ubus("uptime")
787 local d = self:_ubus("data")
789 if type(u) == "number" and type(d) == "table" and
790 type(d.leasetime) == "number"
792 local r = (d.leasetime - (u % d.leasetime))
793 return r > 0 and r or 0
799 function protocol.metric(self)
800 return self:_ubus("metric") or 0
803 function protocol.ipaddr(self)
804 local addrs = self:_ubus("ipv4-address")
805 return addrs and #addrs > 0 and addrs[1].address
808 function protocol.ipaddrs(self)
809 local addrs = self:_ubus("ipv4-address")
812 if type(addrs) == "table" then
814 for n, addr in ipairs(addrs) do
815 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
822 function protocol.netmask(self)
823 local addrs = self:_ubus("ipv4-address")
824 return addrs and #addrs > 0 and
825 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
828 function protocol.gwaddr(self)
830 for _, route in ipairs(self:_ubus("route") or { }) do
831 if route.target == "0.0.0.0" and route.mask == 0 then
837 function protocol.dnsaddrs(self)
840 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
841 if not addr:match(":") then
848 function protocol.ip6addr(self)
849 local addrs = self:_ubus("ipv6-address")
850 if addrs and #addrs > 0 then
851 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
853 addrs = self:_ubus("ipv6-prefix-assignment")
854 if addrs and #addrs > 0 then
855 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
860 function protocol.ip6addrs(self)
861 local addrs = self:_ubus("ipv6-address")
865 if type(addrs) == "table" then
866 for n, addr in ipairs(addrs) do
867 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
871 addrs = self:_ubus("ipv6-prefix-assignment")
873 if type(addrs) == "table" then
874 for n, addr in ipairs(addrs) do
875 rv[#rv+1] = "%s1/%d" %{ addr.address, addr.mask }
882 function protocol.gw6addr(self)
884 for _, route in ipairs(self:_ubus("route") or { }) do
885 if route.target == "::" and route.mask == 0 then
886 return ipc.IPv6(route.nexthop):string()
891 function protocol.dns6addrs(self)
894 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
895 if addr:match(":") then
902 function protocol.is_bridge(self)
903 return (not self:is_virtual() and self:type() == "bridge")
906 function protocol.opkg_package(self)
910 function protocol.is_installed(self)
914 function protocol.is_virtual(self)
918 function protocol.is_floating(self)
922 function protocol.is_empty(self)
923 if self:is_floating() then
928 if (self:_get("ifname") or ""):match("%S+") then
932 _uci:foreach("wireless", "wifi-iface",
935 for n in utl.imatch(s.network) do
936 if n == self.sid then
947 function protocol.add_interface(self, ifname)
948 ifname = _M:ifnameof(ifname)
949 if ifname and not self:is_floating() then
950 -- if its a wifi interface, change its network option
951 local wif = _wifi_lookup(ifname)
953 _append("wireless", wif, "network", self.sid)
955 -- add iface to our iface list
957 _append("network", self.sid, "ifname", ifname)
962 function protocol.del_interface(self, ifname)
963 ifname = _M:ifnameof(ifname)
964 if ifname and not self:is_floating() then
965 -- if its a wireless interface, clear its network option
966 local wif = _wifi_lookup(ifname)
967 if wif then _filter("wireless", wif, "network", self.sid) end
969 -- remove the interface
970 _filter("network", self.sid, "ifname", ifname)
974 function protocol.get_interface(self)
975 if self:is_virtual() then
976 _tunnel[self:proto() .. "-" .. self.sid] = true
977 return interface(self:proto() .. "-" .. self.sid, self)
978 elseif self:is_bridge() then
979 _bridge["br-" .. self.sid] = true
980 return interface("br-" .. self.sid, self)
984 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
985 ifn = ifn:match("^[^:/]+")
986 return ifn and interface(ifn, self)
989 _uci:foreach("wireless", "wifi-iface",
992 num[s.device] = num[s.device] and num[s.device] + 1 or 1
995 for net in utl.imatch(s.network) do
996 if net == self.sid then
997 ifn = "%s.network%d" %{ s.device, num[s.device] }
1003 return ifn and interface(ifn, self)
1007 function protocol.get_interfaces(self)
1008 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
1013 for ifn in utl.imatch(self:get("ifname")) do
1014 ifn = ifn:match("^[^:/]+")
1015 nfs[ifn] = interface(ifn, self)
1018 for ifn in utl.kspairs(nfs) do
1019 ifaces[#ifaces+1] = nfs[ifn]
1024 _uci:foreach("wireless", "wifi-iface",
1027 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1030 for net in utl.imatch(s.network) do
1031 if net == self.sid then
1032 ifn = "%s.network%d" %{ s.device, num[s.device] }
1033 wfs[ifn] = interface(ifn, self)
1039 for ifn in utl.kspairs(wfs) do
1040 ifaces[#ifaces+1] = wfs[ifn]
1047 function protocol.contains_interface(self, ifname)
1048 ifname = _M:ifnameof(ifname)
1051 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1053 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1057 for ifn in utl.imatch(self:get("ifname")) do
1058 ifn = ifn:match("[^:]+")
1059 if ifn == ifname then
1064 local wif = _wifi_lookup(ifname)
1067 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1068 if n == self.sid then
1078 function protocol.adminlink(self)
1079 return dsp.build_url("admin", "network", "network", self.sid)
1083 interface = utl.class()
1085 function interface.__init__(self, ifname, network)
1086 local wif = _wifi_lookup(ifname)
1088 self.wif = wifinet(wif)
1089 self.ifname = _wifi_state("section", wif, "ifname")
1092 self.ifname = self.ifname or ifname
1093 self.dev = _interfaces[self.ifname]
1094 self.network = network
1097 function interface._ubus(self, field)
1098 if not _ubusdevcache[self.ifname] then
1099 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1100 { name = self.ifname })
1102 if _ubusdevcache[self.ifname] and field then
1103 return _ubusdevcache[self.ifname][field]
1105 return _ubusdevcache[self.ifname]
1108 function interface.name(self)
1109 return self.wif and self.wif:ifname() or self.ifname
1112 function interface.mac(self)
1113 local mac = self:_ubus("macaddr")
1114 return mac and mac:upper()
1117 function interface.ipaddrs(self)
1118 return self.dev and self.dev.ipaddrs or { }
1121 function interface.ip6addrs(self)
1122 return self.dev and self.dev.ip6addrs or { }
1125 function interface.type(self)
1126 if self.wif or _wifi_iface(self.ifname) then
1128 elseif _bridge[self.ifname] then
1130 elseif _tunnel[self.ifname] then
1132 elseif self.ifname:match("%.") then
1134 elseif _switch[self.ifname] then
1141 function interface.shortname(self)
1144 self.wif:active_mode(),
1145 self.wif:active_ssid() or self.wif:active_bssid()
1152 function interface.get_i18n(self)
1154 return "%s: %s %q" %{
1155 lng.translate("Wireless Network"),
1156 self.wif:active_mode(),
1157 self.wif:active_ssid() or self.wif:active_bssid()
1160 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1164 function interface.get_type_i18n(self)
1165 local x = self:type()
1167 return lng.translate("Wireless Adapter")
1168 elseif x == "bridge" then
1169 return lng.translate("Bridge")
1170 elseif x == "switch" then
1171 return lng.translate("Ethernet Switch")
1172 elseif x == "vlan" then
1173 return lng.translate("VLAN Interface")
1174 elseif x == "tunnel" then
1175 return lng.translate("Tunnel Interface")
1177 return lng.translate("Ethernet Adapter")
1181 function interface.adminlink(self)
1183 return self.wif:adminlink()
1187 function interface.ports(self)
1188 local members = self:_ubus("bridge-members")
1192 for _, iface in ipairs(members) do
1193 ifaces[#ifaces+1] = interface(iface)
1198 function interface.bridge_id(self)
1206 function interface.bridge_stp(self)
1214 function interface.is_up(self)
1215 return self:_ubus("up") or false
1218 function interface.is_bridge(self)
1219 return (self:type() == "bridge")
1222 function interface.is_bridgeport(self)
1223 return self.dev and self.dev.bridge and true or false
1226 function interface.tx_bytes(self)
1227 local stat = self:_ubus("statistics")
1228 return stat and stat.tx_bytes or 0
1231 function interface.rx_bytes(self)
1232 local stat = self:_ubus("statistics")
1233 return stat and stat.rx_bytes or 0
1236 function interface.tx_packets(self)
1237 local stat = self:_ubus("statistics")
1238 return stat and stat.tx_packets or 0
1241 function interface.rx_packets(self)
1242 local stat = self:_ubus("statistics")
1243 return stat and stat.rx_packets or 0
1246 function interface.get_network(self)
1247 return self:get_networks()[1]
1250 function interface.get_networks(self)
1251 if not self.networks then
1254 for _, net in ipairs(_M:get_networks()) do
1255 if net:contains_interface(self.ifname) or
1256 net:ifname() == self.ifname
1261 table.sort(nets, function(a, b) return a.sid < b.sid end)
1262 self.networks = nets
1265 return self.networks
1269 function interface.get_wifinet(self)
1274 wifidev = utl.class()
1276 function wifidev.__init__(self, dev)
1278 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1281 function wifidev.get(self, opt)
1282 return _get("wireless", self.sid, opt)
1285 function wifidev.set(self, opt, val)
1286 return _set("wireless", self.sid, opt, val)
1289 function wifidev.name(self)
1293 function wifidev.hwmodes(self)
1294 local l = self.iwinfo.hwmodelist
1295 if l and next(l) then
1298 return { b = true, g = true }
1302 function wifidev.get_i18n(self)
1304 if self.iwinfo.type == "wl" then
1306 elseif self.iwinfo.type == "madwifi" then
1311 local l = self:hwmodes()
1312 if l.a then m = m .. "a" end
1313 if l.b then m = m .. "b" end
1314 if l.g then m = m .. "g" end
1315 if l.n then m = m .. "n" end
1316 if l.ac then m = "ac" end
1318 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1321 function wifidev.is_up(self)
1322 if _ubuswificache[self.sid] then
1323 return (_ubuswificache[self.sid].up == true)
1329 function wifidev.get_wifinet(self, net)
1330 if _uci:get("wireless", net) == "wifi-iface" then
1333 local wnet = _wifi_lookup(net)
1335 return wifinet(wnet)
1340 function wifidev.get_wifinets(self)
1343 _uci:foreach("wireless", "wifi-iface",
1345 if s.device == self.sid then
1346 nets[#nets+1] = wifinet(s['.name'])
1353 function wifidev.add_wifinet(self, options)
1354 options = options or { }
1355 options.device = self.sid
1357 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1359 return wifinet(wnet, options)
1363 function wifidev.del_wifinet(self, net)
1364 if utl.instanceof(net, wifinet) then
1366 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1367 net = _wifi_lookup(net)
1370 if net and _uci:get("wireless", net, "device") == self.sid then
1371 _uci:delete("wireless", net)
1379 wifinet = utl.class()
1381 function wifinet.__init__(self, net, data)
1387 _uci:foreach("wireless", "wifi-iface",
1391 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1392 if s['.name'] == self.sid then
1393 sid = "@wifi-iface[%d]" % n
1394 netid = "%s.network%d" %{ s.device, num[s.device] }
1402 for k, r in pairs(_ubuswificache) do
1403 if type(r) == "table" and
1404 type(r.interfaces) == "table"
1406 for _, i in ipairs(r.interfaces) do
1407 if type(i) == "table" and i.section == sid then
1419 local dev = _wifi_state("section", self.sid, "ifname") or netid
1423 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1426 function wifinet.ubus(self, ...)
1427 local n, v = self._ubusdata
1428 for n = 1, select('#', ...) do
1429 if type(v) == "table" then
1430 v = v[select(n, ...)]
1438 function wifinet.get(self, opt)
1439 return _get("wireless", self.sid, opt)
1442 function wifinet.set(self, opt, val)
1443 return _set("wireless", self.sid, opt, val)
1446 function wifinet.mode(self)
1447 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1450 function wifinet.ssid(self)
1451 return self:ubus("net", "config", "ssid") or self:get("ssid")
1454 function wifinet.bssid(self)
1455 return self:ubus("net", "config", "bssid") or self:get("bssid")
1458 function wifinet.network(self)
1459 local net, networks = nil, { }
1460 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1461 networks[#networks+1] = net
1466 function wifinet.id(self)
1470 function wifinet.name(self)
1474 function wifinet.ifname(self)
1475 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1476 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1482 function wifinet.get_device(self)
1483 local dev = self:ubus("radio") or self:get("device")
1484 return dev and wifidev(dev) or nil
1487 function wifinet.is_up(self)
1488 local ifc = self:get_interface()
1489 return (ifc and ifc:is_up() or false)
1492 function wifinet.active_mode(self)
1493 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1495 if m == "ap" then m = "Master"
1496 elseif m == "sta" then m = "Client"
1497 elseif m == "adhoc" then m = "Ad-Hoc"
1498 elseif m == "mesh" then m = "Mesh"
1499 elseif m == "monitor" then m = "Monitor"
1505 function wifinet.active_mode_i18n(self)
1506 return lng.translate(self:active_mode())
1509 function wifinet.active_ssid(self)
1510 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1513 function wifinet.active_bssid(self)
1514 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1517 function wifinet.active_encryption(self)
1518 local enc = self.iwinfo and self.iwinfo.encryption
1519 return enc and enc.description or "-"
1522 function wifinet.assoclist(self)
1523 return self.iwinfo.assoclist or { }
1526 function wifinet.frequency(self)
1527 local freq = self.iwinfo.frequency
1528 if freq and freq > 0 then
1529 return "%.03f" % (freq / 1000)
1533 function wifinet.bitrate(self)
1534 local rate = self.iwinfo.bitrate
1535 if rate and rate > 0 then
1536 return (rate / 1000)
1540 function wifinet.channel(self)
1541 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1542 tonumber(self:get("channel"))
1545 function wifinet.signal(self)
1546 return self.iwinfo.signal or 0
1549 function wifinet.noise(self)
1550 return self.iwinfo.noise or 0
1553 function wifinet.country(self)
1554 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1557 function wifinet.txpower(self)
1558 local pwr = (self.iwinfo.txpower or 0)
1559 return pwr + self:txpower_offset()
1562 function wifinet.txpower_offset(self)
1563 return self.iwinfo.txpower_offset or 0
1566 function wifinet.signal_level(self, s, n)
1567 if self:active_bssid() ~= "00:00:00:00:00:00" then
1568 local signal = s or self:signal()
1569 local noise = n or self:noise()
1571 if signal < 0 and noise < 0 then
1572 local snr = -1 * (noise - signal)
1573 return math.floor(snr / 5)
1582 function wifinet.signal_percent(self)
1583 local qc = self.iwinfo.quality or 0
1584 local qm = self.iwinfo.quality_max or 0
1586 if qc > 0 and qm > 0 then
1587 return math.floor((100 / qm) * qc)
1593 function wifinet.shortname(self)
1595 lng.translate(self:active_mode()),
1596 self:active_ssid() or self:active_bssid()
1600 function wifinet.get_i18n(self)
1601 return "%s: %s %q (%s)" %{
1602 lng.translate("Wireless Network"),
1603 lng.translate(self:active_mode()),
1604 self:active_ssid() or self:active_bssid(),
1609 function wifinet.adminlink(self)
1610 return dsp.build_url("admin", "network", "wireless", self.netid)
1613 function wifinet.get_network(self)
1614 return self:get_networks()[1]
1617 function wifinet.get_networks(self)
1620 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1621 if _uci:get("network", net) == "interface" then
1622 nets[#nets+1] = network(net)
1625 table.sort(nets, function(a, b) return a.sid < b.sid end)
1629 function wifinet.get_interface(self)
1630 return interface(self:ifname())
1634 -- setup base protocols
1635 _M:register_protocol("static")
1636 _M:register_protocol("dhcp")
1637 _M:register_protocol("none")
1639 -- load protocol extensions
1640 local exts = nfs.dir(utl.libpath() .. "/model/network")
1644 if ext:match("%.lua$") then
1645 require("luci.model.network." .. ext:gsub("%.lua$", ""))