Add utility function to generate unique MAC addresses derived from the primary MAC

This commit is contained in:
Matthias Schiffer 2014-09-15 18:55:22 +02:00
parent b977f00162
commit b51e1063b7
3 changed files with 31 additions and 22 deletions

View File

@ -25,7 +25,11 @@ end
local os = os local os = os
local string = string local string = string
local require = require local tonumber = tonumber
local nixio = require 'nixio'
local sysconfig = require 'gluon.sysconfig'
module 'gluon.util' module 'gluon.util'
@ -53,6 +57,23 @@ function unlock(file)
end end
function node_id() function node_id()
local sysconfig = require 'gluon.sysconfig'
return string.gsub(sysconfig.primary_mac, ':', '') return string.gsub(sysconfig.primary_mac, ':', '')
end 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, X): client interface for radioX
-- (3, X): adhoc interface for radioX
-- (4, 0): mesh VPN
function generate_mac(f, i)
local m1, m2, m3, m4, m5, m6 = string.match(sysconfig.primary_mac, '(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)')
m1 = nixio.bit.bor(tonumber(m1, 16), 0x02)
m2 = (tonumber(m2, 16)+f) % 0x100
m3 = (tonumber(m3, 16)+i) % 0x100
return string.format('%02x:%02x:%02x:%s:%s:%s', m1, m2, m3, m4, m5, m6)
end

View File

@ -1,20 +1,14 @@
#!/usr/bin/lua #!/usr/bin/lua
local nixio = require 'nixio'
local sysconfig = require 'gluon.sysconfig' local sysconfig = require 'gluon.sysconfig'
local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor() local uci = require('luci.model.uci').cursor()
if sysconfig.wan_ifname:match('%.') and not uci:get('network', 'wan', 'macaddr') then if sysconfig.wan_ifname:match('%.') then
-- fix up duplicate mac addresses -- fix up duplicate mac addresses (for mesh-on-WAN)
local m1, m2, m3, m4, m5, m6 = string.match(sysconfig.primary_mac, '(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)') uci:set('network', 'wan', 'macaddr', util.generate_mac(1, 0))
m1 = nixio.bit.bor(tonumber(m1, 16), 0x02) uci:save('network')
m4 = (tonumber(m4, 16)+1) % 0x100 uci:commit('network')
m6 = (tonumber(m6, 16)+1) % 0x100
local wanaddr = string.format('%02x:%s:%s:%02x:%s:%02x', m1, m2, m3, m4, m5, m6)
uci:set('network', 'wan', 'macaddr', wanaddr)
end end
uci:save('network')
uci:commit('network')

View File

@ -1,10 +1,9 @@
#!/usr/bin/lua #!/usr/bin/lua
local site = require 'gluon.site_config' local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig'
local users = require 'gluon.users' local users = require 'gluon.users'
local util = require 'gluon.util'
local nixio = require 'nixio'
local uci = require 'luci.model.uci' local uci = require 'luci.model.uci'
local c = uci.cursor() local c = uci.cursor()
@ -64,18 +63,13 @@ c:save('fastd')
c:commit('fastd') c:commit('fastd')
local m1, m2, m3, m4, m5, m6 = string.match(sysconfig.primary_mac, '(%x%x):(%x%x):(%x%x):(%x%x):(%x%x):(%x%x)')
m1 = nixio.bit.bor(tonumber(m1, 16), 0x02)
m4 = (tonumber(m4, 16)+1) % 0x100
local vpnaddr = string.format('%02x:%s:%s:%02x:%s:%s', m1, m2, m3, m4, m5, m6)
c:section('network', 'interface', 'mesh_vpn', c:section('network', 'interface', 'mesh_vpn',
{ {
ifname = 'mesh-vpn', ifname = 'mesh-vpn',
proto = 'batadv', proto = 'batadv',
mesh = 'bat0', mesh = 'bat0',
mesh_no_rebroadcast = 1, mesh_no_rebroadcast = 1,
macaddr = vpnaddr, macaddr = util.generate_mac(4, 0),
} }
) )