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 7f63515332
commit afecfc43f6
3 changed files with 31 additions and 22 deletions

View File

@ -25,7 +25,11 @@ end
local os = os
local string = string
local require = require
local tonumber = tonumber
local nixio = require 'nixio'
local sysconfig = require 'gluon.sysconfig'
module 'gluon.util'
@ -53,6 +57,23 @@ function unlock(file)
end
function node_id()
local sysconfig = require 'gluon.sysconfig'
return string.gsub(sysconfig.primary_mac, ':', '')
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
local nixio = require 'nixio'
local sysconfig = require 'gluon.sysconfig'
local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor()
if sysconfig.wan_ifname:match('%.') and not uci:get('network', 'wan', 'macaddr') then
-- fix up duplicate mac addresses
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
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)
if sysconfig.wan_ifname:match('%.') then
-- fix up duplicate mac addresses (for mesh-on-WAN)
uci:set('network', 'wan', 'macaddr', util.generate_mac(1, 0))
uci:save('network')
uci:commit('network')
end
uci:save('network')
uci:commit('network')

View File

@ -1,10 +1,9 @@
#!/usr/bin/lua
local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig'
local users = require 'gluon.users'
local util = require 'gluon.util'
local nixio = require 'nixio'
local uci = require 'luci.model.uci'
local c = uci.cursor()
@ -64,18 +63,13 @@ c:save('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',
{
ifname = 'mesh-vpn',
proto = 'batadv',
mesh = 'bat0',
mesh_no_rebroadcast = 1,
macaddr = vpnaddr,
macaddr = util.generate_mac(4, 0),
}
)