Skip to content
Snippets Groups Projects
Commit b51e1063 authored by Matthias Schiffer's avatar Matthias Schiffer
Browse files

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

parent b977f001
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,11 @@ end ...@@ -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) ...@@ -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
#!/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')
#!/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') ...@@ -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),
} }
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment