util.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 exec(...)
  34. return os.execute(escape_args('', 'exec', ...))
  35. end
  36. -- Removes all lines starting with a prefix from a file, optionally adding a new one
  37. function replace_prefix(file, prefix, add)
  38. local tmp = file .. '.tmp'
  39. local f = do_filter_prefix(file, tmp, prefix)
  40. if add then
  41. f:write(add)
  42. end
  43. f:close()
  44. os.rename(tmp, file)
  45. end
  46. function readline(fd)
  47. local line = fd:read('*l')
  48. fd:close()
  49. return line
  50. end
  51. function lock(file)
  52. exec('lock', file)
  53. end
  54. function unlock(file)
  55. exec('lock', '-u', file)
  56. end
  57. function node_id()
  58. return string.gsub(sysconfig.primary_mac, ':', '')
  59. end
  60. local function find_phy_by_path(path)
  61. for phy in fs.glob('/sys/devices/' .. path .. '/ieee80211/phy*') do
  62. return phy:match('([^/]+)$')
  63. end
  64. end
  65. local function find_phy_by_macaddr(macaddr)
  66. local addr = macaddr:lower()
  67. for file in fs.glob('/sys/class/ieee80211/*/macaddress') do
  68. if lutil.trim(fs.readfile(file)) == addr then
  69. return file:match('([^/]+)/macaddress$')
  70. end
  71. end
  72. end
  73. local function find_phy(radio)
  74. local config = uci:get_all('wireless', radio)
  75. if not config or config.type ~= 'mac80211' then
  76. return nil
  77. elseif config.path then
  78. return find_phy_by_path(config.path)
  79. elseif config.macaddr then
  80. return find_phy_by_macaddr(config.macaddr)
  81. else
  82. return nil
  83. end
  84. end
  85. local function get_addresses(radio)
  86. local phy = find_phy(radio)
  87. if not phy then
  88. return function() end
  89. end
  90. return io.lines('/sys/class/ieee80211/' .. phy .. '/addresses')
  91. end
  92. -- Generates a (hopefully) unique MAC address
  93. -- The parameter defines the ID to add to the MAC address
  94. --
  95. -- IDs defined so far:
  96. -- 0: client0; WAN
  97. -- 1: mesh0
  98. -- 2: ibss0
  99. -- 3: wan_radio0 (private WLAN); batman-adv primary address
  100. -- 4: client1; LAN
  101. -- 5: mesh1
  102. -- 6: ibss1
  103. -- 7: wan_radio1 (private WLAN); mesh VPN
  104. function generate_mac(i)
  105. if i > 7 or i < 0 then return nil end -- max allowed id (0b111)
  106. local hashed = string.sub(hash.md5(sysconfig.primary_mac), 0, 12)
  107. local m1, m2, m3, m4, m5, m6 = string.match(hashed, '(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)')
  108. m1 = tonumber(m1, 16)
  109. m6 = tonumber(m6, 16)
  110. m1 = nixio.bit.bor(m1, 0x02) -- set locally administered bit
  111. m1 = nixio.bit.band(m1, 0xFE) -- unset the multicast bit
  112. -- It's necessary that the first 45 bits of the MAC address don't
  113. -- vary on a single hardware interface, since some chips are using
  114. -- a hardware MAC filter. (e.g 'rt305x')
  115. m6 = nixio.bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting)
  116. m6 = m6 + i -- add virtual interface id
  117. return string.format('%02x:%s:%s:%s:%s:%02x', m1, m2, m3, m4, m5, m6)
  118. end
  119. local function get_wlan_mac_from_driver(radio, vif)
  120. local primary = sysconfig.primary_mac:lower()
  121. local i = 1
  122. for addr in get_addresses(radio) do
  123. if addr:lower() ~= primary then
  124. if i == vif then
  125. return addr
  126. end
  127. i = i + 1
  128. end
  129. end
  130. end
  131. function get_wlan_mac(radio, index, vif)
  132. local addr = get_wlan_mac_from_driver(radio, vif)
  133. if addr then
  134. return addr
  135. end
  136. return generate_mac(4*(index-1) + (vif-1))
  137. end
  138. -- Iterate over all radios defined in UCI calling
  139. -- f(radio, index, site.wifiX) for each radio found while passing
  140. -- site.wifi24 for 2.4 GHz devices and site.wifi5 for 5 GHz ones.
  141. function iterate_radios(f)
  142. local radios = {}
  143. uci:foreach('wireless', 'wifi-device',
  144. function(s)
  145. table.insert(radios, s['.name'])
  146. end
  147. )
  148. for index, radio in ipairs(radios) do
  149. local hwmode = uci:get('wireless', radio, 'hwmode')
  150. if hwmode == '11g' or hwmode == '11ng' then
  151. f(radio, index, site.wifi24)
  152. elseif hwmode == '11a' or hwmode == '11na' then
  153. f(radio, index, site.wifi5)
  154. end
  155. end
  156. end