200-wireless 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/usr/bin/lua
  2. local util = require 'gluon.util'
  3. local site = require 'gluon.site'
  4. local sysconfig = require 'gluon.sysconfig'
  5. local uci = require('simple-uci').cursor()
  6. -- Initial
  7. if not sysconfig.gluon_version then
  8. uci:delete_all('wireless', 'wifi-iface')
  9. end
  10. local function get_channel(radio, config)
  11. local channel
  12. if uci:get_first('gluon-core', 'wireless', 'preserve_channels') then
  13. channel = radio.channel
  14. end
  15. return channel or config.channel()
  16. end
  17. local function is_disabled(name)
  18. if uci:get('wireless', name) then
  19. return uci:get_bool('wireless', name, 'disabled')
  20. else
  21. return false
  22. end
  23. end
  24. -- Returns the first argument that is not nil; don't call without any non-nil arguments!
  25. local function first_non_nil(first, ...)
  26. if first ~= nil then
  27. return first
  28. else
  29. return first_non_nil(...)
  30. end
  31. end
  32. local function configure_ibss(config, radio, index, suffix, disabled)
  33. local radio_name = radio['.name']
  34. local name = 'ibss_' .. radio_name
  35. uci:delete('network', name)
  36. uci:delete('network', name .. '_vlan')
  37. uci:delete('wireless', name)
  38. if not config then
  39. return
  40. end
  41. local macaddr = util.get_wlan_mac(uci, radio, index, 3)
  42. if not macaddr then
  43. return
  44. end
  45. if config.vlan then
  46. uci:section('network', 'interface', name, {
  47. proto = 'none',
  48. })
  49. uci:section('network', 'interface', name .. '_vlan', {
  50. ifname = '@' .. name .. '.' .. config.vlan,
  51. proto = 'gluon_mesh',
  52. })
  53. else
  54. uci:section('network', 'interface', name, {
  55. proto = 'gluon_mesh',
  56. })
  57. end
  58. uci:section('wireless', 'wifi-iface', name, {
  59. device = radio_name,
  60. network = name,
  61. mode = 'adhoc',
  62. ssid = config.ssid,
  63. bssid = config.bssid,
  64. macaddr = macaddr,
  65. mcast_rate = config.mcast_rate,
  66. ifname = suffix and 'ibss' .. suffix,
  67. disabled = disabled,
  68. })
  69. end
  70. local function configure_mesh(config, radio, index, suffix, disabled)
  71. local radio_name = radio['.name']
  72. local name = 'mesh_' .. radio_name
  73. local macfilter = uci:get('wireless', name, 'macfilter')
  74. local maclist = uci:get('wireless', name, 'maclist')
  75. uci:delete('network', name)
  76. uci:delete('network', name .. '_vlan')
  77. uci:delete('wireless', name)
  78. if not config then
  79. return
  80. end
  81. local macaddr = util.get_wlan_mac(uci, radio, index, 2)
  82. if not macaddr then
  83. return
  84. end
  85. uci:section('network', 'interface', name, {
  86. proto = 'gluon_mesh',
  87. })
  88. uci:section('wireless', 'wifi-iface', name, {
  89. device = radio_name,
  90. network = name,
  91. mode = 'mesh',
  92. mesh_id = config.id,
  93. mesh_fwding = false,
  94. macaddr = macaddr,
  95. mcast_rate = config.mcast_rate,
  96. ifname = suffix and 'mesh' .. suffix,
  97. disabled = disabled,
  98. macfilter = macfilter,
  99. maclist = maclist,
  100. })
  101. end
  102. local function fixup_wan(radio, index)
  103. local radio_name = radio['.name']
  104. local name = 'wan_' .. radio_name
  105. if not uci:get('wireless', name) then
  106. return
  107. end
  108. local macaddr = util.get_wlan_mac(uci, radio, index, 4)
  109. if not macaddr then
  110. return
  111. end
  112. uci:set('wireless', name, 'macaddr', macaddr)
  113. end
  114. util.foreach_radio(uci, function(radio, index, config)
  115. local radio_name = radio['.name']
  116. if not config() then
  117. uci:set('wireless', radio_name, 'disabled', true)
  118. return
  119. end
  120. local suffix = radio_name:match('^radio(%d+)$')
  121. if not suffix then
  122. return
  123. end
  124. local channel = get_channel(radio, config)
  125. uci:delete('wireless', radio_name, 'disabled')
  126. uci:set('wireless', radio_name, 'channel', channel)
  127. uci:set('wireless', radio_name, 'htmode', 'HT20')
  128. uci:set('wireless', radio_name, 'country', site.regdom())
  129. uci:set_list('wireless', radio_name, 'supported_rates', config.supported_rates())
  130. uci:set_list('wireless', radio_name, 'basic_rate', config.basic_rate())
  131. local ibss_disabled = is_disabled('ibss_' .. radio_name)
  132. local mesh_disabled = is_disabled('mesh_' .. radio_name)
  133. configure_ibss(config.ibss(), radio, index, suffix,
  134. first_non_nil(
  135. ibss_disabled,
  136. mesh_disabled,
  137. config.ibss.disabled(false)
  138. )
  139. )
  140. configure_mesh(config.mesh(), radio, index, suffix,
  141. first_non_nil(
  142. mesh_disabled,
  143. ibss_disabled,
  144. config.mesh.disabled(false)
  145. )
  146. )
  147. fixup_wan(radio, index)
  148. end)
  149. if uci:get('system', 'rssid_wlan0') then
  150. if uci:get('wireless', 'mesh_radio0') then
  151. uci:set('system', 'rssid_wlan0', 'dev', 'mesh0')
  152. else
  153. uci:set('system', 'rssid_wlan0', 'dev', 'ibss0')
  154. end
  155. uci:save('system')
  156. end
  157. uci:save('wireless')
  158. uci:save('network')