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 pcall, require, setmetatable = pcall, require, setmetatable
11 local nxo = require "nixio"
12 local nfs = require "nixio.fs"
13 local ipc = require "luci.ip"
14 local utl = require "luci.util"
15 local uci = require "luci.model.uci"
16 local lng = require "luci.i18n"
17 local jsc = require "luci.jsonc"
19 module "luci.model.network"
22 IFACE_PATTERNS_VIRTUAL = { }
23 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$" }
24 IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
27 CONNECT_FAILED = lng.translate("Connection attempt failed"),
28 INVALID_ADDRESS = lng.translate("IP address in invalid"),
29 INVALID_GATEWAY = lng.translate("Gateway address is invalid"),
30 INVALID_LOCAL_ADDRESS = lng.translate("Local IP address is invalid"),
31 MISSING_ADDRESS = lng.translate("IP address is missing"),
32 MISSING_PEER_ADDRESS = lng.translate("Peer address is missing"),
33 NO_DEVICE = lng.translate("Network device is not present"),
34 NO_IFACE = lng.translate("Unable to determine device name"),
35 NO_IFNAME = lng.translate("Unable to determine device name"),
36 NO_WAN_ADDRESS = lng.translate("Unable to determine external IP address"),
37 NO_WAN_LINK = lng.translate("Unable to determine upstream interface"),
38 PEER_RESOLVE_FAIL = lng.translate("Unable to resolve peer host name"),
39 PIN_FAILED = lng.translate("PIN code rejected")
43 protocol = utl.class()
45 local _protocols = { }
47 local _interfaces, _bridge, _switch, _tunnel, _swtopo
48 local _ubusnetcache, _ubusdevcache, _ubuswificache
51 function _filter(c, s, o, r)
52 local val = _uci:get(c, s, o)
55 if type(val) == "string" then
56 for val in val:gmatch("%S+") do
62 _uci:set(c, s, o, table.concat(l, " "))
66 elseif type(val) == "table" then
67 for _, val in ipairs(val) do
81 function _append(c, s, o, a)
82 local val = _uci:get(c, s, o) or ""
83 if type(val) == "string" then
85 for val in val:gmatch("%S+") do
91 _uci:set(c, s, o, table.concat(l, " "))
92 elseif type(val) == "table" then
94 for _, val in ipairs(val) do
104 function _stror(s1, s2)
105 if not s1 or #s1 == 0 then
106 return s2 and #s2 > 0 and s2
112 function _get(c, s, o)
113 return _uci:get(c, s, o)
116 function _set(c, s, o, v)
118 if type(v) == "boolean" then v = v and "1" or "0" end
119 return _uci:set(c, s, o, v)
121 return _uci:delete(c, s, o)
125 local function _wifi_state()
126 if not next(_ubuswificache) then
127 _ubuswificache = utl.ubus("network.wireless", "status", {}) or {}
129 return _ubuswificache
132 local function _wifi_state_by_sid(sid)
133 local t1, n1 = _uci:get("wireless", sid)
134 if t1 == "wifi-iface" and n1 ~= nil then
135 local radioname, radiostate
136 for radioname, radiostate in pairs(_wifi_state()) do
137 if type(radiostate) == "table" and
138 type(radiostate.interfaces) == "table"
140 local netidx, netstate
141 for netidx, netstate in ipairs(radiostate.interfaces) do
142 if type(netstate) == "table" and
143 type(netstate.section) == "string"
145 local t2, n2 = _uci:get("wireless", netstate.section)
146 if t1 == t2 and n1 == n2 then
147 return radioname, radiostate, netstate
156 local function _wifi_state_by_ifname(ifname)
157 if type(ifname) == "string" then
158 local radioname, radiostate
159 for radioname, radiostate in pairs(_wifi_state()) do
160 if type(radiostate) == "table" and
161 type(radiostate.interfaces) == "table"
163 local netidx, netstate
164 for netidx, netstate in ipairs(radiostate.interfaces) do
165 if type(netstate) == "table" and
166 type(netstate.ifname) == "string" and
167 netstate.ifname == ifname
169 return radioname, radiostate, netstate
177 function _wifi_iface(x)
179 for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
187 local function _wifi_iwinfo_by_ifname(ifname, force_phy_only)
188 local stat, iwinfo = pcall(require, "iwinfo")
189 local iwtype = stat and type(ifname) == "string" and iwinfo.type(ifname)
190 local is_nonphy_op = {
202 -- if we got a type but no real netdev, we're referring to a phy
203 local phy_only = force_phy_only or (ipc.link(ifname).type ~= 1)
205 return setmetatable({}, {
206 __index = function(t, k)
207 if k == "ifname" then
209 elseif phy_only and is_nonphy_op[k] then
211 elseif iwinfo[iwtype][k] then
212 return iwinfo[iwtype][k](ifname)
219 local function _wifi_sid_by_netid(netid)
220 if type(netid) == "string" then
221 local radioname, netidx = netid:match("^(%w+)%.network(%d+)$")
222 if radioname and netidx then
225 netidx = tonumber(netidx)
226 _uci:foreach("wireless", "wifi-iface",
228 if s.device == radioname then
242 function _wifi_sid_by_ifname(ifn)
243 local sid = _wifi_sid_by_netid(ifn)
248 local _, _, netstate = _wifi_state_by_ifname(ifn)
249 if netstate and type(netstate.section) == "string" then
250 return netstate.section
254 local function _wifi_netid_by_sid(sid)
255 local t, n = _uci:get("wireless", sid)
256 if t == "wifi-iface" and n ~= nil then
257 local radioname = _uci:get("wireless", n, "device")
258 if type(radioname) == "string" then
259 local i, netid = 0, nil
261 _uci:foreach("wireless", "wifi-iface",
263 if s.device == radioname then
265 if s[".name"] == n then
266 netid = "%s.network%d" %{ radioname, i }
272 return netid, radioname
277 local function _wifi_netid_by_netname(name)
280 _uci:foreach("wireless", "wifi-iface",
283 for net in utl.imatch(s.network) do
285 netid = _wifi_netid_by_sid(s[".name"])
294 function _iface_virtual(x)
296 for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
304 function _iface_ignore(x)
306 for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
314 function init(cursor)
315 _uci = cursor or _uci or uci.cursor()
327 -- read interface information
329 for n, i in ipairs(nxo.getifaddrs()) do
330 local name = i.name:match("[^:]+")
332 if _iface_virtual(name) then
336 if _tunnel[name] or not (_iface_ignore(name) or _iface_virtual(name)) then
337 _interfaces[name] = _interfaces[name] or {
338 idx = i.ifindex or n,
346 if i.family == "packet" then
347 _interfaces[name].flags = i.flags
348 _interfaces[name].stats = i.data
349 _interfaces[name].macaddr = ipc.checkmac(i.addr)
350 elseif i.family == "inet" then
351 _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
352 elseif i.family == "inet6" then
353 _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
358 -- read bridge informaton
360 for l in utl.execi("brctl show") do
361 if not l:match("STP") then
362 local r = utl.split(l, "%s+", nil, true)
368 ifnames = { _interfaces[r[4]] }
371 b.ifnames[1].bridge = b
375 b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
376 b.ifnames[#b.ifnames].bridge = b
381 -- read switch topology
382 local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
383 if type(boardinfo) == "table" and type(boardinfo.switch) == "table" then
385 for switch, layout in pairs(boardinfo.switch) do
386 if type(layout) == "table" and type(layout.ports) == "table" then
392 for _, port in ipairs(layout.ports) do
393 if type(port) == "table" and
394 type(port.num) == "number" and
395 (type(port.role) == "string" or
396 type(port.device) == "string")
400 role = port.role or "cpu",
401 index = port.index or port.num
405 spec.device = port.device
406 spec.tagged = port.need_tag
407 netdevs[tostring(port.num)] = port.device
410 ports[#ports+1] = spec
413 nports[port.role] = (nports[port.role] or 0) + 1
418 table.sort(ports, function(a, b)
419 if a.role ~= b.role then
420 return (a.role < b.role)
423 return (a.index < b.index)
427 for _, port in ipairs(ports) do
428 if port.role ~= role then
433 if role == "cpu" then
434 port.label = "CPU (%s)" % port.device
435 elseif nports[role] > 1 then
436 port.label = "%s %d" %{ role:upper(), pnum }
439 port.label = role:upper()
457 function save(self, ...)
462 function commit(self, ...)
467 function ifnameof(self, x)
468 if utl.instanceof(x, interface) then
470 elseif utl.instanceof(x, protocol) then
472 elseif type(x) == "string" then
473 return x:match("^[^:]+")
477 function get_protocol(self, protoname, netname)
478 local v = _protocols[protoname]
480 return v(netname or "__dummy__")
484 function get_protocols(self)
487 for _, v in ipairs(_protocols) do
488 p[#p+1] = v("__dummy__")
493 function register_protocol(self, protoname)
494 local proto = utl.class(protocol)
496 function proto.__init__(self, name)
500 function proto.proto(self)
504 _protocols[#_protocols+1] = proto
505 _protocols[protoname] = proto
510 function register_pattern_virtual(self, pat)
511 IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
514 function register_error_code(self, code, message)
515 if type(code) == "string" and
516 type(message) == "string" and
517 not IFACE_ERRORS[code]
519 IFACE_ERRORS[code] = message
526 function has_ipv6(self)
527 return nfs.access("/proc/net/ipv6_route")
530 function add_network(self, n, options)
531 local oldnet = self:get_network(n)
532 if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
533 if _uci:section("network", "interface", n, options) then
536 elseif oldnet and oldnet:is_empty() then
539 for k, v in pairs(options) do
547 function get_network(self, n)
548 if n and _uci:get("network", n) == "interface" then
551 local stat = utl.ubus("network.interface", "status", { interface = n })
552 if type(stat) == "table" and
553 type(stat.proto) == "string"
555 return network(n, stat.proto)
560 function get_networks(self)
564 _uci:foreach("network", "interface",
566 nls[s['.name']] = network(s['.name'])
569 local dump = utl.ubus("network.interface", "dump", { })
570 if type(dump) == "table" and
571 type(dump.interface) == "table"
574 for _, net in ipairs(dump.interface) do
575 if type(net) == "table" and
576 type(net.proto) == "string" and
577 type(net.interface) == "string"
579 if not nls[net.interface] then
580 nls[net.interface] = network(net.interface, net.proto)
587 for n in utl.kspairs(nls) do
588 nets[#nets+1] = nls[n]
594 function del_network(self, n)
595 local r = _uci:delete("network", n)
597 _uci:delete_all("luci", "ifstate",
598 function(s) return (s.interface == n) end)
600 _uci:delete_all("network", "alias",
601 function(s) return (s.interface == n) end)
603 _uci:delete_all("network", "route",
604 function(s) return (s.interface == n) end)
606 _uci:delete_all("network", "route6",
607 function(s) return (s.interface == n) end)
609 _uci:foreach("wireless", "wifi-iface",
613 for net in utl.imatch(s.network) do
619 _uci:set("wireless", s['.name'], "network",
620 table.concat(rest, " "))
622 _uci:delete("wireless", s['.name'], "network")
629 function rename_network(self, old, new)
631 if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
632 r = _uci:section("network", "interface", new, _uci:get_all("network", old))
635 _uci:foreach("network", "alias",
637 if s.interface == old then
638 _uci:set("network", s['.name'], "interface", new)
642 _uci:foreach("network", "route",
644 if s.interface == old then
645 _uci:set("network", s['.name'], "interface", new)
649 _uci:foreach("network", "route6",
651 if s.interface == old then
652 _uci:set("network", s['.name'], "interface", new)
656 _uci:foreach("wireless", "wifi-iface",
660 for net in utl.imatch(s.network) do
668 _uci:set("wireless", s['.name'], "network",
669 table.concat(list, " "))
673 _uci:delete("network", old)
679 function get_interface(self, i)
680 if _interfaces[i] or _wifi_iface(i) then
683 local netid = _wifi_netid_by_sid(i)
684 return netid and interface(netid)
688 function get_interfaces(self)
693 -- find normal interfaces
694 _uci:foreach("network", "interface",
696 for iface in utl.imatch(s.ifname) do
697 if not _iface_ignore(iface) and not _iface_virtual(iface) and not _wifi_iface(iface) then
698 nfs[iface] = interface(iface)
703 for iface in utl.kspairs(_interfaces) do
704 if not (nfs[iface] or _iface_ignore(iface) or _iface_virtual(iface) or _wifi_iface(iface)) then
705 nfs[iface] = interface(iface)
709 -- find vlan interfaces
710 _uci:foreach("network", "switch_vlan",
712 if type(s.ports) ~= "string" or
713 type(s.device) ~= "string" or
714 type(_swtopo[s.device]) ~= "table"
720 for pnum, ptag in s.ports:gmatch("(%d+)([tu]?)") do
721 local netdev = _swtopo[s.device].netdevs[pnum]
723 if not nfs[netdev] then
724 nfs[netdev] = interface(netdev)
726 _switch[netdev] = true
729 local vid = tonumber(s.vid or s.vlan)
730 if vid ~= nil and vid >= 0 and vid <= 4095 then
731 local iface = "%s.%d" %{ netdev, vid }
732 if not nfs[iface] then
733 nfs[iface] = interface(iface)
735 _switch[iface] = true
742 for iface in utl.kspairs(nfs) do
743 ifaces[#ifaces+1] = nfs[iface]
746 -- find wifi interfaces
749 _uci:foreach("wireless", "wifi-iface",
752 num[s.device] = num[s.device] and num[s.device] + 1 or 1
753 local i = "%s.network%d" %{ s.device, num[s.device] }
754 wfs[i] = interface(i)
758 for iface in utl.kspairs(wfs) do
759 ifaces[#ifaces+1] = wfs[iface]
765 function ignore_interface(self, x)
766 return _iface_ignore(x)
769 function get_wifidev(self, dev)
770 if _uci:get("wireless", dev) == "wifi-device" then
775 function get_wifidevs(self)
779 _uci:foreach("wireless", "wifi-device",
780 function(s) wfd[#wfd+1] = s['.name'] end)
783 for _, dev in utl.vspairs(wfd) do
784 devs[#devs+1] = wifidev(dev)
790 function get_wifinet(self, net)
791 local wnet = _wifi_sid_by_ifname(net)
797 function add_wifinet(self, net, options)
798 if type(options) == "table" and options.device and
799 _uci:get("wireless", options.device) == "wifi-device"
801 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
806 function del_wifinet(self, net)
807 local wnet = _wifi_sid_by_ifname(net)
809 _uci:delete("wireless", wnet)
815 function get_status_by_route(self, addr, mask)
816 local route_statuses = { }
818 for _, object in ipairs(utl.ubus()) do
819 local net = object:match("^network%.interface%.(.+)")
821 local s = utl.ubus(object, "status", {})
822 if s and s.route then
824 for _, rt in ipairs(s.route) do
825 if not rt.table and rt.target == addr and rt.mask == mask then
826 route_statuses[net] = s
833 return route_statuses
836 function get_status_by_address(self, addr)
838 for _, object in ipairs(utl.ubus()) do
839 local net = object:match("^network%.interface%.(.+)")
841 local s = utl.ubus(object, "status", {})
842 if s and s['ipv4-address'] then
844 for _, a in ipairs(s['ipv4-address']) do
845 if a.address == addr then
850 if s and s['ipv6-address'] then
852 for _, a in ipairs(s['ipv6-address']) do
853 if a.address == addr then
858 if s and s['ipv6-prefix-assignment'] then
860 for _, a in ipairs(s['ipv6-prefix-assignment']) do
861 if a and a['local-address'] and a['local-address'].address == addr then
870 function get_wan_networks(self)
873 local route_statuses = self:get_status_by_route("0.0.0.0", 0)
875 for k, v in pairs(route_statuses) do
876 wan_nets[#wan_nets+1] = network(k, v.proto)
882 function get_wan6_networks(self)
884 local wan6_nets = { }
885 local route_statuses = self:get_status_by_route("::", 0)
887 for k, v in pairs(route_statuses) do
888 wan6_nets[#wan6_nets+1] = network(k, v.proto)
894 function get_switch_topologies(self)
899 function network(name, proto)
901 local p = proto or _uci:get("network", name, "proto")
902 local c = p and _protocols[p] or protocol
907 function protocol.__init__(self, name)
911 function protocol._get(self, opt)
912 local v = _uci:get("network", self.sid, opt)
913 if type(v) == "table" then
914 return table.concat(v, " ")
919 function protocol._ubus(self, field)
920 if not _ubusnetcache[self.sid] then
921 _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
924 if _ubusnetcache[self.sid] and field then
925 return _ubusnetcache[self.sid][field]
927 return _ubusnetcache[self.sid]
930 function protocol.get(self, opt)
931 return _get("network", self.sid, opt)
934 function protocol.set(self, opt, val)
935 return _set("network", self.sid, opt, val)
938 function protocol.ifname(self)
940 if self:is_floating() then
941 ifname = self:_ubus("l3_device")
943 ifname = self:_ubus("device")
946 ifname = _wifi_netid_by_netname(self.sid)
951 function protocol.proto(self)
955 function protocol.get_i18n(self)
956 local p = self:proto()
958 return lng.translate("Unmanaged")
959 elseif p == "static" then
960 return lng.translate("Static address")
961 elseif p == "dhcp" then
962 return lng.translate("DHCP client")
964 return lng.translate("Unknown")
968 function protocol.type(self)
969 return self:_get("type")
972 function protocol.name(self)
976 function protocol.uptime(self)
977 return self:_ubus("uptime") or 0
980 function protocol.expires(self)
981 local u = self:_ubus("uptime")
982 local d = self:_ubus("data")
984 if type(u) == "number" and type(d) == "table" and
985 type(d.leasetime) == "number"
987 local r = (d.leasetime - (u % d.leasetime))
988 return r > 0 and r or 0
994 function protocol.metric(self)
995 return self:_ubus("metric") or 0
998 function protocol.zonename(self)
999 local d = self:_ubus("data")
1001 if type(d) == "table" and type(d.zone) == "string" then
1008 function protocol.ipaddr(self)
1009 local addrs = self:_ubus("ipv4-address")
1010 return addrs and #addrs > 0 and addrs[1].address
1013 function protocol.ipaddrs(self)
1014 local addrs = self:_ubus("ipv4-address")
1017 if type(addrs) == "table" then
1019 for n, addr in ipairs(addrs) do
1020 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
1027 function protocol.netmask(self)
1028 local addrs = self:_ubus("ipv4-address")
1029 return addrs and #addrs > 0 and
1030 ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
1033 function protocol.gwaddr(self)
1035 for _, route in ipairs(self:_ubus("route") or { }) do
1036 if route.target == "0.0.0.0" and route.mask == 0 then
1037 return route.nexthop
1042 function protocol.dnsaddrs(self)
1045 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
1046 if not addr:match(":") then
1053 function protocol.ip6addr(self)
1054 local addrs = self:_ubus("ipv6-address")
1055 if addrs and #addrs > 0 then
1056 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
1058 addrs = self:_ubus("ipv6-prefix-assignment")
1059 if addrs and #addrs > 0 then
1060 return "%s/%d" %{ addrs[1].address, addrs[1].mask }
1065 function protocol.ip6addrs(self)
1066 local addrs = self:_ubus("ipv6-address")
1070 if type(addrs) == "table" then
1071 for n, addr in ipairs(addrs) do
1072 rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
1076 addrs = self:_ubus("ipv6-prefix-assignment")
1078 if type(addrs) == "table" then
1079 for n, addr in ipairs(addrs) do
1080 if type(addr["local-address"]) == "table" and
1081 type(addr["local-address"].mask) == "number" and
1082 type(addr["local-address"].address) == "string"
1084 rv[#rv+1] = "%s/%d" %{
1085 addr["local-address"].address,
1086 addr["local-address"].mask
1095 function protocol.gw6addr(self)
1097 for _, route in ipairs(self:_ubus("route") or { }) do
1098 if route.target == "::" and route.mask == 0 then
1099 return ipc.IPv6(route.nexthop):string()
1104 function protocol.dns6addrs(self)
1107 for _, addr in ipairs(self:_ubus("dns-server") or { }) do
1108 if addr:match(":") then
1115 function protocol.ip6prefix(self)
1116 local prefix = self:_ubus("ipv6-prefix")
1117 if prefix and #prefix > 0 then
1118 return "%s/%d" %{ prefix[1].address, prefix[1].mask }
1122 function protocol.errors(self)
1124 local errors = self:_ubus("errors")
1125 if type(errors) == "table" then
1126 for _, err in ipairs(errors) do
1127 if type(err) == "table" and
1128 type(err.code) == "string"
1131 rv[#rv+1] = IFACE_ERRORS[err.code] or lng.translatef("Unknown error (%s)", err.code)
1138 function protocol.is_bridge(self)
1139 return (not self:is_virtual() and self:type() == "bridge")
1142 function protocol.opkg_package(self)
1146 function protocol.is_installed(self)
1150 function protocol.is_virtual(self)
1154 function protocol.is_floating(self)
1158 function protocol.is_dynamic(self)
1159 return (self:_ubus("dynamic") == true)
1162 function protocol.is_auto(self)
1163 return (self:_get("auto") ~= "0")
1166 function protocol.is_alias(self)
1167 local ifn, parent = nil, nil
1169 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
1170 if #ifn > 1 and ifn:byte(1) == 64 then
1172 elseif parent ~= nil then
1180 function protocol.is_empty(self)
1181 if self:is_floating() then
1186 if (self:_get("ifname") or ""):match("%S+") then
1190 if empty and _wifi_netid_by_netname(self.sid) then
1198 function protocol.is_up(self)
1199 return (self:_ubus("up") == true)
1202 function protocol.add_interface(self, ifname)
1203 ifname = _M:ifnameof(ifname)
1204 if ifname and not self:is_floating() then
1205 -- if its a wifi interface, change its network option
1206 local wif = _wifi_sid_by_ifname(ifname)
1208 _append("wireless", wif, "network", self.sid)
1210 -- add iface to our iface list
1212 _append("network", self.sid, "ifname", ifname)
1217 function protocol.del_interface(self, ifname)
1218 ifname = _M:ifnameof(ifname)
1219 if ifname and not self:is_floating() then
1220 -- if its a wireless interface, clear its network option
1221 local wif = _wifi_sid_by_ifname(ifname)
1222 if wif then _filter("wireless", wif, "network", self.sid) end
1224 -- remove the interface
1225 _filter("network", self.sid, "ifname", ifname)
1229 function protocol.get_interface(self)
1230 if self:is_virtual() then
1231 _tunnel[self:proto() .. "-" .. self.sid] = true
1232 return interface(self:proto() .. "-" .. self.sid, self)
1233 elseif self:is_bridge() then
1234 _bridge["br-" .. self.sid] = true
1235 return interface("br-" .. self.sid, self)
1237 local ifn = self:_ubus("l3_device") or self:_ubus("device")
1239 return interface(ifn, self)
1242 for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
1243 ifn = ifn:match("^[^:/]+")
1244 return ifn and interface(ifn, self)
1247 ifn = _wifi_netid_by_netname(self.sid)
1248 return ifn and interface(ifn, self)
1252 function protocol.get_interfaces(self)
1253 if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
1258 for ifn in utl.imatch(self:get("ifname")) do
1259 ifn = ifn:match("^[^:/]+")
1260 nfs[ifn] = interface(ifn, self)
1263 for ifn in utl.kspairs(nfs) do
1264 ifaces[#ifaces+1] = nfs[ifn]
1268 _uci:foreach("wireless", "wifi-iface",
1272 for net in utl.imatch(s.network) do
1273 if net == self.sid then
1274 ifn = _wifi_netid_by_sid(s[".name"])
1276 wfs[ifn] = interface(ifn, self)
1283 for ifn in utl.kspairs(wfs) do
1284 ifaces[#ifaces+1] = wfs[ifn]
1291 function protocol.contains_interface(self, ifname)
1292 ifname = _M:ifnameof(ifname)
1295 elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
1297 elseif self:is_bridge() and "br-" .. self.sid == ifname then
1301 for ifn in utl.imatch(self:get("ifname")) do
1302 ifn = ifn:match("[^:]+")
1303 if ifn == ifname then
1308 local wif = _wifi_sid_by_ifname(ifname)
1311 for n in utl.imatch(_uci:get("wireless", wif, "network")) do
1312 if n == self.sid then
1322 function protocol.adminlink(self)
1323 local stat, dsp = pcall(require, "luci.dispatcher")
1324 return stat and dsp.build_url("admin", "network", "network", self.sid)
1328 interface = utl.class()
1330 function interface.__init__(self, ifname, network)
1331 local wif = _wifi_sid_by_ifname(ifname)
1333 self.wif = wifinet(wif)
1334 self.ifname = self.wif:ifname()
1337 self.ifname = self.ifname or ifname
1338 self.dev = _interfaces[self.ifname]
1339 self.network = network
1342 function interface._ubus(self, field)
1343 if not _ubusdevcache[self.ifname] then
1344 _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
1345 { name = self.ifname })
1347 if _ubusdevcache[self.ifname] and field then
1348 return _ubusdevcache[self.ifname][field]
1350 return _ubusdevcache[self.ifname]
1353 function interface.name(self)
1354 return self.wif and self.wif:ifname() or self.ifname
1357 function interface.mac(self)
1358 return ipc.checkmac(self:_ubus("macaddr"))
1361 function interface.ipaddrs(self)
1362 return self.dev and self.dev.ipaddrs or { }
1365 function interface.ip6addrs(self)
1366 return self.dev and self.dev.ip6addrs or { }
1369 function interface.type(self)
1370 if self.ifname and self.ifname:byte(1) == 64 then
1372 elseif self.wif or _wifi_iface(self.ifname) then
1374 elseif _bridge[self.ifname] then
1376 elseif _tunnel[self.ifname] then
1378 elseif self.ifname:match("%.") then
1380 elseif _switch[self.ifname] then
1387 function interface.shortname(self)
1389 return self.wif:shortname()
1395 function interface.get_i18n(self)
1397 return "%s: %s %q" %{
1398 lng.translate("Wireless Network"),
1399 self.wif:active_mode(),
1400 self.wif:active_ssid() or self.wif:active_bssid() or self.wif:id() or "?"
1403 return "%s: %q" %{ self:get_type_i18n(), self:name() }
1407 function interface.get_type_i18n(self)
1408 local x = self:type()
1409 if x == "alias" then
1410 return lng.translate("Alias Interface")
1411 elseif x == "wifi" then
1412 return lng.translate("Wireless Adapter")
1413 elseif x == "bridge" then
1414 return lng.translate("Bridge")
1415 elseif x == "switch" then
1416 return lng.translate("Ethernet Switch")
1417 elseif x == "vlan" then
1418 if _switch[self.ifname] then
1419 return lng.translate("Switch VLAN")
1421 return lng.translate("Software VLAN")
1423 elseif x == "tunnel" then
1424 return lng.translate("Tunnel Interface")
1426 return lng.translate("Ethernet Adapter")
1430 function interface.adminlink(self)
1432 return self.wif:adminlink()
1436 function interface.ports(self)
1437 local members = self:_ubus("bridge-members")
1441 for _, iface in ipairs(members) do
1442 ifaces[#ifaces+1] = interface(iface)
1447 function interface.bridge_id(self)
1455 function interface.bridge_stp(self)
1463 function interface.is_up(self)
1464 local up = self:_ubus("up")
1466 up = (self:type() == "alias")
1471 function interface.is_bridge(self)
1472 return (self:type() == "bridge")
1475 function interface.is_bridgeport(self)
1476 return self.dev and self.dev.bridge and true or false
1479 function interface.tx_bytes(self)
1480 local stat = self:_ubus("statistics")
1481 return stat and stat.tx_bytes or 0
1484 function interface.rx_bytes(self)
1485 local stat = self:_ubus("statistics")
1486 return stat and stat.rx_bytes or 0
1489 function interface.tx_packets(self)
1490 local stat = self:_ubus("statistics")
1491 return stat and stat.tx_packets or 0
1494 function interface.rx_packets(self)
1495 local stat = self:_ubus("statistics")
1496 return stat and stat.rx_packets or 0
1499 function interface.get_network(self)
1500 return self:get_networks()[1]
1503 function interface.get_networks(self)
1504 if not self.networks then
1507 for _, net in ipairs(_M:get_networks()) do
1508 if net:contains_interface(self.ifname) or
1509 net:ifname() == self.ifname
1514 table.sort(nets, function(a, b) return a.sid < b.sid end)
1515 self.networks = nets
1518 return self.networks
1522 function interface.get_wifinet(self)
1527 wifidev = utl.class()
1529 function wifidev.__init__(self, name)
1530 local t, n = _uci:get("wireless", name)
1531 if t == "wifi-device" and n ~= nil then
1533 self.iwinfo = _wifi_iwinfo_by_ifname(self.sid, true)
1535 self.sid = self.sid or name
1536 self.iwinfo = self.iwinfo or { ifname = self.sid }
1539 function wifidev.get(self, opt)
1540 return _get("wireless", self.sid, opt)
1543 function wifidev.set(self, opt, val)
1544 return _set("wireless", self.sid, opt, val)
1547 function wifidev.name(self)
1551 function wifidev.hwmodes(self)
1552 local l = self.iwinfo.hwmodelist
1553 if l and next(l) then
1556 return { b = true, g = true }
1560 function wifidev.get_i18n(self)
1561 local t = self.iwinfo.hardware_name or "Generic"
1562 if self.iwinfo.type == "wl" then
1567 local l = self:hwmodes()
1568 if l.a then m = m .. "a" end
1569 if l.b then m = m .. "b" end
1570 if l.g then m = m .. "g" end
1571 if l.n then m = m .. "n" end
1572 if l.ac then m = "ac" end
1574 return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
1577 function wifidev.is_up(self)
1578 if _ubuswificache[self.sid] then
1579 return (_ubuswificache[self.sid].up == true)
1585 function wifidev.get_wifinet(self, net)
1586 if _uci:get("wireless", net) == "wifi-iface" then
1589 local wnet = _wifi_sid_by_ifname(net)
1591 return wifinet(wnet)
1596 function wifidev.get_wifinets(self)
1599 _uci:foreach("wireless", "wifi-iface",
1601 if s.device == self.sid then
1602 nets[#nets+1] = wifinet(s['.name'])
1609 function wifidev.add_wifinet(self, options)
1610 options = options or { }
1611 options.device = self.sid
1613 local wnet = _uci:section("wireless", "wifi-iface", nil, options)
1615 return wifinet(wnet, options)
1619 function wifidev.del_wifinet(self, net)
1620 if utl.instanceof(net, wifinet) then
1622 elseif _uci:get("wireless", net) ~= "wifi-iface" then
1623 net = _wifi_sid_by_ifname(net)
1626 if net and _uci:get("wireless", net, "device") == self.sid then
1627 _uci:delete("wireless", net)
1635 wifinet = utl.class()
1637 function wifinet.__init__(self, name, data)
1638 local sid, netid, radioname, radiostate, netstate
1640 -- lookup state by radio#.network# notation
1641 sid = _wifi_sid_by_netid(name)
1644 radioname, radiostate, netstate = _wifi_state_by_sid(sid)
1646 -- lookup state by ifname (e.g. wlan0)
1647 radioname, radiostate, netstate = _wifi_state_by_ifname(name)
1648 if radioname and radiostate and netstate then
1649 sid = netstate.section
1650 netid = _wifi_netid_by_sid(sid)
1652 -- lookup state by uci section id (e.g. cfg053579)
1653 radioname, radiostate, netstate = _wifi_state_by_sid(name)
1654 if radioname and radiostate and netstate then
1656 netid = _wifi_netid_by_sid(sid)
1658 -- no state available, try to resolve from uci
1659 netid, radioname = _wifi_netid_by_sid(name)
1660 if netid and radioname then
1668 (netstate and _wifi_iwinfo_by_ifname(netstate.ifname)) or
1669 (radioname and _wifi_iwinfo_by_ifname(radioname)) or
1670 { ifname = (netid or sid or name) }
1672 self.sid = sid or name
1673 self.wdev = iwinfo.ifname
1674 self.iwinfo = iwinfo
1683 function wifinet.ubus(self, ...)
1684 local n, v = self._ubusdata
1685 for n = 1, select('#', ...) do
1686 if type(v) == "table" then
1687 v = v[select(n, ...)]
1695 function wifinet.get(self, opt)
1696 return _get("wireless", self.sid, opt)
1699 function wifinet.set(self, opt, val)
1700 return _set("wireless", self.sid, opt, val)
1703 function wifinet.mode(self)
1704 return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1707 function wifinet.ssid(self)
1708 return self:ubus("net", "config", "ssid") or self:get("ssid")
1711 function wifinet.bssid(self)
1712 return self:ubus("net", "config", "bssid") or self:get("bssid")
1715 function wifinet.network(self)
1716 local net, networks = nil, { }
1717 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1718 networks[#networks+1] = net
1723 function wifinet.id(self)
1727 function wifinet.name(self)
1731 function wifinet.ifname(self)
1732 local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
1733 if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
1739 function wifinet.get_device(self)
1740 local dev = self:ubus("radio") or self:get("device")
1741 return dev and wifidev(dev) or nil
1744 function wifinet.is_up(self)
1745 local ifc = self:get_interface()
1746 return (ifc and ifc:is_up() or false)
1749 function wifinet.active_mode(self)
1750 local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
1752 if m == "ap" then m = "Master"
1753 elseif m == "sta" then m = "Client"
1754 elseif m == "adhoc" then m = "Ad-Hoc"
1755 elseif m == "mesh" then m = "Mesh"
1756 elseif m == "monitor" then m = "Monitor"
1762 function wifinet.active_mode_i18n(self)
1763 return lng.translate(self:active_mode())
1766 function wifinet.active_ssid(self)
1767 return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
1770 function wifinet.active_bssid(self)
1771 return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
1774 function wifinet.active_encryption(self)
1775 local enc = self.iwinfo and self.iwinfo.encryption
1776 return enc and enc.description or "-"
1779 function wifinet.assoclist(self)
1780 return self.iwinfo.assoclist or { }
1783 function wifinet.frequency(self)
1784 local freq = self.iwinfo.frequency
1785 if freq and freq > 0 then
1786 return "%.03f" % (freq / 1000)
1790 function wifinet.bitrate(self)
1791 local rate = self.iwinfo.bitrate
1792 if rate and rate > 0 then
1793 return (rate / 1000)
1797 function wifinet.channel(self)
1798 return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
1799 tonumber(self:get("channel"))
1802 function wifinet.signal(self)
1803 return self.iwinfo.signal or 0
1806 function wifinet.noise(self)
1807 return self.iwinfo.noise or 0
1810 function wifinet.country(self)
1811 return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
1814 function wifinet.txpower(self)
1815 local pwr = (self.iwinfo.txpower or 0)
1816 return pwr + self:txpower_offset()
1819 function wifinet.txpower_offset(self)
1820 return self.iwinfo.txpower_offset or 0
1823 function wifinet.signal_level(self, s, n)
1824 if self:active_bssid() ~= "00:00:00:00:00:00" then
1825 local signal = s or self:signal()
1826 local noise = n or self:noise()
1828 if signal < 0 and noise < 0 then
1829 local snr = -1 * (noise - signal)
1830 return math.floor(snr / 5)
1839 function wifinet.signal_percent(self)
1840 local qc = self.iwinfo.quality or 0
1841 local qm = self.iwinfo.quality_max or 0
1843 if qc > 0 and qm > 0 then
1844 return math.floor((100 / qm) * qc)
1850 function wifinet.shortname(self)
1852 lng.translate(self:active_mode()),
1853 self:active_ssid() or self:active_bssid() or self:id()
1857 function wifinet.get_i18n(self)
1858 return "%s: %s %q (%s)" %{
1859 lng.translate("Wireless Network"),
1860 lng.translate(self:active_mode()),
1861 self:active_ssid() or self:active_bssid() or self:id(),
1866 function wifinet.adminlink(self)
1867 local stat, dsp = pcall(require, "luci.dispatcher")
1868 return dsp and dsp.build_url("admin", "network", "wireless", self.netid)
1871 function wifinet.get_network(self)
1872 return self:get_networks()[1]
1875 function wifinet.get_networks(self)
1878 for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
1879 if _uci:get("network", net) == "interface" then
1880 nets[#nets+1] = network(net)
1883 table.sort(nets, function(a, b) return a.sid < b.sid end)
1887 function wifinet.get_interface(self)
1888 return interface(self:ifname())
1892 -- setup base protocols
1893 _M:register_protocol("static")
1894 _M:register_protocol("dhcp")
1895 _M:register_protocol("none")
1897 -- load protocol extensions
1898 local exts = nfs.dir(utl.libpath() .. "/model/network")
1902 if ext:match("%.lua$") then
1903 require("luci.model.network." .. ext:gsub("%.lua$", ""))