util.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. -- Writes all lines from the file input to the file output except those starting with prefix
  2. -- Doesn't close the output file, but returns the file object
  3. local function do_filter_prefix(input, output, prefix)
  4. local f = io.open(output, 'w+')
  5. local l = prefix:len()
  6. for line in io.lines(input) do
  7. if line:sub(1, l) ~= prefix then
  8. f:write(line, '\n')
  9. end
  10. end
  11. return f
  12. end
  13. local function escape_args(ret, arg0, ...)
  14. if not arg0 then
  15. return ret
  16. end
  17. return escape_args(ret .. "'" .. string.gsub(arg0, "'", "'\\''") .. "' ", ...)
  18. end
  19. local io = io
  20. local os = os
  21. local string = string
  22. local tonumber = tonumber
  23. local ipairs = ipairs
  24. local table = table
  25. local nixio = require 'nixio'
  26. local hash = require 'hash'
  27. local sysconfig = require 'gluon.sysconfig'
  28. local site = require 'gluon.site_config'
  29. local uci = require('luci.model.uci').cursor()
  30. local lutil = require 'luci.util'
  31. local fs = require 'nixio.fs'
  32. module 'gluon.util'
  33. function add_to_set(t, itm)
  34. for _,v in ipairs(t) do
  35. if v == itm then return false end
  36. end
  37. table.insert(t, itm)
  38. return true
  39. end
  40. function remove_from_set(t, itm)
  41. local i = 1
  42. local changed = false
  43. while i <= #t do
  44. if t[i] == itm then
  45. table.remove(t, i)
  46. changed = true
  47. else
  48. i = i + 1
  49. end
  50. end
  51. return changed
  52. end
  53. function exec(...)
  54. return os.execute(escape_args('', 'exec', ...))
  55. end
  56. -- Removes all lines starting with a prefix from a file, optionally adding a new one
  57. function replace_prefix(file, prefix, add)
  58. local tmp = file .. '.tmp'
  59. local f = do_filter_prefix(file, tmp, prefix)
  60. if add then
  61. f:write(add)
  62. end
  63. f:close()
  64. os.rename(tmp, file)
  65. end
  66. function readline(fd)
  67. local line = fd:read('*l')
  68. fd:close()
  69. return line
  70. end
  71. function lock(file)
  72. exec('lock', file)
  73. end
  74. function unlock(file)
  75. exec('lock', '-u', file)
  76. end
  77. function node_id()
  78. return string.gsub(sysconfig.primary_mac, ':', '')
  79. end
  80. function get_mesh_devices(uconn)
  81. local dump = uconn:call("network.interface", "dump", {})
  82. local devices = {}
  83. for _, interface in ipairs(dump.interface) do
  84. if ( (interface.proto == "gluon_mesh") and interface.up ) then
  85. table.insert(devices, interface.device)
  86. end
  87. end
  88. return devices
  89. end
  90. local function find_phy_by_path(path)
  91. for phy in fs.glob('/sys/devices/' .. path .. '/ieee80211/phy*') do
  92. return phy:match('([^/]+)$')
  93. end
  94. end
  95. local function find_phy_by_macaddr(macaddr)
  96. local addr = macaddr:lower()
  97. for file in fs.glob('/sys/class/ieee80211/*/macaddress') do
  98. if lutil.trim(fs.readfile(file)) == addr then
  99. return file:match('([^/]+)/macaddress$')
  100. end
  101. end
  102. end
  103. local function find_phy(radio)
  104. local config = uci:get_all('wireless', radio)
  105. if not config or config.type ~= 'mac80211' then
  106. return nil
  107. elseif config.path then
  108. return find_phy_by_path(config.path)
  109. elseif config.macaddr then
  110. return find_phy_by_macaddr(config.macaddr)
  111. else
  112. return nil
  113. end
  114. end
  115. local function get_addresses(radio)
  116. local phy = find_phy(radio)
  117. if not phy then
  118. return function() end
  119. end
  120. return io.lines('/sys/class/ieee80211/' .. phy .. '/addresses')
  121. end
  122. -- Generates a (hopefully) unique MAC address
  123. -- The parameter defines the ID to add to the MAC address
  124. --
  125. -- IDs defined so far:
  126. -- 0: client0; WAN
  127. -- 1: mesh0
  128. -- 2: ibss0
  129. -- 3: wan_radio0 (private WLAN); batman-adv primary address
  130. -- 4: client1; LAN
  131. -- 5: mesh1
  132. -- 6: ibss1
  133. -- 7: wan_radio1 (private WLAN); mesh VPN
  134. function generate_mac(i)
  135. if i > 7 or i < 0 then return nil end -- max allowed id (0b111)
  136. local hashed = string.sub(hash.md5(sysconfig.primary_mac), 0, 12)
  137. local m1, m2, m3, m4, m5, m6 = string.match(hashed, '(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)')
  138. m1 = tonumber(m1, 16)
  139. m6 = tonumber(m6, 16)
  140. m1 = nixio.bit.bor(m1, 0x02) -- set locally administered bit
  141. m1 = nixio.bit.band(m1, 0xFE) -- unset the multicast bit
  142. -- It's necessary that the first 45 bits of the MAC address don't
  143. -- vary on a single hardware interface, since some chips are using
  144. -- a hardware MAC filter. (e.g 'rt305x')
  145. m6 = nixio.bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting)
  146. m6 = m6 + i -- add virtual interface id
  147. return string.format('%02x:%s:%s:%s:%s:%02x', m1, m2, m3, m4, m5, m6)
  148. end
  149. local function get_wlan_mac_from_driver(radio, vif)
  150. local primary = sysconfig.primary_mac:lower()
  151. local i = 1
  152. for addr in get_addresses(radio) do
  153. if addr:lower() ~= primary then
  154. if i == vif then
  155. return addr
  156. end
  157. i = i + 1
  158. end
  159. end
  160. end
  161. function get_wlan_mac(radio, index, vif)
  162. local addr = get_wlan_mac_from_driver(radio, vif)
  163. if addr then
  164. return addr
  165. end
  166. return generate_mac(4*(index-1) + (vif-1))
  167. end
  168. -- Iterate over all radios defined in UCI calling
  169. -- f(radio, index, site.wifiX) for each radio found while passing
  170. -- site.wifi24 for 2.4 GHz devices and site.wifi5 for 5 GHz ones.
  171. function iterate_radios(f)
  172. local radios = {}
  173. uci:foreach('wireless', 'wifi-device',
  174. function(s)
  175. table.insert(radios, s['.name'])
  176. end
  177. )
  178. for index, radio in ipairs(radios) do
  179. local hwmode = uci:get('wireless', radio, 'hwmode')
  180. if hwmode == '11g' or hwmode == '11ng' then
  181. f(radio, index, site.wifi24)
  182. elseif hwmode == '11a' or hwmode == '11na' then
  183. f(radio, index, site.wifi5)
  184. end
  185. end
  186. end