network.lua 41 KB

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