200-wireless 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 = uci:get('wireless', 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 name = 'ibss_' .. radio
  34. uci:delete('network', name)
  35. uci:delete('network', name .. '_vlan')
  36. uci:delete('wireless', name)
  37. if not config then
  38. return
  39. end
  40. local macaddr = util.get_wlan_mac(uci, radio, index, 3)
  41. if not macaddr then
  42. return
  43. end
  44. if config.vlan then
  45. uci:section('network', 'interface', name, {
  46. proto = 'none',
  47. })
  48. uci:section('network', 'interface', name .. '_vlan', {
  49. ifname = '@' .. name .. '.' .. config.vlan,
  50. proto = 'gluon_mesh',
  51. })
  52. else
  53. uci:section('network', 'interface', name, {
  54. proto = 'gluon_mesh',
  55. })
  56. end
  57. uci:section('wireless', 'wifi-iface', name, {
  58. device = radio,
  59. network = name,
  60. mode = 'adhoc',
  61. ssid = config.ssid,
  62. bssid = config.bssid,
  63. macaddr = macaddr,
  64. mcast_rate = config.mcast_rate,
  65. ifname = suffix and 'ibss' .. suffix,
  66. disabled = disabled,
  67. })
  68. end
  69. local function configure_mesh(config, radio, index, suffix, disabled)
  70. local name = 'mesh_' .. radio
  71. local macfilter = uci:get('wireless', name, 'macfilter')
  72. local maclist = uci:get('wireless', name, 'maclist')
  73. uci:delete('network', name)
  74. uci:delete('network', name .. '_vlan')
  75. uci:delete('wireless', name)
  76. if not config then
  77. return
  78. end
  79. local macaddr = util.get_wlan_mac(uci, radio, index, 2)
  80. if not macaddr then
  81. return
  82. end
  83. uci:section('network', 'interface', name, {
  84. proto = 'gluon_mesh',
  85. })
  86. uci:section('wireless', 'wifi-iface', name, {
  87. device = radio,
  88. network = name,
  89. mode = 'mesh',
  90. mesh_id = config.id,
  91. mesh_fwding = false,
  92. macaddr = macaddr,
  93. mcast_rate = config.mcast_rate,
  94. ifname = suffix and 'mesh' .. suffix,
  95. disabled = disabled,
  96. macfilter = macfilter,
  97. maclist = maclist,
  98. })
  99. end
  100. local function fixup_wan(radio, index)
  101. local name = 'wan_' .. radio
  102. if not uci:get('wireless', name) then
  103. return
  104. end
  105. local macaddr = util.get_wlan_mac(uci, radio, index, 4)
  106. if not macaddr then
  107. return
  108. end
  109. uci:set('wireless', name, 'macaddr', macaddr)
  110. end
  111. local function configure_radio(radio, index, config)
  112. if not config() then
  113. return
  114. end
  115. local suffix = radio:match('^radio(%d+)$')
  116. if not suffix then
  117. return
  118. end
  119. local channel = get_channel(radio, config)
  120. uci:delete('wireless', radio, 'disabled')
  121. uci:set('wireless', radio, 'channel', channel)
  122. uci:set('wireless', radio, 'htmode', 'HT20')
  123. uci:set('wireless', radio, 'country', site.regdom())
  124. uci:set_list('wireless', radio, 'supported_rates', config.supported_rates())
  125. uci:set_list('wireless', radio, 'basic_rate', config.basic_rate())
  126. local ibss_disabled = is_disabled('ibss_' .. radio)
  127. local mesh_disabled = is_disabled('mesh_' .. radio)
  128. configure_ibss(config.ibss(), radio, index, suffix,
  129. first_non_nil(
  130. ibss_disabled,
  131. mesh_disabled,
  132. config.ibss.disabled(false)
  133. )
  134. )
  135. configure_mesh(config.mesh(), radio, index, suffix,
  136. first_non_nil(
  137. mesh_disabled,
  138. ibss_disabled,
  139. config.mesh.disabled(false)
  140. )
  141. )
  142. fixup_wan(radio, index)
  143. end
  144. util.iterate_radios(uci, configure_radio)
  145. if uci:get('system', 'rssid_wlan0') then
  146. if uci:get('wireless', 'mesh_radio0') then
  147. uci:set('system', 'rssid_wlan0', 'dev', 'mesh0')
  148. else
  149. uci:set('system', 'rssid_wlan0', 'dev', 'ibss0')
  150. end
  151. uci:save('system')
  152. end
  153. uci:save('wireless')
  154. uci:save('network')