gluon-core: the mac address generation schema now uses a single parameter.

This commit is contained in:
lemoer 2016-04-23 13:41:57 +02:00
parent 16b14c5e57
commit f6592e720c
6 changed files with 39 additions and 20 deletions

View File

@ -21,6 +21,12 @@ local function configure_client(config, radio, index, suffix)
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
uci:section('wireless', 'wifi-iface', name,
{
@ -28,7 +34,7 @@ local function configure_client(config, radio, index, suffix)
network = 'client',
mode = 'ap',
ssid = config.ssid,
macaddr = util.generate_mac(2, index),
macaddr = macaddr,
ifname = suffix and 'client' .. suffix,
disabled = disabled,
}

View File

@ -72,35 +72,36 @@ function node_id()
end
-- Generates a (hopefully) unique MAC address
-- The first parameter defines the function and the second
-- parameter an ID to add to the MAC address
-- Functions and IDs defined so far:
-- (1, 0): WAN (for mesh-on-WAN)
-- (1, 1): LAN (for mesh-on-LAN)
-- (2, n): client interface for the n'th radio
-- (3, n): adhoc interface for n'th radio
-- (4, 0): mesh VPN
-- (5, n): mesh interface for n'th radio (802.11s)
function generate_mac(f, i)
-- The parameter defines the ID to add to the mac addr
--
-- IDs defined so far:
-- 0: client0; mesh-vpn
-- 1: mesh0
-- 2: ibss0
-- 3: client1; mesh-on-wan
-- 4: mesh1
-- 5: ibss1
-- 6: mesh-on-lan
-- 7: unused
function generate_mac(i)
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)')
m1 = tonumber(m1, 16)
m3 = tonumber(m3, 16)
m6 = tonumber(m6, 16)
m1 = nixio.bit.bor(m1, 0x02) -- set locally administered 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
-- not vary on a single interface, since some chips are using
-- 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 = (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
-- Iterate over all radios defined in UCI calling

View File

@ -5,7 +5,7 @@ local uci = require('luci.model.uci').cursor()
-- 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:commit('network')

View File

@ -53,6 +53,12 @@ local function configure_ibss(config, radio, index, suffix, disabled)
)
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,
{
device = radio,
@ -60,7 +66,7 @@ local function configure_ibss(config, radio, index, suffix, disabled)
mode = 'adhoc',
ssid = config.ssid,
bssid = config.bssid,
macaddr = util.generate_mac(3, index),
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = suffix and 'ibss' .. suffix,
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,
{
device = radio,
@ -92,7 +104,7 @@ local function configure_mesh(config, radio, index, suffix, disabled)
mode = 'mesh',
mesh_id = config.id,
mesh_fwding = 0,
macaddr = util.generate_mac(5, index),
macaddr = macaddr,
mcast_rate = config.mcast_rate,
ifname = suffix and 'mesh' .. suffix,
disabled = disabled and 1 or 0,

View File

@ -28,7 +28,7 @@ if sysconfig.lan_ifname and not uci:get('network', 'mesh_lan') then
, proto = 'batadv'
, mesh = 'bat0'
, mesh_no_rebroadcast = '1'
, macaddr = util.generate_mac(1, 1)
, macaddr = util.generate_mac(6)
, auto = enable and 1 or 0
})

View File

@ -128,7 +128,7 @@ uci:section('network', 'interface', 'mesh_vpn',
proto = 'batadv',
mesh = 'bat0',
mesh_no_rebroadcast = 1,
macaddr = util.generate_mac(4, 0),
macaddr = util.generate_mac(0),
}
)