gluon-core: the mac address generation schema now uses a single parameter.
This commit is contained in:
		
							parent
							
								
									16b14c5e57
								
							
						
					
					
						commit
						f6592e720c
					
				@ -21,6 +21,12 @@ local function configure_client(config, radio, index, suffix)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  uci:delete('wireless', name)
 | 
					  uci:delete('wireless', name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if index == 0 then
 | 
				
			||||||
 | 
					    macaddr = util.generate_mac(0)
 | 
				
			||||||
 | 
					  elseif index == 1 then
 | 
				
			||||||
 | 
					    macaddr = util.generate_mac(3)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if config then
 | 
					  if config then
 | 
				
			||||||
    uci:section('wireless', 'wifi-iface', name,
 | 
					    uci:section('wireless', 'wifi-iface', name,
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
@ -28,7 +34,7 @@ local function configure_client(config, radio, index, suffix)
 | 
				
			|||||||
        network = 'client',
 | 
					        network = 'client',
 | 
				
			||||||
        mode = 'ap',
 | 
					        mode = 'ap',
 | 
				
			||||||
        ssid = config.ssid,
 | 
					        ssid = config.ssid,
 | 
				
			||||||
        macaddr = util.generate_mac(2, index),
 | 
					        macaddr = macaddr,
 | 
				
			||||||
        ifname = suffix and 'client' .. suffix,
 | 
					        ifname = suffix and 'client' .. suffix,
 | 
				
			||||||
        disabled = disabled,
 | 
					        disabled = disabled,
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
@ -72,35 +72,36 @@ function node_id()
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Generates a (hopefully) unique MAC address
 | 
					-- Generates a (hopefully) unique MAC address
 | 
				
			||||||
-- The first parameter defines the function and the second
 | 
					-- The parameter defines the ID to add to the mac addr
 | 
				
			||||||
-- parameter an ID to add to the MAC address
 | 
					--
 | 
				
			||||||
-- Functions and IDs defined so far:
 | 
					-- IDs defined so far:
 | 
				
			||||||
-- (1, 0): WAN (for mesh-on-WAN)
 | 
					-- 0: client0; mesh-vpn
 | 
				
			||||||
-- (1, 1): LAN (for mesh-on-LAN)
 | 
					-- 1: mesh0
 | 
				
			||||||
-- (2, n): client interface for the n'th radio
 | 
					-- 2: ibss0
 | 
				
			||||||
-- (3, n): adhoc interface for n'th radio
 | 
					-- 3: client1; mesh-on-wan
 | 
				
			||||||
-- (4, 0): mesh VPN
 | 
					-- 4: mesh1
 | 
				
			||||||
-- (5, n): mesh interface for n'th radio (802.11s)
 | 
					-- 5: ibss1
 | 
				
			||||||
function generate_mac(f, i)
 | 
					-- 6: mesh-on-lan
 | 
				
			||||||
 | 
					-- 7: unused
 | 
				
			||||||
 | 
					function generate_mac(i)
 | 
				
			||||||
  local hashed = string.sub(hash.md5(sysconfig.primary_mac), 0, 12)
 | 
					  local hashed = string.sub(hash.md5(sysconfig.primary_mac), 0, 12)
 | 
				
			||||||
  local m1, m2, m3, m4, m5, m6 = string.match(hashed, '(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)')
 | 
					  local m1, m2, m3, m4, m5, m6 = string.match(hashed, '(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  m1 = tonumber(m1, 16)
 | 
					  m1 = tonumber(m1, 16)
 | 
				
			||||||
  m3 = tonumber(m3, 16)
 | 
					 | 
				
			||||||
  m6 = tonumber(m6, 16)
 | 
					  m6 = tonumber(m6, 16)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  m1 = nixio.bit.bor(m1, 0x02)  -- set locally administered bit
 | 
					  m1 = nixio.bit.bor(m1, 0x02)  -- set locally administered bit
 | 
				
			||||||
  m1 = nixio.bit.band(m1, 0xFE) -- unset the multicast bit
 | 
					  m1 = nixio.bit.band(m1, 0xFE) -- unset the multicast bit
 | 
				
			||||||
  m3 = (m3+i) % 0x100           -- add interface identifier
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  -- It's necessary that the last bits of the mac do
 | 
					  -- It's necessary that the last bits of the mac do
 | 
				
			||||||
  -- not vary on a single interface, since some chips are using
 | 
					  -- not vary on a single interface, since some chips are using
 | 
				
			||||||
  -- a hardware mac filter. (e.g 'ramips-rt305x')
 | 
					  -- a hardware mac filter. (e.g 'ramips-rt305x')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  i = i % 0x08                  -- max allowed value is 0x07
 | 
				
			||||||
  m6 = nixio.bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting)
 | 
					  m6 = nixio.bit.band(m6, 0xF8) -- zero the last three bits (space needed for counting)
 | 
				
			||||||
  m6 = (m6+f) % 0x100           -- add virtual interface id
 | 
					  m6 = m6 + i                   -- add virtual interface id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return string.format('%02x:%s:%02x:%s:%s:%02x', m1, m2, m3, m4, m5, m6)
 | 
					  return string.format('%02x:%s:%s:%s:%s:%02x', m1, m2, m3, m4, m5, m6)
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Iterate over all radios defined in UCI calling
 | 
					-- Iterate over all radios defined in UCI calling
 | 
				
			||||||
 | 
				
			|||||||
@ -5,7 +5,7 @@ local uci = require('luci.model.uci').cursor()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- fix up duplicate mac addresses (for mesh-on-WAN)
 | 
					-- fix up duplicate mac addresses (for mesh-on-WAN)
 | 
				
			||||||
uci:set('network', 'wan', 'macaddr', util.generate_mac(1, 0))
 | 
					uci:set('network', 'wan', 'macaddr', util.generate_mac(3))
 | 
				
			||||||
uci:save('network')
 | 
					uci:save('network')
 | 
				
			||||||
uci:commit('network')
 | 
					uci:commit('network')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -53,6 +53,12 @@ local function configure_ibss(config, radio, index, suffix, disabled)
 | 
				
			|||||||
      )
 | 
					      )
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if index == 0 then
 | 
				
			||||||
 | 
					      macaddr = util.generate_mac(2)
 | 
				
			||||||
 | 
					    elseif index == 1 then
 | 
				
			||||||
 | 
					      macaddr = util.generate_mac(5)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uci:section('wireless', 'wifi-iface', name,
 | 
					    uci:section('wireless', 'wifi-iface', name,
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        device = radio,
 | 
					        device = radio,
 | 
				
			||||||
@ -60,7 +66,7 @@ local function configure_ibss(config, radio, index, suffix, disabled)
 | 
				
			|||||||
        mode = 'adhoc',
 | 
					        mode = 'adhoc',
 | 
				
			||||||
        ssid = config.ssid,
 | 
					        ssid = config.ssid,
 | 
				
			||||||
        bssid = config.bssid,
 | 
					        bssid = config.bssid,
 | 
				
			||||||
        macaddr = util.generate_mac(3, index),
 | 
					        macaddr = macaddr,
 | 
				
			||||||
        mcast_rate = config.mcast_rate,
 | 
					        mcast_rate = config.mcast_rate,
 | 
				
			||||||
        ifname = suffix and 'ibss' .. suffix,
 | 
					        ifname = suffix and 'ibss' .. suffix,
 | 
				
			||||||
        disabled = disabled and 1 or 0,
 | 
					        disabled = disabled and 1 or 0,
 | 
				
			||||||
@ -85,6 +91,12 @@ local function configure_mesh(config, radio, index, suffix, disabled)
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if index == 0 then
 | 
				
			||||||
 | 
					      macaddr = util.generate_mac(1)
 | 
				
			||||||
 | 
					    elseif index == 1 then
 | 
				
			||||||
 | 
					      macaddr = util.generate_mac(4)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uci:section('wireless', 'wifi-iface', name,
 | 
					    uci:section('wireless', 'wifi-iface', name,
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        device = radio,
 | 
					        device = radio,
 | 
				
			||||||
@ -92,7 +104,7 @@ local function configure_mesh(config, radio, index, suffix, disabled)
 | 
				
			|||||||
        mode = 'mesh',
 | 
					        mode = 'mesh',
 | 
				
			||||||
        mesh_id = config.id,
 | 
					        mesh_id = config.id,
 | 
				
			||||||
        mesh_fwding = 0,
 | 
					        mesh_fwding = 0,
 | 
				
			||||||
        macaddr = util.generate_mac(5, index),
 | 
					        macaddr = macaddr,
 | 
				
			||||||
        mcast_rate = config.mcast_rate,
 | 
					        mcast_rate = config.mcast_rate,
 | 
				
			||||||
        ifname = suffix and 'mesh' .. suffix,
 | 
					        ifname = suffix and 'mesh' .. suffix,
 | 
				
			||||||
        disabled = disabled and 1 or 0,
 | 
					        disabled = disabled and 1 or 0,
 | 
				
			||||||
 | 
				
			|||||||
@ -28,7 +28,7 @@ if sysconfig.lan_ifname and not uci:get('network', 'mesh_lan') then
 | 
				
			|||||||
              , proto   = 'batadv'
 | 
					              , proto   = 'batadv'
 | 
				
			||||||
              , mesh    = 'bat0'
 | 
					              , mesh    = 'bat0'
 | 
				
			||||||
              , mesh_no_rebroadcast = '1'
 | 
					              , mesh_no_rebroadcast = '1'
 | 
				
			||||||
              , macaddr = util.generate_mac(1, 1)
 | 
					              , macaddr = util.generate_mac(6)
 | 
				
			||||||
              , auto    = enable and 1 or 0
 | 
					              , auto    = enable and 1 or 0
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -128,7 +128,7 @@ uci:section('network', 'interface', 'mesh_vpn',
 | 
				
			|||||||
		  proto = 'batadv',
 | 
							  proto = 'batadv',
 | 
				
			||||||
		  mesh = 'bat0',
 | 
							  mesh = 'bat0',
 | 
				
			||||||
		  mesh_no_rebroadcast = 1,
 | 
							  mesh_no_rebroadcast = 1,
 | 
				
			||||||
		  macaddr = util.generate_mac(4, 0),
 | 
							  macaddr = util.generate_mac(0),
 | 
				
			||||||
	  }
 | 
						  }
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user