2
0

network.lua 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. -- Copyright 2009-2015 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local type, next, pairs, ipairs, loadfile, table, select
  4. = type, next, pairs, ipairs, loadfile, table, select
  5. local tonumber, tostring, math = tonumber, tostring, math
  6. local pcall, require, setmetatable = pcall, require, setmetatable
  7. local nxo = require "nixio"
  8. local nfs = require "nixio.fs"
  9. local ipc = require "luci.ip"
  10. local utl = require "luci.util"
  11. local uci = require "luci.model.uci"
  12. local lng = require "luci.i18n"
  13. local jsc = require "luci.jsonc"
  14. module "luci.model.network"
  15. IFACE_PATTERNS_VIRTUAL = { }
  16. 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$" }
  17. IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
  18. IFACE_ERRORS = {
  19. CONNECT_FAILED = lng.translate("Connection attempt failed"),
  20. INVALID_ADDRESS = lng.translate("IP address is invalid"),
  21. INVALID_GATEWAY = lng.translate("Gateway address is invalid"),
  22. INVALID_LOCAL_ADDRESS = lng.translate("Local IP address is invalid"),
  23. MISSING_ADDRESS = lng.translate("IP address is missing"),
  24. MISSING_PEER_ADDRESS = lng.translate("Peer address is missing"),
  25. NO_DEVICE = lng.translate("Network device is not present"),
  26. NO_IFACE = lng.translate("Unable to determine device name"),
  27. NO_IFNAME = lng.translate("Unable to determine device name"),
  28. NO_WAN_ADDRESS = lng.translate("Unable to determine external IP address"),
  29. NO_WAN_LINK = lng.translate("Unable to determine upstream interface"),
  30. PEER_RESOLVE_FAIL = lng.translate("Unable to resolve peer host name"),
  31. PIN_FAILED = lng.translate("PIN code rejected")
  32. }
  33. protocol = utl.class()
  34. local _protocols = { }
  35. local _interfaces, _bridge, _switch, _tunnel, _swtopo
  36. local _ubusnetcache, _ubusdevcache, _ubuswificache
  37. local _uci
  38. function _filter(c, s, o, r)
  39. local val = _uci:get(c, s, o)
  40. if val then
  41. local l = { }
  42. if type(val) == "string" then
  43. for val in val:gmatch("%S+") do
  44. if val ~= r then
  45. l[#l+1] = val
  46. end
  47. end
  48. if #l > 0 then
  49. _uci:set(c, s, o, table.concat(l, " "))
  50. else
  51. _uci:delete(c, s, o)
  52. end
  53. elseif type(val) == "table" then
  54. for _, val in ipairs(val) do
  55. if val ~= r then
  56. l[#l+1] = val
  57. end
  58. end
  59. if #l > 0 then
  60. _uci:set(c, s, o, l)
  61. else
  62. _uci:delete(c, s, o)
  63. end
  64. end
  65. end
  66. end
  67. function _append(c, s, o, a)
  68. local val = _uci:get(c, s, o) or ""
  69. if type(val) == "string" then
  70. local l = { }
  71. for val in val:gmatch("%S+") do
  72. if val ~= a then
  73. l[#l+1] = val
  74. end
  75. end
  76. l[#l+1] = a
  77. _uci:set(c, s, o, table.concat(l, " "))
  78. elseif type(val) == "table" then
  79. local l = { }
  80. for _, val in ipairs(val) do
  81. if val ~= a then
  82. l[#l+1] = val
  83. end
  84. end
  85. l[#l+1] = a
  86. _uci:set(c, s, o, l)
  87. end
  88. end
  89. function _stror(s1, s2)
  90. if not s1 or #s1 == 0 then
  91. return s2 and #s2 > 0 and s2
  92. else
  93. return s1
  94. end
  95. end
  96. function _get(c, s, o)
  97. return _uci:get(c, s, o)
  98. end
  99. function _set(c, s, o, v)
  100. if v ~= nil then
  101. if type(v) == "boolean" then v = v and "1" or "0" end
  102. return _uci:set(c, s, o, v)
  103. else
  104. return _uci:delete(c, s, o)
  105. end
  106. end
  107. local function _wifi_state()
  108. if not next(_ubuswificache) then
  109. _ubuswificache = utl.ubus("network.wireless", "status", {}) or {}
  110. end
  111. return _ubuswificache
  112. end
  113. local function _wifi_state_by_sid(sid)
  114. local t1, n1 = _uci:get("wireless", sid)
  115. if t1 == "wifi-iface" and n1 ~= nil then
  116. local radioname, radiostate
  117. for radioname, radiostate in pairs(_wifi_state()) do
  118. if type(radiostate) == "table" and
  119. type(radiostate.interfaces) == "table"
  120. then
  121. local netidx, netstate
  122. for netidx, netstate in ipairs(radiostate.interfaces) do
  123. if type(netstate) == "table" and
  124. type(netstate.section) == "string"
  125. then
  126. local t2, n2 = _uci:get("wireless", netstate.section)
  127. if t1 == t2 and n1 == n2 then
  128. return radioname, radiostate, netstate
  129. end
  130. end
  131. end
  132. end
  133. end
  134. end
  135. end
  136. local function _wifi_state_by_ifname(ifname)
  137. if type(ifname) == "string" then
  138. local radioname, radiostate
  139. for radioname, radiostate in pairs(_wifi_state()) do
  140. if type(radiostate) == "table" and
  141. type(radiostate.interfaces) == "table"
  142. then
  143. local netidx, netstate
  144. for netidx, netstate in ipairs(radiostate.interfaces) do
  145. if type(netstate) == "table" and
  146. type(netstate.ifname) == "string" and
  147. netstate.ifname == ifname
  148. then
  149. return radioname, radiostate, netstate
  150. end
  151. end
  152. end
  153. end
  154. end
  155. end
  156. function _wifi_iface(x)
  157. local _, p
  158. for _, p in ipairs(IFACE_PATTERNS_WIRELESS) do
  159. if x:match(p) then
  160. return true
  161. end
  162. end
  163. return (nfs.access("/sys/class/net/%s/phy80211" % x) == true)
  164. end
  165. local function _wifi_iwinfo_by_ifname(ifname, force_phy_only)
  166. local stat, iwinfo = pcall(require, "iwinfo")
  167. local iwtype = stat and type(ifname) == "string" and iwinfo.type(ifname)
  168. local is_nonphy_op = {
  169. bitrate = true,
  170. quality = true,
  171. quality_max = true,
  172. mode = true,
  173. ssid = true,
  174. bssid = true,
  175. assoclist = true,
  176. encryption = true
  177. }
  178. if iwtype then
  179. -- if we got a type but no real netdev, we're referring to a phy
  180. local phy_only = force_phy_only or (ipc.link(ifname).type ~= 1)
  181. return setmetatable({}, {
  182. __index = function(t, k)
  183. if k == "ifname" then
  184. return ifname
  185. elseif phy_only and is_nonphy_op[k] then
  186. return nil
  187. elseif iwinfo[iwtype][k] then
  188. return iwinfo[iwtype][k](ifname)
  189. end
  190. end
  191. })
  192. end
  193. end
  194. local function _wifi_sid_by_netid(netid)
  195. if type(netid) == "string" then
  196. local radioname, netidx = netid:match("^(%w+)%.network(%d+)$")
  197. if radioname and netidx then
  198. local i, n = 0, nil
  199. netidx = tonumber(netidx)
  200. _uci:foreach("wireless", "wifi-iface",
  201. function(s)
  202. if s.device == radioname then
  203. i = i + 1
  204. if i == netidx then
  205. n = s[".name"]
  206. return false
  207. end
  208. end
  209. end)
  210. return n
  211. end
  212. end
  213. end
  214. function _wifi_sid_by_ifname(ifn)
  215. local sid = _wifi_sid_by_netid(ifn)
  216. if sid then
  217. return sid
  218. end
  219. local _, _, netstate = _wifi_state_by_ifname(ifn)
  220. if netstate and type(netstate.section) == "string" then
  221. return netstate.section
  222. end
  223. end
  224. local function _wifi_netid_by_sid(sid)
  225. local t, n = _uci:get("wireless", sid)
  226. if t == "wifi-iface" and n ~= nil then
  227. local radioname = _uci:get("wireless", n, "device")
  228. if type(radioname) == "string" then
  229. local i, netid = 0, nil
  230. _uci:foreach("wireless", "wifi-iface",
  231. function(s)
  232. if s.device == radioname then
  233. i = i + 1
  234. if s[".name"] == n then
  235. netid = "%s.network%d" %{ radioname, i }
  236. return false
  237. end
  238. end
  239. end)
  240. return netid, radioname
  241. end
  242. end
  243. end
  244. local function _wifi_netid_by_netname(name)
  245. local netid = nil
  246. _uci:foreach("wireless", "wifi-iface",
  247. function(s)
  248. local net
  249. for net in utl.imatch(s.network) do
  250. if net == name then
  251. netid = _wifi_netid_by_sid(s[".name"])
  252. return false
  253. end
  254. end
  255. end)
  256. return netid
  257. end
  258. function _iface_virtual(x)
  259. local _, p
  260. for _, p in ipairs(IFACE_PATTERNS_VIRTUAL) do
  261. if x:match(p) then
  262. return true
  263. end
  264. end
  265. return false
  266. end
  267. function _iface_ignore(x)
  268. local _, p
  269. for _, p in ipairs(IFACE_PATTERNS_IGNORE) do
  270. if x:match(p) then
  271. return true
  272. end
  273. end
  274. return false
  275. end
  276. function init(cursor)
  277. _uci = cursor or _uci or uci.cursor()
  278. _interfaces = { }
  279. _bridge = { }
  280. _switch = { }
  281. _tunnel = { }
  282. _swtopo = { }
  283. _ubusnetcache = { }
  284. _ubusdevcache = { }
  285. _ubuswificache = { }
  286. -- read interface information
  287. local n, i
  288. for n, i in ipairs(nxo.getifaddrs()) do
  289. local name = i.name:match("[^:]+")
  290. if _iface_virtual(name) then
  291. _tunnel[name] = true
  292. end
  293. if _tunnel[name] or not (_iface_ignore(name) or _iface_virtual(name)) then
  294. _interfaces[name] = _interfaces[name] or {
  295. idx = i.ifindex or n,
  296. name = name,
  297. rawname = i.name,
  298. flags = { },
  299. ipaddrs = { },
  300. ip6addrs = { }
  301. }
  302. if i.family == "packet" then
  303. _interfaces[name].flags = i.flags
  304. _interfaces[name].stats = i.data
  305. _interfaces[name].macaddr = ipc.checkmac(i.addr)
  306. elseif i.family == "inet" then
  307. _interfaces[name].ipaddrs[#_interfaces[name].ipaddrs+1] = ipc.IPv4(i.addr, i.netmask)
  308. elseif i.family == "inet6" then
  309. _interfaces[name].ip6addrs[#_interfaces[name].ip6addrs+1] = ipc.IPv6(i.addr, i.netmask)
  310. end
  311. end
  312. end
  313. -- read bridge informaton
  314. local b, l
  315. for l in utl.execi("brctl show") do
  316. if not l:match("STP") then
  317. local r = utl.split(l, "%s+", nil, true)
  318. if #r == 4 then
  319. b = {
  320. name = r[1],
  321. id = r[2],
  322. stp = r[3] == "yes",
  323. ifnames = { _interfaces[r[4]] }
  324. }
  325. if b.ifnames[1] then
  326. b.ifnames[1].bridge = b
  327. end
  328. _bridge[r[1]] = b
  329. _interfaces[r[1]].bridge = b
  330. elseif b then
  331. b.ifnames[#b.ifnames+1] = _interfaces[r[2]]
  332. b.ifnames[#b.ifnames].bridge = b
  333. end
  334. end
  335. end
  336. -- read switch topology
  337. local boardinfo = jsc.parse(nfs.readfile("/etc/board.json") or "")
  338. if type(boardinfo) == "table" and type(boardinfo.switch) == "table" then
  339. local switch, layout
  340. for switch, layout in pairs(boardinfo.switch) do
  341. if type(layout) == "table" and type(layout.ports) == "table" then
  342. local _, port
  343. local ports = { }
  344. local nports = { }
  345. local netdevs = { }
  346. for _, port in ipairs(layout.ports) do
  347. if type(port) == "table" and
  348. type(port.num) == "number" and
  349. (type(port.role) == "string" or
  350. type(port.device) == "string")
  351. then
  352. local spec = {
  353. num = port.num,
  354. role = port.role or "cpu",
  355. index = port.index or port.num
  356. }
  357. if port.device then
  358. spec.device = port.device
  359. spec.tagged = port.need_tag
  360. netdevs[tostring(port.num)] = port.device
  361. end
  362. ports[#ports+1] = spec
  363. if port.role then
  364. nports[port.role] = (nports[port.role] or 0) + 1
  365. end
  366. end
  367. end
  368. table.sort(ports, function(a, b)
  369. if a.role ~= b.role then
  370. return (a.role < b.role)
  371. end
  372. return (a.index < b.index)
  373. end)
  374. local pnum, role
  375. for _, port in ipairs(ports) do
  376. if port.role ~= role then
  377. role = port.role
  378. pnum = 1
  379. end
  380. if role == "cpu" then
  381. port.label = "CPU (%s)" % port.device
  382. elseif nports[role] > 1 then
  383. port.label = "%s %d" %{ role:upper(), pnum }
  384. pnum = pnum + 1
  385. else
  386. port.label = role:upper()
  387. end
  388. port.role = nil
  389. port.index = nil
  390. end
  391. _swtopo[switch] = {
  392. ports = ports,
  393. netdevs = netdevs
  394. }
  395. end
  396. end
  397. end
  398. return _M
  399. end
  400. function save(self, ...)
  401. _uci:save(...)
  402. _uci:load(...)
  403. end
  404. function commit(self, ...)
  405. _uci:commit(...)
  406. _uci:load(...)
  407. end
  408. function ifnameof(self, x)
  409. if utl.instanceof(x, interface) then
  410. return x:name()
  411. elseif utl.instanceof(x, protocol) then
  412. return x:ifname()
  413. elseif type(x) == "string" then
  414. return x:match("^[^:]+")
  415. end
  416. end
  417. function get_protocol(self, protoname, netname)
  418. local v = _protocols[protoname]
  419. if v then
  420. return v(netname or "__dummy__")
  421. end
  422. end
  423. function get_protocols(self)
  424. local p = { }
  425. local _, v
  426. for _, v in ipairs(_protocols) do
  427. p[#p+1] = v("__dummy__")
  428. end
  429. return p
  430. end
  431. function register_protocol(self, protoname)
  432. local proto = utl.class(protocol)
  433. function proto.__init__(self, name)
  434. self.sid = name
  435. end
  436. function proto.proto(self)
  437. return protoname
  438. end
  439. _protocols[#_protocols+1] = proto
  440. _protocols[protoname] = proto
  441. return proto
  442. end
  443. function register_pattern_virtual(self, pat)
  444. IFACE_PATTERNS_VIRTUAL[#IFACE_PATTERNS_VIRTUAL+1] = pat
  445. end
  446. function register_error_code(self, code, message)
  447. if type(code) == "string" and
  448. type(message) == "string" and
  449. not IFACE_ERRORS[code]
  450. then
  451. IFACE_ERRORS[code] = message
  452. return true
  453. end
  454. return false
  455. end
  456. function has_ipv6(self)
  457. return nfs.access("/proc/net/ipv6_route")
  458. end
  459. function add_network(self, n, options)
  460. local oldnet = self:get_network(n)
  461. if n and #n > 0 and n:match("^[a-zA-Z0-9_]+$") and not oldnet then
  462. if _uci:section("network", "interface", n, options) then
  463. return network(n)
  464. end
  465. elseif oldnet and oldnet:is_empty() then
  466. if options then
  467. local k, v
  468. for k, v in pairs(options) do
  469. oldnet:set(k, v)
  470. end
  471. end
  472. return oldnet
  473. end
  474. end
  475. function get_network(self, n)
  476. if n and _uci:get("network", n) == "interface" then
  477. return network(n)
  478. elseif n then
  479. local stat = utl.ubus("network.interface", "status", { interface = n })
  480. if type(stat) == "table" and
  481. type(stat.proto) == "string"
  482. then
  483. return network(n, stat.proto)
  484. end
  485. end
  486. end
  487. function get_networks(self)
  488. local nets = { }
  489. local nls = { }
  490. _uci:foreach("network", "interface",
  491. function(s)
  492. nls[s['.name']] = network(s['.name'])
  493. end)
  494. local dump = utl.ubus("network.interface", "dump", { })
  495. if type(dump) == "table" and
  496. type(dump.interface) == "table"
  497. then
  498. local _, net
  499. for _, net in ipairs(dump.interface) do
  500. if type(net) == "table" and
  501. type(net.proto) == "string" and
  502. type(net.interface) == "string"
  503. then
  504. if not nls[net.interface] then
  505. nls[net.interface] = network(net.interface, net.proto)
  506. end
  507. end
  508. end
  509. end
  510. local n
  511. for n in utl.kspairs(nls) do
  512. nets[#nets+1] = nls[n]
  513. end
  514. return nets
  515. end
  516. function del_network(self, n)
  517. local r = _uci:delete("network", n)
  518. if r then
  519. _uci:delete_all("luci", "ifstate",
  520. function(s) return (s.interface == n) end)
  521. _uci:delete_all("network", "alias",
  522. function(s) return (s.interface == n) end)
  523. _uci:delete_all("network", "route",
  524. function(s) return (s.interface == n) end)
  525. _uci:delete_all("network", "route6",
  526. function(s) return (s.interface == n) end)
  527. _uci:foreach("wireless", "wifi-iface",
  528. function(s)
  529. local net
  530. local rest = { }
  531. for net in utl.imatch(s.network) do
  532. if net ~= n then
  533. rest[#rest+1] = net
  534. end
  535. end
  536. if #rest > 0 then
  537. _uci:set("wireless", s['.name'], "network",
  538. table.concat(rest, " "))
  539. else
  540. _uci:delete("wireless", s['.name'], "network")
  541. end
  542. end)
  543. local ok, fw = pcall(require, "luci.model.firewall")
  544. if ok then
  545. fw.init()
  546. fw:del_network(n)
  547. end
  548. end
  549. return r
  550. end
  551. function rename_network(self, old, new)
  552. local r
  553. if new and #new > 0 and new:match("^[a-zA-Z0-9_]+$") and not self:get_network(new) then
  554. r = _uci:section("network", "interface", new, _uci:get_all("network", old))
  555. if r then
  556. _uci:foreach("network", "alias",
  557. function(s)
  558. if s.interface == old then
  559. _uci:set("network", s['.name'], "interface", new)
  560. end
  561. end)
  562. _uci:foreach("network", "route",
  563. function(s)
  564. if s.interface == old then
  565. _uci:set("network", s['.name'], "interface", new)
  566. end
  567. end)
  568. _uci:foreach("network", "route6",
  569. function(s)
  570. if s.interface == old then
  571. _uci:set("network", s['.name'], "interface", new)
  572. end
  573. end)
  574. _uci:foreach("wireless", "wifi-iface",
  575. function(s)
  576. local net
  577. local list = { }
  578. for net in utl.imatch(s.network) do
  579. if net == old then
  580. list[#list+1] = new
  581. else
  582. list[#list+1] = net
  583. end
  584. end
  585. if #list > 0 then
  586. _uci:set("wireless", s['.name'], "network",
  587. table.concat(list, " "))
  588. end
  589. end)
  590. _uci:delete("network", old)
  591. end
  592. end
  593. return r or false
  594. end
  595. function get_interface(self, i)
  596. if _interfaces[i] or _wifi_iface(i) then
  597. return interface(i)
  598. else
  599. local netid = _wifi_netid_by_sid(i)
  600. return netid and interface(netid)
  601. end
  602. end
  603. function get_interfaces(self)
  604. local iface
  605. local ifaces = { }
  606. local nfs = { }
  607. -- find normal interfaces
  608. _uci:foreach("network", "interface",
  609. function(s)
  610. for iface in utl.imatch(s.ifname) do
  611. if not _iface_ignore(iface) and not _iface_virtual(iface) and not _wifi_iface(iface) then
  612. nfs[iface] = interface(iface)
  613. end
  614. end
  615. end)
  616. for iface in utl.kspairs(_interfaces) do
  617. if not (nfs[iface] or _iface_ignore(iface) or _iface_virtual(iface) or _wifi_iface(iface)) then
  618. nfs[iface] = interface(iface)
  619. end
  620. end
  621. -- find vlan interfaces
  622. _uci:foreach("network", "switch_vlan",
  623. function(s)
  624. if type(s.ports) ~= "string" or
  625. type(s.device) ~= "string" or
  626. type(_swtopo[s.device]) ~= "table"
  627. then
  628. return
  629. end
  630. local pnum, ptag
  631. for pnum, ptag in s.ports:gmatch("(%d+)([tu]?)") do
  632. local netdev = _swtopo[s.device].netdevs[pnum]
  633. if netdev then
  634. if not nfs[netdev] then
  635. nfs[netdev] = interface(netdev)
  636. end
  637. _switch[netdev] = true
  638. if ptag == "t" then
  639. local vid = tonumber(s.vid or s.vlan)
  640. if vid ~= nil and vid >= 0 and vid <= 4095 then
  641. local iface = "%s.%d" %{ netdev, vid }
  642. if not nfs[iface] then
  643. nfs[iface] = interface(iface)
  644. end
  645. _switch[iface] = true
  646. end
  647. end
  648. end
  649. end
  650. end)
  651. for iface in utl.kspairs(nfs) do
  652. ifaces[#ifaces+1] = nfs[iface]
  653. end
  654. -- find wifi interfaces
  655. local num = { }
  656. local wfs = { }
  657. _uci:foreach("wireless", "wifi-iface",
  658. function(s)
  659. if s.device then
  660. num[s.device] = num[s.device] and num[s.device] + 1 or 1
  661. local i = "%s.network%d" %{ s.device, num[s.device] }
  662. wfs[i] = interface(i)
  663. end
  664. end)
  665. for iface in utl.kspairs(wfs) do
  666. ifaces[#ifaces+1] = wfs[iface]
  667. end
  668. return ifaces
  669. end
  670. function ignore_interface(self, x)
  671. return _iface_ignore(x)
  672. end
  673. function get_wifidev(self, dev)
  674. if _uci:get("wireless", dev) == "wifi-device" then
  675. return wifidev(dev)
  676. end
  677. end
  678. function get_wifidevs(self)
  679. local devs = { }
  680. local wfd = { }
  681. _uci:foreach("wireless", "wifi-device",
  682. function(s) wfd[#wfd+1] = s['.name'] end)
  683. local dev
  684. for _, dev in utl.vspairs(wfd) do
  685. devs[#devs+1] = wifidev(dev)
  686. end
  687. return devs
  688. end
  689. function get_wifinet(self, net)
  690. local wnet = _wifi_sid_by_ifname(net)
  691. if wnet then
  692. return wifinet(wnet)
  693. end
  694. end
  695. function add_wifinet(self, net, options)
  696. if type(options) == "table" and options.device and
  697. _uci:get("wireless", options.device) == "wifi-device"
  698. then
  699. local wnet = _uci:section("wireless", "wifi-iface", nil, options)
  700. return wifinet(wnet)
  701. end
  702. end
  703. function del_wifinet(self, net)
  704. local wnet = _wifi_sid_by_ifname(net)
  705. if wnet then
  706. _uci:delete("wireless", wnet)
  707. return true
  708. end
  709. return false
  710. end
  711. function get_status_by_route(self, addr, mask)
  712. local route_statuses = { }
  713. local _, object
  714. for _, object in ipairs(utl.ubus()) do
  715. local net = object:match("^network%.interface%.(.+)")
  716. if net then
  717. local s = utl.ubus(object, "status", {})
  718. if s and s.route then
  719. local rt
  720. for _, rt in ipairs(s.route) do
  721. if not rt.table and rt.target == addr and rt.mask == mask then
  722. route_statuses[net] = s
  723. end
  724. end
  725. end
  726. end
  727. end
  728. return route_statuses
  729. end
  730. function get_status_by_address(self, addr)
  731. local _, object
  732. for _, object in ipairs(utl.ubus()) do
  733. local net = object:match("^network%.interface%.(.+)")
  734. if net then
  735. local s = utl.ubus(object, "status", {})
  736. if s and s['ipv4-address'] then
  737. local a
  738. for _, a in ipairs(s['ipv4-address']) do
  739. if a.address == addr then
  740. return net, s
  741. end
  742. end
  743. end
  744. if s and s['ipv6-address'] then
  745. local a
  746. for _, a in ipairs(s['ipv6-address']) do
  747. if a.address == addr then
  748. return net, s
  749. end
  750. end
  751. end
  752. if s and s['ipv6-prefix-assignment'] then
  753. local a
  754. for _, a in ipairs(s['ipv6-prefix-assignment']) do
  755. if a and a['local-address'] and a['local-address'].address == addr then
  756. return net, s
  757. end
  758. end
  759. end
  760. end
  761. end
  762. end
  763. function get_wan_networks(self)
  764. local k, v
  765. local wan_nets = { }
  766. local route_statuses = self:get_status_by_route("0.0.0.0", 0)
  767. for k, v in pairs(route_statuses) do
  768. wan_nets[#wan_nets+1] = network(k, v.proto)
  769. end
  770. return wan_nets
  771. end
  772. function get_wan6_networks(self)
  773. local k, v
  774. local wan6_nets = { }
  775. local route_statuses = self:get_status_by_route("::", 0)
  776. for k, v in pairs(route_statuses) do
  777. wan6_nets[#wan6_nets+1] = network(k, v.proto)
  778. end
  779. return wan6_nets
  780. end
  781. function get_switch_topologies(self)
  782. return _swtopo
  783. end
  784. function network(name, proto)
  785. if name then
  786. local p = proto or _uci:get("network", name, "proto")
  787. local c = p and _protocols[p] or protocol
  788. return c(name)
  789. end
  790. end
  791. function protocol.__init__(self, name)
  792. self.sid = name
  793. end
  794. function protocol._get(self, opt)
  795. local v = _uci:get("network", self.sid, opt)
  796. if type(v) == "table" then
  797. return table.concat(v, " ")
  798. end
  799. return v or ""
  800. end
  801. function protocol._ubus(self, field)
  802. if not _ubusnetcache[self.sid] then
  803. _ubusnetcache[self.sid] = utl.ubus("network.interface.%s" % self.sid,
  804. "status", { })
  805. end
  806. if _ubusnetcache[self.sid] and field then
  807. return _ubusnetcache[self.sid][field]
  808. end
  809. return _ubusnetcache[self.sid]
  810. end
  811. function protocol.get(self, opt)
  812. return _get("network", self.sid, opt)
  813. end
  814. function protocol.set(self, opt, val)
  815. return _set("network", self.sid, opt, val)
  816. end
  817. function protocol.ifname(self)
  818. local ifname
  819. if self:is_floating() then
  820. ifname = self:_ubus("l3_device")
  821. else
  822. ifname = self:_ubus("device")
  823. end
  824. if not ifname then
  825. ifname = _wifi_netid_by_netname(self.sid)
  826. end
  827. return ifname
  828. end
  829. function protocol.proto(self)
  830. return "none"
  831. end
  832. function protocol.get_i18n(self)
  833. local p = self:proto()
  834. if p == "none" then
  835. return lng.translate("Unmanaged")
  836. elseif p == "static" then
  837. return lng.translate("Static address")
  838. elseif p == "dhcp" then
  839. return lng.translate("DHCP client")
  840. else
  841. return lng.translate("Unknown")
  842. end
  843. end
  844. function protocol.type(self)
  845. return self:_get("type")
  846. end
  847. function protocol.name(self)
  848. return self.sid
  849. end
  850. function protocol.uptime(self)
  851. return self:_ubus("uptime") or 0
  852. end
  853. function protocol.expires(self)
  854. local u = self:_ubus("uptime")
  855. local d = self:_ubus("data")
  856. if type(u) == "number" and type(d) == "table" and
  857. type(d.leasetime) == "number"
  858. then
  859. local r = (d.leasetime - (u % d.leasetime))
  860. return r > 0 and r or 0
  861. end
  862. return -1
  863. end
  864. function protocol.metric(self)
  865. return self:_ubus("metric") or 0
  866. end
  867. function protocol.zonename(self)
  868. local d = self:_ubus("data")
  869. if type(d) == "table" and type(d.zone) == "string" then
  870. return d.zone
  871. end
  872. return nil
  873. end
  874. function protocol.ipaddr(self)
  875. local addrs = self:_ubus("ipv4-address")
  876. return addrs and #addrs > 0 and addrs[1].address
  877. end
  878. function protocol.ipaddrs(self)
  879. local addrs = self:_ubus("ipv4-address")
  880. local rv = { }
  881. if type(addrs) == "table" then
  882. local n, addr
  883. for n, addr in ipairs(addrs) do
  884. rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
  885. end
  886. end
  887. return rv
  888. end
  889. function protocol.netmask(self)
  890. local addrs = self:_ubus("ipv4-address")
  891. return addrs and #addrs > 0 and
  892. ipc.IPv4("0.0.0.0/%d" % addrs[1].mask):mask():string()
  893. end
  894. function protocol.gwaddr(self)
  895. local _, route
  896. for _, route in ipairs(self:_ubus("route") or { }) do
  897. if route.target == "0.0.0.0" and route.mask == 0 then
  898. return route.nexthop
  899. end
  900. end
  901. end
  902. function protocol.dnsaddrs(self)
  903. local dns = { }
  904. local _, addr
  905. for _, addr in ipairs(self:_ubus("dns-server") or { }) do
  906. if not addr:match(":") then
  907. dns[#dns+1] = addr
  908. end
  909. end
  910. return dns
  911. end
  912. function protocol.ip6addr(self)
  913. local addrs = self:_ubus("ipv6-address")
  914. if addrs and #addrs > 0 then
  915. return "%s/%d" %{ addrs[1].address, addrs[1].mask }
  916. else
  917. addrs = self:_ubus("ipv6-prefix-assignment")
  918. if addrs and #addrs > 0 then
  919. return "%s/%d" %{ addrs[1].address, addrs[1].mask }
  920. end
  921. end
  922. end
  923. function protocol.ip6addrs(self)
  924. local addrs = self:_ubus("ipv6-address")
  925. local rv = { }
  926. local n, addr
  927. if type(addrs) == "table" then
  928. for n, addr in ipairs(addrs) do
  929. rv[#rv+1] = "%s/%d" %{ addr.address, addr.mask }
  930. end
  931. end
  932. addrs = self:_ubus("ipv6-prefix-assignment")
  933. if type(addrs) == "table" then
  934. for n, addr in ipairs(addrs) do
  935. if type(addr["local-address"]) == "table" and
  936. type(addr["local-address"].mask) == "number" and
  937. type(addr["local-address"].address) == "string"
  938. then
  939. rv[#rv+1] = "%s/%d" %{
  940. addr["local-address"].address,
  941. addr["local-address"].mask
  942. }
  943. end
  944. end
  945. end
  946. return rv
  947. end
  948. function protocol.gw6addr(self)
  949. local _, route
  950. for _, route in ipairs(self:_ubus("route") or { }) do
  951. if route.target == "::" and route.mask == 0 then
  952. return ipc.IPv6(route.nexthop):string()
  953. end
  954. end
  955. end
  956. function protocol.dns6addrs(self)
  957. local dns = { }
  958. local _, addr
  959. for _, addr in ipairs(self:_ubus("dns-server") or { }) do
  960. if addr:match(":") then
  961. dns[#dns+1] = addr
  962. end
  963. end
  964. return dns
  965. end
  966. function protocol.ip6prefix(self)
  967. local prefix = self:_ubus("ipv6-prefix")
  968. if prefix and #prefix > 0 then
  969. return "%s/%d" %{ prefix[1].address, prefix[1].mask }
  970. end
  971. end
  972. function protocol.errors(self)
  973. local _, err, rv
  974. local errors = self:_ubus("errors")
  975. if type(errors) == "table" then
  976. for _, err in ipairs(errors) do
  977. if type(err) == "table" and
  978. type(err.code) == "string"
  979. then
  980. rv = rv or { }
  981. rv[#rv+1] = IFACE_ERRORS[err.code] or lng.translatef("Unknown error (%s)", err.code)
  982. end
  983. end
  984. end
  985. return rv
  986. end
  987. function protocol.is_bridge(self)
  988. return (not self:is_virtual() and self:type() == "bridge")
  989. end
  990. function protocol.opkg_package(self)
  991. return nil
  992. end
  993. function protocol.is_installed(self)
  994. return true
  995. end
  996. function protocol.is_virtual(self)
  997. return false
  998. end
  999. function protocol.is_floating(self)
  1000. return false
  1001. end
  1002. function protocol.is_dynamic(self)
  1003. return (self:_ubus("dynamic") == true)
  1004. end
  1005. function protocol.is_auto(self)
  1006. return (self:_get("auto") ~= "0")
  1007. end
  1008. function protocol.is_alias(self)
  1009. local ifn, parent = nil, nil
  1010. for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
  1011. if #ifn > 1 and ifn:byte(1) == 64 then
  1012. parent = ifn:sub(2)
  1013. elseif parent ~= nil then
  1014. parent = nil
  1015. end
  1016. end
  1017. return parent
  1018. end
  1019. function protocol.is_empty(self)
  1020. if self:is_floating() then
  1021. return false
  1022. else
  1023. local empty = true
  1024. if (self:_get("ifname") or ""):match("%S+") then
  1025. empty = false
  1026. end
  1027. if empty and _wifi_netid_by_netname(self.sid) then
  1028. empty = false
  1029. end
  1030. return empty
  1031. end
  1032. end
  1033. function protocol.is_up(self)
  1034. return (self:_ubus("up") == true)
  1035. end
  1036. function protocol.add_interface(self, ifname)
  1037. ifname = _M:ifnameof(ifname)
  1038. if ifname and not self:is_floating() then
  1039. -- if its a wifi interface, change its network option
  1040. local wif = _wifi_sid_by_ifname(ifname)
  1041. if wif then
  1042. _append("wireless", wif, "network", self.sid)
  1043. -- add iface to our iface list
  1044. else
  1045. _append("network", self.sid, "ifname", ifname)
  1046. end
  1047. end
  1048. end
  1049. function protocol.del_interface(self, ifname)
  1050. ifname = _M:ifnameof(ifname)
  1051. if ifname and not self:is_floating() then
  1052. -- if its a wireless interface, clear its network option
  1053. local wif = _wifi_sid_by_ifname(ifname)
  1054. if wif then _filter("wireless", wif, "network", self.sid) end
  1055. -- remove the interface
  1056. _filter("network", self.sid, "ifname", ifname)
  1057. end
  1058. end
  1059. function protocol.get_interface(self)
  1060. if self:is_virtual() then
  1061. _tunnel[self:proto() .. "-" .. self.sid] = true
  1062. return interface(self:proto() .. "-" .. self.sid, self)
  1063. elseif self:is_bridge() then
  1064. _bridge["br-" .. self.sid] = true
  1065. return interface("br-" .. self.sid, self)
  1066. else
  1067. local ifn = self:_ubus("l3_device") or self:_ubus("device")
  1068. if ifn then
  1069. return interface(ifn, self)
  1070. end
  1071. for ifn in utl.imatch(_uci:get("network", self.sid, "ifname")) do
  1072. ifn = ifn:match("^[^:/]+")
  1073. return ifn and interface(ifn, self)
  1074. end
  1075. ifn = _wifi_netid_by_netname(self.sid)
  1076. return ifn and interface(ifn, self)
  1077. end
  1078. end
  1079. function protocol.get_interfaces(self)
  1080. if self:is_bridge() or (self:is_virtual() and not self:is_floating()) then
  1081. local ifaces = { }
  1082. local ifn
  1083. local nfs = { }
  1084. for ifn in utl.imatch(self:get("ifname")) do
  1085. ifn = ifn:match("^[^:/]+")
  1086. nfs[ifn] = interface(ifn, self)
  1087. end
  1088. for ifn in utl.kspairs(nfs) do
  1089. ifaces[#ifaces+1] = nfs[ifn]
  1090. end
  1091. local wfs = { }
  1092. _uci:foreach("wireless", "wifi-iface",
  1093. function(s)
  1094. if s.device then
  1095. local net
  1096. for net in utl.imatch(s.network) do
  1097. if net == self.sid then
  1098. ifn = _wifi_netid_by_sid(s[".name"])
  1099. if ifn then
  1100. wfs[ifn] = interface(ifn, self)
  1101. end
  1102. end
  1103. end
  1104. end
  1105. end)
  1106. for ifn in utl.kspairs(wfs) do
  1107. ifaces[#ifaces+1] = wfs[ifn]
  1108. end
  1109. return ifaces
  1110. end
  1111. end
  1112. function protocol.contains_interface(self, ifname)
  1113. ifname = _M:ifnameof(ifname)
  1114. if not ifname then
  1115. return false
  1116. elseif self:is_virtual() and self:proto() .. "-" .. self.sid == ifname then
  1117. return true
  1118. elseif self:is_bridge() and "br-" .. self.sid == ifname then
  1119. return true
  1120. else
  1121. local ifn
  1122. for ifn in utl.imatch(self:get("ifname")) do
  1123. ifn = ifn:match("[^:]+")
  1124. if ifn == ifname then
  1125. return true
  1126. end
  1127. end
  1128. local wif = _wifi_sid_by_ifname(ifname)
  1129. if wif then
  1130. local n
  1131. for n in utl.imatch(_uci:get("wireless", wif, "network")) do
  1132. if n == self.sid then
  1133. return true
  1134. end
  1135. end
  1136. end
  1137. end
  1138. return false
  1139. end
  1140. function protocol.adminlink(self)
  1141. local stat, dsp = pcall(require, "luci.dispatcher")
  1142. return stat and dsp.build_url("admin", "network", "network", self.sid)
  1143. end
  1144. interface = utl.class()
  1145. function interface.__init__(self, ifname, network)
  1146. local wif = _wifi_sid_by_ifname(ifname)
  1147. if wif then
  1148. self.wif = wifinet(wif)
  1149. self.ifname = self.wif:ifname()
  1150. end
  1151. self.ifname = self.ifname or ifname
  1152. self.dev = _interfaces[self.ifname]
  1153. self.network = network
  1154. end
  1155. function interface._ubus(self, field)
  1156. if not _ubusdevcache[self.ifname] then
  1157. _ubusdevcache[self.ifname] = utl.ubus("network.device", "status",
  1158. { name = self.ifname })
  1159. end
  1160. if _ubusdevcache[self.ifname] and field then
  1161. return _ubusdevcache[self.ifname][field]
  1162. end
  1163. return _ubusdevcache[self.ifname]
  1164. end
  1165. function interface.name(self)
  1166. return self.wif and self.wif:ifname() or self.ifname
  1167. end
  1168. function interface.mac(self)
  1169. return ipc.checkmac(self:_ubus("macaddr"))
  1170. end
  1171. function interface.ipaddrs(self)
  1172. return self.dev and self.dev.ipaddrs or { }
  1173. end
  1174. function interface.ip6addrs(self)
  1175. return self.dev and self.dev.ip6addrs or { }
  1176. end
  1177. function interface.type(self)
  1178. if self.ifname and self.ifname:byte(1) == 64 then
  1179. return "alias"
  1180. elseif self.wif or _wifi_iface(self.ifname) then
  1181. return "wifi"
  1182. elseif _bridge[self.ifname] then
  1183. return "bridge"
  1184. elseif _tunnel[self.ifname] then
  1185. return "tunnel"
  1186. elseif self.ifname:match("%.") then
  1187. return "vlan"
  1188. elseif _switch[self.ifname] then
  1189. return "switch"
  1190. else
  1191. return "ethernet"
  1192. end
  1193. end
  1194. function interface.shortname(self)
  1195. if self.wif then
  1196. return self.wif:shortname()
  1197. else
  1198. return self.ifname
  1199. end
  1200. end
  1201. function interface.get_i18n(self)
  1202. if self.wif then
  1203. return "%s: %s %q" %{
  1204. lng.translate("Wireless Network"),
  1205. self.wif:active_mode(),
  1206. self.wif:active_ssid() or self.wif:active_bssid() or self.wif:id() or "?"
  1207. }
  1208. else
  1209. return "%s: %q" %{ self:get_type_i18n(), self:name() }
  1210. end
  1211. end
  1212. function interface.get_type_i18n(self)
  1213. local x = self:type()
  1214. if x == "alias" then
  1215. return lng.translate("Alias Interface")
  1216. elseif x == "wifi" then
  1217. return lng.translate("Wireless Adapter")
  1218. elseif x == "bridge" then
  1219. return lng.translate("Bridge")
  1220. elseif x == "switch" then
  1221. return lng.translate("Ethernet Switch")
  1222. elseif x == "vlan" then
  1223. if _switch[self.ifname] then
  1224. return lng.translate("Switch VLAN")
  1225. else
  1226. return lng.translate("Software VLAN")
  1227. end
  1228. elseif x == "tunnel" then
  1229. return lng.translate("Tunnel Interface")
  1230. else
  1231. return lng.translate("Ethernet Adapter")
  1232. end
  1233. end
  1234. function interface.adminlink(self)
  1235. if self.wif then
  1236. return self.wif:adminlink()
  1237. end
  1238. end
  1239. function interface.ports(self)
  1240. local members = self:_ubus("bridge-members")
  1241. if members then
  1242. local _, iface
  1243. local ifaces = { }
  1244. for _, iface in ipairs(members) do
  1245. ifaces[#ifaces+1] = interface(iface)
  1246. end
  1247. return ifaces
  1248. end
  1249. end
  1250. function interface.bridge_id(self)
  1251. if self.dev and self.dev.bridge then
  1252. return self.dev.bridge.id
  1253. else
  1254. return nil
  1255. end
  1256. end
  1257. function interface.bridge_stp(self)
  1258. if self.dev and self.dev.bridge then
  1259. return self.dev.bridge.stp
  1260. else
  1261. return false
  1262. end
  1263. end
  1264. function interface.is_up(self)
  1265. local up = self:_ubus("up")
  1266. if up == nil then
  1267. up = (self:type() == "alias")
  1268. end
  1269. return up or false
  1270. end
  1271. function interface.is_bridge(self)
  1272. return (self:type() == "bridge")
  1273. end
  1274. function interface.is_bridgeport(self)
  1275. return self.dev and self.dev.bridge and
  1276. (self.dev.bridge.name ~= self:name()) and true or false
  1277. end
  1278. function interface.tx_bytes(self)
  1279. local stat = self:_ubus("statistics")
  1280. return stat and stat.tx_bytes or 0
  1281. end
  1282. function interface.rx_bytes(self)
  1283. local stat = self:_ubus("statistics")
  1284. return stat and stat.rx_bytes or 0
  1285. end
  1286. function interface.tx_packets(self)
  1287. local stat = self:_ubus("statistics")
  1288. return stat and stat.tx_packets or 0
  1289. end
  1290. function interface.rx_packets(self)
  1291. local stat = self:_ubus("statistics")
  1292. return stat and stat.rx_packets or 0
  1293. end
  1294. function interface.get_network(self)
  1295. return self:get_networks()[1]
  1296. end
  1297. function interface.get_networks(self)
  1298. if not self.networks then
  1299. local nets = { }
  1300. local _, net
  1301. for _, net in ipairs(_M:get_networks()) do
  1302. if net:contains_interface(self.ifname) or
  1303. net:ifname() == self.ifname
  1304. then
  1305. nets[#nets+1] = net
  1306. end
  1307. end
  1308. table.sort(nets, function(a, b) return a.sid < b.sid end)
  1309. self.networks = nets
  1310. return nets
  1311. else
  1312. return self.networks
  1313. end
  1314. end
  1315. function interface.get_wifinet(self)
  1316. return self.wif
  1317. end
  1318. wifidev = utl.class()
  1319. function wifidev.__init__(self, name)
  1320. local t, n = _uci:get("wireless", name)
  1321. if t == "wifi-device" and n ~= nil then
  1322. self.sid = n
  1323. self.iwinfo = _wifi_iwinfo_by_ifname(self.sid, true)
  1324. end
  1325. self.sid = self.sid or name
  1326. self.iwinfo = self.iwinfo or { ifname = self.sid }
  1327. end
  1328. function wifidev.get(self, opt)
  1329. return _get("wireless", self.sid, opt)
  1330. end
  1331. function wifidev.set(self, opt, val)
  1332. return _set("wireless", self.sid, opt, val)
  1333. end
  1334. function wifidev.name(self)
  1335. return self.sid
  1336. end
  1337. function wifidev.hwmodes(self)
  1338. local l = self.iwinfo.hwmodelist
  1339. if l and next(l) then
  1340. return l
  1341. else
  1342. return { b = true, g = true }
  1343. end
  1344. end
  1345. function wifidev.get_i18n(self)
  1346. local t = self.iwinfo.hardware_name or "Generic"
  1347. if self.iwinfo.type == "wl" then
  1348. t = "Broadcom"
  1349. end
  1350. local m = ""
  1351. local l = self:hwmodes()
  1352. if l.a then m = m .. "a" end
  1353. if l.b then m = m .. "b" end
  1354. if l.g then m = m .. "g" end
  1355. if l.n then m = m .. "n" end
  1356. if l.ac then m = "ac" end
  1357. return "%s 802.11%s Wireless Controller (%s)" %{ t, m, self:name() }
  1358. end
  1359. function wifidev.is_up(self)
  1360. if _ubuswificache[self.sid] then
  1361. return (_ubuswificache[self.sid].up == true)
  1362. end
  1363. return false
  1364. end
  1365. function wifidev.get_wifinet(self, net)
  1366. if _uci:get("wireless", net) == "wifi-iface" then
  1367. return wifinet(net)
  1368. else
  1369. local wnet = _wifi_sid_by_ifname(net)
  1370. if wnet then
  1371. return wifinet(wnet)
  1372. end
  1373. end
  1374. end
  1375. function wifidev.get_wifinets(self)
  1376. local nets = { }
  1377. _uci:foreach("wireless", "wifi-iface",
  1378. function(s)
  1379. if s.device == self.sid then
  1380. nets[#nets+1] = wifinet(s['.name'])
  1381. end
  1382. end)
  1383. return nets
  1384. end
  1385. function wifidev.add_wifinet(self, options)
  1386. options = options or { }
  1387. options.device = self.sid
  1388. local wnet = _uci:section("wireless", "wifi-iface", nil, options)
  1389. if wnet then
  1390. return wifinet(wnet, options)
  1391. end
  1392. end
  1393. function wifidev.del_wifinet(self, net)
  1394. if utl.instanceof(net, wifinet) then
  1395. net = net.sid
  1396. elseif _uci:get("wireless", net) ~= "wifi-iface" then
  1397. net = _wifi_sid_by_ifname(net)
  1398. end
  1399. if net and _uci:get("wireless", net, "device") == self.sid then
  1400. _uci:delete("wireless", net)
  1401. return true
  1402. end
  1403. return false
  1404. end
  1405. wifinet = utl.class()
  1406. function wifinet.__init__(self, name, data)
  1407. local sid, netid, radioname, radiostate, netstate
  1408. -- lookup state by radio#.network# notation
  1409. sid = _wifi_sid_by_netid(name)
  1410. if sid then
  1411. netid = name
  1412. radioname, radiostate, netstate = _wifi_state_by_sid(sid)
  1413. else
  1414. -- lookup state by ifname (e.g. wlan0)
  1415. radioname, radiostate, netstate = _wifi_state_by_ifname(name)
  1416. if radioname and radiostate and netstate then
  1417. sid = netstate.section
  1418. netid = _wifi_netid_by_sid(sid)
  1419. else
  1420. -- lookup state by uci section id (e.g. cfg053579)
  1421. radioname, radiostate, netstate = _wifi_state_by_sid(name)
  1422. if radioname and radiostate and netstate then
  1423. sid = name
  1424. netid = _wifi_netid_by_sid(sid)
  1425. else
  1426. -- no state available, try to resolve from uci
  1427. netid, radioname = _wifi_netid_by_sid(name)
  1428. if netid and radioname then
  1429. sid = name
  1430. end
  1431. end
  1432. end
  1433. end
  1434. local iwinfo =
  1435. (netstate and _wifi_iwinfo_by_ifname(netstate.ifname)) or
  1436. (radioname and _wifi_iwinfo_by_ifname(radioname)) or
  1437. { ifname = (netid or sid or name) }
  1438. self.sid = sid or name
  1439. self.wdev = iwinfo.ifname
  1440. self.iwinfo = iwinfo
  1441. self.netid = netid
  1442. self._ubusdata = {
  1443. radio = radioname,
  1444. dev = radiostate,
  1445. net = netstate
  1446. }
  1447. end
  1448. function wifinet.ubus(self, ...)
  1449. local n, v = self._ubusdata
  1450. for n = 1, select('#', ...) do
  1451. if type(v) == "table" then
  1452. v = v[select(n, ...)]
  1453. else
  1454. return nil
  1455. end
  1456. end
  1457. return v
  1458. end
  1459. function wifinet.get(self, opt)
  1460. return _get("wireless", self.sid, opt)
  1461. end
  1462. function wifinet.set(self, opt, val)
  1463. return _set("wireless", self.sid, opt, val)
  1464. end
  1465. function wifinet.mode(self)
  1466. return self:ubus("net", "config", "mode") or self:get("mode") or "ap"
  1467. end
  1468. function wifinet.ssid(self)
  1469. return self:ubus("net", "config", "ssid") or self:get("ssid")
  1470. end
  1471. function wifinet.bssid(self)
  1472. return self:ubus("net", "config", "bssid") or self:get("bssid")
  1473. end
  1474. function wifinet.network(self)
  1475. local net, networks = nil, { }
  1476. for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
  1477. networks[#networks+1] = net
  1478. end
  1479. return networks
  1480. end
  1481. function wifinet.id(self)
  1482. return self.netid
  1483. end
  1484. function wifinet.name(self)
  1485. return self.sid
  1486. end
  1487. function wifinet.ifname(self)
  1488. local ifname = self:ubus("net", "ifname") or self.iwinfo.ifname
  1489. if not ifname or ifname:match("^wifi%d") or ifname:match("^radio%d") then
  1490. ifname = self.netid
  1491. end
  1492. return ifname
  1493. end
  1494. function wifinet.get_device(self)
  1495. local dev = self:ubus("radio") or self:get("device")
  1496. return dev and wifidev(dev) or nil
  1497. end
  1498. function wifinet.is_up(self)
  1499. local ifc = self:get_interface()
  1500. return (ifc and ifc:is_up() or false)
  1501. end
  1502. function wifinet.active_mode(self)
  1503. local m = self.iwinfo.mode or self:ubus("net", "config", "mode") or self:get("mode") or "ap"
  1504. if m == "ap" then m = "Master"
  1505. elseif m == "sta" then m = "Client"
  1506. elseif m == "adhoc" then m = "Ad-Hoc"
  1507. elseif m == "mesh" then m = "Mesh Point"
  1508. elseif m == "monitor" then m = "Monitor"
  1509. end
  1510. return m
  1511. end
  1512. function wifinet.active_mode_i18n(self)
  1513. return lng.translate(self:active_mode())
  1514. end
  1515. function wifinet.active_ssid(self)
  1516. return self.iwinfo.ssid or self:ubus("net", "config", "ssid") or self:get("ssid")
  1517. end
  1518. function wifinet.active_bssid(self)
  1519. return self.iwinfo.bssid or self:ubus("net", "config", "bssid") or self:get("bssid")
  1520. end
  1521. function wifinet.active_encryption(self)
  1522. local enc = self.iwinfo and self.iwinfo.encryption
  1523. return enc and enc.description or "-"
  1524. end
  1525. function wifinet.assoclist(self)
  1526. return self.iwinfo.assoclist or { }
  1527. end
  1528. function wifinet.frequency(self)
  1529. local freq = self.iwinfo.frequency
  1530. if freq and freq > 0 then
  1531. return "%.03f" % (freq / 1000)
  1532. end
  1533. end
  1534. function wifinet.bitrate(self)
  1535. local rate = self.iwinfo.bitrate
  1536. if rate and rate > 0 then
  1537. return (rate / 1000)
  1538. end
  1539. end
  1540. function wifinet.channel(self)
  1541. return self.iwinfo.channel or self:ubus("dev", "config", "channel") or
  1542. tonumber(self:get("channel"))
  1543. end
  1544. function wifinet.signal(self)
  1545. return self.iwinfo.signal or 0
  1546. end
  1547. function wifinet.noise(self)
  1548. return self.iwinfo.noise or 0
  1549. end
  1550. function wifinet.country(self)
  1551. return self.iwinfo.country or self:ubus("dev", "config", "country") or "00"
  1552. end
  1553. function wifinet.txpower(self)
  1554. local pwr = (self.iwinfo.txpower or 0)
  1555. return pwr + self:txpower_offset()
  1556. end
  1557. function wifinet.txpower_offset(self)
  1558. return self.iwinfo.txpower_offset or 0
  1559. end
  1560. function wifinet.signal_level(self, s, n)
  1561. if self:active_bssid() ~= "00:00:00:00:00:00" then
  1562. local signal = s or self:signal()
  1563. local noise = n or self:noise()
  1564. if signal < 0 and noise < 0 then
  1565. local snr = -1 * (noise - signal)
  1566. return math.floor(snr / 5)
  1567. else
  1568. return 0
  1569. end
  1570. else
  1571. return -1
  1572. end
  1573. end
  1574. function wifinet.signal_percent(self)
  1575. local qc = self.iwinfo.quality or 0
  1576. local qm = self.iwinfo.quality_max or 0
  1577. if qc > 0 and qm > 0 then
  1578. return math.floor((100 / qm) * qc)
  1579. else
  1580. return 0
  1581. end
  1582. end
  1583. function wifinet.shortname(self)
  1584. return "%s %q" %{
  1585. lng.translate(self:active_mode()),
  1586. self:active_ssid() or self:active_bssid() or self:id()
  1587. }
  1588. end
  1589. function wifinet.get_i18n(self)
  1590. return "%s: %s %q (%s)" %{
  1591. lng.translate("Wireless Network"),
  1592. lng.translate(self:active_mode()),
  1593. self:active_ssid() or self:active_bssid() or self:id(),
  1594. self:ifname()
  1595. }
  1596. end
  1597. function wifinet.adminlink(self)
  1598. local stat, dsp = pcall(require, "luci.dispatcher")
  1599. return dsp and dsp.build_url("admin", "network", "wireless", self.netid)
  1600. end
  1601. function wifinet.get_network(self)
  1602. return self:get_networks()[1]
  1603. end
  1604. function wifinet.get_networks(self)
  1605. local nets = { }
  1606. local net
  1607. for net in utl.imatch(self:ubus("net", "config", "network") or self:get("network")) do
  1608. if _uci:get("network", net) == "interface" then
  1609. nets[#nets+1] = network(net)
  1610. end
  1611. end
  1612. table.sort(nets, function(a, b) return a.sid < b.sid end)
  1613. return nets
  1614. end
  1615. function wifinet.get_interface(self)
  1616. return interface(self:ifname())
  1617. end
  1618. -- setup base protocols
  1619. _M:register_protocol("static")
  1620. _M:register_protocol("dhcp")
  1621. _M:register_protocol("none")
  1622. -- load protocol extensions
  1623. local exts = nfs.dir(utl.libpath() .. "/model/network")
  1624. if exts then
  1625. local ext
  1626. for ext in exts do
  1627. if ext:match("%.lua$") then
  1628. require("luci.model.network." .. ext:gsub("%.lua$", ""))
  1629. end
  1630. end
  1631. end