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
5 = type, next, pairs, ipairs, loadfile, table
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
34 local _uci_real, _uci_state
36 function _filter(c, s, o, r)
37 local val = _uci_real:get(c, s, o)
40 if type(val) == "string" then
41 for val in val:gmatch("%S+") do
47 _uci_real:set(c, s, o, table.concat(l, " "))
49 _uci_real:delete(c, s, o)
51 elseif type(val) == "table" then
52 for _, val in ipairs(val) do
58 _uci_real:set(c, s, o, l)
60 _uci_real:delete(c, s, o)
66 function _append(c, s, o, a)
67 local val = _uci_real:get(c, s, o) or ""
68 if type(val) == "string" then
70 for val in val:gmatch("%S+") do
76 _uci_real:set(c, s, o, table.concat(l, " "))
77 elseif type(val) == "table" then
79 for _, val in ipairs(val) do
85 _uci_real:set(c, s, o, l)
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_real: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_real:set(c, s, o, v)
106 return _uci_real: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_real: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_real: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 state vars
170 elseif _wifi_iface(ifn) then
171 local sid = _wifi_state("ifname", ifn, "section")
173 _uci_state:foreach("wireless", "wifi-iface",
175 if s.ifname == ifn then
186 function _iface_virtual(x)
188 for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
196 function _iface_ignore(x)
198 for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
203 return _iface_virtual(x)
207 function init(cursor)
208 _uci_real = cursor or _uci_real or uci.cursor()
209 _uci_state = _uci_real:substate()
220 -- read interface information
222 for n, i in ipairs(nxo.getifaddrs()) do
223 local name = i.name:match("[^:]+")
224 local prnt = name:match("^([^%.]+)%.")
226 if _iface_virtual(name) then
230 if _tunnel[name] or not _iface_ignore(name) then
231 _interfaces[name] = _interfaces[name] or {
232 idx = i.ifindex or n,
245 if i.family == "packet" then
246 _interfaces[name].flags = i.flags
247 _interfaces[name].stats = i.data
248 _interfaces[name].macaddr = i.addr
249 elseif i.family == "inet" then
250 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
251 elseif i.family == "inet6" then
252 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
257 -- read bridge informaton
259 for l in utl.execi("brctl show") do
260 if not l:match("STP") then
261 local r = utl.split(l, "%s+", nil, true)
267 ifnames = { _interfaces[r[4]] }
270 b.ifnames[1].bridge = b
274 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
275 b.ifnames[#b.ifnames].bridge = b
283 function save(self, ...)
288 function commit(self, ...)
289 _uci_real:commit(...)
293 function ifnameof(self, x)
294 if utl.instanceof(x, interface) then
296 elseif utl.instanceof(x, protocol) then
298 elseif type(x) == "string" then
299 return x:match("^[^:]+")
303 function get_protocol(self, protoname, netname)
304 local v = _protocols[protoname]
306 return v(netname or "__dummy__")
310 function get_protocols(self)
313 for _, v in ipairs(_protocols) do
314 p[#p+1] = v("__dummy__")
319 function register_protocol(self, protoname)
320 local proto = utl.class(protocol)
322 function proto.__init__(self, name)
326 function proto.proto(self)
330 _protocols[#_protocols+1] = proto
331 _protocols[protoname] = proto
336 function register_pattern_virtual(self, pat)
337 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
341 function has_ipv6(self)
342 return nfs.access("/proc/net/ipv6_route")
345 function add_network(self, n, options)
346 local oldnet = self:get_network(n)
347 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
348 if _uci_real:section("network", "interface", n, options) then
351 elseif oldnet and oldnet:is_empty() then
354 for k, v in pairs(options) do
362 function get_network(self, n)
363 if n and _uci_real:get("network", n) == "interface" then
368 function get_networks(self)
372 _uci_real:foreach("network", "interface",
374 nls[s['.name']] = network(s['.name'])
378 for n in utl.kspairs(nls) do
379 nets[#nets+1] = nls[n]
385 function del_network(self, n)
386 local r = _uci_real:delete("network", n)
388 _uci_real:delete_all("network", "alias",
389 function(s) return (s.interface == n) end)
391 _uci_real:delete_all("network", "route",
392 function(s) return (s.interface == n) end)
394 _uci_real:delete_all("network", "route6",
395 function(s) return (s.interface == n) end)
397 _uci_real:foreach("wireless", "wifi-iface",
401 for net in utl.imatch(s.network) do
407 _uci_real:set("wireless", s['.name'], "network",
408 table.concat(rest, " "))
410 _uci_real:delete("wireless", s['.name'], "network")
417 function rename_network(self, old, new)
419 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
420 r = _uci_real:section("network", "interface", new, _uci_real:get_all("network", old))
423 _uci_real:foreach("network", "alias",
425 if s.interface == old then
426 _uci_real:set("network", s['.name'], "interface", new)
430 _uci_real:foreach("network", "route",
432 if s.interface == old then
433 _uci_real:set("network", s['.name'], "interface", new)
437 _uci_real:foreach("network", "route6",
439 if s.interface == old then
440 _uci_real:set("network", s['.name'], "interface", new)
444 _uci_real:foreach("wireless", "wifi-iface",
448 for net in utl.imatch(s.network) do
456 _uci_real:set("wireless", s['.name'], "network",
457 table.concat(list, " "))
461 _uci_real:delete("network", old)
467 function get_interface(self, i)
468 if _interfaces[i] or _wifi_iface(i) then
473 _uci_real:foreach("wireless", "wifi-iface",
476 num[s.device] = num[s.device] and num[s.device] + 1 or 1
477 if s['.name'] == i then
479 "%s.network%d" %{s.device, num[s.device] })
488 function get_interfaces(self)
495 -- find normal interfaces
496 _uci_real:foreach("network", "interface",
498 for iface in utl.imatch(s.ifname) do
499 if not _iface_ignore(iface) and not _wifi_iface(iface) then
501 nfs[iface] = interface(iface)
506 for iface in utl.kspairs(_interfaces) do
507 if not (seen[iface] or _iface_ignore(iface) or _wifi_iface(iface)) then
508 nfs[iface] = interface(iface)
512 -- find vlan interfaces
513 _uci_real:foreach("network", "switch_vlan",
519 local base = baseof[s.device]
521 if not s.device:match("^eth%d") then
523 for l in utl.execi("swconfig dev %q help 2>/dev/null" % s.device) do
525 base = l:match("^%w+: (%w+)")
528 if not base or not base:match("^eth%d") then
534 baseof[s.device] = base
537 local vid = tonumber(s.vid or s.vlan)
538 if vid ~= nil and vid >= 0 and vid <= 4095 then
539 local iface = "%s.%d" %{ base, vid }
540 if not seen[iface] then
542 nfs[iface] = interface(iface)
547 for iface in utl.kspairs(nfs) do
548 ifaces[#ifaces+1] = nfs[iface]
551 -- find wifi interfaces
554 _uci_real:foreach("wireless", "wifi-iface",
557 num[s.device] = num[s.device] and num[s.device] + 1 or 1
558 local i = "%s.network%d" %{ s.device, num[s.device] }
559 wfs[i] = interface(i)
563 for iface in utl.kspairs(wfs) do
564 ifaces[#ifaces+1] = wfs[iface]
570 function ignore_interface(self, x)
571 return _iface_ignore(x)
574 function get_wifidev(self, dev)
575 if _uci_real:get("wireless", dev) == "wifi-device" then
580 function get_wifidevs(self)
584 _uci_real:foreach("wireless", "wifi-device",
585 function(s) wfd[#wfd+1] = s['.name'] end)
588 for _, dev in utl.vspairs(wfd) do
589 devs[#devs+1] = wifidev(dev)
595 function get_wifinet(self, net)
596 local wnet = _wifi_lookup(net)
602 function add_wifinet(self, net, options)
603 if type(options) == "table" and options.device and
604 _uci_real:get("wireless", options.device) == "wifi-device"
606 local wnet = _uci_real:section("wireless", "wifi-iface", nil, options)
611 function del_wifinet(self, net)
612 local wnet = _wifi_lookup(net)
614 _uci_real:delete("wireless", wnet)
620 function get_status_by_route(self, addr, mask)
622 for _, object in ipairs(utl.ubus()) do
623 local net = object:match("^network%.interface%.(.+)")
625 local s = utl.ubus(object, "status", {})
626 if s and s.route then
628 for _, rt in ipairs(s.route) do
629 if not rt.table and rt.target == addr and rt.mask == mask then
638 function get_status_by_address(self, addr)
640 for _, object in ipairs(utl.ubus()) do
641 local net = object:match("^network%.interface%.(.+)")
643 local s = utl.ubus(object, "status", {})
644 if s and s['ipv4-address'] then
646 for _, a in ipairs(s['ipv4-address']) do
647 if a.address == addr then
652 if s and s['ipv6-address'] then
654 for _, a in ipairs(s['ipv6-address']) do
655 if a.address == addr then
664 function get_wannet(self)
665 local net = self:get_status_by_route("0.0.0.0", 0)
666 return net and network(net)
669 function get_wandev(self)
670 local _, stat = self:get_status_by_route("0.0.0.0", 0)
671 return stat and interface(stat.l3_device or stat.device)
674 function get_wan6net(self)
675 local net = self:get_status_by_route("::", 0)
676 return net and network(net)
679 function get_wan6dev(self)
680 local _, stat = self:get_status_by_route("::", 0)
681 return stat and interface(stat.l3_device or stat.device)
685 function network(name, proto)
687 local p = proto or _uci_real:get("network", name, "proto")
688 local c = p and _protocols[p] or protocol
693 function protocol.__init__(self, name)
697 function protocol._get(self, opt)
698 local v = _uci_real:get("network", self.sid, opt)
699 if type(v) == "table" then
700 return table.concat(v, " ")
705 function protocol._ubus(self, field)
706 if not _ubusnetcache[self.sid] then
707 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
710 if _ubusnetcache[self.sid] and field then
711 return _ubusnetcache[self.sid][field]
713 return _ubusnetcache[self.sid]
716 function protocol.get(self, opt)
717 return _get("network", self.sid, opt)
720 function protocol.set(self, opt, val)
721 return _set("network", self.sid, opt, val)
724 function protocol.ifname(self)
726 if self:is_floating() then
727 ifname = self:_ubus("l3_device")
729 ifname = self:_ubus("device")
733 _uci_real:foreach("wireless", "wifi-iface",
736 num[s.device] = num[s.device]
737 and num[s.device] + 1 or 1
740 for net in utl.imatch(s.network) do
741 if net == self.sid then
742 ifname = "%s.network%d" %{ s.device, num[s.device] }
752 function protocol.proto(self)
756 function protocol.get_i18n(self)
757 local p = self:proto()
759 return lng.translate("Unmanaged")
760 elseif p == "static" then
761 return lng.translate("Static address")
762 elseif p == "dhcp" then
763 return lng.translate("DHCP client")
765 return lng.translate("Unknown")
769 function protocol.type(self)
770 return self:_get("type")
773 function protocol.name(self)
777 function protocol.uptime(self)
778 return self:_ubus("uptime") or 0
781 function protocol.expires(self)
782 local a = tonumber(_uci_state:get("network", self.sid, "lease_acquired"))
783 local l = tonumber(_uci_state:get("network", self.sid, "lease_lifetime"))
785 l = l - (nxo.sysinfo().uptime - a)
786 return l > 0 and l or 0
791 function protocol.metric(self)
792 return tonumber(_uci_state:get("network", self.sid, "metric")) or 0
795 function protocol.ipaddr(self)
796 local addrs = self:_ubus("ipv4-address")
797 return addrs and #addrs > 0 and addrs[1].address
800 function protocol.netmask(self)
801 local addrs = self:_ubus("ipv4-address")
802 return addrs and #addrs > 0 and
803 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
806 function protocol.gwaddr(self)
808 for _, route in ipairs(self:_ubus("route") or { }) do
809 if route.target == "0.0.0.0" and route.mask == 0 then
815 function protocol.dnsaddrs(self)
818 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
819 if not addr:match(":") then
826 function protocol.ip6addr(self)
827 local addrs = self:_ubus("ipv6-address")
828 if addrs and #addrs > 0 then
829 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
831 addrs = self:_ubus("ipv6-prefix-assignment")
832 if addrs and #addrs > 0 then
833 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
838 function protocol.gw6addr(self)
840 for _, route in ipairs(self:_ubus("route") or { }) do
841 if route.target == "::" and route.mask == 0 then
842 return ipc.IPv6(route.nexthop):string()
847 function protocol.dns6addrs(self)
850 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
851 if addr:match(":") then
858 function protocol.is_bridge(self)
859 return (not self:is_virtual() and self:type() == "bridge")
862 function protocol.opkg_package(self)
866 function protocol.is_installed(self)
870 function protocol.is_virtual(self)
874 function protocol.is_floating(self)
878 function protocol.is_empty(self)
879 if self:is_floating() then
884 if (self:_get("ifname") or ""):match("%S+") then
888 _uci_real:foreach("wireless", "wifi-iface",
891 for n in utl.imatch(s.network) do
892 if n == self.sid then
903 function protocol.add_interface(self, ifname)
904 ifname = _M:ifnameof(ifname)
905 if ifname and not self:is_floating() then
906 -- if its a wifi interface, change its network option
907 local wif = _wifi_lookup(ifname)
909 _append("wireless", wif, "network", self.sid)
911 -- add iface to our iface list
913 _append("network", self.sid, "ifname", ifname)
918 function protocol.del_interface(self, ifname)
919 ifname = _M:ifnameof(ifname)
920 if ifname and not self:is_floating() then
921 -- if its a wireless interface, clear its network option
922 local wif = _wifi_lookup(ifname)
923 if wif then _filter("wireless", wif, "network", self.sid) end
925 -- remove the interface
926 _filter("network", self.sid, "ifname", ifname)
930 function protocol.get_interface(self)
931 if self:is_virtual() then
932 _tunnel[self:proto() .. "-" .. self.sid] = true
933 return interface(self:proto() .. "-" .. self.sid, self)
934 elseif self:is_bridge() then
935 _bridge["br-" .. self.sid] = true
936 return interface("br-" .. self.sid, self)
940 for ifn in utl.imatch(_uci_real:get("network", self.sid, "ifname")) do
941 ifn = ifn:match("^[^:/]+")
942 return ifn and interface(ifn, self)
945 _uci_real:foreach("wireless", "wifi-iface",
948 num[s.device] = num[s.device] and num[s.device] + 1 or 1
951 for net in utl.imatch(s.network) do
952 if net == self.sid then
953 ifn = "%s.network%d" %{ s.device, num[s.device] }
959 return ifn and interface(ifn, self)
963 function protocol.get_interfaces(self)
964 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
969 for ifn in utl.imatch(self:get("ifname")) do
970 ifn = ifn:match("^[^:/]+")
971 nfs[ifn] = interface(ifn, self)
974 for ifn in utl.kspairs(nfs) do
975 ifaces[#ifaces+1] = nfs[ifn]
980 _uci_real:foreach("wireless", "wifi-iface",
983 num[s.device] = num[s.device] and num[s.device] + 1 or 1
986 for net in utl.imatch(s.network) do
987 if net == self.sid then
988 ifn = "%s.network%d" %{ s.device, num[s.device] }
989 wfs[ifn] = interface(ifn, self)
995 for ifn in utl.kspairs(wfs) do
996 ifaces[#ifaces+1] = wfs[ifn]
1003 function protocol.contains_interface(self, ifname)
1004 ifname = _M:ifnameof(ifname)
1007 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1009 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1013 for ifn in utl.imatch(self:get("ifname")) do
1014 ifn = ifn:match("[^:]+")
1015 if ifn == ifname then
1020 local wif = _wifi_lookup(ifname)
1023 for n in utl.imatch(_uci_real:get("wireless", wif, "network")) do
1024 if n == self.sid then
1034 function protocol.adminlink(self)
1035 return dsp.build_url("admin", "network", "network", self.sid)
1039 interface = utl.class()
1041 function interface.__init__(self, ifname, network)
1042 local wif = _wifi_lookup(ifname)
1044 self.wif = wifinet(wif)
1045 self.ifname = _wifi_state("section", wif, "ifname")
1048 self.ifname = self.ifname or ifname
1049 self.dev = _interfaces[self.ifname]
1050 self.network = network
1053 function interface._ubus(self, field)
1054 if not _ubusdevcache[self.ifname] then
1055 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1056 { name = self.ifname })
1058 if _ubusdevcache[self.ifname] and field then
1059 return _ubusdevcache[self.ifname][field]
1061 return _ubusdevcache[self.ifname]
1064 function interface.name(self)
1065 return self.wif and self.wif:ifname() or self.ifname
1068 function interface.mac(self)
1069 return (self:_ubus("macaddr") or "00:00:00:00:00:00"):upper()
1072 function interface.ipaddrs(self)
1073 return self.dev and self.dev.ipaddrs or { }
1076 function interface.ip6addrs(self)
1077 return self.dev and self.dev.ip6addrs or { }
1080 function interface.type(self)
1081 if self.wif or _wifi_iface(self.ifname) then
1083 elseif _bridge[self.ifname] then
1085 elseif _tunnel[self.ifname] then
1087 elseif self.ifname:match("%.") then
1089 elseif _switch[self.ifname] then
1096 function interface.shortname(self)
1099 self.wif:active_mode(),
1100 self.wif:active_ssid() or self.wif:active_bssid()
1107 function interface.get_i18n(self)
1109 return "%s: %s %q" %{
1110 lng.translate("Wireless Network"),
1111 self.wif:active_mode(),
1112 self.wif:active_ssid() or self.wif:active_bssid()
1115 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1119 function interface.get_type_i18n(self)
1120 local x = self:type()
1122 return lng.translate("Wireless Adapter")
1123 elseif x == "bridge" then
1124 return lng.translate("Bridge")
1125 elseif x == "switch" then
1126 return lng.translate("Ethernet Switch")
1127 elseif x == "vlan" then
1128 return lng.translate("VLAN Interface")
1129 elseif x == "tunnel" then
1130 return lng.translate("Tunnel Interface")
1132 return lng.translate("Ethernet Adapter")
1136 function interface.adminlink(self)
1138 return self.wif:adminlink()
1142 function interface.ports(self)
1143 local members = self:_ubus("bridge-members")
1147 for _, iface in ipairs(members) do
1148 ifaces[#ifaces+1] = interface(iface)
1153 function interface.bridge_id(self)
1161 function interface.bridge_stp(self)
1169 function interface.is_up(self)
1170 return self:_ubus("up") or false
1173 function interface.is_bridge(self)
1174 return (self:type() == "bridge")
1177 function interface.is_bridgeport(self)
1178 return self.dev and self.dev.bridge and true or false
1181 function interface.tx_bytes(self)
1182 local stat = self:_ubus("statistics")
1183 return stat and stat.tx_bytes or 0
1186 function interface.rx_bytes(self)
1187 local stat = self:_ubus("statistics")
1188 return stat and stat.rx_bytes or 0
1191 function interface.tx_packets(self)
1192 local stat = self:_ubus("statistics")
1193 return stat and stat.tx_packets or 0
1196 function interface.rx_packets(self)
1197 local stat = self:_ubus("statistics")
1198 return stat and stat.rx_packets or 0
1201 function interface.get_network(self)
1202 return self:get_networks()[1]
1205 function interface.get_networks(self)
1206 if not self.networks then
1209 for _, net in ipairs(_M:get_networks()) do
1210 if net:contains_interface(self.ifname) or
1211 net:ifname() == self.ifname
1216 table.sort(nets, function(a, b) return a.sid < b.sid end)
1217 self.networks = nets
1220 return self.networks
1224 function interface.get_wifinet(self)
1229 wifidev = utl.class()
1231 function wifidev.__init__(self, dev)
1233 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1236 function wifidev.get(self, opt)
1237 return _get("wireless", self.sid, opt)
1240 function wifidev.set(self, opt, val)
1241 return _set("wireless", self.sid, opt, val)
1244 function wifidev.name(self)
1248 function wifidev.hwmodes(self)
1249 local l = self.iwinfo.hwmodelist
1250 if l and next(l) then
1253 return { b = true, g = true }
1257 function wifidev.get_i18n(self)
1259 if self.iwinfo.type == "wl" then
1261 elseif self.iwinfo.type == "madwifi" then
1266 local l = self:hwmodes()
1267 if l.a then m = m .. "a" end
1268 if l.b then m = m .. "b" end
1269 if l.g then m = m .. "g" end
1270 if l.n then m = m .. "n" end
1271 if l.ac then m = "ac" end
1273 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1276 function wifidev.is_up(self)
1277 if _ubuswificache[self.sid] then
1278 return (_ubuswificache[self.sid].up == true)
1282 _uci_state:foreach("wireless", "wifi-iface",
1284 if s.device == self.sid then
1295 function wifidev.get_wifinet(self, net)
1296 if _uci_real:get("wireless", net) == "wifi-iface" then
1299 local wnet = _wifi_lookup(net)
1301 return wifinet(wnet)
1306 function wifidev.get_wifinets(self)
1309 _uci_real:foreach("wireless", "wifi-iface",
1311 if s.device == self.sid then
1312 nets[#nets+1] = wifinet(s['.name'])
1319 function wifidev.add_wifinet(self, options)
1320 options = options or { }
1321 options.device = self.sid
1323 local wnet = _uci_real:section("wireless", "wifi-iface", nil, options)
1325 return wifinet(wnet, options)
1329 function wifidev.del_wifinet(self, net)
1330 if utl.instanceof(net, wifinet) then
1332 elseif _uci_real:get("wireless", net) ~= "wifi-iface" then
1333 net = _wifi_lookup(net)
1336 if net and _uci_real:get("wireless", net, "device") == self.sid then
1337 _uci_real:delete("wireless", net)
1345 wifinet = utl.class()
1347 function wifinet.__init__(self, net, data)
1352 _uci_real:foreach("wireless", "wifi-iface",
1355 num[s.device] = num[s.device] and num[s.device] + 1 or 1
1356 if s['.name'] == self.sid then
1357 netid = "%s.network%d" %{ s.device, num[s.device] }
1363 local dev = _wifi_state("section", self.sid, "ifname") or netid
1367 self.iwinfo = dev and sys.wifi.getiwinfo(dev) or { }
1368 self.iwdata = data or _uci_state:get_all("wireless", self.sid) or
1369 _uci_real:get_all("wireless", self.sid) or { }
1372 function wifinet.get(self, opt)
1373 return _get("wireless", self.sid, opt)
1376 function wifinet.set(self, opt, val)
1377 return _set("wireless", self.sid, opt, val)
1380 function wifinet.mode(self)
1381 return _uci_state:get("wireless", self.sid, "mode") or "ap"
1384 function wifinet.ssid(self)
1385 return _uci_state:get("wireless", self.sid, "ssid")
1388 function wifinet.bssid(self)
1389 return _uci_state:get("wireless", self.sid, "bssid")
1392 function wifinet.network(self)
1393 return _uci_state:get("wifinet", self.sid, "network")
1396 function wifinet.id(self)
1400 function wifinet.name(self)
1404 function wifinet.ifname(self)
1405 local ifname = self.iwinfo.ifname
1406 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1412 function wifinet.get_device(self)
1413 if self.iwdata.device then
1414 return wifidev(self.iwdata.device)
1418 function wifinet.is_up(self)
1419 local ifc = self:get_interface()
1420 return (ifc and ifc:is_up() or false)
1423 function wifinet.active_mode(self)
1424 local m = _stror(self.iwinfo.mode, self.iwdata.mode) or "ap"
1426 if m == "ap" then m = "Master"
1427 elseif m == "sta" then m = "Client"
1428 elseif m == "adhoc" then m = "Ad-Hoc"
1429 elseif m == "mesh" then m = "Mesh"
1430 elseif m == "monitor" then m = "Monitor"
1436 function wifinet.active_mode_i18n(self)
1437 return lng.translate(self:active_mode())
1440 function wifinet.active_ssid(self)
1441 return _stror(self.iwinfo.ssid, self.iwdata.ssid)
1444 function wifinet.active_bssid(self)
1445 return _stror(self.iwinfo.bssid, self.iwdata.bssid) or "00:00:00:00:00:00"
1448 function wifinet.active_encryption(self)
1449 local enc = self.iwinfo and self.iwinfo.encryption
1450 return enc and enc.description or "-"
1453 function wifinet.assoclist(self)
1454 return self.iwinfo.assoclist or { }
1457 function wifinet.frequency(self)
1458 local freq = self.iwinfo.frequency
1459 if freq and freq > 0 then
1460 return "%.03f" % (freq / 1000)
1464 function wifinet.bitrate(self)
1465 local rate = self.iwinfo.bitrate
1466 if rate and rate > 0 then
1467 return (rate / 1000)
1471 function wifinet.channel(self)
1472 return self.iwinfo.channel or
1473 tonumber(_uci_state:get("wireless", self.iwdata.device, "channel"))
1476 function wifinet.signal(self)
1477 return self.iwinfo.signal or 0
1480 function wifinet.noise(self)
1481 return self.iwinfo.noise or 0
1484 function wifinet.country(self)
1485 return self.iwinfo.country or "00"
1488 function wifinet.txpower(self)
1489 local pwr = (self.iwinfo.txpower or 0)
1490 return pwr + self:txpower_offset()
1493 function wifinet.txpower_offset(self)
1494 return self.iwinfo.txpower_offset or 0
1497 function wifinet.signal_level(self, s, n)
1498 if self:active_bssid() ~= "00:00:00:00:00:00" then
1499 local signal = s or self:signal()
1500 local noise = n or self:noise()
1502 if signal < 0 and noise < 0 then
1503 local snr = -1 * (noise - signal)
1504 return math.floor(snr / 5)
1513 function wifinet.signal_percent(self)
1514 local qc = self.iwinfo.quality or 0
1515 local qm = self.iwinfo.quality_max or 0
1517 if qc > 0 and qm > 0 then
1518 return math.floor((100 / qm) * qc)
1524 function wifinet.shortname(self)
1526 lng.translate(self:active_mode()),
1527 self:active_ssid() or self:active_bssid()
1531 function wifinet.get_i18n(self)
1532 return "%s: %s %q (%s)" %{
1533 lng.translate("Wireless Network"),
1534 lng.translate(self:active_mode()),
1535 self:active_ssid() or self:active_bssid(),
1540 function wifinet.adminlink(self)
1541 return dsp.build_url("admin", "network", "wireless", self.netid)
1544 function wifinet.get_network(self)
1545 return self:get_networks()[1]
1548 function wifinet.get_networks(self)
1551 for net in utl.imatch(tostring(self.iwdata.network)) do
1552 if _uci_real:get("network", net) == "interface" then
1553 nets[#nets+1] = network(net)
1556 table.sort(nets, function(a, b) return a.sid < b.sid end)
1560 function wifinet.get_interface(self)
1561 return interface(self:ifname())
1565 -- setup base protocols
1566 _M:register_protocol("static")
1567 _M:register_protocol("dhcp")
1568 _M:register_protocol("none")
1570 -- load protocol extensions
1571 local exts = nfs.dir(utl.libpath() .. "/model/network")
1575 if ext:match("%.lua$") then
1576 require("luci.model.network." .. ext:gsub("%.lua$", ""))